nodeca / pako

high speed zlib port to javascript, works in browser & node.js

Home Page:http://nodeca.github.io/pako/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect header check with zlib header

tsupinie opened this issue · comments

Hello all,

I think I've found a bug in decompressing this data file. (It's an old data file that I did not create, and I had to do a lot of digging and guessing to figure out what the hell it was.) It appears to have a 2-byte zlib header (78 DA). From my reading through the docs and the code, it seems that by default pako.inflate() should auto-detect whether there's a zlib header as opposed to a gzip header. However, I get an incorrect header check error when doing pako.inflate().

const pako = require('pako');

let input = require('fs').readFileSync('KPAH_SDUS33_NVWVWX_200511060006');
let output = pako.inflate(input); // error: incorrect header check

The standard Python zlib library has no problems with it.

import zlib
with open('KPAH_SDUS33_NVWVWX_200511060006', 'rb') as finput:
    input = finput.read()
output = zlib.decompress(input)
print(len(output)) # 4000

For my specific application, I think I can get away with removing the zlib header and using pako.inflateRaw(), but it seems like pako.inflate() should be able to handle it.

Can this be a dupe of #174 (comment) (header set wrong window size)?

@tsupinie I noticed a similar issue but it only happens with the minimized version of pako. Can you check your file with the minimized vs non-minimized version and report back?

#260

🙏🏼

I'm not sure if this relates, but I try to serialize and de-serialize plain javascript objects to a compressed base64 url safe string and back, but got the same issues (using NextJS13).

Using:

// Dependencies
"@types/pako": "^2.0.0",

// AND Development dependencies
"next": "^13.1.1",
"pako": "^2.1.0",

something like:

'use client';
import { deflate, inflate } from 'pako';
import { plainToInstance, instanceToPlain } from 'class-transformer';
import { encode, decode } from 'js-base64';
import { DTO } from './dto';

/**
 * Serializes an object to compressed base64 string
 */
export function serializeState<A extends DTO>(appState: A) {
  const plain = instanceToPlain(appState);
  const jsonString = JSON.stringify(plain);

  const compressed = deflate(jsonString);
  const compressedString = new window.TextDecoder().decode(compressed)

  return encode(compressedString, true);
}

/**
 * De-Serializes compressed Base64 string to class instance
 */
export function deserializeState<A extends DTO>(
  DTO: new (...args) => A,
  appState: string
): A {
  const compressedString = decode(appState);
  const compressed = new window.TextEncoder().encode(compressedString)
  const jsonString = inflate(compressed, {to : 'string'})
  const plain = JSON.parse(jsonString);
  return plainToInstance(EnkDTO, plain);
}