-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hasPngSignature method (#36)
Closes: #30
- Loading branch information
Showing
6 changed files
with
46 additions
and
31 deletions.
There are no files selected for viewing
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
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,27 @@ | ||
import type { IOBuffer } from 'iobuffer'; | ||
|
||
// https://www.w3.org/TR/PNG/#5PNG-file-signature | ||
|
||
const pngSignature = Uint8Array.of(137, 80, 78, 71, 13, 10, 26, 10); | ||
|
||
export function writeSignature(buffer: IOBuffer) { | ||
buffer.writeBytes(pngSignature); | ||
} | ||
|
||
export function checkSignature(buffer: IOBuffer) { | ||
if (!hasPngSignature(buffer.readBytes(pngSignature.length))) { | ||
throw new Error('wrong PNG signature'); | ||
} | ||
} | ||
|
||
export function hasPngSignature(array: ArrayLike<number>) { | ||
if (array.length < pngSignature.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < pngSignature.length; i++) { | ||
if (array[i] !== pngSignature[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
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