@@ -1122,42 +1122,50 @@ const BINARY_SIZE_16_BIT = 4;
1122
1122
const BINARY_SIZE_32_BIT = 8 ;
1123
1123
const BINARY_SIZE_64_BIT = 12 ;
1124
1124
1125
- const isBigEndian = new Uint8Array ( new Uint32Array ( [ 0x12345678 ] ) . buffer ) [ 0 ] === 0x12 ;
1125
+ const isNativelyBigEndian = new Uint8Array ( new Uint32Array ( [ 0x12345678 ] ) . buffer ) [ 0 ] === 0x12 ;
1126
1126
1127
-
1128
- function writeIntToBuffer ( isSigned , buffer , offset , value , flags ) {
1127
+ function isBigEndianFromFlags ( flags ) {
1129
1128
const endianFlags = flags & BINARY_ENDIAN_MASK ;
1130
- const sizeFlags = flags & ~ BINARY_ENDIAN_MASK ;
1131
-
1132
- let sizeInBytes ;
1129
+ return ( endianFlags === BINARY_ENDIAN_BIG || ( endianFlags == 0 && isBigEndian ) ) ;
1130
+ }
1133
1131
1132
+ function sizeFromFlags ( flags ) {
1133
+ const sizeFlags = flags & ~ BINARY_ENDIAN_MASK ;
1134
1134
if ( sizeFlags == BINARY_SIZE_8_BIT ) {
1135
- sizeInBytes = 1 ;
1135
+ return 1 ;
1136
1136
} else if ( sizeFlags == BINARY_SIZE_16_BIT ) {
1137
- sizeInBytes = 2 ;
1137
+ return 2 ;
1138
1138
} else if ( sizeFlags == BINARY_SIZE_32_BIT ) {
1139
- sizeInBytes = 4 ;
1139
+ return 4 ;
1140
1140
} else if ( sizeFlags == BINARY_SIZE_64_BIT ) {
1141
1141
throw new NQPException ( '64bit writeint is not supported' ) ;
1142
1142
} else {
1143
1143
throw new NQPException ( 'unsupported flags: ' + flags ) ;
1144
1144
}
1145
+ }
1145
1146
1147
+ function writeIntToBuffer ( isSigned , buffer , offset , value , flags ) {
1148
+
1149
+ const sizeInBytes = sizeFromFlags ( flags ) ;
1146
1150
const lowlevelBuffer = Buffer . alloc ( sizeInBytes ) ;
1147
1151
1152
+ const isBigEndian = isBigEndianFromFlags ( flags ) ;
1153
+
1148
1154
const shift = 32 - sizeInBytes * 8 ;
1149
1155
1150
- if ( endianFlags === BINARY_ENDIAN_BIG || ( endianFlags == 0 && isBigEndian ) ) {
1151
- if ( isSigned ) {
1152
- lowlevelBuffer . writeIntBE ( ( value << shift >> shift ) , 0 , sizeInBytes ) ;
1156
+ if ( isSigned ) {
1157
+ const wrapped = value << shift >> shift ;
1158
+ if ( isBigEndian ) {
1159
+ lowlevelBuffer . writeIntBE ( wrapped , 0 , sizeInBytes ) ;
1153
1160
} else {
1154
- lowlevelBuffer . writeUIntBE ( ( value << shift >>> shift ) , 0 , sizeInBytes ) ;
1161
+ lowlevelBuffer . writeIntLE ( wrapped , 0 , sizeInBytes ) ;
1155
1162
}
1156
- } else if ( endianFlags === BINARY_ENDIAN_LITTLE || ( endianFlags == 0 && ! isBigEndian ) ) {
1157
- if ( isSigned ) {
1158
- lowlevelBuffer . writeIntLE ( ( value << shift >> shift ) , 0 , sizeInBytes ) ;
1163
+ } else {
1164
+ const wrapped = value << shift >>> shift ;
1165
+ if ( isBigEndian ) {
1166
+ lowlevelBuffer . writeUIntBE ( wrapped , 0 , sizeInBytes ) ;
1159
1167
} else {
1160
- lowlevelBuffer . writeUIntLE ( ( value << shift >>> shift ) , 0 , sizeInBytes ) ;
1168
+ lowlevelBuffer . writeUIntLE ( wrapped , 0 , sizeInBytes ) ;
1161
1169
}
1162
1170
}
1163
1171
@@ -1172,6 +1180,12 @@ op.writeuint = function(buffer, offset, value, flags) {
1172
1180
writeIntToBuffer ( false , buffer , offset , value , flags ) ;
1173
1181
} ;
1174
1182
1183
+ op . readuint = function ( buffer , offset , flags ) {
1184
+ const sizeInBytes = sizeFromFlags ( flags ) ;
1185
+ const rawData = rawSlice ( buffer , offset , offset + sizeInBytes / byteSize ( buffer ) ) ;
1186
+ return isBigEndianFromFlags ( flags ) ? rawData . readUIntBE ( 0 , sizeInBytes ) : rawData . readUIntLE ( 0 , sizeInBytes ) ;
1187
+ } ;
1188
+
1175
1189
op . encodeconf = function ( str , encoding_ , output , permissive ) {
1176
1190
if ( output . array . length ) {
1177
1191
throw new NQPException ( 'encode requires an empty array' ) ;
@@ -1222,14 +1236,18 @@ op.encoderep = function(str, encoding, replacement, output) {
1222
1236
} ;
1223
1237
1224
1238
function toRawBuffer ( buf ) {
1239
+ return rawSlice ( buf , 0 , buf . array . length ) ;
1240
+ }
1241
+
1242
+ function rawSlice ( buf , start , end ) {
1225
1243
const elementSize = byteSize ( buf ) ;
1226
1244
const isUnsigned = buf . $$STable . REPR . type . $$STable . REPR . isUnsigned ;
1227
1245
const array = buf . array ;
1228
1246
1229
1247
const buffer = Buffer . allocUnsafe ( array . length * elementSize ) ;
1230
1248
1231
1249
let offset = 0 ;
1232
- for ( let i = 0 ; i < array . length ; i ++ ) {
1250
+ for ( let i = start ; i < end ; i ++ ) {
1233
1251
if ( isUnsigned ) {
1234
1252
buffer . writeUIntLE ( array [ i ] , offset , elementSize ) ;
1235
1253
} else {
0 commit comments