Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/libpng/pngset.c
Line
Count
Source
1
/* pngset.c - storage of image information into info struct
2
 *
3
 * Copyright (c) 2018-2026 Cosmin Truta
4
 * Copyright (c) 1998-2018 Glenn Randers-Pehrson
5
 * Copyright (c) 1996-1997 Andreas Dilger
6
 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
 *
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
10
 * and license in png.h
11
 *
12
 * The functions here are used during reads to store data from the file
13
 * into the info struct, and during writes to store application data
14
 * into the info struct for writing into the file.  This abstracts the
15
 * info struct and allows us to change the structure in the future.
16
 */
17
18
#include "pngpriv.h"
19
20
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
21
22
#ifdef PNG_bKGD_SUPPORTED
23
void PNGAPI
24
png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
25
    png_const_color_16p background)
26
240
{
27
240
   png_debug1(1, "in %s storage function", "bKGD");
28
29
240
   if (png_ptr == NULL || info_ptr == NULL || background == NULL)
30
0
      return;
31
32
240
   info_ptr->background = *background;
33
240
   info_ptr->valid |= PNG_INFO_bKGD;
34
240
}
35
#endif
36
37
#ifdef PNG_cHRM_SUPPORTED
38
void PNGFAPI
39
png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
40
    png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
41
    png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
42
    png_fixed_point blue_x, png_fixed_point blue_y)
43
573
{
44
573
   png_debug1(1, "in %s storage function", "cHRM fixed");
45
46
573
   if (png_ptr == NULL || info_ptr == NULL)
47
0
      return;
48
49
573
   info_ptr->cHRM.redx = red_x;
50
573
   info_ptr->cHRM.redy = red_y;
51
573
   info_ptr->cHRM.greenx = green_x;
52
573
   info_ptr->cHRM.greeny = green_y;
53
573
   info_ptr->cHRM.bluex = blue_x;
54
573
   info_ptr->cHRM.bluey = blue_y;
55
573
   info_ptr->cHRM.whitex = white_x;
56
573
   info_ptr->cHRM.whitey = white_y;
57
58
573
   info_ptr->valid |= PNG_INFO_cHRM;
59
573
}
60
61
void PNGFAPI
62
png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
63
    png_fixed_point int_red_X, png_fixed_point int_red_Y,
64
    png_fixed_point int_red_Z, png_fixed_point int_green_X,
65
    png_fixed_point int_green_Y, png_fixed_point int_green_Z,
66
    png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
67
    png_fixed_point int_blue_Z)
68
0
{
69
0
   png_XYZ XYZ;
70
0
   png_xy xy;
71
72
0
   png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
73
74
0
   if (png_ptr == NULL || info_ptr == NULL)
75
0
      return;
76
77
0
   XYZ.red_X = int_red_X;
78
0
   XYZ.red_Y = int_red_Y;
79
0
   XYZ.red_Z = int_red_Z;
80
0
   XYZ.green_X = int_green_X;
81
0
   XYZ.green_Y = int_green_Y;
82
0
   XYZ.green_Z = int_green_Z;
83
0
   XYZ.blue_X = int_blue_X;
84
0
   XYZ.blue_Y = int_blue_Y;
85
0
   XYZ.blue_Z = int_blue_Z;
86
87
0
   if (png_xy_from_XYZ(&xy, &XYZ) == 0)
88
0
   {
89
0
      info_ptr->cHRM = xy;
90
0
      info_ptr->valid |= PNG_INFO_cHRM;
91
0
   }
92
93
0
   else
94
0
      png_app_error(png_ptr, "invalid cHRM XYZ");
95
0
}
96
97
#  ifdef PNG_FLOATING_POINT_SUPPORTED
98
void PNGAPI
99
png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
100
    double white_x, double white_y, double red_x, double red_y,
101
    double green_x, double green_y, double blue_x, double blue_y)
102
0
{
103
0
   png_set_cHRM_fixed(png_ptr, info_ptr,
104
0
       png_fixed(png_ptr, white_x, "cHRM White X"),
105
0
       png_fixed(png_ptr, white_y, "cHRM White Y"),
106
0
       png_fixed(png_ptr, red_x, "cHRM Red X"),
107
0
       png_fixed(png_ptr, red_y, "cHRM Red Y"),
108
0
       png_fixed(png_ptr, green_x, "cHRM Green X"),
109
0
       png_fixed(png_ptr, green_y, "cHRM Green Y"),
110
0
       png_fixed(png_ptr, blue_x, "cHRM Blue X"),
111
0
       png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
112
0
}
113
114
void PNGAPI
115
png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
116
    double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
117
    double blue_X, double blue_Y, double blue_Z)
118
0
{
119
0
   png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
120
0
       png_fixed(png_ptr, red_X, "cHRM Red X"),
121
0
       png_fixed(png_ptr, red_Y, "cHRM Red Y"),
122
0
       png_fixed(png_ptr, red_Z, "cHRM Red Z"),
123
0
       png_fixed(png_ptr, green_X, "cHRM Green X"),
124
0
       png_fixed(png_ptr, green_Y, "cHRM Green Y"),
125
0
       png_fixed(png_ptr, green_Z, "cHRM Green Z"),
126
0
       png_fixed(png_ptr, blue_X, "cHRM Blue X"),
127
0
       png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
128
0
       png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
129
0
}
130
#  endif /* FLOATING_POINT */
131
132
#endif /* cHRM */
133
134
#ifdef PNG_cICP_SUPPORTED
135
void PNGAPI
136
png_set_cICP(png_const_structrp png_ptr, png_inforp info_ptr,
137
             png_byte colour_primaries, png_byte transfer_function,
138
             png_byte matrix_coefficients, png_byte video_full_range_flag)
139
{
140
   png_debug1(1, "in %s storage function", "cICP");
141
142
   if (png_ptr == NULL || info_ptr == NULL)
143
      return;
144
145
   info_ptr->cicp_colour_primaries = colour_primaries;
146
   info_ptr->cicp_transfer_function = transfer_function;
147
   info_ptr->cicp_matrix_coefficients = matrix_coefficients;
148
   info_ptr->cicp_video_full_range_flag = video_full_range_flag;
149
150
   if (info_ptr->cicp_matrix_coefficients != 0)
151
   {
152
      png_warning(png_ptr, "Invalid cICP matrix coefficients");
153
      return;
154
   }
155
156
   info_ptr->valid |= PNG_INFO_cICP;
157
}
158
#endif /* cICP */
159
160
#ifdef PNG_cLLI_SUPPORTED
161
void PNGFAPI
162
png_set_cLLI_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
163
    /* The values below are in cd/m2 (nits) and are scaled by 10,000; not
164
     * 100,000 as in the case of png_fixed_point.
165
     */
166
    png_uint_32 maxCLL, png_uint_32 maxFALL)
167
{
168
   png_debug1(1, "in %s storage function", "cLLI");
169
170
   if (png_ptr == NULL || info_ptr == NULL)
171
      return;
172
173
   /* Check the light level range: */
174
   if (maxCLL > 0x7FFFFFFFU || maxFALL > 0x7FFFFFFFU)
175
   {
176
      /* The limit is 200kcd/m2; somewhat bright but not inconceivable because
177
       * human vision is said to run up to 100Mcd/m2.  The sun is about 2Gcd/m2.
178
       *
179
       * The reference sRGB monitor is 80cd/m2 and the limit of PQ encoding is
180
       * 2kcd/m2.
181
       */
182
      png_chunk_report(png_ptr, "cLLI light level exceeds PNG limit",
183
            PNG_CHUNK_WRITE_ERROR);
184
      return;
185
   }
186
187
   info_ptr->maxCLL = maxCLL;
188
   info_ptr->maxFALL = maxFALL;
189
   info_ptr->valid |= PNG_INFO_cLLI;
190
}
191
192
#  ifdef PNG_FLOATING_POINT_SUPPORTED
193
void PNGAPI
194
png_set_cLLI(png_const_structrp png_ptr, png_inforp info_ptr,
195
   double maxCLL, double maxFALL)
196
{
197
   png_set_cLLI_fixed(png_ptr, info_ptr,
198
       png_fixed_ITU(png_ptr, maxCLL, "png_set_cLLI(maxCLL)"),
199
       png_fixed_ITU(png_ptr, maxFALL, "png_set_cLLI(maxFALL)"));
200
}
201
#  endif /* FLOATING_POINT */
202
#endif /* cLLI */
203
204
#ifdef PNG_mDCV_SUPPORTED
205
static png_uint_16
206
png_ITU_fixed_16(int *error, png_fixed_point v)
207
{
208
   /* Return a safe uint16_t value scaled according to the ITU H273 rules for
209
    * 16-bit display chromaticities.  Functions like the corresponding
210
    * png_fixed() internal function with regard to errors: it's an error on
211
    * write, a chunk_benign_error on read: See the definition of
212
    * png_chunk_report in pngpriv.h.
213
    */
214
   v /= 2; /* rounds to 0 in C: avoids insignificant arithmetic errors */
215
   if (v > 65535 || v < 0)
216
   {
217
      *error = 1;
218
      return 0;
219
   }
220
221
   return (png_uint_16)/*SAFE*/v;
222
}
223
224
void PNGAPI
225
png_set_mDCV_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
226
    png_fixed_point white_x, png_fixed_point white_y,
227
    png_fixed_point red_x, png_fixed_point red_y,
228
    png_fixed_point green_x, png_fixed_point green_y,
229
    png_fixed_point blue_x, png_fixed_point blue_y,
230
    png_uint_32 maxDL,
231
    png_uint_32 minDL)
232
{
233
   png_uint_16 rx, ry, gx, gy, bx, by, wx, wy;
234
   int error;
235
236
   png_debug1(1, "in %s storage function", "mDCV");
237
238
   if (png_ptr == NULL || info_ptr == NULL)
239
      return;
240
241
   /* Check the input values to ensure they are in the expected range: */
242
   error = 0;
243
   rx = png_ITU_fixed_16(&error, red_x);
244
   ry = png_ITU_fixed_16(&error, red_y);
245
   gx = png_ITU_fixed_16(&error, green_x);
246
   gy = png_ITU_fixed_16(&error, green_y);
247
   bx = png_ITU_fixed_16(&error, blue_x);
248
   by = png_ITU_fixed_16(&error, blue_y);
249
   wx = png_ITU_fixed_16(&error, white_x);
250
   wy = png_ITU_fixed_16(&error, white_y);
251
252
   if (error)
253
   {
254
      png_chunk_report(png_ptr,
255
         "mDCV chromaticities outside representable range",
256
         PNG_CHUNK_WRITE_ERROR);
257
      return;
258
   }
259
260
   /* Check the light level range: */
261
   if (maxDL > 0x7FFFFFFFU || minDL > 0x7FFFFFFFU)
262
   {
263
      /* The limit is 200kcd/m2; somewhat bright but not inconceivable because
264
       * human vision is said to run up to 100Mcd/m2.  The sun is about 2Gcd/m2.
265
       *
266
       * The reference sRGB monitor is 80cd/m2 and the limit of PQ encoding is
267
       * 2kcd/m2.
268
       */
269
      png_chunk_report(png_ptr, "mDCV display light level exceeds PNG limit",
270
            PNG_CHUNK_WRITE_ERROR);
271
      return;
272
   }
273
274
   /* All values are safe, the settings are accepted.
275
    *
276
    * IMPLEMENTATION NOTE: in practice the values can be checked and assigned
277
    * but the result is confusing if a writing app calls png_set_mDCV more than
278
    * once, the second time with an invalid value.  This approach is more
279
    * obviously correct at the cost of typing and a very slight machine
280
    * overhead.
281
    */
282
   info_ptr->mastering_red_x = rx;
283
   info_ptr->mastering_red_y = ry;
284
   info_ptr->mastering_green_x = gx;
285
   info_ptr->mastering_green_y = gy;
286
   info_ptr->mastering_blue_x = bx;
287
   info_ptr->mastering_blue_y = by;
288
   info_ptr->mastering_white_x = wx;
289
   info_ptr->mastering_white_y = wy;
290
   info_ptr->mastering_maxDL = maxDL;
291
   info_ptr->mastering_minDL = minDL;
292
   info_ptr->valid |= PNG_INFO_mDCV;
293
}
294
295
#  ifdef PNG_FLOATING_POINT_SUPPORTED
296
void PNGAPI
297
png_set_mDCV(png_const_structrp png_ptr, png_inforp info_ptr,
298
    double white_x, double white_y, double red_x, double red_y, double green_x,
299
    double green_y, double blue_x, double blue_y,
300
    double maxDL, double minDL)
301
{
302
   png_set_mDCV_fixed(png_ptr, info_ptr,
303
      png_fixed(png_ptr, white_x, "png_set_mDCV(white(x))"),
304
      png_fixed(png_ptr, white_y, "png_set_mDCV(white(y))"),
305
      png_fixed(png_ptr, red_x, "png_set_mDCV(red(x))"),
306
      png_fixed(png_ptr, red_y, "png_set_mDCV(red(y))"),
307
      png_fixed(png_ptr, green_x, "png_set_mDCV(green(x))"),
308
      png_fixed(png_ptr, green_y, "png_set_mDCV(green(y))"),
309
      png_fixed(png_ptr, blue_x, "png_set_mDCV(blue(x))"),
310
      png_fixed(png_ptr, blue_y, "png_set_mDCV(blue(y))"),
311
      png_fixed_ITU(png_ptr, maxDL, "png_set_mDCV(maxDL)"),
312
      png_fixed_ITU(png_ptr, minDL, "png_set_mDCV(minDL)"));
313
}
314
#  endif /* FLOATING_POINT */
315
#endif /* mDCV */
316
317
#ifdef PNG_eXIf_SUPPORTED
318
void PNGAPI
319
png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,
320
    png_bytep exif)
321
{
322
  png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");
323
  PNG_UNUSED(info_ptr)
324
  PNG_UNUSED(exif)
325
}
326
327
void PNGAPI
328
png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,
329
    png_uint_32 num_exif, png_bytep exif)
330
{
331
   png_bytep new_exif;
332
333
   png_debug1(1, "in %s storage function", "eXIf");
334
335
   if (png_ptr == NULL || info_ptr == NULL ||
336
       (png_ptr->mode & PNG_WROTE_eXIf) != 0 ||
337
       exif == NULL)
338
      return;
339
340
   new_exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr, num_exif));
341
342
   if (new_exif == NULL)
343
   {
344
      png_warning(png_ptr, "Insufficient memory for eXIf chunk data");
345
      return;
346
   }
347
348
   memcpy(new_exif, exif, (size_t)num_exif);
349
350
   png_free_data(png_ptr, info_ptr, PNG_FREE_EXIF, 0);
351
352
   info_ptr->num_exif = num_exif;
353
   info_ptr->exif = new_exif;
354
   info_ptr->free_me |= PNG_FREE_EXIF;
355
   info_ptr->valid |= PNG_INFO_eXIf;
356
}
357
#endif /* eXIf */
358
359
#ifdef PNG_gAMA_SUPPORTED
360
void PNGFAPI
361
png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
362
    png_fixed_point file_gamma)
363
517
{
364
517
   png_debug1(1, "in %s storage function", "gAMA");
365
366
517
   if (png_ptr == NULL || info_ptr == NULL)
367
0
      return;
368
369
517
   info_ptr->gamma = file_gamma;
370
517
   info_ptr->valid |= PNG_INFO_gAMA;
371
517
}
372
373
#  ifdef PNG_FLOATING_POINT_SUPPORTED
374
void PNGAPI
375
png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
376
0
{
377
0
   png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
378
0
       "png_set_gAMA"));
379
0
}
380
#  endif
381
#endif
382
383
#ifdef PNG_hIST_SUPPORTED
384
void PNGAPI
385
png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
386
    png_const_uint_16p hist)
387
4.32k
{
388
4.32k
   png_uint_16 safe_hist[PNG_MAX_PALETTE_LENGTH];
389
4.32k
   int i;
390
391
4.32k
   png_debug1(1, "in %s storage function", "hIST");
392
393
4.32k
   if (png_ptr == NULL || info_ptr == NULL || hist == NULL)
394
0
      return;
395
396
4.32k
   if (info_ptr->num_palette == 0 || info_ptr->num_palette
397
0
       > PNG_MAX_PALETTE_LENGTH)
398
4.32k
   {
399
4.32k
      png_warning(png_ptr,
400
4.32k
          "Invalid palette size, hIST allocation skipped");
401
402
4.32k
      return;
403
4.32k
   }
404
405
   /* Snapshot the caller's hist before freeing, in case it points to
406
    * info_ptr->hist (getter-to-setter aliasing).
407
    */
408
0
   memcpy(safe_hist, hist, (unsigned int)info_ptr->num_palette *
409
0
       (sizeof (png_uint_16)));
410
0
   hist = safe_hist;
411
412
0
   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
413
414
   /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
415
    * version 1.2.1
416
    */
417
0
   info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
418
0
       PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
419
420
0
   if (info_ptr->hist == NULL)
421
0
   {
422
0
      png_warning(png_ptr, "Insufficient memory for hIST chunk data");
423
0
      return;
424
0
   }
425
426
0
   for (i = 0; i < info_ptr->num_palette; i++)
427
0
      info_ptr->hist[i] = hist[i];
428
429
0
   info_ptr->free_me |= PNG_FREE_HIST;
430
0
   info_ptr->valid |= PNG_INFO_hIST;
431
0
}
432
#endif
433
434
void PNGAPI
435
png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
436
    png_uint_32 width, png_uint_32 height, int bit_depth,
437
    int color_type, int interlace_type, int compression_type,
438
    int filter_type)
439
19.0k
{
440
19.0k
   png_debug1(1, "in %s storage function", "IHDR");
441
442
19.0k
   if (png_ptr == NULL || info_ptr == NULL)
443
0
      return;
444
445
19.0k
   info_ptr->width = width;
446
19.0k
   info_ptr->height = height;
447
19.0k
   info_ptr->bit_depth = (png_byte)bit_depth;
448
19.0k
   info_ptr->color_type = (png_byte)color_type;
449
19.0k
   info_ptr->compression_type = (png_byte)compression_type;
450
19.0k
   info_ptr->filter_type = (png_byte)filter_type;
451
19.0k
   info_ptr->interlace_type = (png_byte)interlace_type;
452
453
19.0k
   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
454
19.0k
       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
455
19.0k
       info_ptr->compression_type, info_ptr->filter_type);
456
457
19.0k
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
458
1.30k
      info_ptr->channels = 1;
459
460
17.7k
   else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
461
3.23k
      info_ptr->channels = 3;
462
463
14.5k
   else
464
14.5k
      info_ptr->channels = 1;
465
466
19.0k
   if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
467
1.89k
      info_ptr->channels++;
468
469
19.0k
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
470
471
19.0k
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
472
19.0k
}
473
474
#ifdef PNG_oFFs_SUPPORTED
475
void PNGAPI
476
png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
477
    png_int_32 offset_x, png_int_32 offset_y, int unit_type)
478
1.99k
{
479
1.99k
   png_debug1(1, "in %s storage function", "oFFs");
480
481
1.99k
   if (png_ptr == NULL || info_ptr == NULL)
482
0
      return;
483
484
1.99k
   info_ptr->x_offset = offset_x;
485
1.99k
   info_ptr->y_offset = offset_y;
486
1.99k
   info_ptr->offset_unit_type = (png_byte)unit_type;
487
1.99k
   info_ptr->valid |= PNG_INFO_oFFs;
488
1.99k
}
489
#endif
490
491
#ifdef PNG_pCAL_SUPPORTED
492
void PNGAPI
493
png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
494
    png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
495
    int nparams, png_const_charp units, png_charpp params)
496
287
{
497
287
   size_t length;
498
287
   int i;
499
500
287
   png_debug1(1, "in %s storage function", "pCAL");
501
502
287
   if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
503
287
       || (nparams > 0 && params == NULL))
504
0
      return;
505
506
287
   length = strlen(purpose) + 1;
507
287
   png_debug1(3, "allocating purpose for info (%lu bytes)",
508
287
       (unsigned long)length);
509
510
   /* TODO: validate format of calibration name and unit name */
511
512
   /* Check that the type matches the specification. */
513
287
   if (type < 0 || type > 3)
514
98
   {
515
98
      png_chunk_report(png_ptr, "Invalid pCAL equation type",
516
98
            PNG_CHUNK_WRITE_ERROR);
517
98
      return;
518
98
   }
519
520
189
   if (nparams < 0 || nparams > 255)
521
0
   {
522
0
      png_chunk_report(png_ptr, "Invalid pCAL parameter count",
523
0
            PNG_CHUNK_WRITE_ERROR);
524
0
      return;
525
0
   }
526
527
   /* Validate params[nparams] */
528
240
   for (i=0; i<nparams; ++i)
529
236
   {
530
236
      if (params[i] == NULL ||
531
236
          !png_check_fp_string(params[i], strlen(params[i])))
532
185
      {
533
185
         png_chunk_report(png_ptr, "Invalid format for pCAL parameter",
534
185
               PNG_CHUNK_WRITE_ERROR);
535
185
         return;
536
185
      }
537
236
   }
538
539
4
   info_ptr->pcal_purpose = png_voidcast(png_charp,
540
4
       png_malloc_warn(png_ptr, length));
541
542
4
   if (info_ptr->pcal_purpose == NULL)
543
0
   {
544
0
      png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",
545
0
            PNG_CHUNK_WRITE_ERROR);
546
0
      return;
547
0
   }
548
549
4
   memcpy(info_ptr->pcal_purpose, purpose, length);
550
551
4
   info_ptr->free_me |= PNG_FREE_PCAL;
552
553
4
   png_debug(3, "storing X0, X1, type, and nparams in info");
554
4
   info_ptr->pcal_X0 = X0;
555
4
   info_ptr->pcal_X1 = X1;
556
4
   info_ptr->pcal_type = (png_byte)type;
557
4
   info_ptr->pcal_nparams = (png_byte)nparams;
558
559
4
   length = strlen(units) + 1;
560
4
   png_debug1(3, "allocating units for info (%lu bytes)",
561
4
       (unsigned long)length);
562
563
4
   info_ptr->pcal_units = png_voidcast(png_charp,
564
4
       png_malloc_warn(png_ptr, length));
565
566
4
   if (info_ptr->pcal_units == NULL)
567
0
   {
568
0
      png_warning(png_ptr, "Insufficient memory for pCAL units");
569
0
      return;
570
0
   }
571
572
4
   memcpy(info_ptr->pcal_units, units, length);
573
574
4
   info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
575
4
       (size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));
576
577
4
   if (info_ptr->pcal_params == NULL)
578
0
   {
579
0
      png_warning(png_ptr, "Insufficient memory for pCAL params");
580
0
      return;
581
0
   }
582
583
4
   memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *
584
4
       (sizeof (png_charp)));
585
586
15
   for (i = 0; i < nparams; i++)
587
11
   {
588
11
      length = strlen(params[i]) + 1;
589
11
      png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
590
11
          (unsigned long)length);
591
592
11
      info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
593
594
11
      if (info_ptr->pcal_params[i] == NULL)
595
0
      {
596
0
         png_warning(png_ptr, "Insufficient memory for pCAL parameter");
597
0
         return;
598
0
      }
599
600
11
      memcpy(info_ptr->pcal_params[i], params[i], length);
601
11
   }
602
603
4
   info_ptr->valid |= PNG_INFO_pCAL;
604
4
}
605
#endif
606
607
#ifdef PNG_sCAL_SUPPORTED
608
void PNGAPI
609
png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
610
    int unit, png_const_charp swidth, png_const_charp sheight)
611
471
{
612
471
   size_t lengthw = 0, lengthh = 0;
613
614
471
   png_debug1(1, "in %s storage function", "sCAL");
615
616
471
   if (png_ptr == NULL || info_ptr == NULL)
617
0
      return;
618
619
   /* Double check the unit (should never get here with an invalid
620
    * unit unless this is an API call.)
621
    */
622
471
   if (unit != 1 && unit != 2)
623
0
      png_error(png_ptr, "Invalid sCAL unit");
624
625
471
   if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
626
471
       swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
627
0
      png_error(png_ptr, "Invalid sCAL width");
628
629
471
   if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
630
471
       sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
631
0
      png_error(png_ptr, "Invalid sCAL height");
632
633
471
   info_ptr->scal_unit = (png_byte)unit;
634
635
471
   ++lengthw;
636
637
471
   png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
638
639
471
   info_ptr->scal_s_width = png_voidcast(png_charp,
640
471
       png_malloc_warn(png_ptr, lengthw));
641
642
471
   if (info_ptr->scal_s_width == NULL)
643
0
   {
644
0
      png_warning(png_ptr, "Memory allocation failed while processing sCAL");
645
646
0
      return;
647
0
   }
648
649
471
   memcpy(info_ptr->scal_s_width, swidth, lengthw);
650
651
471
   ++lengthh;
652
653
471
   png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
654
655
471
   info_ptr->scal_s_height = png_voidcast(png_charp,
656
471
       png_malloc_warn(png_ptr, lengthh));
657
658
471
   if (info_ptr->scal_s_height == NULL)
659
0
   {
660
0
      png_free(png_ptr, info_ptr->scal_s_width);
661
0
      info_ptr->scal_s_width = NULL;
662
663
0
      png_warning(png_ptr, "Memory allocation failed while processing sCAL");
664
0
      return;
665
0
   }
666
667
471
   memcpy(info_ptr->scal_s_height, sheight, lengthh);
668
669
471
   info_ptr->free_me |= PNG_FREE_SCAL;
670
471
   info_ptr->valid |= PNG_INFO_sCAL;
671
471
}
672
673
#  ifdef PNG_FLOATING_POINT_SUPPORTED
674
void PNGAPI
675
png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
676
    double width, double height)
677
0
{
678
0
   png_debug1(1, "in %s storage function", "sCAL");
679
680
   /* Check the arguments. */
681
0
   if (width <= 0)
682
0
      png_warning(png_ptr, "Invalid sCAL width ignored");
683
684
0
   else if (height <= 0)
685
0
      png_warning(png_ptr, "Invalid sCAL height ignored");
686
687
0
   else
688
0
   {
689
      /* Convert 'width' and 'height' to ASCII. */
690
0
      char swidth[PNG_sCAL_MAX_DIGITS+1];
691
0
      char sheight[PNG_sCAL_MAX_DIGITS+1];
692
693
0
      png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
694
0
          PNG_sCAL_PRECISION);
695
0
      png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
696
0
          PNG_sCAL_PRECISION);
697
698
0
      png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
699
0
   }
700
0
}
701
#  endif
702
703
#  ifdef PNG_FIXED_POINT_SUPPORTED
704
void PNGAPI
705
png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
706
    png_fixed_point width, png_fixed_point height)
707
0
{
708
0
   png_debug1(1, "in %s storage function", "sCAL");
709
710
   /* Check the arguments. */
711
0
   if (width <= 0)
712
0
      png_warning(png_ptr, "Invalid sCAL width ignored");
713
714
0
   else if (height <= 0)
715
0
      png_warning(png_ptr, "Invalid sCAL height ignored");
716
717
0
   else
718
0
   {
719
      /* Convert 'width' and 'height' to ASCII. */
720
0
      char swidth[PNG_sCAL_MAX_DIGITS+1];
721
0
      char sheight[PNG_sCAL_MAX_DIGITS+1];
722
723
0
      png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
724
0
      png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
725
726
0
      png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
727
0
   }
728
0
}
729
#  endif
730
#endif
731
732
#ifdef PNG_pHYs_SUPPORTED
733
void PNGAPI
734
png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
735
    png_uint_32 res_x, png_uint_32 res_y, int unit_type)
736
1.16k
{
737
1.16k
   png_debug1(1, "in %s storage function", "pHYs");
738
739
1.16k
   if (png_ptr == NULL || info_ptr == NULL)
740
0
      return;
741
742
1.16k
   info_ptr->x_pixels_per_unit = res_x;
743
1.16k
   info_ptr->y_pixels_per_unit = res_y;
744
1.16k
   info_ptr->phys_unit_type = (png_byte)unit_type;
745
1.16k
   info_ptr->valid |= PNG_INFO_pHYs;
746
1.16k
}
747
#endif
748
749
void PNGAPI
750
png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
751
    png_const_colorp palette, int num_palette)
752
601
{
753
601
   png_color safe_palette[PNG_MAX_PALETTE_LENGTH];
754
601
   png_uint_32 max_palette_length;
755
756
601
   png_debug1(1, "in %s storage function", "PLTE");
757
758
601
   if (png_ptr == NULL || info_ptr == NULL)
759
0
      return;
760
761
601
   max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
762
558
      (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
763
764
601
   if (num_palette < 0 || num_palette > (int) max_palette_length)
765
0
   {
766
0
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
767
0
         png_error(png_ptr, "Invalid palette length");
768
769
0
      else
770
0
      {
771
0
         png_warning(png_ptr, "Invalid palette length");
772
773
0
         return;
774
0
      }
775
0
   }
776
777
601
   if ((num_palette > 0 && palette == NULL) ||
778
601
      (num_palette == 0
779
3
#        ifdef PNG_MNG_FEATURES_SUPPORTED
780
3
            && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
781
601
#        endif
782
601
      ))
783
3
   {
784
3
      png_error(png_ptr, "Invalid palette");
785
3
   }
786
787
   /* Snapshot the caller's palette before freeing, in case it points to
788
    * info_ptr->palette (getter-to-setter aliasing).
789
    */
790
598
   if (num_palette > 0)
791
598
      memcpy(safe_palette, palette, (unsigned int)num_palette *
792
598
          (sizeof (png_color)));
793
794
598
   palette = safe_palette;
795
796
598
   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
797
798
   /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
799
    * of num_palette entries, in case of an invalid PNG file or incorrect
800
    * call to png_set_PLTE() with too-large sample values.
801
    *
802
    * Allocate independent buffers for info_ptr and png_ptr so that the
803
    * lifetime of png_ptr->palette is decoupled from the lifetime of
804
    * info_ptr->palette.  Previously, these two pointers were aliased,
805
    * which caused a use-after-free vulnerability if png_free_data freed
806
    * info_ptr->palette while png_ptr->palette was still in use by the
807
    * row transform functions (e.g. png_do_expand_palette).
808
    *
809
    * Both buffers are allocated with png_calloc to zero-fill, because
810
    * the ARM NEON palette riffle reads all 256 entries unconditionally,
811
    * regardless of num_palette.
812
    */
813
598
   png_free(png_ptr, png_ptr->palette);
814
598
   png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
815
598
       PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
816
598
   info_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
817
598
       PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
818
598
   png_ptr->num_palette = info_ptr->num_palette = (png_uint_16)num_palette;
819
820
598
   if (num_palette > 0)
821
598
   {
822
598
      memcpy(info_ptr->palette, palette, (unsigned int)num_palette *
823
598
          (sizeof (png_color)));
824
598
      memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
825
598
          (sizeof (png_color)));
826
598
   }
827
828
598
   info_ptr->free_me |= PNG_FREE_PLTE;
829
598
   info_ptr->valid |= PNG_INFO_PLTE;
830
598
}
831
832
#ifdef PNG_sBIT_SUPPORTED
833
void PNGAPI
834
png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
835
    png_const_color_8p sig_bit)
836
82
{
837
82
   png_debug1(1, "in %s storage function", "sBIT");
838
839
82
   if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
840
0
      return;
841
842
82
   info_ptr->sig_bit = *sig_bit;
843
82
   info_ptr->valid |= PNG_INFO_sBIT;
844
82
}
845
#endif
846
847
#ifdef PNG_sRGB_SUPPORTED
848
void PNGAPI
849
png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
850
823
{
851
823
   png_debug1(1, "in %s storage function", "sRGB");
852
853
823
   if (png_ptr == NULL || info_ptr == NULL)
854
0
      return;
855
856
823
   info_ptr->rendering_intent = srgb_intent;
857
823
   info_ptr->valid |= PNG_INFO_sRGB;
858
823
}
859
860
void PNGAPI
861
png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
862
    int srgb_intent)
863
0
{
864
0
   png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
865
866
0
   if (png_ptr == NULL || info_ptr == NULL)
867
0
      return;
868
869
0
   png_set_sRGB(png_ptr, info_ptr, srgb_intent);
870
871
0
#  ifdef PNG_gAMA_SUPPORTED
872
0
      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
873
0
#  endif /* gAMA */
874
875
0
#  ifdef PNG_cHRM_SUPPORTED
876
0
      png_set_cHRM_fixed(png_ptr, info_ptr,
877
         /* color      x       y */
878
0
         /* white */ 31270, 32900,
879
0
         /* red   */ 64000, 33000,
880
0
         /* green */ 30000, 60000,
881
0
         /* blue  */ 15000,  6000);
882
0
#  endif /* cHRM */
883
0
}
884
#endif /* sRGB */
885
886
887
#ifdef PNG_iCCP_SUPPORTED
888
void PNGAPI
889
png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
890
    png_const_charp name, int compression_type,
891
    png_const_bytep profile, png_uint_32 proflen)
892
0
{
893
0
   png_charp new_iccp_name;
894
0
   png_bytep new_iccp_profile;
895
0
   size_t length;
896
897
0
   png_debug1(1, "in %s storage function", "iCCP");
898
899
0
   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
900
0
      return;
901
902
0
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
903
0
      png_app_error(png_ptr, "Invalid iCCP compression method");
904
905
0
   length = strlen(name)+1;
906
0
   new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
907
908
0
   if (new_iccp_name == NULL)
909
0
   {
910
0
      png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
911
912
0
      return;
913
0
   }
914
915
0
   memcpy(new_iccp_name, name, length);
916
0
   new_iccp_profile = png_voidcast(png_bytep,
917
0
       png_malloc_warn(png_ptr, proflen));
918
919
0
   if (new_iccp_profile == NULL)
920
0
   {
921
0
      png_free(png_ptr, new_iccp_name);
922
0
      png_benign_error(png_ptr,
923
0
          "Insufficient memory to process iCCP profile");
924
925
0
      return;
926
0
   }
927
928
0
   memcpy(new_iccp_profile, profile, proflen);
929
930
0
   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
931
932
0
   info_ptr->iccp_proflen = proflen;
933
0
   info_ptr->iccp_name = new_iccp_name;
934
0
   info_ptr->iccp_profile = new_iccp_profile;
935
0
   info_ptr->free_me |= PNG_FREE_ICCP;
936
0
   info_ptr->valid |= PNG_INFO_iCCP;
937
0
}
938
#endif
939
940
#ifdef PNG_TEXT_SUPPORTED
941
void PNGAPI
942
png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
943
    png_const_textp text_ptr, int num_text)
944
0
{
945
0
   int ret;
946
0
   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
947
948
0
   if (ret != 0)
949
0
      png_error(png_ptr, "Insufficient memory to store text");
950
0
}
951
952
int /* PRIVATE */
953
png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
954
    png_const_textp text_ptr, int num_text)
955
12.2k
{
956
12.2k
   int i;
957
12.2k
   png_textp old_text = NULL;
958
959
12.2k
   png_debug1(1, "in text storage function, chunk typeid = 0x%lx",
960
12.2k
      png_ptr == NULL ? 0xabadca11UL : (unsigned long)png_ptr->chunk_name);
961
962
12.2k
   if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
963
0
      return 0;
964
965
   /* Make sure we have enough space in the "text" array in info_struct
966
    * to hold all of the incoming text_ptr objects.  This compare can't overflow
967
    * because max_text >= num_text (anyway, subtract of two positive integers
968
    * can't overflow in any case.)
969
    */
970
12.2k
   if (num_text > info_ptr->max_text - info_ptr->num_text)
971
2.74k
   {
972
2.74k
      int old_num_text = info_ptr->num_text;
973
2.74k
      int max_text;
974
2.74k
      png_textp new_text = NULL;
975
976
      /* Calculate an appropriate max_text, checking for overflow. */
977
2.74k
      max_text = old_num_text;
978
2.74k
      if (num_text <= INT_MAX - max_text)
979
2.74k
      {
980
2.74k
         max_text += num_text;
981
982
         /* Round up to a multiple of 8 */
983
2.74k
         if (max_text < INT_MAX-8)
984
2.74k
            max_text = (max_text + 8) & ~0x7;
985
986
0
         else
987
0
            max_text = INT_MAX;
988
989
         /* Now allocate a new array and copy the old members in; this does all
990
          * the overflow checks.
991
          */
992
2.74k
         new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
993
2.74k
             info_ptr->text, old_num_text, max_text-old_num_text,
994
2.74k
             sizeof *new_text));
995
2.74k
      }
996
997
2.74k
      if (new_text == NULL)
998
0
      {
999
0
         png_chunk_report(png_ptr, "too many text chunks",
1000
0
             PNG_CHUNK_WRITE_ERROR);
1001
1002
0
         return 1;
1003
0
      }
1004
1005
      /* Defer freeing the old array until after the copy loop below,
1006
       * in case text_ptr aliases info_ptr->text (getter-to-setter).
1007
       */
1008
2.74k
      old_text = info_ptr->text;
1009
1010
2.74k
      info_ptr->text = new_text;
1011
2.74k
      info_ptr->free_me |= PNG_FREE_TEXT;
1012
2.74k
      info_ptr->max_text = max_text;
1013
      /* num_text is adjusted below as the entries are copied in */
1014
1015
2.74k
      png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
1016
2.74k
   }
1017
1018
24.5k
   for (i = 0; i < num_text; i++)
1019
12.2k
   {
1020
12.2k
      size_t text_length, key_len;
1021
12.2k
      size_t lang_len, lang_key_len;
1022
12.2k
      png_textp textp = &(info_ptr->text[info_ptr->num_text]);
1023
1024
12.2k
      if (text_ptr[i].key == NULL)
1025
0
          continue;
1026
1027
12.2k
      if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
1028
12.2k
          text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
1029
0
      {
1030
0
         png_chunk_report(png_ptr, "text compression mode is out of range",
1031
0
             PNG_CHUNK_WRITE_ERROR);
1032
0
         continue;
1033
0
      }
1034
1035
12.2k
      key_len = strlen(text_ptr[i].key);
1036
1037
12.2k
      if (text_ptr[i].compression <= 0)
1038
10.8k
      {
1039
10.8k
         lang_len = 0;
1040
10.8k
         lang_key_len = 0;
1041
10.8k
      }
1042
1043
1.42k
      else
1044
1.42k
#  ifdef PNG_iTXt_SUPPORTED
1045
1.42k
      {
1046
         /* Set iTXt data */
1047
1048
1.42k
         if (text_ptr[i].lang != NULL)
1049
1.42k
            lang_len = strlen(text_ptr[i].lang);
1050
1051
0
         else
1052
0
            lang_len = 0;
1053
1054
1.42k
         if (text_ptr[i].lang_key != NULL)
1055
1.42k
            lang_key_len = strlen(text_ptr[i].lang_key);
1056
1057
0
         else
1058
0
            lang_key_len = 0;
1059
1.42k
      }
1060
#  else /* iTXt */
1061
      {
1062
         png_chunk_report(png_ptr, "iTXt chunk not supported",
1063
             PNG_CHUNK_WRITE_ERROR);
1064
         continue;
1065
      }
1066
#  endif
1067
1068
12.2k
      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
1069
3.71k
      {
1070
3.71k
         text_length = 0;
1071
3.71k
#  ifdef PNG_iTXt_SUPPORTED
1072
3.71k
         if (text_ptr[i].compression > 0)
1073
1.15k
            textp->compression = PNG_ITXT_COMPRESSION_NONE;
1074
1075
2.55k
         else
1076
2.55k
#  endif
1077
2.55k
            textp->compression = PNG_TEXT_COMPRESSION_NONE;
1078
3.71k
      }
1079
1080
8.57k
      else
1081
8.57k
      {
1082
8.57k
         text_length = strlen(text_ptr[i].text);
1083
8.57k
         textp->compression = text_ptr[i].compression;
1084
8.57k
      }
1085
1086
12.2k
      textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
1087
12.2k
          key_len + text_length + lang_len + lang_key_len + 4));
1088
1089
12.2k
      if (textp->key == NULL)
1090
0
      {
1091
0
         png_chunk_report(png_ptr, "text chunk: out of memory",
1092
0
             PNG_CHUNK_WRITE_ERROR);
1093
0
         png_free(png_ptr, old_text);
1094
1095
0
         return 1;
1096
0
      }
1097
1098
12.2k
      png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
1099
12.2k
          (unsigned long)(png_uint_32)
1100
12.2k
          (key_len + lang_len + lang_key_len + text_length + 4),
1101
12.2k
          textp->key);
1102
1103
12.2k
      memcpy(textp->key, text_ptr[i].key, key_len);
1104
12.2k
      *(textp->key + key_len) = '\0';
1105
1106
12.2k
      if (text_ptr[i].compression > 0)
1107
1.42k
      {
1108
1.42k
         textp->lang = textp->key + key_len + 1;
1109
1.42k
         memcpy(textp->lang, text_ptr[i].lang, lang_len);
1110
1.42k
         *(textp->lang + lang_len) = '\0';
1111
1.42k
         textp->lang_key = textp->lang + lang_len + 1;
1112
1.42k
         memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
1113
1.42k
         *(textp->lang_key + lang_key_len) = '\0';
1114
1.42k
         textp->text = textp->lang_key + lang_key_len + 1;
1115
1.42k
      }
1116
1117
10.8k
      else
1118
10.8k
      {
1119
10.8k
         textp->lang=NULL;
1120
10.8k
         textp->lang_key=NULL;
1121
10.8k
         textp->text = textp->key + key_len + 1;
1122
10.8k
      }
1123
1124
12.2k
      if (text_length != 0)
1125
8.57k
         memcpy(textp->text, text_ptr[i].text, text_length);
1126
1127
12.2k
      *(textp->text + text_length) = '\0';
1128
1129
12.2k
#  ifdef PNG_iTXt_SUPPORTED
1130
12.2k
      if (textp->compression > 0)
1131
1.42k
      {
1132
1.42k
         textp->text_length = 0;
1133
1.42k
         textp->itxt_length = text_length;
1134
1.42k
      }
1135
1136
10.8k
      else
1137
10.8k
#  endif
1138
10.8k
      {
1139
10.8k
         textp->text_length = text_length;
1140
10.8k
         textp->itxt_length = 0;
1141
10.8k
      }
1142
1143
12.2k
      info_ptr->num_text++;
1144
12.2k
      png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
1145
12.2k
   }
1146
1147
12.2k
   png_free(png_ptr, old_text);
1148
1149
12.2k
   return 0;
1150
12.2k
}
1151
#endif
1152
1153
#ifdef PNG_tIME_SUPPORTED
1154
void PNGAPI
1155
png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
1156
    png_const_timep mod_time)
1157
1.24k
{
1158
1.24k
   png_debug1(1, "in %s storage function", "tIME");
1159
1160
1.24k
   if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
1161
1.24k
       (png_ptr->mode & PNG_WROTE_tIME) != 0)
1162
0
      return;
1163
1164
1.24k
   if (mod_time->month == 0   || mod_time->month > 12  ||
1165
834
       mod_time->day   == 0   || mod_time->day   > 31  ||
1166
563
       mod_time->hour  > 23   || mod_time->minute > 59 ||
1167
255
       mod_time->second > 60)
1168
1.17k
   {
1169
1.17k
      png_warning(png_ptr, "Ignoring invalid time value");
1170
1171
1.17k
      return;
1172
1.17k
   }
1173
1174
73
   info_ptr->mod_time = *mod_time;
1175
73
   info_ptr->valid |= PNG_INFO_tIME;
1176
73
}
1177
#endif
1178
1179
#ifdef PNG_tRNS_SUPPORTED
1180
void PNGAPI
1181
png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
1182
    png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
1183
2.74k
{
1184
2.74k
   png_debug1(1, "in %s storage function", "tRNS");
1185
1186
2.74k
   if (png_ptr == NULL || info_ptr == NULL)
1187
1188
0
      return;
1189
1190
2.74k
   if (trans_alpha != NULL)
1191
2.74k
   {
1192
       /* Snapshot the caller's trans_alpha before freeing, in case it
1193
        * points to info_ptr->trans_alpha (getter-to-setter aliasing).
1194
        */
1195
2.74k
       png_byte safe_trans[PNG_MAX_PALETTE_LENGTH];
1196
1197
2.74k
       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
1198
2.74k
          memcpy(safe_trans, trans_alpha, (size_t)num_trans);
1199
1200
2.74k
       trans_alpha = safe_trans;
1201
1202
2.74k
       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
1203
1204
2.74k
       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
1205
2.74k
       {
1206
          /* Allocate info_ptr's copy of the transparency data.
1207
           * Initialize all entries to fully opaque (0xff), then overwrite
1208
           * the first num_trans entries with the actual values.
1209
           */
1210
2.74k
          info_ptr->trans_alpha = png_voidcast(png_bytep,
1211
2.74k
              png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
1212
2.74k
          memset(info_ptr->trans_alpha, 0xff, PNG_MAX_PALETTE_LENGTH);
1213
2.74k
          memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
1214
2.74k
          info_ptr->free_me |= PNG_FREE_TRNS;
1215
2.74k
          info_ptr->valid |= PNG_INFO_tRNS;
1216
1217
          /* Allocate an independent copy for png_struct, so that the
1218
           * lifetime of png_ptr->trans_alpha is decoupled from the
1219
           * lifetime of info_ptr->trans_alpha.  Previously these two
1220
           * pointers were aliased, which caused a use-after-free if
1221
           * png_free_data freed info_ptr->trans_alpha while
1222
           * png_ptr->trans_alpha was still in use by the row transform
1223
           * functions (e.g. png_do_expand_palette).
1224
           */
1225
2.74k
          png_free(png_ptr, png_ptr->trans_alpha);
1226
2.74k
          png_ptr->trans_alpha = png_voidcast(png_bytep,
1227
2.74k
              png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
1228
2.74k
          memset(png_ptr->trans_alpha, 0xff, PNG_MAX_PALETTE_LENGTH);
1229
2.74k
          memcpy(png_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
1230
2.74k
       }
1231
0
       else
1232
0
       {
1233
0
          png_free(png_ptr, png_ptr->trans_alpha);
1234
0
          png_ptr->trans_alpha = NULL;
1235
0
       }
1236
2.74k
   }
1237
1238
2.74k
   if (trans_color != NULL)
1239
2.74k
   {
1240
2.74k
#ifdef PNG_WARNINGS_SUPPORTED
1241
2.74k
      if (info_ptr->bit_depth < 16)
1242
2.18k
      {
1243
2.18k
         int sample_max = (1 << info_ptr->bit_depth) - 1;
1244
1245
2.18k
         if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
1246
1.59k
             trans_color->gray > sample_max) ||
1247
1.67k
             (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
1248
277
             (trans_color->red > sample_max ||
1249
172
             trans_color->green > sample_max ||
1250
81
             trans_color->blue > sample_max)))
1251
755
            png_warning(png_ptr,
1252
755
                "tRNS chunk has out-of-range samples for bit_depth");
1253
2.18k
      }
1254
2.74k
#endif
1255
1256
2.74k
      info_ptr->trans_color = *trans_color;
1257
1258
2.74k
      if (num_trans == 0)
1259
0
         num_trans = 1;
1260
2.74k
   }
1261
1262
2.74k
   info_ptr->num_trans = (png_uint_16)num_trans;
1263
1264
2.74k
   if (num_trans != 0)
1265
2.74k
   {
1266
2.74k
      info_ptr->free_me |= PNG_FREE_TRNS;
1267
2.74k
      info_ptr->valid |= PNG_INFO_tRNS;
1268
2.74k
   }
1269
2.74k
}
1270
#endif
1271
1272
#ifdef PNG_sPLT_SUPPORTED
1273
void PNGAPI
1274
png_set_sPLT(png_const_structrp png_ptr,
1275
    png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
1276
/*
1277
 *  entries        - array of png_sPLT_t structures
1278
 *                   to be added to the list of palettes
1279
 *                   in the info structure.
1280
 *
1281
 *  nentries       - number of palette structures to be
1282
 *                   added.
1283
 */
1284
3.24k
{
1285
3.24k
   png_sPLT_tp np;
1286
3.24k
   png_sPLT_tp old_spalettes;
1287
1288
3.24k
   png_debug1(1, "in %s storage function", "sPLT");
1289
1290
3.24k
   if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
1291
0
      return;
1292
1293
   /* Use the internal realloc function, which checks for all the possible
1294
    * overflows.  Notice that the parameters are (int) and (size_t)
1295
    */
1296
3.24k
   np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
1297
3.24k
       info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
1298
3.24k
       sizeof *np));
1299
1300
3.24k
   if (np == NULL)
1301
0
   {
1302
      /* Out of memory or too many chunks */
1303
0
      png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
1304
0
      return;
1305
0
   }
1306
1307
   /* Defer freeing the old array until after the copy loop below,
1308
    * in case entries aliases info_ptr->splt_palettes (getter-to-setter).
1309
    */
1310
3.24k
   old_spalettes = info_ptr->splt_palettes;
1311
1312
3.24k
   info_ptr->splt_palettes = np;
1313
3.24k
   info_ptr->free_me |= PNG_FREE_SPLT;
1314
1315
3.24k
   np += info_ptr->splt_palettes_num;
1316
1317
3.24k
   do
1318
3.24k
   {
1319
3.24k
      size_t length;
1320
1321
      /* Skip invalid input entries */
1322
3.24k
      if (entries->name == NULL || entries->entries == NULL)
1323
0
      {
1324
         /* png_handle_sPLT doesn't do this, so this is an app error */
1325
0
         png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
1326
         /* Just skip the invalid entry */
1327
0
         continue;
1328
0
      }
1329
1330
3.24k
      np->depth = entries->depth;
1331
1332
      /* In the event of out-of-memory just return - there's no point keeping
1333
       * on trying to add sPLT chunks.
1334
       */
1335
3.24k
      length = strlen(entries->name) + 1;
1336
3.24k
      np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
1337
1338
3.24k
      if (np->name == NULL)
1339
0
         break;
1340
1341
3.24k
      memcpy(np->name, entries->name, length);
1342
1343
      /* IMPORTANT: we have memory now that won't get freed if something else
1344
       * goes wrong; this code must free it.  png_malloc_array produces no
1345
       * warnings; use a png_chunk_report (below) if there is an error.
1346
       */
1347
3.24k
      np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
1348
3.24k
          entries->nentries, sizeof (png_sPLT_entry)));
1349
1350
3.24k
      if (np->entries == NULL)
1351
0
      {
1352
0
         png_free(png_ptr, np->name);
1353
0
         np->name = NULL;
1354
0
         break;
1355
0
      }
1356
1357
3.24k
      np->nentries = entries->nentries;
1358
      /* This multiply can't overflow because png_malloc_array has already
1359
       * checked it when doing the allocation.
1360
       */
1361
3.24k
      memcpy(np->entries, entries->entries,
1362
3.24k
          (unsigned int)entries->nentries * sizeof (png_sPLT_entry));
1363
1364
      /* Note that 'continue' skips the advance of the out pointer and out
1365
       * count, so an invalid entry is not added.
1366
       */
1367
3.24k
      info_ptr->valid |= PNG_INFO_sPLT;
1368
3.24k
      ++(info_ptr->splt_palettes_num);
1369
3.24k
      ++np;
1370
3.24k
      ++entries;
1371
3.24k
   }
1372
3.24k
   while (--nentries);
1373
1374
3.24k
   png_free(png_ptr, old_spalettes);
1375
1376
3.24k
   if (nentries > 0)
1377
0
      png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
1378
3.24k
}
1379
#endif /* sPLT */
1380
1381
#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1382
static png_byte
1383
check_location(png_const_structrp png_ptr, int location)
1384
877k
{
1385
877k
   location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1386
1387
   /* New in 1.6.0; copy the location and check it.  This is an API
1388
    * change; previously the app had to use the
1389
    * png_set_unknown_chunk_location API below for each chunk.
1390
    */
1391
877k
   if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1392
0
   {
1393
      /* Write struct, so unknown chunks come from the app */
1394
0
      png_app_warning(png_ptr,
1395
0
          "png_set_unknown_chunks now expects a valid location");
1396
      /* Use the old behavior */
1397
0
      location = (png_byte)(png_ptr->mode &
1398
0
          (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
1399
0
   }
1400
1401
   /* This need not be an internal error - if the app calls
1402
    * png_set_unknown_chunks on a read pointer it must get the location right.
1403
    */
1404
877k
   if (location == 0)
1405
15
      png_error(png_ptr, "invalid location in png_set_unknown_chunks");
1406
1407
   /* Now reduce the location to the top-most set bit by removing each least
1408
    * significant bit in turn.
1409
    */
1410
880k
   while (location != (location & -location))
1411
3.10k
      location &= ~(location & -location);
1412
1413
   /* The cast is safe because 'location' is a bit mask and only the low four
1414
    * bits are significant.
1415
    */
1416
877k
   return (png_byte)location;
1417
877k
}
1418
1419
void PNGAPI
1420
png_set_unknown_chunks(png_const_structrp png_ptr,
1421
    png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
1422
877k
{
1423
877k
   png_unknown_chunkp np;
1424
877k
   png_unknown_chunkp old_unknowns;
1425
1426
877k
   if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
1427
877k
       unknowns == NULL)
1428
0
      return;
1429
1430
   /* Check for the failure cases where support has been disabled at compile
1431
    * time.  This code is hardly ever compiled - it's here because
1432
    * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
1433
    * code) but may be meaningless if the read or write handling of unknown
1434
    * chunks is not compiled in.
1435
    */
1436
#  if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
1437
      defined(PNG_READ_SUPPORTED)
1438
      if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
1439
      {
1440
         png_app_error(png_ptr, "no unknown chunk support on read");
1441
1442
         return;
1443
      }
1444
#  endif
1445
#  if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
1446
      defined(PNG_WRITE_SUPPORTED)
1447
      if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1448
      {
1449
         png_app_error(png_ptr, "no unknown chunk support on write");
1450
1451
         return;
1452
      }
1453
#  endif
1454
1455
   /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
1456
    * unknown critical chunks could be lost with just a warning resulting in
1457
    * undefined behavior.  Now png_chunk_report is used to provide behavior
1458
    * appropriate to read or write.
1459
    */
1460
877k
   np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
1461
877k
       info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
1462
877k
       sizeof *np));
1463
1464
877k
   if (np == NULL)
1465
0
   {
1466
0
      png_chunk_report(png_ptr, "too many unknown chunks",
1467
0
          PNG_CHUNK_WRITE_ERROR);
1468
0
      return;
1469
0
   }
1470
1471
   /* Defer freeing the old array until after the copy loop below,
1472
    * in case unknowns aliases info_ptr->unknown_chunks (getter-to-setter).
1473
    */
1474
877k
   old_unknowns = info_ptr->unknown_chunks;
1475
1476
877k
   info_ptr->unknown_chunks = np; /* safe because it is initialized */
1477
877k
   info_ptr->free_me |= PNG_FREE_UNKN;
1478
1479
877k
   np += info_ptr->unknown_chunks_num;
1480
1481
   /* Increment unknown_chunks_num each time round the loop to protect the
1482
    * just-allocated chunk data.
1483
    */
1484
1.75M
   for (; num_unknowns > 0; --num_unknowns, ++unknowns)
1485
877k
   {
1486
877k
      memcpy(np->name, unknowns->name, (sizeof np->name));
1487
877k
      np->name[(sizeof np->name)-1] = '\0';
1488
877k
      np->location = check_location(png_ptr, unknowns->location);
1489
1490
877k
      if (unknowns->size == 0)
1491
278k
      {
1492
278k
         np->data = NULL;
1493
278k
         np->size = 0;
1494
278k
      }
1495
1496
599k
      else
1497
599k
      {
1498
599k
         np->data = png_voidcast(png_bytep,
1499
599k
             png_malloc_base(png_ptr, unknowns->size));
1500
1501
599k
         if (np->data == NULL)
1502
0
         {
1503
0
            png_chunk_report(png_ptr, "unknown chunk: out of memory",
1504
0
                PNG_CHUNK_WRITE_ERROR);
1505
            /* But just skip storing the unknown chunk */
1506
0
            continue;
1507
0
         }
1508
1509
599k
         memcpy(np->data, unknowns->data, unknowns->size);
1510
599k
         np->size = unknowns->size;
1511
599k
      }
1512
1513
      /* These increments are skipped on out-of-memory for the data - the
1514
       * unknown chunk entry gets overwritten if the png_chunk_report returns.
1515
       * This is correct in the read case (the chunk is just dropped.)
1516
       */
1517
877k
      ++np;
1518
877k
      ++(info_ptr->unknown_chunks_num);
1519
877k
   }
1520
1521
877k
   png_free(png_ptr, old_unknowns);
1522
877k
}
1523
1524
void PNGAPI
1525
png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
1526
    int chunk, int location)
1527
0
{
1528
   /* This API is pretty pointless in 1.6.0 because the location can be set
1529
    * before the call to png_set_unknown_chunks.
1530
    *
1531
    * TODO: add a png_app_warning in 1.7
1532
    */
1533
0
   if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
1534
0
      chunk < info_ptr->unknown_chunks_num)
1535
0
   {
1536
0
      if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
1537
0
      {
1538
0
         png_app_error(png_ptr, "invalid unknown chunk location");
1539
         /* Fake out the pre 1.6.0 behavior: */
1540
0
         if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */
1541
0
            location = PNG_AFTER_IDAT;
1542
1543
0
         else
1544
0
            location = PNG_HAVE_IHDR; /* also undocumented */
1545
0
      }
1546
1547
0
      info_ptr->unknown_chunks[chunk].location =
1548
0
         check_location(png_ptr, location);
1549
0
   }
1550
0
}
1551
#endif /* STORE_UNKNOWN_CHUNKS */
1552
1553
#ifdef PNG_MNG_FEATURES_SUPPORTED
1554
png_uint_32 PNGAPI
1555
png_permit_mng_features(png_structrp png_ptr, png_uint_32 mng_features)
1556
0
{
1557
0
   png_debug(1, "in png_permit_mng_features");
1558
1559
0
   if (png_ptr == NULL)
1560
0
      return 0;
1561
1562
0
   png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
1563
1564
0
   return png_ptr->mng_features_permitted;
1565
0
}
1566
#endif
1567
1568
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1569
static unsigned int
1570
add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
1571
0
{
1572
0
   unsigned int i;
1573
1574
   /* Utility function: update the 'keep' state of a chunk if it is already in
1575
    * the list, otherwise add it to the list.
1576
    */
1577
0
   for (i=0; i<count; ++i, list += 5)
1578
0
   {
1579
0
      if (memcmp(list, add, 4) == 0)
1580
0
      {
1581
0
         list[4] = (png_byte)keep;
1582
1583
0
         return count;
1584
0
      }
1585
0
   }
1586
1587
0
   if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1588
0
   {
1589
0
      ++count;
1590
0
      memcpy(list, add, 4);
1591
0
      list[4] = (png_byte)keep;
1592
0
   }
1593
1594
0
   return count;
1595
0
}
1596
1597
void PNGAPI
1598
png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
1599
    png_const_bytep chunk_list, int num_chunks_in)
1600
20.4k
{
1601
20.4k
   png_bytep new_list;
1602
20.4k
   unsigned int num_chunks, old_num_chunks;
1603
1604
20.4k
   if (png_ptr == NULL)
1605
0
      return;
1606
1607
20.4k
   if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
1608
0
   {
1609
0
      png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
1610
1611
0
      return;
1612
0
   }
1613
1614
20.4k
   if (num_chunks_in <= 0)
1615
20.4k
   {
1616
20.4k
      png_ptr->unknown_default = keep;
1617
1618
      /* '0' means just set the flags, so stop here */
1619
20.4k
      if (num_chunks_in == 0)
1620
20.4k
        return;
1621
20.4k
   }
1622
1623
0
   if (num_chunks_in < 0)
1624
0
   {
1625
      /* Ignore all unknown chunks and all chunks recognized by
1626
       * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
1627
       */
1628
0
      static const png_byte chunks_to_ignore[] = {
1629
0
         98,  75,  71,  68, '\0',  /* bKGD */
1630
0
         99,  72,  82,  77, '\0',  /* cHRM */
1631
0
         99,  73,  67,  80, '\0',  /* cICP */
1632
0
         99,  76,  76,  73, '\0',  /* cLLI */
1633
0
        101,  88,  73, 102, '\0',  /* eXIf */
1634
0
        103,  65,  77,  65, '\0',  /* gAMA */
1635
0
        104,  73,  83,  84, '\0',  /* hIST */
1636
0
        105,  67,  67,  80, '\0',  /* iCCP */
1637
0
        105,  84,  88, 116, '\0',  /* iTXt */
1638
0
        109,  68,  67,  86, '\0',  /* mDCV */
1639
0
        111,  70,  70, 115, '\0',  /* oFFs */
1640
0
        112,  67,  65,  76, '\0',  /* pCAL */
1641
0
        112,  72,  89, 115, '\0',  /* pHYs */
1642
0
        115,  66,  73,  84, '\0',  /* sBIT */
1643
0
        115,  67,  65,  76, '\0',  /* sCAL */
1644
0
        115,  80,  76,  84, '\0',  /* sPLT */
1645
0
        115,  84,  69,  82, '\0',  /* sTER */
1646
0
        115,  82,  71,  66, '\0',  /* sRGB */
1647
0
        116,  69,  88, 116, '\0',  /* tEXt */
1648
0
        116,  73,  77,  69, '\0',  /* tIME */
1649
0
        122,  84,  88, 116, '\0'   /* zTXt */
1650
0
      };
1651
1652
0
      chunk_list = chunks_to_ignore;
1653
0
      num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
1654
0
   }
1655
1656
0
   else /* num_chunks_in > 0 */
1657
0
   {
1658
0
      if (chunk_list == NULL)
1659
0
      {
1660
         /* Prior to 1.6.0 this was silently ignored, now it is an app_error
1661
          * which can be switched off.
1662
          */
1663
0
         png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
1664
1665
0
         return;
1666
0
      }
1667
1668
0
      num_chunks = (unsigned int)num_chunks_in;
1669
0
   }
1670
1671
0
   old_num_chunks = png_ptr->num_chunk_list;
1672
0
   if (png_ptr->chunk_list == NULL)
1673
0
      old_num_chunks = 0;
1674
1675
   /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1676
    */
1677
0
   if (num_chunks + old_num_chunks > UINT_MAX/5)
1678
0
   {
1679
0
      png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
1680
1681
0
      return;
1682
0
   }
1683
1684
   /* If these chunks are being reset to the default then no more memory is
1685
    * required because add_one_chunk above doesn't extend the list if the 'keep'
1686
    * parameter is the default.
1687
    */
1688
0
   if (keep != 0)
1689
0
   {
1690
0
      new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1691
0
          5 * (num_chunks + old_num_chunks)));
1692
1693
0
      if (old_num_chunks > 0)
1694
0
         memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
1695
0
   }
1696
1697
0
   else if (old_num_chunks > 0)
1698
0
      new_list = png_ptr->chunk_list;
1699
1700
0
   else
1701
0
      new_list = NULL;
1702
1703
   /* Add the new chunks together with each one's handling code.  If the chunk
1704
    * already exists the code is updated, otherwise the chunk is added to the
1705
    * end.  (In libpng 1.6.0 order no longer matters because this code enforces
1706
    * the earlier convention that the last setting is the one that is used.)
1707
    */
1708
0
   if (new_list != NULL)
1709
0
   {
1710
0
      png_const_bytep inlist;
1711
0
      png_bytep outlist;
1712
0
      unsigned int i;
1713
1714
0
      for (i=0; i<num_chunks; ++i)
1715
0
      {
1716
0
         old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1717
0
             chunk_list+5*i, keep);
1718
0
      }
1719
1720
      /* Now remove any spurious 'default' entries. */
1721
0
      num_chunks = 0;
1722
0
      for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
1723
0
      {
1724
0
         if (inlist[4])
1725
0
         {
1726
0
            if (outlist != inlist)
1727
0
               memcpy(outlist, inlist, 5);
1728
0
            outlist += 5;
1729
0
            ++num_chunks;
1730
0
         }
1731
0
      }
1732
1733
      /* This means the application has removed all the specialized handling. */
1734
0
      if (num_chunks == 0)
1735
0
      {
1736
0
         if (png_ptr->chunk_list != new_list)
1737
0
            png_free(png_ptr, new_list);
1738
1739
0
         new_list = NULL;
1740
0
      }
1741
0
   }
1742
1743
0
   else
1744
0
      num_chunks = 0;
1745
1746
0
   png_ptr->num_chunk_list = num_chunks;
1747
1748
0
   if (png_ptr->chunk_list != new_list)
1749
0
   {
1750
0
      if (png_ptr->chunk_list != NULL)
1751
0
         png_free(png_ptr, png_ptr->chunk_list);
1752
1753
0
      png_ptr->chunk_list = new_list;
1754
0
   }
1755
0
}
1756
#endif
1757
1758
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1759
void PNGAPI
1760
png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
1761
    png_user_chunk_ptr read_user_chunk_fn)
1762
20.4k
{
1763
20.4k
   png_debug(1, "in png_set_read_user_chunk_fn");
1764
1765
20.4k
   if (png_ptr == NULL)
1766
0
      return;
1767
1768
20.4k
   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1769
20.4k
   png_ptr->user_chunk_ptr = user_chunk_ptr;
1770
20.4k
}
1771
#endif
1772
1773
#ifdef PNG_INFO_IMAGE_SUPPORTED
1774
void PNGAPI
1775
png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
1776
    png_bytepp row_pointers)
1777
0
{
1778
0
   png_debug(1, "in png_set_rows");
1779
1780
0
   if (png_ptr == NULL || info_ptr == NULL)
1781
0
      return;
1782
1783
0
   if (info_ptr->row_pointers != NULL &&
1784
0
       (info_ptr->row_pointers != row_pointers))
1785
0
      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1786
1787
0
   info_ptr->row_pointers = row_pointers;
1788
1789
0
   if (row_pointers != NULL)
1790
0
      info_ptr->valid |= PNG_INFO_IDAT;
1791
0
}
1792
#endif
1793
1794
void PNGAPI
1795
png_set_compression_buffer_size(png_structrp png_ptr, size_t size)
1796
0
{
1797
0
   png_debug(1, "in png_set_compression_buffer_size");
1798
1799
0
   if (png_ptr == NULL)
1800
0
      return;
1801
1802
0
   if (size == 0 || size > PNG_UINT_31_MAX)
1803
0
      png_error(png_ptr, "invalid compression buffer size");
1804
1805
0
#  ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1806
0
   if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
1807
0
   {
1808
0
      png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
1809
0
      return;
1810
0
   }
1811
0
#  endif
1812
1813
0
#  ifdef PNG_WRITE_SUPPORTED
1814
0
   if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1815
0
   {
1816
0
      if (png_ptr->zowner != 0)
1817
0
      {
1818
0
         png_warning(png_ptr,
1819
0
             "Compression buffer size cannot be changed because it is in use");
1820
1821
0
         return;
1822
0
      }
1823
1824
0
#ifndef __COVERITY__
1825
      /* Some compilers complain that this is always false.  However, it
1826
       * can be true when integer overflow happens.
1827
       */
1828
0
      if (size > ZLIB_IO_MAX)
1829
0
      {
1830
0
         png_warning(png_ptr,
1831
0
             "Compression buffer size limited to system maximum");
1832
0
         size = ZLIB_IO_MAX; /* must fit */
1833
0
      }
1834
0
#endif
1835
1836
0
      if (size < 6)
1837
0
      {
1838
         /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
1839
          * if this is permitted.
1840
          */
1841
0
         png_warning(png_ptr,
1842
0
             "Compression buffer size cannot be reduced below 6");
1843
1844
0
         return;
1845
0
      }
1846
1847
0
      if (png_ptr->zbuffer_size != size)
1848
0
      {
1849
0
         png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1850
0
         png_ptr->zbuffer_size = (uInt)size;
1851
0
      }
1852
0
   }
1853
0
#  endif
1854
0
}
1855
1856
void PNGAPI
1857
png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
1858
0
{
1859
0
   if (png_ptr != NULL && info_ptr != NULL)
1860
0
      info_ptr->valid &= (unsigned int)(~mask);
1861
0
}
1862
1863
1864
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
1865
/* This function was added to libpng 1.2.6 */
1866
void PNGAPI
1867
png_set_user_limits(png_structrp png_ptr, png_uint_32 user_width_max,
1868
    png_uint_32 user_height_max)
1869
0
{
1870
0
   png_debug(1, "in png_set_user_limits");
1871
1872
   /* Images with dimensions larger than these limits will be
1873
    * rejected by png_set_IHDR().  To accept any PNG datastream
1874
    * regardless of dimensions, set both limits to 0x7fffffff.
1875
    */
1876
0
   if (png_ptr == NULL)
1877
0
      return;
1878
1879
0
   png_ptr->user_width_max = user_width_max;
1880
0
   png_ptr->user_height_max = user_height_max;
1881
0
}
1882
1883
/* This function was added to libpng 1.4.0 */
1884
void PNGAPI
1885
png_set_chunk_cache_max(png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
1886
0
{
1887
0
   png_debug(1, "in png_set_chunk_cache_max");
1888
1889
0
   if (png_ptr != NULL)
1890
0
      png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1891
0
}
1892
1893
/* This function was added to libpng 1.4.1 */
1894
void PNGAPI
1895
png_set_chunk_malloc_max(png_structrp png_ptr,
1896
    png_alloc_size_t user_chunk_malloc_max)
1897
0
{
1898
0
   png_debug(1, "in png_set_chunk_malloc_max");
1899
1900
   /* pngstruct::user_chunk_malloc_max is initialized to a non-zero value in
1901
    * png.c.  This API supports '0' for unlimited, make sure the correct
1902
    * (unlimited) value is set here to avoid a need to check for 0 everywhere
1903
    * the parameter is used.
1904
    */
1905
0
   if (png_ptr != NULL)
1906
0
   {
1907
0
      if (user_chunk_malloc_max == 0U) /* unlimited */
1908
0
      {
1909
#        ifdef PNG_MAX_MALLOC_64K
1910
            png_ptr->user_chunk_malloc_max = 65536U;
1911
#        else
1912
0
            png_ptr->user_chunk_malloc_max = PNG_SIZE_MAX;
1913
0
#        endif
1914
0
      }
1915
0
      else
1916
0
         png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
1917
0
   }
1918
0
}
1919
#endif /* ?SET_USER_LIMITS */
1920
1921
1922
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
1923
void PNGAPI
1924
png_set_benign_errors(png_structrp png_ptr, int allowed)
1925
0
{
1926
0
   png_debug(1, "in png_set_benign_errors");
1927
1928
   /* If allowed is 1, png_benign_error() is treated as a warning.
1929
    *
1930
    * If allowed is 0, png_benign_error() is treated as an error (which
1931
    * is the default behavior if png_set_benign_errors() is not called).
1932
    */
1933
1934
0
   if (allowed != 0)
1935
0
      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1936
0
         PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
1937
1938
0
   else
1939
0
      png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
1940
0
         PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
1941
0
}
1942
#endif /* BENIGN_ERRORS */
1943
1944
#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
1945
   /* Whether to report invalid palette index; added at libpng-1.5.10.
1946
    * It is possible for an indexed (color-type==3) PNG file to contain
1947
    * pixels with invalid (out-of-range) indexes if the PLTE chunk has
1948
    * fewer entries than the image's bit-depth would allow. We recover
1949
    * from this gracefully by filling any incomplete palette with zeros
1950
    * (opaque black).  By default, when this occurs libpng will issue
1951
    * a benign error.  This API can be used to override that behavior.
1952
    */
1953
void PNGAPI
1954
png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
1955
0
{
1956
0
   png_debug(1, "in png_set_check_for_invalid_index");
1957
1958
0
   if (allowed > 0)
1959
0
      png_ptr->num_palette_max = 0;
1960
1961
0
   else
1962
0
      png_ptr->num_palette_max = -1;
1963
0
}
1964
#endif
1965
1966
#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
1967
    defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
1968
/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1969
 * and if invalid, correct the keyword rather than discarding the entire
1970
 * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in
1971
 * length, forbids leading or trailing whitespace, multiple internal spaces,
1972
 * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.
1973
 *
1974
 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
1975
 * trailing '\0').  If this routine returns 0 then there was no keyword, or a
1976
 * valid one could not be generated, and the caller must png_error.
1977
 */
1978
png_uint_32 /* PRIVATE */
1979
png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
1980
0
{
1981
0
#ifdef PNG_WARNINGS_SUPPORTED
1982
0
   png_const_charp orig_key = key;
1983
0
#endif
1984
0
   png_uint_32 key_len = 0;
1985
0
   int bad_character = 0;
1986
0
   int space = 1;
1987
1988
0
   png_debug(1, "in png_check_keyword");
1989
1990
0
   if (key == NULL)
1991
0
   {
1992
0
      *new_key = 0;
1993
0
      return 0;
1994
0
   }
1995
1996
0
   while (*key && key_len < 79)
1997
0
   {
1998
0
      png_byte ch = (png_byte)*key++;
1999
2000
0
      if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
2001
0
      {
2002
0
         *new_key++ = ch; ++key_len; space = 0;
2003
0
      }
2004
2005
0
      else if (space == 0)
2006
0
      {
2007
         /* A space or an invalid character when one wasn't seen immediately
2008
          * before; output just a space.
2009
          */
2010
0
         *new_key++ = 32; ++key_len; space = 1;
2011
2012
         /* If the character was not a space then it is invalid. */
2013
0
         if (ch != 32)
2014
0
            bad_character = ch;
2015
0
      }
2016
2017
0
      else if (bad_character == 0)
2018
0
         bad_character = ch; /* just skip it, record the first error */
2019
0
   }
2020
2021
0
   if (key_len > 0 && space != 0) /* trailing space */
2022
0
   {
2023
0
      --key_len; --new_key;
2024
0
      if (bad_character == 0)
2025
0
         bad_character = 32;
2026
0
   }
2027
2028
   /* Terminate the keyword */
2029
0
   *new_key = 0;
2030
2031
0
   if (key_len == 0)
2032
0
      return 0;
2033
2034
0
#ifdef PNG_WARNINGS_SUPPORTED
2035
   /* Try to only output one warning per keyword: */
2036
0
   if (*key != 0) /* keyword too long */
2037
0
      png_warning(png_ptr, "keyword truncated");
2038
2039
0
   else if (bad_character != 0)
2040
0
   {
2041
0
      PNG_WARNING_PARAMETERS(p)
2042
2043
0
      png_warning_parameter(p, 1, orig_key);
2044
0
      png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
2045
2046
0
      png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
2047
0
   }
2048
#else /* !WARNINGS */
2049
   PNG_UNUSED(png_ptr)
2050
#endif /* !WARNINGS */
2051
2052
0
   return key_len;
2053
0
}
2054
#endif /* TEXT || pCAL || iCCP || sPLT */
2055
#endif /* READ || WRITE */