pagyra-js 0.0.1

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 (540) hide show
  1. package/.eslintrc.json +30 -0
  2. package/CHANGELOG.md +13 -0
  3. package/README.md +275 -0
  4. package/UA_Styles_Chromium.md +93 -0
  5. package/_ext/woff2_conversion/brotli-decode.d.ts +139 -0
  6. package/_ext/woff2_conversion/brotli-encode.d.ts +129 -0
  7. package/_ext/woff2_conversion/brotli-port.d.ts +12 -0
  8. package/_ext/woff2_conversion/brotli-shared-dictionary.d.ts +25 -0
  9. package/_ext/woff2_conversion/brotli-types.d.ts +15 -0
  10. package/_ext/woff2_conversion/woff2-common.d.ts +37 -0
  11. package/_ext/woff2_conversion/woff2-decode.d.ts +32 -0
  12. package/_ext/woff2_conversion/woff2-encode.d.ts +31 -0
  13. package/_ext/woff2_conversion/woff2-output.d.ts +39 -0
  14. package/_ext/woff2_original_cpp/brotli/brotli.c +1559 -0
  15. package/_ext/woff2_original_cpp/brotli/brotli.md +116 -0
  16. package/_ext/woff2_original_cpp/brotli/decode.h +409 -0
  17. package/_ext/woff2_original_cpp/brotli/encode.h +505 -0
  18. package/_ext/woff2_original_cpp/brotli/port.h +302 -0
  19. package/_ext/woff2_original_cpp/brotli/shared_dictionary.h +100 -0
  20. package/_ext/woff2_original_cpp/brotli/types.h +83 -0
  21. package/_ext/woff2_original_cpp/cmake/FindBrotliDec.cmake +35 -0
  22. package/_ext/woff2_original_cpp/cmake/FindBrotliEnc.cmake +35 -0
  23. package/_ext/woff2_original_cpp/include/woff2/decode.h +36 -0
  24. package/_ext/woff2_original_cpp/include/woff2/encode.h +43 -0
  25. package/_ext/woff2_original_cpp/include/woff2/output.h +86 -0
  26. package/_ext/woff2_original_cpp/src/buffer.h +164 -0
  27. package/_ext/woff2_original_cpp/src/convert_woff2ttf_fuzzer.cc +13 -0
  28. package/_ext/woff2_original_cpp/src/convert_woff2ttf_fuzzer_new_entry.cc +12 -0
  29. package/_ext/woff2_original_cpp/src/file.h +30 -0
  30. package/_ext/woff2_original_cpp/src/font.cc +400 -0
  31. package/_ext/woff2_original_cpp/src/font.h +105 -0
  32. package/_ext/woff2_original_cpp/src/glyph.cc +383 -0
  33. package/_ext/woff2_original_cpp/src/glyph.h +71 -0
  34. package/_ext/woff2_original_cpp/src/normalize.cc +314 -0
  35. package/_ext/woff2_original_cpp/src/normalize.h +39 -0
  36. package/_ext/woff2_original_cpp/src/port.h +66 -0
  37. package/_ext/woff2_original_cpp/src/round.h +27 -0
  38. package/_ext/woff2_original_cpp/src/store_bytes.h +55 -0
  39. package/_ext/woff2_original_cpp/src/table_tags.cc +82 -0
  40. package/_ext/woff2_original_cpp/src/table_tags.h +30 -0
  41. package/_ext/woff2_original_cpp/src/transform.cc +430 -0
  42. package/_ext/woff2_original_cpp/src/transform.h +26 -0
  43. package/_ext/woff2_original_cpp/src/variable_length.cc +129 -0
  44. package/_ext/woff2_original_cpp/src/variable_length.h +30 -0
  45. package/_ext/woff2_original_cpp/src/woff2_common.cc +50 -0
  46. package/_ext/woff2_original_cpp/src/woff2_common.h +64 -0
  47. package/_ext/woff2_original_cpp/src/woff2_compress.cc +43 -0
  48. package/_ext/woff2_original_cpp/src/woff2_dec.cc +1398 -0
  49. package/_ext/woff2_original_cpp/src/woff2_decompress.cc +41 -0
  50. package/_ext/woff2_original_cpp/src/woff2_enc.cc +458 -0
  51. package/_ext/woff2_original_cpp/src/woff2_info.cc +142 -0
  52. package/_ext/woff2_original_cpp/src/woff2_out.cc +63 -0
  53. package/assets/fonts/ttf/arimo/Arimo-Bold.ttf +0 -0
  54. package/assets/fonts/ttf/arimo/Arimo-BoldItalic.ttf +0 -0
  55. package/assets/fonts/ttf/arimo/Arimo-Italic.ttf +0 -0
  56. package/assets/fonts/ttf/arimo/Arimo-Regular.ttf +0 -0
  57. package/assets/fonts/ttf/cinzeldecorative/CinzelDecorative-Black.ttf +0 -0
  58. package/assets/fonts/ttf/cinzeldecorative/CinzelDecorative-Bold.ttf +0 -0
  59. package/assets/fonts/ttf/cinzeldecorative/CinzelDecorative-Regular.ttf +0 -0
  60. package/assets/fonts/ttf/dejavu/DejaVuSans.ttf +0 -0
  61. package/assets/fonts/ttf/firecode/FiraCode-Bold.ttf +0 -0
  62. package/assets/fonts/ttf/firecode/FiraCode-Light.ttf +0 -0
  63. package/assets/fonts/ttf/firecode/FiraCode-Medium.ttf +0 -0
  64. package/assets/fonts/ttf/firecode/FiraCode-Regular.ttf +0 -0
  65. package/assets/fonts/ttf/firecode/FiraCode-SemiBold.ttf +0 -0
  66. package/assets/fonts/ttf/notoemoji/NotoEmoji-Bold.ttf +0 -0
  67. package/assets/fonts/ttf/notoemoji/NotoEmoji-Light.ttf +0 -0
  68. package/assets/fonts/ttf/notoemoji/NotoEmoji-Medium.ttf +0 -0
  69. package/assets/fonts/ttf/notoemoji/NotoEmoji-Regular.ttf +0 -0
  70. package/assets/fonts/ttf/notoemoji/NotoEmoji-SemiBold.ttf +0 -0
  71. package/assets/fonts/ttf/notosans/NotoSans-Regular.ttf +0 -0
  72. package/assets/fonts/ttf/roboto/Roboto-Bold.ttf +0 -0
  73. package/assets/fonts/ttf/roboto/Roboto-BoldItalic.ttf +0 -0
  74. package/assets/fonts/ttf/roboto/Roboto-Italic.ttf +0 -0
  75. package/assets/fonts/ttf/roboto/Roboto-Regular.ttf +0 -0
  76. package/assets/fonts/ttf/stixtwomath/STIXTwoMath-Regular.ttf +0 -0
  77. package/assets/fonts/ttf/tinos/Tinos-Bold.ttf +0 -0
  78. package/assets/fonts/ttf/tinos/Tinos-BoldItalic.ttf +0 -0
  79. package/assets/fonts/ttf/tinos/Tinos-Italic.ttf +0 -0
  80. package/assets/fonts/ttf/tinos/Tinos-Regular.ttf +0 -0
  81. package/assets/fonts/woff/lato/lato-latin-400-italic.woff +0 -0
  82. package/assets/fonts/woff/lato/lato-latin-400-normal.woff +0 -0
  83. package/assets/fonts/woff/lato/lato-latin-700-italic.woff +0 -0
  84. package/assets/fonts/woff/lato/lato-latin-700-normal.woff +0 -0
  85. package/assets/fonts/woff2/caveat/Caveat-Bold.woff2 +0 -0
  86. package/assets/fonts/woff2/caveat/Caveat-Regular.woff2 +0 -0
  87. package/assets/fonts/woff2/lato/lato-latin-400-italic.woff2 +0 -0
  88. package/assets/fonts/woff2/lato/lato-latin-400-normal.woff2 +0 -0
  89. package/assets/fonts/woff2/lato/lato-latin-700-italic.woff2 +0 -0
  90. package/assets/fonts/woff2/lato/lato-latin-700-normal.woff2 +0 -0
  91. package/docs/AGENTS.md +288 -0
  92. package/docs/BACKGROUND-REPEAT-IMPLEMENTATION.md +127 -0
  93. package/docs/BACKGROUND-REPEAT-REFERENCE.md +127 -0
  94. package/docs/BACKGROUND-REPEAT-SPACE-ROUND.md +164 -0
  95. package/docs/css-properties-support.md +256 -0
  96. package/docs/src_modules_table.md +172 -0
  97. package/docs/text-overlap-fix.md +85 -0
  98. package/docs/text-overlap-investigation.md +27 -0
  99. package/eslint.config.js +36 -0
  100. package/glyph_measure.htm +1458 -0
  101. package/package.json +50 -0
  102. package/playground/browser-entry.ts +2 -0
  103. package/playground/exports/background-text-debug.pdf +0 -0
  104. package/playground/public/app.js +875 -0
  105. package/playground/public/assets/1.webp +0 -0
  106. package/playground/public/examples/accents-test.html +24 -0
  107. package/playground/public/examples/advanced-selectors-demo.html +118 -0
  108. package/playground/public/examples/background-advanced-showcase.html +82 -0
  109. package/playground/public/examples/background-clip-text.html +36 -0
  110. package/playground/public/examples/background-origin-showcase.html +137 -0
  111. package/playground/public/examples/background-position-showcase.html +83 -0
  112. package/playground/public/examples/background-repeat-showcase.html +83 -0
  113. package/playground/public/examples/background-repeat-space-round.html +348 -0
  114. package/playground/public/examples/background-size-showcase.html +82 -0
  115. package/playground/public/examples/background-text-debug.html +18 -0
  116. package/playground/public/examples/baseline-test.html +24 -0
  117. package/playground/public/examples/bold-showcase.html +150 -0
  118. package/playground/public/examples/bold-strike-example.html +12 -0
  119. package/playground/public/examples/border-collapse-test.html +23 -0
  120. package/playground/public/examples/centered-shadow-div.html +72 -0
  121. package/playground/public/examples/css-variables.html +50 -0
  122. package/playground/public/examples/debug-accents.html +11 -0
  123. package/playground/public/examples/debug-text-overlap.html +46 -0
  124. package/playground/public/examples/flex-gap-column.html +130 -0
  125. package/playground/public/examples/flex-gap-row.html +137 -0
  126. package/playground/public/examples/flex-padding-test.html +29 -0
  127. package/playground/public/examples/flexbox-text-test.html +193 -0
  128. package/playground/public/examples/fonts-demo.html +126 -0
  129. package/playground/public/examples/footer-example.html +4 -0
  130. package/playground/public/examples/gradient-text.html +54 -0
  131. package/playground/public/examples/grid-gap-demo.html +156 -0
  132. package/playground/public/examples/header-example.html +4 -0
  133. package/playground/public/examples/header-footer-example.html +27 -0
  134. package/playground/public/examples/image-showcase.html +33 -0
  135. package/playground/public/examples/justify-text.html +22 -0
  136. package/playground/public/examples/linear-gradient-example.html +38 -0
  137. package/playground/public/examples/lorem-span.html +14 -0
  138. package/playground/public/examples/margin-block-showcase.html +21 -0
  139. package/playground/public/examples/margin-inline-showcase.html +21 -0
  140. package/playground/public/examples/monthly-summary.html +95 -0
  141. package/playground/public/examples/multi-page-lorem.html +190 -0
  142. package/playground/public/examples/opacity-debug.html +39 -0
  143. package/playground/public/examples/opacity-example.html +70 -0
  144. package/playground/public/examples/png-image-example.html +13 -0
  145. package/playground/public/examples/red-rectangle.html +18 -0
  146. package/playground/public/examples/repro.html +24 -0
  147. package/playground/public/examples/rounded-borders-test.html +24 -0
  148. package/playground/public/examples/simple-list.html +89 -0
  149. package/playground/public/examples/simple-svg.html +37 -0
  150. package/playground/public/examples/simple-table.html +52 -0
  151. package/playground/public/examples/skew-div.html +138 -0
  152. package/playground/public/examples/skew-text.html +21 -0
  153. package/playground/public/examples/starter-report.css +51 -0
  154. package/playground/public/examples/starter-report.html +23 -0
  155. package/playground/public/examples/svg-aspect-ratio-showcase.html +116 -0
  156. package/playground/public/examples/svg-gradients-linear.html +28 -0
  157. package/playground/public/examples/svg-gradients-radial.html +29 -0
  158. package/playground/public/examples/svg-gradients-showcase.html +66 -0
  159. package/playground/public/examples/svg-image-path-test.html +43 -0
  160. package/playground/public/examples/svg-images-clipping.html +27 -0
  161. package/playground/public/examples/svg-path-gallery.html +118 -0
  162. package/playground/public/examples/svg-radial-transform-demo.html +78 -0
  163. package/playground/public/examples/svg-transform-stack.html +103 -0
  164. package/playground/public/examples/svg-transforms-demo.html +127 -0
  165. package/playground/public/examples/table-merge-test.html +34 -0
  166. package/playground/public/examples/text-decoration-showcase.html +138 -0
  167. package/playground/public/examples/text-indent-showcase.html +137 -0
  168. package/playground/public/examples/text-shadow-example.html +29 -0
  169. package/playground/public/examples/very-complex-css.html +293 -0
  170. package/playground/public/examples/webp-example.html +13 -0
  171. package/playground/public/examples/z-index-demo.html +93 -0
  172. package/playground/public/examples.json +240 -0
  173. package/playground/public/images/dice.png +0 -0
  174. package/playground/public/images/duck.jpg +0 -0
  175. package/playground/public/index.html +149 -0
  176. package/playground/public/mode.js +1 -0
  177. package/playground/public/styles.css +382 -0
  178. package/playground/public/tmp-h2-debug.html +33 -0
  179. package/playground/public/tmp-italic-debug.html +32 -0
  180. package/playground/public/vendor/codemirror/codemirror.min.css +1 -0
  181. package/playground/public/vendor/codemirror/codemirror.min.js +1 -0
  182. package/playground/public/vendor/codemirror/css.min.js +1 -0
  183. package/playground/public/vendor/codemirror/darcula.min.css +1 -0
  184. package/playground/public/vendor/codemirror/htmlmixed.min.js +1 -0
  185. package/playground/public/vendor/codemirror/javascript.min.js +1 -0
  186. package/playground/public/vendor/codemirror/xml.min.js +1 -0
  187. package/playground/public/vendor/pagyra-playground-browser.js +165966 -0
  188. package/playground/public/vendor/pagyra-playground-browser.js.map +7 -0
  189. package/playground/server.d.ts +1 -0
  190. package/playground/server.js +68 -0
  191. package/playground/server.ts +128 -0
  192. package/scripts/browser-build.ts +101 -0
  193. package/scripts/build-browser-bundle.ts +52 -0
  194. package/scripts/glyph-comparison/simulate.ts +744 -0
  195. package/scripts/playground-browser-server.ts +57 -0
  196. package/scripts/probe-roboto.ts +6 -0
  197. package/scripts/render-playground-example.ts +121 -0
  198. package/scripts/run-glyph-atlas-tuner-runner.mjs +113 -0
  199. package/scripts/run-glyph-atlas-tuner.ts +141 -0
  200. package/scripts/top-ts-files.ps1 +39 -0
  201. package/scripts/top-ts-files.sh +37 -0
  202. package/scripts/woff2_info.ps1 +132 -0
  203. package/src/browser-entry.ts +14 -0
  204. package/src/compression/adler32.ts +45 -0
  205. package/src/compression/brotli/brotli.ts +463 -0
  206. package/src/compression/brotli/index.ts +15 -0
  207. package/src/compression/brotli/transform.ts +184 -0
  208. package/src/compression/brotli/types.ts +58 -0
  209. package/src/compression/brotli/utils.ts +157 -0
  210. package/src/compression/brotli/vendor/bit_reader.js +124 -0
  211. package/src/compression/brotli/vendor/context.js +250 -0
  212. package/src/compression/brotli/vendor/decode.d.ts +2 -0
  213. package/src/compression/brotli/vendor/decode.js +938 -0
  214. package/src/compression/brotli/vendor/dictionary-data.js +9469 -0
  215. package/src/compression/brotli/vendor/dictionary.js +36 -0
  216. package/src/compression/brotli/vendor/huffman.js +123 -0
  217. package/src/compression/brotli/vendor/package.json +3 -0
  218. package/src/compression/brotli/vendor/prefix.js +60 -0
  219. package/src/compression/brotli/vendor/streams.js +31 -0
  220. package/src/compression/brotli/vendor/transform.js +247 -0
  221. package/src/compression/brotli/vendor-decode.d.ts +4 -0
  222. package/src/compression/brotli/woff2-glyf-transform.ts +623 -0
  223. package/src/compression/decompress.ts +16 -0
  224. package/src/compression/deflate.ts +295 -0
  225. package/src/compression/index.ts +4 -0
  226. package/src/compression/types.ts +26 -0
  227. package/src/compression/utils.ts +107 -0
  228. package/src/core.ts +18 -0
  229. package/src/css/apply-declarations.ts +86 -0
  230. package/src/css/background-types.ts +65 -0
  231. package/src/css/browser-defaults.ts +16 -0
  232. package/src/css/clip-path-types.ts +13 -0
  233. package/src/css/compute-style.ts +494 -0
  234. package/src/css/css-unit-resolver.ts +65 -0
  235. package/src/css/custom-properties.ts +215 -0
  236. package/src/css/enums.ts +127 -0
  237. package/src/css/font-face-parser.ts +233 -0
  238. package/src/css/font-weight.ts +65 -0
  239. package/src/css/inline-style-parser.ts +27 -0
  240. package/src/css/layout-property-resolver.ts +75 -0
  241. package/src/css/length.ts +141 -0
  242. package/src/css/line-height.ts +96 -0
  243. package/src/css/named-colors.ts +150 -0
  244. package/src/css/parsers/background-parser-extended.ts +111 -0
  245. package/src/css/parsers/background-parser.ts +456 -0
  246. package/src/css/parsers/border-block-parser.ts +26 -0
  247. package/src/css/parsers/border-inline-parser.ts +26 -0
  248. package/src/css/parsers/border-parser-extended.ts +256 -0
  249. package/src/css/parsers/border-parser.ts +175 -0
  250. package/src/css/parsers/box-shadow-parser.ts +106 -0
  251. package/src/css/parsers/clip-path-parser.ts +92 -0
  252. package/src/css/parsers/color-parser.ts +14 -0
  253. package/src/css/parsers/dimension-parser.ts +117 -0
  254. package/src/css/parsers/display-flex-parser.ts +59 -0
  255. package/src/css/parsers/flex-parser.ts +144 -0
  256. package/src/css/parsers/font-parser.ts +40 -0
  257. package/src/css/parsers/gradient-parser.ts +366 -0
  258. package/src/css/parsers/grid-parser-extended.ts +55 -0
  259. package/src/css/parsers/grid-parser.ts +218 -0
  260. package/src/css/parsers/length-parser.ts +95 -0
  261. package/src/css/parsers/list-style-parser.ts +39 -0
  262. package/src/css/parsers/margin-block-parser.ts +12 -0
  263. package/src/css/parsers/margin-inline-parser.ts +12 -0
  264. package/src/css/parsers/margin-parser.ts +30 -0
  265. package/src/css/parsers/opacity-parser.ts +32 -0
  266. package/src/css/parsers/overflow-wrap-parser.ts +38 -0
  267. package/src/css/parsers/padding-block-parser.ts +12 -0
  268. package/src/css/parsers/padding-inline-parser.ts +12 -0
  269. package/src/css/parsers/padding-parser.ts +30 -0
  270. package/src/css/parsers/position-parser.ts +75 -0
  271. package/src/css/parsers/register-parsers.ts +302 -0
  272. package/src/css/parsers/registry.ts +18 -0
  273. package/src/css/parsers/text-parser-extended.ts +144 -0
  274. package/src/css/parsers/text-parser.ts +25 -0
  275. package/src/css/parsers/text-shadow-parser.ts +94 -0
  276. package/src/css/properties/box-model.ts +82 -0
  277. package/src/css/properties/flexbox.ts +44 -0
  278. package/src/css/properties/gap.ts +14 -0
  279. package/src/css/properties/grid.ts +94 -0
  280. package/src/css/properties/layout.ts +59 -0
  281. package/src/css/properties/misc.ts +44 -0
  282. package/src/css/properties/typography.ts +71 -0
  283. package/src/css/properties/visual.ts +68 -0
  284. package/src/css/selectors/matcher.ts +219 -0
  285. package/src/css/selectors/parser.ts +163 -0
  286. package/src/css/selectors/simple-key.ts +31 -0
  287. package/src/css/selectors/specificity.ts +41 -0
  288. package/src/css/selectors/types.ts +31 -0
  289. package/src/css/shorthands/border-shorthand.ts +68 -0
  290. package/src/css/shorthands/box-shorthand.ts +33 -0
  291. package/src/css/style-inheritance.ts +50 -0
  292. package/src/css/style.ts +402 -0
  293. package/src/css/ua-defaults/base-defaults.ts +266 -0
  294. package/src/css/ua-defaults/browser-defaults.ts +134 -0
  295. package/src/css/ua-defaults/element-defaults.ts +374 -0
  296. package/src/css/ua-defaults/types.ts +43 -0
  297. package/src/css/unit-conversion.ts +24 -0
  298. package/src/css/utils.ts +108 -0
  299. package/src/css/viewport.ts +17 -0
  300. package/src/debug/audit.ts +20 -0
  301. package/src/debug/ids.ts +13 -0
  302. package/src/debug/log.js +28 -0
  303. package/src/debug/log.ts +52 -0
  304. package/src/debug/tree.ts +57 -0
  305. package/src/dom/node.ts +133 -0
  306. package/src/environment/browser-environment.ts +78 -0
  307. package/src/environment/environment.ts +35 -0
  308. package/src/environment/global.ts +13 -0
  309. package/src/environment/node-environment.browser.ts +28 -0
  310. package/src/environment/node-environment.ts +64 -0
  311. package/src/fonts/detector.ts +28 -0
  312. package/src/fonts/engines/ttf-engine.ts +28 -0
  313. package/src/fonts/engines/woff-engine.ts +38 -0
  314. package/src/fonts/engines/woff2-engine.ts +41 -0
  315. package/src/fonts/extractors/metrics-extractor.ts +362 -0
  316. package/src/fonts/font-registry-resolver.ts +132 -0
  317. package/src/fonts/index.ts +3 -0
  318. package/src/fonts/orchestrator.ts +92 -0
  319. package/src/fonts/parsers/base-parser.ts +23 -0
  320. package/src/fonts/types.ts +85 -0
  321. package/src/fonts/utils/ttf-reconstructor.ts +120 -0
  322. package/src/fonts/woff/decoder.ts +105 -0
  323. package/src/fonts/woff2/buffer.ts +106 -0
  324. package/src/fonts/woff2/decoder.ts +981 -0
  325. package/src/geometry/box.ts +48 -0
  326. package/src/geometry/matrix.ts +59 -0
  327. package/src/html/css/parse-css.ts +85 -0
  328. package/src/html/dom-converter.ts +433 -0
  329. package/src/html/image-converter.ts +200 -0
  330. package/src/html-to-pdf.ts +410 -0
  331. package/src/image/base-decoder.ts +149 -0
  332. package/src/image/image-service.ts +188 -0
  333. package/src/image/jpeg-decoder.ts +73 -0
  334. package/src/image/png-decoder.ts +550 -0
  335. package/src/image/types.ts +20 -0
  336. package/src/image/webp-decoder.ts +242 -0
  337. package/src/image/webp-huffman.ts +218 -0
  338. package/src/image/webp-riff-parser.ts +54 -0
  339. package/src/image/webp-vp8l-decoder.ts +199 -0
  340. package/src/index.ts +35 -0
  341. package/src/layout/context/float-context.ts +62 -0
  342. package/src/layout/context/layout-environment.ts +29 -0
  343. package/src/layout/debug.ts +18 -0
  344. package/src/layout/inline/bounding-box-calculator.ts +132 -0
  345. package/src/layout/inline/font-baseline-calculator.ts +76 -0
  346. package/src/layout/inline/inline-utils.ts +94 -0
  347. package/src/layout/inline/layout.ts +285 -0
  348. package/src/layout/inline/line_breaker.ts +109 -0
  349. package/src/layout/inline/measurement.ts +144 -0
  350. package/src/layout/inline/run-placer.ts +139 -0
  351. package/src/layout/inline/text-alignment.ts +70 -0
  352. package/src/layout/inline/tokenizer.ts +195 -0
  353. package/src/layout/inline/types.ts +76 -0
  354. package/src/layout/pipeline/context-factory.ts +16 -0
  355. package/src/layout/pipeline/default-engine.ts +24 -0
  356. package/src/layout/pipeline/engine.ts +59 -0
  357. package/src/layout/pipeline/layout-tree.ts +13 -0
  358. package/src/layout/pipeline/out-of-flow-manager.ts +73 -0
  359. package/src/layout/pipeline/strategy.ts +12 -0
  360. package/src/layout/pipeline/text-metrics-initializer.ts +13 -0
  361. package/src/layout/strategies/block.ts +236 -0
  362. package/src/layout/strategies/display-none.ts +14 -0
  363. package/src/layout/strategies/fallback.ts +15 -0
  364. package/src/layout/strategies/flex.ts +477 -0
  365. package/src/layout/strategies/fragmentation.ts +17 -0
  366. package/src/layout/strategies/grid.ts +247 -0
  367. package/src/layout/strategies/image.ts +342 -0
  368. package/src/layout/strategies/inline.ts +128 -0
  369. package/src/layout/strategies/table.ts +595 -0
  370. package/src/layout/table/cell_layout.ts +31 -0
  371. package/src/layout/table/diagnostics.ts +19 -0
  372. package/src/layout/text-run.ts +42 -0
  373. package/src/layout/utils/content-measurer.ts +117 -0
  374. package/src/layout/utils/display-utils.ts +24 -0
  375. package/src/layout/utils/floats.ts +98 -0
  376. package/src/layout/utils/gap-calculator.ts +167 -0
  377. package/src/layout/utils/inline-formatter.ts +31 -0
  378. package/src/layout/utils/inline-formatting.ts +9 -0
  379. package/src/layout/utils/margin.ts +140 -0
  380. package/src/layout/utils/node-math.ts +237 -0
  381. package/src/layout/utils/overflow.ts +14 -0
  382. package/src/layout/utils/sizing.ts +12 -0
  383. package/src/layout/utils/text-metrics.ts +361 -0
  384. package/src/logging/debug.ts +58 -0
  385. package/src/pdf/font/base14/widths-courier-bold.ts +159 -0
  386. package/src/pdf/font/base14/widths-courier.ts +159 -0
  387. package/src/pdf/font/base14/widths-helvetica-bold.ts +158 -0
  388. package/src/pdf/font/base14/widths-helvetica.ts +158 -0
  389. package/src/pdf/font/base14/widths-times-bold.ts +158 -0
  390. package/src/pdf/font/base14/widths-times-roman.ts +158 -0
  391. package/src/pdf/font/base14/widths-types.ts +25 -0
  392. package/src/pdf/font/base14-widths.ts +32 -0
  393. package/src/pdf/font/blur.ts +81 -0
  394. package/src/pdf/font/builtin-fonts.browser.ts +262 -0
  395. package/src/pdf/font/builtin-fonts.ts +126 -0
  396. package/src/pdf/font/composite-glyph-parser.ts +242 -0
  397. package/src/pdf/font/embedder.ts +395 -0
  398. package/src/pdf/font/font-config.ts +190 -0
  399. package/src/pdf/font/font-registry.ts +263 -0
  400. package/src/pdf/font/font-subset.ts +258 -0
  401. package/src/pdf/font/glyph-atlas-maxrects.ts +305 -0
  402. package/src/pdf/font/glyph-atlas-tuner.ts +98 -0
  403. package/src/pdf/font/glyph-atlas.ts +226 -0
  404. package/src/pdf/font/glyph-cache.ts +127 -0
  405. package/src/pdf/font/loca-reader.ts +109 -0
  406. package/src/pdf/font/managers/font-resource-manager.ts +73 -0
  407. package/src/pdf/font/managers/subset-resource-manager.ts +164 -0
  408. package/src/pdf/font/rasterizer.ts +270 -0
  409. package/src/pdf/font/resolvers/base-font-mapper.ts +77 -0
  410. package/src/pdf/font/resolvers/family-resolver.ts +33 -0
  411. package/src/pdf/font/resolvers/weight-style-applicator.ts +63 -0
  412. package/src/pdf/font/simple-glyph-parser.ts +289 -0
  413. package/src/pdf/font/to-unicode.ts +109 -0
  414. package/src/pdf/font/transformation-matrix.ts +136 -0
  415. package/src/pdf/font/ttf-cmap.ts +180 -0
  416. package/src/pdf/font/ttf-global-metrics.ts +58 -0
  417. package/src/pdf/font/ttf-glyf.ts +125 -0
  418. package/src/pdf/font/ttf-glyph-metrics.ts +43 -0
  419. package/src/pdf/font/ttf-lite.ts +269 -0
  420. package/src/pdf/font/ttf-table-parser.ts +132 -0
  421. package/src/pdf/font/ttf-table-provider.ts +61 -0
  422. package/src/pdf/font/widths.ts +79 -0
  423. package/src/pdf/font-subset/font-registry.ts +127 -0
  424. package/src/pdf/header-footer-layout.ts +153 -0
  425. package/src/pdf/header-footer-painter.ts +209 -0
  426. package/src/pdf/header-footer-renderer.ts +357 -0
  427. package/src/pdf/header-footer-tokens.ts +55 -0
  428. package/src/pdf/header-footer.ts +25 -0
  429. package/src/pdf/layout-tree-builder.ts +261 -0
  430. package/src/pdf/page-painter.ts +241 -0
  431. package/src/pdf/pagination.ts +155 -0
  432. package/src/pdf/primitives/pdf-builder.ts +378 -0
  433. package/src/pdf/primitives/pdf-bytes.ts +40 -0
  434. package/src/pdf/primitives/pdf-document.ts +108 -0
  435. package/src/pdf/primitives/pdf-reference-manager.ts +47 -0
  436. package/src/pdf/primitives/pdf-resource-registries.ts +255 -0
  437. package/src/pdf/primitives/pdf-serializers.ts +194 -0
  438. package/src/pdf/primitives/pdf-types.ts +73 -0
  439. package/src/pdf/render.ts +210 -0
  440. package/src/pdf/renderer/box-painter.ts +236 -0
  441. package/src/pdf/renderer/page-paint.ts +102 -0
  442. package/src/pdf/renderer/paint-box-shadows.ts +218 -0
  443. package/src/pdf/renderer/radius.ts +58 -0
  444. package/src/pdf/renderers/graphics-state-manager.ts +40 -0
  445. package/src/pdf/renderers/image-renderer.ts +127 -0
  446. package/src/pdf/renderers/radius-utils.ts +80 -0
  447. package/src/pdf/renderers/rectangle-renderer.ts +129 -0
  448. package/src/pdf/renderers/rounded-rect-path.ts +120 -0
  449. package/src/pdf/renderers/shape-renderer.ts +563 -0
  450. package/src/pdf/renderers/shape-utils.ts +194 -0
  451. package/src/pdf/renderers/text-decoration-renderer.ts +313 -0
  452. package/src/pdf/renderers/text-encoder.ts +41 -0
  453. package/src/pdf/renderers/text-font-resolver.ts +75 -0
  454. package/src/pdf/renderers/text-renderer-utils.ts +28 -0
  455. package/src/pdf/renderers/text-renderer.ts +391 -0
  456. package/src/pdf/renderers/text-shadow-renderer.ts +300 -0
  457. package/src/pdf/shading/gradient-service.ts +525 -0
  458. package/src/pdf/shading/index.ts +1 -0
  459. package/src/pdf/stacking/build-stacking-contexts.ts +93 -0
  460. package/src/pdf/stacking/resolve-paint-order.ts +157 -0
  461. package/src/pdf/stacking/types.ts +40 -0
  462. package/src/pdf/svg/aspect-ratio.ts +81 -0
  463. package/src/pdf/svg/coordinate-mapper.ts +81 -0
  464. package/src/pdf/svg/geometry-builder.ts +45 -0
  465. package/src/pdf/svg/render-svg.ts +296 -0
  466. package/src/pdf/svg/shape-renderer.ts +463 -0
  467. package/src/pdf/svg/style-computer.ts +246 -0
  468. package/src/pdf/transform-adapter.ts +26 -0
  469. package/src/pdf/types.ts +377 -0
  470. package/src/pdf/utils/background-layer-resolver.ts +439 -0
  471. package/src/pdf/utils/background-tiles.ts +192 -0
  472. package/src/pdf/utils/border-dashes.ts +109 -0
  473. package/src/pdf/utils/border-radius-utils.ts +86 -0
  474. package/src/pdf/utils/box-dimensions-utils.ts +47 -0
  475. package/src/pdf/utils/clip-path-resolver.ts +50 -0
  476. package/src/pdf/utils/clipping-path-builder.ts +190 -0
  477. package/src/pdf/utils/color-utils.ts +102 -0
  478. package/src/pdf/utils/coordinate-transformer.ts +30 -0
  479. package/src/pdf/utils/drop-shadow-raster.ts +233 -0
  480. package/src/pdf/utils/encoding.ts +98 -0
  481. package/src/pdf/utils/glyph-atlas-registrar.ts +13 -0
  482. package/src/pdf/utils/glyph-run-renderer.ts +129 -0
  483. package/src/pdf/utils/image-command-partitioner.ts +28 -0
  484. package/src/pdf/utils/image-matrix-builder.ts +31 -0
  485. package/src/pdf/utils/image-utils.ts +26 -0
  486. package/src/pdf/utils/list-utils.ts +194 -0
  487. package/src/pdf/utils/node-text-run-factory.ts +202 -0
  488. package/src/pdf/utils/page-resource-registrar.ts +46 -0
  489. package/src/pdf/utils/result-combiner.ts +102 -0
  490. package/src/pdf/utils/shadow-utils.ts +127 -0
  491. package/src/pdf/utils/text-alignment-resolver.ts +76 -0
  492. package/src/pdf/utils/text-decoration-utils.ts +64 -0
  493. package/src/pdf/utils/text-layout-adjuster.ts +185 -0
  494. package/src/pdf/utils/text-utils.ts +193 -0
  495. package/src/pdf/utils/transform-scope-manager.ts +69 -0
  496. package/src/render/offset.ts +170 -0
  497. package/src/shim/empty.ts +2 -0
  498. package/src/shim/fs-empty.ts +5 -0
  499. package/src/shim/url-empty.ts +7 -0
  500. package/src/shim/zlib-empty.ts +9 -0
  501. package/src/style/shorthands/index.ts +19 -0
  502. package/src/style/ua/defaults.ts +69 -0
  503. package/src/svg/index.ts +4 -0
  504. package/src/svg/parser-registry.ts +71 -0
  505. package/src/svg/parser.ts +486 -0
  506. package/src/svg/path-data.ts +515 -0
  507. package/src/svg/types.ts +194 -0
  508. package/src/text/line-breaker.ts +321 -0
  509. package/src/text/text-transform.ts +43 -0
  510. package/src/text/text.ts +33 -0
  511. package/src/transform/css-parser.ts +95 -0
  512. package/src/types/fonts.ts +62 -0
  513. package/src/types/public.ts +19 -0
  514. package/src/units/page-utils.ts +58 -0
  515. package/src/units/units.ts +50 -0
  516. package/src/utils/base64.ts +24 -0
  517. package/test-output.txt +79 -0
  518. package/tests/css/background-parser.spec.ts +14 -0
  519. package/tests/css/clip-path-parser.spec.ts +66 -0
  520. package/tests/environment/path-resolution.spec.ts +104 -0
  521. package/tests/helpers/ai-layout-diagnostics.ts +141 -0
  522. package/tests/helpers/render-utils.ts +52 -0
  523. package/tests/helpers/text-geometry.ts +56 -0
  524. package/tests/layout/custom-properties.test.ts +38 -0
  525. package/tests/layout/gap-calculator.spec.ts +196 -0
  526. package/tests/layout/inline-background-alignment.spec.ts +93 -0
  527. package/tests/layout/inline-fragments.spec.ts +26 -0
  528. package/tests/layout/run-placer-baseline.spec.ts +108 -0
  529. package/tests/pdf/alignments.spec.ts +26 -0
  530. package/tests/pdf/background-clip.spec.ts +57 -0
  531. package/tests/pdf/background-repeat-space-round.spec.ts +35 -0
  532. package/tests/pdf/background-repeat.spec.ts +137 -0
  533. package/tests/pdf/border-radius.spec.ts +151 -0
  534. package/tests/pdf/clip-path.spec.ts +92 -0
  535. package/tests/pdf/radial-gradient.spec.ts +50 -0
  536. package/tests/pdf/svg-stroke-dash.spec.ts +81 -0
  537. package/tests/pdf/text-transform-matrix.spec.ts +43 -0
  538. package/tsconfig.json +17 -0
  539. package/types/fonts.js +10 -0
  540. package/vitest.config.ts +9 -0
@@ -0,0 +1,1559 @@
1
+ /* Copyright 2014 Google Inc. All Rights Reserved.
2
+
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
+ */
6
+
7
+ /* Command line interface for Brotli library. */
8
+
9
+ /* Mute strerror/strcpy warnings. */
10
+ #include <brotli/shared_dictionary.h>
11
+ #if !defined(_CRT_SECURE_NO_WARNINGS)
12
+ #define _CRT_SECURE_NO_WARNINGS
13
+ #endif
14
+
15
+ #include <errno.h>
16
+ #include <fcntl.h>
17
+ #include <stdio.h>
18
+ #include <stdlib.h>
19
+ #include <string.h>
20
+ #include <sys/stat.h>
21
+ #include <sys/types.h>
22
+ #include <time.h>
23
+
24
+ #include <brotli/types.h>
25
+ #include "../common/constants.h"
26
+ #include "../common/version.h"
27
+ #include <brotli/decode.h>
28
+ #include <brotli/encode.h>
29
+
30
+ #if defined(_WIN32)
31
+ #include <io.h>
32
+ #include <share.h>
33
+ #include <sys/utime.h>
34
+
35
+ #define MAKE_BINARY(FILENO) (_setmode((FILENO), _O_BINARY), (FILENO))
36
+
37
+ #if !defined(__MINGW32__)
38
+ #define STDIN_FILENO _fileno(stdin)
39
+ #define STDOUT_FILENO _fileno(stdout)
40
+ #define S_IRUSR S_IREAD
41
+ #define S_IWUSR S_IWRITE
42
+ #endif
43
+
44
+ #define fdopen _fdopen
45
+ #define isatty _isatty
46
+ #define unlink _unlink
47
+ #define utimbuf _utimbuf
48
+ #define utime _utime
49
+
50
+ #define fopen ms_fopen
51
+ #define open ms_open
52
+
53
+ #define chmod(F, P) (0)
54
+ #define chown(F, O, G) (0)
55
+
56
+ #if defined(_MSC_VER) && (_MSC_VER >= 1400)
57
+ #define fseek _fseeki64
58
+ #define ftell _ftelli64
59
+ #endif
60
+
61
+ static FILE* ms_fopen(const char* filename, const char* mode) {
62
+ FILE* result = 0;
63
+ fopen_s(&result, filename, mode);
64
+ return result;
65
+ }
66
+
67
+ static int ms_open(const char* filename, int oflag, int pmode) {
68
+ int result = -1;
69
+ _sopen_s(&result, filename, oflag | O_BINARY, _SH_DENYNO, pmode);
70
+ return result;
71
+ }
72
+ #else /* !defined(_WIN32) */
73
+ #include <unistd.h>
74
+ #include <utime.h>
75
+ #define MAKE_BINARY(FILENO) (FILENO)
76
+ #endif /* defined(_WIN32) */
77
+
78
+ #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
79
+ #define HAVE_UTIMENSAT 1
80
+ #elif defined(_ATFILE_SOURCE)
81
+ #define HAVE_UTIMENSAT 1
82
+ #else
83
+ #define HAVE_UTIMENSAT 0
84
+ #endif
85
+
86
+ #if HAVE_UTIMENSAT
87
+ #if defined(__APPLE__)
88
+ #define ATIME_NSEC(S) ((S)->st_atimespec.tv_nsec)
89
+ #define MTIME_NSEC(S) ((S)->st_mtimespec.tv_nsec)
90
+ #else /* defined(__APPLE__) */
91
+ #define ATIME_NSEC(S) ((S)->st_atim.tv_nsec)
92
+ #define MTIME_NSEC(S) ((S)->st_mtim.tv_nsec)
93
+ #endif
94
+ #endif /* HAVE_UTIMENSAT */
95
+
96
+ typedef enum {
97
+ COMMAND_COMPRESS,
98
+ COMMAND_DECOMPRESS,
99
+ COMMAND_HELP,
100
+ COMMAND_INVALID,
101
+ COMMAND_TEST_INTEGRITY,
102
+ COMMAND_NOOP,
103
+ COMMAND_VERSION
104
+ } Command;
105
+
106
+ typedef enum {
107
+ COMMENT_INIT,
108
+ COMMENT_READ,
109
+ COMMENT_OK,
110
+ COMMENT_BAD,
111
+ } CommentState;
112
+
113
+ #define DEFAULT_LGWIN 24
114
+ #define DEFAULT_SUFFIX ".br"
115
+ #define MAX_OPTIONS 24
116
+ #define MAX_COMMENT_LEN 80
117
+
118
+ typedef struct {
119
+ /* Parameters */
120
+ int quality;
121
+ int lgwin;
122
+ int verbosity;
123
+ BROTLI_BOOL force_overwrite;
124
+ BROTLI_BOOL junk_source;
125
+ BROTLI_BOOL reject_uncompressible;
126
+ BROTLI_BOOL copy_stat;
127
+ BROTLI_BOOL write_to_stdout;
128
+ BROTLI_BOOL test_integrity;
129
+ BROTLI_BOOL decompress;
130
+ BROTLI_BOOL large_window;
131
+ BROTLI_BOOL allow_concatenated;
132
+ const char* output_path;
133
+ const char* dictionary_path;
134
+ const char* suffix;
135
+ uint8_t comment[MAX_COMMENT_LEN];
136
+ size_t comment_len;
137
+ size_t comment_pos;
138
+ CommentState comment_state;
139
+ int not_input_indices[MAX_OPTIONS];
140
+ size_t longest_path_len;
141
+ size_t input_count;
142
+
143
+ /* Inner state */
144
+ int argc;
145
+ char** argv;
146
+ uint8_t* dictionary;
147
+ size_t dictionary_size;
148
+ BrotliEncoderPreparedDictionary* prepared_dictionary;
149
+ BrotliDecoderState* decoder;
150
+ char* modified_path; /* Storage for path with appended / cut suffix */
151
+ int iterator;
152
+ int ignore;
153
+ BROTLI_BOOL iterator_error;
154
+ uint8_t* buffer;
155
+ uint8_t* input;
156
+ uint8_t* output;
157
+ const char* current_input_path;
158
+ const char* current_output_path;
159
+ int64_t input_file_length; /* -1, if impossible to calculate */
160
+ FILE* fin;
161
+ FILE* fout;
162
+
163
+ /* I/O buffers */
164
+ size_t available_in;
165
+ const uint8_t* next_in;
166
+ size_t available_out;
167
+ uint8_t* next_out;
168
+
169
+ /* Reporting */
170
+ /* size_t would be large enough,
171
+ until 4GiB+ files are compressed / decompressed on 32-bit CPUs. */
172
+ size_t total_in;
173
+ size_t total_out;
174
+ clock_t start_time;
175
+ clock_t end_time;
176
+ } Context;
177
+
178
+ /* Parse base 64 encoded string to buffer. Not performance-centric.
179
+ |out_len| as input is buffer size; |out_len| as output is decoded length.
180
+ Returns BROTLI_FALSE if either input is not (relaxed) base 64 conformant,
181
+ or output does not fit buffer. */
182
+ static BROTLI_BOOL ParseBase64(const char* str, uint8_t* out, size_t* out_len) {
183
+ size_t in_len = strlen(str);
184
+ size_t max_out_len = *out_len;
185
+ size_t i;
186
+ size_t bit_count = 0;
187
+ uint32_t bits = 0;
188
+ size_t padding_count = 0;
189
+ size_t octet_count = 0;
190
+ for (i = 0; i < in_len; ++i) {
191
+ char c = str[i];
192
+ int sextet = 0;
193
+ if (c == 9 || c == 10 || c == 13 || c == ' ') {
194
+ continue;
195
+ }
196
+ if (c == '=') {
197
+ padding_count++;
198
+ continue;
199
+ }
200
+ if (padding_count) return BROTLI_FALSE;
201
+ if (c == '+' || c == '-') {
202
+ sextet = 62;
203
+ } else if (c == '/' || c == '_') {
204
+ sextet = 63;
205
+ } else if (c >= 'A' && c <= 'Z') {
206
+ sextet = c - 'A';
207
+ } else if (c >= 'a' && c <= 'z') {
208
+ sextet = c - 'a' + 26;
209
+ } else if (c >= '0' && c <= '9') {
210
+ sextet = c - '0' + 52;
211
+ } else {
212
+ return BROTLI_FALSE;
213
+ }
214
+ bits = (bits << 6) | (uint32_t)sextet;
215
+ bit_count += 6;
216
+ if (bit_count >= 8) {
217
+ if (octet_count == max_out_len) return BROTLI_FALSE;
218
+ bit_count -= 8;
219
+ out[octet_count++] = (bits >> bit_count) & 0xFF;
220
+ }
221
+ }
222
+ if (padding_count > 2) return BROTLI_FALSE;
223
+ *out_len = octet_count;
224
+ return BROTLI_TRUE;
225
+ }
226
+
227
+ /* Parse up to 5 decimal digits. */
228
+ static BROTLI_BOOL ParseInt(const char* s, int low, int high, int* result) {
229
+ int value = 0;
230
+ int i;
231
+ for (i = 0; i < 5; ++i) {
232
+ char c = s[i];
233
+ if (c == 0) break;
234
+ if (s[i] < '0' || s[i] > '9') return BROTLI_FALSE;
235
+ value = (10 * value) + (c - '0');
236
+ }
237
+ if (i == 0) return BROTLI_FALSE;
238
+ if (i > 1 && s[0] == '0') return BROTLI_FALSE;
239
+ if (s[i] != 0) return BROTLI_FALSE;
240
+ if (value < low || value > high) return BROTLI_FALSE;
241
+ *result = value;
242
+ return BROTLI_TRUE;
243
+ }
244
+
245
+ /* Returns "base file name" or its tail, if it contains '/' or '\'. */
246
+ static const char* FileName(const char* path) {
247
+ const char* separator_position = strrchr(path, '/');
248
+ if (separator_position) path = separator_position + 1;
249
+ separator_position = strrchr(path, '\\');
250
+ if (separator_position) path = separator_position + 1;
251
+ return path;
252
+ }
253
+
254
+ /* Detect if the program name is a special alias that infers a command type. */
255
+ static BROTLI_BOOL CheckAlias(const char* name, const char* alias) {
256
+ /* TODO: cast name to lower case? */
257
+ size_t alias_len = strlen(alias);
258
+ name = FileName(name);
259
+ /* Partial comparison. On Windows there could be ".exe" suffix. */
260
+ if (strncmp(name, alias, alias_len) == 0) {
261
+ char terminator = name[alias_len];
262
+ if (terminator == 0 || terminator == '.') return BROTLI_TRUE;
263
+ }
264
+ return BROTLI_FALSE;
265
+ }
266
+
267
+ static Command ParseParams(Context* params) {
268
+ int argc = params->argc;
269
+ char** argv = params->argv;
270
+ int i;
271
+ int next_option_index = 0;
272
+ size_t input_count = 0;
273
+ size_t longest_path_len = 1;
274
+ BROTLI_BOOL command_set = BROTLI_FALSE;
275
+ BROTLI_BOOL quality_set = BROTLI_FALSE;
276
+ BROTLI_BOOL output_set = BROTLI_FALSE;
277
+ BROTLI_BOOL keep_set = BROTLI_FALSE;
278
+ BROTLI_BOOL squash_set = BROTLI_FALSE;
279
+ BROTLI_BOOL lgwin_set = BROTLI_FALSE;
280
+ BROTLI_BOOL suffix_set = BROTLI_FALSE;
281
+ BROTLI_BOOL after_dash_dash = BROTLI_FALSE;
282
+ BROTLI_BOOL comment_set = BROTLI_FALSE;
283
+ BROTLI_BOOL concatenated_set = BROTLI_FALSE;
284
+ Command command = COMMAND_COMPRESS;
285
+
286
+ if (CheckAlias(argv[0], "brcat")) {
287
+ command_set = BROTLI_TRUE;
288
+ command = COMMAND_DECOMPRESS;
289
+ concatenated_set = BROTLI_TRUE;
290
+ params->allow_concatenated = BROTLI_TRUE;
291
+ output_set = BROTLI_TRUE;
292
+ params->write_to_stdout = BROTLI_TRUE;
293
+ } else if (CheckAlias(argv[0], "unbrotli")) {
294
+ command_set = BROTLI_TRUE;
295
+ command = COMMAND_DECOMPRESS;
296
+ }
297
+
298
+ for (i = 1; i < argc; ++i) {
299
+ const char* arg = argv[i];
300
+ /* C99 5.1.2.2.1: "members argv[0] through argv[argc-1] inclusive shall
301
+ contain pointers to strings"; NULL and 0-length are not forbidden. */
302
+ size_t arg_len = arg ? strlen(arg) : 0;
303
+
304
+ if (arg_len == 0) {
305
+ params->not_input_indices[next_option_index++] = i;
306
+ continue;
307
+ }
308
+
309
+ /* Too many options. The expected longest option list is:
310
+ "-q 0 -w 10 -o f -D d -S b -d -f -k -n -v -K --", i.e. 17 items in total.
311
+ This check is an additional guard that is never triggered, but provides
312
+ a guard for future changes. */
313
+ if (next_option_index > (MAX_OPTIONS - 2)) {
314
+ fprintf(stderr, "too many options passed\n");
315
+ return COMMAND_INVALID;
316
+ }
317
+
318
+ /* Input file entry. */
319
+ if (after_dash_dash || arg[0] != '-' || arg_len == 1) {
320
+ input_count++;
321
+ if (longest_path_len < arg_len) longest_path_len = arg_len;
322
+ continue;
323
+ }
324
+
325
+ /* Not a file entry. */
326
+ params->not_input_indices[next_option_index++] = i;
327
+
328
+ /* '--' entry stop parsing arguments. */
329
+ if (arg_len == 2 && arg[1] == '-') {
330
+ after_dash_dash = BROTLI_TRUE;
331
+ continue;
332
+ }
333
+
334
+ /* Simple / coalesced options. */
335
+ if (arg[1] != '-') {
336
+ size_t j;
337
+ for (j = 1; j < arg_len; ++j) {
338
+ char c = arg[j];
339
+ if (c >= '0' && c <= '9') {
340
+ if (quality_set) {
341
+ fprintf(stderr, "quality already set\n");
342
+ return COMMAND_INVALID;
343
+ }
344
+ quality_set = BROTLI_TRUE;
345
+ params->quality = c - '0';
346
+ continue;
347
+ } else if (c == 'c') {
348
+ if (output_set) {
349
+ fprintf(stderr, "write to standard output already set\n");
350
+ return COMMAND_INVALID;
351
+ }
352
+ output_set = BROTLI_TRUE;
353
+ params->write_to_stdout = BROTLI_TRUE;
354
+ continue;
355
+ } else if (c == 'd') {
356
+ if (command_set) {
357
+ fprintf(stderr, "command already set when parsing -d\n");
358
+ return COMMAND_INVALID;
359
+ }
360
+ command_set = BROTLI_TRUE;
361
+ command = COMMAND_DECOMPRESS;
362
+ continue;
363
+ } else if (c == 'f') {
364
+ if (params->force_overwrite) {
365
+ fprintf(stderr, "force output overwrite already set\n");
366
+ return COMMAND_INVALID;
367
+ }
368
+ params->force_overwrite = BROTLI_TRUE;
369
+ continue;
370
+ } else if (c == 'h') {
371
+ /* Don't parse further. */
372
+ return COMMAND_HELP;
373
+ } else if (c == 'j' || c == 'k') {
374
+ if (keep_set) {
375
+ fprintf(stderr, "argument --rm / -j or --keep / -k already set\n");
376
+ return COMMAND_INVALID;
377
+ }
378
+ keep_set = BROTLI_TRUE;
379
+ params->junk_source = TO_BROTLI_BOOL(c == 'j');
380
+ continue;
381
+ } else if (c == 'n') {
382
+ if (!params->copy_stat) {
383
+ fprintf(stderr, "argument --no-copy-stat / -n already set\n");
384
+ return COMMAND_INVALID;
385
+ }
386
+ params->copy_stat = BROTLI_FALSE;
387
+ continue;
388
+ } else if (c == 's') {
389
+ if (squash_set) {
390
+ fprintf(stderr, "argument --squash / -s already set\n");
391
+ return COMMAND_INVALID;
392
+ }
393
+ squash_set = BROTLI_TRUE;
394
+ params->reject_uncompressible = BROTLI_TRUE;
395
+ continue;
396
+ } else if (c == 't') {
397
+ if (command_set) {
398
+ fprintf(stderr, "command already set when parsing -t\n");
399
+ return COMMAND_INVALID;
400
+ }
401
+ command_set = BROTLI_TRUE;
402
+ command = COMMAND_TEST_INTEGRITY;
403
+ continue;
404
+ } else if (c == 'v') {
405
+ if (params->verbosity > 0) {
406
+ fprintf(stderr, "argument --verbose / -v already set\n");
407
+ return COMMAND_INVALID;
408
+ }
409
+ params->verbosity = 1;
410
+ continue;
411
+ } else if (c == 'K') {
412
+ if (concatenated_set) {
413
+ fprintf(stderr, "argument -K / --concatenated already set\n");
414
+ return COMMAND_INVALID;
415
+ }
416
+ concatenated_set = BROTLI_TRUE;
417
+ params->allow_concatenated = BROTLI_TRUE;
418
+ continue;
419
+ } else if (c == 'V') {
420
+ /* Don't parse further. */
421
+ return COMMAND_VERSION;
422
+ } else if (c == 'Z') {
423
+ if (quality_set) {
424
+ fprintf(stderr, "quality already set\n");
425
+ return COMMAND_INVALID;
426
+ }
427
+ quality_set = BROTLI_TRUE;
428
+ params->quality = 11;
429
+ continue;
430
+ }
431
+ /* o/q/w/C/D/S with parameter is expected */
432
+ if (c != 'o' && c != 'q' && c != 'w' && c != 'C' && c != 'D' &&
433
+ c != 'S') {
434
+ fprintf(stderr, "invalid argument -%c\n", c);
435
+ return COMMAND_INVALID;
436
+ }
437
+ if (j + 1 != arg_len) {
438
+ fprintf(stderr, "expected parameter for argument -%c\n", c);
439
+ return COMMAND_INVALID;
440
+ }
441
+ i++;
442
+ if (i == argc || !argv[i] || argv[i][0] == 0) {
443
+ fprintf(stderr, "expected parameter for argument -%c\n", c);
444
+ return COMMAND_INVALID;
445
+ }
446
+ params->not_input_indices[next_option_index++] = i;
447
+ if (c == 'o') {
448
+ if (output_set) {
449
+ fprintf(stderr, "write to standard output already set (-o)\n");
450
+ return COMMAND_INVALID;
451
+ }
452
+ params->output_path = argv[i];
453
+ } else if (c == 'q') {
454
+ if (quality_set) {
455
+ fprintf(stderr, "quality already set\n");
456
+ return COMMAND_INVALID;
457
+ }
458
+ quality_set = ParseInt(argv[i], BROTLI_MIN_QUALITY,
459
+ BROTLI_MAX_QUALITY, &params->quality);
460
+ if (!quality_set) {
461
+ fprintf(stderr, "error parsing quality value [%s]\n", argv[i]);
462
+ return COMMAND_INVALID;
463
+ }
464
+ } else if (c == 'w') {
465
+ if (lgwin_set) {
466
+ fprintf(stderr, "lgwin parameter already set\n");
467
+ return COMMAND_INVALID;
468
+ }
469
+ lgwin_set = ParseInt(argv[i], 0,
470
+ BROTLI_MAX_WINDOW_BITS, &params->lgwin);
471
+ if (!lgwin_set) {
472
+ fprintf(stderr, "error parsing lgwin value [%s]\n", argv[i]);
473
+ return COMMAND_INVALID;
474
+ }
475
+ if (params->lgwin != 0 && params->lgwin < BROTLI_MIN_WINDOW_BITS) {
476
+ fprintf(stderr,
477
+ "lgwin parameter (%d) smaller than the minimum (%d)\n",
478
+ params->lgwin, BROTLI_MIN_WINDOW_BITS);
479
+ return COMMAND_INVALID;
480
+ }
481
+ } else if (c == 'C') {
482
+ if (comment_set) {
483
+ fprintf(stderr, "comment already set\n");
484
+ return COMMAND_INVALID;
485
+ }
486
+ params->comment_len = MAX_COMMENT_LEN;
487
+ if (!ParseBase64(argv[i], params->comment, &params->comment_len)) {
488
+ fprintf(stderr, "invalid base64-encoded comment\n");
489
+ return COMMAND_INVALID;
490
+ }
491
+ comment_set = BROTLI_TRUE;
492
+ } else if (c == 'D') {
493
+ if (params->dictionary_path) {
494
+ fprintf(stderr, "dictionary path already set\n");
495
+ return COMMAND_INVALID;
496
+ }
497
+ params->dictionary_path = argv[i];
498
+ } else if (c == 'S') {
499
+ if (suffix_set) {
500
+ fprintf(stderr, "suffix already set\n");
501
+ return COMMAND_INVALID;
502
+ }
503
+ suffix_set = BROTLI_TRUE;
504
+ params->suffix = argv[i];
505
+ }
506
+ }
507
+ } else { /* Double-dash. */
508
+ arg = &arg[2];
509
+ if (strcmp("best", arg) == 0) {
510
+ if (quality_set) {
511
+ fprintf(stderr, "quality already set\n");
512
+ return COMMAND_INVALID;
513
+ }
514
+ quality_set = BROTLI_TRUE;
515
+ params->quality = 11;
516
+ } else if (strcmp("concatenated", arg) == 0) {
517
+ if (concatenated_set) {
518
+ fprintf(stderr, "argument -K / --concatenated already set\n");
519
+ return COMMAND_INVALID;
520
+ }
521
+ concatenated_set = BROTLI_TRUE;
522
+ params->allow_concatenated = BROTLI_TRUE;
523
+ continue;
524
+ } else if (strcmp("decompress", arg) == 0) {
525
+ if (command_set) {
526
+ fprintf(stderr, "command already set when parsing --decompress\n");
527
+ return COMMAND_INVALID;
528
+ }
529
+ command_set = BROTLI_TRUE;
530
+ command = COMMAND_DECOMPRESS;
531
+ } else if (strcmp("force", arg) == 0) {
532
+ if (params->force_overwrite) {
533
+ fprintf(stderr, "force output overwrite already set\n");
534
+ return COMMAND_INVALID;
535
+ }
536
+ params->force_overwrite = BROTLI_TRUE;
537
+ } else if (strcmp("help", arg) == 0) {
538
+ /* Don't parse further. */
539
+ return COMMAND_HELP;
540
+ } else if (strcmp("keep", arg) == 0) {
541
+ if (keep_set) {
542
+ fprintf(stderr, "argument --rm / -j or --keep / -k already set\n");
543
+ return COMMAND_INVALID;
544
+ }
545
+ keep_set = BROTLI_TRUE;
546
+ params->junk_source = BROTLI_FALSE;
547
+ } else if (strcmp("no-copy-stat", arg) == 0) {
548
+ if (!params->copy_stat) {
549
+ fprintf(stderr, "argument --no-copy-stat / -n already set\n");
550
+ return COMMAND_INVALID;
551
+ }
552
+ params->copy_stat = BROTLI_FALSE;
553
+ } else if (strcmp("rm", arg) == 0) {
554
+ if (keep_set) {
555
+ fprintf(stderr, "argument --rm / -j or --keep / -k already set\n");
556
+ return COMMAND_INVALID;
557
+ }
558
+ keep_set = BROTLI_TRUE;
559
+ params->junk_source = BROTLI_TRUE;
560
+ } else if (strcmp("squash", arg) == 0) {
561
+ if (squash_set) {
562
+ fprintf(stderr, "argument --squash / -s already set\n");
563
+ return COMMAND_INVALID;
564
+ }
565
+ squash_set = BROTLI_TRUE;
566
+ params->reject_uncompressible = BROTLI_TRUE;
567
+ continue;
568
+ } else if (strcmp("stdout", arg) == 0) {
569
+ if (output_set) {
570
+ fprintf(stderr, "write to standard output already set\n");
571
+ return COMMAND_INVALID;
572
+ }
573
+ output_set = BROTLI_TRUE;
574
+ params->write_to_stdout = BROTLI_TRUE;
575
+ } else if (strcmp("test", arg) == 0) {
576
+ if (command_set) {
577
+ fprintf(stderr, "command already set when parsing --test\n");
578
+ return COMMAND_INVALID;
579
+ }
580
+ command_set = BROTLI_TRUE;
581
+ command = COMMAND_TEST_INTEGRITY;
582
+ } else if (strcmp("verbose", arg) == 0) {
583
+ if (params->verbosity > 0) {
584
+ fprintf(stderr, "argument --verbose / -v already set\n");
585
+ return COMMAND_INVALID;
586
+ }
587
+ params->verbosity = 1;
588
+ } else if (strcmp("version", arg) == 0) {
589
+ /* Don't parse further. */
590
+ return COMMAND_VERSION;
591
+ } else {
592
+ /* key=value */
593
+ const char* value = strchr(arg, '=');
594
+ size_t key_len;
595
+ if (!value || value[1] == 0) {
596
+ fprintf(stderr, "must pass the parameter as --%s=value\n", arg);
597
+ return COMMAND_INVALID;
598
+ }
599
+ key_len = (size_t)(value - arg);
600
+ value++;
601
+ if (strncmp("comment", arg, key_len) == 0) {
602
+ if (comment_set) {
603
+ fprintf(stderr, "comment already set\n");
604
+ return COMMAND_INVALID;
605
+ }
606
+ params->comment_len = MAX_COMMENT_LEN;
607
+ if (!ParseBase64(value, params->comment, &params->comment_len)) {
608
+ fprintf(stderr, "invalid base64-encoded comment\n");
609
+ return COMMAND_INVALID;
610
+ }
611
+ comment_set = BROTLI_TRUE;
612
+ } else if (strncmp("dictionary", arg, key_len) == 0) {
613
+ if (params->dictionary_path) {
614
+ fprintf(stderr, "dictionary path already set\n");
615
+ return COMMAND_INVALID;
616
+ }
617
+ params->dictionary_path = value;
618
+ } else if (strncmp("lgwin", arg, key_len) == 0) {
619
+ if (lgwin_set) {
620
+ fprintf(stderr, "lgwin parameter already set\n");
621
+ return COMMAND_INVALID;
622
+ }
623
+ lgwin_set = ParseInt(value, 0,
624
+ BROTLI_MAX_WINDOW_BITS, &params->lgwin);
625
+ if (!lgwin_set) {
626
+ fprintf(stderr, "error parsing lgwin value [%s]\n", value);
627
+ return COMMAND_INVALID;
628
+ }
629
+ if (params->lgwin != 0 && params->lgwin < BROTLI_MIN_WINDOW_BITS) {
630
+ fprintf(stderr,
631
+ "lgwin parameter (%d) smaller than the minimum (%d)\n",
632
+ params->lgwin, BROTLI_MIN_WINDOW_BITS);
633
+ return COMMAND_INVALID;
634
+ }
635
+ } else if (strncmp("large_window", arg, key_len) == 0) {
636
+ /* This option is intentionally not mentioned in help. */
637
+ if (lgwin_set) {
638
+ fprintf(stderr, "lgwin parameter already set\n");
639
+ return COMMAND_INVALID;
640
+ }
641
+ lgwin_set = ParseInt(value, 0,
642
+ BROTLI_LARGE_MAX_WINDOW_BITS, &params->lgwin);
643
+ if (!lgwin_set) {
644
+ fprintf(stderr, "error parsing lgwin value [%s]\n", value);
645
+ return COMMAND_INVALID;
646
+ }
647
+ if (params->lgwin != 0 && params->lgwin < BROTLI_MIN_WINDOW_BITS) {
648
+ fprintf(stderr,
649
+ "lgwin parameter (%d) smaller than the minimum (%d)\n",
650
+ params->lgwin, BROTLI_MIN_WINDOW_BITS);
651
+ return COMMAND_INVALID;
652
+ }
653
+ } else if (strncmp("output", arg, key_len) == 0) {
654
+ if (output_set) {
655
+ fprintf(stderr,
656
+ "write to standard output already set (--output)\n");
657
+ return COMMAND_INVALID;
658
+ }
659
+ params->output_path = value;
660
+ } else if (strncmp("quality", arg, key_len) == 0) {
661
+ if (quality_set) {
662
+ fprintf(stderr, "quality already set\n");
663
+ return COMMAND_INVALID;
664
+ }
665
+ quality_set = ParseInt(value, BROTLI_MIN_QUALITY,
666
+ BROTLI_MAX_QUALITY, &params->quality);
667
+ if (!quality_set) {
668
+ fprintf(stderr, "error parsing quality value [%s]\n", value);
669
+ return COMMAND_INVALID;
670
+ }
671
+ } else if (strncmp("suffix", arg, key_len) == 0) {
672
+ if (suffix_set) {
673
+ fprintf(stderr, "suffix already set\n");
674
+ return COMMAND_INVALID;
675
+ }
676
+ suffix_set = BROTLI_TRUE;
677
+ params->suffix = value;
678
+ } else {
679
+ fprintf(stderr, "invalid parameter: [%s]\n", arg);
680
+ return COMMAND_INVALID;
681
+ }
682
+ }
683
+ }
684
+ }
685
+
686
+ params->input_count = input_count;
687
+ params->longest_path_len = longest_path_len;
688
+ params->decompress = (command == COMMAND_DECOMPRESS);
689
+ params->test_integrity = (command == COMMAND_TEST_INTEGRITY);
690
+
691
+ if (input_count > 1 && output_set) return COMMAND_INVALID;
692
+ if (params->test_integrity) {
693
+ if (params->output_path) return COMMAND_INVALID;
694
+ if (params->write_to_stdout) return COMMAND_INVALID;
695
+ }
696
+ if (params->reject_uncompressible && params->write_to_stdout) {
697
+ return COMMAND_INVALID;
698
+ }
699
+ if (strchr(params->suffix, '/') || strchr(params->suffix, '\\')) {
700
+ return COMMAND_INVALID;
701
+ }
702
+ if (!params->decompress && params->allow_concatenated) {
703
+ return COMMAND_INVALID;
704
+ }
705
+ if (params->allow_concatenated && params->comment_len) {
706
+ return COMMAND_INVALID;
707
+ }
708
+
709
+ return command;
710
+ }
711
+
712
+ static void PrintVersion(void) {
713
+ int major = BROTLI_VERSION_MAJOR;
714
+ int minor = BROTLI_VERSION_MINOR;
715
+ int patch = BROTLI_VERSION_PATCH;
716
+ fprintf(stdout, "brotli %d.%d.%d\n", major, minor, patch);
717
+ }
718
+
719
+ static void PrintHelp(const char* name, BROTLI_BOOL error) {
720
+ FILE* media = error ? stderr : stdout;
721
+ /* String is cut to pieces with length less than 509, to conform C90 spec. */
722
+ fprintf(media,
723
+ "Usage: %s [OPTION]... [FILE]...\n",
724
+ name);
725
+ fprintf(media,
726
+ "Options:\n"
727
+ " -# compression level (0-9)\n"
728
+ " -c, --stdout write on standard output\n"
729
+ " -d, --decompress decompress\n"
730
+ " -f, --force force output file overwrite\n"
731
+ " -h, --help display this help and exit\n");
732
+ fprintf(media,
733
+ " -j, --rm remove source file(s)\n"
734
+ " -s, --squash remove destination file if larger than source\n"
735
+ " -k, --keep keep source file(s) (default)\n"
736
+ " -n, --no-copy-stat do not copy source file(s) attributes\n"
737
+ " -o FILE, --output=FILE output file (only if 1 input file)\n");
738
+ fprintf(media,
739
+ " -q NUM, --quality=NUM compression level (%d-%d)\n",
740
+ BROTLI_MIN_QUALITY, BROTLI_MAX_QUALITY);
741
+ fprintf(media,
742
+ " -t, --test test compressed file integrity\n"
743
+ " -v, --verbose verbose mode\n");
744
+ fprintf(media,
745
+ " -w NUM, --lgwin=NUM set LZ77 window size (0, %d-%d)\n"
746
+ " window size = 2**NUM - 16\n"
747
+ " 0 lets compressor choose the optimal value\n",
748
+ BROTLI_MIN_WINDOW_BITS, BROTLI_MAX_WINDOW_BITS);
749
+ fprintf(media,
750
+ " --large_window=NUM use incompatible large-window brotli\n"
751
+ " bitstream with window size (0, %d-%d)\n"
752
+ " WARNING: this format is not compatible\n"
753
+ " with brotli RFC 7932 and may not be\n"
754
+ " decodable with regular brotli decoders\n",
755
+ BROTLI_MIN_WINDOW_BITS, BROTLI_LARGE_MAX_WINDOW_BITS);
756
+ fprintf(media,
757
+ " -C B64, --comment=B64 set comment; argument is base64-decoded first;\n"
758
+ " (maximal decoded length: %d)\n"
759
+ " when decoding: check stream comment;\n"
760
+ " when encoding: embed comment (fingerprint)\n",
761
+ MAX_COMMENT_LEN);
762
+ fprintf(media,
763
+ " -D FILE, --dictionary=FILE use FILE as raw (LZ77) dictionary\n"
764
+ " -K, --concatenated allows concatenated brotli streams as input\n");
765
+ fprintf(media,
766
+ " -S SUF, --suffix=SUF output file suffix (default:'%s')\n",
767
+ DEFAULT_SUFFIX);
768
+ fprintf(media,
769
+ " -V, --version display version and exit\n"
770
+ " -Z, --best use best compression level (11) (default)\n"
771
+ "Simple options could be coalesced, i.e. '-9kf' is equivalent to '-9 -k -f'.\n"
772
+ "With no FILE, or when FILE is -, read standard input.\n"
773
+ "All arguments after '--' are treated as files.\n");
774
+ }
775
+
776
+ static const char* PrintablePath(const char* path) {
777
+ return path ? path : "con";
778
+ }
779
+
780
+ static BROTLI_BOOL OpenInputFile(const char* input_path, FILE** f) {
781
+ *f = NULL;
782
+ if (!input_path) {
783
+ *f = fdopen(MAKE_BINARY(STDIN_FILENO), "rb");
784
+ return BROTLI_TRUE;
785
+ }
786
+ *f = fopen(input_path, "rb");
787
+ if (!*f) {
788
+ fprintf(stderr, "failed to open input file [%s]: %s\n",
789
+ PrintablePath(input_path), strerror(errno));
790
+ return BROTLI_FALSE;
791
+ }
792
+ return BROTLI_TRUE;
793
+ }
794
+
795
+ static BROTLI_BOOL OpenOutputFile(const char* output_path, FILE** f,
796
+ BROTLI_BOOL force) {
797
+ int fd;
798
+ *f = NULL;
799
+ if (!output_path) {
800
+ *f = fdopen(MAKE_BINARY(STDOUT_FILENO), "wb");
801
+ return BROTLI_TRUE;
802
+ }
803
+ fd = open(output_path, O_CREAT | (force ? 0 : O_EXCL) | O_WRONLY | O_TRUNC,
804
+ S_IRUSR | S_IWUSR);
805
+ if (fd < 0) {
806
+ fprintf(stderr, "failed to open output file [%s]: %s\n",
807
+ PrintablePath(output_path), strerror(errno));
808
+ return BROTLI_FALSE;
809
+ }
810
+ *f = fdopen(fd, "wb");
811
+ if (!*f) {
812
+ fprintf(stderr, "failed to open output file [%s]: %s\n",
813
+ PrintablePath(output_path), strerror(errno));
814
+ return BROTLI_FALSE;
815
+ }
816
+ return BROTLI_TRUE;
817
+ }
818
+
819
+ static int64_t FileSize(const char* path) {
820
+ FILE* f = fopen(path, "rb");
821
+ int64_t retval;
822
+ if (f == NULL) {
823
+ return -1;
824
+ }
825
+ if (fseek(f, 0L, SEEK_END) != 0) {
826
+ fclose(f);
827
+ return -1;
828
+ }
829
+ retval = ftell(f);
830
+ if (fclose(f) != 0) {
831
+ return -1;
832
+ }
833
+ return retval;
834
+ }
835
+
836
+ static int CopyTimeStat(const struct stat* statbuf, const char* output_path) {
837
+ #if HAVE_UTIMENSAT
838
+ struct timespec times[2];
839
+ times[0].tv_sec = statbuf->st_atime;
840
+ times[0].tv_nsec = ATIME_NSEC(statbuf);
841
+ times[1].tv_sec = statbuf->st_mtime;
842
+ times[1].tv_nsec = MTIME_NSEC(statbuf);
843
+ return utimensat(AT_FDCWD, output_path, times, AT_SYMLINK_NOFOLLOW);
844
+ #else
845
+ struct utimbuf times;
846
+ times.actime = statbuf->st_atime;
847
+ times.modtime = statbuf->st_mtime;
848
+ return utime(output_path, &times);
849
+ #endif
850
+ }
851
+
852
+ /* Copy file times and permissions.
853
+ TODO(eustas): this is a "best effort" implementation; honest cross-platform
854
+ fully featured implementation is way too hacky; add more hacks by request. */
855
+ static void CopyStat(const char* input_path, const char* output_path) {
856
+ struct stat statbuf;
857
+ int res;
858
+ if (input_path == 0 || output_path == 0) {
859
+ return;
860
+ }
861
+ if (stat(input_path, &statbuf) != 0) {
862
+ return;
863
+ }
864
+ res = CopyTimeStat(&statbuf, output_path);
865
+ res = chmod(output_path, statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
866
+ if (res != 0) {
867
+ fprintf(stderr, "setting access bits failed for [%s]: %s\n",
868
+ PrintablePath(output_path), strerror(errno));
869
+ }
870
+ res = chown(output_path, (uid_t)-1, statbuf.st_gid);
871
+ if (res != 0) {
872
+ fprintf(stderr, "setting group failed for [%s]: %s\n",
873
+ PrintablePath(output_path), strerror(errno));
874
+ }
875
+ res = chown(output_path, statbuf.st_uid, (gid_t)-1);
876
+ if (res != 0) {
877
+ fprintf(stderr, "setting user failed for [%s]: %s\n",
878
+ PrintablePath(output_path), strerror(errno));
879
+ }
880
+ }
881
+
882
+ /* Result ownership is passed to caller.
883
+ |*dictionary_size| is set to resulting buffer size. */
884
+ static BROTLI_BOOL ReadDictionary(Context* context, Command command) {
885
+ static const int kMaxDictionarySize =
886
+ BROTLI_MAX_DISTANCE - BROTLI_MAX_BACKWARD_LIMIT(24);
887
+ FILE* f;
888
+ int64_t file_size_64;
889
+ uint8_t* buffer;
890
+ size_t bytes_read;
891
+
892
+ if (context->dictionary_path == NULL) return BROTLI_TRUE;
893
+ f = fopen(context->dictionary_path, "rb");
894
+ if (f == NULL) {
895
+ fprintf(stderr, "failed to open dictionary file [%s]: %s\n",
896
+ PrintablePath(context->dictionary_path), strerror(errno));
897
+ return BROTLI_FALSE;
898
+ }
899
+
900
+ file_size_64 = FileSize(context->dictionary_path);
901
+ if (file_size_64 == -1) {
902
+ fprintf(stderr, "could not get size of dictionary file [%s]",
903
+ PrintablePath(context->dictionary_path));
904
+ fclose(f);
905
+ return BROTLI_FALSE;
906
+ }
907
+
908
+ if (file_size_64 > kMaxDictionarySize) {
909
+ fprintf(stderr, "dictionary [%s] is larger than maximum allowed: %d\n",
910
+ PrintablePath(context->dictionary_path), kMaxDictionarySize);
911
+ fclose(f);
912
+ return BROTLI_FALSE;
913
+ }
914
+ context->dictionary_size = (size_t)file_size_64;
915
+
916
+ buffer = (uint8_t*)malloc(context->dictionary_size);
917
+ if (!buffer) {
918
+ fprintf(stderr, "could not read dictionary: out of memory\n");
919
+ fclose(f);
920
+ return BROTLI_FALSE;
921
+ }
922
+ bytes_read = fread(buffer, sizeof(uint8_t), context->dictionary_size, f);
923
+ if (bytes_read != context->dictionary_size) {
924
+ free(buffer);
925
+ fprintf(stderr, "failed to read dictionary [%s]: %s\n",
926
+ PrintablePath(context->dictionary_path), strerror(errno));
927
+ fclose(f);
928
+ return BROTLI_FALSE;
929
+ }
930
+ fclose(f);
931
+ context->dictionary = buffer;
932
+ if (command == COMMAND_COMPRESS) {
933
+ context->prepared_dictionary = BrotliEncoderPrepareDictionary(
934
+ BROTLI_SHARED_DICTIONARY_RAW, context->dictionary_size,
935
+ context->dictionary, BROTLI_MAX_QUALITY, NULL, NULL, NULL);
936
+ if (context->prepared_dictionary == NULL) {
937
+ fprintf(stderr, "failed to prepare dictionary [%s]\n",
938
+ PrintablePath(context->dictionary_path));
939
+ return BROTLI_FALSE;
940
+ }
941
+ }
942
+ return BROTLI_TRUE;
943
+ }
944
+
945
+ static BROTLI_BOOL NextFile(Context* context) {
946
+ const char* arg;
947
+ size_t arg_len;
948
+
949
+ /* Iterator points to last used arg; increment to search for the next one. */
950
+ context->iterator++;
951
+
952
+ context->input_file_length = -1;
953
+
954
+ /* No input path; read from console. */
955
+ if (context->input_count == 0) {
956
+ if (context->iterator > 1) return BROTLI_FALSE;
957
+ context->current_input_path = NULL;
958
+ /* Either write to the specified path, or to console. */
959
+ context->current_output_path = context->output_path;
960
+ return BROTLI_TRUE;
961
+ }
962
+
963
+ /* Skip option arguments. */
964
+ while (context->iterator == context->not_input_indices[context->ignore]) {
965
+ context->iterator++;
966
+ context->ignore++;
967
+ }
968
+
969
+ /* All args are scanned already. */
970
+ if (context->iterator >= context->argc) return BROTLI_FALSE;
971
+
972
+ /* Iterator now points to the input file name. */
973
+ arg = context->argv[context->iterator];
974
+ arg_len = strlen(arg);
975
+ /* Read from console. */
976
+ if (arg_len == 1 && arg[0] == '-') {
977
+ context->current_input_path = NULL;
978
+ context->current_output_path = context->output_path;
979
+ return BROTLI_TRUE;
980
+ }
981
+
982
+ context->current_input_path = arg;
983
+ context->input_file_length = FileSize(arg);
984
+ context->current_output_path = context->output_path;
985
+
986
+ if (context->output_path) return BROTLI_TRUE;
987
+ if (context->write_to_stdout) return BROTLI_TRUE;
988
+
989
+ strcpy(context->modified_path, arg);
990
+ context->current_output_path = context->modified_path;
991
+ /* If output is not specified, input path suffix should match. */
992
+ if (context->decompress) {
993
+ size_t suffix_len = strlen(context->suffix);
994
+ char* name = (char*)FileName(context->modified_path);
995
+ char* name_suffix;
996
+ size_t name_len = strlen(name);
997
+ if (name_len < suffix_len + 1) {
998
+ fprintf(stderr, "empty output file name for [%s] input file\n",
999
+ PrintablePath(arg));
1000
+ context->iterator_error = BROTLI_TRUE;
1001
+ return BROTLI_FALSE;
1002
+ }
1003
+ name_suffix = name + name_len - suffix_len;
1004
+ if (strcmp(context->suffix, name_suffix) != 0) {
1005
+ fprintf(stderr, "input file [%s] suffix mismatch\n",
1006
+ PrintablePath(arg));
1007
+ context->iterator_error = BROTLI_TRUE;
1008
+ return BROTLI_FALSE;
1009
+ }
1010
+ name_suffix[0] = 0;
1011
+ return BROTLI_TRUE;
1012
+ } else {
1013
+ strcpy(context->modified_path + arg_len, context->suffix);
1014
+ return BROTLI_TRUE;
1015
+ }
1016
+ }
1017
+
1018
+ static BROTLI_BOOL OpenFiles(Context* context) {
1019
+ BROTLI_BOOL is_ok = OpenInputFile(context->current_input_path, &context->fin);
1020
+ if (!context->test_integrity && is_ok) {
1021
+ is_ok = OpenOutputFile(
1022
+ context->current_output_path, &context->fout, context->force_overwrite);
1023
+ }
1024
+ return is_ok;
1025
+ }
1026
+
1027
+ static BROTLI_BOOL CloseFiles(Context* context, BROTLI_BOOL rm_input,
1028
+ BROTLI_BOOL rm_output) {
1029
+ BROTLI_BOOL is_ok = BROTLI_TRUE;
1030
+ if (!context->test_integrity && context->fout) {
1031
+ if (fclose(context->fout) != 0) {
1032
+ if (is_ok) {
1033
+ fprintf(stderr, "fclose failed [%s]: %s\n",
1034
+ PrintablePath(context->current_output_path), strerror(errno));
1035
+ }
1036
+ is_ok = BROTLI_FALSE;
1037
+ }
1038
+ if (rm_output && context->current_output_path) {
1039
+ unlink(context->current_output_path);
1040
+ }
1041
+
1042
+ /* TOCTOU violation, but otherwise it is impossible to set file times. */
1043
+ if (!rm_output && is_ok && context->copy_stat) {
1044
+ CopyStat(context->current_input_path, context->current_output_path);
1045
+ }
1046
+ }
1047
+
1048
+ if (context->fin) {
1049
+ if (fclose(context->fin) != 0) {
1050
+ if (is_ok) {
1051
+ fprintf(stderr, "fclose failed [%s]: %s\n",
1052
+ PrintablePath(context->current_input_path), strerror(errno));
1053
+ }
1054
+ is_ok = BROTLI_FALSE;
1055
+ }
1056
+ }
1057
+ if (rm_input && context->current_input_path) {
1058
+ unlink(context->current_input_path);
1059
+ }
1060
+
1061
+ context->fin = NULL;
1062
+ context->fout = NULL;
1063
+
1064
+ return is_ok;
1065
+ }
1066
+
1067
+ static const size_t kFileBufferSize = 1 << 19;
1068
+
1069
+ static void InitializeBuffers(Context* context) {
1070
+ context->available_in = 0;
1071
+ context->next_in = NULL;
1072
+ context->available_out = kFileBufferSize;
1073
+ context->next_out = context->output;
1074
+ context->total_in = 0;
1075
+ context->total_out = 0;
1076
+ if (context->verbosity > 0) {
1077
+ context->start_time = clock();
1078
+ }
1079
+ }
1080
+
1081
+ /* This method might give the false-negative result.
1082
+ However, after an empty / incomplete read it should tell the truth. */
1083
+ static BROTLI_BOOL HasMoreInput(Context* context) {
1084
+ return feof(context->fin) ? BROTLI_FALSE : BROTLI_TRUE;
1085
+ }
1086
+
1087
+ static BROTLI_BOOL ProvideInput(Context* context) {
1088
+ context->available_in =
1089
+ fread(context->input, 1, kFileBufferSize, context->fin);
1090
+ context->total_in += context->available_in;
1091
+ context->next_in = context->input;
1092
+ if (ferror(context->fin)) {
1093
+ fprintf(stderr, "failed to read input [%s]: %s\n",
1094
+ PrintablePath(context->current_input_path), strerror(errno));
1095
+ return BROTLI_FALSE;
1096
+ }
1097
+ return BROTLI_TRUE;
1098
+ }
1099
+
1100
+ /* Internal: should be used only in Provide-/Flush-Output. */
1101
+ static BROTLI_BOOL WriteOutput(Context* context) {
1102
+ size_t out_size = (size_t)(context->next_out - context->output);
1103
+ context->total_out += out_size;
1104
+ if (out_size == 0) return BROTLI_TRUE;
1105
+ if (context->test_integrity) return BROTLI_TRUE;
1106
+
1107
+ fwrite(context->output, 1, out_size, context->fout);
1108
+ if (ferror(context->fout)) {
1109
+ fprintf(stderr, "failed to write output [%s]: %s\n",
1110
+ PrintablePath(context->current_output_path), strerror(errno));
1111
+ return BROTLI_FALSE;
1112
+ }
1113
+ return BROTLI_TRUE;
1114
+ }
1115
+
1116
+ static BROTLI_BOOL ProvideOutput(Context* context) {
1117
+ if (!WriteOutput(context)) return BROTLI_FALSE;
1118
+ context->available_out = kFileBufferSize;
1119
+ context->next_out = context->output;
1120
+ return BROTLI_TRUE;
1121
+ }
1122
+
1123
+ static BROTLI_BOOL FlushOutput(Context* context) {
1124
+ if (!WriteOutput(context)) return BROTLI_FALSE;
1125
+ context->available_out = 0;
1126
+ context->next_out = context->output;
1127
+ return BROTLI_TRUE;
1128
+ }
1129
+
1130
+ static void PrintBytes(size_t value) {
1131
+ if (value < 1024) {
1132
+ fprintf(stderr, "%d B", (int)value);
1133
+ } else if (value < 1048576) {
1134
+ fprintf(stderr, "%0.3f KiB", (double)value / 1024.0);
1135
+ } else if (value < 1073741824) {
1136
+ fprintf(stderr, "%0.3f MiB", (double)value / 1048576.0);
1137
+ } else {
1138
+ fprintf(stderr, "%0.3f GiB", (double)value / 1073741824.0);
1139
+ }
1140
+ }
1141
+
1142
+ static void PrintFileProcessingProgress(Context* context) {
1143
+ fprintf(stderr, "[%s]: ", PrintablePath(context->current_input_path));
1144
+ PrintBytes(context->total_in);
1145
+ fprintf(stderr, " -> ");
1146
+ PrintBytes(context->total_out);
1147
+ fprintf(stderr, " in %1.2f sec", (double)(context->end_time - context->start_time) / CLOCKS_PER_SEC);
1148
+ }
1149
+
1150
+ static const char* PrettyDecoderErrorString(BrotliDecoderErrorCode code) {
1151
+ /* "_ERROR_domain_" is in only added in newer versions. If CLI is linked
1152
+ against older shared library, return error string as is; result might be
1153
+ a bit confusing, e.g. "RESERVED" instead of "FORMAT_RESERVED" */
1154
+ const char* prefix = "_ERROR_";
1155
+ size_t prefix_len = strlen(prefix);
1156
+ const char* error_str = BrotliDecoderErrorString(code);
1157
+ size_t error_len = strlen(error_str);
1158
+ if (error_len > prefix_len) {
1159
+ if (strncmp(error_str, prefix, prefix_len) == 0) {
1160
+ error_str += prefix_len;
1161
+ }
1162
+ }
1163
+ return error_str;
1164
+ }
1165
+
1166
+ static void OnMetadataStart(void* opaque, size_t size) {
1167
+ Context* context = (Context*) opaque;
1168
+ if (context->comment_state == COMMENT_INIT) {
1169
+ if (context->comment_len != size) {
1170
+ context->comment_state = COMMENT_BAD;
1171
+ return;
1172
+ }
1173
+ context->comment_pos = 0;
1174
+ context->comment_state = COMMENT_READ;
1175
+ }
1176
+ }
1177
+
1178
+ static void OnMetadataChunk(void* opaque, const uint8_t* data, size_t size) {
1179
+ Context* context = (Context*) opaque;
1180
+ if (context->comment_state == COMMENT_READ) {
1181
+ size_t i;
1182
+ for (i = 0; i < size; ++i) {
1183
+ if (context->comment_pos >= context->comment_len) {
1184
+ context->comment_state = COMMENT_BAD;
1185
+ return;
1186
+ }
1187
+ if (context->comment[context->comment_pos++] != data[i]) {
1188
+ context->comment_state = COMMENT_BAD;
1189
+ return;
1190
+ }
1191
+ }
1192
+ if (context->comment_pos == context->comment_len) {
1193
+ context->comment_state = COMMENT_OK;
1194
+ }
1195
+ }
1196
+ }
1197
+
1198
+ static BROTLI_BOOL InitDecoder(Context* context) {
1199
+ context->decoder = BrotliDecoderCreateInstance(NULL, NULL, NULL);
1200
+ if (!context->decoder) {
1201
+ fprintf(stderr, "out of memory\n");
1202
+ return BROTLI_FALSE;
1203
+ }
1204
+ /* This allows decoding "large-window" streams. Though it creates
1205
+ fragmentation (new builds decode streams that old builds don't),
1206
+ it is better from used experience perspective. */
1207
+ BrotliDecoderSetParameter(
1208
+ context->decoder, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1u);
1209
+ if (context->dictionary) {
1210
+ BrotliDecoderAttachDictionary(context->decoder,
1211
+ BROTLI_SHARED_DICTIONARY_RAW, context->dictionary_size,
1212
+ context->dictionary);
1213
+ }
1214
+ return BROTLI_TRUE;
1215
+ }
1216
+
1217
+ static BROTLI_BOOL DecompressFile(Context* context) {
1218
+ BrotliDecoderState* s = context->decoder;
1219
+ BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
1220
+ if (context->comment_len) {
1221
+ context->comment_state = COMMENT_INIT;
1222
+ BrotliDecoderSetMetadataCallbacks(s, &OnMetadataStart, &OnMetadataChunk,
1223
+ (void*)context);
1224
+ } else {
1225
+ context->comment_state = COMMENT_OK;
1226
+ }
1227
+
1228
+ InitializeBuffers(context);
1229
+ for (;;) {
1230
+ /* Early check */
1231
+ if (context->comment_state == COMMENT_BAD) {
1232
+ fprintf(stderr, "corrupt input [%s]\n",
1233
+ PrintablePath(context->current_input_path));
1234
+ if (context->verbosity > 0) {
1235
+ fprintf(stderr, "reason: comment mismatch\n");
1236
+ }
1237
+ return BROTLI_FALSE;
1238
+ }
1239
+ if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
1240
+ if (!HasMoreInput(context)) {
1241
+ fprintf(stderr, "corrupt input [%s]\n",
1242
+ PrintablePath(context->current_input_path));
1243
+ if (context->verbosity > 0) {
1244
+ fprintf(stderr, "reason: truncated input\n");
1245
+ }
1246
+ return BROTLI_FALSE;
1247
+ }
1248
+ if (!ProvideInput(context)) return BROTLI_FALSE;
1249
+ } else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
1250
+ if (!ProvideOutput(context)) return BROTLI_FALSE;
1251
+ } else if (result == BROTLI_DECODER_RESULT_SUCCESS) {
1252
+ if (!FlushOutput(context)) return BROTLI_FALSE;
1253
+ BROTLI_BOOL has_more_input = (context->available_in != 0);
1254
+ int extra_char = EOF;
1255
+ if (!has_more_input) {
1256
+ extra_char = fgetc(context->fin);
1257
+ if (extra_char != EOF) {
1258
+ has_more_input = BROTLI_TRUE;
1259
+ context->input[0] = (uint8_t)extra_char;
1260
+ context->next_in = context->input;
1261
+ context->available_in = 1;
1262
+ }
1263
+ }
1264
+ if (has_more_input) {
1265
+ if (context->allow_concatenated) {
1266
+ if (context->verbosity > 0) {
1267
+ fprintf(stderr, "extra input\n");
1268
+ }
1269
+ if (!ProvideOutput(context)) return BROTLI_FALSE;
1270
+ BrotliDecoderDestroyInstance(context->decoder);
1271
+ context->decoder = NULL;
1272
+ if (!InitDecoder(context)) return BROTLI_FALSE;
1273
+ s = context->decoder;
1274
+ } else {
1275
+ fprintf(stderr, "corrupt input [%s]\n",
1276
+ PrintablePath(context->current_input_path));
1277
+ if (context->verbosity > 0) {
1278
+ fprintf(stderr, "reason: extra input\n");
1279
+ }
1280
+ return BROTLI_FALSE;
1281
+ }
1282
+ } else {
1283
+ if (context->verbosity > 0) {
1284
+ context->end_time = clock();
1285
+ fprintf(stderr, "Decompressed ");
1286
+ PrintFileProcessingProgress(context);
1287
+ fprintf(stderr, "\n");
1288
+ }
1289
+ /* Final check */
1290
+ if (context->comment_state != COMMENT_OK) {
1291
+ fprintf(stderr, "corrupt input [%s]\n",
1292
+ PrintablePath(context->current_input_path));
1293
+ if (context->verbosity > 0) {
1294
+ fprintf(stderr, "reason: comment mismatch\n");
1295
+ }
1296
+ }
1297
+ return BROTLI_TRUE;
1298
+ }
1299
+ } else { /* result == BROTLI_DECODER_RESULT_ERROR */
1300
+ fprintf(stderr, "corrupt input [%s]\n",
1301
+ PrintablePath(context->current_input_path));
1302
+ if (context->verbosity > 0) {
1303
+ BrotliDecoderErrorCode error = BrotliDecoderGetErrorCode(s);
1304
+ const char* error_str = PrettyDecoderErrorString(error);
1305
+ fprintf(stderr, "reason: %s (%d)\n", error_str, error);
1306
+ }
1307
+ return BROTLI_FALSE;
1308
+ }
1309
+
1310
+ result = BrotliDecoderDecompressStream(s, &context->available_in,
1311
+ &context->next_in, &context->available_out, &context->next_out, 0);
1312
+ }
1313
+ }
1314
+
1315
+ static BROTLI_BOOL DecompressFiles(Context* context) {
1316
+ while (NextFile(context)) {
1317
+ BROTLI_BOOL is_ok = BROTLI_TRUE;
1318
+ BROTLI_BOOL rm_input = BROTLI_FALSE;
1319
+ BROTLI_BOOL rm_output = BROTLI_TRUE;
1320
+ if (!InitDecoder(context)) return BROTLI_FALSE;
1321
+ is_ok = OpenFiles(context);
1322
+ if (is_ok && !context->current_input_path &&
1323
+ !context->force_overwrite && isatty(STDIN_FILENO)) {
1324
+ fprintf(stderr, "Use -h help. Use -f to force input from a terminal.\n");
1325
+ is_ok = BROTLI_FALSE;
1326
+ }
1327
+ if (is_ok) is_ok = DecompressFile(context);
1328
+ if (context->decoder) BrotliDecoderDestroyInstance(context->decoder);
1329
+ context->decoder = NULL;
1330
+ rm_output = !is_ok;
1331
+ rm_input = !rm_output && context->junk_source;
1332
+ if (!CloseFiles(context, rm_input, rm_output)) is_ok = BROTLI_FALSE;
1333
+ if (!is_ok) return BROTLI_FALSE;
1334
+ }
1335
+ return BROTLI_TRUE;
1336
+ }
1337
+
1338
+ static BROTLI_BOOL CompressFile(Context* context, BrotliEncoderState* s) {
1339
+ BROTLI_BOOL is_eof = BROTLI_FALSE;
1340
+ BROTLI_BOOL prologue = !!context->comment_len;
1341
+ InitializeBuffers(context);
1342
+ for (;;) {
1343
+ if (context->available_in == 0 && !is_eof) {
1344
+ if (!ProvideInput(context)) return BROTLI_FALSE;
1345
+ is_eof = !HasMoreInput(context);
1346
+ }
1347
+
1348
+ if (prologue) {
1349
+ prologue = BROTLI_FALSE;
1350
+ const uint8_t* next_meta = context->comment;
1351
+ size_t available_meta = context->comment_len;
1352
+ if (!BrotliEncoderCompressStream(s,
1353
+ BROTLI_OPERATION_EMIT_METADATA,
1354
+ &available_meta, &next_meta,
1355
+ &context->available_out, &context->next_out, NULL)) {
1356
+ /* Should detect OOM? */
1357
+ fprintf(stderr, "failed to emit metadata [%s]\n",
1358
+ PrintablePath(context->current_input_path));
1359
+ return BROTLI_FALSE;
1360
+ }
1361
+ if (available_meta != 0) {
1362
+ fprintf(stderr, "failed to emit metadata [%s]\n",
1363
+ PrintablePath(context->current_input_path));
1364
+ return BROTLI_FALSE;
1365
+ }
1366
+ } else {
1367
+ if (!BrotliEncoderCompressStream(s,
1368
+ is_eof ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS,
1369
+ &context->available_in, &context->next_in,
1370
+ &context->available_out, &context->next_out, NULL)) {
1371
+ /* Should detect OOM? */
1372
+ fprintf(stderr, "failed to compress data [%s]\n",
1373
+ PrintablePath(context->current_input_path));
1374
+ return BROTLI_FALSE;
1375
+ }
1376
+ }
1377
+
1378
+ if (context->available_out == 0) {
1379
+ if (!ProvideOutput(context)) return BROTLI_FALSE;
1380
+ }
1381
+
1382
+ if (BrotliEncoderIsFinished(s)) {
1383
+ if (!FlushOutput(context)) return BROTLI_FALSE;
1384
+ if (context->verbosity > 0) {
1385
+ context->end_time = clock();
1386
+ fprintf(stderr, "Compressed ");
1387
+ PrintFileProcessingProgress(context);
1388
+ fprintf(stderr, "\n");
1389
+ }
1390
+ return BROTLI_TRUE;
1391
+ }
1392
+ }
1393
+ }
1394
+
1395
+ static BROTLI_BOOL CompressFiles(Context* context) {
1396
+ while (NextFile(context)) {
1397
+ BROTLI_BOOL is_ok = BROTLI_TRUE;
1398
+ BROTLI_BOOL rm_input = BROTLI_FALSE;
1399
+ BROTLI_BOOL rm_output = BROTLI_TRUE;
1400
+ BrotliEncoderState* s = BrotliEncoderCreateInstance(NULL, NULL, NULL);
1401
+ if (!s) {
1402
+ fprintf(stderr, "out of memory\n");
1403
+ return BROTLI_FALSE;
1404
+ }
1405
+ BrotliEncoderSetParameter(s,
1406
+ BROTLI_PARAM_QUALITY, (uint32_t)context->quality);
1407
+ if (context->lgwin > 0) {
1408
+ /* Specified by user. */
1409
+ /* Do not enable "large-window" extension, if not required. */
1410
+ if (context->lgwin > BROTLI_MAX_WINDOW_BITS) {
1411
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LARGE_WINDOW, 1u);
1412
+ }
1413
+ BrotliEncoderSetParameter(s,
1414
+ BROTLI_PARAM_LGWIN, (uint32_t)context->lgwin);
1415
+ } else {
1416
+ /* 0, or not specified by user; could be chosen by compressor. */
1417
+ uint32_t lgwin = DEFAULT_LGWIN;
1418
+ /* Use file size to limit lgwin. */
1419
+ if (context->input_file_length >= 0) {
1420
+ lgwin = BROTLI_MIN_WINDOW_BITS;
1421
+ while (BROTLI_MAX_BACKWARD_LIMIT(lgwin) <
1422
+ (uint64_t)context->input_file_length) {
1423
+ lgwin++;
1424
+ if (lgwin == BROTLI_MAX_WINDOW_BITS) break;
1425
+ }
1426
+ }
1427
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, lgwin);
1428
+ }
1429
+ if (context->input_file_length > 0) {
1430
+ uint32_t size_hint = context->input_file_length < (1 << 30) ?
1431
+ (uint32_t)context->input_file_length : (1u << 30);
1432
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_SIZE_HINT, size_hint);
1433
+ }
1434
+ if (context->dictionary) {
1435
+ BrotliEncoderAttachPreparedDictionary(s, context->prepared_dictionary);
1436
+ }
1437
+ is_ok = OpenFiles(context);
1438
+ if (is_ok && !context->current_output_path &&
1439
+ !context->force_overwrite && isatty(STDOUT_FILENO)) {
1440
+ fprintf(stderr, "Use -h help. Use -f to force output to a terminal.\n");
1441
+ is_ok = BROTLI_FALSE;
1442
+ }
1443
+ if (is_ok) is_ok = CompressFile(context, s);
1444
+ BrotliEncoderDestroyInstance(s);
1445
+ rm_output = !is_ok;
1446
+ if (is_ok && context->reject_uncompressible) {
1447
+ if (context->total_out >= context->total_in) {
1448
+ rm_output = BROTLI_TRUE;
1449
+ if (context->verbosity > 0) {
1450
+ fprintf(stderr, "Output is larger than input\n");
1451
+ }
1452
+ }
1453
+ }
1454
+ rm_input = !rm_output && context->junk_source;
1455
+ if (!CloseFiles(context, rm_input, rm_output)) is_ok = BROTLI_FALSE;
1456
+ if (!is_ok) return BROTLI_FALSE;
1457
+ }
1458
+ return BROTLI_TRUE;
1459
+ }
1460
+
1461
+ int main(int argc, char** argv) {
1462
+ Command command;
1463
+ Context context;
1464
+ BROTLI_BOOL is_ok = BROTLI_TRUE;
1465
+ int i;
1466
+
1467
+ context.quality = 11;
1468
+ context.lgwin = -1;
1469
+ context.verbosity = 0;
1470
+ context.comment_len = 0;
1471
+ context.force_overwrite = BROTLI_FALSE;
1472
+ context.junk_source = BROTLI_FALSE;
1473
+ context.reject_uncompressible = BROTLI_FALSE;
1474
+ context.copy_stat = BROTLI_TRUE;
1475
+ context.test_integrity = BROTLI_FALSE;
1476
+ context.write_to_stdout = BROTLI_FALSE;
1477
+ context.decompress = BROTLI_FALSE;
1478
+ context.large_window = BROTLI_FALSE;
1479
+ context.allow_concatenated = BROTLI_FALSE;
1480
+ context.output_path = NULL;
1481
+ context.dictionary_path = NULL;
1482
+ context.suffix = DEFAULT_SUFFIX;
1483
+ for (i = 0; i < MAX_OPTIONS; ++i) context.not_input_indices[i] = 0;
1484
+ context.longest_path_len = 1;
1485
+ context.input_count = 0;
1486
+
1487
+ context.argc = argc;
1488
+ context.argv = argv;
1489
+ context.dictionary = NULL;
1490
+ context.dictionary_size = 0;
1491
+ context.decoder = NULL;
1492
+ context.prepared_dictionary = NULL;
1493
+ context.modified_path = NULL;
1494
+ context.iterator = 0;
1495
+ context.ignore = 0;
1496
+ context.iterator_error = BROTLI_FALSE;
1497
+ context.buffer = NULL;
1498
+ context.current_input_path = NULL;
1499
+ context.current_output_path = NULL;
1500
+ context.fin = NULL;
1501
+ context.fout = NULL;
1502
+
1503
+ command = ParseParams(&context);
1504
+
1505
+ if (command == COMMAND_COMPRESS || command == COMMAND_DECOMPRESS ||
1506
+ command == COMMAND_TEST_INTEGRITY) {
1507
+ if (!ReadDictionary(&context, command)) is_ok = BROTLI_FALSE;
1508
+ if (is_ok) {
1509
+ size_t modified_path_len =
1510
+ context.longest_path_len + strlen(context.suffix) + 1;
1511
+ context.modified_path = (char*)malloc(modified_path_len);
1512
+ context.buffer = (uint8_t*)malloc(kFileBufferSize * 2);
1513
+ if (!context.modified_path || !context.buffer) {
1514
+ fprintf(stderr, "out of memory\n");
1515
+ is_ok = BROTLI_FALSE;
1516
+ } else {
1517
+ context.input = context.buffer;
1518
+ context.output = context.buffer + kFileBufferSize;
1519
+ }
1520
+ }
1521
+ }
1522
+
1523
+ if (!is_ok) command = COMMAND_NOOP;
1524
+
1525
+ switch (command) {
1526
+ case COMMAND_NOOP:
1527
+ break;
1528
+
1529
+ case COMMAND_VERSION:
1530
+ PrintVersion();
1531
+ break;
1532
+
1533
+ case COMMAND_COMPRESS:
1534
+ is_ok = CompressFiles(&context);
1535
+ break;
1536
+
1537
+ case COMMAND_DECOMPRESS:
1538
+ case COMMAND_TEST_INTEGRITY:
1539
+ is_ok = DecompressFiles(&context);
1540
+ break;
1541
+
1542
+ case COMMAND_HELP:
1543
+ case COMMAND_INVALID:
1544
+ default:
1545
+ is_ok = (command == COMMAND_HELP);
1546
+ PrintHelp(FileName(argv[0]), is_ok);
1547
+ break;
1548
+ }
1549
+
1550
+ if (context.iterator_error) is_ok = BROTLI_FALSE;
1551
+
1552
+ BrotliEncoderDestroyPreparedDictionary(context.prepared_dictionary);
1553
+ free(context.dictionary);
1554
+ free(context.modified_path);
1555
+ free(context.buffer);
1556
+
1557
+ if (!is_ok) exit(1);
1558
+ return 0;
1559
+ }