koffi 2.12.1 → 2.12.3

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 (186) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE.txt +2 -2
  3. package/README.md +8 -8
  4. package/build/koffi/darwin_arm64/koffi.node +0 -0
  5. package/build/koffi/darwin_x64/koffi.node +0 -0
  6. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  7. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  8. package/build/koffi/freebsd_x64/koffi.node +0 -0
  9. package/build/koffi/linux_arm64/koffi.node +0 -0
  10. package/build/koffi/linux_armhf/koffi.node +0 -0
  11. package/build/koffi/linux_ia32/koffi.node +0 -0
  12. package/build/koffi/linux_loong64/koffi.node +0 -0
  13. package/build/koffi/linux_riscv64d/koffi.node +0 -0
  14. package/build/koffi/linux_x64/koffi.node +0 -0
  15. package/build/koffi/musl_arm64/koffi.node +0 -0
  16. package/build/koffi/musl_x64/koffi.node +0 -0
  17. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  18. package/build/koffi/openbsd_x64/koffi.node +0 -0
  19. package/build/koffi/win32_arm64/koffi.exp +0 -0
  20. package/build/koffi/win32_arm64/koffi.node +0 -0
  21. package/build/koffi/win32_ia32/koffi.exp +0 -0
  22. package/build/koffi/win32_ia32/koffi.node +0 -0
  23. package/build/koffi/win32_x64/koffi.exp +0 -0
  24. package/build/koffi/win32_x64/koffi.node +0 -0
  25. package/doc/assets.ini +3 -3
  26. package/doc/develop.sh +4 -3
  27. package/doc/pages/platforms.md +1 -1
  28. package/doc/static/highlight.js +1 -1
  29. package/doc/static/koffi.css +3 -2
  30. package/doc/static/print.css +1 -1
  31. package/doc/templates/code.html +5 -5
  32. package/doc/templates/page.html +4 -4
  33. package/index.d.ts +228 -145
  34. package/index.js +25 -25
  35. package/indirect.js +25 -5
  36. package/package.json +2 -2
  37. package/src/cnoke/LICENSE.txt +2 -2
  38. package/src/cnoke/README.md +2 -0
  39. package/src/cnoke/assets/FindCNoke.cmake +12 -2
  40. package/src/cnoke/assets/win_delay_hook.c +4 -4
  41. package/src/cnoke/cnoke.js +4 -2
  42. package/src/cnoke/package.json +1 -1
  43. package/src/cnoke/src/builder.js +11 -14
  44. package/src/cnoke/src/index.js +2 -2
  45. package/src/cnoke/src/tools.js +34 -2
  46. package/src/core/{libcc/libcc.cc → base/base.cc} +3174 -1640
  47. package/src/core/{libcc/libcc.hh → base/base.hh} +3066 -2427
  48. package/src/core/{libcc → base}/mimetypes.inc +42 -4
  49. package/src/core/{libcc → base}/mimetypes_gen.py +3 -3
  50. package/src/core/base/unicode.inc +426 -0
  51. package/src/core/base/unicode_gen.py +189 -0
  52. package/src/koffi/CMakeLists.txt +7 -5
  53. package/src/koffi/cmake/raylib.cmake +6 -2
  54. package/src/koffi/cmake/{sqlite.cmake → sqlite3.cmake} +3 -3
  55. package/src/koffi/examples/electron-forge/forge.config.js +23 -3
  56. package/src/koffi/examples/electron-forge/package.json +18 -16
  57. package/src/koffi/src/abi_arm32.cc +7 -7
  58. package/src/koffi/src/abi_arm32_asm.S +2 -2
  59. package/src/koffi/src/abi_arm64.cc +45 -45
  60. package/src/koffi/src/abi_arm64_asm.S +4 -4
  61. package/src/koffi/src/abi_arm64_asm.asm +2 -2
  62. package/src/koffi/src/abi_loong64_asm.S +2 -2
  63. package/src/koffi/src/abi_riscv64.cc +5 -5
  64. package/src/koffi/src/abi_riscv64_asm.S +2 -2
  65. package/src/koffi/src/abi_x64_sysv.cc +3 -3
  66. package/src/koffi/src/abi_x64_sysv_asm.S +5 -5
  67. package/src/koffi/src/abi_x64_win.cc +3 -3
  68. package/src/koffi/src/abi_x64_win_asm.asm +2 -2
  69. package/src/koffi/src/abi_x86.cc +8 -8
  70. package/src/koffi/src/abi_x86_asm.S +2 -2
  71. package/src/koffi/src/abi_x86_asm.asm +2 -2
  72. package/src/koffi/src/call.cc +12 -12
  73. package/src/koffi/src/call.hh +8 -8
  74. package/src/koffi/src/errno.inc +152 -152
  75. package/src/koffi/src/ffi.cc +44 -39
  76. package/src/koffi/src/ffi.hh +6 -6
  77. package/src/koffi/src/init.js +1 -1
  78. package/src/koffi/src/parser.cc +5 -5
  79. package/src/koffi/src/parser.hh +3 -3
  80. package/src/koffi/src/trampolines/armasm.inc +2 -2
  81. package/src/koffi/src/trampolines/gnu.inc +2 -2
  82. package/src/koffi/src/trampolines/masm32.inc +2 -2
  83. package/src/koffi/src/trampolines/masm64.inc +2 -2
  84. package/src/koffi/src/trampolines/prototypes.inc +2 -2
  85. package/src/koffi/src/util.cc +9 -9
  86. package/src/koffi/src/util.hh +3 -3
  87. package/src/koffi/src/win32.cc +5 -5
  88. package/src/koffi/src/win32.hh +9 -9
  89. package/vendor/node-addon-api/CHANGELOG.md +97 -2
  90. package/vendor/node-addon-api/CONTRIBUTING.md +117 -7
  91. package/vendor/node-addon-api/README.md +25 -249
  92. package/vendor/node-addon-api/common.gypi +1 -0
  93. package/vendor/node-addon-api/doc/README.md +145 -0
  94. package/vendor/node-addon-api/doc/array_buffer.md +15 -15
  95. package/vendor/node-addon-api/doc/basic_env.md +200 -0
  96. package/vendor/node-addon-api/doc/buffer.md +24 -26
  97. package/vendor/node-addon-api/doc/cmake-js.md +19 -0
  98. package/vendor/node-addon-api/doc/{creating_a_release.md → contributing/creating_a_release.md} +16 -4
  99. package/vendor/node-addon-api/doc/env.md +11 -131
  100. package/vendor/node-addon-api/doc/error_handling.md +12 -0
  101. package/vendor/node-addon-api/doc/external.md +13 -4
  102. package/vendor/node-addon-api/doc/finalization.md +153 -0
  103. package/vendor/node-addon-api/doc/memory_management.md +1 -1
  104. package/vendor/node-addon-api/doc/object_wrap.md +19 -3
  105. package/vendor/node-addon-api/doc/promises.md +51 -0
  106. package/vendor/node-addon-api/doc/setup.md +29 -6
  107. package/vendor/node-addon-api/doc/value.md +13 -0
  108. package/vendor/node-addon-api/doc/version_management.md +2 -2
  109. package/vendor/node-addon-api/eslint.config.js +5 -0
  110. package/vendor/node-addon-api/index.js +2 -0
  111. package/vendor/node-addon-api/napi-inl.h +592 -166
  112. package/vendor/node-addon-api/napi.h +167 -59
  113. package/vendor/node-addon-api/node_addon_api.gyp +10 -0
  114. package/vendor/node-addon-api/noexcept.gypi +1 -1
  115. package/vendor/node-addon-api/package.json +10 -13
  116. package/vendor/node-addon-api/release-please-config.json +15 -0
  117. package/vendor/node-addon-api/test/addon_build/tpl/binding.gyp +2 -1
  118. package/vendor/node-addon-api/test/array_buffer.js +1 -1
  119. package/vendor/node-addon-api/test/async_context.js +2 -2
  120. package/vendor/node-addon-api/test/async_progress_queue_worker.js +3 -3
  121. package/vendor/node-addon-api/test/async_progress_worker.js +3 -3
  122. package/vendor/node-addon-api/test/async_worker.cc +15 -13
  123. package/vendor/node-addon-api/test/async_worker.js +5 -5
  124. package/vendor/node-addon-api/test/basic_types/value.cc +6 -0
  125. package/vendor/node-addon-api/test/basic_types/value.js +17 -0
  126. package/vendor/node-addon-api/test/binding.cc +8 -0
  127. package/vendor/node-addon-api/test/binding.gyp +23 -1
  128. package/vendor/node-addon-api/test/buffer.js +1 -2
  129. package/vendor/node-addon-api/test/common/index.js +1 -1
  130. package/vendor/node-addon-api/test/except_all.cc +22 -0
  131. package/vendor/node-addon-api/test/except_all.js +14 -0
  132. package/vendor/node-addon-api/test/exports.js +19 -0
  133. package/vendor/node-addon-api/test/finalizer_order.cc +152 -0
  134. package/vendor/node-addon-api/test/finalizer_order.js +98 -0
  135. package/vendor/node-addon-api/test/function.js +2 -2
  136. package/vendor/node-addon-api/test/function_reference.js +2 -2
  137. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.cc +4 -4
  138. package/vendor/node-addon-api/test/globalObject/global_object_get_property.cc +4 -4
  139. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.cc +3 -3
  140. package/vendor/node-addon-api/test/globalObject/global_object_set_property.cc +5 -5
  141. package/vendor/node-addon-api/test/name.cc +10 -8
  142. package/vendor/node-addon-api/test/object/delete_property.cc +5 -5
  143. package/vendor/node-addon-api/test/object/get_property.cc +5 -5
  144. package/vendor/node-addon-api/test/object/has_own_property.cc +4 -4
  145. package/vendor/node-addon-api/test/object/has_property.cc +5 -5
  146. package/vendor/node-addon-api/test/object/object.cc +1 -1
  147. package/vendor/node-addon-api/test/object/set_property.cc +5 -5
  148. package/vendor/node-addon-api/test/object/subscript_operator.cc +3 -3
  149. package/vendor/node-addon-api/test/object_reference.cc +18 -18
  150. package/vendor/node-addon-api/test/promise.cc +75 -0
  151. package/vendor/node-addon-api/test/promise.js +23 -0
  152. package/vendor/node-addon-api/test/require_basic_finalizers/index.js +38 -0
  153. package/vendor/node-addon-api/test/require_basic_finalizers/tpl/addon.cc +12 -0
  154. package/vendor/node-addon-api/test/require_basic_finalizers/tpl/binding.gyp +48 -0
  155. package/vendor/node-addon-api/test/require_basic_finalizers/tpl/index.js +3 -0
  156. package/vendor/node-addon-api/test/require_basic_finalizers/tpl/package.json +11 -0
  157. package/vendor/node-addon-api/test/run_script.cc +1 -1
  158. package/vendor/node-addon-api/test/type_taggable.cc +1 -1
  159. package/vendor/node-addon-api/test/type_taggable.js +3 -4
  160. package/vendor/node-addon-api/test/typedarray.cc +28 -24
  161. package/vendor/node-addon-api/tools/conversion.js +1 -1
  162. package/vendor/node-api-headers/CHANGELOG.md +29 -0
  163. package/vendor/node-api-headers/CREATING_A_RELEASE.md +12 -2
  164. package/vendor/node-api-headers/def/js_native_api.def +6 -1
  165. package/vendor/node-api-headers/def/node_api.def +7 -1
  166. package/vendor/node-api-headers/include/js_native_api.h +56 -24
  167. package/vendor/node-api-headers/include/js_native_api_types.h +27 -0
  168. package/vendor/node-api-headers/include/node_api.h +38 -29
  169. package/vendor/node-api-headers/lib/parse-utils.js +92 -0
  170. package/vendor/node-api-headers/package.json +7 -7
  171. package/vendor/node-api-headers/release-please-config.json +12 -0
  172. package/vendor/node-api-headers/scripts/update-headers.js +63 -12
  173. package/vendor/node-api-headers/scripts/write-symbols.js +1 -1
  174. package/vendor/node-api-headers/symbols.js +17 -1
  175. package/vendor/node-api-headers/test/parse-utils.js +21 -0
  176. package/doc/flat/flat.css +0 -27
  177. package/doc/flat/normal.css +0 -429
  178. package/doc/flat/print.css +0 -29
  179. package/doc/flat/reset.css +0 -41
  180. package/doc/flat/small.css +0 -104
  181. package/doc/flat/static.js +0 -161
  182. package/src/core/libcc/brotli.cc +0 -186
  183. package/src/core/libcc/lz4.cc +0 -197
  184. package/src/core/libcc/miniz.cc +0 -353
  185. package/vendor/node-addon-api/tools/eslint-format.js +0 -79
  186. /package/vendor/node-api-headers/{scripts → lib}/clang-utils.js +0 -0
@@ -1,161 +0,0 @@
1
- // Copyright (C) 2024 Niels Martignène <niels.martignene@protonmail.com>
2
- //
3
- // This program is free software: you can redistribute it and/or modify
4
- // it under the terms of the GNU General Public License as published by
5
- // the Free Software Foundation, either version 3 of the License, or
6
- // (at your option) any later version.
7
- //
8
- // This program is distributed in the hope that it will be useful,
9
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
- // GNU General Public License for more details.
12
- //
13
- // You should have received a copy of the GNU General Public License
14
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
15
-
16
- window.addEventListener('load', e => {
17
- initDeploy();
18
- initMenu();
19
- initSide();
20
- initScroll();
21
- });
22
-
23
- function initDeploy() {
24
- let el = document.querySelector('#deploy');
25
-
26
- if (el != null)
27
- el.addEventListener('click', deploy);
28
- }
29
-
30
- function deploy() {
31
- let top = document.querySelector('nav#top');
32
- top.classList.toggle('active');
33
- }
34
-
35
- function initMenu() {
36
- let items = document.querySelectorAll('nav#top li');
37
-
38
- document.body.addEventListener('click', e => {
39
- if (e.target.tagName == 'DIV' && findParent(e.target, el => el.id == 'top'))
40
- return;
41
-
42
- // Expand top menu categories
43
- {
44
- let target = findParent(e.target, el => el.tagName == 'LI');
45
-
46
- if (target?.querySelector('div') != null) {
47
- for (let item of items) {
48
- if (item == target) {
49
- item.classList.toggle('active');
50
- } else {
51
- item.classList.remove('active');
52
- }
53
- }
54
- }
55
- }
56
-
57
- if (e.target.tagName == 'A' && e.target.getAttribute('href') == '#')
58
- e.preventDefault();
59
- });
60
- }
61
-
62
- function initSide() {
63
- // Find all side menu items, and the pointed to elements
64
- let links = [].slice.call(document.querySelectorAll('nav#side a'));
65
- let items = [].slice.call(links).map(link => {
66
- let anchor = document.querySelector(link.getAttribute('href'));
67
- return [link, anchor];
68
- });
69
- if (!items.length)
70
- return;
71
-
72
- let pending_request = false;
73
- let active_idx = null;
74
- function highlight(idx) {
75
- if (pending_request)
76
- return;
77
-
78
- window.requestAnimationFrame(() => {
79
- pending_request = false;
80
-
81
- if (idx !== active_idx) {
82
- if (active_idx !== null)
83
- items[active_idx][0].classList.remove('active');
84
- items[idx][0].classList.add('active');
85
- active_idx = idx;
86
- }
87
- });
88
- pending_request = true;
89
- }
90
-
91
- // Event handlers, for clicks
92
- let ignore_scroll = false;
93
- function highlightOnClick(idx) {
94
- highlight(idx);
95
- setTimeout(() => { ignore_scroll = false; }, 50);
96
- ignore_scroll = true;
97
- }
98
- function highlightOnScroll() {
99
- if (ignore_scroll)
100
- return;
101
-
102
- let style = window.getComputedStyle(document.documentElement);
103
- let treshold = parseFloat(style.getPropertyValue('scroll-padding-top')) + 10;
104
-
105
- let idx;
106
- for (idx = 0; idx < items.length; idx++) {
107
- let rect = items[idx][1].getBoundingClientRect();
108
- if (rect.top >= treshold)
109
- break;
110
- }
111
- if (idx)
112
- idx--;
113
-
114
- highlight(idx);
115
- }
116
-
117
- // The scroll handler is not enough because it does not work for
118
- // small sections at the end of the page.
119
- for (let i = 0; i < items.length; i++) {
120
- items[i][0].addEventListener('click', highlightOnClick.bind(this, i));
121
- }
122
-
123
- // Enable only on big screens, when the side menu is fixed
124
- let mql = window.matchMedia('(min-width: 801px)');
125
- if (mql.matches) {
126
- window.addEventListener('scroll', highlightOnScroll);
127
- highlightOnScroll();
128
- }
129
- mql.addListener(mql => {
130
- if (mql.matches) {
131
- window.addEventListener('scroll', highlightOnScroll);
132
- highlightOnScroll();
133
- } else {
134
- window.removeEventListener('scroll', highlightOnScroll);
135
- }
136
- });
137
- }
138
-
139
- function initScroll() {
140
- window.addEventListener('scroll', adjust_top);
141
- adjust_top();
142
-
143
- function adjust_top() {
144
- let top = document.querySelector('nav#top');
145
-
146
- if (top != null)
147
- top.classList.toggle('border', window.pageYOffset >= 20);
148
- }
149
- }
150
-
151
- function findParent(el, func) {
152
- while (el && !func(el))
153
- el = el.parentElement;
154
- return el;
155
- }
156
-
157
-
158
- document.documentElement.classList.remove('nojs');
159
- document.documentElement.classList.add('js');
160
-
161
- export { deploy }
@@ -1,186 +0,0 @@
1
- // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
-
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- // this software and associated documentation files (the “Software”), to deal in
5
- // the Software without restriction, including without limitation the rights to use,
6
- // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
- // Software, and to permit persons to whom the Software is furnished to do so,
8
- // subject to the following conditions:
9
-
10
- // The above copyright notice and this permission notice shall be included in all
11
- // copies or substantial portions of the Software.
12
-
13
- // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
- // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
- // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
- // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
- // OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- #include "libcc.hh"
23
-
24
- #include "vendor/brotli/c/include/brotli/decode.h"
25
- #include "vendor/brotli/c/include/brotli/encode.h"
26
-
27
- namespace RG {
28
-
29
- class BrotliDecompressor: public StreamDecoder {
30
- BrotliDecoderState *state = nullptr;
31
- bool done = false;
32
-
33
- uint8_t in_buf[256 * 1024];
34
- Size in_len = 0;
35
-
36
- uint8_t out_buf[256 * 1024];
37
- Size out_len = 0;
38
-
39
- public:
40
- BrotliDecompressor(StreamReader *reader, CompressionType type);
41
- ~BrotliDecompressor();
42
-
43
- Size Read(Size max_len, void *out_buf) override;
44
- };
45
-
46
- BrotliDecompressor::BrotliDecompressor(StreamReader *reader, CompressionType)
47
- : StreamDecoder(reader)
48
- {
49
- state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
50
- if (!state)
51
- throw std::bad_alloc();
52
- }
53
-
54
- BrotliDecompressor::~BrotliDecompressor()
55
- {
56
- BrotliDecoderDestroyInstance(state);
57
- }
58
-
59
- Size BrotliDecompressor::Read(Size max_len, void *user_buf)
60
- {
61
- for (;;) {
62
- if (out_len || done) {
63
- Size copy_len = std::min(max_len, out_len);
64
-
65
- out_len -= copy_len;
66
- memcpy(user_buf, out_buf, copy_len);
67
- memmove(out_buf, out_buf + copy_len, out_len);
68
-
69
- SetEOF(!out_len && done);
70
- return copy_len;
71
- }
72
-
73
- if (in_len < RG_SIZE(in_buf)) {
74
- Size raw_len = ReadRaw(RG_SIZE(in_buf) - in_len, in_buf + in_len);
75
- if (raw_len < 0)
76
- return -1;
77
- in_len += raw_len;
78
- }
79
-
80
- const uint8_t *next_in = in_buf;
81
- uint8_t *next_out = out_buf + out_len;
82
- size_t avail_in = (size_t)in_len;
83
- size_t avail_out = (size_t)(RG_SIZE(out_buf) - out_len);
84
-
85
- BrotliDecoderResult ret = BrotliDecoderDecompressStream(state, &avail_in, &next_in,
86
- &avail_out, &next_out, nullptr);
87
-
88
- if (ret == BROTLI_DECODER_RESULT_SUCCESS) {
89
- done = true;
90
- } else if (ret == BROTLI_DECODER_RESULT_ERROR) {
91
- LogError("Malformed Brotli stream in '%1'", GetFileName());
92
- return -1;
93
- }
94
-
95
- memmove_safe(in_buf, next_in, (size_t)avail_in);
96
- in_len = avail_in;
97
-
98
- out_len = next_out - out_buf - out_len;
99
- }
100
-
101
- RG_UNREACHABLE();
102
- }
103
-
104
- class BrotliCompressor: public StreamEncoder {
105
- BrotliEncoderStateStruct *state = nullptr;
106
-
107
- public:
108
- BrotliCompressor(StreamWriter *writer, CompressionType type, CompressionSpeed speed);
109
- ~BrotliCompressor();
110
-
111
- bool Write(Span<const uint8_t> buf) override;
112
- bool Finalize() override;
113
- };
114
-
115
- BrotliCompressor::BrotliCompressor(StreamWriter *writer, CompressionType, CompressionSpeed speed)
116
- : StreamEncoder(writer)
117
- {
118
- state = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
119
- if (!state)
120
- throw std::bad_alloc();
121
-
122
- static_assert(BROTLI_MIN_QUALITY == 0 && BROTLI_MAX_QUALITY == 11);
123
-
124
- switch (speed) {
125
- case CompressionSpeed::Default: { BrotliEncoderSetParameter(state, BROTLI_PARAM_QUALITY, 6); } break;
126
- case CompressionSpeed::Slow: { BrotliEncoderSetParameter(state, BROTLI_PARAM_QUALITY, 11); } break;
127
- case CompressionSpeed::Fast: { BrotliEncoderSetParameter(state, BROTLI_PARAM_QUALITY, 0); } break;
128
- }
129
- }
130
-
131
- BrotliCompressor::~BrotliCompressor()
132
- {
133
- BrotliEncoderDestroyInstance(state);
134
- }
135
-
136
- bool BrotliCompressor::Write(Span<const uint8_t> buf)
137
- {
138
- uint8_t output_buf[2048];
139
-
140
- while (buf.len || BrotliEncoderHasMoreOutput(state)) {
141
- const uint8_t *next_in = buf.ptr;
142
- uint8_t *next_out = output_buf;
143
- size_t avail_in = (size_t)buf.len;
144
- size_t avail_out = RG_SIZE(output_buf);
145
-
146
- if (!BrotliEncoderCompressStream(state, BROTLI_OPERATION_PROCESS,
147
- &avail_in, &next_in, &avail_out, &next_out, nullptr)) {
148
- LogError("Failed to compress '%1' with Brotli", GetFileName());
149
- return false;
150
- }
151
- if (!WriteRaw(MakeSpan(output_buf, next_out - output_buf)))
152
- return false;
153
-
154
- buf.len -= next_in - buf.ptr;
155
- buf.ptr = next_in;
156
- }
157
-
158
- return true;
159
- }
160
-
161
- bool BrotliCompressor::Finalize()
162
- {
163
- uint8_t output_buf[2048];
164
-
165
- do {
166
- const uint8_t *next_in = nullptr;
167
- uint8_t *next_out = output_buf;
168
- size_t avail_in = 0;
169
- size_t avail_out = RG_SIZE(output_buf);
170
-
171
- if (!BrotliEncoderCompressStream(state, BROTLI_OPERATION_FINISH,
172
- &avail_in, &next_in, &avail_out, &next_out, nullptr)) {
173
- LogError("Failed to compress '%1' with Brotli", GetFileName());
174
- return false;
175
- }
176
- if (!WriteRaw(MakeSpan(output_buf, next_out - output_buf)))
177
- return false;
178
- } while (BrotliEncoderHasMoreOutput(state));
179
-
180
- return true;
181
- }
182
-
183
- RG_REGISTER_DECOMPRESSOR(CompressionType::Brotli, BrotliDecompressor);
184
- RG_REGISTER_COMPRESSOR(CompressionType::Brotli, BrotliCompressor);
185
-
186
- }
@@ -1,197 +0,0 @@
1
- // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
-
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- // this software and associated documentation files (the “Software”), to deal in
5
- // the Software without restriction, including without limitation the rights to use,
6
- // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
- // Software, and to permit persons to whom the Software is furnished to do so,
8
- // subject to the following conditions:
9
-
10
- // The above copyright notice and this permission notice shall be included in all
11
- // copies or substantial portions of the Software.
12
-
13
- // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
- // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
- // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
- // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
- // OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- #include "libcc.hh"
23
-
24
- #include "vendor/lz4/lib/lz4.h"
25
- #include "vendor/lz4/lib/lz4hc.h"
26
- #include "vendor/lz4/lib/lz4frame.h"
27
-
28
- namespace RG {
29
-
30
- class LZ4Decompressor: public StreamDecoder {
31
- LZ4F_dctx *decoder = nullptr;
32
- bool done = false;
33
-
34
- uint8_t in_buf[256 * 1024];
35
- Size in_len = 0;
36
- Size in_hint = RG_SIZE(in_buf);
37
-
38
- uint8_t out_buf[256 * 1024];
39
- Size out_len = 0;
40
-
41
- public:
42
- LZ4Decompressor(StreamReader *reader, CompressionType type);
43
- ~LZ4Decompressor();
44
-
45
- Size Read(Size max_len, void *out_buf) override;
46
- };
47
-
48
- LZ4Decompressor::LZ4Decompressor(StreamReader *reader, CompressionType)
49
- : StreamDecoder(reader)
50
- {
51
- LZ4F_errorCode_t err = LZ4F_createDecompressionContext(&decoder, LZ4F_VERSION);
52
- if (LZ4F_isError(err))
53
- throw std::bad_alloc();
54
- }
55
-
56
- LZ4Decompressor::~LZ4Decompressor()
57
- {
58
- LZ4F_freeDecompressionContext(decoder);
59
- }
60
-
61
- Size LZ4Decompressor::Read(Size max_len, void *user_buf)
62
- {
63
- for (;;) {
64
- if (out_len || done) {
65
- Size copy_len = std::min(max_len, out_len);
66
-
67
- out_len -= copy_len;
68
- memcpy(user_buf, out_buf, copy_len);
69
- memmove(out_buf, out_buf + copy_len, out_len);
70
-
71
- SetEOF(!out_len && done);
72
- return copy_len;
73
- }
74
-
75
- if (in_len < in_hint) {
76
- Size raw_len = ReadRaw(in_hint - in_len, in_buf + in_len);
77
- if (raw_len < 0)
78
- return -1;
79
- in_len += raw_len;
80
- }
81
-
82
- const uint8_t *next_in = in_buf;
83
- uint8_t *next_out = out_buf + out_len;
84
- size_t avail_in = (size_t)in_len;
85
- size_t avail_out = (size_t)(RG_SIZE(out_buf) - out_len);
86
-
87
- LZ4F_decompressOptions_t opt = {};
88
- size_t ret = LZ4F_decompress(decoder, next_out, &avail_out, next_in, &avail_in, &opt);
89
-
90
- if (!ret) {
91
- done = true;
92
- } else if (LZ4F_isError(ret)) {
93
- LogError("Malformed LZ4 stream in '%1': %2", GetFileName(), LZ4F_getErrorName(ret));
94
- return -1;
95
- }
96
-
97
- memmove_safe(in_buf, in_buf + avail_in, (size_t)in_len - avail_in);
98
- in_len -= avail_in;
99
- in_hint = std::min(RG_SIZE(in_buf), (Size)ret);
100
-
101
- out_len += (Size)avail_out;
102
- }
103
-
104
- RG_UNREACHABLE();
105
- }
106
-
107
- class LZ4Compressor: public StreamEncoder {
108
- LZ4F_cctx *encoder = nullptr;
109
- LZ4F_preferences_t prefs = {};
110
-
111
- HeapArray<uint8_t> dynamic_buf;
112
-
113
- public:
114
- LZ4Compressor(StreamWriter *writer, CompressionType type, CompressionSpeed speed);
115
- ~LZ4Compressor();
116
-
117
- bool Write(Span<const uint8_t> buf) override;
118
- bool Finalize() override;
119
- };
120
-
121
- LZ4Compressor::LZ4Compressor(StreamWriter *writer, CompressionType, CompressionSpeed speed)
122
- : StreamEncoder(writer)
123
- {
124
- LZ4F_errorCode_t err = LZ4F_createCompressionContext(&encoder, LZ4F_VERSION);
125
- if (LZ4F_isError(err))
126
- throw std::bad_alloc();
127
-
128
- switch (speed) {
129
- case CompressionSpeed::Default: { prefs.compressionLevel = LZ4HC_CLEVEL_MIN; } break;
130
- case CompressionSpeed::Slow: { prefs.compressionLevel = LZ4HC_CLEVEL_MAX; } break;
131
- case CompressionSpeed::Fast: { prefs.compressionLevel = 0; } break;
132
- }
133
-
134
- dynamic_buf.Grow(LZ4F_HEADER_SIZE_MAX);
135
-
136
- size_t ret = LZ4F_compressBegin(encoder, dynamic_buf.end(), dynamic_buf.capacity - dynamic_buf.len, &prefs);
137
- if (LZ4F_isError(ret))
138
- throw std::bad_alloc();
139
-
140
- dynamic_buf.len += ret;
141
- }
142
-
143
- LZ4Compressor::~LZ4Compressor()
144
- {
145
- LZ4F_freeCompressionContext(encoder);
146
- }
147
-
148
- bool LZ4Compressor::Write(Span<const uint8_t> buf)
149
- {
150
- size_t needed = LZ4F_compressBound((size_t)buf.len, &prefs);
151
- dynamic_buf.Grow((Size)needed);
152
-
153
- size_t ret = LZ4F_compressUpdate(encoder, dynamic_buf.end(), (size_t)(dynamic_buf.capacity - dynamic_buf.len),
154
- buf.ptr, (size_t)buf.len, nullptr);
155
-
156
- if (LZ4F_isError(ret)) {
157
- LogError("Failed to write LZ4 stream for '%1': %2", GetFileName(), LZ4F_getErrorName(ret));
158
- return false;
159
- }
160
-
161
- dynamic_buf.len += (Size)ret;
162
-
163
- if (dynamic_buf.len >= 512) {
164
- if (!WriteRaw(dynamic_buf))
165
- return false;
166
- dynamic_buf.len = 0;
167
- }
168
-
169
- return true;
170
- }
171
-
172
- bool LZ4Compressor::Finalize()
173
- {
174
- size_t needed = LZ4F_compressBound(0, &prefs);
175
- dynamic_buf.Grow((Size)needed);
176
-
177
- size_t ret = LZ4F_compressEnd(encoder, dynamic_buf.end(),
178
- (size_t)(dynamic_buf.capacity - dynamic_buf.len), nullptr);
179
-
180
- if (LZ4F_isError(ret)) {
181
- LogError("Failed to finalize LZ4 stream for '%1': %2", GetFileName(), LZ4F_getErrorName(ret));
182
- return false;
183
- }
184
-
185
- dynamic_buf.len += (Size)ret;
186
-
187
- if (!WriteRaw(dynamic_buf))
188
- return false;
189
- dynamic_buf.len = 0;
190
-
191
- return true;
192
- }
193
-
194
- RG_REGISTER_DECOMPRESSOR(CompressionType::LZ4, LZ4Decompressor);
195
- RG_REGISTER_COMPRESSOR(CompressionType::LZ4, LZ4Compressor);
196
-
197
- }