Skip to content

Commit

Permalink
fix: Allow jpegs with errors to decompress
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor-pelykh committed Feb 5, 2024
1 parent 197ec3c commit 48074cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/formats/jpeg/jpeg-quantize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ export abstract class JpegQuantize {

// convert to 8-bit integers
for (let i = 0; i < 64; ++i) {
dataOut[i] =
JpegQuantize._dctClip[
JpegQuantize._dctClipOffset + 128 + BitUtils.sshR(p[i] + 8, 4)
];
const index =
JpegQuantize._dctClipOffset + 128 + BitUtils.sshR(p[i] + 8, 4);
if (index < 0) {
break;
}
dataOut[i] = JpegQuantize._dctClip[index];
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/formats/jpeg/jpeg-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ export class JpegScan {
if (this._bitsData === 0xff) {
const nextByte = this.input.read();
if (nextByte !== 0) {
const marker = ((this._bitsData << 8) | nextByte).toString(16);
throw new LibError(`unexpected marker: ${marker}`);
// const marker = ((this._bitsData << 8) | nextByte).toString(16);
// throw new LibError(`unexpected marker: ${marker}`);
return undefined;
}
}

Expand Down Expand Up @@ -197,10 +198,16 @@ export class JpegScan {
}

private receiveAndExtend(length: number | undefined): number {
if (length === undefined) {
return 0;
}
if (length === 1) {
return this.readBit() === 1 ? 1 : -1;
}
const n = this.receive(length!)!;
const n = this.receive(length);
if (n === undefined) {
return 0;
}
if (n >= 1 << ((length ?? 0) - 1)) {
return n;
}
Expand All @@ -215,7 +222,10 @@ export class JpegScan {

let k = 1;
while (k < 64) {
const rs = this.decodeHuffman(component.huffmanTableAC)!;
const rs = this.decodeHuffman(component.huffmanTableAC);
if (rs === undefined) {
break;
}
let s = rs & 15;
const r = rs >>> 4;
if (s === 0) {
Expand Down

0 comments on commit 48074cd

Please sign in to comment.