@@ -229,20 +229,20 @@ static void one_to_buffer(size_t sz, unsigned char *buf, uint64_t val) {
229229 significant bits first. This allows 32-bit and 64-bit architectures to
230230 interchange serialized HashContexts. */
231231
232- PHP_HASH_API zend_result php_hash_serialize_spec (const php_hashcontext_object * hash , zval * zv , const char * spec ) /* {{{ */
232+ PHP_HASH_API hash_spec_result php_hash_serialize_spec (const php_hashcontext_object * hash , zval * zv , const char * spec ) /* {{{ */
233233{
234234 size_t pos = 0 , max_alignment = 1 ;
235235 unsigned char * buf = (unsigned char * ) hash -> context ;
236236 zval tmp ;
237237 if (buf == NULL ) {
238- return FAILURE ;
238+ return HASH_SPEC_FAILURE ;
239239 }
240240 array_init (zv );
241241 while (* spec != '\0' && * spec != '.' ) {
242242 char spec_ch = * spec ;
243243 size_t sz , count = parse_serialize_spec (& spec , & pos , & sz , & max_alignment );
244244 if (pos + count * sz > hash -> ops -> context_size ) {
245- return FAILURE ;
245+ return HASH_SPEC_FAILURE ;
246246 }
247247 if (isupper ((unsigned char ) spec_ch )) {
248248 pos += count * sz ;
@@ -265,38 +265,33 @@ PHP_HASH_API zend_result php_hash_serialize_spec(const php_hashcontext_object *h
265265 }
266266 }
267267 if (* spec == '.' && align_to (pos , max_alignment ) != hash -> ops -> context_size ) {
268- return FAILURE ;
268+ return HASH_SPEC_FAILURE ;
269269 }
270- return SUCCESS ;
270+ return HASH_SPEC_SUCCESS ;
271271}
272272/* }}} */
273273
274- /* Unserialize a hash context serialized by `php_hash_serialize_spec` with `spec`.
275- Returns SUCCESS on success and a negative error code on failure.
276- Codes: FAILURE (-1) == generic failure
277- -999 == spec wrong size for context
278- -1000 - POS == problem at byte offset POS */
279-
280- PHP_HASH_API int php_hash_unserialize_spec (php_hashcontext_object * hash , const zval * zv , const char * spec ) /* {{{ */
274+ /* Unserialize a hash context serialized by `php_hash_serialize_spec` with `spec`. */
275+ PHP_HASH_API hash_spec_result php_hash_unserialize_spec (php_hashcontext_object * hash , const zval * zv , const char * spec ) /* {{{ */
281276{
282277 size_t pos = 0 , max_alignment = 1 , j = 0 ;
283278 unsigned char * buf = (unsigned char * ) hash -> context ;
284279 zval * elt ;
285280 if (Z_TYPE_P (zv ) != IS_ARRAY ) {
286- return FAILURE ;
281+ return HASH_SPEC_FAILURE ;
287282 }
288283 while (* spec != '\0' && * spec != '.' ) {
289284 char spec_ch = * spec ;
290285 size_t sz , count = parse_serialize_spec (& spec , & pos , & sz , & max_alignment );
291286 if (pos + count * sz > hash -> ops -> context_size ) {
292- return -999 ;
287+ return WRONG_CONTEXT_SIZE ;
293288 }
294289 if (isupper ((unsigned char ) spec_ch )) {
295290 pos += count * sz ;
296291 } else if (sz == 1 && count > 1 ) {
297292 elt = zend_hash_index_find (Z_ARRVAL_P (zv ), j );
298293 if (!elt || Z_TYPE_P (elt ) != IS_STRING || Z_STRLEN_P (elt ) != count ) {
299- return -1000 - pos ;
294+ return BYTE_OFFSET_POS_ERROR - pos ;
300295 }
301296 ++ j ;
302297 memcpy (buf + pos , Z_STRVAL_P (elt ), count );
@@ -306,14 +301,14 @@ PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const z
306301 uint64_t val ;
307302 elt = zend_hash_index_find (Z_ARRVAL_P (zv ), j );
308303 if (!elt || Z_TYPE_P (elt ) != IS_LONG ) {
309- return -1000 - pos ;
304+ return BYTE_OFFSET_POS_ERROR - pos ;
310305 }
311306 ++ j ;
312307 val = (uint32_t ) Z_LVAL_P (elt );
313308 if (sz == 8 ) {
314309 elt = zend_hash_index_find (Z_ARRVAL_P (zv ), j );
315310 if (!elt || Z_TYPE_P (elt ) != IS_LONG ) {
316- return -1000 - pos ;
311+ return BYTE_OFFSET_POS_ERROR - pos ;
317312 }
318313 ++ j ;
319314 val += ((uint64_t ) Z_LVAL_P (elt )) << 32 ;
@@ -325,31 +320,32 @@ PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const z
325320 }
326321 }
327322 if (* spec == '.' && align_to (pos , max_alignment ) != hash -> ops -> context_size ) {
328- return -999 ;
323+ return WRONG_CONTEXT_SIZE ;
329324 }
330- return SUCCESS ;
325+
326+ return HASH_SPEC_SUCCESS ;
331327}
332328/* }}} */
333329
334- PHP_HASH_API zend_result php_hash_serialize (const php_hashcontext_object * hash , zend_long * magic , zval * zv ) /* {{{ */
330+ PHP_HASH_API hash_spec_result php_hash_serialize (const php_hashcontext_object * hash , zend_long * magic , zval * zv ) /* {{{ */
335331{
336- if (hash -> ops -> serialize_spec ) {
337- * magic = PHP_HASH_SERIALIZE_MAGIC_SPEC ;
338- return php_hash_serialize_spec ( hash , zv , hash -> ops -> serialize_spec );
339- } else {
340- return FAILURE ;
341- }
332+ if (! hash -> ops -> serialize_spec ) {
333+ return HASH_SPEC_FAILURE ;
334+ }
335+
336+ * magic = PHP_HASH_SERIALIZE_MAGIC_SPEC ;
337+ return php_hash_serialize_spec ( hash , zv , hash -> ops -> serialize_spec );
342338}
343339/* }}} */
344340
345- PHP_HASH_API int php_hash_unserialize (php_hashcontext_object * hash , zend_long magic , const zval * zv ) /* {{{ */
341+ PHP_HASH_API hash_spec_result php_hash_unserialize (php_hashcontext_object * hash , zend_long magic , const zval * zv ) /* {{{ */
346342{
347343 if (hash -> ops -> serialize_spec
348344 && magic == PHP_HASH_SERIALIZE_MAGIC_SPEC ) {
349345 return php_hash_unserialize_spec (hash , zv , hash -> ops -> serialize_spec );
350- } else {
351- return FAILURE ;
352346 }
347+
348+ return HASH_SPEC_FAILURE ;
353349}
354350/* }}} */
355351
@@ -1475,7 +1471,7 @@ PHP_METHOD(HashContext, __serialize)
14751471 ZVAL_LONG (& tmp , hash -> options );
14761472 zend_hash_next_index_insert (Z_ARRVAL_P (return_value ), & tmp );
14771473
1478- if (hash -> ops -> hash_serialize (hash , & magic , & tmp ) != SUCCESS ) {
1474+ if (hash -> ops -> hash_serialize (hash , & magic , & tmp ) != HASH_SPEC_SUCCESS ) {
14791475 goto serialize_failure ;
14801476 }
14811477 zend_hash_next_index_insert (Z_ARRVAL_P (return_value ), & tmp );
@@ -1504,7 +1500,7 @@ PHP_METHOD(HashContext, __unserialize)
15041500 HashTable * data ;
15051501 zval * algo_zv , * magic_zv , * options_zv , * hash_zv , * members_zv ;
15061502 zend_long magic , options ;
1507- int unserialize_result ;
1503+ hash_spec_result unserialize_result ;
15081504 const php_hash_ops * ops ;
15091505
15101506 if (zend_parse_parameters (ZEND_NUM_ARGS (), "h" , & data ) == FAILURE ) {
@@ -1553,7 +1549,7 @@ PHP_METHOD(HashContext, __unserialize)
15531549 ops -> hash_init (hash -> context , NULL );
15541550
15551551 unserialize_result = ops -> hash_unserialize (hash , magic , hash_zv );
1556- if (unserialize_result != SUCCESS ) {
1552+ if (unserialize_result != HASH_SPEC_SUCCESS ) {
15571553 zend_throw_exception_ex (NULL , 0 , "Incomplete or ill-formed serialization data (\"%s\" code %d)" , ops -> algo , unserialize_result );
15581554 /* free context */
15591555 php_hashcontext_dtor (Z_OBJ_P (object ));
0 commit comments