Coverage Report

Created: 2025-11-16 06:41

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