-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly decode non-ASCII tEXt chunks
Refs: #26
- Loading branch information
Showing
5 changed files
with
62 additions
and
13 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IOBuffer } from 'iobuffer'; | ||
|
||
const latin1Decoder = new TextDecoder('latin1'); | ||
|
||
const NULL = 0; | ||
|
||
const keywordRegex = /^[\x20-\x7e\xa1-\xff]+$/; | ||
|
||
// https://www.w3.org/TR/png/#11keywords | ||
export function readKeyword(buffer: IOBuffer): string { | ||
buffer.mark(); | ||
while (buffer.readByte() !== NULL) { | ||
/* advance */ | ||
} | ||
const end = buffer.offset; | ||
buffer.reset(); | ||
const keyword = latin1Decoder.decode( | ||
buffer.readBytes(end - buffer.offset - 1), | ||
); | ||
// NULL | ||
buffer.skip(1); | ||
|
||
if (!keywordRegex.test(keyword)) { | ||
throw new Error(`keyword contains invalid characters: ${keyword}`); | ||
} | ||
|
||
return keyword; | ||
} | ||
|
||
export function readLatin1(buffer: IOBuffer, length: number): string { | ||
return latin1Decoder.decode(buffer.readBytes(length)); | ||
} |