yencode 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/yencode.cc CHANGED
@@ -12,11 +12,6 @@
12
12
 
13
13
  using namespace v8;
14
14
 
15
- union crc32 {
16
- uint32_t u32;
17
- unsigned char u8a[4];
18
- };
19
-
20
15
  static void free_buffer(char* data, void* _size) {
21
16
  #if !NODE_VERSION_AT_LEAST(0, 11, 0)
22
17
  int size = (int)(size_t)_size;
@@ -252,7 +247,7 @@ FUNC(Decode) {
252
247
  isRaw = ARG_TO_BOOL(args[1]);
253
248
 
254
249
  unsigned char *result = (unsigned char*) malloc(arg_len);
255
- size_t len = (isRaw ? do_decode<true> : do_decode<false>)((const unsigned char*)node::Buffer::Data(args[0]), result, arg_len, NULL);
250
+ size_t len = do_decode(isRaw, (const unsigned char*)node::Buffer::Data(args[0]), result, arg_len, NULL);
256
251
  result = (unsigned char*)realloc(result, len);
257
252
  MARK_EXT_MEM(len);
258
253
  RETURN_VAL( NEW_BUFFER((char*)result, len, free_buffer, (void*)len) );
@@ -276,7 +271,7 @@ FUNC(DecodeTo) {
276
271
  if (args.Length() > 2)
277
272
  isRaw = ARG_TO_BOOL(args[2]);
278
273
 
279
- size_t len = (isRaw ? do_decode<true> : do_decode<false>)((const unsigned char*)node::Buffer::Data(args[0]), (unsigned char*)node::Buffer::Data(args[1]), arg_len, NULL);
274
+ size_t len = do_decode(isRaw, (const unsigned char*)node::Buffer::Data(args[0]), (unsigned char*)node::Buffer::Data(args[1]), arg_len, NULL);
280
275
  RETURN_VAL( Integer::New(ISOLATE len) );
281
276
  }
282
277
 
@@ -336,17 +331,23 @@ FUNC(DecodeIncr) {
336
331
  }
337
332
 
338
333
 
339
- #if NODE_VERSION_AT_LEAST(3, 0, 0)
340
- // for whatever reason, iojs 3 gives buffer corruption if you pass in a pointer without a free function
341
- #define RETURN_CRC(x) do { \
342
- Local<Object> buff = NEW_BUFFER(4); \
343
- memcpy(node::Buffer::Data(buff), &x.u32, sizeof(uint32_t)); \
344
- args.GetReturnValue().Set( buff ); \
345
- } while(0)
346
- #else
347
- #define RETURN_CRC(x) RETURN_VAL( NEW_BUFFER((char*)x.u8a, 4) )
334
+ static inline uint32_t read_crc32(const Local<Value>& buf) {
335
+ const uint8_t* arr = (const uint8_t*)node::Buffer::Data(buf);
336
+ return (((uint_fast32_t)arr[0] << 24) | ((uint_fast32_t)arr[1] << 16) | ((uint_fast32_t)arr[2] << 8) | (uint_fast32_t)arr[3]);
337
+ }
338
+ static inline Local<Object> pack_crc32(
339
+ #if NODE_VERSION_AT_LEAST(0, 11, 0)
340
+ Isolate* isolate,
348
341
  #endif
349
-
342
+ uint32_t crc) {
343
+ Local<Object> buff = NEW_BUFFER(4);
344
+ unsigned char* d = (unsigned char*)node::Buffer::Data(buff);
345
+ d[0] = (unsigned char)(crc >> 24) & 0xFF;
346
+ d[1] = (unsigned char)(crc >> 16) & 0xFF;
347
+ d[2] = (unsigned char)(crc >> 8) & 0xFF;
348
+ d[3] = (unsigned char)crc & 0xFF;
349
+ return buff;
350
+ }
350
351
 
351
352
  // crc32(str, init)
352
353
  FUNC(CRC32) {
@@ -356,25 +357,18 @@ FUNC(CRC32) {
356
357
  RETURN_ERROR("You must supply a Buffer");
357
358
  // TODO: support string args??
358
359
 
359
- union crc32 init;
360
- init.u32 = 0;
360
+ uint32_t crc = 0;
361
361
  if (args.Length() >= 2) {
362
362
  if (!node::Buffer::HasInstance(args[1]) || node::Buffer::Length(args[1]) != 4)
363
363
  RETURN_ERROR("Second argument must be a 4 byte buffer");
364
- memcpy(&init.u32, node::Buffer::Data(args[1]), sizeof(uint32_t));
365
- do_crc32_incremental(
366
- (const void*)node::Buffer::Data(args[0]),
367
- node::Buffer::Length(args[0]),
368
- init.u8a
369
- );
370
- } else {
371
- do_crc32(
372
- (const void*)node::Buffer::Data(args[0]),
373
- node::Buffer::Length(args[0]),
374
- init.u8a
375
- );
364
+ crc = read_crc32(args[1]);
376
365
  }
377
- RETURN_CRC(init);
366
+ crc = do_crc32(
367
+ (const void*)node::Buffer::Data(args[0]),
368
+ node::Buffer::Length(args[0]),
369
+ crc
370
+ );
371
+ RETURN_VAL(pack_crc32(ISOLATE crc));
378
372
  }
379
373
 
380
374
  FUNC(CRC32Combine) {
@@ -386,14 +380,11 @@ FUNC(CRC32Combine) {
386
380
  || !node::Buffer::HasInstance(args[1]) || node::Buffer::Length(args[1]) != 4)
387
381
  RETURN_ERROR("You must supply a 4 byte Buffer for the first two arguments");
388
382
 
389
- union crc32 crc1, crc2;
383
+ uint32_t crc1 = read_crc32(args[0]), crc2 = read_crc32(args[1]);
390
384
  size_t len = (size_t)ARG_TO_INT(args[2]);
391
385
 
392
- memcpy(&crc1.u32, node::Buffer::Data(args[0]), sizeof(uint32_t));
393
- memcpy(&crc2.u32, node::Buffer::Data(args[1]), sizeof(uint32_t));
394
-
395
- do_crc32_combine(crc1.u8a, crc2.u8a, len);
396
- RETURN_CRC(crc1);
386
+ crc1 = do_crc32_combine(crc1, crc2, len);
387
+ RETURN_VAL(pack_crc32(ISOLATE crc1));
397
388
  }
398
389
 
399
390
  FUNC(CRC32Zeroes) {
@@ -402,17 +393,15 @@ FUNC(CRC32Zeroes) {
402
393
  if (args.Length() < 1)
403
394
  RETURN_ERROR("At least 1 argument required");
404
395
 
405
- union crc32 crc1;
396
+ uint32_t crc1 = 0;
406
397
  if (args.Length() >= 2) {
407
398
  if (!node::Buffer::HasInstance(args[1]) || node::Buffer::Length(args[1]) != 4)
408
399
  RETURN_ERROR("Second argument must be a 4 byte buffer");
409
- memcpy(&crc1.u32, node::Buffer::Data(args[1]), sizeof(uint32_t));
410
- } else {
411
- crc1.u32 = 0;
400
+ crc1 = read_crc32(args[1]);
412
401
  }
413
402
  size_t len = (size_t)ARG_TO_INT(args[0]);
414
- do_crc32_zeros(crc1.u8a, len);
415
- RETURN_CRC(crc1);
403
+ crc1 = do_crc32_zeros(crc1, len);
404
+ RETURN_VAL(pack_crc32(ISOLATE crc1));
416
405
  }
417
406
 
418
407
  static void init_all() {