@@ -313,20 +313,39 @@ macro_rules! sign_bit {
313
313
} ;
314
314
}
315
315
316
- macro_rules! unpack_px {
316
+ macro_rules! unpack_ux {
317
317
( $ux: ident, $input: expr, $n: expr) => { {
318
318
let ( input, bytes) = parse_bytes( $input, $n) ?;
319
319
let mut arr = [ 0u8 ; :: std:: mem:: size_of:: <$ux>( ) ] ;
320
320
( & mut arr[ ( :: std:: mem:: size_of:: <$ux>( ) - $n) ..] ) . copy_from_slice( bytes) ;
321
321
( input, $ux:: from_be_bytes( arr) )
322
322
} } ;
323
323
}
324
+
325
+ macro_rules! unpack_px {
326
+ ( $ix: ident, $ux: ident, $input: expr, $n: expr) => { {
327
+ let ( input, bytes) = parse_bytes( $input, $n) ?;
328
+ let mut arr = [ 0u8 ; :: std:: mem:: size_of:: <$ux>( ) ] ;
329
+ ( & mut arr[ ( :: std:: mem:: size_of:: <$ux>( ) - $n) ..] ) . copy_from_slice( bytes) ;
330
+ let x = $ix:: from_be_bytes( arr) ;
331
+ if x < 0 {
332
+ Err ( PackError :: UnsupportedIntLength )
333
+ } else {
334
+ Ok ( ( input, x) )
335
+ }
336
+ } } ;
337
+ }
324
338
macro_rules! unpack_nx {
325
- ( $ix: ident, $input: expr, $n: expr) => { {
339
+ ( $ix: ident, $ux : ident , $ input: expr, $n: expr) => { {
326
340
let ( input, bytes) = parse_bytes( $input, $n) ?;
327
341
let mut arr = [ 0xffu8 ; :: std:: mem:: size_of:: <$ix>( ) ] ;
328
342
( & mut arr[ ( :: std:: mem:: size_of:: <$ix>( ) - $n) ..] ) . copy_from_slice( bytes) ;
329
- ( input, $ix:: from_be_bytes( arr) . wrapping_add( 1 ) )
343
+ let x = $ix:: from_be_bytes( arr) . wrapping_add( 1 ) ;
344
+ if x > 0 {
345
+ Err ( PackError :: UnsupportedIntLength )
346
+ } else {
347
+ Ok ( ( input, x) )
348
+ }
330
349
} } ;
331
350
}
332
351
@@ -363,14 +382,14 @@ macro_rules! impl_ux {
363
382
let ( input, found) = parse_byte( input) ?;
364
383
if INTZERO <= found && found <= INTZERO + $max_sz as u8 {
365
384
let n = ( found - INTZERO ) as usize ;
366
- Ok ( unpack_px !( $ux, input, n) )
385
+ Ok ( unpack_ux !( $ux, input, n) )
367
386
} else if found == POSINTEND {
368
387
let ( input, raw_length) = parse_byte( input) ?;
369
388
let n: usize = usize :: from( raw_length) ;
370
389
if n > SZ {
371
390
return Err ( PackError :: UnsupportedIntLength ) ;
372
391
}
373
- Ok ( unpack_px !( $ux, input, n) )
392
+ Ok ( unpack_ux !( $ux, input, n) )
374
393
} else {
375
394
Err ( PackError :: BadCode {
376
395
found,
@@ -427,24 +446,24 @@ macro_rules! impl_ix {
427
446
let ( input, found) = parse_byte( input) ?;
428
447
if INTZERO <= found && found <= INTZERO + $max_sz as u8 {
429
448
let n = ( found - INTZERO ) as usize ;
430
- Ok ( unpack_px!( $ix, input, n) )
449
+ unpack_px!( $ix, $ux , input, n)
431
450
} else if INTZERO - $max_sz as u8 <= found && found < INTZERO {
432
451
let n = ( INTZERO - found) as usize ;
433
- Ok ( unpack_nx!( $ix, input, n) )
452
+ unpack_nx!( $ix, $ux , input, n)
434
453
} else if found == NEGINTSTART {
435
454
let ( input, raw_length) = parse_byte( input) ?;
436
455
let n = usize :: from( raw_length ^ 0xff ) ;
437
456
if n > SZ {
438
457
return Err ( PackError :: UnsupportedIntLength ) ;
439
458
}
440
- Ok ( unpack_nx!( $ix, input, n) )
459
+ unpack_nx!( $ix, $ux , input, n)
441
460
} else if found == POSINTEND {
442
461
let ( input, raw_length) = parse_byte( input) ?;
443
462
let n: usize = usize :: from( raw_length) ;
444
463
if n > SZ {
445
464
return Err ( PackError :: UnsupportedIntLength ) ;
446
465
}
447
- Ok ( unpack_px!( $ix, input, n) )
466
+ unpack_px!( $ix, $ux , input, n)
448
467
} else {
449
468
Err ( PackError :: BadCode {
450
469
found,
@@ -961,10 +980,15 @@ impl<'de> TupleUnpack<'de> for Element<'de> {
961
980
let ( input, v) = Vec :: < Self > :: unpack ( input, tuple_depth) ?;
962
981
( input, Element :: Tuple ( v) )
963
982
}
964
- INTMIN ..=INTMAX => {
965
- let ( input, v) = i64:: unpack ( input, tuple_depth) ?;
966
- ( input, Element :: Int ( v) )
967
- }
983
+ INTMIN ..=INTMAX => match i64:: unpack ( input, tuple_depth) {
984
+ Ok ( ( input, v) ) => ( input, Element :: Int ( v) ) ,
985
+ #[ cfg( feature = "num-bigint" ) ]
986
+ Err ( PackError :: UnsupportedIntLength ) => {
987
+ let ( input, v) = num_bigint:: BigInt :: unpack ( input, tuple_depth) ?;
988
+ ( input, Element :: BigInt ( v) )
989
+ }
990
+ Err ( err) => return Err ( err) ,
991
+ } ,
968
992
#[ cfg( feature = "num-bigint" ) ]
969
993
NEGINTSTART => {
970
994
let ( input, v) = num_bigint:: BigInt :: unpack ( input, tuple_depth) ?;
0 commit comments