pdf-parser.js 5.2.27 → 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 (145) 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.js +5 -3
  7. package/dist/builtin-encoding.cjs +17 -6
  8. package/dist/builtin-encoding.js +17 -6
  9. package/dist/bytes/crc32.cjs +5 -3
  10. package/dist/bytes/crc32.js +5 -3
  11. package/dist/bytes/flate.cjs +0 -1
  12. package/dist/bytes/flate.js +0 -1
  13. package/dist/cff-bounds.cjs +42 -24
  14. package/dist/cff-bounds.js +42 -24
  15. package/dist/cff.cjs +13 -6
  16. package/dist/cff.js +13 -6
  17. package/dist/cmap-table.cjs +54 -26
  18. package/dist/cmap-table.js +54 -26
  19. package/dist/cmap.cjs +5 -3
  20. package/dist/cmap.js +5 -3
  21. package/dist/content-write.cjs +3 -2
  22. package/dist/content-write.js +3 -2
  23. package/dist/crypto/aes.cjs +76 -55
  24. package/dist/crypto/aes.js +76 -55
  25. package/dist/crypto/md5.cjs +345 -149
  26. package/dist/crypto/md5.js +345 -149
  27. package/dist/crypto/rc4.cjs +5 -4
  28. package/dist/crypto/rc4.js +5 -4
  29. package/dist/crypto/sha2.cjs +92 -43
  30. package/dist/crypto/sha2.js +92 -43
  31. package/dist/embedded-font.cjs +4 -2
  32. package/dist/embedded-font.js +4 -2
  33. package/dist/encoding.cjs +1 -1
  34. package/dist/encoding.js +1 -1
  35. package/dist/encrypt-write.cjs +25 -21
  36. package/dist/encrypt-write.js +25 -21
  37. package/dist/encrypt.cjs +43 -64
  38. package/dist/encrypt.js +43 -64
  39. package/dist/filters.cjs +40 -20
  40. package/dist/filters.js +40 -20
  41. package/dist/font-face.cjs +2 -1
  42. package/dist/font-face.js +2 -1
  43. package/dist/font-read.cjs +12 -6
  44. package/dist/font-read.js +12 -6
  45. package/dist/font-tables.cjs +55 -28
  46. package/dist/font-tables.js +55 -28
  47. package/dist/gdef-table.cjs +6 -3
  48. package/dist/gdef-table.js +6 -3
  49. package/dist/glyf-contours.cjs +3 -1
  50. package/dist/glyf-contours.js +3 -1
  51. package/dist/glyf.cjs +22 -11
  52. package/dist/glyf.js +22 -11
  53. package/dist/gpos-table.cjs +37 -19
  54. package/dist/gpos-table.js +37 -19
  55. package/dist/gsub-table.cjs +28 -17
  56. package/dist/gsub-table.js +28 -17
  57. package/dist/hmtx-table.cjs +5 -3
  58. package/dist/hmtx-table.js +5 -3
  59. package/dist/image/ccitt-encode.cjs +8 -5
  60. package/dist/image/ccitt-encode.js +8 -5
  61. package/dist/image/ccitt.cjs +801 -209
  62. package/dist/image/ccitt.js +801 -209
  63. package/dist/image/jbig2-arith.cjs +340 -306
  64. package/dist/image/jbig2-arith.js +340 -306
  65. package/dist/image/jbig2-bitmap.cjs +8 -5
  66. package/dist/image/jbig2-bitmap.js +8 -5
  67. package/dist/image/jbig2-generic.cjs +240 -69
  68. package/dist/image/jbig2-generic.js +240 -69
  69. package/dist/image/jbig2-text.cjs +4 -2
  70. package/dist/image/jbig2-text.js +4 -2
  71. package/dist/image/jbig2.cjs +69 -31
  72. package/dist/image/jbig2.js +69 -31
  73. package/dist/image/jp2-boxes.cjs +52 -27
  74. package/dist/image/jp2-boxes.js +52 -27
  75. package/dist/image/jpeg-info.cjs +29 -24
  76. package/dist/image/jpeg-info.js +29 -24
  77. package/dist/image/jpeg2000-codestream.cjs +44 -24
  78. package/dist/image/jpeg2000-codestream.js +44 -24
  79. package/dist/image/jpeg2000-dwt.cjs +2 -1
  80. package/dist/image/jpeg2000-dwt.js +2 -1
  81. package/dist/image/jpeg2000-t1.cjs +21 -11
  82. package/dist/image/jpeg2000-t1.js +21 -11
  83. package/dist/image/jpeg2000-t2.cjs +12 -5
  84. package/dist/image/jpeg2000-t2.js +12 -5
  85. package/dist/image/jpeg2000-tagtree.cjs +8 -4
  86. package/dist/image/jpeg2000-tagtree.js +8 -4
  87. package/dist/image/jpeg2000.cjs +11 -6
  88. package/dist/image/jpeg2000.js +11 -6
  89. package/dist/image/png-decode.cjs +62 -39
  90. package/dist/image/png-decode.d.cts +3 -2
  91. package/dist/image/png-decode.d.ts +3 -2
  92. package/dist/image/png-decode.js +62 -39
  93. package/dist/image/png-filter.cjs +23 -15
  94. package/dist/image/png-filter.js +23 -15
  95. package/dist/images-read.cjs +41 -36
  96. package/dist/images-read.js +41 -36
  97. package/dist/index.cjs +1 -1
  98. package/dist/index.js +1 -1
  99. package/dist/interpret.cjs +39 -24
  100. package/dist/interpret.js +39 -24
  101. package/dist/lexer.cjs +86 -51
  102. package/dist/lexer.js +86 -51
  103. package/dist/math-content-write.cjs +9 -5
  104. package/dist/math-content-write.js +9 -5
  105. package/dist/math-font-write.cjs +2 -1
  106. package/dist/math-font-write.js +2 -1
  107. package/dist/math-font.cjs +16 -7
  108. package/dist/math-font.js +16 -7
  109. package/dist/math-table.cjs +25 -13
  110. package/dist/math-table.js +25 -13
  111. package/dist/matrix.cjs +3 -2
  112. package/dist/matrix.js +3 -2
  113. package/dist/measure.cjs +1 -1
  114. package/dist/measure.js +1 -1
  115. package/dist/navigation.cjs +10 -5
  116. package/dist/navigation.js +10 -5
  117. package/dist/ot-layout-common.cjs +4 -2
  118. package/dist/ot-layout-common.js +4 -2
  119. package/dist/parse.cjs +7 -5
  120. package/dist/parse.js +7 -5
  121. package/dist/pdf-text.cjs +5 -2
  122. package/dist/pdf-text.js +5 -2
  123. package/dist/predictors.cjs +14 -9
  124. package/dist/predictors.js +14 -9
  125. package/dist/raster.cjs +30 -19
  126. package/dist/raster.js +30 -19
  127. package/dist/read.cjs +17 -8
  128. package/dist/read.js +17 -8
  129. package/dist/serialize.cjs +6 -2
  130. package/dist/serialize.js +6 -2
  131. package/dist/sfnt-subset.cjs +25 -13
  132. package/dist/sfnt-subset.js +25 -13
  133. package/dist/sfnt.cjs +20 -10
  134. package/dist/sfnt.js +20 -10
  135. package/dist/text-group.cjs +0 -1
  136. package/dist/text-group.js +0 -1
  137. package/dist/tounicode.cjs +4 -2
  138. package/dist/tounicode.js +4 -2
  139. package/dist/winansi.cjs +1 -1
  140. package/dist/winansi.js +1 -1
  141. package/dist/write.cjs +20 -12
  142. package/dist/write.js +20 -12
  143. package/dist/xref.cjs +5 -2
  144. package/dist/xref.js +5 -2
  145. 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 };
@@ -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,
@@ -110,6 +110,8 @@ function parseHhea(font) {
110
110
  };
111
111
  }
112
112
  const CID_BYTE_LENGTH = 2;
113
+ const BITS_PER_BYTE = 8;
114
+ const BYTE_MASK = 255;
113
115
  function encodeForShowEmbedded(text, face) {
114
116
  const resolvedGlyphIds = [];
115
117
  const substitutions = [];
@@ -136,8 +138,8 @@ function encodeForShowEmbedded(text, face) {
136
138
  }
137
139
  const codes = new Uint8Array(shownGlyphIds.length * CID_BYTE_LENGTH);
138
140
  shownGlyphIds.forEach((glyphId, index) => {
139
- codes[index * CID_BYTE_LENGTH] = glyphId >> 8 & 255;
140
- codes[index * CID_BYTE_LENGTH + 1] = glyphId & 255;
141
+ codes[index * CID_BYTE_LENGTH] = glyphId >> BITS_PER_BYTE & BYTE_MASK;
142
+ codes[index * CID_BYTE_LENGTH + 1] = glyphId & BYTE_MASK;
141
143
  });
142
144
  return {
143
145
  codes,
package/dist/encoding.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_afm_widths = require("./afm-widths-BYiX2L9D.cjs");
2
+ const require_afm_widths = require("./afm-widths-BHD2HZIL.cjs");
3
3
  exports.MACROMAN_GLYPH_NAMES = require_afm_widths.MACROMAN_GLYPH_NAMES;
4
4
  exports.STANDARD_GLYPH_NAMES = require_afm_widths.STANDARD_GLYPH_NAMES;
5
5
  exports.SYMBOL_GLYPH_NAMES = require_afm_widths.SYMBOL_GLYPH_NAMES;
package/dist/encoding.js CHANGED
@@ -1,2 +1,2 @@
1
- import { a as SYMBOL_GLYPH_NAMES, c as glyphNameToUnicode, d as standardGlyphName, f as symbolGlyphName, i as STANDARD_GLYPH_NAMES, l as macRomanGlyphName, m as zapfDingbatsGlyphName, o as WINANSI_GLYPH_NAMES, p as winAnsiGlyphName, r as MACROMAN_GLYPH_NAMES, s as ZAPFDINGBATS_GLYPH_NAMES, u as namedEncodingGlyphName } from "./afm-widths-blUDDjlI.js";
1
+ import { a as SYMBOL_GLYPH_NAMES, c as glyphNameToUnicode, d as standardGlyphName, f as symbolGlyphName, i as STANDARD_GLYPH_NAMES, l as macRomanGlyphName, m as zapfDingbatsGlyphName, o as WINANSI_GLYPH_NAMES, p as winAnsiGlyphName, r as MACROMAN_GLYPH_NAMES, s as ZAPFDINGBATS_GLYPH_NAMES, u as namedEncodingGlyphName } from "./afm-widths-B_IGbGky.js";
2
2
  export { MACROMAN_GLYPH_NAMES, STANDARD_GLYPH_NAMES, SYMBOL_GLYPH_NAMES, WINANSI_GLYPH_NAMES, ZAPFDINGBATS_GLYPH_NAMES, glyphNameToUnicode, macRomanGlyphName, namedEncodingGlyphName, standardGlyphName, symbolGlyphName, winAnsiGlyphName, zapfDingbatsGlyphName };
@@ -15,6 +15,8 @@ var PdfEncryptionError = class extends Error {
15
15
  };
16
16
  const RC4_128_KEY_BYTES = 16;
17
17
  const R6_PASSWORD_MAX_BYTES = 127;
18
+ const BITS_PER_BYTE = 8;
19
+ const MAX_PERMISSION_BITS = 32;
18
20
  const RESERVED_BIT_7_AND_8 = 192;
19
21
  const DEPRECATED_ALWAYS_ONE_BIT_10 = 512;
20
22
  const SCHEME_SPECS = {
@@ -43,11 +45,12 @@ const SCHEME_SPECS = {
43
45
  method: "aes"
44
46
  }
45
47
  };
48
+ const MAX_PRINTABLE_ASCII = 127;
46
49
  function legacyPasswordBytes(password) {
47
50
  const bytes = new Uint8Array(password.length);
48
51
  for (let i = 0; i < password.length; i++) {
49
52
  const code = password.charCodeAt(i);
50
- if (code > 127) throw new PdfEncryptionError(`password contains character code ${String(code)}, outside the printable-ASCII range this writer supports for the rc4-40/rc4-128/aes-128 schemes; use a plain-ASCII password, or the aes-256 scheme, whose revision-6 passwords are Unicode`);
53
+ if (code > MAX_PRINTABLE_ASCII) throw new PdfEncryptionError(`password contains character code ${String(code)}, outside the printable-ASCII range this writer supports for the rc4-40/rc4-128/aes-128 schemes; use a plain-ASCII password, or the aes-256 scheme, whose revision-6 passwords are Unicode`);
51
54
  bytes[i] = code;
52
55
  }
53
56
  return bytes;
@@ -66,15 +69,16 @@ function permissionsToP(permissions) {
66
69
  p |= DEPRECATED_ALWAYS_ONE_BIT_10;
67
70
  if (permissions?.assemble ?? true) p |= 1024;
68
71
  if (permissions?.printHighRes ?? true) p |= 2048;
69
- for (let bit = 13; bit <= 32; bit++) p |= 1 << bit - 1;
72
+ for (let bit = 13; bit <= MAX_PERMISSION_BITS; bit++) p |= 1 << bit - 1;
70
73
  return p;
71
74
  }
75
+ const ITERATED_REVISION_MIN = 3;
72
76
  function computeLegacyOwnerValue(ownerPassword, userPassword, revision, keyBytes) {
73
77
  let digest = require_crypto_md5.md5(require_encrypt.padOrTruncatePassword(ownerPassword));
74
- if (revision >= 3) for (let i = 0; i < 50; i++) digest = require_crypto_md5.md5(digest);
78
+ if (revision >= ITERATED_REVISION_MIN) for (let i = 0; i < 50; i++) digest = require_crypto_md5.md5(digest);
75
79
  const ownerKey = digest.subarray(0, keyBytes);
76
80
  let value = require_crypto_rc4.rc4(ownerKey, require_encrypt.padOrTruncatePassword(userPassword));
77
- if (revision >= 3) for (let i = 1; i <= 19; i++) {
81
+ if (revision >= ITERATED_REVISION_MIN) for (let i = 1; i <= 19; i++) {
78
82
  const roundKey = Uint8Array.from(ownerKey, (byte) => byte ^ i);
79
83
  value = require_crypto_rc4.rc4(roundKey, value);
80
84
  }
@@ -125,9 +129,9 @@ function buildLegacyEncryptor(spec, options, fileId) {
125
129
  ["O", require_objects.pdfHexString(owner)],
126
130
  ["U", require_objects.pdfHexString(user)],
127
131
  ["P", require_objects.pdfNum(p)],
128
- ["Length", require_objects.pdfNum(spec.keyBytes * 8)]
132
+ ["Length", require_objects.pdfNum(spec.keyBytes * BITS_PER_BYTE)]
129
133
  ]);
130
- if (spec.v === 4) {
134
+ if (spec.v === SCHEME_SPECS["aes-128"].v) {
131
135
  entries.set("CF", require_objects.pdfDict({ StdCF: require_objects.pdfDict({
132
136
  AuthEvent: require_objects.pdfName("DocOpen"),
133
137
  CFM: require_objects.pdfName("AESV2"),
@@ -166,21 +170,21 @@ function buildAes256Encryptor(spec, options) {
166
170
  ]);
167
171
  const ownerIntermediateKey = require_encrypt.hardenedHash(ownerPassword, ownerKeySalt, u, spec.r);
168
172
  const oe = require_crypto_aes.aesCbcEncrypt(ownerIntermediateKey, require_encrypt.ZERO_IV, fileKey);
173
+ const PERMS_P_BYTES = 4;
174
+ const PERMS_SIGN_EXTENSION_BYTES = new Uint8Array(PERMS_P_BYTES).fill(255);
175
+ const PERMS_METADATA_FLAG_OFFSET = 8;
176
+ const ENCRYPT_METADATA_TRUE_ASCII = 84;
177
+ const ENCRYPT_METADATA_FALSE_ASCII = 70;
178
+ const ADB_MARKER_OFFSET = 9;
179
+ const ADB_MARKER_BYTES = Uint8Array.from("adb", (c) => c.charCodeAt(0));
180
+ const PERMS_RANDOM_BYTES = 4;
181
+ const PERMS_RANDOM_OFFSET = ADB_MARKER_OFFSET + ADB_MARKER_BYTES.length;
169
182
  const permsBlock = /* @__PURE__ */ new Uint8Array(16);
170
183
  permsBlock.set(require_encrypt.permissionsBytes(p), 0);
171
- permsBlock.set([
172
- 255,
173
- 255,
174
- 255,
175
- 255
176
- ], 4);
177
- permsBlock[8] = encryptMetadata ? 84 : 70;
178
- permsBlock.set([
179
- 97,
180
- 100,
181
- 98
182
- ], 9);
183
- permsBlock.set(require_crypto_random.randomBytes(4), 12);
184
+ permsBlock.set(PERMS_SIGN_EXTENSION_BYTES, PERMS_P_BYTES);
185
+ permsBlock[PERMS_METADATA_FLAG_OFFSET] = encryptMetadata ? ENCRYPT_METADATA_TRUE_ASCII : ENCRYPT_METADATA_FALSE_ASCII;
186
+ permsBlock.set(ADB_MARKER_BYTES, ADB_MARKER_OFFSET);
187
+ permsBlock.set(require_crypto_random.randomBytes(PERMS_RANDOM_BYTES), PERMS_RANDOM_OFFSET);
184
188
  const perms = require_crypto_aes.aesCbcEncrypt(fileKey, require_encrypt.ZERO_IV, permsBlock);
185
189
  const entries = /* @__PURE__ */ new Map([
186
190
  ["Filter", require_objects.pdfName("Standard")],
@@ -192,7 +196,7 @@ function buildAes256Encryptor(spec, options) {
192
196
  ["UE", require_objects.pdfHexString(ue)],
193
197
  ["P", require_objects.pdfNum(p)],
194
198
  ["Perms", require_objects.pdfHexString(perms)],
195
- ["Length", require_objects.pdfNum(spec.keyBytes * 8)],
199
+ ["Length", require_objects.pdfNum(spec.keyBytes * BITS_PER_BYTE)],
196
200
  ["CF", require_objects.pdfDict({ StdCF: require_objects.pdfDict({
197
201
  AuthEvent: require_objects.pdfName("DocOpen"),
198
202
  CFM: require_objects.pdfName("AESV3"),
@@ -207,7 +211,7 @@ function buildAes256Encryptor(spec, options) {
207
211
  function createStandardEncryptor(options, fileId) {
208
212
  const scheme = options.scheme ?? "aes-256";
209
213
  const spec = SCHEME_SPECS[scheme];
210
- return spec.v === 5 ? buildAes256Encryptor(spec, options) : buildLegacyEncryptor(spec, options, fileId);
214
+ return spec.v === SCHEME_SPECS["aes-256"].v ? buildAes256Encryptor(spec, options) : buildLegacyEncryptor(spec, options, fileId);
211
215
  }
212
216
  function encryptIndirectObject(value, num, gen, encryptor) {
213
217
  if (value.kind === "string") return {