Coverage Report

Created: 2025-06-16 07:00

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