Coverage Report

Created: 2025-11-14 07:32

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
2.03k
{
27
2.03k
   png_debug1(1, "in %s storage function", "bKGD");
28
29
2.03k
   if (png_ptr == NULL || info_ptr == NULL || background == NULL)
30
0
      return;
31
32
2.03k
   info_ptr->background = *background;
33
2.03k
   info_ptr->valid |= PNG_INFO_bKGD;
34
2.03k
}
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
1.41k
{
44
1.41k
   png_debug1(1, "in %s storage function", "cHRM fixed");
45
46
1.41k
   if (png_ptr == NULL || info_ptr == NULL)
47
0
      return;
48
49
1.41k
   info_ptr->cHRM.redx = red_x;
50
1.41k
   info_ptr->cHRM.redy = red_y;
51
1.41k
   info_ptr->cHRM.greenx = green_x;
52
1.41k
   info_ptr->cHRM.greeny = green_y;
53
1.41k
   info_ptr->cHRM.bluex = blue_x;
54
1.41k
   info_ptr->cHRM.bluey = blue_y;
55
1.41k
   info_ptr->cHRM.whitex = white_x;
56
1.41k
   info_ptr->cHRM.whitey = white_y;
57
58
1.41k
   info_ptr->valid |= PNG_INFO_cHRM;
59
1.41k
}
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
860
{
103
860
   png_set_cHRM_fixed(png_ptr, info_ptr,
104
860
       png_fixed(png_ptr, white_x, "cHRM White X"),
105
860
       png_fixed(png_ptr, white_y, "cHRM White Y"),
106
860
       png_fixed(png_ptr, red_x, "cHRM Red X"),
107
860
       png_fixed(png_ptr, red_y, "cHRM Red Y"),
108
860
       png_fixed(png_ptr, green_x, "cHRM Green X"),
109
860
       png_fixed(png_ptr, green_y, "cHRM Green Y"),
110
860
       png_fixed(png_ptr, blue_x, "cHRM Blue X"),
111
860
       png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
112
860
}
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
26
{
140
26
   png_debug1(1, "in %s storage function", "cICP");
141
142
26
   if (png_ptr == NULL || info_ptr == NULL)
143
0
      return;
144
145
26
   info_ptr->cicp_colour_primaries = colour_primaries;
146
26
   info_ptr->cicp_transfer_function = transfer_function;
147
26
   info_ptr->cicp_matrix_coefficients = matrix_coefficients;
148
26
   info_ptr->cicp_video_full_range_flag = video_full_range_flag;
149
150
26
   if (info_ptr->cicp_matrix_coefficients != 0)
151
11
   {
152
11
      png_warning(png_ptr, "Invalid cICP matrix coefficients");
153
11
      return;
154
11
   }
155
156
15
   info_ptr->valid |= PNG_INFO_cICP;
157
15
}
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
61
{
168
61
   png_debug1(1, "in %s storage function", "cLLI");
169
170
61
   if (png_ptr == NULL || info_ptr == NULL)
171
0
      return;
172
173
   /* Check the light level range: */
174
61
   if (maxCLL > 0x7FFFFFFFU || maxFALL > 0x7FFFFFFFU)
175
41
   {
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
41
      png_chunk_report(png_ptr, "cLLI light level exceeds PNG limit",
183
41
            PNG_CHUNK_WRITE_ERROR);
184
41
      return;
185
41
   }
186
187
20
   info_ptr->maxCLL = maxCLL;
188
20
   info_ptr->maxFALL = maxFALL;
189
20
   info_ptr->valid |= PNG_INFO_cLLI;
190
20
}
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
1.59k
{
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
1.59k
   v /= 2; /* rounds to 0 in C: avoids insignificant arithmetic errors */
215
1.59k
   if (v > 65535 || v < 0)
216
0
   {
217
0
      *error = 1;
218
0
      return 0;
219
0
   }
220
221
1.59k
   return (png_uint_16)/*SAFE*/v;
222
1.59k
}
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
199
{
233
199
   png_uint_16 rx, ry, gx, gy, bx, by, wx, wy;
234
199
   int error;
235
236
199
   png_debug1(1, "in %s storage function", "mDCV");
237
238
199
   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
199
   error = 0;
243
199
   rx = png_ITU_fixed_16(&error, red_x);
244
199
   ry = png_ITU_fixed_16(&error, red_y);
245
199
   gx = png_ITU_fixed_16(&error, green_x);
246
199
   gy = png_ITU_fixed_16(&error, green_y);
247
199
   bx = png_ITU_fixed_16(&error, blue_x);
248
199
   by = png_ITU_fixed_16(&error, blue_y);
249
199
   wx = png_ITU_fixed_16(&error, white_x);
250
199
   wy = png_ITU_fixed_16(&error, white_y);
251
252
199
   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
199
   if (maxDL > 0x7FFFFFFFU || minDL > 0x7FFFFFFFU)
262
100
   {
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
100
      png_chunk_report(png_ptr, "mDCV display light level exceeds PNG limit",
270
100
            PNG_CHUNK_WRITE_ERROR);
271
100
      return;
272
100
   }
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
99
   info_ptr->mastering_red_x = rx;
283
99
   info_ptr->mastering_red_y = ry;
284
99
   info_ptr->mastering_green_x = gx;
285
99
   info_ptr->mastering_green_y = gy;
286
99
   info_ptr->mastering_blue_x = bx;
287
99
   info_ptr->mastering_blue_y = by;
288
99
   info_ptr->mastering_white_x = wx;
289
99
   info_ptr->mastering_white_y = wy;
290
99
   info_ptr->mastering_maxDL = maxDL;
291
99
   info_ptr->mastering_minDL = minDL;
292
99
   info_ptr->valid |= PNG_INFO_mDCV;
293
99
}
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
31
{
322
31
   png_byte *new_exif;
323
324
31
   png_debug1(1, "in %s storage function", "eXIf");
325
326
31
   if (png_ptr == NULL || info_ptr == NULL ||
327
31
       (png_ptr->mode & PNG_WROTE_eXIf) != 0)
328
0
      return;
329
330
31
   new_exif = png_voidcast(png_byte *, png_malloc_warn(png_ptr, num_exif));
331
332
31
   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
31
   memcpy(new_exif, exif, (size_t)num_exif);
339
340
31
   png_free_data(png_ptr, info_ptr, PNG_FREE_EXIF, 0);
341
342
31
   info_ptr->num_exif = num_exif;
343
31
   info_ptr->exif = new_exif;
344
31
   info_ptr->free_me |= PNG_FREE_EXIF;
345
31
   info_ptr->valid |= PNG_INFO_eXIf;
346
31
}
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
867
{
354
867
   png_debug1(1, "in %s storage function", "gAMA");
355
356
867
   if (png_ptr == NULL || info_ptr == NULL)
357
0
      return;
358
359
867
   info_ptr->gamma = file_gamma;
360
867
   info_ptr->valid |= PNG_INFO_gAMA;
361
867
}
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
171
{
367
171
   png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
368
171
       "png_set_gAMA"));
369
171
}
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
0
{
378
0
   int i;
379
380
0
   png_debug1(1, "in %s storage function", "hIST");
381
382
0
   if (png_ptr == NULL || info_ptr == NULL)
383
0
      return;
384
385
0
   if (info_ptr->num_palette == 0 || info_ptr->num_palette
386
0
       > PNG_MAX_PALETTE_LENGTH)
387
0
   {
388
0
      png_warning(png_ptr,
389
0
          "Invalid palette size, hIST allocation skipped");
390
391
0
      return;
392
0
   }
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.9k
{
422
77.9k
   png_debug1(1, "in %s storage function", "IHDR");
423
424
77.9k
   if (png_ptr == NULL || info_ptr == NULL)
425
0
      return;
426
427
77.9k
   info_ptr->width = width;
428
77.9k
   info_ptr->height = height;
429
77.9k
   info_ptr->bit_depth = (png_byte)bit_depth;
430
77.9k
   info_ptr->color_type = (png_byte)color_type;
431
77.9k
   info_ptr->compression_type = (png_byte)compression_type;
432
77.9k
   info_ptr->filter_type = (png_byte)filter_type;
433
77.9k
   info_ptr->interlace_type = (png_byte)interlace_type;
434
435
77.9k
   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
436
77.9k
       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
437
77.9k
       info_ptr->compression_type, info_ptr->filter_type);
438
439
77.9k
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
440
6.93k
      info_ptr->channels = 1;
441
442
71.0k
   else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
443
19.7k
      info_ptr->channels = 3;
444
445
51.3k
   else
446
51.3k
      info_ptr->channels = 1;
447
448
77.9k
   if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
449
10.9k
      info_ptr->channels++;
450
451
77.9k
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
452
453
77.9k
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
454
455
77.9k
#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.9k
   info_ptr->num_frames = 1;
460
77.9k
#endif
461
77.9k
}
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
74
{
468
74
   png_debug1(1, "in %s storage function", "oFFs");
469
470
74
   if (png_ptr == NULL || info_ptr == NULL)
471
0
      return;
472
473
74
   info_ptr->x_offset = offset_x;
474
74
   info_ptr->y_offset = offset_y;
475
74
   info_ptr->offset_unit_type = (png_byte)unit_type;
476
74
   info_ptr->valid |= PNG_INFO_oFFs;
477
74
}
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
0
{
486
0
   size_t length;
487
0
   int i;
488
489
0
   png_debug1(1, "in %s storage function", "pCAL");
490
491
0
   if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
492
0
       || (nparams > 0 && params == NULL))
493
0
      return;
494
495
0
   length = strlen(purpose) + 1;
496
0
   png_debug1(3, "allocating purpose for info (%lu bytes)",
497
0
       (unsigned long)length);
498
499
   /* TODO: validate format of calibration name and unit name */
500
501
   /* Check that the type matches the specification. */
502
0
   if (type < 0 || type > 3)
503
0
   {
504
0
      png_chunk_report(png_ptr, "Invalid pCAL equation type",
505
0
            PNG_CHUNK_WRITE_ERROR);
506
0
      return;
507
0
   }
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
424
{
726
424
   png_debug1(1, "in %s storage function", "pHYs");
727
728
424
   if (png_ptr == NULL || info_ptr == NULL)
729
0
      return;
730
731
424
   info_ptr->x_pixels_per_unit = res_x;
732
424
   info_ptr->y_pixels_per_unit = res_y;
733
424
   info_ptr->phys_unit_type = (png_byte)unit_type;
734
424
   info_ptr->valid |= PNG_INFO_pHYs;
735
424
}
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
2.87k
{
742
743
2.87k
   png_uint_32 max_palette_length;
744
745
2.87k
   png_debug1(1, "in %s storage function", "PLTE");
746
747
2.87k
   if (png_ptr == NULL || info_ptr == NULL)
748
0
      return;
749
750
2.87k
   max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
751
2.55k
      (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
752
753
2.87k
   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
2.87k
   if ((num_palette > 0 && palette == NULL) ||
767
2.87k
      (num_palette == 0
768
50
#        ifdef PNG_MNG_FEATURES_SUPPORTED
769
50
            && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
770
2.87k
#        endif
771
2.87k
      ))
772
10
   {
773
10
      png_error(png_ptr, "Invalid palette");
774
10
   }
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
2.86k
   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
2.86k
   png_ptr->palette = png_voidcast(png_color *, png_calloc(png_ptr,
790
2.86k
       PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
791
792
2.86k
   if (num_palette > 0)
793
2.82k
      memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
794
2.82k
          (sizeof (png_color)));
795
796
2.86k
   info_ptr->palette = png_ptr->palette;
797
2.86k
   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
798
2.86k
   info_ptr->free_me |= PNG_FREE_PLTE;
799
2.86k
   info_ptr->valid |= PNG_INFO_PLTE;
800
2.86k
}
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
145
{
807
145
   png_debug1(1, "in %s storage function", "sBIT");
808
809
145
   if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
810
0
      return;
811
812
145
   info_ptr->sig_bit = *sig_bit;
813
145
   info_ptr->valid |= PNG_INFO_sBIT;
814
145
}
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
211
{
821
211
   png_debug1(1, "in %s storage function", "sRGB");
822
823
211
   if (png_ptr == NULL || info_ptr == NULL)
824
0
      return;
825
826
211
   info_ptr->rendering_intent = srgb_intent;
827
211
   info_ptr->valid |= PNG_INFO_sRGB;
828
211
}
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
18
{
863
18
   char *new_iccp_name;
864
18
   png_byte *new_iccp_profile;
865
18
   size_t length;
866
867
18
   png_debug1(1, "in %s storage function", "iCCP");
868
869
18
   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
870
0
      return;
871
872
18
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
873
0
      png_app_error(png_ptr, "Invalid iCCP compression method");
874
875
18
   length = strlen(name)+1;
876
18
   new_iccp_name = png_voidcast(char *, png_malloc_warn(png_ptr, length));
877
878
18
   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
18
   memcpy(new_iccp_name, name, length);
886
18
   new_iccp_profile = png_voidcast(png_byte *,
887
18
       png_malloc_warn(png_ptr, proflen));
888
889
18
   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
18
   memcpy(new_iccp_profile, profile, proflen);
899
900
18
   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
901
902
18
   info_ptr->iccp_proflen = proflen;
903
18
   info_ptr->iccp_name = new_iccp_name;
904
18
   info_ptr->iccp_profile = new_iccp_profile;
905
18
   info_ptr->free_me |= PNG_FREE_ICCP;
906
18
   info_ptr->valid |= PNG_INFO_iCCP;
907
18
}
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
6.55k
{
915
6.55k
   int ret;
916
6.55k
   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
917
918
6.55k
   if (ret != 0)
919
0
      png_error(png_ptr, "Insufficient memory to store text");
920
6.55k
}
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
51.6k
{
926
51.6k
   int i;
927
928
51.6k
   png_debug1(1, "in text storage function, chunk typeid = 0x%lx",
929
51.6k
      png_ptr == NULL ? 0xabadca11UL : (unsigned long)png_ptr->chunk_name);
930
931
51.6k
   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
51.6k
   if (num_text > info_ptr->max_text - info_ptr->num_text)
940
8.83k
   {
941
8.83k
      int old_num_text = info_ptr->num_text;
942
8.83k
      int max_text;
943
8.83k
      png_text *new_text = NULL;
944
945
      /* Calculate an appropriate max_text, checking for overflow. */
946
8.83k
      max_text = old_num_text;
947
8.83k
      if (num_text <= INT_MAX - max_text)
948
8.83k
      {
949
8.83k
         max_text += num_text;
950
951
         /* Round up to a multiple of 8 */
952
8.83k
         if (max_text < INT_MAX-8)
953
8.83k
            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
8.83k
         new_text = png_voidcast(png_text *,png_realloc_array(png_ptr,
962
8.83k
             info_ptr->text, old_num_text, max_text-old_num_text,
963
8.83k
             sizeof *new_text));
964
8.83k
      }
965
966
8.83k
      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
8.83k
      png_free(png_ptr, info_ptr->text);
975
976
8.83k
      info_ptr->text = new_text;
977
8.83k
      info_ptr->free_me |= PNG_FREE_TEXT;
978
8.83k
      info_ptr->max_text = max_text;
979
      /* num_text is adjusted below as the entries are copied in */
980
981
8.83k
      png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
982
8.83k
   }
983
984
103k
   for (i = 0; i < num_text; i++)
985
51.6k
   {
986
51.6k
      size_t text_length, key_len;
987
51.6k
      size_t lang_len, lang_key_len;
988
51.6k
      png_text *textp = &(info_ptr->text[info_ptr->num_text]);
989
990
51.6k
      if (text_ptr[i].key == NULL)
991
0
          continue;
992
993
51.6k
      if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
994
51.6k
          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
51.6k
      key_len = strlen(text_ptr[i].key);
1002
1003
51.6k
      if (text_ptr[i].compression <= 0)
1004
51.5k
      {
1005
51.5k
         lang_len = 0;
1006
51.5k
         lang_key_len = 0;
1007
51.5k
      }
1008
1009
54
      else
1010
54
#  ifdef PNG_iTXt_SUPPORTED
1011
54
      {
1012
         /* Set iTXt data */
1013
1014
54
         if (text_ptr[i].lang != NULL)
1015
0
            lang_len = strlen(text_ptr[i].lang);
1016
1017
54
         else
1018
54
            lang_len = 0;
1019
1020
54
         if (text_ptr[i].lang_key != NULL)
1021
0
            lang_key_len = strlen(text_ptr[i].lang_key);
1022
1023
54
         else
1024
54
            lang_key_len = 0;
1025
54
      }
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
51.6k
      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
1035
31.2k
      {
1036
31.2k
         text_length = 0;
1037
31.2k
#  ifdef PNG_iTXt_SUPPORTED
1038
31.2k
         if (text_ptr[i].compression > 0)
1039
0
            textp->compression = PNG_ITXT_COMPRESSION_NONE;
1040
1041
31.2k
         else
1042
31.2k
#  endif
1043
31.2k
            textp->compression = PNG_TEXT_COMPRESSION_NONE;
1044
31.2k
      }
1045
1046
20.3k
      else
1047
20.3k
      {
1048
20.3k
         text_length = strlen(text_ptr[i].text);
1049
20.3k
         textp->compression = text_ptr[i].compression;
1050
20.3k
      }
1051
1052
51.6k
      textp->key = png_voidcast(char *,png_malloc_base(png_ptr,
1053
51.6k
          key_len + text_length + lang_len + lang_key_len + 4));
1054
1055
51.6k
      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
51.6k
      png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
1064
51.6k
          (unsigned long)(png_uint_32)
1065
51.6k
          (key_len + lang_len + lang_key_len + text_length + 4),
1066
51.6k
          textp->key);
1067
1068
51.6k
      memcpy(textp->key, text_ptr[i].key, key_len);
1069
51.6k
      *(textp->key + key_len) = '\0';
1070
1071
51.6k
      if (text_ptr[i].compression > 0)
1072
54
      {
1073
54
         textp->lang = textp->key + key_len + 1;
1074
54
         memcpy(textp->lang, text_ptr[i].lang, lang_len);
1075
54
         *(textp->lang + lang_len) = '\0';
1076
54
         textp->lang_key = textp->lang + lang_len + 1;
1077
54
         memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
1078
54
         *(textp->lang_key + lang_key_len) = '\0';
1079
54
         textp->text = textp->lang_key + lang_key_len + 1;
1080
54
      }
1081
1082
51.5k
      else
1083
51.5k
      {
1084
51.5k
         textp->lang=NULL;
1085
51.5k
         textp->lang_key=NULL;
1086
51.5k
         textp->text = textp->key + key_len + 1;
1087
51.5k
      }
1088
1089
51.6k
      if (text_length != 0)
1090
20.3k
         memcpy(textp->text, text_ptr[i].text, text_length);
1091
1092
51.6k
      *(textp->text + text_length) = '\0';
1093
1094
51.6k
#  ifdef PNG_iTXt_SUPPORTED
1095
51.6k
      if (textp->compression > 0)
1096
54
      {
1097
54
         textp->text_length = 0;
1098
54
         textp->itxt_length = text_length;
1099
54
      }
1100
1101
51.5k
      else
1102
51.5k
#  endif
1103
51.5k
      {
1104
51.5k
         textp->text_length = text_length;
1105
51.5k
         textp->itxt_length = 0;
1106
51.5k
      }
1107
1108
51.6k
      info_ptr->num_text++;
1109
51.6k
      png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
1110
51.6k
   }
1111
1112
51.6k
   return 0;
1113
51.6k
}
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
2.00k
{
1121
2.00k
   png_debug1(1, "in %s storage function", "tIME");
1122
1123
2.00k
   if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
1124
2.00k
       (png_ptr->mode & PNG_WROTE_tIME) != 0)
1125
0
      return;
1126
1127
2.00k
   if (mod_time->month == 0   || mod_time->month > 12  ||
1128
1.94k
       mod_time->day   == 0   || mod_time->day   > 31  ||
1129
1.92k
       mod_time->hour  > 23   || mod_time->minute > 59 ||
1130
1.89k
       mod_time->second > 60)
1131
124
   {
1132
124
      png_warning(png_ptr, "Ignoring invalid time value");
1133
1134
124
      return;
1135
124
   }
1136
1137
1.88k
   info_ptr->mod_time = *mod_time;
1138
1.88k
   info_ptr->valid |= PNG_INFO_tIME;
1139
1.88k
}
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
1.69k
{
1147
1.69k
   png_debug1(1, "in %s storage function", "tRNS");
1148
1149
1.69k
   if (png_ptr == NULL || info_ptr == NULL)
1150
1151
0
      return;
1152
1153
1.69k
   if (trans_alpha != NULL)
1154
1.61k
   {
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
1.61k
       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
1165
1166
1.61k
       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
1167
1.60k
       {
1168
         /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
1169
1.60k
          info_ptr->trans_alpha = png_voidcast(png_byte *,
1170
1.60k
              png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
1171
1.60k
          memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
1172
1173
1.60k
          info_ptr->free_me |= PNG_FREE_TRNS;
1174
1.60k
          info_ptr->valid |= PNG_INFO_tRNS;
1175
1.60k
       }
1176
1.61k
       png_ptr->trans_alpha = info_ptr->trans_alpha;
1177
1.61k
   }
1178
1179
1.69k
   if (trans_color != NULL)
1180
1.62k
   {
1181
1.62k
#ifdef PNG_WARNINGS_SUPPORTED
1182
1.62k
      if (info_ptr->bit_depth < 16)
1183
1.18k
      {
1184
1.18k
         int sample_max = (1 << info_ptr->bit_depth) - 1;
1185
1186
1.18k
         if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
1187
410
             trans_color->gray > sample_max) ||
1188
1.09k
             (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
1189
492
             (trans_color->red > sample_max ||
1190
439
             trans_color->green > sample_max ||
1191
405
             trans_color->blue > sample_max)))
1192
237
            png_warning(png_ptr,
1193
237
                "tRNS chunk has out-of-range samples for bit_depth");
1194
1.18k
      }
1195
1.62k
#endif
1196
1197
1.62k
      info_ptr->trans_color = *trans_color;
1198
1199
1.62k
      if (num_trans == 0)
1200
89
         num_trans = 1;
1201
1.62k
   }
1202
1203
1.69k
   info_ptr->num_trans = (png_uint_16)num_trans;
1204
1205
1.69k
   if (num_trans != 0)
1206
1.69k
   {
1207
1.69k
      info_ptr->free_me |= PNG_FREE_TRNS;
1208
1.69k
      info_ptr->valid |= PNG_INFO_tRNS;
1209
1.69k
   }
1210
1.69k
}
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
0
{
1226
0
   png_sPLT_t *np;
1227
1228
0
   png_debug1(1, "in %s storage function", "sPLT");
1229
1230
0
   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
0
   np = png_voidcast(png_sPLT_t *,png_realloc_array(png_ptr,
1237
0
       info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
1238
0
       sizeof *np));
1239
1240
0
   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
0
   png_free(png_ptr, info_ptr->splt_palettes);
1248
1249
0
   info_ptr->splt_palettes = np;
1250
0
   info_ptr->free_me |= PNG_FREE_SPLT;
1251
1252
0
   np += info_ptr->splt_palettes_num;
1253
1254
0
   do
1255
0
   {
1256
0
      size_t length;
1257
1258
      /* Skip invalid input entries */
1259
0
      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
0
      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
0
      length = strlen(entries->name) + 1;
1273
0
      np->name = png_voidcast(char *, png_malloc_base(png_ptr, length));
1274
1275
0
      if (np->name == NULL)
1276
0
         break;
1277
1278
0
      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
0
      np->entries = png_voidcast(png_sPLT_entry *, png_malloc_array(png_ptr,
1285
0
          entries->nentries, sizeof (png_sPLT_entry)));
1286
1287
0
      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
0
      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
0
      memcpy(np->entries, entries->entries,
1299
0
          (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
0
      info_ptr->valid |= PNG_INFO_sPLT;
1305
0
      ++(info_ptr->splt_palettes_num);
1306
0
      ++np;
1307
0
      ++entries;
1308
0
   }
1309
0
   while (--nentries);
1310
1311
0
   if (nentries > 0)
1312
0
      png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
1313
0
}
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
0
{
1321
0
   png_debug1(1, "in %s storage function", "acTL");
1322
1323
0
   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
0
   if (num_frames == 0)
1330
0
   {
1331
0
      png_warning(png_ptr,
1332
0
                  "Ignoring attempt to set acTL with num_frames zero");
1333
0
      return 0;
1334
0
   }
1335
0
   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
0
   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
0
   info_ptr->num_frames = num_frames;
1349
0
   info_ptr->num_plays = num_plays;
1350
1351
0
   info_ptr->valid |= PNG_INFO_acTL;
1352
1353
0
   return 1;
1354
0
}
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
84.5k
{
1458
84.5k
   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
84.5k
   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
84.5k
   if (location == 0)
1478
2.20k
      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
91.5k
   while (location != (location & -location))
1484
9.17k
      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
82.3k
   return (png_byte)location;
1490
84.5k
}
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
84.5k
{
1496
84.5k
   png_unknown_chunk *np;
1497
1498
84.5k
   if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
1499
84.5k
       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
84.5k
   np = png_voidcast(png_unknown_chunk *, png_realloc_array(png_ptr,
1533
84.5k
       info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
1534
84.5k
       sizeof *np));
1535
1536
84.5k
   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
84.5k
   png_free(png_ptr, info_ptr->unknown_chunks);
1544
1545
84.5k
   info_ptr->unknown_chunks = np; /* safe because it is initialized */
1546
84.5k
   info_ptr->free_me |= PNG_FREE_UNKN;
1547
1548
84.5k
   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
169k
   for (; num_unknowns > 0; --num_unknowns, ++unknowns)
1554
84.5k
   {
1555
84.5k
      memcpy(np->name, unknowns->name, (sizeof np->name));
1556
84.5k
      np->name[(sizeof np->name)-1] = '\0';
1557
84.5k
      np->location = check_location(png_ptr, unknowns->location);
1558
1559
84.5k
      if (unknowns->size == 0)
1560
46.2k
      {
1561
46.2k
         np->data = NULL;
1562
46.2k
         np->size = 0;
1563
46.2k
      }
1564
1565
38.3k
      else
1566
38.3k
      {
1567
38.3k
         np->data = png_voidcast(png_byte *,
1568
38.3k
             png_malloc_base(png_ptr, unknowns->size));
1569
1570
38.3k
         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
38.3k
         memcpy(np->data, unknowns->data, unknowns->size);
1579
38.3k
         np->size = unknowns->size;
1580
38.3k
      }
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
84.5k
      ++np;
1587
84.5k
      ++(info_ptr->unknown_chunks_num);
1588
84.5k
   }
1589
84.5k
}
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
22.1k
{
1624
22.1k
   png_debug(1, "in png_permit_mng_features");
1625
1626
22.1k
   if (png_ptr == NULL)
1627
0
      return 0;
1628
1629
22.1k
   png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
1630
1631
22.1k
   return png_ptr->mng_features_permitted;
1632
22.1k
}
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
936k
{
1639
936k
   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
5.15M
   for (i=0; i<count; ++i, list += 5)
1645
4.21M
   {
1646
4.21M
      if (memcmp(list, add, 4) == 0)
1647
0
      {
1648
0
         list[4] = (png_byte)keep;
1649
1650
0
         return count;
1651
0
      }
1652
4.21M
   }
1653
1654
936k
   if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1655
936k
   {
1656
936k
      ++count;
1657
936k
      memcpy(list, add, 4);
1658
936k
      list[4] = (png_byte)keep;
1659
936k
   }
1660
1661
936k
   return count;
1662
936k
}
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
374k
{
1668
374k
   png_byte *new_list;
1669
374k
   unsigned int num_chunks, old_num_chunks;
1670
1671
374k
   if (png_ptr == NULL)
1672
0
      return;
1673
1674
374k
   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
374k
   if (num_chunks_in <= 0)
1682
93.6k
   {
1683
93.6k
      png_ptr->unknown_default = keep;
1684
1685
      /* '0' means just set the flags, so stop here */
1686
93.6k
      if (num_chunks_in == 0)
1687
93.6k
        return;
1688
93.6k
   }
1689
1690
281k
   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
281k
   else /* num_chunks_in > 0 */
1727
281k
   {
1728
281k
      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
281k
      num_chunks = (unsigned int)num_chunks_in;
1739
281k
   }
1740
1741
281k
   old_num_chunks = png_ptr->num_chunk_list;
1742
281k
   if (png_ptr->chunk_list == NULL)
1743
93.6k
      old_num_chunks = 0;
1744
1745
   /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1746
    */
1747
281k
   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
281k
   if (keep != 0)
1759
281k
   {
1760
281k
      new_list = png_voidcast(png_byte *, png_malloc(png_ptr,
1761
281k
          5 * (num_chunks + old_num_chunks)));
1762
1763
281k
      if (old_num_chunks > 0)
1764
187k
         memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
1765
281k
   }
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
281k
   if (new_list != NULL)
1779
281k
   {
1780
281k
      const png_byte *inlist;
1781
281k
      png_byte *outlist;
1782
281k
      unsigned int i;
1783
1784
1.21M
      for (i=0; i<num_chunks; ++i)
1785
936k
      {
1786
936k
         old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1787
936k
             chunk_list+5*i, keep);
1788
936k
      }
1789
1790
      /* Now remove any spurious 'default' entries. */
1791
281k
      num_chunks = 0;
1792
1.49M
      for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
1793
1.21M
      {
1794
1.21M
         if (inlist[4])
1795
1.21M
         {
1796
1.21M
            if (outlist != inlist)
1797
0
               memcpy(outlist, inlist, 5);
1798
1.21M
            outlist += 5;
1799
1.21M
            ++num_chunks;
1800
1.21M
         }
1801
1.21M
      }
1802
1803
      /* This means the application has removed all the specialized handling. */
1804
281k
      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
281k
   }
1812
1813
0
   else
1814
0
      num_chunks = 0;
1815
1816
281k
   png_ptr->num_chunk_list = num_chunks;
1817
1818
281k
   if (png_ptr->chunk_list != new_list)
1819
281k
   {
1820
281k
      if (png_ptr->chunk_list != NULL)
1821
187k
         png_free(png_ptr, png_ptr->chunk_list);
1822
1823
281k
      png_ptr->chunk_list = new_list;
1824
281k
   }
1825
281k
}
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
93.6k
{
1833
93.6k
   png_debug(1, "in png_set_read_user_chunk_fn");
1834
1835
93.6k
   if (png_ptr == NULL)
1836
0
      return;
1837
1838
93.6k
   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1839
93.6k
   png_ptr->user_chunk_ptr = user_chunk_ptr;
1840
93.6k
}
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
0
{
1848
0
   png_debug(1, "in png_set_rows");
1849
1850
0
   if (png_ptr == NULL || info_ptr == NULL)
1851
0
      return;
1852
1853
0
   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
0
   info_ptr->row_pointers = row_pointers;
1858
1859
0
   if (row_pointers != NULL)
1860
0
      info_ptr->valid |= PNG_INFO_IDAT;
1861
0
}
1862
#endif
1863
1864
void
1865
png_set_compression_buffer_size(png_struct *png_ptr, size_t size)
1866
2.38k
{
1867
2.38k
   png_debug(1, "in png_set_compression_buffer_size");
1868
1869
2.38k
   if (png_ptr == NULL)
1870
0
      return;
1871
1872
2.38k
   if (size == 0 || size > PNG_UINT_31_MAX)
1873
0
      png_error(png_ptr, "invalid compression buffer size");
1874
1875
2.38k
#  ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1876
2.38k
   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
2.38k
#  endif
1882
1883
2.38k
#  ifdef PNG_WRITE_SUPPORTED
1884
2.38k
   if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1885
2.38k
   {
1886
2.38k
      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
2.38k
#ifndef __COVERITY__
1895
      /* Some compilers complain that this is always false.  However, it
1896
       * can be true when integer overflow happens.
1897
       */
1898
2.38k
      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
2.38k
#endif
1905
1906
2.38k
      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
2.38k
      if (png_ptr->zbuffer_size != size)
1918
2.38k
      {
1919
2.38k
         png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1920
2.38k
         png_ptr->zbuffer_size = (uInt)size;
1921
2.38k
      }
1922
2.38k
   }
1923
2.38k
#  endif
1924
2.38k
}
1925
1926
void
1927
png_set_invalid(const png_struct *png_ptr, png_info *info_ptr, int mask)
1928
212
{
1929
212
   if (png_ptr != NULL && info_ptr != NULL)
1930
212
      info_ptr->valid &= (unsigned int)(~mask);
1931
212
}
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
96.0k
{
1940
96.0k
   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
96.0k
   if (png_ptr == NULL)
1947
0
      return;
1948
1949
96.0k
   png_ptr->user_width_max = user_width_max;
1950
96.0k
   png_ptr->user_height_max = user_height_max;
1951
96.0k
}
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
93.6k
{
1957
93.6k
   png_debug(1, "in png_set_chunk_cache_max");
1958
1959
93.6k
   if (png_ptr != NULL)
1960
93.6k
      png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1961
93.6k
}
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
96.0k
{
1996
96.0k
   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
96.0k
   if (allowed != 0)
2005
96.0k
      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
2006
96.0k
         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
96.0k
}
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
94.0k
{
2026
94.0k
   png_debug(1, "in png_set_check_for_invalid_index");
2027
2028
94.0k
   if (allowed > 0)
2029
0
      png_ptr->num_palette_max = 0;
2030
2031
94.0k
   else
2032
94.0k
      png_ptr->num_palette_max = -1;
2033
94.0k
}
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
6.57k
{
2051
6.57k
#ifdef PNG_WARNINGS_SUPPORTED
2052
6.57k
   const char *orig_key = key;
2053
6.57k
#endif
2054
6.57k
   png_uint_32 key_len = 0;
2055
6.57k
   int bad_character = 0;
2056
6.57k
   int space = 1;
2057
2058
6.57k
   png_debug(1, "in png_check_keyword");
2059
2060
6.57k
   if (key == NULL)
2061
0
   {
2062
0
      *new_key = 0;
2063
0
      return 0;
2064
0
   }
2065
2066
82.2k
   while (*key && key_len < 79)
2067
75.7k
   {
2068
75.7k
      png_byte ch = (png_byte)*key++;
2069
2070
75.7k
      if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
2071
75.3k
      {
2072
75.3k
         *new_key++ = ch; ++key_len; space = 0;
2073
75.3k
      }
2074
2075
341
      else if (space == 0)
2076
329
      {
2077
         /* A space or an invalid character when one wasn't seen immediately
2078
          * before; output just a space.
2079
          */
2080
329
         *new_key++ = 32; ++key_len; space = 1;
2081
2082
         /* If the character was not a space then it is invalid. */
2083
329
         if (ch != 32)
2084
275
            bad_character = ch;
2085
329
      }
2086
2087
12
      else if (bad_character == 0)
2088
0
         bad_character = ch; /* just skip it, record the first error */
2089
75.7k
   }
2090
2091
6.57k
   if (key_len > 0 && space != 0) /* trailing space */
2092
272
   {
2093
272
      --key_len; --new_key;
2094
272
      if (bad_character == 0)
2095
0
         bad_character = 32;
2096
272
   }
2097
2098
   /* Terminate the keyword */
2099
6.57k
   *new_key = 0;
2100
2101
6.57k
   if (key_len == 0)
2102
0
      return 0;
2103
2104
6.57k
#ifdef PNG_WARNINGS_SUPPORTED
2105
   /* Try to only output one warning per keyword: */
2106
6.57k
   if (*key != 0) /* keyword too long */
2107
0
      png_warning(png_ptr, "keyword truncated");
2108
2109
6.57k
   else if (bad_character != 0)
2110
275
   {
2111
275
      PNG_WARNING_PARAMETERS(p)
2112
2113
275
      png_warning_parameter(p, 1, orig_key);
2114
275
      png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
2115
2116
275
      png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
2117
275
   }
2118
#else /* !WARNINGS */
2119
   PNG_UNUSED(png_ptr)
2120
#endif /* !WARNINGS */
2121
2122
6.57k
   return key_len;
2123
6.57k
}
2124
#endif /* TEXT || pCAL || iCCP || sPLT */
2125
#endif /* READ || WRITE */