bare-ico 1.0.0 → 1.0.2
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.
- package/binding.c +74 -34
- package/package.json +5 -2
- package/prebuilds/android-arm/bare-ico.bare +0 -0
- package/prebuilds/android-arm64/bare-ico.bare +0 -0
- package/prebuilds/android-ia32/bare-ico.bare +0 -0
- package/prebuilds/android-x64/bare-ico.bare +0 -0
- package/prebuilds/darwin-arm64/bare-ico.bare +0 -0
- package/prebuilds/darwin-x64/bare-ico.bare +0 -0
- package/prebuilds/ios-arm64/bare-ico.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-ico.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-ico.bare +0 -0
- package/prebuilds/linux-arm64/bare-ico.bare +0 -0
- package/prebuilds/linux-x64/bare-ico.bare +0 -0
- package/prebuilds/win32-arm64/bare-ico.bare +0 -0
- package/prebuilds/win32-x64/bare-ico.bare +0 -0
package/binding.c
CHANGED
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
#define STBI_NO_STDIO
|
|
11
11
|
#include "stb_image.h"
|
|
12
12
|
|
|
13
|
+
#define BARE_ICO_MAX_PIXELS (1ull << 28)
|
|
14
|
+
|
|
13
15
|
// ICO file format structures
|
|
14
16
|
typedef struct __attribute__((packed)) {
|
|
15
|
-
uint16_t reserved;
|
|
16
|
-
uint16_t type;
|
|
17
|
-
uint16_t count;
|
|
17
|
+
uint16_t reserved; // Must be 0
|
|
18
|
+
uint16_t type; // 1 for ICO, 2 for CUR
|
|
19
|
+
uint16_t count; // Number of images
|
|
18
20
|
} IconDir;
|
|
19
21
|
|
|
20
22
|
typedef struct __attribute__((packed)) {
|
|
@@ -33,6 +35,15 @@ bare_ico__on_finalize(js_env_t *env, void *data, void *finalize_hint) {
|
|
|
33
35
|
free(data);
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
static void
|
|
39
|
+
bare_ico__free(uint8_t *data, bool from_stbi) {
|
|
40
|
+
if (from_stbi) {
|
|
41
|
+
stbi_image_free(data);
|
|
42
|
+
} else {
|
|
43
|
+
free(data);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
36
47
|
/**
|
|
37
48
|
* Decode ICO file to RGBA format
|
|
38
49
|
* Extracts largest image or closest to requested size
|
|
@@ -133,7 +144,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
133
144
|
uint32_t offset = entry->image_offset;
|
|
134
145
|
uint32_t size = entry->bytes_in_res;
|
|
135
146
|
|
|
136
|
-
|
|
147
|
+
// Check bounds without overflowing uint32_t
|
|
148
|
+
if (offset > ico_len || size > (uint32_t) (ico_len - offset)) {
|
|
137
149
|
err = js_throw_error(env, NULL, "Invalid ICO file: image data out of bounds");
|
|
138
150
|
assert(err == 0);
|
|
139
151
|
return NULL;
|
|
@@ -142,17 +154,19 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
142
154
|
uint8_t *img_data = ico_data + offset;
|
|
143
155
|
|
|
144
156
|
// Check if PNG (magic: 0x89 0x50 0x4E 0x47)
|
|
145
|
-
bool is_png = (size >= 8 && img_data[0] == 0x89 && img_data[1] == 0x50 &&
|
|
146
|
-
img_data[2] == 0x4E && img_data[3] == 0x47);
|
|
157
|
+
bool is_png = (size >= 8 && img_data[0] == 0x89 && img_data[1] == 0x50 && img_data[2] == 0x4E && img_data[3] == 0x47);
|
|
147
158
|
|
|
148
159
|
int width, height, channels;
|
|
149
160
|
uint8_t *rgba_data = NULL;
|
|
161
|
+
bool from_stbi = false;
|
|
150
162
|
|
|
151
163
|
if (is_png) {
|
|
152
164
|
// PNG format - can decode directly
|
|
153
165
|
rgba_data = stbi_load_from_memory(
|
|
154
166
|
img_data, size, &width, &height, &channels, 4
|
|
155
167
|
);
|
|
168
|
+
|
|
169
|
+
from_stbi = true;
|
|
156
170
|
} else {
|
|
157
171
|
// BMP-in-ICO format: has DIB header but no BITMAPFILEHEADER
|
|
158
172
|
// Height is doubled (includes AND mask)
|
|
@@ -177,8 +191,10 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
177
191
|
memcpy(&bmp_bpp, img_data + 14, 2);
|
|
178
192
|
memcpy(&bmp_compression, img_data + 16, 4);
|
|
179
193
|
|
|
180
|
-
// Validate
|
|
181
|
-
|
|
194
|
+
// Validate. Cap dimensions to keep all downstream size math within int32.
|
|
195
|
+
// 65535 is well above any realistic ICO image and prevents overflow in
|
|
196
|
+
// row_size / pixel_data_size / rgba_size calculations below.
|
|
197
|
+
if (header_size < 40 || bmp_width <= 0 || bmp_height <= 0 || bmp_width > 65535 || bmp_height > 131070) {
|
|
182
198
|
err = js_throw_error(env, NULL, "Invalid BMP-in-ICO header");
|
|
183
199
|
assert(err == 0);
|
|
184
200
|
return NULL;
|
|
@@ -187,34 +203,51 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
187
203
|
// ICO BMP height is doubled (image data + AND mask)
|
|
188
204
|
int32_t actual_height = bmp_height / 2;
|
|
189
205
|
|
|
190
|
-
//
|
|
191
|
-
|
|
192
|
-
|
|
206
|
+
// Compute sizes in 64-bit to detect any wrap before we trust them.
|
|
207
|
+
int64_t row_size_64 = (((int64_t) bmp_width * bmp_bpp + 31) / 32) * 4;
|
|
208
|
+
int64_t pixel_data_size_64 = row_size_64 * actual_height;
|
|
209
|
+
int64_t rgba_size_64 = (int64_t) bmp_width * actual_height * 4;
|
|
210
|
+
|
|
211
|
+
// Palette size (only present for <=8bpp)
|
|
212
|
+
int palette_size = (bmp_bpp <= 8) ? (1 << bmp_bpp) * 4 : 0;
|
|
213
|
+
|
|
214
|
+
// Verify palette + pixel data lie entirely within the input image.
|
|
215
|
+
if ((int64_t) header_size + palette_size > (int64_t) size || pixel_data_size_64 > (int64_t) size - header_size - palette_size) {
|
|
216
|
+
err = js_throw_error(env, NULL, "Invalid BMP-in-ICO: pixel data out of bounds");
|
|
217
|
+
assert(err == 0);
|
|
218
|
+
|
|
219
|
+
return NULL;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (rgba_size_64 > (int64_t) BARE_ICO_MAX_PIXELS * 4) {
|
|
223
|
+
err = js_throw_error(env, NULL, "BMP-in-ICO dimensions exceed maximum");
|
|
224
|
+
assert(err == 0);
|
|
225
|
+
|
|
226
|
+
return NULL;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
size_t row_size = (size_t) row_size_64;
|
|
193
230
|
|
|
194
231
|
// Allocate output RGBA buffer
|
|
195
232
|
width = bmp_width;
|
|
196
233
|
height = actual_height;
|
|
197
|
-
rgba_data = (uint8_t *)malloc(
|
|
234
|
+
rgba_data = (uint8_t *) malloc((size_t) rgba_size_64);
|
|
198
235
|
if (!rgba_data) {
|
|
199
236
|
err = js_throw_error(env, NULL, "Memory allocation failed");
|
|
200
237
|
assert(err == 0);
|
|
238
|
+
|
|
201
239
|
return NULL;
|
|
202
240
|
}
|
|
203
241
|
|
|
204
|
-
//
|
|
205
|
-
uint8_t *pixel_start = img_data + header_size;
|
|
206
|
-
if (bmp_bpp <= 8) {
|
|
207
|
-
// Has color palette
|
|
208
|
-
int palette_size = (1 << bmp_bpp) * 4;
|
|
209
|
-
pixel_start += palette_size;
|
|
210
|
-
}
|
|
242
|
+
// Pixel data starts after header and (optional) color palette
|
|
243
|
+
uint8_t *pixel_start = img_data + header_size + palette_size;
|
|
211
244
|
|
|
212
245
|
// Decode based on bit depth
|
|
213
246
|
if (bmp_bpp == 32 && bmp_compression == 0) {
|
|
214
247
|
// 32-bit RGBA - direct copy with BGR→RGB conversion
|
|
215
248
|
for (int y = 0; y < actual_height; y++) {
|
|
216
|
-
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size
|
|
217
|
-
uint8_t *dst = rgba_data + (y * width * 4
|
|
249
|
+
uint8_t *src = pixel_start + (size_t) (actual_height - 1 - y) * row_size;
|
|
250
|
+
uint8_t *dst = rgba_data + (size_t) y * width * 4;
|
|
218
251
|
for (int x = 0; x < width; x++) {
|
|
219
252
|
dst[x * 4 + 0] = src[x * 4 + 2]; // R
|
|
220
253
|
dst[x * 4 + 1] = src[x * 4 + 1]; // G
|
|
@@ -225,35 +258,35 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
225
258
|
} else if (bmp_bpp == 24 && bmp_compression == 0) {
|
|
226
259
|
// 24-bit RGB - convert to RGBA
|
|
227
260
|
for (int y = 0; y < actual_height; y++) {
|
|
228
|
-
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size
|
|
229
|
-
uint8_t *dst = rgba_data + (y * width * 4
|
|
261
|
+
uint8_t *src = pixel_start + (size_t) (actual_height - 1 - y) * row_size;
|
|
262
|
+
uint8_t *dst = rgba_data + (size_t) y * width * 4;
|
|
230
263
|
for (int x = 0; x < width; x++) {
|
|
231
264
|
dst[x * 4 + 0] = src[x * 3 + 2]; // R
|
|
232
265
|
dst[x * 4 + 1] = src[x * 3 + 1]; // G
|
|
233
266
|
dst[x * 4 + 2] = src[x * 3 + 0]; // B
|
|
234
|
-
dst[x * 4 + 3] = 255;
|
|
267
|
+
dst[x * 4 + 3] = 255; // A
|
|
235
268
|
}
|
|
236
269
|
}
|
|
237
270
|
} else if (bmp_bpp == 8 && bmp_compression == 0) {
|
|
238
271
|
// 8-bit indexed color - use palette
|
|
239
272
|
uint8_t *palette = img_data + header_size;
|
|
240
273
|
for (int y = 0; y < actual_height; y++) {
|
|
241
|
-
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size
|
|
242
|
-
uint8_t *dst = rgba_data + (y * width * 4
|
|
274
|
+
uint8_t *src = pixel_start + (size_t) (actual_height - 1 - y) * row_size;
|
|
275
|
+
uint8_t *dst = rgba_data + (size_t) y * width * 4;
|
|
243
276
|
for (int x = 0; x < width; x++) {
|
|
244
277
|
uint8_t index = src[x];
|
|
245
278
|
dst[x * 4 + 0] = palette[index * 4 + 2]; // R
|
|
246
279
|
dst[x * 4 + 1] = palette[index * 4 + 1]; // G
|
|
247
280
|
dst[x * 4 + 2] = palette[index * 4 + 0]; // B
|
|
248
|
-
dst[x * 4 + 3] = 255;
|
|
281
|
+
dst[x * 4 + 3] = 255; // A
|
|
249
282
|
}
|
|
250
283
|
}
|
|
251
284
|
} else if (bmp_bpp == 4 && bmp_compression == 0) {
|
|
252
285
|
// 4-bit indexed color - use palette
|
|
253
286
|
uint8_t *palette = img_data + header_size;
|
|
254
287
|
for (int y = 0; y < actual_height; y++) {
|
|
255
|
-
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size
|
|
256
|
-
uint8_t *dst = rgba_data + (y * width * 4
|
|
288
|
+
uint8_t *src = pixel_start + (size_t) (actual_height - 1 - y) * row_size;
|
|
289
|
+
uint8_t *dst = rgba_data + (size_t) y * width * 4;
|
|
257
290
|
for (int x = 0; x < width; x++) {
|
|
258
291
|
uint8_t byte = src[x / 2];
|
|
259
292
|
uint8_t index;
|
|
@@ -265,7 +298,7 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
265
298
|
dst[x * 4 + 0] = palette[index * 4 + 2]; // R
|
|
266
299
|
dst[x * 4 + 1] = palette[index * 4 + 1]; // G
|
|
267
300
|
dst[x * 4 + 2] = palette[index * 4 + 0]; // B
|
|
268
|
-
dst[x * 4 + 3] = 255;
|
|
301
|
+
dst[x * 4 + 3] = 255; // A (opaque)
|
|
269
302
|
}
|
|
270
303
|
}
|
|
271
304
|
} else {
|
|
@@ -304,18 +337,25 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
|
304
337
|
err = js_set_named_property(env, result, "height", height_val);
|
|
305
338
|
assert(err == 0);
|
|
306
339
|
|
|
307
|
-
// Set data
|
|
308
|
-
|
|
340
|
+
// Set data. Use size_t arithmetic to avoid int overflow on the PNG path
|
|
341
|
+
// where width/height come from stb_image and are not pre-bounded here.
|
|
342
|
+
if (width <= 0 || height <= 0 || (uint64_t) width * height > BARE_ICO_MAX_PIXELS) {
|
|
343
|
+
bare_ico__free(rgba_data, from_stbi);
|
|
344
|
+
err = js_throw_error(env, NULL, "Invalid decoded image dimensions");
|
|
345
|
+
assert(err == 0);
|
|
346
|
+
return NULL;
|
|
347
|
+
}
|
|
348
|
+
size_t data_len = (size_t) width * (size_t) height * 4;
|
|
309
349
|
uint8_t *result_data = malloc(data_len);
|
|
310
350
|
if (!result_data) {
|
|
311
|
-
|
|
351
|
+
bare_ico__free(rgba_data, from_stbi);
|
|
312
352
|
err = js_throw_error(env, NULL, "Memory allocation failed");
|
|
313
353
|
assert(err == 0);
|
|
314
354
|
return NULL;
|
|
315
355
|
}
|
|
316
356
|
|
|
317
357
|
memcpy(result_data, rgba_data, data_len);
|
|
318
|
-
|
|
358
|
+
bare_ico__free(rgba_data, from_stbi);
|
|
319
359
|
|
|
320
360
|
js_value_t *buffer;
|
|
321
361
|
err = js_create_external_arraybuffer(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-ico",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "ICO support for Bare",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"exports": {
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
],
|
|
17
17
|
"addon": true,
|
|
18
18
|
"scripts": {
|
|
19
|
-
"
|
|
19
|
+
"format": "prettier --write . && lunte --fix",
|
|
20
|
+
"lint": "prettier --check . && lunte",
|
|
21
|
+
"test": "brittle-bare --coverage test.js"
|
|
20
22
|
},
|
|
21
23
|
"repository": {
|
|
22
24
|
"type": "git",
|
|
@@ -32,6 +34,7 @@
|
|
|
32
34
|
"brittle": "^3.4.0",
|
|
33
35
|
"cmake-bare": "^1.1.2",
|
|
34
36
|
"cmake-fetch": "^1.0.0",
|
|
37
|
+
"lunte": "^1.8.0",
|
|
35
38
|
"prettier": "^3.3.3",
|
|
36
39
|
"prettier-config-holepunch": "^2.0.0"
|
|
37
40
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|