pdf-parser.js 5.2.26 → 5.2.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/dist/{afm-widths-BYiX2L9D.cjs → afm-widths-BHD2HZIL.cjs} +553 -642
  2. package/dist/{afm-widths-blUDDjlI.js → afm-widths-B_IGbGky.js} +553 -642
  3. package/dist/afm-widths.cjs +1 -1
  4. package/dist/afm-widths.js +1 -1
  5. package/dist/annotations.cjs +5 -3
  6. package/dist/annotations.d.cts +2 -2
  7. package/dist/annotations.d.ts +2 -2
  8. package/dist/annotations.js +5 -3
  9. package/dist/attachments.d.cts +2 -2
  10. package/dist/attachments.d.ts +2 -2
  11. package/dist/builtin-encoding.cjs +17 -6
  12. package/dist/builtin-encoding.js +17 -6
  13. package/dist/bytes/crc32.cjs +5 -3
  14. package/dist/bytes/crc32.js +5 -3
  15. package/dist/bytes/flate.cjs +0 -1
  16. package/dist/bytes/flate.js +0 -1
  17. package/dist/cff-bounds.cjs +73 -45
  18. package/dist/cff-bounds.js +73 -45
  19. package/dist/cff.cjs +13 -6
  20. package/dist/cff.js +13 -6
  21. package/dist/cmap-table.cjs +54 -26
  22. package/dist/cmap-table.js +54 -26
  23. package/dist/cmap.cjs +5 -3
  24. package/dist/cmap.js +5 -3
  25. package/dist/content-read.d.cts +1 -1
  26. package/dist/content-read.d.ts +1 -1
  27. package/dist/content-write.cjs +21 -14
  28. package/dist/content-write.d.cts +2 -2
  29. package/dist/content-write.d.ts +2 -2
  30. package/dist/content-write.js +21 -14
  31. package/dist/crypto/aes.cjs +76 -55
  32. package/dist/crypto/aes.js +76 -55
  33. package/dist/crypto/md5.cjs +345 -149
  34. package/dist/crypto/md5.js +345 -149
  35. package/dist/crypto/rc4.cjs +5 -4
  36. package/dist/crypto/rc4.js +5 -4
  37. package/dist/crypto/sha2.cjs +92 -43
  38. package/dist/crypto/sha2.js +92 -43
  39. package/dist/document.cjs +17 -11
  40. package/dist/document.d.cts +1 -1
  41. package/dist/document.d.ts +1 -1
  42. package/dist/document.js +17 -11
  43. package/dist/{embedded-font-Bnhh4juV.d.ts → embedded-font-ByD7ZiQS.d.ts} +1 -1
  44. package/dist/{embedded-font-CEf-zBCA.d.cts → embedded-font-DgVBedw6.d.cts} +1 -1
  45. package/dist/embedded-font-write.d.cts +2 -2
  46. package/dist/embedded-font-write.d.ts +2 -2
  47. package/dist/embedded-font.cjs +4 -2
  48. package/dist/embedded-font.d.cts +1 -1
  49. package/dist/embedded-font.d.ts +1 -1
  50. package/dist/embedded-font.js +4 -2
  51. package/dist/encoding.cjs +1 -1
  52. package/dist/encoding.js +1 -1
  53. package/dist/encrypt-write.cjs +25 -21
  54. package/dist/encrypt-write.d.cts +1 -1
  55. package/dist/encrypt-write.d.ts +1 -1
  56. package/dist/encrypt-write.js +25 -21
  57. package/dist/encrypt.cjs +43 -64
  58. package/dist/encrypt.d.cts +1 -1
  59. package/dist/encrypt.d.ts +1 -1
  60. package/dist/encrypt.js +43 -64
  61. package/dist/filters.cjs +46 -26
  62. package/dist/filters.d.cts +1 -1
  63. package/dist/filters.d.ts +1 -1
  64. package/dist/filters.js +46 -26
  65. package/dist/font-face.cjs +2 -1
  66. package/dist/font-face.js +2 -1
  67. package/dist/font-read.cjs +12 -6
  68. package/dist/font-read.d.cts +1 -1
  69. package/dist/font-read.d.ts +1 -1
  70. package/dist/font-read.js +12 -6
  71. package/dist/font-registry.d.cts +3 -3
  72. package/dist/font-registry.d.ts +3 -3
  73. package/dist/font-tables.cjs +55 -28
  74. package/dist/font-tables.js +55 -28
  75. package/dist/form.cjs +13 -8
  76. package/dist/form.d.cts +2 -2
  77. package/dist/form.d.ts +2 -2
  78. package/dist/form.js +13 -8
  79. package/dist/gdef-table.cjs +6 -3
  80. package/dist/gdef-table.js +6 -3
  81. package/dist/{glyf-contours-C0epBNqP.d.ts → glyf-contours-B1PsI9oA.d.ts} +1 -1
  82. package/dist/{glyf-contours-BRpvd0rt.d.cts → glyf-contours-NHBpgks-.d.cts} +1 -1
  83. package/dist/glyf-contours.cjs +3 -1
  84. package/dist/glyf-contours.d.cts +1 -1
  85. package/dist/glyf-contours.d.ts +1 -1
  86. package/dist/glyf-contours.js +3 -1
  87. package/dist/glyf.cjs +22 -11
  88. package/dist/glyf.js +22 -11
  89. package/dist/gpos-table.cjs +37 -19
  90. package/dist/gpos-table.js +37 -19
  91. package/dist/gsub-table.cjs +40 -25
  92. package/dist/gsub-table.js +40 -25
  93. package/dist/hmtx-table.cjs +5 -3
  94. package/dist/hmtx-table.js +5 -3
  95. package/dist/image/ccitt-encode.cjs +8 -5
  96. package/dist/image/ccitt-encode.js +8 -5
  97. package/dist/image/ccitt.cjs +801 -209
  98. package/dist/image/ccitt.js +801 -209
  99. package/dist/image/jbig2-arith.cjs +340 -306
  100. package/dist/image/jbig2-arith.js +340 -306
  101. package/dist/image/jbig2-bitmap.cjs +8 -5
  102. package/dist/image/jbig2-bitmap.js +8 -5
  103. package/dist/image/jbig2-generic.cjs +240 -69
  104. package/dist/image/jbig2-generic.js +240 -69
  105. package/dist/image/jbig2-text.cjs +4 -2
  106. package/dist/image/jbig2-text.js +4 -2
  107. package/dist/image/jbig2.cjs +69 -31
  108. package/dist/image/jbig2.js +69 -31
  109. package/dist/image/jp2-boxes.cjs +52 -27
  110. package/dist/image/jp2-boxes.js +52 -27
  111. package/dist/image/jpeg-info.cjs +29 -24
  112. package/dist/image/jpeg-info.js +29 -24
  113. package/dist/image/jpeg2000-codestream.cjs +61 -32
  114. package/dist/image/jpeg2000-codestream.js +61 -32
  115. package/dist/image/jpeg2000-dwt.cjs +2 -1
  116. package/dist/image/jpeg2000-dwt.js +2 -1
  117. package/dist/image/jpeg2000-t1.cjs +21 -11
  118. package/dist/image/jpeg2000-t1.js +21 -11
  119. package/dist/image/jpeg2000-t2.cjs +12 -5
  120. package/dist/image/jpeg2000-t2.js +12 -5
  121. package/dist/image/jpeg2000-tagtree.cjs +8 -4
  122. package/dist/image/jpeg2000-tagtree.js +8 -4
  123. package/dist/image/jpeg2000.cjs +11 -6
  124. package/dist/image/jpeg2000.js +11 -6
  125. package/dist/image/png-decode.cjs +62 -39
  126. package/dist/image/png-decode.d.cts +3 -2
  127. package/dist/image/png-decode.d.ts +3 -2
  128. package/dist/image/png-decode.js +62 -39
  129. package/dist/image/png-filter.cjs +23 -15
  130. package/dist/image/png-filter.js +23 -15
  131. package/dist/images-read.cjs +41 -36
  132. package/dist/images-read.d.cts +2 -2
  133. package/dist/images-read.d.ts +2 -2
  134. package/dist/images-read.js +41 -36
  135. package/dist/index.cjs +1 -1
  136. package/dist/index.d.cts +1 -1
  137. package/dist/index.d.ts +1 -1
  138. package/dist/index.js +1 -1
  139. package/dist/interpret.cjs +68 -40
  140. package/dist/interpret.d.cts +1 -1
  141. package/dist/interpret.d.ts +1 -1
  142. package/dist/interpret.js +68 -40
  143. package/dist/lexer.cjs +86 -51
  144. package/dist/lexer.js +86 -51
  145. package/dist/math-content-write.cjs +9 -5
  146. package/dist/math-content-write.js +9 -5
  147. package/dist/math-font-write.cjs +2 -1
  148. package/dist/math-font-write.d.cts +1 -1
  149. package/dist/math-font-write.d.ts +1 -1
  150. package/dist/math-font-write.js +2 -1
  151. package/dist/math-font.cjs +16 -7
  152. package/dist/math-font.js +16 -7
  153. package/dist/math-table.cjs +25 -13
  154. package/dist/math-table.js +25 -13
  155. package/dist/matrix.cjs +3 -2
  156. package/dist/matrix.js +3 -2
  157. package/dist/measure.cjs +1 -1
  158. package/dist/measure.js +1 -1
  159. package/dist/names.cjs +7 -7
  160. package/dist/names.d.cts +2 -2
  161. package/dist/names.d.ts +2 -2
  162. package/dist/names.js +7 -7
  163. package/dist/navigation.cjs +16 -12
  164. package/dist/navigation.d.cts +4 -4
  165. package/dist/navigation.d.ts +4 -4
  166. package/dist/navigation.js +16 -12
  167. package/dist/{objects-B7HdMeWS.d.cts → objects-e2RS3-yT.d.cts} +3 -3
  168. package/dist/{objects-B7HdMeWS.d.ts → objects-e2RS3-yT.d.ts} +3 -3
  169. package/dist/objects.d.cts +1 -1
  170. package/dist/objects.d.ts +1 -1
  171. package/dist/optional-content.d.cts +2 -2
  172. package/dist/optional-content.d.ts +2 -2
  173. package/dist/ot-layout-common.cjs +22 -15
  174. package/dist/ot-layout-common.js +22 -15
  175. package/dist/parse.cjs +7 -5
  176. package/dist/parse.d.cts +1 -1
  177. package/dist/parse.d.ts +1 -1
  178. package/dist/parse.js +7 -5
  179. package/dist/pdf-text.cjs +5 -2
  180. package/dist/pdf-text.js +5 -2
  181. package/dist/predictors.cjs +14 -9
  182. package/dist/predictors.d.cts +1 -1
  183. package/dist/predictors.d.ts +1 -1
  184. package/dist/predictors.js +14 -9
  185. package/dist/raster.cjs +30 -19
  186. package/dist/raster.d.cts +8 -8
  187. package/dist/raster.d.ts +8 -8
  188. package/dist/raster.js +30 -19
  189. package/dist/read.cjs +29 -16
  190. package/dist/read.js +29 -16
  191. package/dist/serialize.cjs +6 -2
  192. package/dist/serialize.d.cts +1 -1
  193. package/dist/serialize.d.ts +1 -1
  194. package/dist/serialize.js +6 -2
  195. package/dist/sfnt-subset.cjs +25 -13
  196. package/dist/sfnt-subset.d.cts +1 -1
  197. package/dist/sfnt-subset.d.ts +1 -1
  198. package/dist/sfnt-subset.js +25 -13
  199. package/dist/sfnt.cjs +20 -10
  200. package/dist/sfnt.js +20 -10
  201. package/dist/structure.d.cts +2 -2
  202. package/dist/structure.d.ts +2 -2
  203. package/dist/text-group.cjs +0 -1
  204. package/dist/text-group.js +0 -1
  205. package/dist/text-layout.d.cts +1 -1
  206. package/dist/text-layout.d.ts +1 -1
  207. package/dist/tounicode.cjs +4 -2
  208. package/dist/tounicode.d.cts +1 -1
  209. package/dist/tounicode.d.ts +1 -1
  210. package/dist/tounicode.js +4 -2
  211. package/dist/winansi.cjs +1 -1
  212. package/dist/winansi.js +1 -1
  213. package/dist/write.cjs +20 -12
  214. package/dist/write.d.cts +1 -1
  215. package/dist/write.d.ts +1 -1
  216. package/dist/write.js +20 -12
  217. package/dist/xref.cjs +5 -2
  218. package/dist/xref.d.cts +1 -1
  219. package/dist/xref.d.ts +1 -1
  220. package/dist/xref.js +5 -2
  221. package/package.json +1 -1
@@ -107,42 +107,66 @@ const H384 = [
107
107
  15784041429090275239n,
108
108
  5167115440072839076n
109
109
  ];
110
- const K256 = Uint32Array.from(K512.slice(0, SHA256_ROUNDS), (k) => Number(k >> 32n));
111
- const H256 = Uint32Array.from(H512, (h) => Number(h >> 32n));
112
- const K512_HIGH = Uint32Array.from(K512, (k) => Number(k >> 32n));
113
- const K512_LOW = Uint32Array.from(K512, (k) => Number(BigInt.asUintN(32, k)));
110
+ const WORD_BITS = 32;
111
+ const WORD_BITS_BIGINT = 32n;
112
+ const K256 = Uint32Array.from(K512.slice(0, SHA256_ROUNDS), (k) => Number(k >> WORD_BITS_BIGINT));
113
+ const H256 = Uint32Array.from(H512, (h) => Number(h >> WORD_BITS_BIGINT));
114
+ const K512_HIGH = Uint32Array.from(K512, (k) => Number(k >> WORD_BITS_BIGINT));
115
+ const K512_LOW = Uint32Array.from(K512, (k) => Number(BigInt.asUintN(WORD_BITS, k)));
114
116
  const TWO_POW_32 = 4294967296;
117
+ const BITS_PER_BYTE = 8;
118
+ const BYTE_MODULUS = 256;
115
119
  function padBigEndian(bytes, blockBytes, lengthBytes) {
116
120
  const paddedLength = (Math.floor((bytes.length + lengthBytes) / blockBytes) + 1) * blockBytes;
117
121
  const padded = new Uint8Array(paddedLength);
118
122
  padded.set(bytes);
119
123
  padded[bytes.length] = 128;
120
- let bitLength = bytes.length * 8;
124
+ let bitLength = bytes.length * BITS_PER_BYTE;
121
125
  for (let i = 0; i < lengthBytes; i++) {
122
- padded[paddedLength - 1 - i] = bitLength % 256;
123
- bitLength = Math.floor(bitLength / 256);
126
+ padded[paddedLength - 1 - i] = bitLength % BYTE_MODULUS;
127
+ bitLength = Math.floor(bitLength / BYTE_MODULUS);
124
128
  }
125
129
  return padded;
126
130
  }
127
131
  function rotr32(value, bits) {
128
- return (value >>> bits | value << 32 - bits) >>> 0;
132
+ return (value >>> bits | value << WORD_BITS - bits) >>> 0;
129
133
  }
134
+ const SHA256_SIGMA0_ROTR_A = 7;
135
+ const SHA256_SIGMA0_ROTR_B = 18;
136
+ const SHA256_SIGMA0_SHR = 3;
137
+ const SHA256_SIGMA1_ROTR_A = 17;
138
+ const SHA256_SIGMA1_ROTR_B = 19;
139
+ const SHA256_SIGMA1_SHR = 10;
140
+ const SHA256_BIG_SIGMA0_ROTR_B = 13;
141
+ const SHA256_BIG_SIGMA0_ROTR_C = 22;
142
+ const SHA256_BIG_SIGMA1_ROTR_A = 6;
143
+ const SHA256_BIG_SIGMA1_ROTR_B = 11;
144
+ const SHA256_BIG_SIGMA1_ROTR_C = 25;
145
+ const BYTES_PER_WORD = 4;
146
+ const WORD_LAST_BYTE_OFFSET = 3;
147
+ const SCHEDULE_OFFSET_7 = 7;
148
+ const SCHEDULE_OFFSET_15 = 15;
149
+ const SCHEDULE_OFFSET_16 = 16;
150
+ const BYTE_SHIFT_1 = 8;
151
+ const BYTE_SHIFT_2 = 16;
152
+ const BYTE_SHIFT_3 = 24;
153
+ const BYTE_MASK = 255;
130
154
  function sha256(bytes) {
131
155
  const padded = padBigEndian(bytes, SHA256_BLOCK_BYTES, 8);
132
156
  const state = Uint32Array.from(H256);
133
157
  const w = new Uint32Array(SHA256_ROUNDS);
134
158
  for (let offset = 0; offset < padded.length; offset += SHA256_BLOCK_BYTES) {
135
159
  w.set(Array.from({ length: WORDS_PER_BLOCK }, (_, t) => {
136
- const at = offset + t * 4;
137
- return (padded[at] << 24 | padded[at + 1] << 16 | padded[at + 2] << 8 | padded[at + 3]) >>> 0;
160
+ const at = offset + t * BYTES_PER_WORD;
161
+ return (padded[at] << BYTE_SHIFT_3 | padded[at + 1] << BYTE_SHIFT_2 | padded[at + 2] << BYTE_SHIFT_1 | padded[at + WORD_LAST_BYTE_OFFSET]) >>> 0;
138
162
  }));
139
163
  Array.from({ length: SHA256_ROUNDS }, (_, t) => {
140
164
  if (t < WORDS_PER_BLOCK) return;
141
- const x = w[t - 15];
165
+ const x = w[t - SCHEDULE_OFFSET_15];
142
166
  const y = w[t - 2];
143
- const s0 = rotr32(x, 7) ^ rotr32(x, 18) ^ x >>> 3;
144
- const s1 = rotr32(y, 17) ^ rotr32(y, 19) ^ y >>> 10;
145
- w[t] = w[t - 16] + s0 + w[t - 7] + s1 >>> 0;
167
+ const s0 = rotr32(x, SHA256_SIGMA0_ROTR_A) ^ rotr32(x, SHA256_SIGMA0_ROTR_B) ^ x >>> SHA256_SIGMA0_SHR;
168
+ const s1 = rotr32(y, SHA256_SIGMA1_ROTR_A) ^ rotr32(y, SHA256_SIGMA1_ROTR_B) ^ y >>> SHA256_SIGMA1_SHR;
169
+ w[t] = w[t - SCHEDULE_OFFSET_16] + s0 + w[t - SCHEDULE_OFFSET_7] + s1 >>> 0;
146
170
  });
147
171
  let a = state[0];
148
172
  let b = state[1];
@@ -153,10 +177,10 @@ function sha256(bytes) {
153
177
  let g = state[6];
154
178
  let h = state[7];
155
179
  for (let t = 0; t < SHA256_ROUNDS; t++) {
156
- const bigS1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25);
180
+ const bigS1 = rotr32(e, SHA256_BIG_SIGMA1_ROTR_A) ^ rotr32(e, SHA256_BIG_SIGMA1_ROTR_B) ^ rotr32(e, SHA256_BIG_SIGMA1_ROTR_C);
157
181
  const ch = e & f ^ ~e & g;
158
182
  const t1 = h + bigS1 + (ch >>> 0) + K256[t] + w[t] >>> 0;
159
- const t2 = (rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22)) + ((a & b ^ a & c ^ b & c) >>> 0) >>> 0;
183
+ const t2 = (rotr32(a, 2) ^ rotr32(a, SHA256_BIG_SIGMA0_ROTR_B) ^ rotr32(a, SHA256_BIG_SIGMA0_ROTR_C)) + ((a & b ^ a & c ^ b & c) >>> 0) >>> 0;
160
184
  h = g;
161
185
  g = f;
162
186
  f = e;
@@ -178,44 +202,67 @@ function sha256(bytes) {
178
202
  ];
179
203
  state.set(state.map((value, i) => value + next[i] >>> 0));
180
204
  }
181
- const digest = new Uint8Array(state.length * 4);
205
+ const digest = new Uint8Array(state.length * BYTES_PER_WORD);
182
206
  state.forEach((word, i) => {
183
- digest[i * 4] = word >>> 24 & 255;
184
- digest[i * 4 + 1] = word >>> 16 & 255;
185
- digest[i * 4 + 2] = word >>> 8 & 255;
186
- digest[i * 4 + 3] = word & 255;
207
+ digest[i * BYTES_PER_WORD] = word >>> BYTE_SHIFT_3 & BYTE_MASK;
208
+ digest[i * BYTES_PER_WORD + 1] = word >>> BYTE_SHIFT_2 & BYTE_MASK;
209
+ digest[i * BYTES_PER_WORD + 2] = word >>> BYTE_SHIFT_1 & BYTE_MASK;
210
+ digest[i * BYTES_PER_WORD + WORD_LAST_BYTE_OFFSET] = word & BYTE_MASK;
187
211
  });
188
212
  return digest;
189
213
  }
190
214
  function readWord32(bytes, offset) {
191
- return (bytes[offset] << 24 | bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
215
+ return (bytes[offset] << BYTE_SHIFT_3 | bytes[offset + 1] << BYTE_SHIFT_2 | bytes[offset + 2] << BYTE_SHIFT_1 | bytes[offset + WORD_LAST_BYTE_OFFSET]) >>> 0;
192
216
  }
217
+ const SHA512_LENGTH_FIELD_BYTES = 16;
218
+ const BYTES_PER_WORD64 = 8;
219
+ const SHA512_SIGMA0_ROTR_1 = 1;
220
+ const SHA512_SIGMA0_ROTR_1_COMPLEMENT = 31;
221
+ const SHA512_SIGMA0_ROTR_8 = 8;
222
+ const SHA512_SIGMA0_ROTR_8_COMPLEMENT = 24;
223
+ const SHA512_SIGMA0_SHR_7 = 7;
224
+ const SHA512_SIGMA0_SHR_7_CARRY = 25;
225
+ const SHA512_SIGMA1_ROTR_19 = 19;
226
+ const SHA512_SIGMA1_ROTR_19_COMPLEMENT = 13;
227
+ const SHA512_SIGMA1_ROTR_61_SHIFT = 29;
228
+ const SHA512_SIGMA1_ROTR_61_COMPLEMENT = 3;
229
+ const SHA512_SIGMA1_SHR_6 = 6;
230
+ const SHA512_SIGMA1_SHR_6_CARRY = 26;
231
+ const SHA512_BIG_SIGMA1_ROTR_A = 14;
232
+ const SHA512_BIG_SIGMA1_ROTR_B = 18;
233
+ const SHA512_BIG_SIGMA1_ROTR_41_SHIFT = 9;
234
+ const SHA512_BIG_SIGMA1_ROTR_41_COMPLEMENT = 23;
235
+ const SHA512_BIG_SIGMA0_ROTR_28 = 28;
236
+ const SHA512_BIG_SIGMA0_ROTR_28_COMPLEMENT = 4;
237
+ const SHA512_BIG_SIGMA0_ROTR_34_COMPLEMENT = 30;
238
+ const SHA512_BIG_SIGMA0_ROTR_39_SHIFT = 7;
239
+ const SHA512_BIG_SIGMA0_ROTR_39_COMPLEMENT = 25;
193
240
  function sha512Core(bytes, initialState, outputWords) {
194
- const padded = padBigEndian(bytes, SHA512_BLOCK_BYTES, 16);
195
- const stateHigh = Uint32Array.from(initialState, (word) => Number(word >> 32n));
196
- const stateLow = Uint32Array.from(initialState, (word) => Number(BigInt.asUintN(32, word)));
241
+ const padded = padBigEndian(bytes, SHA512_BLOCK_BYTES, SHA512_LENGTH_FIELD_BYTES);
242
+ const stateHigh = Uint32Array.from(initialState, (word) => Number(word >> WORD_BITS_BIGINT));
243
+ const stateLow = Uint32Array.from(initialState, (word) => Number(BigInt.asUintN(WORD_BITS, word)));
197
244
  const wHigh = new Uint32Array(SHA512_ROUNDS);
198
245
  const wLow = new Uint32Array(SHA512_ROUNDS);
199
246
  for (let offset = 0; offset < padded.length; offset += SHA512_BLOCK_BYTES) {
200
247
  wHigh.forEach((_word, t) => {
201
248
  if (t < WORDS_PER_BLOCK) {
202
- const at = offset + t * 8;
249
+ const at = offset + t * BYTES_PER_WORD64;
203
250
  wHigh[t] = readWord32(padded, at);
204
- wLow[t] = readWord32(padded, at + 4);
251
+ wLow[t] = readWord32(padded, at + BYTES_PER_WORD);
205
252
  return;
206
253
  }
207
- const xh = wHigh[t - 15];
208
- const xl = wLow[t - 15];
254
+ const xh = wHigh[t - SCHEDULE_OFFSET_15];
255
+ const xl = wLow[t - SCHEDULE_OFFSET_15];
209
256
  const yh = wHigh[t - 2];
210
257
  const yl = wLow[t - 2];
211
- const s0h = (xh >>> 1 | xl << 31) ^ (xh >>> 8 | xl << 24) ^ xh >>> 7;
212
- const s0l = (xl >>> 1 | xh << 31) ^ (xl >>> 8 | xh << 24) ^ (xl >>> 7 | xh << 25);
213
- const s1h = (yh >>> 19 | yl << 13) ^ (yl >>> 29 | yh << 3) ^ yh >>> 6;
214
- const s1l = (yl >>> 19 | yh << 13) ^ (yh >>> 29 | yl << 3) ^ (yl >>> 6 | yh << 26);
215
- const low = wLow[t - 16] + (s0l >>> 0) + wLow[t - 7] + (s1l >>> 0);
258
+ const s0h = (xh >>> SHA512_SIGMA0_ROTR_1 | xl << SHA512_SIGMA0_ROTR_1_COMPLEMENT) ^ (xh >>> SHA512_SIGMA0_ROTR_8 | xl << SHA512_SIGMA0_ROTR_8_COMPLEMENT) ^ xh >>> SHA512_SIGMA0_SHR_7;
259
+ const s0l = (xl >>> SHA512_SIGMA0_ROTR_1 | xh << SHA512_SIGMA0_ROTR_1_COMPLEMENT) ^ (xl >>> SHA512_SIGMA0_ROTR_8 | xh << SHA512_SIGMA0_ROTR_8_COMPLEMENT) ^ (xl >>> SHA512_SIGMA0_SHR_7 | xh << SHA512_SIGMA0_SHR_7_CARRY);
260
+ const s1h = (yh >>> SHA512_SIGMA1_ROTR_19 | yl << SHA512_SIGMA1_ROTR_19_COMPLEMENT) ^ (yl >>> SHA512_SIGMA1_ROTR_61_SHIFT | yh << SHA512_SIGMA1_ROTR_61_COMPLEMENT) ^ yh >>> SHA512_SIGMA1_SHR_6;
261
+ const s1l = (yl >>> SHA512_SIGMA1_ROTR_19 | yh << SHA512_SIGMA1_ROTR_19_COMPLEMENT) ^ (yh >>> SHA512_SIGMA1_ROTR_61_SHIFT | yl << SHA512_SIGMA1_ROTR_61_COMPLEMENT) ^ (yl >>> SHA512_SIGMA1_SHR_6 | yh << SHA512_SIGMA1_SHR_6_CARRY);
262
+ const low = wLow[t - SCHEDULE_OFFSET_16] + (s0l >>> 0) + wLow[t - SCHEDULE_OFFSET_7] + (s1l >>> 0);
216
263
  const carry = Math.floor(low / TWO_POW_32);
217
264
  wLow[t] = low;
218
- wHigh[t] = wHigh[t - 16] + (s0h >>> 0) + wHigh[t - 7] + (s1h >>> 0) + carry;
265
+ wHigh[t] = wHigh[t - SCHEDULE_OFFSET_16] + (s0h >>> 0) + wHigh[t - SCHEDULE_OFFSET_7] + (s1h >>> 0) + carry;
219
266
  });
220
267
  let ah = stateHigh[0];
221
268
  let al = stateLow[0];
@@ -234,15 +281,15 @@ function sha512Core(bytes, initialState, outputWords) {
234
281
  let hh = stateHigh[7];
235
282
  let hl = stateLow[7];
236
283
  for (let t = 0; t < SHA512_ROUNDS; t++) {
237
- const bigS1h = (eh >>> 14 | el << 18) ^ (eh >>> 18 | el << 14) ^ (el >>> 9 | eh << 23);
238
- const bigS1l = (el >>> 14 | eh << 18) ^ (el >>> 18 | eh << 14) ^ (eh >>> 9 | el << 23);
284
+ const bigS1h = (eh >>> SHA512_BIG_SIGMA1_ROTR_A | el << SHA512_BIG_SIGMA1_ROTR_B) ^ (eh >>> SHA512_BIG_SIGMA1_ROTR_B | el << SHA512_BIG_SIGMA1_ROTR_A) ^ (el >>> SHA512_BIG_SIGMA1_ROTR_41_SHIFT | eh << SHA512_BIG_SIGMA1_ROTR_41_COMPLEMENT);
285
+ const bigS1l = (el >>> SHA512_BIG_SIGMA1_ROTR_A | eh << SHA512_BIG_SIGMA1_ROTR_B) ^ (el >>> SHA512_BIG_SIGMA1_ROTR_B | eh << SHA512_BIG_SIGMA1_ROTR_A) ^ (eh >>> SHA512_BIG_SIGMA1_ROTR_41_SHIFT | el << SHA512_BIG_SIGMA1_ROTR_41_COMPLEMENT);
239
286
  const chh = eh & fh ^ ~eh & gh;
240
287
  const chl = el & fl ^ ~el & gl;
241
288
  const t1Sum = hl + (bigS1l >>> 0) + (chl >>> 0) + K512_LOW[t] + wLow[t];
242
289
  const t1l = t1Sum >>> 0;
243
290
  const t1h = hh + (bigS1h >>> 0) + (chh >>> 0) + K512_HIGH[t] + wHigh[t] + Math.floor(t1Sum / TWO_POW_32) >>> 0;
244
- const bigS0h = (ah >>> 28 | al << 4) ^ (al >>> 2 | ah << 30) ^ (al >>> 7 | ah << 25);
245
- const bigS0l = (al >>> 28 | ah << 4) ^ (ah >>> 2 | al << 30) ^ (ah >>> 7 | al << 25);
291
+ const bigS0h = (ah >>> SHA512_BIG_SIGMA0_ROTR_28 | al << SHA512_BIG_SIGMA0_ROTR_28_COMPLEMENT) ^ (al >>> 2 | ah << SHA512_BIG_SIGMA0_ROTR_34_COMPLEMENT) ^ (al >>> SHA512_BIG_SIGMA0_ROTR_39_SHIFT | ah << SHA512_BIG_SIGMA0_ROTR_39_COMPLEMENT);
292
+ const bigS0l = (al >>> SHA512_BIG_SIGMA0_ROTR_28 | ah << SHA512_BIG_SIGMA0_ROTR_28_COMPLEMENT) ^ (ah >>> 2 | al << SHA512_BIG_SIGMA0_ROTR_34_COMPLEMENT) ^ (ah >>> SHA512_BIG_SIGMA0_ROTR_39_SHIFT | al << SHA512_BIG_SIGMA0_ROTR_39_COMPLEMENT);
246
293
  const majh = ah & bh ^ ah & ch ^ bh & ch;
247
294
  const majl = al & bl ^ al & cl ^ bl & cl;
248
295
  const t2Sum = (bigS0l >>> 0) + (majl >>> 0);
@@ -293,15 +340,17 @@ function sha512Core(bytes, initialState, outputWords) {
293
340
  stateLow[i] = sum;
294
341
  });
295
342
  }
296
- return Uint8Array.from({ length: outputWords * 8 }, (_unused, index) => {
297
- return (index % 8 < 4 ? stateHigh : stateLow)[Math.floor(index / 8)] >>> 24 - 8 * (index % 4) & 255;
343
+ return Uint8Array.from({ length: outputWords * BYTES_PER_WORD64 }, (_unused, index) => {
344
+ return (index % BYTES_PER_WORD64 < BYTES_PER_WORD ? stateHigh : stateLow)[Math.floor(index / BYTES_PER_WORD64)] >>> BYTE_SHIFT_3 - BYTE_SHIFT_1 * (index % BYTES_PER_WORD) & BYTE_MASK;
298
345
  });
299
346
  }
347
+ const SHA512_OUTPUT_WORDS = 8;
348
+ const SHA384_OUTPUT_WORDS = 6;
300
349
  function sha512(bytes) {
301
- return sha512Core(bytes, H512, 8);
350
+ return sha512Core(bytes, H512, SHA512_OUTPUT_WORDS);
302
351
  }
303
352
  function sha384(bytes) {
304
- return sha512Core(bytes, H384, 6);
353
+ return sha512Core(bytes, H384, SHA384_OUTPUT_WORDS);
305
354
  }
306
355
  //#endregion
307
356
  exports.sha256 = sha256;
@@ -106,42 +106,66 @@ const H384 = [
106
106
  15784041429090275239n,
107
107
  5167115440072839076n
108
108
  ];
109
- const K256 = Uint32Array.from(K512.slice(0, SHA256_ROUNDS), (k) => Number(k >> 32n));
110
- const H256 = Uint32Array.from(H512, (h) => Number(h >> 32n));
111
- const K512_HIGH = Uint32Array.from(K512, (k) => Number(k >> 32n));
112
- const K512_LOW = Uint32Array.from(K512, (k) => Number(BigInt.asUintN(32, k)));
109
+ const WORD_BITS = 32;
110
+ const WORD_BITS_BIGINT = 32n;
111
+ const K256 = Uint32Array.from(K512.slice(0, SHA256_ROUNDS), (k) => Number(k >> WORD_BITS_BIGINT));
112
+ const H256 = Uint32Array.from(H512, (h) => Number(h >> WORD_BITS_BIGINT));
113
+ const K512_HIGH = Uint32Array.from(K512, (k) => Number(k >> WORD_BITS_BIGINT));
114
+ const K512_LOW = Uint32Array.from(K512, (k) => Number(BigInt.asUintN(WORD_BITS, k)));
113
115
  const TWO_POW_32 = 4294967296;
116
+ const BITS_PER_BYTE = 8;
117
+ const BYTE_MODULUS = 256;
114
118
  function padBigEndian(bytes, blockBytes, lengthBytes) {
115
119
  const paddedLength = (Math.floor((bytes.length + lengthBytes) / blockBytes) + 1) * blockBytes;
116
120
  const padded = new Uint8Array(paddedLength);
117
121
  padded.set(bytes);
118
122
  padded[bytes.length] = 128;
119
- let bitLength = bytes.length * 8;
123
+ let bitLength = bytes.length * BITS_PER_BYTE;
120
124
  for (let i = 0; i < lengthBytes; i++) {
121
- padded[paddedLength - 1 - i] = bitLength % 256;
122
- bitLength = Math.floor(bitLength / 256);
125
+ padded[paddedLength - 1 - i] = bitLength % BYTE_MODULUS;
126
+ bitLength = Math.floor(bitLength / BYTE_MODULUS);
123
127
  }
124
128
  return padded;
125
129
  }
126
130
  function rotr32(value, bits) {
127
- return (value >>> bits | value << 32 - bits) >>> 0;
131
+ return (value >>> bits | value << WORD_BITS - bits) >>> 0;
128
132
  }
133
+ const SHA256_SIGMA0_ROTR_A = 7;
134
+ const SHA256_SIGMA0_ROTR_B = 18;
135
+ const SHA256_SIGMA0_SHR = 3;
136
+ const SHA256_SIGMA1_ROTR_A = 17;
137
+ const SHA256_SIGMA1_ROTR_B = 19;
138
+ const SHA256_SIGMA1_SHR = 10;
139
+ const SHA256_BIG_SIGMA0_ROTR_B = 13;
140
+ const SHA256_BIG_SIGMA0_ROTR_C = 22;
141
+ const SHA256_BIG_SIGMA1_ROTR_A = 6;
142
+ const SHA256_BIG_SIGMA1_ROTR_B = 11;
143
+ const SHA256_BIG_SIGMA1_ROTR_C = 25;
144
+ const BYTES_PER_WORD = 4;
145
+ const WORD_LAST_BYTE_OFFSET = 3;
146
+ const SCHEDULE_OFFSET_7 = 7;
147
+ const SCHEDULE_OFFSET_15 = 15;
148
+ const SCHEDULE_OFFSET_16 = 16;
149
+ const BYTE_SHIFT_1 = 8;
150
+ const BYTE_SHIFT_2 = 16;
151
+ const BYTE_SHIFT_3 = 24;
152
+ const BYTE_MASK = 255;
129
153
  function sha256(bytes) {
130
154
  const padded = padBigEndian(bytes, SHA256_BLOCK_BYTES, 8);
131
155
  const state = Uint32Array.from(H256);
132
156
  const w = new Uint32Array(SHA256_ROUNDS);
133
157
  for (let offset = 0; offset < padded.length; offset += SHA256_BLOCK_BYTES) {
134
158
  w.set(Array.from({ length: WORDS_PER_BLOCK }, (_, t) => {
135
- const at = offset + t * 4;
136
- return (padded[at] << 24 | padded[at + 1] << 16 | padded[at + 2] << 8 | padded[at + 3]) >>> 0;
159
+ const at = offset + t * BYTES_PER_WORD;
160
+ return (padded[at] << BYTE_SHIFT_3 | padded[at + 1] << BYTE_SHIFT_2 | padded[at + 2] << BYTE_SHIFT_1 | padded[at + WORD_LAST_BYTE_OFFSET]) >>> 0;
137
161
  }));
138
162
  Array.from({ length: SHA256_ROUNDS }, (_, t) => {
139
163
  if (t < WORDS_PER_BLOCK) return;
140
- const x = w[t - 15];
164
+ const x = w[t - SCHEDULE_OFFSET_15];
141
165
  const y = w[t - 2];
142
- const s0 = rotr32(x, 7) ^ rotr32(x, 18) ^ x >>> 3;
143
- const s1 = rotr32(y, 17) ^ rotr32(y, 19) ^ y >>> 10;
144
- w[t] = w[t - 16] + s0 + w[t - 7] + s1 >>> 0;
166
+ const s0 = rotr32(x, SHA256_SIGMA0_ROTR_A) ^ rotr32(x, SHA256_SIGMA0_ROTR_B) ^ x >>> SHA256_SIGMA0_SHR;
167
+ const s1 = rotr32(y, SHA256_SIGMA1_ROTR_A) ^ rotr32(y, SHA256_SIGMA1_ROTR_B) ^ y >>> SHA256_SIGMA1_SHR;
168
+ w[t] = w[t - SCHEDULE_OFFSET_16] + s0 + w[t - SCHEDULE_OFFSET_7] + s1 >>> 0;
145
169
  });
146
170
  let a = state[0];
147
171
  let b = state[1];
@@ -152,10 +176,10 @@ function sha256(bytes) {
152
176
  let g = state[6];
153
177
  let h = state[7];
154
178
  for (let t = 0; t < SHA256_ROUNDS; t++) {
155
- const bigS1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25);
179
+ const bigS1 = rotr32(e, SHA256_BIG_SIGMA1_ROTR_A) ^ rotr32(e, SHA256_BIG_SIGMA1_ROTR_B) ^ rotr32(e, SHA256_BIG_SIGMA1_ROTR_C);
156
180
  const ch = e & f ^ ~e & g;
157
181
  const t1 = h + bigS1 + (ch >>> 0) + K256[t] + w[t] >>> 0;
158
- const t2 = (rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22)) + ((a & b ^ a & c ^ b & c) >>> 0) >>> 0;
182
+ const t2 = (rotr32(a, 2) ^ rotr32(a, SHA256_BIG_SIGMA0_ROTR_B) ^ rotr32(a, SHA256_BIG_SIGMA0_ROTR_C)) + ((a & b ^ a & c ^ b & c) >>> 0) >>> 0;
159
183
  h = g;
160
184
  g = f;
161
185
  f = e;
@@ -177,44 +201,67 @@ function sha256(bytes) {
177
201
  ];
178
202
  state.set(state.map((value, i) => value + next[i] >>> 0));
179
203
  }
180
- const digest = new Uint8Array(state.length * 4);
204
+ const digest = new Uint8Array(state.length * BYTES_PER_WORD);
181
205
  state.forEach((word, i) => {
182
- digest[i * 4] = word >>> 24 & 255;
183
- digest[i * 4 + 1] = word >>> 16 & 255;
184
- digest[i * 4 + 2] = word >>> 8 & 255;
185
- digest[i * 4 + 3] = word & 255;
206
+ digest[i * BYTES_PER_WORD] = word >>> BYTE_SHIFT_3 & BYTE_MASK;
207
+ digest[i * BYTES_PER_WORD + 1] = word >>> BYTE_SHIFT_2 & BYTE_MASK;
208
+ digest[i * BYTES_PER_WORD + 2] = word >>> BYTE_SHIFT_1 & BYTE_MASK;
209
+ digest[i * BYTES_PER_WORD + WORD_LAST_BYTE_OFFSET] = word & BYTE_MASK;
186
210
  });
187
211
  return digest;
188
212
  }
189
213
  function readWord32(bytes, offset) {
190
- return (bytes[offset] << 24 | bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
214
+ return (bytes[offset] << BYTE_SHIFT_3 | bytes[offset + 1] << BYTE_SHIFT_2 | bytes[offset + 2] << BYTE_SHIFT_1 | bytes[offset + WORD_LAST_BYTE_OFFSET]) >>> 0;
191
215
  }
216
+ const SHA512_LENGTH_FIELD_BYTES = 16;
217
+ const BYTES_PER_WORD64 = 8;
218
+ const SHA512_SIGMA0_ROTR_1 = 1;
219
+ const SHA512_SIGMA0_ROTR_1_COMPLEMENT = 31;
220
+ const SHA512_SIGMA0_ROTR_8 = 8;
221
+ const SHA512_SIGMA0_ROTR_8_COMPLEMENT = 24;
222
+ const SHA512_SIGMA0_SHR_7 = 7;
223
+ const SHA512_SIGMA0_SHR_7_CARRY = 25;
224
+ const SHA512_SIGMA1_ROTR_19 = 19;
225
+ const SHA512_SIGMA1_ROTR_19_COMPLEMENT = 13;
226
+ const SHA512_SIGMA1_ROTR_61_SHIFT = 29;
227
+ const SHA512_SIGMA1_ROTR_61_COMPLEMENT = 3;
228
+ const SHA512_SIGMA1_SHR_6 = 6;
229
+ const SHA512_SIGMA1_SHR_6_CARRY = 26;
230
+ const SHA512_BIG_SIGMA1_ROTR_A = 14;
231
+ const SHA512_BIG_SIGMA1_ROTR_B = 18;
232
+ const SHA512_BIG_SIGMA1_ROTR_41_SHIFT = 9;
233
+ const SHA512_BIG_SIGMA1_ROTR_41_COMPLEMENT = 23;
234
+ const SHA512_BIG_SIGMA0_ROTR_28 = 28;
235
+ const SHA512_BIG_SIGMA0_ROTR_28_COMPLEMENT = 4;
236
+ const SHA512_BIG_SIGMA0_ROTR_34_COMPLEMENT = 30;
237
+ const SHA512_BIG_SIGMA0_ROTR_39_SHIFT = 7;
238
+ const SHA512_BIG_SIGMA0_ROTR_39_COMPLEMENT = 25;
192
239
  function sha512Core(bytes, initialState, outputWords) {
193
- const padded = padBigEndian(bytes, SHA512_BLOCK_BYTES, 16);
194
- const stateHigh = Uint32Array.from(initialState, (word) => Number(word >> 32n));
195
- const stateLow = Uint32Array.from(initialState, (word) => Number(BigInt.asUintN(32, word)));
240
+ const padded = padBigEndian(bytes, SHA512_BLOCK_BYTES, SHA512_LENGTH_FIELD_BYTES);
241
+ const stateHigh = Uint32Array.from(initialState, (word) => Number(word >> WORD_BITS_BIGINT));
242
+ const stateLow = Uint32Array.from(initialState, (word) => Number(BigInt.asUintN(WORD_BITS, word)));
196
243
  const wHigh = new Uint32Array(SHA512_ROUNDS);
197
244
  const wLow = new Uint32Array(SHA512_ROUNDS);
198
245
  for (let offset = 0; offset < padded.length; offset += SHA512_BLOCK_BYTES) {
199
246
  wHigh.forEach((_word, t) => {
200
247
  if (t < WORDS_PER_BLOCK) {
201
- const at = offset + t * 8;
248
+ const at = offset + t * BYTES_PER_WORD64;
202
249
  wHigh[t] = readWord32(padded, at);
203
- wLow[t] = readWord32(padded, at + 4);
250
+ wLow[t] = readWord32(padded, at + BYTES_PER_WORD);
204
251
  return;
205
252
  }
206
- const xh = wHigh[t - 15];
207
- const xl = wLow[t - 15];
253
+ const xh = wHigh[t - SCHEDULE_OFFSET_15];
254
+ const xl = wLow[t - SCHEDULE_OFFSET_15];
208
255
  const yh = wHigh[t - 2];
209
256
  const yl = wLow[t - 2];
210
- const s0h = (xh >>> 1 | xl << 31) ^ (xh >>> 8 | xl << 24) ^ xh >>> 7;
211
- const s0l = (xl >>> 1 | xh << 31) ^ (xl >>> 8 | xh << 24) ^ (xl >>> 7 | xh << 25);
212
- const s1h = (yh >>> 19 | yl << 13) ^ (yl >>> 29 | yh << 3) ^ yh >>> 6;
213
- const s1l = (yl >>> 19 | yh << 13) ^ (yh >>> 29 | yl << 3) ^ (yl >>> 6 | yh << 26);
214
- const low = wLow[t - 16] + (s0l >>> 0) + wLow[t - 7] + (s1l >>> 0);
257
+ const s0h = (xh >>> SHA512_SIGMA0_ROTR_1 | xl << SHA512_SIGMA0_ROTR_1_COMPLEMENT) ^ (xh >>> SHA512_SIGMA0_ROTR_8 | xl << SHA512_SIGMA0_ROTR_8_COMPLEMENT) ^ xh >>> SHA512_SIGMA0_SHR_7;
258
+ const s0l = (xl >>> SHA512_SIGMA0_ROTR_1 | xh << SHA512_SIGMA0_ROTR_1_COMPLEMENT) ^ (xl >>> SHA512_SIGMA0_ROTR_8 | xh << SHA512_SIGMA0_ROTR_8_COMPLEMENT) ^ (xl >>> SHA512_SIGMA0_SHR_7 | xh << SHA512_SIGMA0_SHR_7_CARRY);
259
+ const s1h = (yh >>> SHA512_SIGMA1_ROTR_19 | yl << SHA512_SIGMA1_ROTR_19_COMPLEMENT) ^ (yl >>> SHA512_SIGMA1_ROTR_61_SHIFT | yh << SHA512_SIGMA1_ROTR_61_COMPLEMENT) ^ yh >>> SHA512_SIGMA1_SHR_6;
260
+ const s1l = (yl >>> SHA512_SIGMA1_ROTR_19 | yh << SHA512_SIGMA1_ROTR_19_COMPLEMENT) ^ (yh >>> SHA512_SIGMA1_ROTR_61_SHIFT | yl << SHA512_SIGMA1_ROTR_61_COMPLEMENT) ^ (yl >>> SHA512_SIGMA1_SHR_6 | yh << SHA512_SIGMA1_SHR_6_CARRY);
261
+ const low = wLow[t - SCHEDULE_OFFSET_16] + (s0l >>> 0) + wLow[t - SCHEDULE_OFFSET_7] + (s1l >>> 0);
215
262
  const carry = Math.floor(low / TWO_POW_32);
216
263
  wLow[t] = low;
217
- wHigh[t] = wHigh[t - 16] + (s0h >>> 0) + wHigh[t - 7] + (s1h >>> 0) + carry;
264
+ wHigh[t] = wHigh[t - SCHEDULE_OFFSET_16] + (s0h >>> 0) + wHigh[t - SCHEDULE_OFFSET_7] + (s1h >>> 0) + carry;
218
265
  });
219
266
  let ah = stateHigh[0];
220
267
  let al = stateLow[0];
@@ -233,15 +280,15 @@ function sha512Core(bytes, initialState, outputWords) {
233
280
  let hh = stateHigh[7];
234
281
  let hl = stateLow[7];
235
282
  for (let t = 0; t < SHA512_ROUNDS; t++) {
236
- const bigS1h = (eh >>> 14 | el << 18) ^ (eh >>> 18 | el << 14) ^ (el >>> 9 | eh << 23);
237
- const bigS1l = (el >>> 14 | eh << 18) ^ (el >>> 18 | eh << 14) ^ (eh >>> 9 | el << 23);
283
+ const bigS1h = (eh >>> SHA512_BIG_SIGMA1_ROTR_A | el << SHA512_BIG_SIGMA1_ROTR_B) ^ (eh >>> SHA512_BIG_SIGMA1_ROTR_B | el << SHA512_BIG_SIGMA1_ROTR_A) ^ (el >>> SHA512_BIG_SIGMA1_ROTR_41_SHIFT | eh << SHA512_BIG_SIGMA1_ROTR_41_COMPLEMENT);
284
+ const bigS1l = (el >>> SHA512_BIG_SIGMA1_ROTR_A | eh << SHA512_BIG_SIGMA1_ROTR_B) ^ (el >>> SHA512_BIG_SIGMA1_ROTR_B | eh << SHA512_BIG_SIGMA1_ROTR_A) ^ (eh >>> SHA512_BIG_SIGMA1_ROTR_41_SHIFT | el << SHA512_BIG_SIGMA1_ROTR_41_COMPLEMENT);
238
285
  const chh = eh & fh ^ ~eh & gh;
239
286
  const chl = el & fl ^ ~el & gl;
240
287
  const t1Sum = hl + (bigS1l >>> 0) + (chl >>> 0) + K512_LOW[t] + wLow[t];
241
288
  const t1l = t1Sum >>> 0;
242
289
  const t1h = hh + (bigS1h >>> 0) + (chh >>> 0) + K512_HIGH[t] + wHigh[t] + Math.floor(t1Sum / TWO_POW_32) >>> 0;
243
- const bigS0h = (ah >>> 28 | al << 4) ^ (al >>> 2 | ah << 30) ^ (al >>> 7 | ah << 25);
244
- const bigS0l = (al >>> 28 | ah << 4) ^ (ah >>> 2 | al << 30) ^ (ah >>> 7 | al << 25);
290
+ const bigS0h = (ah >>> SHA512_BIG_SIGMA0_ROTR_28 | al << SHA512_BIG_SIGMA0_ROTR_28_COMPLEMENT) ^ (al >>> 2 | ah << SHA512_BIG_SIGMA0_ROTR_34_COMPLEMENT) ^ (al >>> SHA512_BIG_SIGMA0_ROTR_39_SHIFT | ah << SHA512_BIG_SIGMA0_ROTR_39_COMPLEMENT);
291
+ const bigS0l = (al >>> SHA512_BIG_SIGMA0_ROTR_28 | ah << SHA512_BIG_SIGMA0_ROTR_28_COMPLEMENT) ^ (ah >>> 2 | al << SHA512_BIG_SIGMA0_ROTR_34_COMPLEMENT) ^ (ah >>> SHA512_BIG_SIGMA0_ROTR_39_SHIFT | al << SHA512_BIG_SIGMA0_ROTR_39_COMPLEMENT);
245
292
  const majh = ah & bh ^ ah & ch ^ bh & ch;
246
293
  const majl = al & bl ^ al & cl ^ bl & cl;
247
294
  const t2Sum = (bigS0l >>> 0) + (majl >>> 0);
@@ -292,15 +339,17 @@ function sha512Core(bytes, initialState, outputWords) {
292
339
  stateLow[i] = sum;
293
340
  });
294
341
  }
295
- return Uint8Array.from({ length: outputWords * 8 }, (_unused, index) => {
296
- return (index % 8 < 4 ? stateHigh : stateLow)[Math.floor(index / 8)] >>> 24 - 8 * (index % 4) & 255;
342
+ return Uint8Array.from({ length: outputWords * BYTES_PER_WORD64 }, (_unused, index) => {
343
+ return (index % BYTES_PER_WORD64 < BYTES_PER_WORD ? stateHigh : stateLow)[Math.floor(index / BYTES_PER_WORD64)] >>> BYTE_SHIFT_3 - BYTE_SHIFT_1 * (index % BYTES_PER_WORD) & BYTE_MASK;
297
344
  });
298
345
  }
346
+ const SHA512_OUTPUT_WORDS = 8;
347
+ const SHA384_OUTPUT_WORDS = 6;
299
348
  function sha512(bytes) {
300
- return sha512Core(bytes, H512, 8);
349
+ return sha512Core(bytes, H512, SHA512_OUTPUT_WORDS);
301
350
  }
302
351
  function sha384(bytes) {
303
- return sha512Core(bytes, H384, 6);
352
+ return sha512Core(bytes, H384, SHA384_OUTPUT_WORDS);
304
353
  }
305
354
  //#endregion
306
355
  export { sha256, sha384, sha512 };
package/dist/document.cjs CHANGED
@@ -154,19 +154,22 @@ function openPdfDocument(bytes, sink) {
154
154
  function pages() {
155
155
  const pagesRoot = resolveDict(require_objects.dictGet(catalog, "Pages"));
156
156
  if (pagesRoot === void 0) return [];
157
- const result = [];
158
157
  originalLeaves.clear();
159
- walkPageTree(pagesRoot, {}, /* @__PURE__ */ new Set(), result);
158
+ const result = [];
159
+ for (const leaf of walkPageTree(pagesRoot, {}, /* @__PURE__ */ new Set())) {
160
+ originalLeaves.set(leaf.node, result.length);
161
+ result.push(leaf.page);
162
+ }
160
163
  return result;
161
164
  }
162
- function walkPageTree(node, inherited, visited, result) {
165
+ function walkPageTree(node, inherited, visited) {
163
166
  if (visited.has(node)) {
164
167
  sink({
165
168
  code: "pdf/page-tree-cycle",
166
169
  severity: "warning",
167
170
  message: "the page tree contains a cycle; stopping descent at the repeated node"
168
171
  });
169
- return;
172
+ return [];
170
173
  }
171
174
  visited.add(node);
172
175
  const merged = { ...inherited };
@@ -178,17 +181,20 @@ function openPdfDocument(bytes, sink) {
178
181
  if (kids === void 0) {
179
182
  const entries = new Map(node.entries);
180
183
  for (const key of INHERITABLE_PAGE_KEYS) if (!entries.has(key) && merged[key] !== void 0) entries.set(key, merged[key]);
181
- originalLeaves.set(node, result.length);
182
- result.push({
183
- kind: "dict",
184
- entries
185
- });
186
- return;
184
+ return [{
185
+ node,
186
+ page: {
187
+ kind: "dict",
188
+ entries
189
+ }
190
+ }];
187
191
  }
192
+ const leaves = [];
188
193
  for (const kid of kids) {
189
194
  const kidDict = resolveDict(kid);
190
- if (kidDict !== void 0) walkPageTree(kidDict, merged, visited, result);
195
+ if (kidDict !== void 0) for (const leaf of walkPageTree(kidDict, merged, visited)) leaves.push(leaf);
191
196
  }
197
+ return leaves;
192
198
  }
193
199
  function pageIndex(obj) {
194
200
  const dict = require_objects.asDict(resolve(obj));
@@ -1,5 +1,5 @@
1
1
  import { PdfDiagnosticSink } from "./diagnostics.cjs";
2
- import { n as PdfDict, r as PdfObject } from "./objects-B7HdMeWS.cjs";
2
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.cjs";
3
3
  //#region src/document.d.ts
4
4
  interface PdfDocument {
5
5
  readonly trailer: PdfDict;
@@ -1,5 +1,5 @@
1
1
  import { PdfDiagnosticSink } from "./diagnostics.js";
2
- import { n as PdfDict, r as PdfObject } from "./objects-B7HdMeWS.js";
2
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.js";
3
3
  //#region src/document.d.ts
4
4
  interface PdfDocument {
5
5
  readonly trailer: PdfDict;
package/dist/document.js CHANGED
@@ -153,19 +153,22 @@ function openPdfDocument(bytes, sink) {
153
153
  function pages() {
154
154
  const pagesRoot = resolveDict(dictGet(catalog, "Pages"));
155
155
  if (pagesRoot === void 0) return [];
156
- const result = [];
157
156
  originalLeaves.clear();
158
- walkPageTree(pagesRoot, {}, /* @__PURE__ */ new Set(), result);
157
+ const result = [];
158
+ for (const leaf of walkPageTree(pagesRoot, {}, /* @__PURE__ */ new Set())) {
159
+ originalLeaves.set(leaf.node, result.length);
160
+ result.push(leaf.page);
161
+ }
159
162
  return result;
160
163
  }
161
- function walkPageTree(node, inherited, visited, result) {
164
+ function walkPageTree(node, inherited, visited) {
162
165
  if (visited.has(node)) {
163
166
  sink({
164
167
  code: "pdf/page-tree-cycle",
165
168
  severity: "warning",
166
169
  message: "the page tree contains a cycle; stopping descent at the repeated node"
167
170
  });
168
- return;
171
+ return [];
169
172
  }
170
173
  visited.add(node);
171
174
  const merged = { ...inherited };
@@ -177,17 +180,20 @@ function openPdfDocument(bytes, sink) {
177
180
  if (kids === void 0) {
178
181
  const entries = new Map(node.entries);
179
182
  for (const key of INHERITABLE_PAGE_KEYS) if (!entries.has(key) && merged[key] !== void 0) entries.set(key, merged[key]);
180
- originalLeaves.set(node, result.length);
181
- result.push({
182
- kind: "dict",
183
- entries
184
- });
185
- return;
183
+ return [{
184
+ node,
185
+ page: {
186
+ kind: "dict",
187
+ entries
188
+ }
189
+ }];
186
190
  }
191
+ const leaves = [];
187
192
  for (const kid of kids) {
188
193
  const kidDict = resolveDict(kid);
189
- if (kidDict !== void 0) walkPageTree(kidDict, merged, visited, result);
194
+ if (kidDict !== void 0) for (const leaf of walkPageTree(kidDict, merged, visited)) leaves.push(leaf);
190
195
  }
196
+ return leaves;
191
197
  }
192
198
  function pageIndex(obj) {
193
199
  const dict = asDict(resolve(obj));
@@ -45,6 +45,6 @@ interface EmbeddedShow {
45
45
  readonly kerns: readonly EmbeddedKern[];
46
46
  }
47
47
  declare function encodeForShowEmbedded(text: string, face: EmbeddedFace): EmbeddedShow;
48
- declare function collectEmbeddedGlyphs(texts: Iterable<string>, face: EmbeddedFace): ReadonlyMap<number, readonly number[]>;
48
+ declare function collectEmbeddedGlyphs(texts: Readonly<Iterable<string>>, face: EmbeddedFace): ReadonlyMap<number, readonly number[]>;
49
49
  //#endregion
50
50
  export { EmbeddedShow as a, encodeForShowEmbedded as c, EmbeddedKern as i, loadEmbeddedFace as l, EmbeddedFaceMetrics as n, NOTDEF_GLYPH_ID as o, EmbeddedFaceSubstitution as r, collectEmbeddedGlyphs as s, EmbeddedFace as t };
@@ -45,6 +45,6 @@ interface EmbeddedShow {
45
45
  readonly kerns: readonly EmbeddedKern[];
46
46
  }
47
47
  declare function encodeForShowEmbedded(text: string, face: EmbeddedFace): EmbeddedShow;
48
- declare function collectEmbeddedGlyphs(texts: Iterable<string>, face: EmbeddedFace): ReadonlyMap<number, readonly number[]>;
48
+ declare function collectEmbeddedGlyphs(texts: Readonly<Iterable<string>>, face: EmbeddedFace): ReadonlyMap<number, readonly number[]>;
49
49
  //#endregion
50
50
  export { EmbeddedShow as a, encodeForShowEmbedded as c, EmbeddedKern as i, loadEmbeddedFace as l, EmbeddedFaceMetrics as n, NOTDEF_GLYPH_ID as o, EmbeddedFaceSubstitution as r, collectEmbeddedGlyphs as s, EmbeddedFace as t };
@@ -1,5 +1,5 @@
1
- import { n as PdfDict, r as PdfObject } from "./objects-B7HdMeWS.cjs";
2
- import { t as EmbeddedFace } from "./embedded-font-CEf-zBCA.cjs";
1
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.cjs";
2
+ import { t as EmbeddedFace } from "./embedded-font-DgVBedw6.cjs";
3
3
  import { SfntSubsetResult } from "./sfnt-subset.cjs";
4
4
  //#region src/embedded-font-write.d.ts
5
5
  declare function embeddedSubsetTag(postScriptName: string, glyphIds: readonly number[]): string;
@@ -1,5 +1,5 @@
1
- import { n as PdfDict, r as PdfObject } from "./objects-B7HdMeWS.js";
2
- import { t as EmbeddedFace } from "./embedded-font-Bnhh4juV.js";
1
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.js";
2
+ import { t as EmbeddedFace } from "./embedded-font-ByD7ZiQS.js";
3
3
  import { SfntSubsetResult } from "./sfnt-subset.js";
4
4
  //#region src/embedded-font-write.d.ts
5
5
  declare function embeddedSubsetTag(postScriptName: string, glyphIds: readonly number[]): string;
@@ -111,6 +111,8 @@ function parseHhea(font) {
111
111
  };
112
112
  }
113
113
  const CID_BYTE_LENGTH = 2;
114
+ const BITS_PER_BYTE = 8;
115
+ const BYTE_MASK = 255;
114
116
  function encodeForShowEmbedded(text, face) {
115
117
  const resolvedGlyphIds = [];
116
118
  const substitutions = [];
@@ -137,8 +139,8 @@ function encodeForShowEmbedded(text, face) {
137
139
  }
138
140
  const codes = new Uint8Array(shownGlyphIds.length * CID_BYTE_LENGTH);
139
141
  shownGlyphIds.forEach((glyphId, index) => {
140
- codes[index * CID_BYTE_LENGTH] = glyphId >> 8 & 255;
141
- codes[index * CID_BYTE_LENGTH + 1] = glyphId & 255;
142
+ codes[index * CID_BYTE_LENGTH] = glyphId >> BITS_PER_BYTE & BYTE_MASK;
143
+ codes[index * CID_BYTE_LENGTH + 1] = glyphId & BYTE_MASK;
142
144
  });
143
145
  return {
144
146
  codes,
@@ -1,2 +1,2 @@
1
- import { a as EmbeddedShow, c as encodeForShowEmbedded, i as EmbeddedKern, l as loadEmbeddedFace, n as EmbeddedFaceMetrics, o as NOTDEF_GLYPH_ID, r as EmbeddedFaceSubstitution, s as collectEmbeddedGlyphs, t as EmbeddedFace } from "./embedded-font-CEf-zBCA.cjs";
1
+ import { a as EmbeddedShow, c as encodeForShowEmbedded, i as EmbeddedKern, l as loadEmbeddedFace, n as EmbeddedFaceMetrics, o as NOTDEF_GLYPH_ID, r as EmbeddedFaceSubstitution, s as collectEmbeddedGlyphs, t as EmbeddedFace } from "./embedded-font-DgVBedw6.cjs";
2
2
  export { EmbeddedFace, EmbeddedFaceMetrics, EmbeddedFaceSubstitution, EmbeddedKern, EmbeddedShow, NOTDEF_GLYPH_ID, collectEmbeddedGlyphs, encodeForShowEmbedded, loadEmbeddedFace };