ooxml-core 0.9.0 → 0.10.0

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 (286) hide show
  1. package/README.md +14 -13
  2. package/dist/crypto/aes-ecb.d.ts +13 -0
  3. package/dist/crypto/aes-ecb.d.ts.map +1 -0
  4. package/dist/crypto/decrypt.d.ts +63 -0
  5. package/dist/crypto/decrypt.d.ts.map +1 -0
  6. package/dist/crypto/detect.d.ts +56 -0
  7. package/dist/crypto/detect.d.ts.map +1 -0
  8. package/dist/crypto/encrypt.d.ts +27 -0
  9. package/dist/crypto/encrypt.d.ts.map +1 -0
  10. package/dist/crypto/encryption-info-write.d.ts +46 -0
  11. package/dist/crypto/encryption-info-write.d.ts.map +1 -0
  12. package/dist/crypto/encryption-info.d.ts +22 -0
  13. package/dist/crypto/encryption-info.d.ts.map +1 -0
  14. package/dist/crypto/errors.d.ts +39 -0
  15. package/dist/crypto/errors.d.ts.map +1 -0
  16. package/dist/crypto/index.cjs +1805 -0
  17. package/dist/crypto/index.d.ts +26 -0
  18. package/dist/crypto/index.d.ts.map +1 -0
  19. package/dist/crypto/index.mjs +1774 -0
  20. package/dist/crypto/key-derivation.d.ts +73 -0
  21. package/dist/crypto/key-derivation.d.ts.map +1 -0
  22. package/dist/crypto/types.d.ts +104 -0
  23. package/dist/crypto/types.d.ts.map +1 -0
  24. package/dist/docx/embedded.cjs +2 -0
  25. package/dist/docx/embedded.mjs +2 -0
  26. package/dist/docx/index.cjs +2 -0
  27. package/dist/docx/index.mjs +2 -0
  28. package/dist/docx/load/detect.d.ts +14 -2
  29. package/dist/docx/load/detect.d.ts.map +1 -1
  30. package/dist/docx/load/index.cjs +8995 -7728
  31. package/dist/docx/load/index.d.ts +3 -1
  32. package/dist/docx/load/index.d.ts.map +1 -1
  33. package/dist/docx/load/index.mjs +8990 -7729
  34. package/dist/docx/load/legacy-doc.d.ts.map +1 -1
  35. package/dist/index.cjs +749 -19
  36. package/dist/index.mjs +749 -19
  37. package/dist/opc/index.cjs +797 -1
  38. package/dist/opc/index.d.ts +2 -0
  39. package/dist/opc/index.d.ts.map +1 -1
  40. package/dist/opc/index.mjs +784 -3
  41. package/dist/opc/properties/app.d.ts +10 -0
  42. package/dist/opc/properties/app.d.ts.map +1 -0
  43. package/dist/opc/properties/core.d.ts +13 -0
  44. package/dist/opc/properties/core.d.ts.map +1 -0
  45. package/dist/opc/properties/custom.d.ts +15 -0
  46. package/dist/opc/properties/custom.d.ts.map +1 -0
  47. package/dist/opc/properties/index.d.ts +5 -0
  48. package/dist/opc/properties/index.d.ts.map +1 -0
  49. package/dist/opc/properties/types.d.ts +114 -0
  50. package/dist/opc/properties/types.d.ts.map +1 -0
  51. package/dist/opc/relationship-types.d.ts +1 -0
  52. package/dist/opc/relationship-types.d.ts.map +1 -1
  53. package/dist/opc/safe-href.d.ts +34 -0
  54. package/dist/opc/safe-href.d.ts.map +1 -1
  55. package/dist/opc/signature/constants.d.ts +42 -0
  56. package/dist/opc/signature/constants.d.ts.map +1 -0
  57. package/dist/opc/signature/detection.d.ts +97 -0
  58. package/dist/opc/signature/detection.d.ts.map +1 -0
  59. package/dist/opc/signature/digest.d.ts +16 -0
  60. package/dist/opc/signature/digest.d.ts.map +1 -0
  61. package/dist/opc/signature/index.d.ts +8 -0
  62. package/dist/opc/signature/index.d.ts.map +1 -0
  63. package/dist/opc/signature/inspection-status.d.ts +24 -0
  64. package/dist/opc/signature/inspection-status.d.ts.map +1 -0
  65. package/dist/opc/signature/reference-utils.d.ts +12 -0
  66. package/dist/opc/signature/reference-utils.d.ts.map +1 -0
  67. package/dist/opc/signature/types.d.ts +90 -0
  68. package/dist/opc/signature/types.d.ts.map +1 -0
  69. package/dist/opc/signature/xml-utils.d.ts +35 -0
  70. package/dist/opc/signature/xml-utils.d.ts.map +1 -0
  71. package/dist/pptx/chunks/{index-CojJtI77.d.ts → index-BWUBPGgv.d.ts} +9 -11
  72. package/dist/pptx/chunks/index-BWUBPGgv.d.ts.map +1 -0
  73. package/dist/pptx/chunks/{signature-inspection-status-Bh2oap7B.d.ts → inspection-status-B2E-jSpG.d.ts} +8 -8
  74. package/dist/pptx/chunks/inspection-status-B2E-jSpG.d.ts.map +1 -0
  75. package/dist/pptx/cli/index.cjs +1409 -895
  76. package/dist/pptx/cli/index.mjs +1409 -895
  77. package/dist/pptx/converter/index.d.ts +1 -1
  78. package/dist/pptx/index.cjs +1933 -1391
  79. package/dist/pptx/index.d.ts +20 -67
  80. package/dist/pptx/index.d.ts.map +1 -1
  81. package/dist/pptx/index.mjs +1933 -1391
  82. package/dist/pptx/signature-node/index.cjs +5 -5
  83. package/dist/pptx/signature-node/index.d.ts +1 -1
  84. package/dist/pptx/signature-node/index.mjs +5 -5
  85. package/dist/visio/edit-master-index.d.ts +10 -0
  86. package/dist/visio/edit-master-index.d.ts.map +1 -0
  87. package/dist/visio/edit-master-scope.d.ts +7 -0
  88. package/dist/visio/edit-master-scope.d.ts.map +1 -0
  89. package/dist/visio/edit-scope.d.ts.map +1 -1
  90. package/dist/visio/formula-arithmetic.d.ts +8 -0
  91. package/dist/visio/formula-arithmetic.d.ts.map +1 -0
  92. package/dist/visio/formula-evaluate.d.ts.map +1 -1
  93. package/dist/visio/formula-master.d.ts +13 -0
  94. package/dist/visio/formula-master.d.ts.map +1 -0
  95. package/dist/visio/formula-numeric-functions.d.ts +8 -0
  96. package/dist/visio/formula-numeric-functions.d.ts.map +1 -0
  97. package/dist/visio/formula.d.ts +1 -1
  98. package/dist/visio/formula.d.ts.map +1 -1
  99. package/dist/visio/index.cjs +540 -61
  100. package/dist/visio/index.mjs +540 -61
  101. package/dist/xlsx/edit/annotations.d.ts +5 -1
  102. package/dist/xlsx/edit/annotations.d.ts.map +1 -1
  103. package/dist/xlsx/edit/cell-values.d.ts +7 -2
  104. package/dist/xlsx/edit/cell-values.d.ts.map +1 -1
  105. package/dist/xlsx/edit/clipboard.d.ts.map +1 -1
  106. package/dist/xlsx/edit/doc-properties.d.ts +14 -0
  107. package/dist/xlsx/edit/doc-properties.d.ts.map +1 -0
  108. package/dist/xlsx/edit/fill-series.d.ts.map +1 -1
  109. package/dist/xlsx/edit/find.d.ts +2 -1
  110. package/dist/xlsx/edit/find.d.ts.map +1 -1
  111. package/dist/xlsx/edit/format-lines.d.ts +22 -0
  112. package/dist/xlsx/edit/format-lines.d.ts.map +1 -0
  113. package/dist/xlsx/edit/format.d.ts +2 -1
  114. package/dist/xlsx/edit/format.d.ts.map +1 -1
  115. package/dist/xlsx/edit/history-refs.d.ts +51 -0
  116. package/dist/xlsx/edit/history-refs.d.ts.map +1 -0
  117. package/dist/xlsx/edit/history.d.ts +40 -5
  118. package/dist/xlsx/edit/history.d.ts.map +1 -1
  119. package/dist/xlsx/edit/index.d.ts +3 -0
  120. package/dist/xlsx/edit/index.d.ts.map +1 -1
  121. package/dist/xlsx/edit/input-text.d.ts +13 -0
  122. package/dist/xlsx/edit/input-text.d.ts.map +1 -0
  123. package/dist/xlsx/edit/merge.d.ts +10 -2
  124. package/dist/xlsx/edit/merge.d.ts.map +1 -1
  125. package/dist/xlsx/edit/session.d.ts.map +1 -1
  126. package/dist/xlsx/edit/sheets.d.ts.map +1 -1
  127. package/dist/xlsx/edit/shift-formulas.d.ts +2 -1
  128. package/dist/xlsx/edit/shift-formulas.d.ts.map +1 -1
  129. package/dist/xlsx/edit/shift-preserved.d.ts +17 -0
  130. package/dist/xlsx/edit/shift-preserved.d.ts.map +1 -0
  131. package/dist/xlsx/edit/shift-sheet.d.ts +2 -2
  132. package/dist/xlsx/edit/shift-sheet.d.ts.map +1 -1
  133. package/dist/xlsx/edit/structure.d.ts.map +1 -1
  134. package/dist/xlsx/edit/types.d.ts +7 -0
  135. package/dist/xlsx/edit/types.d.ts.map +1 -1
  136. package/dist/xlsx/edit/validation-list.d.ts +24 -0
  137. package/dist/xlsx/edit/validation-list.d.ts.map +1 -0
  138. package/dist/xlsx/edit/validation.d.ts +2 -1
  139. package/dist/xlsx/edit/validation.d.ts.map +1 -1
  140. package/dist/xlsx/formula/area-index.d.ts +18 -0
  141. package/dist/xlsx/formula/area-index.d.ts.map +1 -0
  142. package/dist/xlsx/formula/array-context.d.ts.map +1 -1
  143. package/dist/xlsx/formula/ast.d.ts +5 -1
  144. package/dist/xlsx/formula/ast.d.ts.map +1 -1
  145. package/dist/xlsx/formula/call.d.ts.map +1 -1
  146. package/dist/xlsx/formula/coerce.d.ts.map +1 -1
  147. package/dist/xlsx/formula/context.d.ts +5 -0
  148. package/dist/xlsx/formula/context.d.ts.map +1 -1
  149. package/dist/xlsx/formula/date-serial.d.ts +11 -4
  150. package/dist/xlsx/formula/date-serial.d.ts.map +1 -1
  151. package/dist/xlsx/formula/engine-core.d.ts +6 -0
  152. package/dist/xlsx/formula/engine-core.d.ts.map +1 -1
  153. package/dist/xlsx/formula/engine-host.d.ts +35 -3
  154. package/dist/xlsx/formula/engine-host.d.ts.map +1 -1
  155. package/dist/xlsx/formula/engine-util.d.ts.map +1 -1
  156. package/dist/xlsx/formula/engine.d.ts.map +1 -1
  157. package/dist/xlsx/formula/evaluator.d.ts.map +1 -1
  158. package/dist/xlsx/formula/functions/array-shape.d.ts.map +1 -1
  159. package/dist/xlsx/formula/functions/array.d.ts.map +1 -1
  160. package/dist/xlsx/formula/functions/criteria-index.d.ts +12 -0
  161. package/dist/xlsx/formula/functions/criteria-index.d.ts.map +1 -0
  162. package/dist/xlsx/formula/functions/criteria.d.ts +24 -16
  163. package/dist/xlsx/formula/functions/criteria.d.ts.map +1 -1
  164. package/dist/xlsx/formula/functions/date-calc.d.ts +4 -0
  165. package/dist/xlsx/formula/functions/date-calc.d.ts.map +1 -1
  166. package/dist/xlsx/formula/functions/depreciation.d.ts.map +1 -1
  167. package/dist/xlsx/formula/functions/financial.d.ts.map +1 -1
  168. package/dist/xlsx/formula/functions/helpers.d.ts.map +1 -1
  169. package/dist/xlsx/formula/functions/logical.d.ts.map +1 -1
  170. package/dist/xlsx/formula/functions/lookup-core.d.ts +14 -2
  171. package/dist/xlsx/formula/functions/lookup-core.d.ts.map +1 -1
  172. package/dist/xlsx/formula/functions/lookup.d.ts.map +1 -1
  173. package/dist/xlsx/formula/functions/math-mod.d.ts +8 -0
  174. package/dist/xlsx/formula/functions/math-mod.d.ts.map +1 -0
  175. package/dist/xlsx/formula/functions/math-sum.d.ts.map +1 -1
  176. package/dist/xlsx/formula/functions/math.d.ts.map +1 -1
  177. package/dist/xlsx/formula/functions/statistical-count.d.ts.map +1 -1
  178. package/dist/xlsx/formula/functions/statistical.d.ts.map +1 -1
  179. package/dist/xlsx/formula/functions/stats-core.d.ts +6 -0
  180. package/dist/xlsx/formula/functions/stats-core.d.ts.map +1 -1
  181. package/dist/xlsx/formula/functions/text-case.d.ts +14 -0
  182. package/dist/xlsx/formula/functions/text-case.d.ts.map +1 -0
  183. package/dist/xlsx/formula/functions/text.d.ts.map +1 -1
  184. package/dist/xlsx/formula/functions/workdays.d.ts.map +1 -1
  185. package/dist/xlsx/formula/graph-index.d.ts +15 -0
  186. package/dist/xlsx/formula/graph-index.d.ts.map +1 -0
  187. package/dist/xlsx/formula/graph.d.ts +13 -18
  188. package/dist/xlsx/formula/graph.d.ts.map +1 -1
  189. package/dist/xlsx/formula/operators.d.ts +9 -0
  190. package/dist/xlsx/formula/operators.d.ts.map +1 -1
  191. package/dist/xlsx/formula/parser.d.ts +2 -0
  192. package/dist/xlsx/formula/parser.d.ts.map +1 -1
  193. package/dist/xlsx/formula/references.d.ts +4 -1
  194. package/dist/xlsx/formula/references.d.ts.map +1 -1
  195. package/dist/xlsx/formula/text-number.d.ts.map +1 -1
  196. package/dist/xlsx/formula/transform.d.ts.map +1 -1
  197. package/dist/xlsx/formula/values.d.ts +44 -3
  198. package/dist/xlsx/formula/values.d.ts.map +1 -1
  199. package/dist/xlsx/index.cjs +4698 -1322
  200. package/dist/xlsx/index.mjs +4688 -1323
  201. package/dist/xlsx/layout/anchors.d.ts +47 -0
  202. package/dist/xlsx/layout/anchors.d.ts.map +1 -0
  203. package/dist/xlsx/layout/cf-evaluator.d.ts +1 -2
  204. package/dist/xlsx/layout/cf-evaluator.d.ts.map +1 -1
  205. package/dist/xlsx/layout/cf-helpers.d.ts +8 -1
  206. package/dist/xlsx/layout/cf-helpers.d.ts.map +1 -1
  207. package/dist/xlsx/layout/index.d.ts +1 -0
  208. package/dist/xlsx/layout/index.d.ts.map +1 -1
  209. package/dist/xlsx/load/csv.d.ts +24 -3
  210. package/dist/xlsx/load/csv.d.ts.map +1 -1
  211. package/dist/xlsx/load/detect.d.ts +39 -7
  212. package/dist/xlsx/load/detect.d.ts.map +1 -1
  213. package/dist/xlsx/load/errors.d.ts +10 -0
  214. package/dist/xlsx/load/errors.d.ts.map +1 -0
  215. package/dist/xlsx/load/index.cjs +16220 -12163
  216. package/dist/xlsx/load/index.d.ts +5 -2
  217. package/dist/xlsx/load/index.d.ts.map +1 -1
  218. package/dist/xlsx/load/index.mjs +16213 -12164
  219. package/dist/xlsx/load/text-decode.d.ts +7 -0
  220. package/dist/xlsx/load/text-decode.d.ts.map +1 -0
  221. package/dist/xlsx/model.d.ts +121 -17
  222. package/dist/xlsx/model.d.ts.map +1 -1
  223. package/dist/xlsx/numfmt/date.d.ts +7 -0
  224. package/dist/xlsx/numfmt/date.d.ts.map +1 -1
  225. package/dist/xlsx/numfmt/format.d.ts.map +1 -1
  226. package/dist/xlsx/numfmt/fraction.d.ts +5 -1
  227. package/dist/xlsx/numfmt/fraction.d.ts.map +1 -1
  228. package/dist/xlsx/numfmt/input-date.d.ts.map +1 -1
  229. package/dist/xlsx/numfmt/input.d.ts.map +1 -1
  230. package/dist/xlsx/numfmt/number.d.ts.map +1 -1
  231. package/dist/xlsx/read/conditional.d.ts.map +1 -1
  232. package/dist/xlsx/read/drawing.d.ts +2 -2
  233. package/dist/xlsx/read/drawing.d.ts.map +1 -1
  234. package/dist/xlsx/read/load.d.ts.map +1 -1
  235. package/dist/xlsx/read/package.d.ts +18 -2
  236. package/dist/xlsx/read/package.d.ts.map +1 -1
  237. package/dist/xlsx/read/sheet-data.d.ts.map +1 -1
  238. package/dist/xlsx/read/sheet-props.d.ts +15 -1
  239. package/dist/xlsx/read/sheet-props.d.ts.map +1 -1
  240. package/dist/xlsx/read/smart-art.d.ts +12 -0
  241. package/dist/xlsx/read/smart-art.d.ts.map +1 -0
  242. package/dist/xlsx/read/styles.d.ts.map +1 -1
  243. package/dist/xlsx/read/workbook-part.d.ts +5 -2
  244. package/dist/xlsx/read/workbook-part.d.ts.map +1 -1
  245. package/dist/xlsx/read/worksheet.d.ts.map +1 -1
  246. package/dist/xlsx/write/attr-merge.d.ts +10 -0
  247. package/dist/xlsx/write/attr-merge.d.ts.map +1 -0
  248. package/dist/xlsx/write/carried-refs.d.ts +61 -0
  249. package/dist/xlsx/write/carried-refs.d.ts.map +1 -0
  250. package/dist/xlsx/write/comments.d.ts +12 -2
  251. package/dist/xlsx/write/comments.d.ts.map +1 -1
  252. package/dist/xlsx/write/conditional.d.ts.map +1 -1
  253. package/dist/xlsx/write/doc-props.d.ts +30 -0
  254. package/dist/xlsx/write/doc-props.d.ts.map +1 -0
  255. package/dist/xlsx/write/drawing.d.ts +5 -0
  256. package/dist/xlsx/write/drawing.d.ts.map +1 -1
  257. package/dist/xlsx/write/metadata.d.ts +23 -0
  258. package/dist/xlsx/write/metadata.d.ts.map +1 -0
  259. package/dist/xlsx/write/package-writer.d.ts +6 -0
  260. package/dist/xlsx/write/package-writer.d.ts.map +1 -1
  261. package/dist/xlsx/write/page-setup.d.ts +8 -0
  262. package/dist/xlsx/write/page-setup.d.ts.map +1 -0
  263. package/dist/xlsx/write/save.d.ts.map +1 -1
  264. package/dist/xlsx/write/sheet-data.d.ts +8 -0
  265. package/dist/xlsx/write/sheet-data.d.ts.map +1 -1
  266. package/dist/xlsx/write/sheet-parts.d.ts +6 -0
  267. package/dist/xlsx/write/sheet-parts.d.ts.map +1 -1
  268. package/dist/xlsx/write/sheet-props.d.ts +0 -6
  269. package/dist/xlsx/write/sheet-props.d.ts.map +1 -1
  270. package/dist/xlsx/write/styles.d.ts.map +1 -1
  271. package/dist/xlsx/write/table-patch.d.ts +12 -0
  272. package/dist/xlsx/write/table-patch.d.ts.map +1 -0
  273. package/dist/xlsx/write/tables.d.ts +9 -0
  274. package/dist/xlsx/write/tables.d.ts.map +1 -1
  275. package/dist/xlsx/write/vml-ids.d.ts +29 -0
  276. package/dist/xlsx/write/vml-ids.d.ts.map +1 -0
  277. package/dist/xlsx/write/workbook-part.d.ts +1 -4
  278. package/dist/xlsx/write/workbook-part.d.ts.map +1 -1
  279. package/dist/xlsx/write/worksheet.d.ts.map +1 -1
  280. package/dist/xml/index.cjs +4 -0
  281. package/dist/xml/index.mjs +4 -0
  282. package/dist/xml/namespaces.d.ts +4 -0
  283. package/dist/xml/namespaces.d.ts.map +1 -1
  284. package/package.json +6 -1
  285. package/dist/pptx/chunks/index-CojJtI77.d.ts.map +0 -1
  286. package/dist/pptx/chunks/signature-inspection-status-Bh2oap7B.d.ts.map +0 -1
@@ -0,0 +1,1805 @@
1
+ 'use strict';
2
+
3
+ // node_modules/.bun/@christophervr+ole2@0.4.0/node_modules/@christophervr/ole2/dist/ole2-parser-types.js
4
+ var OLE_MAGIC = new Uint8Array([208, 207, 17, 224, 161, 177, 26, 225]);
5
+ var ENDOFCHAIN = 4294967294;
6
+ var FREESECT = 4294967295;
7
+ var FATSECT = 4294967293;
8
+ var DIFSECT = 4294967292;
9
+ var MAXREGSECT = 4294967290;
10
+ var ENTRY_TYPE_EMPTY = 0;
11
+ var ENTRY_TYPE_STREAM = 2;
12
+ var ENTRY_TYPE_ROOT = 5;
13
+ var DIR_ENTRY_SIZE = 128;
14
+ var Ole2ParseError = class extends Error {
15
+ constructor(message) {
16
+ super(message);
17
+ this.name = "Ole2ParseError";
18
+ }
19
+ };
20
+
21
+ // node_modules/.bun/@christophervr+ole2@0.4.0/node_modules/@christophervr/ole2/dist/ole2-parser-read.js
22
+ function parseOle2(buffer) {
23
+ const data = new Uint8Array(buffer);
24
+ const view = new DataView(buffer);
25
+ for (let i = 0; i < OLE_MAGIC.length; i++) {
26
+ if (data[i] !== OLE_MAGIC[i]) {
27
+ throw new Ole2ParseError("Not a valid OLE2 compound file");
28
+ }
29
+ }
30
+ view.getUint16(24, true);
31
+ const majorVersion = view.getUint16(26, true);
32
+ const byteOrder = view.getUint16(28, true);
33
+ if (byteOrder !== 65534) {
34
+ throw new Ole2ParseError("Invalid byte order mark");
35
+ }
36
+ const sectorSizePower = view.getUint16(30, true);
37
+ const miniSectorSizePower = view.getUint16(32, true);
38
+ const sectorSize = 1 << sectorSizePower;
39
+ const miniSectorSize = 1 << miniSectorSizePower;
40
+ const totalFATSectors = view.getUint32(44, true);
41
+ const firstDirectorySector = view.getUint32(48, true);
42
+ const miniStreamCutoff = view.getUint32(56, true);
43
+ const firstMiniFATSector = view.getUint32(60, true);
44
+ const totalMiniFATSectors = view.getUint32(64, true);
45
+ const firstDIFATSector = view.getUint32(68, true);
46
+ const totalDIFATSectors = view.getUint32(72, true);
47
+ function sectorOffset(sector) {
48
+ return (sector + 1) * sectorSize;
49
+ }
50
+ function readSector(sector) {
51
+ const offset = sectorOffset(sector);
52
+ if (offset + sectorSize > data.length) {
53
+ throw new Ole2ParseError(`Sector ${sector} at offset ${offset} exceeds file size ${data.length}`);
54
+ }
55
+ return data.subarray(offset, offset + sectorSize);
56
+ }
57
+ const fatSectors = [];
58
+ for (let i = 0; i < 109 && fatSectors.length < totalFATSectors; i++) {
59
+ const sector = view.getUint32(76 + i * 4, true);
60
+ if (sector <= MAXREGSECT) {
61
+ fatSectors.push(sector);
62
+ }
63
+ }
64
+ let difatSector = firstDIFATSector;
65
+ for (let d = 0; d < totalDIFATSectors && difatSector <= MAXREGSECT; d++) {
66
+ const difatData = readSector(difatSector);
67
+ const difatView = new DataView(difatData.buffer, difatData.byteOffset, difatData.byteLength);
68
+ const entriesPerSector = (sectorSize - 4) / 4;
69
+ for (let i = 0; i < entriesPerSector && fatSectors.length < totalFATSectors; i++) {
70
+ const sector = difatView.getUint32(i * 4, true);
71
+ if (sector <= MAXREGSECT) {
72
+ fatSectors.push(sector);
73
+ }
74
+ }
75
+ difatSector = difatView.getUint32(sectorSize - 4, true);
76
+ }
77
+ const fatEntries = [];
78
+ for (const fatSector of fatSectors) {
79
+ const fatData = readSector(fatSector);
80
+ const fatView = new DataView(fatData.buffer, fatData.byteOffset, fatData.byteLength);
81
+ for (let i = 0; i < sectorSize / 4; i++) {
82
+ fatEntries.push(fatView.getUint32(i * 4, true));
83
+ }
84
+ }
85
+ function readSectorChain(startSector) {
86
+ const sectors = [];
87
+ let current = startSector;
88
+ const visited = /* @__PURE__ */ new Set();
89
+ while (current <= MAXREGSECT) {
90
+ if (visited.has(current)) {
91
+ throw new Ole2ParseError(`Circular reference in FAT chain at sector ${current}`);
92
+ }
93
+ visited.add(current);
94
+ sectors.push(readSector(current));
95
+ current = fatEntries[current] ?? ENDOFCHAIN;
96
+ }
97
+ const totalLength = sectors.length * sectorSize;
98
+ const result = new Uint8Array(totalLength);
99
+ let offset = 0;
100
+ for (const sector of sectors) {
101
+ result.set(sector, offset);
102
+ offset += sectorSize;
103
+ }
104
+ return result;
105
+ }
106
+ function readStream(startSector, size) {
107
+ const raw = readSectorChain(startSector);
108
+ return raw.subarray(0, Math.min(size, raw.length));
109
+ }
110
+ const miniFatEntries = [];
111
+ if (firstMiniFATSector <= MAXREGSECT && totalMiniFATSectors > 0) {
112
+ const miniFatRaw = readSectorChain(firstMiniFATSector);
113
+ const miniFatView = new DataView(miniFatRaw.buffer, miniFatRaw.byteOffset, miniFatRaw.byteLength);
114
+ for (let i = 0; i < miniFatRaw.length / 4; i++) {
115
+ miniFatEntries.push(miniFatView.getUint32(i * 4, true));
116
+ }
117
+ }
118
+ const dirRaw = readSectorChain(firstDirectorySector);
119
+ const numEntries = Math.floor(dirRaw.length / DIR_ENTRY_SIZE);
120
+ const entries = [];
121
+ for (let i = 0; i < numEntries; i++) {
122
+ const entryOffset = i * DIR_ENTRY_SIZE;
123
+ const entryView = new DataView(dirRaw.buffer, dirRaw.byteOffset + entryOffset, DIR_ENTRY_SIZE);
124
+ const nameLen = entryView.getUint16(64, true);
125
+ const objectType = entryView.getUint8(66);
126
+ if (objectType === ENTRY_TYPE_EMPTY) {
127
+ continue;
128
+ }
129
+ const nameBytes = Math.max(0, nameLen - 2);
130
+ let name = "";
131
+ for (let j = 0; j < nameBytes; j += 2) {
132
+ name += String.fromCharCode(entryView.getUint16(j, true));
133
+ }
134
+ const leftSiblingId = entryView.getUint32(68, true);
135
+ const rightSiblingId = entryView.getUint32(72, true);
136
+ const childId = entryView.getUint32(76, true);
137
+ const clsid = new Uint8Array(entryView.buffer.slice(entryView.byteOffset + 80, entryView.byteOffset + 96));
138
+ const startSector = entryView.getUint32(116, true);
139
+ const sizeLow = entryView.getUint32(120, true);
140
+ let size = sizeLow;
141
+ if (majorVersion === 4) {
142
+ entryView.getUint32(124, true);
143
+ size = sizeLow;
144
+ }
145
+ entries.push({
146
+ name,
147
+ type: objectType,
148
+ startSector,
149
+ size,
150
+ childId: childId === 4294967295 ? -1 : childId,
151
+ leftSiblingId: leftSiblingId === 4294967295 ? -1 : leftSiblingId,
152
+ rightSiblingId: rightSiblingId === 4294967295 ? -1 : rightSiblingId,
153
+ clsid
154
+ });
155
+ }
156
+ const rootEntry = entries.find((e) => e.type === ENTRY_TYPE_ROOT);
157
+ let miniStreamData;
158
+ if (rootEntry && rootEntry.startSector <= MAXREGSECT) {
159
+ miniStreamData = readSectorChain(rootEntry.startSector);
160
+ }
161
+ function readMiniStream(startSector, size) {
162
+ if (!miniStreamData) {
163
+ throw new Ole2ParseError("Mini stream container not found");
164
+ }
165
+ const sectors = [];
166
+ let current = startSector;
167
+ const visited = /* @__PURE__ */ new Set();
168
+ while (current <= MAXREGSECT) {
169
+ if (visited.has(current)) {
170
+ throw new Ole2ParseError(`Circular reference in mini FAT chain at sector ${current}`);
171
+ }
172
+ visited.add(current);
173
+ const offset2 = current * miniSectorSize;
174
+ sectors.push(miniStreamData.subarray(offset2, offset2 + miniSectorSize));
175
+ current = miniFatEntries[current] ?? ENDOFCHAIN;
176
+ }
177
+ const totalLength = sectors.length * miniSectorSize;
178
+ const result = new Uint8Array(totalLength);
179
+ let offset = 0;
180
+ for (const sector of sectors) {
181
+ result.set(sector, offset);
182
+ offset += miniSectorSize;
183
+ }
184
+ return result.subarray(0, Math.min(size, result.length));
185
+ }
186
+ function getStream(name) {
187
+ const entry = entries.find((e) => (e.type === ENTRY_TYPE_STREAM || e.type === ENTRY_TYPE_ROOT) && e.name === name);
188
+ if (!entry) {
189
+ return void 0;
190
+ }
191
+ if (entry.size < miniStreamCutoff && entry.type !== ENTRY_TYPE_ROOT) {
192
+ return readMiniStream(entry.startSector, entry.size);
193
+ }
194
+ return readStream(entry.startSector, entry.size);
195
+ }
196
+ return { entries, getStream };
197
+ }
198
+
199
+ // node_modules/.bun/@christophervr+ole2@0.4.0/node_modules/@christophervr/ole2/dist/utils/ooxml-crypto-primitives.js
200
+ function getSubtle() {
201
+ if (typeof globalThis.crypto !== "undefined" && globalThis.crypto.subtle) {
202
+ return globalThis.crypto.subtle;
203
+ }
204
+ throw new Error("Web Crypto API (crypto.subtle) is not available in this environment. Password-protected PPTX files require a runtime with Web Crypto support.");
205
+ }
206
+ function getCrypto() {
207
+ if (typeof globalThis.crypto !== "undefined") {
208
+ return globalThis.crypto;
209
+ }
210
+ throw new Error("crypto API is not available in this environment.");
211
+ }
212
+ function encodePasswordUtf16LE(password) {
213
+ const buf = new Uint8Array(password.length * 2);
214
+ for (let i = 0; i < password.length; i++) {
215
+ const code = password.charCodeAt(i);
216
+ buf[i * 2] = code & 255;
217
+ buf[i * 2 + 1] = code >> 8 & 255;
218
+ }
219
+ return buf;
220
+ }
221
+ function concatArrays(...arrays) {
222
+ let totalLength = 0;
223
+ for (const arr of arrays) {
224
+ totalLength += arr.length;
225
+ }
226
+ const result = new Uint8Array(totalLength);
227
+ let offset = 0;
228
+ for (const arr of arrays) {
229
+ result.set(arr, offset);
230
+ offset += arr.length;
231
+ }
232
+ return result;
233
+ }
234
+ function uint32LE(value) {
235
+ const buf = new Uint8Array(4);
236
+ const view = new DataView(buf.buffer);
237
+ view.setUint32(0, value, true);
238
+ return buf;
239
+ }
240
+ function base64Decode(str) {
241
+ const binary = atob(str);
242
+ const bytes = new Uint8Array(binary.length);
243
+ for (let i = 0; i < binary.length; i++) {
244
+ bytes[i] = binary.charCodeAt(i);
245
+ }
246
+ return bytes;
247
+ }
248
+ function base64Encode(bytes) {
249
+ let binary = "";
250
+ for (let i = 0; i < bytes.length; i++) {
251
+ binary += String.fromCharCode(bytes[i]);
252
+ }
253
+ return btoa(binary);
254
+ }
255
+ function mapHashAlgorithm(algorithm) {
256
+ const upper = algorithm.toUpperCase().replace(/-/g, "");
257
+ switch (upper) {
258
+ case "SHA1":
259
+ return "SHA-1";
260
+ case "SHA256":
261
+ return "SHA-256";
262
+ case "SHA384":
263
+ return "SHA-384";
264
+ case "SHA512":
265
+ return "SHA-512";
266
+ default:
267
+ return algorithm;
268
+ }
269
+ }
270
+ async function hash(algorithm, data) {
271
+ const subtle = getSubtle();
272
+ const webCryptoAlg = mapHashAlgorithm(algorithm);
273
+ const result = await subtle.digest(webCryptoAlg, data);
274
+ return new Uint8Array(result);
275
+ }
276
+ async function aesCbcDecryptRaw(key, iv, data) {
277
+ const subtle = getSubtle();
278
+ const blockSize = 16;
279
+ if (data.length % blockSize !== 0) {
280
+ const padded = new Uint8Array(Math.ceil(data.length / blockSize) * blockSize);
281
+ padded.set(data);
282
+ data = padded;
283
+ }
284
+ if (data.length === 0) {
285
+ return new Uint8Array(0);
286
+ }
287
+ const cryptoKey = await subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["encrypt", "decrypt"]);
288
+ const lastCiphertextBlock = data.subarray(data.length - blockSize);
289
+ const paddingPlaintext = new Uint8Array(blockSize);
290
+ paddingPlaintext.fill(blockSize);
291
+ const encryptedPadding = await subtle.encrypt({ name: "AES-CBC", iv: lastCiphertextBlock }, cryptoKey, paddingPlaintext);
292
+ const encPadBlock = new Uint8Array(encryptedPadding).subarray(0, blockSize);
293
+ const paddedData = new Uint8Array(data.length + blockSize);
294
+ paddedData.set(data);
295
+ paddedData.set(encPadBlock, data.length);
296
+ const result = await subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, paddedData);
297
+ return new Uint8Array(result).subarray(0, data.length);
298
+ }
299
+ async function aesCbcEncryptNoPad(key, iv, data) {
300
+ const blockSize = 16;
301
+ if (data.length % blockSize !== 0) {
302
+ throw new Error("Data must be block-aligned for no-padding encryption");
303
+ }
304
+ const subtle = getSubtle();
305
+ const cryptoKey = await subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["encrypt"]);
306
+ const result = await subtle.encrypt({ name: "AES-CBC", iv }, cryptoKey, data);
307
+ return new Uint8Array(result).subarray(0, data.length);
308
+ }
309
+ async function hmac(algorithm, key, data) {
310
+ const subtle = getSubtle();
311
+ const webCryptoAlg = mapHashAlgorithm(algorithm);
312
+ const cryptoKey = await subtle.importKey("raw", key, { name: "HMAC", hash: webCryptoAlg }, false, ["sign"]);
313
+ const result = await subtle.sign("HMAC", cryptoKey, data);
314
+ return new Uint8Array(result);
315
+ }
316
+
317
+ // src/crypto/aes-ecb.ts
318
+ var BLOCK = 16;
319
+ var BATCH = 4096;
320
+ async function aesEcbDecrypt(key, data) {
321
+ const plain = await aesCbcDecryptRaw(key, new Uint8Array(BLOCK), data);
322
+ for (let at = BLOCK; at < plain.length && at < data.length; at++) {
323
+ plain[at] = plain[at] ^ data[at - BLOCK];
324
+ }
325
+ return plain;
326
+ }
327
+ async function aesEcbEncrypt(key, data) {
328
+ const padded = new Uint8Array(Math.ceil(data.length / BLOCK) * BLOCK);
329
+ padded.set(data);
330
+ const subtle = getSubtle();
331
+ const cryptoKey = await subtle.importKey("raw", key, { name: "AES-CBC" }, false, [
332
+ "encrypt"
333
+ ]);
334
+ const iv = new Uint8Array(BLOCK);
335
+ const out = new Uint8Array(padded.length);
336
+ for (let start = 0; start < padded.length; start += BLOCK * BATCH) {
337
+ const end = Math.min(padded.length, start + BLOCK * BATCH);
338
+ const jobs = [];
339
+ for (let at = start; at < end; at += BLOCK) {
340
+ const block = padded.slice(at, at + BLOCK);
341
+ jobs.push(
342
+ subtle.encrypt({ name: "AES-CBC", iv }, cryptoKey, block).then((result) => {
343
+ out.set(new Uint8Array(result, 0, BLOCK), at);
344
+ })
345
+ );
346
+ }
347
+ await Promise.all(jobs);
348
+ }
349
+ return out;
350
+ }
351
+
352
+ // src/crypto/errors.ts
353
+ var PasswordRequiredError = class extends Error {
354
+ code = "password-required";
355
+ constructor(message = "This file is password protected. Enter the password to open it.") {
356
+ super(message);
357
+ this.name = "PasswordRequiredError";
358
+ }
359
+ };
360
+ var IncorrectPasswordError = class extends Error {
361
+ code = "incorrect-password";
362
+ constructor(message = "The password is incorrect.") {
363
+ super(message);
364
+ this.name = "IncorrectPasswordError";
365
+ }
366
+ };
367
+ var DataIntegrityError = class extends Error {
368
+ code = "data-integrity";
369
+ constructor(message = "Data integrity check failed. The encrypted file may be corrupted or tampered with.") {
370
+ super(message);
371
+ this.name = "DataIntegrityError";
372
+ }
373
+ };
374
+ function isOoxmlCryptoError(error) {
375
+ if (!(error instanceof Error)) return false;
376
+ const code = error.code;
377
+ return code === "password-required" || code === "incorrect-password" || code === "data-integrity";
378
+ }
379
+
380
+ // src/digest/algorithm-names.ts
381
+ var CANONICAL_BY_LOOKUP_KEY = {
382
+ SHA1: "SHA-1",
383
+ SHA256: "SHA-256",
384
+ SHA384: "SHA-384",
385
+ SHA512: "SHA-512",
386
+ MD2: "MD2",
387
+ MD4: "MD4",
388
+ MD5: "MD5",
389
+ RIPEMD128: "RIPEMD-128",
390
+ RIPEMD160: "RIPEMD-160",
391
+ WHIRLPOOL: "WHIRLPOOL"
392
+ };
393
+ function normalizeDigestAlgorithmName(name) {
394
+ const key = name.toUpperCase().replace(/[-_\s]/g, "");
395
+ return Object.hasOwn(CANONICAL_BY_LOOKUP_KEY, key) ? CANONICAL_BY_LOOKUP_KEY[key] : void 0;
396
+ }
397
+
398
+ // src/digest/sha256.ts
399
+ var K = new Uint32Array([
400
+ 1116352408,
401
+ 1899447441,
402
+ 3049323471,
403
+ 3921009573,
404
+ 961987163,
405
+ 1508970993,
406
+ 2453635748,
407
+ 2870763221,
408
+ 3624381080,
409
+ 310598401,
410
+ 607225278,
411
+ 1426881987,
412
+ 1925078388,
413
+ 2162078206,
414
+ 2614888103,
415
+ 3248222580,
416
+ 3835390401,
417
+ 4022224774,
418
+ 264347078,
419
+ 604807628,
420
+ 770255983,
421
+ 1249150122,
422
+ 1555081692,
423
+ 1996064986,
424
+ 2554220882,
425
+ 2821834349,
426
+ 2952996808,
427
+ 3210313671,
428
+ 3336571891,
429
+ 3584528711,
430
+ 113926993,
431
+ 338241895,
432
+ 666307205,
433
+ 773529912,
434
+ 1294757372,
435
+ 1396182291,
436
+ 1695183700,
437
+ 1986661051,
438
+ 2177026350,
439
+ 2456956037,
440
+ 2730485921,
441
+ 2820302411,
442
+ 3259730800,
443
+ 3345764771,
444
+ 3516065817,
445
+ 3600352804,
446
+ 4094571909,
447
+ 275423344,
448
+ 430227734,
449
+ 506948616,
450
+ 659060556,
451
+ 883997877,
452
+ 958139571,
453
+ 1322822218,
454
+ 1537002063,
455
+ 1747873779,
456
+ 1955562222,
457
+ 2024104815,
458
+ 2227730452,
459
+ 2361852424,
460
+ 2428436474,
461
+ 2756734187,
462
+ 3204031479,
463
+ 3329325298
464
+ ]);
465
+ var IV = [
466
+ 1779033703,
467
+ 3144134277,
468
+ 1013904242,
469
+ 2773480762,
470
+ 1359893119,
471
+ 2600822924,
472
+ 528734635,
473
+ 1541459225
474
+ ];
475
+ var W = new Uint32Array(64);
476
+ var rotr = (x, n) => x >>> n | x << 32 - n;
477
+ function compress(h, view, offset) {
478
+ for (let i = 0; i < 16; i++) W[i] = view.getUint32(offset + i * 4);
479
+ for (let i = 16; i < 64; i++) {
480
+ const a2 = W[i - 15];
481
+ const b2 = W[i - 2];
482
+ const s0 = rotr(a2, 7) ^ rotr(a2, 18) ^ a2 >>> 3;
483
+ const s1 = rotr(b2, 17) ^ rotr(b2, 19) ^ b2 >>> 10;
484
+ W[i] = W[i - 16] + s0 + W[i - 7] + s1 >>> 0;
485
+ }
486
+ let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], k = h[7];
487
+ for (let i = 0; i < 64; i++) {
488
+ const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
489
+ const ch = e & f ^ ~e & g;
490
+ const t1 = k + S1 + ch + K[i] + W[i] >>> 0;
491
+ const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
492
+ const maj = a & b ^ a & c ^ b & c;
493
+ const t2 = S0 + maj >>> 0;
494
+ k = g;
495
+ g = f;
496
+ f = e;
497
+ e = d + t1 >>> 0;
498
+ d = c;
499
+ c = b;
500
+ b = a;
501
+ a = t1 + t2 >>> 0;
502
+ }
503
+ h[0] = h[0] + a >>> 0;
504
+ h[1] = h[1] + b >>> 0;
505
+ h[2] = h[2] + c >>> 0;
506
+ h[3] = h[3] + d >>> 0;
507
+ h[4] = h[4] + e >>> 0;
508
+ h[5] = h[5] + f >>> 0;
509
+ h[6] = h[6] + g >>> 0;
510
+ h[7] = h[7] + k >>> 0;
511
+ }
512
+ function pad64(data) {
513
+ const padded = new Uint8Array(Math.ceil((data.length + 9) / 64) * 64);
514
+ padded.set(data);
515
+ padded[data.length] = 128;
516
+ const bits = data.length * 8;
517
+ const view = new DataView(padded.buffer);
518
+ view.setUint32(padded.length - 8, Math.floor(bits / 4294967296));
519
+ view.setUint32(padded.length - 4, bits >>> 0);
520
+ return view;
521
+ }
522
+ function wordsOut(h) {
523
+ const out = new Uint8Array(h.length * 4);
524
+ const view = new DataView(out.buffer);
525
+ for (let i = 0; i < h.length; i++) view.setUint32(i * 4, h[i]);
526
+ return out;
527
+ }
528
+ function sha256(data) {
529
+ const h = new Uint32Array(IV);
530
+ const view = pad64(data);
531
+ for (let offset = 0; offset < view.byteLength; offset += 64) compress(h, view, offset);
532
+ return wordsOut(h);
533
+ }
534
+
535
+ // src/digest/sha1.ts
536
+ var W2 = new Uint32Array(80);
537
+ var rotl = (x, n) => x << n | x >>> 32 - n;
538
+ function compress2(h, view, offset) {
539
+ for (let i = 0; i < 16; i++) W2[i] = view.getUint32(offset + i * 4);
540
+ for (let i = 16; i < 80; i++) W2[i] = rotl(W2[i - 3] ^ W2[i - 8] ^ W2[i - 14] ^ W2[i - 16], 1);
541
+ let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
542
+ for (let i = 0; i < 80; i++) {
543
+ let f;
544
+ let k;
545
+ if (i < 20) {
546
+ f = b & c | ~b & d;
547
+ k = 1518500249;
548
+ } else if (i < 40) {
549
+ f = b ^ c ^ d;
550
+ k = 1859775393;
551
+ } else if (i < 60) {
552
+ f = b & c | b & d | c & d;
553
+ k = 2400959708;
554
+ } else {
555
+ f = b ^ c ^ d;
556
+ k = 3395469782;
557
+ }
558
+ const t = rotl(a, 5) + f + e + k + W2[i] >>> 0;
559
+ e = d;
560
+ d = c;
561
+ c = rotl(b, 30) >>> 0;
562
+ b = a;
563
+ a = t;
564
+ }
565
+ h[0] = h[0] + a >>> 0;
566
+ h[1] = h[1] + b >>> 0;
567
+ h[2] = h[2] + c >>> 0;
568
+ h[3] = h[3] + d >>> 0;
569
+ h[4] = h[4] + e >>> 0;
570
+ }
571
+ function sha1(data) {
572
+ const h = new Uint32Array([1732584193, 4023233417, 2562383102, 271733878, 3285377520]);
573
+ const view = pad64(data);
574
+ for (let offset = 0; offset < view.byteLength; offset += 64) compress2(h, view, offset);
575
+ return wordsOut(h);
576
+ }
577
+
578
+ // src/digest/sha512.ts
579
+ var K2 = new Uint32Array([
580
+ 1116352408,
581
+ 3609767458,
582
+ 1899447441,
583
+ 602891725,
584
+ 3049323471,
585
+ 3964484399,
586
+ 3921009573,
587
+ 2173295548,
588
+ 961987163,
589
+ 4081628472,
590
+ 1508970993,
591
+ 3053834265,
592
+ 2453635748,
593
+ 2937671579,
594
+ 2870763221,
595
+ 3664609560,
596
+ 3624381080,
597
+ 2734883394,
598
+ 310598401,
599
+ 1164996542,
600
+ 607225278,
601
+ 1323610764,
602
+ 1426881987,
603
+ 3590304994,
604
+ 1925078388,
605
+ 4068182383,
606
+ 2162078206,
607
+ 991336113,
608
+ 2614888103,
609
+ 633803317,
610
+ 3248222580,
611
+ 3479774868,
612
+ 3835390401,
613
+ 2666613458,
614
+ 4022224774,
615
+ 944711139,
616
+ 264347078,
617
+ 2341262773,
618
+ 604807628,
619
+ 2007800933,
620
+ 770255983,
621
+ 1495990901,
622
+ 1249150122,
623
+ 1856431235,
624
+ 1555081692,
625
+ 3175218132,
626
+ 1996064986,
627
+ 2198950837,
628
+ 2554220882,
629
+ 3999719339,
630
+ 2821834349,
631
+ 766784016,
632
+ 2952996808,
633
+ 2566594879,
634
+ 3210313671,
635
+ 3203337956,
636
+ 3336571891,
637
+ 1034457026,
638
+ 3584528711,
639
+ 2466948901,
640
+ 113926993,
641
+ 3758326383,
642
+ 338241895,
643
+ 168717936,
644
+ 666307205,
645
+ 1188179964,
646
+ 773529912,
647
+ 1546045734,
648
+ 1294757372,
649
+ 1522805485,
650
+ 1396182291,
651
+ 2643833823,
652
+ 1695183700,
653
+ 2343527390,
654
+ 1986661051,
655
+ 1014477480,
656
+ 2177026350,
657
+ 1206759142,
658
+ 2456956037,
659
+ 344077627,
660
+ 2730485921,
661
+ 1290863460,
662
+ 2820302411,
663
+ 3158454273,
664
+ 3259730800,
665
+ 3505952657,
666
+ 3345764771,
667
+ 106217008,
668
+ 3516065817,
669
+ 3606008344,
670
+ 3600352804,
671
+ 1432725776,
672
+ 4094571909,
673
+ 1467031594,
674
+ 275423344,
675
+ 851169720,
676
+ 430227734,
677
+ 3100823752,
678
+ 506948616,
679
+ 1363258195,
680
+ 659060556,
681
+ 3750685593,
682
+ 883997877,
683
+ 3785050280,
684
+ 958139571,
685
+ 3318307427,
686
+ 1322822218,
687
+ 3812723403,
688
+ 1537002063,
689
+ 2003034995,
690
+ 1747873779,
691
+ 3602036899,
692
+ 1955562222,
693
+ 1575990012,
694
+ 2024104815,
695
+ 1125592928,
696
+ 2227730452,
697
+ 2716904306,
698
+ 2361852424,
699
+ 442776044,
700
+ 2428436474,
701
+ 593698344,
702
+ 2756734187,
703
+ 3733110249,
704
+ 3204031479,
705
+ 2999351573,
706
+ 3329325298,
707
+ 3815920427,
708
+ 3391569614,
709
+ 3928383900,
710
+ 3515267271,
711
+ 566280711,
712
+ 3940187606,
713
+ 3454069534,
714
+ 4118630271,
715
+ 4000239992,
716
+ 116418474,
717
+ 1914138554,
718
+ 174292421,
719
+ 2731055270,
720
+ 289380356,
721
+ 3203993006,
722
+ 460393269,
723
+ 320620315,
724
+ 685471733,
725
+ 587496836,
726
+ 852142971,
727
+ 1086792851,
728
+ 1017036298,
729
+ 365543100,
730
+ 1126000580,
731
+ 2618297676,
732
+ 1288033470,
733
+ 3409855158,
734
+ 1501505948,
735
+ 4234509866,
736
+ 1607167915,
737
+ 987167468,
738
+ 1816402316,
739
+ 1246189591
740
+ ]);
741
+ var IV_512 = [
742
+ 1779033703,
743
+ 4089235720,
744
+ 3144134277,
745
+ 2227873595,
746
+ 1013904242,
747
+ 4271175723,
748
+ 2773480762,
749
+ 1595750129,
750
+ 1359893119,
751
+ 2917565137,
752
+ 2600822924,
753
+ 725511199,
754
+ 528734635,
755
+ 4215389547,
756
+ 1541459225,
757
+ 327033209
758
+ ];
759
+ var IV_384 = [
760
+ 3418070365,
761
+ 3238371032,
762
+ 1654270250,
763
+ 914150663,
764
+ 2438529370,
765
+ 812702999,
766
+ 355462360,
767
+ 4144912697,
768
+ 1731405415,
769
+ 4290775857,
770
+ 2394180231,
771
+ 1750603025,
772
+ 3675008525,
773
+ 1694076839,
774
+ 1203062813,
775
+ 3204075428
776
+ ];
777
+ var W3 = new Uint32Array(160);
778
+ function compress3(h, data, offset) {
779
+ for (let i = 0; i < 32; i++) {
780
+ const o = offset + i * 4;
781
+ W3[i] = (data[o] << 24 | data[o + 1] << 16 | data[o + 2] << 8 | data[o + 3]) >>> 0;
782
+ }
783
+ for (let i = 16; i < 80; i++) {
784
+ const xh = W3[(i - 15) * 2];
785
+ const xl = W3[(i - 15) * 2 + 1];
786
+ const s0h = (xh >>> 1 | xl << 31) ^ (xh >>> 8 | xl << 24) ^ xh >>> 7;
787
+ const s0l = (xl >>> 1 | xh << 31) ^ (xl >>> 8 | xh << 24) ^ (xl >>> 7 | xh << 25);
788
+ const yh = W3[(i - 2) * 2];
789
+ const yl = W3[(i - 2) * 2 + 1];
790
+ const s1h = (yh >>> 19 | yl << 13) ^ (yl >>> 29 | yh << 3) ^ yh >>> 6;
791
+ const s1l = (yl >>> 19 | yh << 13) ^ (yh >>> 29 | yl << 3) ^ (yl >>> 6 | yh << 26);
792
+ let lo = (s0l >>> 0) + (s1l >>> 0) + W3[(i - 7) * 2 + 1] + W3[(i - 16) * 2 + 1];
793
+ const hi = s0h + s1h + W3[(i - 7) * 2] + W3[(i - 16) * 2] + Math.floor(lo / 4294967296);
794
+ lo >>>= 0;
795
+ W3[i * 2] = hi >>> 0;
796
+ W3[i * 2 + 1] = lo;
797
+ }
798
+ let ah = h[0], al = h[1], bh = h[2], bl = h[3], ch = h[4], cl = h[5], dh = h[6], dl = h[7];
799
+ let eh = h[8], el = h[9], fh = h[10], fl = h[11], gh = h[12], gl = h[13], hh = h[14], hl = h[15];
800
+ for (let i = 0; i < 80; i++) {
801
+ const S1h = (eh >>> 14 | el << 18) ^ (eh >>> 18 | el << 14) ^ (el >>> 9 | eh << 23);
802
+ const S1l = (el >>> 14 | eh << 18) ^ (el >>> 18 | eh << 14) ^ (eh >>> 9 | el << 23);
803
+ const chh = eh & fh ^ ~eh & gh;
804
+ const chl = el & fl ^ ~el & gl;
805
+ let t1l = (hl >>> 0) + (S1l >>> 0) + (chl >>> 0) + K2[i * 2 + 1] + W3[i * 2 + 1];
806
+ const t1h = hh + S1h + chh + K2[i * 2] + W3[i * 2] + Math.floor(t1l / 4294967296);
807
+ t1l >>>= 0;
808
+ const S0h = (ah >>> 28 | al << 4) ^ (al >>> 2 | ah << 30) ^ (al >>> 7 | ah << 25);
809
+ const S0l = (al >>> 28 | ah << 4) ^ (ah >>> 2 | al << 30) ^ (ah >>> 7 | al << 25);
810
+ const majh = ah & bh ^ ah & ch ^ bh & ch;
811
+ const majl = al & bl ^ al & cl ^ bl & cl;
812
+ let t2l = (S0l >>> 0) + (majl >>> 0);
813
+ const t2h = S0h + majh + Math.floor(t2l / 4294967296);
814
+ t2l >>>= 0;
815
+ hh = gh;
816
+ hl = gl;
817
+ gh = fh;
818
+ gl = fl;
819
+ fh = eh;
820
+ fl = el;
821
+ let nl = (dl >>> 0) + t1l;
822
+ eh = dh + t1h + Math.floor(nl / 4294967296) >>> 0;
823
+ el = nl >>> 0;
824
+ dh = ch;
825
+ dl = cl;
826
+ ch = bh;
827
+ cl = bl;
828
+ bh = ah;
829
+ bl = al;
830
+ nl = t1l + t2l;
831
+ ah = t1h + t2h + Math.floor(nl / 4294967296) >>> 0;
832
+ al = nl >>> 0;
833
+ }
834
+ const add = (i, xh, xl) => {
835
+ const lo = (h[i + 1] >>> 0) + (xl >>> 0);
836
+ h[i] = h[i] + xh + Math.floor(lo / 4294967296) >>> 0;
837
+ h[i + 1] = lo >>> 0;
838
+ };
839
+ add(0, ah, al);
840
+ add(2, bh, bl);
841
+ add(4, ch, cl);
842
+ add(6, dh, dl);
843
+ add(8, eh, el);
844
+ add(10, fh, fl);
845
+ add(12, gh, gl);
846
+ add(14, hh, hl);
847
+ }
848
+ function digest64(data, iv, bytes) {
849
+ const h = new Uint32Array(iv);
850
+ const padded = new Uint8Array(Math.ceil((data.length + 17) / 128) * 128);
851
+ padded.set(data);
852
+ padded[data.length] = 128;
853
+ const bits = data.length * 8;
854
+ const view = new DataView(padded.buffer);
855
+ view.setUint32(padded.length - 8, Math.floor(bits / 4294967296));
856
+ view.setUint32(padded.length - 4, bits >>> 0);
857
+ for (let offset = 0; offset < padded.length; offset += 128) compress3(h, padded, offset);
858
+ const out = new Uint8Array(bytes);
859
+ const outView = new DataView(out.buffer);
860
+ for (let i = 0; i < bytes / 4; i++) outView.setUint32(i * 4, h[i]);
861
+ return out;
862
+ }
863
+ var sha512 = (data) => digest64(data, IV_512, 64);
864
+ var sha384 = (data) => digest64(data, IV_384, 48);
865
+
866
+ // src/digest/digest.ts
867
+ var DIGESTS = {
868
+ "SHA-1": sha1,
869
+ "SHA-256": sha256,
870
+ "SHA-384": sha384,
871
+ "SHA-512": sha512
872
+ };
873
+ function digestFunction(name) {
874
+ const canonical = normalizeDigestAlgorithmName(name);
875
+ return canonical && canonical in DIGESTS ? DIGESTS[canonical] : void 0;
876
+ }
877
+
878
+ // src/crypto/key-derivation.ts
879
+ var BLOCK_KEYS = {
880
+ verifierHashInput: new Uint8Array([254, 167, 210, 118, 59, 75, 158, 121]),
881
+ verifierHashValue: new Uint8Array([215, 170, 15, 109, 48, 97, 52, 78]),
882
+ encryptedKeyValue: new Uint8Array([20, 110, 11, 231, 171, 172, 208, 214]),
883
+ dataIntegrityHmacKey: new Uint8Array([95, 178, 173, 1, 12, 185, 225, 246]),
884
+ dataIntegrityHmacValue: new Uint8Array([160, 103, 127, 2, 178, 44, 132, 51])
885
+ };
886
+ var MAX_SPIN_COUNT = 1e7;
887
+ async function spinPassword(password, salt, spinCount, hashAlgorithm) {
888
+ if (!Number.isInteger(spinCount) || spinCount < 0 || spinCount > MAX_SPIN_COUNT) {
889
+ throw new Error(`Unsupported encryption spin count: ${spinCount}.`);
890
+ }
891
+ const passwordBytes = encodePasswordUtf16LE(password);
892
+ const digest = digestFunction(hashAlgorithm);
893
+ if (digest) {
894
+ let h2 = digest(concatArrays(salt, passwordBytes));
895
+ const buffer = new Uint8Array(4 + h2.length);
896
+ const view = new DataView(buffer.buffer);
897
+ for (let i = 0; i < spinCount; i++) {
898
+ view.setUint32(0, i, true);
899
+ buffer.set(h2, 4);
900
+ h2 = digest(buffer);
901
+ }
902
+ return h2;
903
+ }
904
+ let h = await hash(hashAlgorithm, concatArrays(salt, passwordBytes));
905
+ for (let i = 0; i < spinCount; i++) {
906
+ h = await hash(hashAlgorithm, concatArrays(uint32LE(i), h));
907
+ }
908
+ return h;
909
+ }
910
+ async function extendKey(h, hashAlgorithm, keyLength) {
911
+ const pad = async (value) => {
912
+ const input = new Uint8Array(64).fill(value);
913
+ for (let i = 0; i < h.length && i < 64; i++) input[i] = h[i] ^ value;
914
+ return hash(hashAlgorithm, input);
915
+ };
916
+ const x3 = concatArrays(await pad(54), await pad(92));
917
+ return x3.subarray(0, keyLength);
918
+ }
919
+ function computeAgileKeyBase(password, salt, spinCount, hashAlgorithm) {
920
+ return spinPassword(password, salt, spinCount, hashAlgorithm);
921
+ }
922
+ async function deriveAgileKeyFromBase(base, hashAlgorithm, blockKey, keyBits, hashSize) {
923
+ const h = await hash(hashAlgorithm, concatArrays(base, blockKey));
924
+ const keyLength = keyBits / 8;
925
+ if (hashSize >= keyLength) return h.subarray(0, keyLength);
926
+ return extendKey(h, hashAlgorithm, keyLength);
927
+ }
928
+ async function deriveAgileKey(password, salt, spinCount, hashAlgorithm, blockKey, keyBits, hashSize) {
929
+ const base = await computeAgileKeyBase(password, salt, spinCount, hashAlgorithm);
930
+ return deriveAgileKeyFromBase(base, hashAlgorithm, blockKey, keyBits, hashSize);
931
+ }
932
+ function computeStandardKeyBase(password, salt) {
933
+ return spinPassword(password, salt, 5e4, "SHA-1");
934
+ }
935
+ async function deriveStandardKeyFromBase(base, keySize, blockNumber = 0) {
936
+ const h = await hash("SHA-1", concatArrays(base, uint32LE(blockNumber)));
937
+ return extendKey(h, "SHA-1", keySize / 8);
938
+ }
939
+ async function deriveStandardKey(password, salt, keySize, _algIdHash, blockNumber = 0) {
940
+ const base = await computeStandardKeyBase(password, salt);
941
+ return deriveStandardKeyFromBase(base, keySize, blockNumber);
942
+ }
943
+ async function generateIV(hashAlgorithm, salt, blockKey, blockSize) {
944
+ const h = await hash(hashAlgorithm, concatArrays(salt, blockKey));
945
+ if (h.length >= blockSize) return h.subarray(0, blockSize);
946
+ const padded = new Uint8Array(blockSize).fill(54);
947
+ padded.set(h);
948
+ return padded;
949
+ }
950
+
951
+ // src/crypto/decrypt.ts
952
+ async function verifyAgilePassword(info, password) {
953
+ const pke = info.passwordKeyEncryptor;
954
+ const base = await computeAgileKeyBase(password, pke.saltValue, pke.spinCount, pke.hashAlgorithm);
955
+ const blockKey = (block) => deriveAgileKeyFromBase(base, pke.hashAlgorithm, block, pke.keyBits, pke.hashSize);
956
+ const verifierHashInput = await aesCbcDecryptRaw(
957
+ await blockKey(BLOCK_KEYS.verifierHashInput),
958
+ pke.saltValue,
959
+ pke.encryptedVerifierHashInput
960
+ );
961
+ const verifierHashValue = await aesCbcDecryptRaw(
962
+ await blockKey(BLOCK_KEYS.verifierHashValue),
963
+ pke.saltValue,
964
+ pke.encryptedVerifierHashValue
965
+ );
966
+ const computedHash = await hash(pke.hashAlgorithm, verifierHashInput.subarray(0, pke.saltSize));
967
+ if (computedHash.length < pke.hashSize || verifierHashValue.length < pke.hashSize) return null;
968
+ let difference = 0;
969
+ for (let i = 0; i < pke.hashSize; i++) difference |= verifierHashValue[i] ^ computedHash[i];
970
+ if (difference !== 0) return null;
971
+ const decryptedKey = await aesCbcDecryptRaw(
972
+ await blockKey(BLOCK_KEYS.encryptedKeyValue),
973
+ pke.saltValue,
974
+ pke.encryptedKeyValue
975
+ );
976
+ return decryptedKey.subarray(0, info.keyData.keyBits / 8);
977
+ }
978
+ async function verifyStandardPassword(info, password) {
979
+ const key = await deriveStandardKey(
980
+ password,
981
+ info.verifier.salt,
982
+ info.header.keySize,
983
+ info.header.algIdHash
984
+ );
985
+ const decryptedVerifier = await aesEcbDecrypt(key, info.verifier.encryptedVerifier);
986
+ const decryptedHash = await aesEcbDecrypt(key, info.verifier.encryptedVerifierHash);
987
+ const computedHash = await hash("SHA-1", decryptedVerifier);
988
+ const hashSize = info.verifier.verifierHashSize;
989
+ let match = true;
990
+ for (let i = 0; i < Math.min(hashSize, 20); i++) {
991
+ if (computedHash[i] !== decryptedHash[i]) {
992
+ match = false;
993
+ break;
994
+ }
995
+ }
996
+ return match ? key : null;
997
+ }
998
+ async function verifyAgileDataIntegrity(info, key, encryptedPackage) {
999
+ const keyData = info.keyData;
1000
+ if (!info.dataIntegrity) {
1001
+ return;
1002
+ }
1003
+ const hmacKeyIV = await generateIV(
1004
+ keyData.hashAlgorithm,
1005
+ keyData.saltValue,
1006
+ BLOCK_KEYS.dataIntegrityHmacKey,
1007
+ keyData.blockSize
1008
+ );
1009
+ const decryptedHmacKey = await aesCbcDecryptRaw(
1010
+ key,
1011
+ hmacKeyIV,
1012
+ info.dataIntegrity.encryptedHmacKey
1013
+ );
1014
+ const hmacKey = decryptedHmacKey.subarray(0, keyData.hashSize);
1015
+ const hmacValueIV = await generateIV(
1016
+ keyData.hashAlgorithm,
1017
+ keyData.saltValue,
1018
+ BLOCK_KEYS.dataIntegrityHmacValue,
1019
+ keyData.blockSize
1020
+ );
1021
+ const decryptedHmacValue = await aesCbcDecryptRaw(
1022
+ key,
1023
+ hmacValueIV,
1024
+ info.dataIntegrity.encryptedHmacValue
1025
+ );
1026
+ const expectedHmac = decryptedHmacValue.subarray(0, keyData.hashSize);
1027
+ const computedHmac = await hmac(keyData.hashAlgorithm, hmacKey, encryptedPackage);
1028
+ let match = true;
1029
+ if (computedHmac.length < keyData.hashSize) {
1030
+ match = false;
1031
+ } else {
1032
+ for (let i = 0; i < keyData.hashSize; i++) {
1033
+ if (computedHmac[i] !== expectedHmac[i]) {
1034
+ match = false;
1035
+ break;
1036
+ }
1037
+ }
1038
+ }
1039
+ if (!match) {
1040
+ throw new DataIntegrityError(
1041
+ "Data integrity check failed. The encrypted file may be corrupted or tampered with."
1042
+ );
1043
+ }
1044
+ }
1045
+ async function decryptAgilePackage(encryptedPackage, key, info) {
1046
+ const keyData = info.keyData;
1047
+ const sizeView = new DataView(encryptedPackage.buffer, encryptedPackage.byteOffset, 8);
1048
+ const originalSize = sizeView.getUint32(0, true) + sizeView.getUint32(4, true) * 4294967296;
1049
+ const encryptedData = encryptedPackage.subarray(8);
1050
+ const segmentSize = 4096;
1051
+ const result = new Uint8Array(originalSize);
1052
+ let resultOffset = 0;
1053
+ const numSegments = Math.ceil(encryptedData.length / segmentSize);
1054
+ for (let segment = 0; segment < numSegments; segment++) {
1055
+ const segmentStart = segment * segmentSize;
1056
+ const segmentEnd = Math.min(segmentStart + segmentSize, encryptedData.length);
1057
+ const segmentData = encryptedData.subarray(segmentStart, segmentEnd);
1058
+ const blockKeyBytes = uint32LE(segment);
1059
+ const segmentIV = await generateIV(
1060
+ keyData.hashAlgorithm,
1061
+ keyData.saltValue,
1062
+ blockKeyBytes,
1063
+ keyData.blockSize
1064
+ );
1065
+ const decrypted = await aesCbcDecryptRaw(key, segmentIV, segmentData);
1066
+ const bytesToCopy = Math.min(decrypted.length, originalSize - resultOffset);
1067
+ result.set(decrypted.subarray(0, bytesToCopy), resultOffset);
1068
+ resultOffset += bytesToCopy;
1069
+ }
1070
+ return result.buffer;
1071
+ }
1072
+ async function decryptStandardPackage(encryptedPackage, key) {
1073
+ const sizeView = new DataView(encryptedPackage.buffer, encryptedPackage.byteOffset, 8);
1074
+ const originalSize = sizeView.getUint32(0, true);
1075
+ const encryptedData = encryptedPackage.subarray(8);
1076
+ const decrypted = await aesEcbDecrypt(key, encryptedData);
1077
+ return decrypted.slice(0, originalSize).buffer;
1078
+ }
1079
+
1080
+ // src/crypto/detect.ts
1081
+ var OLE_MAGIC2 = [208, 207, 17, 224, 161, 177, 26, 225];
1082
+ var toBytes = (input) => input instanceof Uint8Array ? input : new Uint8Array(input);
1083
+ function toArrayBuffer(bytes) {
1084
+ if (bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) {
1085
+ return bytes.buffer;
1086
+ }
1087
+ return bytes.slice().buffer;
1088
+ }
1089
+ function isCompoundFile(input) {
1090
+ const bytes = toBytes(input);
1091
+ return bytes.length >= 8 && OLE_MAGIC2.every((value, index) => bytes[index] === value);
1092
+ }
1093
+ function openEncryptedPackage(input) {
1094
+ const bytes = toBytes(input);
1095
+ if (!isCompoundFile(bytes)) return void 0;
1096
+ try {
1097
+ const file = parseOle2(toArrayBuffer(bytes));
1098
+ if (!file.entries.some((entry) => entry.name === "EncryptedPackage")) return void 0;
1099
+ const encryptionInfo = file.getStream("EncryptionInfo");
1100
+ const encryptedPackage = encryptionInfo && file.getStream("EncryptedPackage");
1101
+ return encryptionInfo && encryptedPackage ? { encryptionInfo, encryptedPackage } : void 0;
1102
+ } catch {
1103
+ return void 0;
1104
+ }
1105
+ }
1106
+ function isEncryptedOoxmlPackage(input) {
1107
+ const bytes = toBytes(input);
1108
+ if (!isCompoundFile(bytes)) return false;
1109
+ try {
1110
+ const names = new Set(parseOle2(toArrayBuffer(bytes)).entries.map((entry) => entry.name));
1111
+ return names.has("EncryptionInfo") && names.has("EncryptedPackage");
1112
+ } catch {
1113
+ return false;
1114
+ }
1115
+ }
1116
+ function detectFileFormat(data) {
1117
+ const bytes = toBytes(data);
1118
+ if (bytes.length < 8) return { format: "unknown", encrypted: false };
1119
+ if (isCompoundFile(bytes)) return { format: "ole", encrypted: true };
1120
+ if (bytes[0] === 80 && bytes[1] === 75) return { format: "zip", encrypted: false };
1121
+ return { format: "unknown", encrypted: false };
1122
+ }
1123
+ var EncryptedFileError = class extends Error {
1124
+ isEncrypted = true;
1125
+ constructor(message) {
1126
+ super(message);
1127
+ this.name = "EncryptedFileError";
1128
+ }
1129
+ };
1130
+
1131
+ // node_modules/.bun/@christophervr+ole2@0.4.0/node_modules/@christophervr/ole2/dist/ole2-parser-write-helpers.js
1132
+ function compareDirEntryNames(a, b) {
1133
+ if (a.length !== b.length) {
1134
+ return a.length - b.length;
1135
+ }
1136
+ const ua = a.toUpperCase();
1137
+ const ub = b.toUpperCase();
1138
+ for (let i = 0; i < ua.length; i++) {
1139
+ const diff = ua.charCodeAt(i) - ub.charCodeAt(i);
1140
+ if (diff !== 0) {
1141
+ return diff;
1142
+ }
1143
+ }
1144
+ return 0;
1145
+ }
1146
+ function encodeName(name) {
1147
+ const bytes = new Uint8Array((name.length + 1) * 2);
1148
+ for (let i = 0; i < name.length; i++) {
1149
+ bytes[i * 2] = name.charCodeAt(i) & 255;
1150
+ bytes[i * 2 + 1] = name.charCodeAt(i) >> 8 & 255;
1151
+ }
1152
+ return bytes;
1153
+ }
1154
+ function writeFatChain(fat, chain) {
1155
+ for (let i = 0; i < chain.sectors.length; i++) {
1156
+ fat[chain.sectors[i]] = i < chain.sectors.length - 1 ? chain.sectors[i + 1] : ENDOFCHAIN;
1157
+ }
1158
+ }
1159
+ function writeFatRun(fat, firstSector, count) {
1160
+ for (let i = 0; i < count; i++) {
1161
+ const sector = firstSector + i;
1162
+ fat[sector] = i < count - 1 ? sector + 1 : ENDOFCHAIN;
1163
+ }
1164
+ }
1165
+ function writeInt32Sectors(outBytes, int32Data, firstSector, numSectors, sectorSize) {
1166
+ const entriesPerSector = sectorSize / 4;
1167
+ for (let i = 0; i < numSectors; i++) {
1168
+ const sectorOff = (firstSector + i + 1) * sectorSize;
1169
+ const start = i * entriesPerSector;
1170
+ const end = start + entriesPerSector;
1171
+ const chunk = int32Data.subarray(start, end);
1172
+ const chunkBytes = new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
1173
+ outBytes.set(chunkBytes, sectorOff);
1174
+ }
1175
+ }
1176
+
1177
+ // node_modules/.bun/@christophervr+ole2@0.4.0/node_modules/@christophervr/ole2/dist/ole2-parser-write-serialize.js
1178
+ function writeHeader(outView, outBytes, params) {
1179
+ outBytes.set(OLE_MAGIC, 0);
1180
+ outView.setUint16(24, 62, true);
1181
+ outView.setUint16(26, 3, true);
1182
+ outView.setUint16(28, 65534, true);
1183
+ outView.setUint16(30, 9, true);
1184
+ outView.setUint16(32, 6, true);
1185
+ outView.setUint32(40, 0, true);
1186
+ outView.setUint32(44, params.numFATSectors, true);
1187
+ outView.setUint32(48, params.firstDirSector, true);
1188
+ outView.setUint32(52, 0, true);
1189
+ outView.setUint32(56, params.miniStreamCutoff, true);
1190
+ outView.setUint32(60, params.numMiniFATSectors > 0 ? params.firstMiniFATSector : ENDOFCHAIN, true);
1191
+ outView.setUint32(64, params.numMiniFATSectors, true);
1192
+ const numDIFATSectors = params.numDIFATSectors ?? 0;
1193
+ outView.setUint32(68, numDIFATSectors > 0 ? params.firstDIFATSector ?? ENDOFCHAIN : ENDOFCHAIN, true);
1194
+ outView.setUint32(72, numDIFATSectors, true);
1195
+ for (let i = 0; i < HEADER_DIFAT_ENTRIES; i++) {
1196
+ if (i < params.numFATSectors) {
1197
+ outView.setUint32(76 + i * 4, params.firstFATSector + i, true);
1198
+ } else {
1199
+ outView.setUint32(76 + i * 4, FREESECT, true);
1200
+ }
1201
+ }
1202
+ }
1203
+ var HEADER_DIFAT_ENTRIES = 109;
1204
+ function difatSectorsFor(numFATSectors, sectorSize) {
1205
+ const overflow = numFATSectors - HEADER_DIFAT_ENTRIES;
1206
+ return overflow > 0 ? Math.ceil(overflow / (sectorSize / 4 - 1)) : 0;
1207
+ }
1208
+ function sizeFatSectors(dataSectors, sectorSize) {
1209
+ const entriesPerFAT = sectorSize / 4;
1210
+ let numFATSectors = 1;
1211
+ while (true) {
1212
+ const numDIFATSectors = difatSectorsFor(numFATSectors, sectorSize);
1213
+ const needed = Math.ceil((dataSectors + numFATSectors + numDIFATSectors) / entriesPerFAT);
1214
+ if (needed <= numFATSectors) {
1215
+ return { numFATSectors, numDIFATSectors };
1216
+ }
1217
+ numFATSectors = needed;
1218
+ }
1219
+ }
1220
+ function writeDifatSectors(outView, params) {
1221
+ const perSector = params.sectorSize / 4 - 1;
1222
+ for (let d = 0; d < params.numDIFATSectors; d++) {
1223
+ const base = (params.firstDIFATSector + d + 1) * params.sectorSize;
1224
+ for (let j = 0; j < perSector; j++) {
1225
+ const fatIndex = HEADER_DIFAT_ENTRIES + d * perSector + j;
1226
+ outView.setUint32(base + j * 4, fatIndex < params.numFATSectors ? params.firstFATSector + fatIndex : FREESECT, true);
1227
+ }
1228
+ const next = d < params.numDIFATSectors - 1 ? params.firstDIFATSector + d + 1 : ENDOFCHAIN;
1229
+ outView.setUint32(base + perSector * 4, next, true);
1230
+ }
1231
+ }
1232
+ function serializeDirectoryEntries(dirEntries, numDirSectors, sectorSize) {
1233
+ const dirData = new Uint8Array(numDirSectors * sectorSize);
1234
+ const dirView = new DataView(dirData.buffer);
1235
+ for (let i = 0; i < dirEntries.length; i++) {
1236
+ const entry = dirEntries[i];
1237
+ const entryOffset = i * DIR_ENTRY_SIZE;
1238
+ const nameBytes = encodeName(entry.name);
1239
+ dirData.set(nameBytes.subarray(0, Math.min(nameBytes.length, 64)), entryOffset);
1240
+ dirView.setUint16(entryOffset + 64, Math.min((entry.name.length + 1) * 2, 64), true);
1241
+ dirData[entryOffset + 66] = entry.type;
1242
+ dirData[entryOffset + 67] = 1;
1243
+ if (entry.clsid) {
1244
+ dirData.set(entry.clsid.subarray(0, 16), entryOffset + 80);
1245
+ }
1246
+ if (i === 0) {
1247
+ dirView.setUint32(entryOffset + 68, 4294967295, true);
1248
+ dirView.setUint32(entryOffset + 72, 4294967295, true);
1249
+ dirView.setUint32(entryOffset + 76, dirEntries.length > 1 ? 1 : 4294967295, true);
1250
+ } else {
1251
+ dirView.setUint32(entryOffset + 68, 4294967295, true);
1252
+ dirView.setUint32(entryOffset + 72, i + 1 < dirEntries.length ? i + 1 : 4294967295, true);
1253
+ dirView.setUint32(entryOffset + 76, 4294967295, true);
1254
+ }
1255
+ dirView.setUint32(entryOffset + 116, entry.startSector, true);
1256
+ dirView.setUint32(entryOffset + 120, entry.size, true);
1257
+ }
1258
+ return dirData;
1259
+ }
1260
+ function writeStreamSectors(outBytes, streamData, chain, sectorSize) {
1261
+ for (let i = 0; i < chain.sectors.length; i++) {
1262
+ const sectorOffset = (chain.sectors[i] + 1) * sectorSize;
1263
+ const srcOffset = i * sectorSize;
1264
+ const srcEnd = Math.min(srcOffset + sectorSize, streamData.length);
1265
+ outBytes.set(streamData.subarray(srcOffset, srcEnd), sectorOffset);
1266
+ }
1267
+ }
1268
+
1269
+ // node_modules/.bun/@christophervr+ole2@0.4.0/node_modules/@christophervr/ole2/dist/ole2-parser-write.js
1270
+ function buildOle2(streams2, rootClsid, miniStreamCutoff = 4096) {
1271
+ const sectorSize = 512;
1272
+ const miniSectorSize = 64;
1273
+ const regularStreams = [];
1274
+ const miniStreams = [];
1275
+ for (const [name, data] of streams2) {
1276
+ if (data.length < miniStreamCutoff) {
1277
+ miniStreams.push({ name, data });
1278
+ } else {
1279
+ regularStreams.push({ name, data });
1280
+ }
1281
+ }
1282
+ let nextSector = 0;
1283
+ const fatChains = /* @__PURE__ */ new Map();
1284
+ for (const stream of regularStreams) {
1285
+ const numSectors = Math.ceil(stream.data.length / sectorSize);
1286
+ const sectors = [];
1287
+ for (let i = 0; i < numSectors; i++) {
1288
+ sectors.push(nextSector++);
1289
+ }
1290
+ fatChains.set(stream.name, { start: sectors[0] ?? 0, sectors });
1291
+ }
1292
+ let miniStreamContainer = new Uint8Array(0);
1293
+ const miniFatChains = /* @__PURE__ */ new Map();
1294
+ let nextMiniSector = 0;
1295
+ if (miniStreams.length > 0) {
1296
+ let miniStreamSize = 0;
1297
+ for (const s of miniStreams) {
1298
+ miniStreamSize += Math.ceil(s.data.length / miniSectorSize) * miniSectorSize;
1299
+ }
1300
+ miniStreamContainer = new Uint8Array(miniStreamSize);
1301
+ let miniOffset = 0;
1302
+ for (const stream of miniStreams) {
1303
+ const numMiniSectors = Math.ceil(stream.data.length / miniSectorSize);
1304
+ const miniSectors = [];
1305
+ for (let i = 0; i < numMiniSectors; i++) {
1306
+ miniSectors.push(nextMiniSector++);
1307
+ const srcOffset = i * miniSectorSize;
1308
+ const srcEnd = Math.min(srcOffset + miniSectorSize, stream.data.length);
1309
+ miniStreamContainer.set(stream.data.subarray(srcOffset, srcEnd), miniOffset);
1310
+ miniOffset += miniSectorSize;
1311
+ }
1312
+ miniFatChains.set(stream.name, {
1313
+ start: miniSectors[0] ?? 0,
1314
+ sectors: miniSectors
1315
+ });
1316
+ }
1317
+ }
1318
+ let rootStartSector = -1;
1319
+ const rootSectors = [];
1320
+ if (miniStreamContainer.length > 0) {
1321
+ const numSectors = Math.ceil(miniStreamContainer.length / sectorSize);
1322
+ rootStartSector = nextSector;
1323
+ for (let i = 0; i < numSectors; i++) {
1324
+ rootSectors.push(nextSector++);
1325
+ }
1326
+ }
1327
+ const dirEntries = [];
1328
+ dirEntries.push({
1329
+ name: "Root Entry",
1330
+ type: ENTRY_TYPE_ROOT,
1331
+ startSector: rootStartSector === -1 ? ENDOFCHAIN : rootStartSector,
1332
+ size: miniStreamContainer.length,
1333
+ clsid: rootClsid
1334
+ });
1335
+ for (const stream of regularStreams) {
1336
+ const chain = fatChains.get(stream.name);
1337
+ dirEntries.push({
1338
+ name: stream.name,
1339
+ type: ENTRY_TYPE_STREAM,
1340
+ startSector: chain.start,
1341
+ size: stream.data.length
1342
+ });
1343
+ }
1344
+ for (const stream of miniStreams) {
1345
+ const chain = miniFatChains.get(stream.name);
1346
+ dirEntries.push({
1347
+ name: stream.name,
1348
+ type: ENTRY_TYPE_STREAM,
1349
+ startSector: chain.start,
1350
+ size: stream.data.length
1351
+ });
1352
+ }
1353
+ const [rootEntry, ...streamEntries] = dirEntries;
1354
+ streamEntries.sort((a, b) => compareDirEntryNames(a.name, b.name));
1355
+ const sortedDirEntries = [rootEntry, ...streamEntries];
1356
+ const numDirSectors = Math.ceil(dirEntries.length * DIR_ENTRY_SIZE / sectorSize);
1357
+ const firstDirSector = nextSector;
1358
+ nextSector += numDirSectors;
1359
+ let firstMiniFATSector = ENDOFCHAIN;
1360
+ let numMiniFATSectors = 0;
1361
+ if (miniStreams.length > 0) {
1362
+ numMiniFATSectors = Math.ceil(nextMiniSector * 4 / sectorSize);
1363
+ firstMiniFATSector = nextSector;
1364
+ nextSector += numMiniFATSectors;
1365
+ }
1366
+ const { numFATSectors, numDIFATSectors } = sizeFatSectors(nextSector, sectorSize);
1367
+ const firstFATSector = nextSector;
1368
+ nextSector += numFATSectors;
1369
+ const firstDIFATSector = nextSector;
1370
+ nextSector += numDIFATSectors;
1371
+ const totalSectors = nextSector;
1372
+ const fat = new Int32Array(numFATSectors * (sectorSize / 4));
1373
+ fat.fill(-1);
1374
+ for (const [, chain] of fatChains) {
1375
+ writeFatChain(fat, chain);
1376
+ }
1377
+ writeFatChain(fat, { start: rootSectors[0] ?? 0, sectors: rootSectors });
1378
+ writeFatRun(fat, firstDirSector, numDirSectors);
1379
+ if (numMiniFATSectors > 0) {
1380
+ writeFatRun(fat, firstMiniFATSector, numMiniFATSectors);
1381
+ }
1382
+ for (let i = 0; i < numFATSectors; i++) {
1383
+ fat[firstFATSector + i] = FATSECT;
1384
+ }
1385
+ for (let i = 0; i < numDIFATSectors; i++) {
1386
+ fat[firstDIFATSector + i] = DIFSECT;
1387
+ }
1388
+ let miniFat;
1389
+ if (miniStreams.length > 0) {
1390
+ miniFat = new Int32Array(numMiniFATSectors * (sectorSize / 4));
1391
+ miniFat.fill(-1);
1392
+ for (const [, chain] of miniFatChains) {
1393
+ writeFatChain(miniFat, chain);
1394
+ }
1395
+ }
1396
+ const fileSize = (totalSectors + 1) * sectorSize;
1397
+ const output = new ArrayBuffer(fileSize);
1398
+ const outView = new DataView(output);
1399
+ const outBytes = new Uint8Array(output);
1400
+ writeHeader(outView, outBytes, {
1401
+ numFATSectors,
1402
+ firstDirSector,
1403
+ miniStreamCutoff,
1404
+ firstMiniFATSector,
1405
+ numMiniFATSectors,
1406
+ firstFATSector,
1407
+ firstDIFATSector,
1408
+ numDIFATSectors
1409
+ });
1410
+ for (const stream of regularStreams) {
1411
+ writeStreamSectors(outBytes, stream.data, fatChains.get(stream.name), sectorSize);
1412
+ }
1413
+ if (miniStreamContainer.length > 0) {
1414
+ writeStreamSectors(outBytes, miniStreamContainer, { start: rootSectors[0] ?? 0, sectors: rootSectors }, sectorSize);
1415
+ }
1416
+ const dirData = serializeDirectoryEntries(sortedDirEntries, numDirSectors, sectorSize);
1417
+ for (let i = 0; i < numDirSectors; i++) {
1418
+ const sectorOff = (firstDirSector + i + 1) * sectorSize;
1419
+ outBytes.set(dirData.subarray(i * sectorSize, (i + 1) * sectorSize), sectorOff);
1420
+ }
1421
+ if (miniFat) {
1422
+ writeInt32Sectors(outBytes, miniFat, firstMiniFATSector, numMiniFATSectors, sectorSize);
1423
+ }
1424
+ writeInt32Sectors(outBytes, fat, firstFATSector, numFATSectors, sectorSize);
1425
+ writeDifatSectors(outView, {
1426
+ firstFATSector,
1427
+ numFATSectors,
1428
+ firstDIFATSector,
1429
+ numDIFATSectors,
1430
+ sectorSize
1431
+ });
1432
+ return output;
1433
+ }
1434
+
1435
+ // src/crypto/encryption-info-write.ts
1436
+ function buildAgileEncryptionInfoXml(keyData, pke, dataIntegrity) {
1437
+ const xmlNs = "http://schemas.microsoft.com/office/2006/encryption";
1438
+ const pNs = "http://schemas.microsoft.com/office/2006/keyEncryptor/password";
1439
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r
1440
+ <encryption xmlns="${xmlNs}" xmlns:p="${pNs}"><keyData saltSize="${keyData.saltSize}" blockSize="${keyData.blockSize}" keyBits="${keyData.keyBits}" hashSize="${keyData.hashSize}" cipherAlgorithm="${keyData.cipherAlgorithm}" cipherChaining="${keyData.cipherChaining}" hashAlgorithm="${keyData.hashAlgorithm}" saltValue="${base64Encode(keyData.saltValue)}"/><dataIntegrity encryptedHmacKey="${base64Encode(dataIntegrity.encryptedHmacKey)}" encryptedHmacValue="${base64Encode(dataIntegrity.encryptedHmacValue)}"/><keyEncryptors><keyEncryptor uri="http://schemas.microsoft.com/office/2006/keyEncryptor/password"><p:encryptedKey spinCount="${pke.spinCount}" saltSize="${pke.saltSize}" blockSize="${pke.blockSize}" keyBits="${pke.keyBits}" hashSize="${pke.hashSize}" cipherAlgorithm="${pke.cipherAlgorithm}" cipherChaining="${pke.cipherChaining}" hashAlgorithm="${pke.hashAlgorithm}" saltValue="${base64Encode(pke.saltValue)}" encryptedVerifierHashInput="${base64Encode(pke.encryptedVerifierHashInput)}" encryptedVerifierHashValue="${base64Encode(pke.encryptedVerifierHashValue)}" encryptedKeyValue="${base64Encode(pke.encryptedKeyValue)}"/></keyEncryptor></keyEncryptors></encryption>`;
1441
+ }
1442
+ function buildEncryptionInfoStream(xmlString) {
1443
+ const xmlBytes = new TextEncoder().encode(xmlString);
1444
+ const result = new Uint8Array(8 + xmlBytes.length);
1445
+ const view = new DataView(result.buffer);
1446
+ view.setUint16(0, 4, true);
1447
+ view.setUint16(2, 4, true);
1448
+ view.setUint32(4, 64, true);
1449
+ result.set(xmlBytes, 8);
1450
+ return result;
1451
+ }
1452
+ var STANDARD_ALG_IDS = {
1453
+ 128: 26126,
1454
+ // CALG_AES_128
1455
+ 192: 26127,
1456
+ // CALG_AES_192
1457
+ 256: 26128
1458
+ // CALG_AES_256
1459
+ };
1460
+ var STANDARD_ALG_ID_HASH_SHA1 = 32772;
1461
+ var STANDARD_PROVIDER_TYPE_RSA_AES = 24;
1462
+ var STANDARD_CSP_NAME = "Microsoft Enhanced RSA and AES Cryptographic Provider";
1463
+ var STANDARD_FLAGS = 36;
1464
+ function buildStandardEncryptionInfoStream(keyBits, salt, encryptedVerifier, encryptedVerifierHash) {
1465
+ const algId = STANDARD_ALG_IDS[keyBits];
1466
+ if (!algId) {
1467
+ throw new Error(
1468
+ `Unsupported standard encryption key size: ${keyBits} bits. Use 128, 192, or 256.`
1469
+ );
1470
+ }
1471
+ const cspNameBytes = new Uint8Array((STANDARD_CSP_NAME.length + 1) * 2);
1472
+ for (let i = 0; i < STANDARD_CSP_NAME.length; i++) {
1473
+ const code = STANDARD_CSP_NAME.charCodeAt(i);
1474
+ cspNameBytes[i * 2] = code & 255;
1475
+ cspNameBytes[i * 2 + 1] = code >> 8 & 255;
1476
+ }
1477
+ const headerFixedSize = 32;
1478
+ const headerSize = headerFixedSize + cspNameBytes.length;
1479
+ const verifierSize = 4 + 16 + 16 + 4 + 32;
1480
+ const totalSize = 12 + headerSize + verifierSize;
1481
+ const data = new Uint8Array(totalSize);
1482
+ const view = new DataView(data.buffer);
1483
+ view.setUint16(0, 4, true);
1484
+ view.setUint16(2, 2, true);
1485
+ view.setUint32(4, STANDARD_FLAGS, true);
1486
+ view.setUint32(8, headerSize, true);
1487
+ const h = 12;
1488
+ view.setUint32(h, STANDARD_FLAGS, true);
1489
+ view.setUint32(h + 4, 0, true);
1490
+ view.setUint32(h + 8, algId, true);
1491
+ view.setUint32(h + 12, STANDARD_ALG_ID_HASH_SHA1, true);
1492
+ view.setUint32(h + 16, keyBits, true);
1493
+ view.setUint32(h + 20, STANDARD_PROVIDER_TYPE_RSA_AES, true);
1494
+ view.setUint32(h + 24, 0, true);
1495
+ view.setUint32(h + 28, 0, true);
1496
+ data.set(cspNameBytes, h + 32);
1497
+ const v = 12 + headerSize;
1498
+ view.setUint32(v, 16, true);
1499
+ data.set(salt.subarray(0, 16), v + 4);
1500
+ data.set(encryptedVerifier.subarray(0, 16), v + 20);
1501
+ view.setUint32(v + 36, 20, true);
1502
+ data.set(encryptedVerifierHash.subarray(0, 32), v + 40);
1503
+ return data;
1504
+ }
1505
+
1506
+ // src/crypto/encrypt.ts
1507
+ var SEGMENT_SIZE = 4096;
1508
+ function container(encryptionInfo, encryptedPackage) {
1509
+ const streams2 = /* @__PURE__ */ new Map();
1510
+ streams2.set("EncryptionInfo", encryptionInfo);
1511
+ streams2.set("EncryptedPackage", encryptedPackage);
1512
+ return new Uint8Array(buildOle2(streams2));
1513
+ }
1514
+ function randomBytes(size) {
1515
+ const bytes = new Uint8Array(size);
1516
+ getCrypto().getRandomValues(bytes);
1517
+ return bytes;
1518
+ }
1519
+ function padTo(data, blockSize) {
1520
+ const padded = new Uint8Array(Math.ceil(data.length / blockSize) * blockSize);
1521
+ padded.set(data);
1522
+ return padded;
1523
+ }
1524
+ function withSizePrefix(size, body) {
1525
+ const out = new Uint8Array(8 + body.length);
1526
+ const view = new DataView(out.buffer, 0, 8);
1527
+ view.setUint32(0, size % 4294967296, true);
1528
+ view.setUint32(4, Math.floor(size / 4294967296), true);
1529
+ out.set(body, 8);
1530
+ return out;
1531
+ }
1532
+ async function encryptAgilePackage(packageData, key, info) {
1533
+ const { hashAlgorithm, saltValue, blockSize } = info.keyData;
1534
+ const padded = padTo(packageData, SEGMENT_SIZE);
1535
+ const encrypted = withSizePrefix(packageData.length, padded);
1536
+ for (let start = 0; start < padded.length; start += SEGMENT_SIZE) {
1537
+ const iv = await generateIV(
1538
+ hashAlgorithm,
1539
+ saltValue,
1540
+ uint32LE(start / SEGMENT_SIZE),
1541
+ blockSize
1542
+ );
1543
+ const segment = padded.subarray(start, start + SEGMENT_SIZE);
1544
+ encrypted.set(await aesCbcEncryptNoPad(key, iv, segment), 8 + start);
1545
+ }
1546
+ return encrypted;
1547
+ }
1548
+ async function encryptStandardPackage(packageData, key) {
1549
+ const encryptedData = await aesEcbEncrypt(key, packageData);
1550
+ return withSizePrefix(packageData.length, encryptedData);
1551
+ }
1552
+ async function encryptAgile(packageData, password, options = {}) {
1553
+ const keyBits = options.algorithm === "AES128" ? 128 : 256;
1554
+ const hashAlgorithm = "SHA-512";
1555
+ const hashSize = 64;
1556
+ const blockSize = 16;
1557
+ const spinCount = options.spinCount ?? 1e5;
1558
+ const keyDataSalt = randomBytes(16);
1559
+ const pkeSalt = randomBytes(16);
1560
+ const documentKey = randomBytes(keyBits / 8);
1561
+ const verifierHashInput = randomBytes(16);
1562
+ const verifierHash = await hash(hashAlgorithm, verifierHashInput);
1563
+ const base = await computeAgileKeyBase(password, pkeSalt, spinCount, hashAlgorithm);
1564
+ const encryptUnder = async (block, data) => {
1565
+ const key = await deriveAgileKeyFromBase(base, hashAlgorithm, block, keyBits, hashSize);
1566
+ return aesCbcEncryptNoPad(key, pkeSalt, padTo(data, blockSize));
1567
+ };
1568
+ const passwordKeyEncryptor = {
1569
+ saltSize: 16,
1570
+ blockSize,
1571
+ keyBits,
1572
+ hashSize,
1573
+ cipherAlgorithm: "AES",
1574
+ cipherChaining: "ChainingModeCBC",
1575
+ hashAlgorithm,
1576
+ saltValue: pkeSalt,
1577
+ spinCount,
1578
+ encryptedVerifierHashInput: await encryptUnder(BLOCK_KEYS.verifierHashInput, verifierHashInput),
1579
+ encryptedVerifierHashValue: await encryptUnder(BLOCK_KEYS.verifierHashValue, verifierHash),
1580
+ encryptedKeyValue: await encryptUnder(BLOCK_KEYS.encryptedKeyValue, documentKey)
1581
+ };
1582
+ const keyData = {
1583
+ saltSize: 16,
1584
+ blockSize,
1585
+ keyBits,
1586
+ hashSize,
1587
+ cipherAlgorithm: "AES",
1588
+ cipherChaining: "ChainingModeCBC",
1589
+ hashAlgorithm,
1590
+ saltValue: keyDataSalt
1591
+ };
1592
+ const info = {
1593
+ keyData};
1594
+ const encryptedPackage = await encryptAgilePackage(packageData, documentKey, info);
1595
+ const hmacKey = randomBytes(hashSize);
1596
+ const hmacValue = await hmac(hashAlgorithm, hmacKey, encryptedPackage);
1597
+ const encryptIntegrity = async (block, data) => {
1598
+ const iv = await generateIV(hashAlgorithm, keyDataSalt, block, blockSize);
1599
+ return aesCbcEncryptNoPad(documentKey, iv, padTo(data, blockSize));
1600
+ };
1601
+ const dataIntegrity = {
1602
+ encryptedHmacKey: await encryptIntegrity(BLOCK_KEYS.dataIntegrityHmacKey, hmacKey),
1603
+ encryptedHmacValue: await encryptIntegrity(BLOCK_KEYS.dataIntegrityHmacValue, hmacValue)
1604
+ };
1605
+ const xml = buildAgileEncryptionInfoXml(keyData, passwordKeyEncryptor, dataIntegrity);
1606
+ return container(buildEncryptionInfoStream(xml), encryptedPackage);
1607
+ }
1608
+ async function encryptStandard(packageData, password, options = {}) {
1609
+ const keyBits = options.algorithm === "AES128" ? 128 : 256;
1610
+ const salt = randomBytes(16);
1611
+ const key = await deriveStandardKey(password, salt, keyBits);
1612
+ const verifier = randomBytes(16);
1613
+ const paddedVerifierHash = new Uint8Array(32);
1614
+ paddedVerifierHash.set((await hash("SHA-1", verifier)).subarray(0, 20));
1615
+ const encryptionInfo = buildStandardEncryptionInfoStream(
1616
+ keyBits,
1617
+ salt,
1618
+ await aesEcbEncrypt(key, verifier),
1619
+ await aesEcbEncrypt(key, paddedVerifierHash)
1620
+ );
1621
+ return container(encryptionInfo, await encryptStandardPackage(packageData, key));
1622
+ }
1623
+
1624
+ // src/crypto/encryption-info.ts
1625
+ var RC4_ALG_ID = 26625;
1626
+ function parseEncryptionInfo(data) {
1627
+ if (data.byteLength < 8) throw new Error("The EncryptionInfo stream is truncated.");
1628
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1629
+ const versionMajor = view.getUint16(0, true);
1630
+ const versionMinor = view.getUint16(2, true);
1631
+ if (versionMajor === 4 && versionMinor === 4) return parseAgileEncryptionInfo(data);
1632
+ if ((versionMajor === 2 || versionMajor === 3 || versionMajor === 4) && versionMinor === 2) {
1633
+ return parseStandardEncryptionInfo(data);
1634
+ }
1635
+ throw new Error(
1636
+ `Unsupported encryption version: ${versionMajor}.${versionMinor}. Only Standard (2.2-4.2) and Agile (4.4) encryption are supported.`
1637
+ );
1638
+ }
1639
+ function parseStandardEncryptionInfo(data) {
1640
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1641
+ const versionMajor = view.getUint16(0, true);
1642
+ const versionMinor = view.getUint16(2, true);
1643
+ const flags = view.getUint32(4, true);
1644
+ const headerSize = view.getUint32(8, true);
1645
+ const headerOffset = 12;
1646
+ const hFlags = view.getUint32(headerOffset, true);
1647
+ const algId = view.getUint32(headerOffset + 8, true);
1648
+ const algIdHash = view.getUint32(headerOffset + 12, true);
1649
+ const keySize = view.getUint32(headerOffset + 16, true);
1650
+ const providerType = view.getUint32(headerOffset + 20, true);
1651
+ let cspName = "";
1652
+ const cspEnd = headerOffset + headerSize;
1653
+ for (let i = headerOffset + 32; i < cspEnd - 1; i += 2) {
1654
+ const ch = view.getUint16(i, true);
1655
+ if (ch === 0) break;
1656
+ cspName += String.fromCharCode(ch);
1657
+ }
1658
+ const verifierOffset = 12 + headerSize;
1659
+ const saltSize = view.getUint32(verifierOffset, true);
1660
+ const salt = new Uint8Array(data.buffer, data.byteOffset + verifierOffset + 4, 16);
1661
+ const encryptedVerifier = new Uint8Array(data.buffer, data.byteOffset + verifierOffset + 20, 16);
1662
+ const verifierHashSize = view.getUint32(verifierOffset + 36, true);
1663
+ const encryptedVerifierHashLength = algId === RC4_ALG_ID ? verifierHashSize : Math.ceil(verifierHashSize / 16) * 16;
1664
+ const encryptedVerifierHash = new Uint8Array(
1665
+ data.buffer,
1666
+ data.byteOffset + verifierOffset + 40,
1667
+ encryptedVerifierHashLength
1668
+ );
1669
+ return {
1670
+ version: { major: versionMajor, minor: versionMinor },
1671
+ isAgile: false,
1672
+ isStandard: true,
1673
+ flags,
1674
+ headerSize,
1675
+ header: { flags: hFlags, algId, algIdHash, keySize, providerType, cspName },
1676
+ verifier: {
1677
+ saltSize,
1678
+ salt: new Uint8Array(salt),
1679
+ encryptedVerifier: new Uint8Array(encryptedVerifier),
1680
+ verifierHashSize,
1681
+ encryptedVerifierHash: new Uint8Array(encryptedVerifierHash)
1682
+ }
1683
+ };
1684
+ }
1685
+ function tagAttribute(xml, tag, attr) {
1686
+ const tagMatch = xml.match(new RegExp(`<[^>]*${tag}[^>]*>`, "i"));
1687
+ if (!tagMatch) return "";
1688
+ const attrMatch = tagMatch[0].match(new RegExp(`\\b${attr}="([^"]*)"`, "i"));
1689
+ return attrMatch?.[1] ?? "";
1690
+ }
1691
+ function parseAgileEncryptionInfo(data) {
1692
+ const xml = new TextDecoder("utf-8").decode(data.subarray(8));
1693
+ const keyData = (attr) => tagAttribute(xml, "keyData", attr);
1694
+ const integrity = (attr) => tagAttribute(xml, "dataIntegrity", attr);
1695
+ const pke = (attr) => tagAttribute(xml, "encryptedKey", attr);
1696
+ const int = (value) => parseInt(value, 10);
1697
+ const encryptedHmacKey = integrity("encryptedHmacKey");
1698
+ return {
1699
+ version: { major: 4, minor: 4 },
1700
+ isAgile: true,
1701
+ keyData: {
1702
+ saltSize: int(keyData("saltSize")),
1703
+ blockSize: int(keyData("blockSize")),
1704
+ keyBits: int(keyData("keyBits")),
1705
+ hashSize: int(keyData("hashSize")),
1706
+ cipherAlgorithm: keyData("cipherAlgorithm"),
1707
+ cipherChaining: keyData("cipherChaining"),
1708
+ hashAlgorithm: keyData("hashAlgorithm"),
1709
+ saltValue: base64Decode(keyData("saltValue"))
1710
+ },
1711
+ ...encryptedHmacKey ? {
1712
+ dataIntegrity: {
1713
+ encryptedHmacKey: base64Decode(encryptedHmacKey),
1714
+ encryptedHmacValue: base64Decode(integrity("encryptedHmacValue"))
1715
+ }
1716
+ } : {},
1717
+ passwordKeyEncryptor: {
1718
+ saltSize: int(pke("saltSize")),
1719
+ blockSize: int(pke("blockSize")),
1720
+ keyBits: int(pke("keyBits")),
1721
+ hashSize: int(pke("hashSize")),
1722
+ cipherAlgorithm: pke("cipherAlgorithm"),
1723
+ cipherChaining: pke("cipherChaining"),
1724
+ hashAlgorithm: pke("hashAlgorithm"),
1725
+ saltValue: base64Decode(pke("saltValue")),
1726
+ spinCount: int(pke("spinCount")),
1727
+ encryptedVerifierHashInput: base64Decode(pke("encryptedVerifierHashInput")),
1728
+ encryptedVerifierHashValue: base64Decode(pke("encryptedVerifierHashValue")),
1729
+ encryptedKeyValue: base64Decode(pke("encryptedKeyValue"))
1730
+ }
1731
+ };
1732
+ }
1733
+
1734
+ // src/crypto/index.ts
1735
+ var isStandard = (info) => "isStandard" in info && info.isStandard;
1736
+ function streams(bytes) {
1737
+ const opened = openEncryptedPackage(bytes);
1738
+ if (opened) return { info: opened.encryptionInfo, data: opened.encryptedPackage };
1739
+ const file = parseOle2(toArrayBuffer(bytes));
1740
+ if (!file.getStream("EncryptionInfo")) {
1741
+ throw new Error(
1742
+ "EncryptionInfo stream not found. The file may not be an encrypted OOXML package."
1743
+ );
1744
+ }
1745
+ throw new Error("EncryptedPackage stream not found. The file may be corrupted.");
1746
+ }
1747
+ async function decryptOoxmlPackage(input, password) {
1748
+ const { info: infoStream, data } = streams(toBytes(input));
1749
+ const info = parseEncryptionInfo(infoStream);
1750
+ if (isStandard(info)) {
1751
+ const key2 = await verifyStandardPassword(info, password);
1752
+ if (!key2) throw new IncorrectPasswordError();
1753
+ return new Uint8Array(await decryptStandardPackage(data, key2));
1754
+ }
1755
+ const key = await verifyAgilePassword(info, password);
1756
+ if (!key) throw new IncorrectPasswordError();
1757
+ await verifyAgileDataIntegrity(info, key, data);
1758
+ return new Uint8Array(await decryptAgilePackage(data, key, info));
1759
+ }
1760
+ function encryptOoxmlPackage(input, password, options = {}) {
1761
+ const bytes = toBytes(input);
1762
+ return options.encryptionScheme === "standard" ? encryptStandard(bytes, password, options) : encryptAgile(bytes, password, options);
1763
+ }
1764
+ async function verifyOoxmlPackagePassword(input, password) {
1765
+ try {
1766
+ const opened = openEncryptedPackage(input);
1767
+ if (!opened) return false;
1768
+ const info = parseEncryptionInfo(opened.encryptionInfo);
1769
+ const key = isStandard(info) ? await verifyStandardPassword(info, password) : await verifyAgilePassword(info, password);
1770
+ return key !== null;
1771
+ } catch {
1772
+ return false;
1773
+ }
1774
+ }
1775
+
1776
+ exports.BLOCK_KEYS = BLOCK_KEYS;
1777
+ exports.DataIntegrityError = DataIntegrityError;
1778
+ exports.EncryptedFileError = EncryptedFileError;
1779
+ exports.IncorrectPasswordError = IncorrectPasswordError;
1780
+ exports.PasswordRequiredError = PasswordRequiredError;
1781
+ exports.RC4_ALG_ID = RC4_ALG_ID;
1782
+ exports.buildAgileEncryptionInfoXml = buildAgileEncryptionInfoXml;
1783
+ exports.buildEncryptionInfoStream = buildEncryptionInfoStream;
1784
+ exports.buildStandardEncryptionInfoStream = buildStandardEncryptionInfoStream;
1785
+ exports.computeStandardKeyBase = computeStandardKeyBase;
1786
+ exports.decryptAgilePackage = decryptAgilePackage;
1787
+ exports.decryptOoxmlPackage = decryptOoxmlPackage;
1788
+ exports.decryptStandardPackage = decryptStandardPackage;
1789
+ exports.deriveAgileKey = deriveAgileKey;
1790
+ exports.deriveStandardKey = deriveStandardKey;
1791
+ exports.deriveStandardKeyFromBase = deriveStandardKeyFromBase;
1792
+ exports.detectFileFormat = detectFileFormat;
1793
+ exports.encryptAgilePackage = encryptAgilePackage;
1794
+ exports.encryptOoxmlPackage = encryptOoxmlPackage;
1795
+ exports.encryptStandardPackage = encryptStandardPackage;
1796
+ exports.generateIV = generateIV;
1797
+ exports.isCompoundFile = isCompoundFile;
1798
+ exports.isEncryptedOoxmlPackage = isEncryptedOoxmlPackage;
1799
+ exports.isOoxmlCryptoError = isOoxmlCryptoError;
1800
+ exports.openEncryptedPackage = openEncryptedPackage;
1801
+ exports.parseEncryptionInfo = parseEncryptionInfo;
1802
+ exports.verifyAgileDataIntegrity = verifyAgileDataIntegrity;
1803
+ exports.verifyAgilePassword = verifyAgilePassword;
1804
+ exports.verifyOoxmlPackagePassword = verifyOoxmlPackagePassword;
1805
+ exports.verifyStandardPassword = verifyStandardPassword;