pierrec / node-lz4

LZ4 fast compression algorithm for NodeJS
MIT License
438 stars 98 forks source link

Why the compressed files of node-lz4 can't be decompressed by ./build/lz4.js? #96

Open Wonder233 opened 4 years ago

Wonder233 commented 4 years ago

hello everyone! I was confused by this.

  1. I run npm install lz4 in my project, and compress a file in node environment. Then, I decompress it in browser. But it works wrong.
  2. I run examples/compress.html and change it a little to get a compressed buffer. And then I run examples/decompress.html, it works.

So, why files compressed with the node version of lz4 cannot be decompressed with the js version of lz4? What I think is that no matter what lz4 tool is used to compress the file, the compressed file should finally be able to be decompressed.

Below is the node_version & js_version Buffer:

<script type="text/javascript" src="../build/lz4.js"></script>
<script type="text/javascript">
  var Buffer = require('buffer').Buffer
  var LZ4 = require('lz4')
  // LZ4 archive v1.4 compressed string: abc\n
  const jsVersion = [0x04,0x22,0x4d,0x18,0x40,0x70,0xdf,0xcf,0x00,0x00,0x00,0xf0,0x1c,0x5b,0x7b,0x22,
  0x66,0x69,0x6c,0x65,0x6e,0x61,0x6d,0x65,0x22,0x3a,0x22,0x74,0x6f,0x70,0x69,0x63,
  0x41,0x2e,0x31,0x35,0x39,0x31,0x31,0x36,0x37,0x30,0x30,0x39,0x2e,0x6c,0x6f,0x67,
  0x22,0x2c,0x22,0x73,0x6f,0x75,0x72,0x63,0x21,0x00,0xf0,0x08,0x31,0x32,0x37,0x2e,
  0x30,0x2e,0x30,0x2e,0x31,0x22,0x2c,0x22,0x6c,0x6f,0x67,0x73,0x22,0x3a,0x5b,0x7b,
  0x22,0x74,0x69,0x3d,0x00,0x06,0x35,0x00,0x92,0x2c,0x22,0x63,0x6f,0x6e,0x74,0x65,
  0x6e,0x74,0x1f,0x00,0x30,0x6b,0x65,0x79,0x5b,0x00,0xf0,0x03,0x79,0x70,0x65,0x70,
  0x6c,0x61,0x74,0x66,0x6f,0x72,0x6d,0x22,0x2c,0x22,0x76,0x61,0x6c,0x75,0x51,0x00,
  0xc2,0x77,0x69,0x6e,0x64,0x6f,0x77,0x73,0x22,0x7d,0x2c,0x7b,0x22,0x29,0x00,0x77,
  0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x24,0x00,0x57,0x32,0x2e,0x31,0x2e,0x30,0x22,
  0x00,0x28,0x75,0x69,0x1e,0x00,0x97,0x34,0x35,0x39,0x34,0x35,0x32,0x33,0x37,0x32,
  0x22,0x00,0x47,0x74,0x79,0x70,0x65,0x41,0x00,0xc7,0x75,0x70,0x6c,0x6f,0x61,0x64,
  0x5f,0x65,0x72,0x72,0x6f,0x72,0x26,0x00,0x57,0x69,0x6e,0x64,0x65,0x78,0x27,0x00,
  0x90,0x31,0x31,0x22,0x7d,0x5d,0x7d,0x5d,0x7d,0x5d,0x00,0x00,0x00,0x00]

  const nodeVersion = [0xf0,0x1c,0x5b,0x7b,0x22,0x66,0x69,0x6c,0x65,0x6e,0x61,0x6d,0x65,0x22,0x3a,0x22,
  0x74,0x6f,0x70,0x69,0x63,0x41,0x2e,0x31,0x35,0x39,0x31,0x31,0x36,0x37,0x30,0x30,
  0x39,0x2e,0x6c,0x6f,0x67,0x22,0x2c,0x22,0x73,0x6f,0x75,0x72,0x63,0x21,0x00,0xf0,
  0x08,0x31,0x32,0x37,0x2e,0x30,0x2e,0x30,0x2e,0x31,0x22,0x2c,0x22,0x6c,0x6f,0x67,
  0x73,0x22,0x3a,0x5b,0x7b,0x22,0x74,0x69,0x3d,0x00,0x06,0x35,0x00,0x92,0x2c,0x22,
  0x63,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x1f,0x00,0x30,0x6b,0x65,0x79,0x5b,0x00,0xf0,
  0x03,0x79,0x70,0x65,0x70,0x6c,0x61,0x74,0x66,0x6f,0x72,0x6d,0x22,0x2c,0x22,0x76,
  0x61,0x6c,0x75,0x51,0x00,0xa4,0x77,0x69,0x6e,0x64,0x6f,0x77,0x73,0x22,0x7d,0x2c,
  0x29,0x00,0x77,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x24,0x00,0x57,0x32,0x2e,0x31,
  0x2e,0x30,0x22,0x00,0x28,0x75,0x69,0x1e,0x00,0x97,0x34,0x35,0x39,0x34,0x35,0x32,
  0x33,0x37,0x32,0x22,0x00,0x47,0x74,0x79,0x70,0x65,0x41,0x00,0xc7,0x75,0x70,0x6c,
  0x6f,0x61,0x64,0x5f,0x65,0x72,0x72,0x6f,0x72,0x26,0x00,0x57,0x69,0x6e,0x64,0x65,
  0x78,0x27,0x00,0x90,0x31,0x31,0x22,0x7d,0x5d,0x7d,0x5d,0x7d,0x5d]

  var jscompressed = new Buffer(jsVersion)
  console.log( "js decode:\n", LZ4.decode(jscompressed).toString() )

  var nodecompressed = new Buffer(nodeVersion)
  console.log( "node decode:\n", LZ4.decode(nodecompressed).toString() )

</script>

What's more, I use c/c++ LZ4: https://github.com/lz4/lz4 to decompress these two Buffer. The nodeVersion Buffer can be decompressed by using function LZ4_decompress_safe when the jsVersion can't.

Wonder233 commented 4 years ago

The result of running in the browser is as follows: image

The console says "Uncaught Error: Invalid magic number: 7B5B1CF0 @0".