Coverage Report

Created: 2025-06-22 07:10

/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
6
{
27
6
   png_debug1(1, "in %s storage function", "bKGD");
28
29
6
   if (png_ptr == NULL || info_ptr == NULL || background == NULL)
30
0
      return;
31
32
6
   info_ptr->background = *background;
33
6
   info_ptr->valid |= PNG_INFO_bKGD;
34
6
}
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
25
{
44
25
   png_debug1(1, "in %s storage function", "cHRM fixed");
45
46
25
   if (png_ptr == NULL || info_ptr == NULL)
47
0
      return;
48
49
25
   info_ptr->cHRM.redx = red_x;
50
25
   info_ptr->cHRM.redy = red_y;
51
25
   info_ptr->cHRM.greenx = green_x;
52
25
   info_ptr->cHRM.greeny = green_y;
53
25
   info_ptr->cHRM.bluex = blue_x;
54
25
   info_ptr->cHRM.bluey = blue_y;
55
25
   info_ptr->cHRM.whitex = white_x;
56
25
   info_ptr->cHRM.whitey = white_y;
57
58
25
   info_ptr->valid |= PNG_INFO_cHRM;
59
25
}
60
61
void PNGFAPI
62
png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
63
    png_fixed_point int_red_X, png_fixed_point int_red_Y,
64
    png_fixed_point int_red_Z, png_fixed_point int_green_X,
65
    png_fixed_point int_green_Y, png_fixed_point int_green_Z,
66
    png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
67
    png_fixed_point int_blue_Z)
68
0
{
69
0
   png_XYZ XYZ;
70
0
   png_xy xy;
71
72
0
   png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
73
74
0
   if (png_ptr == NULL || info_ptr == NULL)
75
0
      return;
76
77
0
   XYZ.red_X = int_red_X;
78
0
   XYZ.red_Y = int_red_Y;
79
0
   XYZ.red_Z = int_red_Z;
80
0
   XYZ.green_X = int_green_X;
81
0
   XYZ.green_Y = int_green_Y;
82
0
   XYZ.green_Z = int_green_Z;
83
0
   XYZ.blue_X = int_blue_X;
84
0
   XYZ.blue_Y = int_blue_Y;
85
0
   XYZ.blue_Z = int_blue_Z;
86
87
0
   if (png_xy_from_XYZ(&xy, &XYZ) == 0)
88
0
   {
89
0
      info_ptr->cHRM = xy;
90
0
      info_ptr->valid |= PNG_INFO_cHRM;
91
0
   }
92
93
0
   else
94
0
      png_app_error(png_ptr, "invalid cHRM XYZ");
95
0
}
96
97
#  ifdef PNG_FLOATING_POINT_SUPPORTED
98
void PNGAPI
99
png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
100
    double white_x, double white_y, double red_x, double red_y,
101
    double green_x, double green_y, double blue_x, double blue_y)
102
0
{
103
0
   png_set_cHRM_fixed(png_ptr, info_ptr,
104
0
       png_fixed(png_ptr, white_x, "cHRM White X"),
105
0
       png_fixed(png_ptr, white_y, "cHRM White Y"),
106
0
       png_fixed(png_ptr, red_x, "cHRM Red X"),
107
0
       png_fixed(png_ptr, red_y, "cHRM Red Y"),
108
0
       png_fixed(png_ptr, green_x, "cHRM Green X"),
109
0
       png_fixed(png_ptr, green_y, "cHRM Green Y"),
110
0
       png_fixed(png_ptr, blue_x, "cHRM Blue X"),
111
0
       png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
112
0
}
113
114
void PNGAPI
115
png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
116
    double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
117
    double blue_X, double blue_Y, double blue_Z)
118
0
{
119
0
   png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
120
0
       png_fixed(png_ptr, red_X, "cHRM Red X"),
121
0
       png_fixed(png_ptr, red_Y, "cHRM Red Y"),
122
0
       png_fixed(png_ptr, red_Z, "cHRM Red Z"),
123
0
       png_fixed(png_ptr, green_X, "cHRM Green X"),
124
0
       png_fixed(png_ptr, green_Y, "cHRM Green Y"),
125
0
       png_fixed(png_ptr, green_Z, "cHRM Green Z"),
126
0
       png_fixed(png_ptr, blue_X, "cHRM Blue X"),
127
0
       png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
128
0
       png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
129
0
}
130
#  endif /* FLOATING_POINT */
131
132
#endif /* cHRM */
133
134
#ifdef PNG_cICP_SUPPORTED
135
void PNGAPI
136
png_set_cICP(png_const_structrp png_ptr, png_inforp info_ptr,
137
             png_byte colour_primaries, png_byte transfer_function,
138
             png_byte matrix_coefficients, png_byte video_full_range_flag)
139
0
{
140
0
   png_debug1(1, "in %s storage function", "cICP");
141
142
0
   if (png_ptr == NULL || info_ptr == NULL)
143
0
      return;
144
145
0
   info_ptr->cicp_colour_primaries = colour_primaries;
146
0
   info_ptr->cicp_transfer_function = transfer_function;
147
0
   info_ptr->cicp_matrix_coefficients = matrix_coefficients;
148
0
   info_ptr->cicp_video_full_range_flag = video_full_range_flag;
149
150
0
   if (info_ptr->cicp_matrix_coefficients != 0)
151
0
   {
152
0
      png_warning(png_ptr, "Invalid cICP matrix coefficients");
153
0
      return;
154
0
   }
155
156
0
   info_ptr->valid |= PNG_INFO_cICP;
157
0
}
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
0
{
168
0
   png_debug1(1, "in %s storage function", "cLLI");
169
170
0
   if (png_ptr == NULL || info_ptr == NULL)
171
0
      return;
172
173
   /* Check the light level range: */
174
0
   if (maxCLL > 0x7FFFFFFFU || maxFALL > 0x7FFFFFFFU)
175
0
   {
176
      /* The limit is 200kcd/m2; somewhat bright but not inconceivable because
177
       * human vision is said to run up to 100Mcd/m2.  The sun is about 2Gcd/m2.
178
       *
179
       * The reference sRGB monitor is 80cd/m2 and the limit of PQ encoding is
180
       * 2kcd/m2.
181
       */
182
0
      png_chunk_report(png_ptr, "cLLI light level exceeds PNG limit",
183
0
            PNG_CHUNK_WRITE_ERROR);
184
0
      return;
185
0
   }
186
187
0
   info_ptr->maxCLL = maxCLL;
188
0
   info_ptr->maxFALL = maxFALL;
189
0
   info_ptr->valid |= PNG_INFO_cLLI;
190
0
}
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
24
{
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
24
   v /= 2; /* rounds to 0 in C: avoids insignificant arithmetic errors */
215
24
   if (v > 65535 || v < 0)
216
0
   {
217
0
      *error = 1;
218
0
      return 0;
219
0
   }
220
221
24
   return (png_uint_16)/*SAFE*/v;
222
24
}
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
3
{
233
3
   png_uint_16 rx, ry, gx, gy, bx, by, wx, wy;
234
3
   int error;
235
236
3
   png_debug1(1, "in %s storage function", "mDCV");
237
238
3
   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
3
   error = 0;
243
3
   rx = png_ITU_fixed_16(&error, red_x);
244
3
   ry = png_ITU_fixed_16(&error, red_y);
245
3
   gx = png_ITU_fixed_16(&error, green_x);
246
3
   gy = png_ITU_fixed_16(&error, green_y);
247
3
   bx = png_ITU_fixed_16(&error, blue_x);
248
3
   by = png_ITU_fixed_16(&error, blue_y);
249
3
   wx = png_ITU_fixed_16(&error, white_x);
250
3
   wy = png_ITU_fixed_16(&error, white_y);
251
252
3
   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
3
   if (maxDL > 0x7FFFFFFFU || minDL > 0x7FFFFFFFU)
262
1
   {
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
1
      png_chunk_report(png_ptr, "mDCV display light level exceeds PNG limit",
270
1
            PNG_CHUNK_WRITE_ERROR);
271
1
      return;
272
1
   }
273
274
   /* All values are safe, the settings are accepted.
275
    *
276
    * IMPLEMENTATION NOTE: in practice the values can be checked and assigned
277
    * but the result is confusing if a writing app calls png_set_mDCV more than
278
    * once, the second time with an invalid value.  This approach is more
279
    * obviously correct at the cost of typing and a very slight machine
280
    * overhead.
281
    */
282
2
   info_ptr->mastering_red_x = rx;
283
2
   info_ptr->mastering_red_y = ry;
284
2
   info_ptr->mastering_green_x = gx;
285
2
   info_ptr->mastering_green_y = gy;
286
2
   info_ptr->mastering_blue_x = bx;
287
2
   info_ptr->mastering_blue_y = by;
288
2
   info_ptr->mastering_white_x = wx;
289
2
   info_ptr->mastering_white_y = wy;
290
2
   info_ptr->mastering_maxDL = maxDL;
291
2
   info_ptr->mastering_minDL = minDL;
292
2
   info_ptr->valid |= PNG_INFO_mDCV;
293
2
}
294
295
#  ifdef PNG_FLOATING_POINT_SUPPORTED
296
void 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
0
{
331
0
   png_bytep new_exif;
332
333
0
   png_debug1(1, "in %s storage function", "eXIf");
334
335
0
   if (png_ptr == NULL || info_ptr == NULL ||
336
0
       (png_ptr->mode & PNG_WROTE_eXIf) != 0)
337
0
      return;
338
339
0
   new_exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr, num_exif));
340
341
0
   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
0
   memcpy(new_exif, exif, (size_t)num_exif);
348
349
0
   png_free_data(png_ptr, info_ptr, PNG_FREE_EXIF, 0);
350
351
0
   info_ptr->num_exif = num_exif;
352
0
   info_ptr->exif = new_exif;
353
0
   info_ptr->free_me |= PNG_FREE_EXIF;
354
0
   info_ptr->valid |= PNG_INFO_eXIf;
355
0
}
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
59
{
363
59
   png_debug1(1, "in %s storage function", "gAMA");
364
365
59
   if (png_ptr == NULL || info_ptr == NULL)
366
0
      return;
367
368
59
   info_ptr->gamma = file_gamma;
369
59
   info_ptr->valid |= PNG_INFO_gAMA;
370
59
}
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
0
{
376
0
   png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
377
0
       "png_set_gAMA"));
378
0
}
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
5
{
387
5
   int i;
388
389
5
   png_debug1(1, "in %s storage function", "hIST");
390
391
5
   if (png_ptr == NULL || info_ptr == NULL)
392
0
      return;
393
394
5
   if (info_ptr->num_palette == 0 || info_ptr->num_palette
395
0
       > PNG_MAX_PALETTE_LENGTH)
396
5
   {
397
5
      png_warning(png_ptr,
398
5
          "Invalid palette size, hIST allocation skipped");
399
400
5
      return;
401
5
   }
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
79.2k
{
431
79.2k
   png_debug1(1, "in %s storage function", "IHDR");
432
433
79.2k
   if (png_ptr == NULL || info_ptr == NULL)
434
0
      return;
435
436
79.2k
   info_ptr->width = width;
437
79.2k
   info_ptr->height = height;
438
79.2k
   info_ptr->bit_depth = (png_byte)bit_depth;
439
79.2k
   info_ptr->color_type = (png_byte)color_type;
440
79.2k
   info_ptr->compression_type = (png_byte)compression_type;
441
79.2k
   info_ptr->filter_type = (png_byte)filter_type;
442
79.2k
   info_ptr->interlace_type = (png_byte)interlace_type;
443
444
79.2k
   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
445
79.2k
       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
446
79.2k
       info_ptr->compression_type, info_ptr->filter_type);
447
448
79.2k
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
449
500
      info_ptr->channels = 1;
450
451
78.7k
   else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
452
1.09k
      info_ptr->channels = 3;
453
454
77.6k
   else
455
77.6k
      info_ptr->channels = 1;
456
457
79.2k
   if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
458
135
      info_ptr->channels++;
459
460
79.2k
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
461
462
79.2k
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
463
79.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
15
{
470
15
   png_debug1(1, "in %s storage function", "oFFs");
471
472
15
   if (png_ptr == NULL || info_ptr == NULL)
473
0
      return;
474
475
15
   info_ptr->x_offset = offset_x;
476
15
   info_ptr->y_offset = offset_y;
477
15
   info_ptr->offset_unit_type = (png_byte)unit_type;
478
15
   info_ptr->valid |= PNG_INFO_oFFs;
479
15
}
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
2
{
488
2
   size_t length;
489
2
   int i;
490
491
2
   png_debug1(1, "in %s storage function", "pCAL");
492
493
2
   if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
494
2
       || (nparams > 0 && params == NULL))
495
0
      return;
496
497
2
   length = strlen(purpose) + 1;
498
2
   png_debug1(3, "allocating purpose for info (%lu bytes)",
499
2
       (unsigned long)length);
500
501
   /* TODO: validate format of calibration name and unit name */
502
503
   /* Check that the type matches the specification. */
504
2
   if (type < 0 || type > 3)
505
2
   {
506
2
      png_chunk_report(png_ptr, "Invalid pCAL equation type",
507
2
            PNG_CHUNK_WRITE_ERROR);
508
2
      return;
509
2
   }
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
61.0k
{
728
61.0k
   png_debug1(1, "in %s storage function", "pHYs");
729
730
61.0k
   if (png_ptr == NULL || info_ptr == NULL)
731
0
      return;
732
733
61.0k
   info_ptr->x_pixels_per_unit = res_x;
734
61.0k
   info_ptr->y_pixels_per_unit = res_y;
735
61.0k
   info_ptr->phys_unit_type = (png_byte)unit_type;
736
61.0k
   info_ptr->valid |= PNG_INFO_pHYs;
737
61.0k
}
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
461
{
744
745
461
   png_uint_32 max_palette_length;
746
747
461
   png_debug1(1, "in %s storage function", "PLTE");
748
749
461
   if (png_ptr == NULL || info_ptr == NULL)
750
0
      return;
751
752
461
   max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
753
439
      (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
754
755
461
   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
461
   if ((num_palette > 0 && palette == NULL) ||
769
461
      (num_palette == 0
770
461
#        ifdef PNG_MNG_FEATURES_SUPPORTED
771
461
            && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
772
461
#        endif
773
461
      ))
774
2
   {
775
2
      png_error(png_ptr, "Invalid palette");
776
2
   }
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
459
   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
459
   png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
792
459
       PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
793
794
459
   if (num_palette > 0)
795
459
      memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
796
459
          (sizeof (png_color)));
797
798
459
   info_ptr->palette = png_ptr->palette;
799
459
   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
800
459
   info_ptr->free_me |= PNG_FREE_PLTE;
801
459
   info_ptr->valid |= PNG_INFO_PLTE;
802
459
}
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
2
{
809
2
   png_debug1(1, "in %s storage function", "sBIT");
810
811
2
   if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
812
0
      return;
813
814
2
   info_ptr->sig_bit = *sig_bit;
815
2
   info_ptr->valid |= PNG_INFO_sBIT;
816
2
}
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
21
{
823
21
   png_debug1(1, "in %s storage function", "sRGB");
824
825
21
   if (png_ptr == NULL || info_ptr == NULL)
826
0
      return;
827
828
21
   info_ptr->rendering_intent = srgb_intent;
829
21
   info_ptr->valid |= PNG_INFO_sRGB;
830
21
}
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
0
{
865
0
   png_charp new_iccp_name;
866
0
   png_bytep new_iccp_profile;
867
0
   size_t length;
868
869
0
   png_debug1(1, "in %s storage function", "iCCP");
870
871
0
   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
872
0
      return;
873
874
0
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
875
0
      png_app_error(png_ptr, "Invalid iCCP compression method");
876
877
0
   length = strlen(name)+1;
878
0
   new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
879
880
0
   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
0
   memcpy(new_iccp_name, name, length);
888
0
   new_iccp_profile = png_voidcast(png_bytep,
889
0
       png_malloc_warn(png_ptr, proflen));
890
891
0
   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
0
   memcpy(new_iccp_profile, profile, proflen);
901
902
0
   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
903
904
0
   info_ptr->iccp_proflen = proflen;
905
0
   info_ptr->iccp_name = new_iccp_name;
906
0
   info_ptr->iccp_profile = new_iccp_profile;
907
0
   info_ptr->free_me |= PNG_FREE_ICCP;
908
0
   info_ptr->valid |= PNG_INFO_iCCP;
909
0
}
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
0
{
917
0
   int ret;
918
0
   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
919
920
0
   if (ret != 0)
921
0
      png_error(png_ptr, "Insufficient memory to store text");
922
0
}
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
41.9k
{
928
41.9k
   int i;
929
930
41.9k
   png_debug1(1, "in text storage function, chunk typeid = 0x%lx",
931
41.9k
      png_ptr == NULL ? 0xabadca11UL : (unsigned long)png_ptr->chunk_name);
932
933
41.9k
   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
41.9k
   if (num_text > info_ptr->max_text - info_ptr->num_text)
942
37.3k
   {
943
37.3k
      int old_num_text = info_ptr->num_text;
944
37.3k
      int max_text;
945
37.3k
      png_textp new_text = NULL;
946
947
      /* Calculate an appropriate max_text, checking for overflow. */
948
37.3k
      max_text = old_num_text;
949
37.3k
      if (num_text <= INT_MAX - max_text)
950
37.3k
      {
951
37.3k
         max_text += num_text;
952
953
         /* Round up to a multiple of 8 */
954
37.3k
         if (max_text < INT_MAX-8)
955
37.3k
            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
37.3k
         new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
964
37.3k
             info_ptr->text, old_num_text, max_text-old_num_text,
965
37.3k
             sizeof *new_text));
966
37.3k
      }
967
968
37.3k
      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
37.3k
      png_free(png_ptr, info_ptr->text);
977
978
37.3k
      info_ptr->text = new_text;
979
37.3k
      info_ptr->free_me |= PNG_FREE_TEXT;
980
37.3k
      info_ptr->max_text = max_text;
981
      /* num_text is adjusted below as the entries are copied in */
982
983
37.3k
      png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
984
37.3k
   }
985
986
83.8k
   for (i = 0; i < num_text; i++)
987
41.9k
   {
988
41.9k
      size_t text_length, key_len;
989
41.9k
      size_t lang_len, lang_key_len;
990
41.9k
      png_textp textp = &(info_ptr->text[info_ptr->num_text]);
991
992
41.9k
      if (text_ptr[i].key == NULL)
993
0
          continue;
994
995
41.9k
      if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
996
41.9k
          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
41.9k
      key_len = strlen(text_ptr[i].key);
1004
1005
41.9k
      if (text_ptr[i].compression <= 0)
1006
41.9k
      {
1007
41.9k
         lang_len = 0;
1008
41.9k
         lang_key_len = 0;
1009
41.9k
      }
1010
1011
13
      else
1012
13
#  ifdef PNG_iTXt_SUPPORTED
1013
13
      {
1014
         /* Set iTXt data */
1015
1016
13
         if (text_ptr[i].lang != NULL)
1017
13
            lang_len = strlen(text_ptr[i].lang);
1018
1019
0
         else
1020
0
            lang_len = 0;
1021
1022
13
         if (text_ptr[i].lang_key != NULL)
1023
13
            lang_key_len = strlen(text_ptr[i].lang_key);
1024
1025
0
         else
1026
0
            lang_key_len = 0;
1027
13
      }
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
41.9k
      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
1037
4.74k
      {
1038
4.74k
         text_length = 0;
1039
4.74k
#  ifdef PNG_iTXt_SUPPORTED
1040
4.74k
         if (text_ptr[i].compression > 0)
1041
11
            textp->compression = PNG_ITXT_COMPRESSION_NONE;
1042
1043
4.73k
         else
1044
4.73k
#  endif
1045
4.73k
            textp->compression = PNG_TEXT_COMPRESSION_NONE;
1046
4.74k
      }
1047
1048
37.1k
      else
1049
37.1k
      {
1050
37.1k
         text_length = strlen(text_ptr[i].text);
1051
37.1k
         textp->compression = text_ptr[i].compression;
1052
37.1k
      }
1053
1054
41.9k
      textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
1055
41.9k
          key_len + text_length + lang_len + lang_key_len + 4));
1056
1057
41.9k
      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
41.9k
      png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
1066
41.9k
          (unsigned long)(png_uint_32)
1067
41.9k
          (key_len + lang_len + lang_key_len + text_length + 4),
1068
41.9k
          textp->key);
1069
1070
41.9k
      memcpy(textp->key, text_ptr[i].key, key_len);
1071
41.9k
      *(textp->key + key_len) = '\0';
1072
1073
41.9k
      if (text_ptr[i].compression > 0)
1074
13
      {
1075
13
         textp->lang = textp->key + key_len + 1;
1076
13
         memcpy(textp->lang, text_ptr[i].lang, lang_len);
1077
13
         *(textp->lang + lang_len) = '\0';
1078
13
         textp->lang_key = textp->lang + lang_len + 1;
1079
13
         memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
1080
13
         *(textp->lang_key + lang_key_len) = '\0';
1081
13
         textp->text = textp->lang_key + lang_key_len + 1;
1082
13
      }
1083
1084
41.9k
      else
1085
41.9k
      {
1086
41.9k
         textp->lang=NULL;
1087
41.9k
         textp->lang_key=NULL;
1088
41.9k
         textp->text = textp->key + key_len + 1;
1089
41.9k
      }
1090
1091
41.9k
      if (text_length != 0)
1092
37.1k
         memcpy(textp->text, text_ptr[i].text, text_length);
1093
1094
41.9k
      *(textp->text + text_length) = '\0';
1095
1096
41.9k
#  ifdef PNG_iTXt_SUPPORTED
1097
41.9k
      if (textp->compression > 0)
1098
13
      {
1099
13
         textp->text_length = 0;
1100
13
         textp->itxt_length = text_length;
1101
13
      }
1102
1103
41.9k
      else
1104
41.9k
#  endif
1105
41.9k
      {
1106
41.9k
         textp->text_length = text_length;
1107
41.9k
         textp->itxt_length = 0;
1108
41.9k
      }
1109
1110
41.9k
      info_ptr->num_text++;
1111
41.9k
      png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
1112
41.9k
   }
1113
1114
41.9k
   return 0;
1115
41.9k
}
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
4
{
1123
4
   png_debug1(1, "in %s storage function", "tIME");
1124
1125
4
   if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
1126
4
       (png_ptr->mode & PNG_WROTE_tIME) != 0)
1127
0
      return;
1128
1129
4
   if (mod_time->month == 0   || mod_time->month > 12  ||
1130
4
       mod_time->day   == 0   || mod_time->day   > 31  ||
1131
4
       mod_time->hour  > 23   || mod_time->minute > 59 ||
1132
4
       mod_time->second > 60)
1133
4
   {
1134
4
      png_warning(png_ptr, "Ignoring invalid time value");
1135
1136
4
      return;
1137
4
   }
1138
1139
0
   info_ptr->mod_time = *mod_time;
1140
0
   info_ptr->valid |= PNG_INFO_tIME;
1141
0
}
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
338
{
1149
338
   png_debug1(1, "in %s storage function", "tRNS");
1150
1151
338
   if (png_ptr == NULL || info_ptr == NULL)
1152
1153
0
      return;
1154
1155
338
   if (trans_alpha != NULL)
1156
338
   {
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
338
       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
1167
1168
338
       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
1169
338
       {
1170
         /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
1171
338
          info_ptr->trans_alpha = png_voidcast(png_bytep,
1172
338
              png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
1173
338
          memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
1174
1175
338
          info_ptr->free_me |= PNG_FREE_TRNS;
1176
338
          info_ptr->valid |= PNG_INFO_tRNS;
1177
338
       }
1178
338
       png_ptr->trans_alpha = info_ptr->trans_alpha;
1179
338
   }
1180
1181
338
   if (trans_color != NULL)
1182
116
   {
1183
116
#ifdef PNG_WARNINGS_SUPPORTED
1184
116
      if (info_ptr->bit_depth < 16)
1185
116
      {
1186
116
         int sample_max = (1 << info_ptr->bit_depth) - 1;
1187
1188
116
         if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
1189
116
             trans_color->gray > sample_max) ||
1190
116
             (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
1191
72
             (trans_color->red > sample_max ||
1192
6
             trans_color->green > sample_max ||
1193
6
             trans_color->blue > sample_max)))
1194
49
            png_warning(png_ptr,
1195
49
                "tRNS chunk has out-of-range samples for bit_depth");
1196
116
      }
1197
116
#endif
1198
1199
116
      info_ptr->trans_color = *trans_color;
1200
1201
116
      if (num_trans == 0)
1202
0
         num_trans = 1;
1203
116
   }
1204
1205
338
   info_ptr->num_trans = (png_uint_16)num_trans;
1206
1207
338
   if (num_trans != 0)
1208
338
   {
1209
338
      info_ptr->free_me |= PNG_FREE_TRNS;
1210
338
      info_ptr->valid |= PNG_INFO_tRNS;
1211
338
   }
1212
338
}
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
1.46k
{
1228
1.46k
   png_sPLT_tp np;
1229
1230
1.46k
   png_debug1(1, "in %s storage function", "sPLT");
1231
1232
1.46k
   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
1.46k
   np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
1239
1.46k
       info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
1240
1.46k
       sizeof *np));
1241
1242
1.46k
   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
1.46k
   png_free(png_ptr, info_ptr->splt_palettes);
1250
1251
1.46k
   info_ptr->splt_palettes = np;
1252
1.46k
   info_ptr->free_me |= PNG_FREE_SPLT;
1253
1254
1.46k
   np += info_ptr->splt_palettes_num;
1255
1256
1.46k
   do
1257
1.46k
   {
1258
1.46k
      size_t length;
1259
1260
      /* Skip invalid input entries */
1261
1.46k
      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
1.46k
      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
1.46k
      length = strlen(entries->name) + 1;
1275
1.46k
      np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
1276
1277
1.46k
      if (np->name == NULL)
1278
0
         break;
1279
1280
1.46k
      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
1.46k
      np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
1287
1.46k
          entries->nentries, sizeof (png_sPLT_entry)));
1288
1289
1.46k
      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
1.46k
      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
1.46k
      memcpy(np->entries, entries->entries,
1301
1.46k
          (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
1.46k
      info_ptr->valid |= PNG_INFO_sPLT;
1307
1.46k
      ++(info_ptr->splt_palettes_num);
1308
1.46k
      ++np;
1309
1.46k
      ++entries;
1310
1.46k
   }
1311
1.46k
   while (--nentries);
1312
1313
1.46k
   if (nentries > 0)
1314
0
      png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
1315
1.46k
}
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
0
{
1322
0
   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
0
   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
0
   if (location == 0)
1342
0
      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
0
   while (location != (location & -location))
1348
0
      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
0
   return (png_byte)location;
1354
0
}
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
0
{
1360
0
   png_unknown_chunkp np;
1361
1362
0
   if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
1363
0
       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
0
   np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
1397
0
       info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
1398
0
       sizeof *np));
1399
1400
0
   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
0
   png_free(png_ptr, info_ptr->unknown_chunks);
1408
1409
0
   info_ptr->unknown_chunks = np; /* safe because it is initialized */
1410
0
   info_ptr->free_me |= PNG_FREE_UNKN;
1411
1412
0
   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
0
   for (; num_unknowns > 0; --num_unknowns, ++unknowns)
1418
0
   {
1419
0
      memcpy(np->name, unknowns->name, (sizeof np->name));
1420
0
      np->name[(sizeof np->name)-1] = '\0';
1421
0
      np->location = check_location(png_ptr, unknowns->location);
1422
1423
0
      if (unknowns->size == 0)
1424
0
      {
1425
0
         np->data = NULL;
1426
0
         np->size = 0;
1427
0
      }
1428
1429
0
      else
1430
0
      {
1431
0
         np->data = png_voidcast(png_bytep,
1432
0
             png_malloc_base(png_ptr, unknowns->size));
1433
1434
0
         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
0
         memcpy(np->data, unknowns->data, unknowns->size);
1443
0
         np->size = unknowns->size;
1444
0
      }
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
0
      ++np;
1451
0
      ++(info_ptr->unknown_chunks_num);
1452
0
   }
1453
0
}
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
0
{
1488
0
   png_debug(1, "in png_permit_mng_features");
1489
1490
0
   if (png_ptr == NULL)
1491
0
      return 0;
1492
1493
0
   png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
1494
1495
0
   return png_ptr->mng_features_permitted;
1496
0
}
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
0
{
1503
0
   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
0
   for (i=0; i<count; ++i, list += 5)
1509
0
   {
1510
0
      if (memcmp(list, add, 4) == 0)
1511
0
      {
1512
0
         list[4] = (png_byte)keep;
1513
1514
0
         return count;
1515
0
      }
1516
0
   }
1517
1518
0
   if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1519
0
   {
1520
0
      ++count;
1521
0
      memcpy(list, add, 4);
1522
0
      list[4] = (png_byte)keep;
1523
0
   }
1524
1525
0
   return count;
1526
0
}
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
0
{
1532
0
   png_bytep new_list;
1533
0
   unsigned int num_chunks, old_num_chunks;
1534
1535
0
   if (png_ptr == NULL)
1536
0
      return;
1537
1538
0
   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
0
   if (num_chunks_in <= 0)
1546
0
   {
1547
0
      png_ptr->unknown_default = keep;
1548
1549
      /* '0' means just set the flags, so stop here */
1550
0
      if (num_chunks_in == 0)
1551
0
        return;
1552
0
   }
1553
1554
0
   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
0
   else /* num_chunks_in > 0 */
1588
0
   {
1589
0
      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
0
      num_chunks = (unsigned int)num_chunks_in;
1600
0
   }
1601
1602
0
   old_num_chunks = png_ptr->num_chunk_list;
1603
0
   if (png_ptr->chunk_list == NULL)
1604
0
      old_num_chunks = 0;
1605
1606
   /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1607
    */
1608
0
   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
0
   if (keep != 0)
1620
0
   {
1621
0
      new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1622
0
          5 * (num_chunks + old_num_chunks)));
1623
1624
0
      if (old_num_chunks > 0)
1625
0
         memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
1626
0
   }
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
0
   if (new_list != NULL)
1640
0
   {
1641
0
      png_const_bytep inlist;
1642
0
      png_bytep outlist;
1643
0
      unsigned int i;
1644
1645
0
      for (i=0; i<num_chunks; ++i)
1646
0
      {
1647
0
         old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1648
0
             chunk_list+5*i, keep);
1649
0
      }
1650
1651
      /* Now remove any spurious 'default' entries. */
1652
0
      num_chunks = 0;
1653
0
      for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
1654
0
      {
1655
0
         if (inlist[4])
1656
0
         {
1657
0
            if (outlist != inlist)
1658
0
               memcpy(outlist, inlist, 5);
1659
0
            outlist += 5;
1660
0
            ++num_chunks;
1661
0
         }
1662
0
      }
1663
1664
      /* This means the application has removed all the specialized handling. */
1665
0
      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
0
   }
1673
1674
0
   else
1675
0
      num_chunks = 0;
1676
1677
0
   png_ptr->num_chunk_list = num_chunks;
1678
1679
0
   if (png_ptr->chunk_list != new_list)
1680
0
   {
1681
0
      if (png_ptr->chunk_list != NULL)
1682
0
         png_free(png_ptr, png_ptr->chunk_list);
1683
1684
0
      png_ptr->chunk_list = new_list;
1685
0
   }
1686
0
}
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
0
{
1694
0
   png_debug(1, "in png_set_read_user_chunk_fn");
1695
1696
0
   if (png_ptr == NULL)
1697
0
      return;
1698
1699
0
   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1700
0
   png_ptr->user_chunk_ptr = user_chunk_ptr;
1701
0
}
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
1.43k
{
1709
1.43k
   png_debug(1, "in png_set_rows");
1710
1711
1.43k
   if (png_ptr == NULL || info_ptr == NULL)
1712
0
      return;
1713
1714
1.43k
   if (info_ptr->row_pointers != NULL &&
1715
1.43k
       (info_ptr->row_pointers != row_pointers))
1716
0
      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1717
1718
1.43k
   info_ptr->row_pointers = row_pointers;
1719
1720
1.43k
   if (row_pointers != NULL)
1721
1.43k
      info_ptr->valid |= PNG_INFO_IDAT;
1722
1.43k
}
1723
#endif
1724
1725
void PNGAPI
1726
png_set_compression_buffer_size(png_structrp png_ptr, size_t size)
1727
0
{
1728
0
   png_debug(1, "in png_set_compression_buffer_size");
1729
1730
0
   if (png_ptr == NULL)
1731
0
      return;
1732
1733
0
   if (size == 0 || size > PNG_UINT_31_MAX)
1734
0
      png_error(png_ptr, "invalid compression buffer size");
1735
1736
0
#  ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1737
0
   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
0
#  endif
1743
1744
0
#  ifdef PNG_WRITE_SUPPORTED
1745
0
   if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1746
0
   {
1747
0
      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
0
#ifndef __COVERITY__
1756
      /* Some compilers complain that this is always false.  However, it
1757
       * can be true when integer overflow happens.
1758
       */
1759
0
      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
0
#endif
1766
1767
0
      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
0
      if (png_ptr->zbuffer_size != size)
1779
0
      {
1780
0
         png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1781
0
         png_ptr->zbuffer_size = (uInt)size;
1782
0
      }
1783
0
   }
1784
0
#  endif
1785
0
}
1786
1787
void PNGAPI
1788
png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
1789
0
{
1790
0
   if (png_ptr != NULL && info_ptr != NULL)
1791
0
      info_ptr->valid &= (unsigned int)(~mask);
1792
0
}
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
0
{
1801
0
   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
0
   if (png_ptr == NULL)
1808
0
      return;
1809
1810
0
   png_ptr->user_width_max = user_width_max;
1811
0
   png_ptr->user_height_max = user_height_max;
1812
0
}
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
0
{
1818
0
   png_debug(1, "in png_set_chunk_cache_max");
1819
1820
0
   if (png_ptr != NULL)
1821
0
      png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1822
0
}
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
0
{
1857
0
   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
0
   if (allowed != 0)
1866
0
      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1867
0
         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
0
}
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
0
{
1887
0
   png_debug(1, "in png_set_check_for_invalid_index");
1888
1889
0
   if (allowed > 0)
1890
0
      png_ptr->num_palette_max = 0;
1891
1892
0
   else
1893
0
      png_ptr->num_palette_max = -1;
1894
0
}
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
0
{
1912
0
#ifdef PNG_WARNINGS_SUPPORTED
1913
0
   png_const_charp orig_key = key;
1914
0
#endif
1915
0
   png_uint_32 key_len = 0;
1916
0
   int bad_character = 0;
1917
0
   int space = 1;
1918
1919
0
   png_debug(1, "in png_check_keyword");
1920
1921
0
   if (key == NULL)
1922
0
   {
1923
0
      *new_key = 0;
1924
0
      return 0;
1925
0
   }
1926
1927
0
   while (*key && key_len < 79)
1928
0
   {
1929
0
      png_byte ch = (png_byte)*key++;
1930
1931
0
      if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
1932
0
      {
1933
0
         *new_key++ = ch; ++key_len; space = 0;
1934
0
      }
1935
1936
0
      else if (space == 0)
1937
0
      {
1938
         /* A space or an invalid character when one wasn't seen immediately
1939
          * before; output just a space.
1940
          */
1941
0
         *new_key++ = 32; ++key_len; space = 1;
1942
1943
         /* If the character was not a space then it is invalid. */
1944
0
         if (ch != 32)
1945
0
            bad_character = ch;
1946
0
      }
1947
1948
0
      else if (bad_character == 0)
1949
0
         bad_character = ch; /* just skip it, record the first error */
1950
0
   }
1951
1952
0
   if (key_len > 0 && space != 0) /* trailing space */
1953
0
   {
1954
0
      --key_len; --new_key;
1955
0
      if (bad_character == 0)
1956
0
         bad_character = 32;
1957
0
   }
1958
1959
   /* Terminate the keyword */
1960
0
   *new_key = 0;
1961
1962
0
   if (key_len == 0)
1963
0
      return 0;
1964
1965
0
#ifdef PNG_WARNINGS_SUPPORTED
1966
   /* Try to only output one warning per keyword: */
1967
0
   if (*key != 0) /* keyword too long */
1968
0
      png_warning(png_ptr, "keyword truncated");
1969
1970
0
   else if (bad_character != 0)
1971
0
   {
1972
0
      PNG_WARNING_PARAMETERS(p)
1973
1974
0
      png_warning_parameter(p, 1, orig_key);
1975
0
      png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
1976
1977
0
      png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
1978
0
   }
1979
#else /* !WARNINGS */
1980
   PNG_UNUSED(png_ptr)
1981
#endif /* !WARNINGS */
1982
1983
0
   return key_len;
1984
0
}
1985
#endif /* TEXT || pCAL || iCCP || sPLT */
1986
#endif /* READ || WRITE */