Coverage Report

Created: 2026-09-06 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/mux/muxinternal.c
Line
Count
Source
1
// Copyright 2011 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Internal objects and utils for mux.
11
//
12
// Authors: Urvang (urvang@google.com)
13
//          Vikas (vikasa@google.com)
14
15
#include <assert.h>
16
#include <stddef.h>
17
#include <string.h>
18
19
#include "src/mux/muxi.h"
20
#include "src/utils/utils.h"
21
#include "src/webp/format_constants.h"
22
#include "src/webp/mux.h"
23
#include "src/webp/mux_types.h"
24
#include "src/webp/types.h"
25
26
#define UNDEFINED_CHUNK_SIZE ((uint32_t)(-1))
27
28
const ChunkInfo kChunks[] = {
29
    {MKFOURCC('V', 'P', '8', 'X'), WEBP_CHUNK_VP8X, VP8X_CHUNK_SIZE},
30
    {MKFOURCC('I', 'C', 'C', 'P'), WEBP_CHUNK_ICCP, UNDEFINED_CHUNK_SIZE},
31
    {MKFOURCC('A', 'N', 'I', 'M'), WEBP_CHUNK_ANIM, ANIM_CHUNK_SIZE},
32
    {MKFOURCC('A', 'N', 'M', 'F'), WEBP_CHUNK_ANMF, ANMF_CHUNK_SIZE},
33
    {MKFOURCC('A', 'L', 'P', 'H'), WEBP_CHUNK_ALPHA, UNDEFINED_CHUNK_SIZE},
34
    {MKFOURCC('V', 'P', '8', ' '), WEBP_CHUNK_IMAGE, UNDEFINED_CHUNK_SIZE},
35
    {MKFOURCC('V', 'P', '8', 'L'), WEBP_CHUNK_IMAGE, UNDEFINED_CHUNK_SIZE},
36
    {MKFOURCC('E', 'X', 'I', 'F'), WEBP_CHUNK_EXIF, UNDEFINED_CHUNK_SIZE},
37
    {MKFOURCC('X', 'M', 'P', ' '), WEBP_CHUNK_XMP, UNDEFINED_CHUNK_SIZE},
38
    {NIL_TAG, WEBP_CHUNK_UNKNOWN, UNDEFINED_CHUNK_SIZE},
39
40
    {NIL_TAG, WEBP_CHUNK_NIL, UNDEFINED_CHUNK_SIZE}};
41
42
//------------------------------------------------------------------------------
43
44
0
int WebPGetMuxVersion(void) {
45
0
  return (MUX_MAJ_VERSION << 16) | (MUX_MIN_VERSION << 8) | MUX_REV_VERSION;
46
0
}
47
48
//------------------------------------------------------------------------------
49
// Life of a chunk object.
50
51
279k
void ChunkInit(WebPChunk* const chunk) {
52
279k
  assert(chunk);
53
279k
  memset(chunk, 0, sizeof(*chunk));
54
279k
  chunk->tag = NIL_TAG;
55
279k
}
56
57
126k
WebPChunk* ChunkRelease(WebPChunk* const chunk) {
58
126k
  WebPChunk* next;
59
126k
  if (chunk == NULL) return NULL;
60
126k
  if (chunk->owner) {
61
306
    WebPDataClear(&chunk->data);
62
306
  }
63
126k
  next = chunk->next;
64
126k
  ChunkInit(chunk);
65
126k
  return next;
66
126k
}
67
68
//------------------------------------------------------------------------------
69
// Chunk misc methods.
70
71
156
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag) {
72
156
  int i;
73
884
  for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
74
879
    if (tag == kChunks[i].tag) return (CHUNK_INDEX)i;
75
879
  }
76
5
  return IDX_UNKNOWN;
77
156
}
78
79
13.0k
WebPChunkId ChunkGetIdFromTag(uint32_t tag) {
80
13.0k
  int i;
81
123k
  for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
82
111k
    if (tag == kChunks[i].tag) return kChunks[i].id;
83
111k
  }
84
11.3k
  return WEBP_CHUNK_UNKNOWN;
85
13.0k
}
86
87
111
uint32_t ChunkGetTagFromFourCC(const char fourcc[4]) {
88
111
  return MKFOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
89
111
}
90
91
56
CHUNK_INDEX ChunkGetIndexFromFourCC(const char fourcc[4]) {
92
56
  const uint32_t tag = ChunkGetTagFromFourCC(fourcc);
93
56
  return ChunkGetIndexFromTag(tag);
94
56
}
95
96
//------------------------------------------------------------------------------
97
// Chunk search methods.
98
99
// Returns next chunk in the chunk list with the given tag.
100
493
static WebPChunk* ChunkSearchNextInList(WebPChunk* chunk, uint32_t tag) {
101
500
  while (chunk != NULL && chunk->tag != tag) {
102
7
    chunk = chunk->next;
103
7
  }
104
493
  return chunk;
105
493
}
106
107
493
WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag) {
108
493
  uint32_t iter = nth;
109
493
  first = ChunkSearchNextInList(first, tag);
110
493
  if (first == NULL) return NULL;
111
112
284
  while (--iter != 0) {
113
0
    WebPChunk* next_chunk = ChunkSearchNextInList(first->next, tag);
114
0
    if (next_chunk == NULL) break;
115
0
    first = next_chunk;
116
0
  }
117
284
  return ((nth > 0) && (iter > 0)) ? NULL : first;
118
493
}
119
120
//------------------------------------------------------------------------------
121
// Chunk writer methods.
122
123
WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
124
12.6k
                             int copy_data, uint32_t tag) {
125
  // For internally allocated chunks, always copy data & make it owner of data.
126
12.6k
  if (tag == kChunks[IDX_VP8X].tag || tag == kChunks[IDX_ANIM].tag) {
127
256
    copy_data = 1;
128
256
  }
129
130
12.6k
  ChunkRelease(chunk);
131
132
12.6k
  if (data != NULL) {
133
12.6k
    if (copy_data) {  // Copy data.
134
306
      if (!WebPDataCopy(data, &chunk->data)) return WEBP_MUX_MEMORY_ERROR;
135
306
      chunk->owner = 1;  // Chunk is owner of data.
136
12.3k
    } else {             // Don't copy data.
137
12.3k
      chunk->data = *data;
138
12.3k
    }
139
12.6k
  }
140
12.6k
  chunk->tag = tag;
141
12.6k
  return WEBP_MUX_OK;
142
12.6k
}
143
144
WebPMuxError ChunkSetHead(WebPChunk* const chunk,
145
12.5k
                          WebPChunk** const chunk_list) {
146
12.5k
  WebPChunk* new_chunk;
147
148
12.5k
  assert(chunk_list != NULL);
149
12.5k
  if (*chunk_list != NULL) {
150
0
    return WEBP_MUX_NOT_FOUND;
151
0
  }
152
153
12.5k
  new_chunk = (WebPChunk*)WebPSafeMalloc(1ULL, sizeof(*new_chunk));
154
12.5k
  if (new_chunk == NULL) return WEBP_MUX_MEMORY_ERROR;
155
12.5k
  *new_chunk = *chunk;
156
12.5k
  chunk->owner = 0;
157
12.5k
  new_chunk->next = NULL;
158
12.5k
  *chunk_list = new_chunk;
159
12.5k
  return WEBP_MUX_OK;
160
12.5k
}
161
162
WebPMuxError ChunkAppend(WebPChunk* const chunk,
163
11.6k
                         WebPChunk*** const chunk_list) {
164
11.6k
  WebPMuxError err;
165
11.6k
  assert(chunk_list != NULL && *chunk_list != NULL);
166
167
11.6k
  if (**chunk_list == NULL) {
168
448
    err = ChunkSetHead(chunk, *chunk_list);
169
11.1k
  } else {
170
11.1k
    WebPChunk* last_chunk = **chunk_list;
171
11.1k
    while (last_chunk->next != NULL) last_chunk = last_chunk->next;
172
11.1k
    err = ChunkSetHead(chunk, &last_chunk->next);
173
11.1k
    if (err == WEBP_MUX_OK) *chunk_list = &last_chunk->next;
174
11.1k
  }
175
11.6k
  return err;
176
11.6k
}
177
178
//------------------------------------------------------------------------------
179
// Chunk deletion method(s).
180
181
12.5k
WebPChunk* ChunkDelete(WebPChunk* const chunk) {
182
12.5k
  WebPChunk* const next = ChunkRelease(chunk);
183
12.5k
  WebPSafeFree(chunk);
184
12.5k
  return next;
185
12.5k
}
186
187
615k
void ChunkListDelete(WebPChunk** const chunk_list) {
188
628k
  while (*chunk_list != NULL) {
189
12.5k
    *chunk_list = ChunkDelete(*chunk_list);
190
12.5k
  }
191
615k
}
192
193
//------------------------------------------------------------------------------
194
// Chunk serialization methods.
195
196
150
static uint8_t* ChunkEmit(const WebPChunk* const chunk, uint8_t* dst) {
197
150
  const size_t chunk_size = chunk->data.size;
198
150
  assert(chunk);
199
  // Do not check chunk->tag != NIL as NIL_TAG could have been read from a file.
200
150
  PutLE32(dst + 0, chunk->tag);
201
150
  PutLE32(dst + TAG_SIZE, (uint32_t)chunk_size);
202
150
  assert(chunk_size == (uint32_t)chunk_size);
203
150
  memcpy(dst + CHUNK_HEADER_SIZE, chunk->data.bytes, chunk_size);
204
150
  if (chunk_size & 1) dst[CHUNK_HEADER_SIZE + chunk_size] = 0;  // Add padding.
205
150
  return dst + ChunkDiskSize(chunk);
206
150
}
207
208
300
uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst) {
209
400
  while (chunk_list != NULL) {
210
100
    dst = ChunkEmit(chunk_list, dst);
211
100
    chunk_list = chunk_list->next;
212
100
  }
213
300
  return dst;
214
300
}
215
216
300
size_t ChunkListDiskSize(const WebPChunk* chunk_list) {
217
300
  size_t size = 0;
218
400
  while (chunk_list != NULL) {
219
100
    size += ChunkDiskSize(chunk_list);
220
100
    chunk_list = chunk_list->next;
221
100
  }
222
300
  return size;
223
300
}
224
225
//------------------------------------------------------------------------------
226
// Life of a MuxImage object.
227
228
2.28k
void MuxImageInit(WebPMuxImage* const wpi) {
229
2.28k
  assert(wpi);
230
2.28k
  memset(wpi, 0, sizeof(*wpi));
231
2.28k
}
232
233
102k
WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) {
234
102k
  WebPMuxImage* next;
235
102k
  if (wpi == NULL) return NULL;
236
  // There should be at most one chunk of 'header', 'alpha', 'img' but we call
237
  // ChunkListDelete to be safe
238
1.14k
  ChunkListDelete(&wpi->header);
239
1.14k
  ChunkListDelete(&wpi->alpha);
240
1.14k
  ChunkListDelete(&wpi->img);
241
1.14k
  ChunkListDelete(&wpi->unknown);
242
243
1.14k
  next = wpi->next;
244
1.14k
  MuxImageInit(wpi);
245
1.14k
  return next;
246
102k
}
247
248
//------------------------------------------------------------------------------
249
// MuxImage search methods.
250
251
// Get a reference to appropriate chunk list within an image given chunk tag.
252
static WebPChunk** GetChunkListFromId(const WebPMuxImage* const wpi,
253
1.12k
                                      WebPChunkId id) {
254
1.12k
  assert(wpi != NULL);
255
1.12k
  switch (id) {
256
535
    case WEBP_CHUNK_ANMF:
257
535
      return (WebPChunk**)&wpi->header;
258
69
    case WEBP_CHUNK_ALPHA:
259
69
      return (WebPChunk**)&wpi->alpha;
260
524
    case WEBP_CHUNK_IMAGE:
261
524
      return (WebPChunk**)&wpi->img;
262
0
    default:
263
0
      return NULL;
264
1.12k
  }
265
1.12k
}
266
267
1.05k
int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) {
268
1.05k
  int count = 0;
269
1.05k
  const WebPMuxImage* current;
270
2.18k
  for (current = wpi_list; current != NULL; current = current->next) {
271
1.12k
    if (id == WEBP_CHUNK_NIL) {
272
0
      ++count;  // Special case: count all images.
273
1.12k
    } else {
274
1.12k
      const WebPChunk* const wpi_chunk = *GetChunkListFromId(current, id);
275
1.12k
      if (wpi_chunk != NULL) {
276
534
        const WebPChunkId wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag);
277
534
        if (wpi_chunk_id == id) ++count;  // Count images with a matching 'id'.
278
534
      }
279
1.12k
    }
280
1.12k
  }
281
1.05k
  return count;
282
1.05k
}
283
284
// Outputs a pointer to 'prev_wpi->next',
285
//   where 'prev_wpi' is the pointer to the image at position (nth - 1).
286
// Returns true if nth image was found.
287
static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth,
288
0
                                    WebPMuxImage*** const location) {
289
0
  uint32_t count = 0;
290
0
  assert(wpi_list);
291
0
  *location = wpi_list;
292
293
0
  if (nth == 0) {
294
0
    nth = MuxImageCount(*wpi_list, WEBP_CHUNK_NIL);
295
0
    if (nth == 0) return 0;  // Not found.
296
0
  }
297
298
0
  while (*wpi_list != NULL) {
299
0
    WebPMuxImage* const cur_wpi = *wpi_list;
300
0
    ++count;
301
0
    if (count == nth) return 1;  // Found.
302
0
    wpi_list = &cur_wpi->next;
303
0
    *location = wpi_list;
304
0
  }
305
0
  return 0;  // Not found.
306
0
}
307
308
//------------------------------------------------------------------------------
309
// MuxImage writer methods.
310
311
645
WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list) {
312
645
  WebPMuxImage* new_wpi;
313
314
779
  while (*wpi_list != NULL) {
315
240
    WebPMuxImage* const cur_wpi = *wpi_list;
316
240
    if (cur_wpi->next == NULL) break;
317
134
    wpi_list = &cur_wpi->next;
318
134
  }
319
320
645
  new_wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*new_wpi));
321
645
  if (new_wpi == NULL) return WEBP_MUX_MEMORY_ERROR;
322
645
  *new_wpi = *wpi;
323
645
  new_wpi->next = NULL;
324
325
645
  if (*wpi_list != NULL) {
326
106
    (*wpi_list)->next = new_wpi;
327
539
  } else {
328
539
    *wpi_list = new_wpi;
329
539
  }
330
645
  return WEBP_MUX_OK;
331
645
}
332
333
//------------------------------------------------------------------------------
334
// MuxImage deletion methods.
335
336
102k
WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi) {
337
  // Delete the components of wpi. If wpi is NULL this is a noop.
338
102k
  WebPMuxImage* const next = MuxImageRelease(wpi);
339
102k
  WebPSafeFree(wpi);
340
102k
  return next;
341
102k
}
342
343
0
WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth) {
344
0
  assert(wpi_list);
345
0
  if (!SearchImageToGetOrDelete(wpi_list, nth, &wpi_list)) {
346
0
    return WEBP_MUX_NOT_FOUND;
347
0
  }
348
0
  *wpi_list = MuxImageDelete(*wpi_list);
349
0
  return WEBP_MUX_OK;
350
0
}
351
352
//------------------------------------------------------------------------------
353
// MuxImage reader methods.
354
355
WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
356
0
                            WebPMuxImage** wpi) {
357
0
  assert(wpi_list);
358
0
  assert(wpi);
359
0
  if (!SearchImageToGetOrDelete((WebPMuxImage**)wpi_list, nth,
360
0
                                (WebPMuxImage***)&wpi_list)) {
361
0
    return WEBP_MUX_NOT_FOUND;
362
0
  }
363
0
  *wpi = (WebPMuxImage*)*wpi_list;
364
0
  return WEBP_MUX_OK;
365
0
}
366
367
//------------------------------------------------------------------------------
368
// MuxImage serialization methods.
369
370
// Size of an image.
371
50
size_t MuxImageDiskSize(const WebPMuxImage* const wpi) {
372
50
  size_t size = 0;
373
50
  if (wpi->header != NULL) size += ChunkDiskSize(wpi->header);
374
50
  if (wpi->alpha != NULL) size += ChunkDiskSize(wpi->alpha);
375
50
  if (wpi->img != NULL) size += ChunkDiskSize(wpi->img);
376
50
  if (wpi->unknown != NULL) size += ChunkListDiskSize(wpi->unknown);
377
50
  return size;
378
50
}
379
380
// Special case as ANMF chunk encapsulates other image chunks.
381
static uint8_t* ChunkEmitSpecial(const WebPChunk* const header,
382
0
                                 size_t total_size, uint8_t* dst) {
383
0
  const size_t header_size = header->data.size;
384
0
  const size_t offset_to_next = total_size - CHUNK_HEADER_SIZE;
385
0
  assert(header->tag == kChunks[IDX_ANMF].tag);
386
0
  PutLE32(dst + 0, header->tag);
387
0
  PutLE32(dst + TAG_SIZE, (uint32_t)offset_to_next);
388
0
  assert(header_size == (uint32_t)header_size);
389
0
  memcpy(dst + CHUNK_HEADER_SIZE, header->data.bytes, header_size);
390
0
  if (header_size & 1) {
391
0
    dst[CHUNK_HEADER_SIZE + header_size] = 0;  // Add padding.
392
0
  }
393
0
  return dst + ChunkDiskSize(header);
394
0
}
395
396
50
uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) {
397
  // Ordering of chunks to be emitted is strictly as follows:
398
  // 1. ANMF chunk (if present).
399
  // 2. ALPH chunk (if present).
400
  // 3. VP8/VP8L chunk.
401
50
  assert(wpi);
402
50
  if (wpi->header != NULL) {
403
0
    dst = ChunkEmitSpecial(wpi->header, MuxImageDiskSize(wpi), dst);
404
0
  }
405
50
  if (wpi->alpha != NULL) dst = ChunkEmit(wpi->alpha, dst);
406
50
  if (wpi->img != NULL) dst = ChunkEmit(wpi->img, dst);
407
50
  if (wpi->unknown != NULL) dst = ChunkListEmit(wpi->unknown, dst);
408
50
  return dst;
409
50
}
410
411
//------------------------------------------------------------------------------
412
// Helper methods for mux.
413
414
285
int MuxHasAlpha(const WebPMuxImage* images) {
415
543
  while (images != NULL) {
416
285
    if (images->has_alpha) return 1;
417
258
    images = images->next;
418
258
  }
419
258
  return 0;
420
285
}
421
422
50
uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size) {
423
50
  PutLE32(data + 0, MKFOURCC('R', 'I', 'F', 'F'));
424
50
  PutLE32(data + TAG_SIZE, (uint32_t)size - CHUNK_HEADER_SIZE);
425
50
  assert(size == (uint32_t)size);
426
50
  PutLE32(data + TAG_SIZE + CHUNK_SIZE_BYTES, MKFOURCC('W', 'E', 'B', 'P'));
427
50
  return data + RIFF_HEADER_SIZE;
428
50
}
429
430
1.95k
WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id) {
431
1.95k
  assert(mux != NULL);
432
1.95k
  switch (id) {
433
476
    case WEBP_CHUNK_VP8X:
434
476
      return (WebPChunk**)&mux->vp8x;
435
292
    case WEBP_CHUNK_ICCP:
436
292
      return (WebPChunk**)&mux->iccp;
437
328
    case WEBP_CHUNK_ANIM:
438
328
      return (WebPChunk**)&mux->anim;
439
387
    case WEBP_CHUNK_EXIF:
440
387
      return (WebPChunk**)&mux->exif;
441
283
    case WEBP_CHUNK_XMP:
442
283
      return (WebPChunk**)&mux->xmp;
443
190
    default:
444
190
      return (WebPChunk**)&mux->unknown;
445
1.95k
  }
446
1.95k
}
447
448
840
static int IsNotCompatible(int feature, int num_items) {
449
840
  return (feature != 0) != (num_items > 0);
450
840
}
451
452
4.84k
#define NO_FLAG ((WebPFeatureFlags)0)
453
454
// Test basic constraints:
455
// retrieval, maximum number of chunks by index (use -1 to skip)
456
// and feature incompatibility (use NO_FLAG to skip).
457
// On success returns WEBP_MUX_OK and stores the chunk count in *num.
458
static WebPMuxError ValidateChunk(const WebPMux* const mux, CHUNK_INDEX idx,
459
                                  WebPFeatureFlags feature, uint32_t vp8x_flags,
460
1.90k
                                  int max, int* num) {
461
1.90k
  const WebPMuxError err = WebPMuxNumChunks(mux, kChunks[idx].id, num);
462
1.90k
  if (err != WEBP_MUX_OK) return err;
463
1.90k
  if (max > -1 && *num > max) return WEBP_MUX_INVALID_ARGUMENT;
464
1.89k
  if (feature != NO_FLAG && IsNotCompatible(vp8x_flags & feature, *num)) {
465
10
    return WEBP_MUX_INVALID_ARGUMENT;
466
10
  }
467
1.88k
  return WEBP_MUX_OK;
468
1.89k
}
469
470
306
WebPMuxError MuxValidate(const WebPMux* const mux) {
471
306
  int num_iccp;
472
306
  int num_exif;
473
306
  int num_xmp;
474
306
  int num_anim;
475
306
  int num_frames;
476
306
  int num_vp8x;
477
306
  int num_images;
478
306
  int num_alpha;
479
306
  uint32_t flags;
480
306
  WebPMuxError err;
481
482
  // Verify mux is not NULL.
483
306
  if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
484
485
  // Verify mux has at least one image.
486
306
  if (mux->images == NULL) return WEBP_MUX_INVALID_ARGUMENT;
487
488
306
  err = WebPMuxGetFeatures(mux, &flags);
489
306
  if (err != WEBP_MUX_OK) return err;
490
491
  // At most one color profile chunk.
492
287
  err = ValidateChunk(mux, IDX_ICCP, ICCP_FLAG, flags, 1, &num_iccp);
493
287
  if (err != WEBP_MUX_OK) return err;
494
495
  // At most one EXIF metadata.
496
282
  err = ValidateChunk(mux, IDX_EXIF, EXIF_FLAG, flags, 1, &num_exif);
497
282
  if (err != WEBP_MUX_OK) return err;
498
499
  // At most one XMP metadata.
500
276
  err = ValidateChunk(mux, IDX_XMP, XMP_FLAG, flags, 1, &num_xmp);
501
276
  if (err != WEBP_MUX_OK) return err;
502
503
  // Verify either VP8X chunk is present OR there is only one elem in
504
  // mux->images.
505
272
  err = ValidateChunk(mux, IDX_VP8X, NO_FLAG, flags, 1, &num_vp8x);
506
272
  if (err != WEBP_MUX_OK) return err;
507
271
  err = ValidateChunk(mux, IDX_VP8, NO_FLAG, flags, -1, &num_images);
508
271
  if (err != WEBP_MUX_OK) return err;
509
271
  if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT;
510
511
  // Animation: ANIMATION_FLAG, ANIM chunk and ANMF chunk(s) are consistent.
512
  // At most one ANIM chunk.
513
261
  err = ValidateChunk(mux, IDX_ANIM, NO_FLAG, flags, 1, &num_anim);
514
261
  if (err != WEBP_MUX_OK) return err;
515
256
  err = ValidateChunk(mux, IDX_ANMF, NO_FLAG, flags, -1, &num_frames);
516
256
  if (err != WEBP_MUX_OK) return err;
517
518
256
  {
519
256
    const int has_animation = !!(flags & ANIMATION_FLAG);
520
    // An ANIM chunk must be present when has_animation is true, and all images
521
    // must be contained in ANMF chunks.
522
256
    if (has_animation &&
523
2
        (num_anim == 0 || num_frames == 0 || num_frames != num_images)) {
524
2
      return WEBP_MUX_INVALID_ARGUMENT;
525
2
    }
526
254
    if (!has_animation && (num_anim == 1 || num_frames > 0)) {
527
3
      return WEBP_MUX_INVALID_ARGUMENT;
528
3
    }
529
251
    if (!has_animation) {
530
251
      const WebPMuxImage* images = mux->images;
531
      // There can be only one image.
532
251
      if (images == NULL || images->next != NULL) {
533
1
        return WEBP_MUX_INVALID_ARGUMENT;
534
1
      }
535
      // Size must match.
536
250
      if (mux->canvas_width > 0) {
537
82
        if (images->width != mux->canvas_width ||
538
70
            images->height != mux->canvas_height) {
539
15
          return WEBP_MUX_INVALID_ARGUMENT;
540
15
        }
541
82
      }
542
250
    }
543
251
  }
544
545
  // ALPHA_FLAG & alpha chunk(s) are consistent.
546
  // Note: ALPHA_FLAG can be set when there is actually no Alpha data present.
547
235
  if (MuxHasAlpha(mux->images)) {
548
27
    if (num_vp8x > 0) {
549
      // VP8X chunk is present, so it should contain ALPHA_FLAG.
550
8
      if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT;
551
19
    } else {
552
      // VP8X chunk is not present, so ALPH chunks should NOT be present either.
553
19
      err = WebPMuxNumChunks(mux, WEBP_CHUNK_ALPHA, &num_alpha);
554
19
      if (err != WEBP_MUX_OK) return err;
555
19
      if (num_alpha > 0) return WEBP_MUX_INVALID_ARGUMENT;
556
19
    }
557
27
  }
558
559
231
  return WEBP_MUX_OK;
560
235
}
561
562
#undef NO_FLAG
563
564
//------------------------------------------------------------------------------