diff --git a/public/common/bytes.js b/public/common/bytes.js new file mode 100644 index 0000000..7337a73 --- /dev/null +++ b/public/common/bytes.js @@ -0,0 +1,28 @@ +// @ts-check + +/** + * @param {Uint8Array} a + * @param {Uint8Array} b + * @returns {boolean} + */ +export const areBytesEqual = (a, b) => { + if (a.length !== b.length) return false; + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) return false; + } + return true; +}; + +/** + * @param {Uint8Array} bytes + * @param {number} size + * @returns {Uint8Array[]} + */ +export const chunkBytes = (bytes, size) => { + /** @type {Uint8Array[]} */ + const result = []; + for (let i = 0; i < bytes.byteLength; i += size) { + result.push(bytes.subarray(i, i + size)); + } + return result; +}; diff --git a/public/png/constants.js b/public/png/constants.js new file mode 100644 index 0000000..5f4ce5a --- /dev/null +++ b/public/png/constants.js @@ -0,0 +1,99 @@ +// @ts-check + +/** @enum {string} */ +export const PngNodeType = [ + "root", + "signature", + + "unknownChunk", + "chunkLength", + "chunkType", + "chunkData", + "chunkCrc", + + "ihdr", + "ihdrChunkData", + "ihdrWidth", + "ihdrHeight", + "ihdrBitDepth", + "ihdrColourType", + "ihdrCompressionMethod", + "ihdrFilterMethod", + "ihdrInterlaceMethod", + + "plte", + "plteChunkData", + "plteColor", + + "idat", + "idatChunkData", + + "iend", + "iendChunkLength", + + "trns", + + "chrm", + + "gama", + + "iccp", + + "sbit", + + "srgb", + + "cicp", + + "text", + "textData", + "textKeyword", + "textNullSeparator", + "textString", + + "ztxt", + "ztxtData", + "ztxtCompressionMethod", + "ztxtString", + + "itxt", + + "bkgd", + + "hist", + + "phys", + + "splt", + + "exif", + + "time", + + "actl", + + "fctl", + + "fdat", + + "offs", + + "pcal", + + "scal", + + "gifg", + + "gifx", + + "gift", + + "ster", + + "dsig", + + "idot", +].reduce((result, id) => { + result[id] = id; + return result; +}, Object.create(null)); diff --git a/public/png/crc32.js b/public/png/crc32.js new file mode 100644 index 0000000..37028be --- /dev/null +++ b/public/png/crc32.js @@ -0,0 +1,28 @@ +// @ts-check + +/** * @type {number[]} */ +const crcTable = []; +for (let i = 0; i < 256; i++) { + let b = i; + for (let j = 0; j < 8; j++) { + b = b & 1 ? 0xedb88320 ^ (b >>> 1) : b >>> 1; + } + crcTable[i] = b >>> 0; +} + +/** + * Compute the CRC32 checksum of some `Uint8Array`s. + * @param {Uint8Array[]} uint8arrays + * @returns {number} + */ +export default (...uint8arrays) => { + let crc = -1; + + for (const bytes of uint8arrays) { + for (let i = 0; i < bytes.length; i++) { + crc = crcTable[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8); + } + } + + return (crc ^ -1) >>> 0; +}; diff --git a/public/png/index.html b/public/png/index.html index 786effd..a83c846 100644 --- a/public/png/index.html +++ b/public/png/index.html @@ -11,7 +11,7 @@ - +