bare-ico 1.0.1 → 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 CHANGED
@@ -10,6 +10,8 @@
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
17
  uint16_t reserved; // Must be 0
@@ -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
@@ -147,12 +158,15 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
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)
@@ -205,7 +219,14 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
205
219
  return NULL;
206
220
  }
207
221
 
208
- int row_size = (int) row_size_64;
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;
209
230
 
210
231
  // Allocate output RGBA buffer
211
232
  width = bmp_width;
@@ -225,8 +246,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
225
246
  if (bmp_bpp == 32 && bmp_compression == 0) {
226
247
  // 32-bit RGBA - direct copy with BGR→RGB conversion
227
248
  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);
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;
230
251
  for (int x = 0; x < width; x++) {
231
252
  dst[x * 4 + 0] = src[x * 4 + 2]; // R
232
253
  dst[x * 4 + 1] = src[x * 4 + 1]; // G
@@ -237,8 +258,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
237
258
  } else if (bmp_bpp == 24 && bmp_compression == 0) {
238
259
  // 24-bit RGB - convert to RGBA
239
260
  for (int y = 0; y < actual_height; y++) {
240
- uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
241
- 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;
242
263
  for (int x = 0; x < width; x++) {
243
264
  dst[x * 4 + 0] = src[x * 3 + 2]; // R
244
265
  dst[x * 4 + 1] = src[x * 3 + 1]; // G
@@ -250,8 +271,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
250
271
  // 8-bit indexed color - use palette
251
272
  uint8_t *palette = img_data + header_size;
252
273
  for (int y = 0; y < actual_height; y++) {
253
- uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
254
- 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;
255
276
  for (int x = 0; x < width; x++) {
256
277
  uint8_t index = src[x];
257
278
  dst[x * 4 + 0] = palette[index * 4 + 2]; // R
@@ -264,8 +285,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
264
285
  // 4-bit indexed color - use palette
265
286
  uint8_t *palette = img_data + header_size;
266
287
  for (int y = 0; y < actual_height; y++) {
267
- uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
268
- 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;
269
290
  for (int x = 0; x < width; x++) {
270
291
  uint8_t byte = src[x / 2];
271
292
  uint8_t index;
@@ -318,8 +339,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
318
339
 
319
340
  // Set data. Use size_t arithmetic to avoid int overflow on the PNG path
320
341
  // where width/height come from stb_image and are not pre-bounded here.
321
- if (width <= 0 || height <= 0) {
322
- stbi_image_free(rgba_data);
342
+ if (width <= 0 || height <= 0 || (uint64_t) width * height > BARE_ICO_MAX_PIXELS) {
343
+ bare_ico__free(rgba_data, from_stbi);
323
344
  err = js_throw_error(env, NULL, "Invalid decoded image dimensions");
324
345
  assert(err == 0);
325
346
  return NULL;
@@ -327,14 +348,14 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
327
348
  size_t data_len = (size_t) width * (size_t) height * 4;
328
349
  uint8_t *result_data = malloc(data_len);
329
350
  if (!result_data) {
330
- stbi_image_free(rgba_data);
351
+ bare_ico__free(rgba_data, from_stbi);
331
352
  err = js_throw_error(env, NULL, "Memory allocation failed");
332
353
  assert(err == 0);
333
354
  return NULL;
334
355
  }
335
356
 
336
357
  memcpy(result_data, rgba_data, data_len);
337
- stbi_image_free(rgba_data);
358
+ bare_ico__free(rgba_data, from_stbi);
338
359
 
339
360
  js_value_t *buffer;
340
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.1",
3
+ "version": "1.0.2",
4
4
  "description": "ICO support for Bare",
5
5
  "main": "index.js",
6
6
  "exports": {
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file