Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.62k stars 1.29k forks source link

Uncompressed data size mismatch (nodejs) still there in 3.7.1 - 32 Bit Issue with << #777

Open JMS-1 opened 2 years ago

JMS-1 commented 2 years ago

I got a ZIP which includes a single file with more than 2GB in (decompressed) size. On decompression the error from the title is thrown. It clearly is some 32Bit signed/unsigned issue:

worker.on("end", function () {
            if (this.streamInfo['data_length'] !== that.uncompressedSize) {
                throw new Error("Bug : uncompressed data size mismatch");
            }
        });

this.streamInfo['data_length']
    4011341711 (0xEF18378F)
that.uncompressedSize
    -283625585 (0xEF18378F)

So my question is: is there a size limit of 2GB for individual files inside the ZIP (which by the way has been created with jszip 3.7.1 as well)?

Thanks

Jochen

JMS-1 commented 2 years ago

Problem is that << only reproduces 32 Bit Value, so

readInt: function(size) {
        var result = 0,
            i;
        this.checkOffset(size);
        for (i = this.index + size - 1; i >= this.index; i--) {
            result = (result << 8) + this.byteAt(i);
        }
        this.index += size;
        return result;
    },

will mess with the sign if the MSB is larger than 127. Better use multiplication

readInt: function(size) {
        var result = 0,
            i;
        this.checkOffset(size);
        for (i = this.index + size - 1; i >= this.index; i--) {
            result = (result * 256) + this.byteAt(i);
        }
        this.index += size;
        return result;
    },

_FYI: * 256 will be safe up to 53 Bit (MAX_SAFEINTEGER) which is around 9 PetaByte.

RubenGarcia commented 2 years ago

We are also affected.