@@ -142,12 +142,25 @@ public void __init__(CodeContext/*!*/ context, object fmt) {
142142 break ;
143143 case FormatType . UnsignedInt :
144144 for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
145- WriteUInt ( res , _isLittleEndian , GetULongValue ( context , curObj ++ , values ) ) ;
145+ WriteUInt ( res , _isLittleEndian , GetUIntValue ( context , curObj ++ , values ) ) ;
146+ }
147+ break ;
148+ case FormatType . Long :
149+ for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
150+ if ( _isStandardized || TypecodeOps . IsCLong32Bit ) {
151+ WriteInt ( res , _isLittleEndian , GetIntValue ( context , curObj ++ , values ) ) ;
152+ } else {
153+ WriteLong ( res , _isLittleEndian , GetLongValue ( context , curObj ++ , values ) ) ;
154+ }
146155 }
147156 break ;
148157 case FormatType . UnsignedLong :
149158 for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
150- WriteUInt ( res , _isLittleEndian , GetULongValue ( context , curObj ++ , values ) ) ;
159+ if ( _isStandardized || TypecodeOps . IsCLong32Bit ) {
160+ WriteUInt ( res , _isLittleEndian , GetUIntValue ( context , curObj ++ , values ) ) ;
161+ } else {
162+ WriteULong ( res , _isLittleEndian , GetULongValue ( context , curObj ++ , values ) ) ;
163+ }
151164 }
152165 break ;
153166 case FormatType . LongLong :
@@ -310,11 +323,28 @@ public void pack_into(CodeContext/*!*/ context, [NotNone] IBufferProtocol/*!*/ b
310323 }
311324 break ;
312325 case FormatType . UnsignedInt :
313- case FormatType . UnsignedLong :
314326 for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
315327 res [ res_idx ++ ] = BigIntegerOps . __int__ ( CreateUIntValue ( context , ref curIndex , _isLittleEndian , data ) ) ;
316328 }
317329 break ;
330+ case FormatType . Long :
331+ for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
332+ if ( _isStandardized || TypecodeOps . IsCLong32Bit ) {
333+ res [ res_idx ++ ] = CreateIntValue ( context , ref curIndex , _isLittleEndian , data ) ;
334+ } else {
335+ res [ res_idx ++ ] = BigIntegerOps . __int__ ( CreateLongValue ( context , ref curIndex , _isLittleEndian , data ) ) ;
336+ }
337+ }
338+ break ;
339+ case FormatType . UnsignedLong :
340+ for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
341+ if ( _isStandardized || TypecodeOps . IsCLong32Bit ) {
342+ res [ res_idx ++ ] = BigIntegerOps . __int__ ( CreateUIntValue ( context , ref curIndex , _isLittleEndian , data ) ) ;
343+ } else {
344+ res [ res_idx ++ ] = BigIntegerOps . __int__ ( CreateULongValue ( context , ref curIndex , _isLittleEndian , data ) ) ;
345+ }
346+ }
347+ break ;
318348 case FormatType . LongLong :
319349 for ( int j = 0 ; j < curFormat . Count ; j ++ ) {
320350 res [ res_idx ++ ] = BigIntegerOps . __int__ ( CreateLongValue ( context , ref curIndex , _isLittleEndian , data ) ) ;
@@ -472,14 +502,17 @@ private static Struct CompileAndCache(CodeContext/*!*/ context, string/*!*/ fmt)
472502 count = 1 ;
473503 break ;
474504 case 'i' : // int
475- case 'l' : // long
476505 res . Add ( new Format ( FormatType . Int , count ) ) ;
477506 count = 1 ;
478507 break ;
479508 case 'I' : // unsigned int
480509 res . Add ( new Format ( FormatType . UnsignedInt , count ) ) ;
481510 count = 1 ;
482511 break ;
512+ case 'l' : // long
513+ res . Add ( new Format ( FormatType . Long , count ) ) ;
514+ count = 1 ;
515+ break ;
483516 case 'L' : // unsigned long
484517 res . Add ( new Format ( FormatType . UnsignedLong , count ) ) ;
485518 count = 1 ;
@@ -624,7 +657,7 @@ private void InitCountAndSize() {
624657 encodingSize = Align ( encodingSize , format . NativeSize ) ;
625658 }
626659
627- encodingSize += GetNativeSize ( format . Type ) * format . Count ;
660+ encodingSize += GetNativeSize ( format . Type , _isStandardized ) * format . Count ;
628661 }
629662 _encodingCount = encodingCount ;
630663 _encodingSize = encodingSize ;
@@ -729,6 +762,7 @@ private enum FormatType {
729762
730763 Int,
731764 UnsignedInt ,
765+ Long ,
732766 UnsignedLong ,
733767 Float ,
734768
@@ -747,7 +781,7 @@ private enum FormatType {
747781 SizeT ,
748782 }
749783
750- private static int GetNativeSize ( FormatType c ) {
784+ private static int GetNativeSize ( FormatType c , bool isStandardized ) {
751785 switch ( c ) {
752786 case FormatType . Char :
753787 case FormatType . SignedChar :
@@ -765,11 +799,13 @@ private static int GetNativeSize(FormatType c) {
765799 return 2 ;
766800 case FormatType . Int :
767801 case FormatType . UnsignedInt :
768- case FormatType . UnsignedLong :
769802 case FormatType . Float :
770803 case FormatType . SignedSizeT :
771804 case FormatType . SizeT :
772805 return 4 ;
806+ case FormatType . Long :
807+ case FormatType . UnsignedLong :
808+ return isStandardized || TypecodeOps . IsCLong32Bit ? 4 : 8 ;
773809 case FormatType . LongLong :
774810 case FormatType . UnsignedLongLong :
775811 case FormatType . Double :
@@ -786,16 +822,11 @@ private static int GetNativeSize(FormatType c) {
786822 /// <summary>
787823 /// Struct used to store the format and the number of times it should be repeated.
788824 /// </summary>
789- private readonly struct Format {
790- public readonly FormatType Type ;
791- public readonly int Count ;
792-
793- public Format ( FormatType type , int count ) {
794- Type = type ;
795- Count = count ;
796- }
825+ private readonly struct Format ( FormatType type , int count ) {
826+ public readonly FormatType Type = type ;
827+ public readonly int Count = count ;
797828
798- public int NativeSize => GetNativeSize ( Type ) ;
829+ public int NativeSize => GetNativeSize ( Type , isStandardized : false ) ;
799830 }
800831
801832 #endregion
@@ -1106,19 +1137,33 @@ internal static int GetIntValue(CodeContext/*!*/ context, int index, object[] ar
11061137 return res ;
11071138 }
11081139
1109- internal static uint GetULongValue ( CodeContext /*!*/ context , int index , object [ ] args ) {
1140+ internal static uint GetUIntValue ( CodeContext /*!*/ context , int index , object [ ] args ) {
11101141 BigInteger val = GetIntegerValue ( context , index , args ) ;
11111142 if ( ! val . AsUInt32 ( out uint res ) )
11121143 throw Error ( context , "argument out of range" ) ;
11131144 return res ;
11141145 }
11151146
1147+ internal static long GetLongValue ( CodeContext /*!*/ context , int index , object [ ] args ) {
1148+ BigInteger val = GetIntegerValue ( context , index , args ) ;
1149+ if ( ! val . AsInt64 ( out long res ) )
1150+ throw Error ( context , "argument out of range" ) ;
1151+ return res ;
1152+ }
1153+
1154+ internal static ulong GetULongValue ( CodeContext /*!*/ context , int index , object [ ] args ) {
1155+ BigInteger val = GetIntegerValue ( context , index , args ) ;
1156+ if ( ! val . AsUInt64 ( out ulong res ) )
1157+ throw Error ( context , "argument out of range" ) ;
1158+ return res ;
1159+ }
1160+
11161161 internal static int GetSignedSizeT ( CodeContext /*!*/ context , int index , object [ ] args ) {
11171162 return GetIntValue ( context , index , args ) ;
11181163 }
11191164
11201165 internal static uint GetSizeT ( CodeContext /*!*/ context , int index , object [ ] args ) {
1121- return GetULongValue ( context , index , args ) ;
1166+ return GetUIntValue ( context , index , args ) ;
11221167 }
11231168
11241169 internal static ulong GetPointer ( CodeContext /*!*/ context , int index , object [ ] args ) {
0 commit comments