Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect header check with zlib header #256

Open
tsupinie opened this issue Jun 12, 2022 · 3 comments
Open

Incorrect header check with zlib header #256

tsupinie opened this issue Jun 12, 2022 · 3 comments

Comments

@tsupinie
Copy link

tsupinie commented Jun 12, 2022

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.

@puzrin
Copy link
Member

puzrin commented Jun 13, 2022

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

@sethborg
Copy link

sethborg commented Oct 7, 2022

@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

@icazemier
Copy link

icazemier commented Jan 12, 2023

🙏🏼

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants