/src/libwebp/src/demux/demux.c
Line | Count | Source |
1 | | // Copyright 2012 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 | | // WebP container demux. |
11 | | // |
12 | | |
13 | | #ifdef HAVE_CONFIG_H |
14 | | #include "src/webp/config.h" |
15 | | #endif |
16 | | |
17 | | #include <assert.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include "src/utils/utils.h" |
22 | | #include "src/webp/decode.h" // WebPGetFeatures |
23 | | #include "src/webp/demux.h" |
24 | | #include "src/webp/format_constants.h" |
25 | | #include "src/webp/mux.h" |
26 | | #include "src/webp/mux_types.h" |
27 | | #include "src/webp/types.h" |
28 | | |
29 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
30 | | |
31 | 0 | #define DMUX_MAJ_VERSION 1 |
32 | 0 | #define DMUX_MIN_VERSION 6 |
33 | 0 | #define DMUX_REV_VERSION 0 |
34 | | |
35 | | typedef struct { |
36 | | size_t start; // start location of the data |
37 | | size_t end; // end location |
38 | | size_t riff_end; // riff chunk end location, can be > end. |
39 | | size_t buf_size; // size of the buffer |
40 | | const uint8_t* buf; |
41 | | } MemBuffer; |
42 | | |
43 | | typedef struct { |
44 | | size_t offset; |
45 | | size_t size; |
46 | | } ChunkData; |
47 | | |
48 | | typedef struct Frame { |
49 | | int x_offset, y_offset; |
50 | | int width, height; |
51 | | int has_alpha; |
52 | | int duration; |
53 | | WebPMuxAnimDispose dispose_method; |
54 | | WebPMuxAnimBlend blend_method; |
55 | | int frame_num; |
56 | | int complete; // img_components contains a full image. |
57 | | ChunkData img_components[2]; // 0=VP8{,L} 1=ALPH |
58 | | struct Frame* next; |
59 | | } Frame; |
60 | | |
61 | | typedef struct Chunk { |
62 | | ChunkData data; |
63 | | struct Chunk* next; |
64 | | } Chunk; |
65 | | |
66 | | struct WebPDemuxer { |
67 | | MemBuffer mem; |
68 | | WebPDemuxState state; |
69 | | int is_ext_format; |
70 | | uint32_t feature_flags; |
71 | | int canvas_width, canvas_height; |
72 | | int loop_count; |
73 | | uint32_t bgcolor; |
74 | | int num_frames; |
75 | | Frame* frames; |
76 | | Frame** frames_tail; |
77 | | Chunk* chunks; // non-image chunks |
78 | | Chunk** chunks_tail; |
79 | | }; |
80 | | |
81 | | typedef enum { PARSE_OK, PARSE_NEED_MORE_DATA, PARSE_ERROR } ParseStatus; |
82 | | |
83 | | typedef struct ChunkParser { |
84 | | uint8_t id[4]; |
85 | | ParseStatus (*parse)(WebPDemuxer* const dmux); |
86 | | int (*valid)(const WebPDemuxer* const dmux); |
87 | | } ChunkParser; |
88 | | |
89 | | static ParseStatus ParseSingleImage(WebPDemuxer* const dmux); |
90 | | static ParseStatus ParseVP8X(WebPDemuxer* const dmux); |
91 | | static int IsValidSimpleFormat(const WebPDemuxer* const dmux); |
92 | | static int IsValidExtendedFormat(const WebPDemuxer* const dmux); |
93 | | |
94 | | static const ChunkParser kMasterChunks[] = { |
95 | | {{'V', 'P', '8', ' '}, ParseSingleImage, IsValidSimpleFormat}, |
96 | | {{'V', 'P', '8', 'L'}, ParseSingleImage, IsValidSimpleFormat}, |
97 | | {{'V', 'P', '8', 'X'}, ParseVP8X, IsValidExtendedFormat}, |
98 | | {{'0', '0', '0', '0'}, NULL, NULL}, |
99 | | }; |
100 | | |
101 | | //------------------------------------------------------------------------------ |
102 | | |
103 | 0 | int WebPGetDemuxVersion(void) { |
104 | 0 | return (DMUX_MAJ_VERSION << 16) | (DMUX_MIN_VERSION << 8) | DMUX_REV_VERSION; |
105 | 0 | } |
106 | | |
107 | | // ----------------------------------------------------------------------------- |
108 | | // MemBuffer |
109 | | |
110 | | static int RemapMemBuffer(MemBuffer* const mem, const uint8_t* data, |
111 | 22.6k | size_t size) { |
112 | 22.6k | if (size < mem->buf_size) return 0; // can't remap to a shorter buffer! |
113 | | |
114 | 22.6k | mem->buf = data; |
115 | 22.6k | mem->end = mem->buf_size = size; |
116 | 22.6k | return 1; |
117 | 22.6k | } |
118 | | |
119 | | static int InitMemBuffer(MemBuffer* const mem, const uint8_t* data, |
120 | 22.6k | size_t size) { |
121 | 22.6k | WEBP_UNSAFE_MEMSET(mem, 0, sizeof(*mem)); |
122 | 22.6k | return RemapMemBuffer(mem, data, size); |
123 | 22.6k | } |
124 | | |
125 | | // Return the remaining data size available in 'mem'. |
126 | 40.1k | static WEBP_INLINE size_t MemDataSize(const MemBuffer* const mem) { |
127 | 40.1k | return (mem->end - mem->start); |
128 | 40.1k | } |
129 | | |
130 | | // Return true if 'size' exceeds the end of the RIFF chunk. |
131 | 8.63k | static WEBP_INLINE int SizeIsInvalid(const MemBuffer* const mem, size_t size) { |
132 | 8.63k | return (size > mem->riff_end - mem->start); |
133 | 8.63k | } |
134 | | |
135 | 25.0k | static WEBP_INLINE void Skip(MemBuffer* const mem, size_t size) { |
136 | 25.0k | mem->start += size; |
137 | 25.0k | } |
138 | | |
139 | 436 | static WEBP_INLINE void Rewind(MemBuffer* const mem, size_t size) { |
140 | 436 | mem->start -= size; |
141 | 436 | } |
142 | | |
143 | 30.5k | static WEBP_INLINE const uint8_t* GetBuffer(MemBuffer* const mem) { |
144 | 30.5k | return mem->buf + mem->start; |
145 | 30.5k | } |
146 | | |
147 | | // Read from 'mem' and skip the read bytes. |
148 | 1.10k | static WEBP_INLINE uint8_t ReadByte(MemBuffer* const mem) { |
149 | 1.10k | const uint8_t byte = mem->buf[mem->start]; |
150 | 1.10k | Skip(mem, 1); |
151 | 1.10k | return byte; |
152 | 1.10k | } |
153 | | |
154 | 19 | static WEBP_INLINE int ReadLE16s(MemBuffer* const mem) { |
155 | 19 | const uint8_t* const data = mem->buf + mem->start; |
156 | 19 | const int val = GetLE16(data); |
157 | 19 | Skip(mem, 2); |
158 | 19 | return val; |
159 | 19 | } |
160 | | |
161 | 2.20k | static WEBP_INLINE int ReadLE24s(MemBuffer* const mem) { |
162 | 2.20k | const uint8_t* const data = mem->buf + mem->start; |
163 | 2.20k | const int val = GetLE24(data); |
164 | 2.20k | Skip(mem, 3); |
165 | 2.20k | return val; |
166 | 2.20k | } |
167 | | |
168 | 12.2k | static WEBP_INLINE uint32_t ReadLE32(MemBuffer* const mem) { |
169 | 12.2k | const uint8_t* const data = mem->buf + mem->start; |
170 | 12.2k | const uint32_t val = GetLE32(data); |
171 | 12.2k | Skip(mem, 4); |
172 | 12.2k | return val; |
173 | 12.2k | } |
174 | | |
175 | | // ----------------------------------------------------------------------------- |
176 | | // Secondary chunk parsing |
177 | | |
178 | 3.29k | static void AddChunk(WebPDemuxer* const dmux, Chunk* const chunk) { |
179 | 3.29k | *dmux->chunks_tail = chunk; |
180 | 3.29k | chunk->next = NULL; |
181 | 3.29k | dmux->chunks_tail = &chunk->next; |
182 | 3.29k | } |
183 | | |
184 | | // Add a frame to the end of the list, ensuring the last frame is complete. |
185 | | // Returns true on success, false otherwise. |
186 | 18.1k | static int AddFrame(WebPDemuxer* const dmux, Frame* const frame) { |
187 | 18.1k | const Frame* const last_frame = *dmux->frames_tail; |
188 | 18.1k | if (last_frame != NULL && !last_frame->complete) return 0; |
189 | | |
190 | 18.1k | *dmux->frames_tail = frame; |
191 | 18.1k | frame->next = NULL; |
192 | 18.1k | dmux->frames_tail = &frame->next; |
193 | 18.1k | return 1; |
194 | 18.1k | } |
195 | | |
196 | | static void SetFrameInfo(size_t start_offset, size_t size, int frame_num, |
197 | | int complete, |
198 | | const WebPBitstreamFeatures* const features, |
199 | 18.1k | Frame* const frame) { |
200 | 18.1k | frame->img_components[0].offset = start_offset; |
201 | 18.1k | frame->img_components[0].size = size; |
202 | 18.1k | frame->width = features->width; |
203 | 18.1k | frame->height = features->height; |
204 | 18.1k | frame->has_alpha |= features->has_alpha; |
205 | 18.1k | frame->frame_num = frame_num; |
206 | 18.1k | frame->complete = complete; |
207 | 18.1k | } |
208 | | |
209 | | // Store image bearing chunks to 'frame'. 'min_size' is an optional size |
210 | | // requirement, it may be zero. |
211 | | static ParseStatus StoreFrame(int frame_num, uint32_t min_size, |
212 | 825 | MemBuffer* const mem, Frame* const frame) { |
213 | 825 | int alpha_chunks = 0; |
214 | 825 | int image_chunks = 0; |
215 | 825 | int done = |
216 | 825 | (MemDataSize(mem) < CHUNK_HEADER_SIZE || MemDataSize(mem) < min_size); |
217 | 825 | ParseStatus status = PARSE_OK; |
218 | | |
219 | 825 | if (done) return PARSE_NEED_MORE_DATA; |
220 | | |
221 | 1.27k | do { |
222 | 1.27k | const size_t chunk_start_offset = mem->start; |
223 | 1.27k | const uint32_t fourcc = ReadLE32(mem); |
224 | 1.27k | const uint32_t payload_size = ReadLE32(mem); |
225 | 1.27k | uint32_t payload_size_padded; |
226 | 1.27k | size_t payload_available; |
227 | 1.27k | size_t chunk_size; |
228 | | |
229 | 1.27k | if (payload_size > MAX_CHUNK_PAYLOAD) return PARSE_ERROR; |
230 | | |
231 | 1.27k | payload_size_padded = payload_size + (payload_size & 1); |
232 | 1.27k | payload_available = (payload_size_padded > MemDataSize(mem)) |
233 | 1.27k | ? MemDataSize(mem) |
234 | 1.27k | : payload_size_padded; |
235 | 1.27k | chunk_size = CHUNK_HEADER_SIZE + payload_available; |
236 | 1.27k | if (SizeIsInvalid(mem, payload_size_padded)) return PARSE_ERROR; |
237 | 1.13k | if (payload_size_padded > MemDataSize(mem)) status = PARSE_NEED_MORE_DATA; |
238 | | |
239 | 1.13k | switch (fourcc) { |
240 | 25 | case MKFOURCC('A', 'L', 'P', 'H'): |
241 | 25 | if (alpha_chunks == 0) { |
242 | 25 | ++alpha_chunks; |
243 | 25 | frame->img_components[1].offset = chunk_start_offset; |
244 | 25 | frame->img_components[1].size = chunk_size; |
245 | 25 | frame->has_alpha = 1; |
246 | 25 | frame->frame_num = frame_num; |
247 | 25 | Skip(mem, payload_available); |
248 | 25 | } else { |
249 | 0 | goto Done; |
250 | 0 | } |
251 | 25 | break; |
252 | 649 | case MKFOURCC('V', 'P', '8', 'L'): |
253 | 649 | if (alpha_chunks > 0) return PARSE_ERROR; // VP8L has its own alpha |
254 | | // fall through |
255 | 740 | case MKFOURCC('V', 'P', '8', ' '): |
256 | 740 | if (image_chunks == 0) { |
257 | | // Extract the bitstream features, tolerating failures when the data |
258 | | // is incomplete. |
259 | 739 | WebPBitstreamFeatures features; |
260 | 739 | const VP8StatusCode vp8_status = WebPGetFeatures( |
261 | 739 | mem->buf + chunk_start_offset, chunk_size, &features); |
262 | 739 | if (status == PARSE_NEED_MORE_DATA && |
263 | 151 | vp8_status == VP8_STATUS_NOT_ENOUGH_DATA) { |
264 | 42 | return PARSE_NEED_MORE_DATA; |
265 | 697 | } else if (vp8_status != VP8_STATUS_OK) { |
266 | | // We have enough data, and yet WebPGetFeatures() failed. |
267 | 87 | return PARSE_ERROR; |
268 | 87 | } |
269 | 610 | ++image_chunks; |
270 | 610 | SetFrameInfo(chunk_start_offset, chunk_size, frame_num, |
271 | 610 | status == PARSE_OK, &features, frame); |
272 | 610 | Skip(mem, payload_available); |
273 | 610 | } else { |
274 | 1 | goto Done; |
275 | 1 | } |
276 | 610 | break; |
277 | 610 | Done: |
278 | 368 | default: |
279 | | // Restore fourcc/size when moving up one level in parsing. |
280 | 368 | Rewind(mem, CHUNK_HEADER_SIZE); |
281 | 368 | done = 1; |
282 | 368 | break; |
283 | 1.13k | } |
284 | | |
285 | 1.00k | if (mem->start == mem->riff_end) { |
286 | 25 | done = 1; |
287 | 978 | } else if (MemDataSize(mem) < CHUNK_HEADER_SIZE) { |
288 | 158 | status = PARSE_NEED_MORE_DATA; |
289 | 158 | } |
290 | 1.00k | } while (!done && status == PARSE_OK); |
291 | | |
292 | 551 | return status; |
293 | 825 | } |
294 | | |
295 | | // Creates a new Frame if 'actual_size' is within bounds and 'mem' contains |
296 | | // enough data ('min_size') to parse the payload. |
297 | | // Returns PARSE_OK on success with *frame pointing to the new Frame. |
298 | | // Returns PARSE_NEED_MORE_DATA with insufficient data, PARSE_ERROR otherwise. |
299 | | static ParseStatus NewFrame(const MemBuffer* const mem, uint32_t min_size, |
300 | 0 | uint32_t actual_size, Frame** frame) { |
301 | 0 | if (SizeIsInvalid(mem, min_size)) return PARSE_ERROR; |
302 | 0 | if (actual_size < min_size) return PARSE_ERROR; |
303 | 0 | if (MemDataSize(mem) < min_size) return PARSE_NEED_MORE_DATA; |
304 | | |
305 | 0 | *frame = (Frame*)WebPSafeCalloc(1ULL, sizeof(**frame)); |
306 | 0 | return (*frame == NULL) ? PARSE_ERROR : PARSE_OK; |
307 | 0 | } |
308 | | |
309 | | // Parse a 'ANMF' chunk and any image bearing chunks that immediately follow. |
310 | | // 'frame_chunk_size' is the previously validated, padded chunk size. |
311 | | static ParseStatus ParseAnimationFrame(WebPDemuxer* const dmux, |
312 | 0 | uint32_t frame_chunk_size) { |
313 | 0 | const int is_animation = !!(dmux->feature_flags & ANIMATION_FLAG); |
314 | 0 | int added_frame = 0; |
315 | 0 | int bits; |
316 | 0 | MemBuffer* const mem = &dmux->mem; |
317 | 0 | Frame* frame; |
318 | 0 | size_t start_offset; |
319 | 0 | ParseStatus status = NewFrame(mem, ANMF_CHUNK_SIZE, frame_chunk_size, &frame); |
320 | 0 | if (status != PARSE_OK) return status; |
321 | | |
322 | 0 | frame->x_offset = 2 * ReadLE24s(mem); |
323 | 0 | frame->y_offset = 2 * ReadLE24s(mem); |
324 | 0 | frame->width = 1 + ReadLE24s(mem); |
325 | 0 | frame->height = 1 + ReadLE24s(mem); |
326 | 0 | frame->duration = ReadLE24s(mem); |
327 | 0 | bits = ReadByte(mem); |
328 | 0 | frame->dispose_method = |
329 | 0 | (bits & 1) ? WEBP_MUX_DISPOSE_BACKGROUND : WEBP_MUX_DISPOSE_NONE; |
330 | 0 | frame->blend_method = (bits & 2) ? WEBP_MUX_NO_BLEND : WEBP_MUX_BLEND; |
331 | 0 | if (frame->width * (uint64_t)frame->height >= MAX_IMAGE_AREA) { |
332 | 0 | WebPSafeFree(frame); |
333 | 0 | return PARSE_ERROR; |
334 | 0 | } |
335 | | |
336 | | // Store a frame only if the animation flag is set there is some data for |
337 | | // this frame is available. |
338 | 0 | start_offset = mem->start; |
339 | 0 | { |
340 | 0 | const uint32_t anmf_payload_size = frame_chunk_size - ANMF_CHUNK_SIZE; |
341 | 0 | status = StoreFrame(dmux->num_frames + 1, anmf_payload_size, mem, frame); |
342 | 0 | if (status != PARSE_ERROR && |
343 | 0 | mem->start - start_offset > anmf_payload_size) { |
344 | 0 | status = PARSE_ERROR; |
345 | 0 | } |
346 | 0 | } |
347 | 0 | if (status != PARSE_ERROR && is_animation && frame->frame_num > 0) { |
348 | 0 | added_frame = AddFrame(dmux, frame); |
349 | 0 | if (added_frame) { |
350 | 0 | ++dmux->num_frames; |
351 | 0 | } else { |
352 | 0 | status = PARSE_ERROR; |
353 | 0 | } |
354 | 0 | } |
355 | |
|
356 | 0 | if (!added_frame) WebPSafeFree(frame); |
357 | 0 | return status; |
358 | 0 | } |
359 | | |
360 | | // General chunk storage, starting with the header at 'start_offset', allowing |
361 | | // the user to request the payload via a fourcc string. 'size' includes the |
362 | | // header and the unpadded payload size. |
363 | | // Returns true on success, false otherwise. |
364 | | static int StoreChunk(WebPDemuxer* const dmux, size_t start_offset, |
365 | 3.29k | uint32_t size) { |
366 | 3.29k | Chunk* const chunk = (Chunk*)WebPSafeCalloc(1ULL, sizeof(*chunk)); |
367 | 3.29k | if (chunk == NULL) return 0; |
368 | | |
369 | 3.29k | chunk->data.offset = start_offset; |
370 | 3.29k | chunk->data.size = size; |
371 | 3.29k | AddChunk(dmux, chunk); |
372 | 3.29k | return 1; |
373 | 3.29k | } |
374 | | |
375 | | // ----------------------------------------------------------------------------- |
376 | | // Primary chunk parsing |
377 | | |
378 | 22.6k | static ParseStatus ReadHeader(MemBuffer* const mem) { |
379 | 22.6k | const size_t min_size = RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE; |
380 | 22.6k | uint32_t riff_size; |
381 | | |
382 | | // Basic file level validation. |
383 | 22.6k | if (MemDataSize(mem) < min_size) return PARSE_NEED_MORE_DATA; |
384 | 21.2k | if (memcmp(GetBuffer(mem), "RIFF", CHUNK_SIZE_BYTES) || |
385 | 19.0k | memcmp(GetBuffer(mem) + CHUNK_HEADER_SIZE, "WEBP", CHUNK_SIZE_BYTES)) { |
386 | 19.0k | return PARSE_ERROR; |
387 | 19.0k | } |
388 | | |
389 | 2.13k | riff_size = GetLE32(GetBuffer(mem) + TAG_SIZE); |
390 | 2.13k | if (riff_size < CHUNK_HEADER_SIZE) return PARSE_ERROR; |
391 | 2.12k | if (riff_size > MAX_CHUNK_PAYLOAD) return PARSE_ERROR; |
392 | | |
393 | | // There's no point in reading past the end of the RIFF chunk |
394 | 2.12k | mem->riff_end = riff_size + CHUNK_HEADER_SIZE; |
395 | 2.12k | if (mem->buf_size > mem->riff_end) { |
396 | 106 | mem->buf_size = mem->end = mem->riff_end; |
397 | 106 | } |
398 | | |
399 | 2.12k | Skip(mem, RIFF_HEADER_SIZE); |
400 | 2.12k | return PARSE_OK; |
401 | 2.12k | } |
402 | | |
403 | 831 | static ParseStatus ParseSingleImage(WebPDemuxer* const dmux) { |
404 | 831 | const size_t min_size = CHUNK_HEADER_SIZE; |
405 | 831 | MemBuffer* const mem = &dmux->mem; |
406 | 831 | Frame* frame; |
407 | 831 | ParseStatus status; |
408 | 831 | int image_added = 0; |
409 | | |
410 | 831 | if (dmux->frames != NULL) return PARSE_ERROR; |
411 | 831 | if (SizeIsInvalid(mem, min_size)) return PARSE_ERROR; |
412 | 825 | if (MemDataSize(mem) < min_size) return PARSE_NEED_MORE_DATA; |
413 | | |
414 | 825 | frame = (Frame*)WebPSafeCalloc(1ULL, sizeof(*frame)); |
415 | 825 | if (frame == NULL) return PARSE_ERROR; |
416 | | |
417 | | // For the single image case we allow parsing of a partial frame, so no |
418 | | // minimum size is imposed here. |
419 | 825 | status = StoreFrame(1, 0, &dmux->mem, frame); |
420 | 825 | if (status != PARSE_ERROR) { |
421 | 593 | const int has_alpha = !!(dmux->feature_flags & ALPHA_FLAG); |
422 | | // Clear any alpha when the alpha flag is missing. |
423 | 593 | if (!has_alpha && frame->img_components[1].size > 0) { |
424 | 8 | frame->img_components[1].offset = 0; |
425 | 8 | frame->img_components[1].size = 0; |
426 | 8 | frame->has_alpha = 0; |
427 | 8 | } |
428 | | |
429 | | // Use the frame width/height as the canvas values for non-vp8x files. |
430 | | // Also, set ALPHA_FLAG if this is a lossless image with alpha. |
431 | 593 | if (!dmux->is_ext_format && frame->width > 0 && frame->height > 0) { |
432 | 486 | dmux->state = WEBP_DEMUX_PARSED_HEADER; |
433 | 486 | dmux->canvas_width = frame->width; |
434 | 486 | dmux->canvas_height = frame->height; |
435 | 486 | dmux->feature_flags |= frame->has_alpha ? ALPHA_FLAG : 0; |
436 | 486 | } |
437 | 593 | if (!AddFrame(dmux, frame)) { |
438 | 0 | status = PARSE_ERROR; // last frame was left incomplete |
439 | 593 | } else { |
440 | 593 | image_added = 1; |
441 | 593 | dmux->num_frames = 1; |
442 | 593 | } |
443 | 593 | } |
444 | | |
445 | 825 | if (!image_added) WebPSafeFree(frame); |
446 | 825 | return status; |
447 | 825 | } |
448 | | |
449 | 1.03k | static ParseStatus ParseVP8XChunks(WebPDemuxer* const dmux) { |
450 | 1.03k | const int is_animation = !!(dmux->feature_flags & ANIMATION_FLAG); |
451 | 1.03k | MemBuffer* const mem = &dmux->mem; |
452 | 1.03k | int anim_chunks = 0; |
453 | 1.03k | ParseStatus status = PARSE_OK; |
454 | | |
455 | 4.23k | do { |
456 | 4.23k | int store_chunk = 1; |
457 | 4.23k | const size_t chunk_start_offset = mem->start; |
458 | 4.23k | const uint32_t fourcc = ReadLE32(mem); |
459 | 4.23k | const uint32_t chunk_size = ReadLE32(mem); |
460 | 4.23k | uint32_t chunk_size_padded; |
461 | | |
462 | 4.23k | if (chunk_size > MAX_CHUNK_PAYLOAD) return PARSE_ERROR; |
463 | | |
464 | 4.23k | chunk_size_padded = chunk_size + (chunk_size & 1); |
465 | 4.23k | if (SizeIsInvalid(mem, chunk_size_padded)) return PARSE_ERROR; |
466 | | |
467 | 3.91k | switch (fourcc) { |
468 | 1 | case MKFOURCC('V', 'P', '8', 'X'): { |
469 | 1 | return PARSE_ERROR; |
470 | 0 | } |
471 | 18 | case MKFOURCC('A', 'L', 'P', 'H'): |
472 | 20 | case MKFOURCC('V', 'P', '8', ' '): |
473 | 70 | case MKFOURCC('V', 'P', '8', 'L'): { |
474 | | // check that this isn't an animation (all frames should be in an ANMF). |
475 | 70 | if (anim_chunks > 0 || is_animation) return PARSE_ERROR; |
476 | | |
477 | 68 | Rewind(mem, CHUNK_HEADER_SIZE); |
478 | 68 | status = ParseSingleImage(dmux); |
479 | 68 | break; |
480 | 70 | } |
481 | 33 | case MKFOURCC('A', 'N', 'I', 'M'): { |
482 | 33 | if (chunk_size_padded < ANIM_CHUNK_SIZE) return PARSE_ERROR; |
483 | | |
484 | 32 | if (MemDataSize(mem) < chunk_size_padded) { |
485 | 13 | status = PARSE_NEED_MORE_DATA; |
486 | 19 | } else if (anim_chunks == 0) { |
487 | 19 | ++anim_chunks; |
488 | 19 | dmux->bgcolor = ReadLE32(mem); |
489 | 19 | dmux->loop_count = ReadLE16s(mem); |
490 | 19 | Skip(mem, chunk_size_padded - ANIM_CHUNK_SIZE); |
491 | 19 | } else { |
492 | 0 | store_chunk = 0; |
493 | 0 | goto Skip; |
494 | 0 | } |
495 | 32 | break; |
496 | 32 | } |
497 | 32 | case MKFOURCC('A', 'N', 'M', 'F'): { |
498 | 1 | if (anim_chunks == 0) return PARSE_ERROR; // 'ANIM' precedes frames. |
499 | 0 | status = ParseAnimationFrame(dmux, chunk_size_padded); |
500 | 0 | break; |
501 | 1 | } |
502 | 3 | case MKFOURCC('I', 'C', 'C', 'P'): { |
503 | 3 | store_chunk = !!(dmux->feature_flags & ICCP_FLAG); |
504 | 3 | goto Skip; |
505 | 1 | } |
506 | 30 | case MKFOURCC('E', 'X', 'I', 'F'): { |
507 | 30 | store_chunk = !!(dmux->feature_flags & EXIF_FLAG); |
508 | 30 | goto Skip; |
509 | 1 | } |
510 | 1 | case MKFOURCC('X', 'M', 'P', ' '): { |
511 | 1 | store_chunk = !!(dmux->feature_flags & XMP_FLAG); |
512 | 1 | goto Skip; |
513 | 1 | } |
514 | 34 | Skip: |
515 | 3.80k | default: { |
516 | 3.80k | if (chunk_size_padded <= MemDataSize(mem)) { |
517 | 3.29k | if (store_chunk) { |
518 | | // Store only the chunk header and unpadded size as only the payload |
519 | | // will be returned to the user. |
520 | 3.29k | if (!StoreChunk(dmux, chunk_start_offset, |
521 | 3.29k | CHUNK_HEADER_SIZE + chunk_size)) { |
522 | 0 | return PARSE_ERROR; |
523 | 0 | } |
524 | 3.29k | } |
525 | 3.29k | Skip(mem, chunk_size_padded); |
526 | 3.29k | } else { |
527 | 514 | status = PARSE_NEED_MORE_DATA; |
528 | 514 | } |
529 | 3.80k | } |
530 | 3.91k | } |
531 | | |
532 | 3.90k | if (mem->start == mem->riff_end) { |
533 | 40 | break; |
534 | 3.86k | } else if (MemDataSize(mem) < CHUNK_HEADER_SIZE) { |
535 | 620 | status = PARSE_NEED_MORE_DATA; |
536 | 620 | } |
537 | 3.90k | } while (status == PARSE_OK); |
538 | | |
539 | 711 | return status; |
540 | 1.03k | } |
541 | | |
542 | 1.21k | static ParseStatus ParseVP8X(WebPDemuxer* const dmux) { |
543 | 1.21k | MemBuffer* const mem = &dmux->mem; |
544 | 1.21k | uint32_t vp8x_size; |
545 | | |
546 | 1.21k | if (MemDataSize(mem) < CHUNK_HEADER_SIZE) return PARSE_NEED_MORE_DATA; |
547 | | |
548 | 1.20k | dmux->is_ext_format = 1; |
549 | 1.20k | Skip(mem, TAG_SIZE); // VP8X |
550 | 1.20k | vp8x_size = ReadLE32(mem); |
551 | 1.20k | if (vp8x_size > MAX_CHUNK_PAYLOAD) return PARSE_ERROR; |
552 | 1.20k | if (vp8x_size < VP8X_CHUNK_SIZE) return PARSE_ERROR; |
553 | 1.19k | vp8x_size += vp8x_size & 1; |
554 | 1.19k | if (SizeIsInvalid(mem, vp8x_size)) return PARSE_ERROR; |
555 | 1.13k | if (MemDataSize(mem) < vp8x_size) return PARSE_NEED_MORE_DATA; |
556 | | |
557 | 1.10k | dmux->feature_flags = ReadByte(mem); |
558 | 1.10k | Skip(mem, 3); // Reserved. |
559 | 1.10k | dmux->canvas_width = 1 + ReadLE24s(mem); |
560 | 1.10k | dmux->canvas_height = 1 + ReadLE24s(mem); |
561 | 1.10k | if (dmux->canvas_width * (uint64_t)dmux->canvas_height >= MAX_IMAGE_AREA) { |
562 | 8 | return PARSE_ERROR; // image final dimension is too large |
563 | 8 | } |
564 | 1.09k | Skip(mem, vp8x_size - VP8X_CHUNK_SIZE); // skip any trailing data. |
565 | 1.09k | dmux->state = WEBP_DEMUX_PARSED_HEADER; |
566 | | |
567 | 1.09k | if (SizeIsInvalid(mem, CHUNK_HEADER_SIZE)) return PARSE_ERROR; |
568 | 1.09k | if (MemDataSize(mem) < CHUNK_HEADER_SIZE) return PARSE_NEED_MORE_DATA; |
569 | | |
570 | 1.03k | return ParseVP8XChunks(dmux); |
571 | 1.09k | } |
572 | | |
573 | | // ----------------------------------------------------------------------------- |
574 | | // Format validation |
575 | | |
576 | 520 | static int IsValidSimpleFormat(const WebPDemuxer* const dmux) { |
577 | 520 | const Frame* const frame = dmux->frames; |
578 | 520 | if (dmux->state == WEBP_DEMUX_PARSING_HEADER) return 1; |
579 | | |
580 | 481 | if (dmux->canvas_width <= 0 || dmux->canvas_height <= 0) return 0; |
581 | 481 | if (dmux->state == WEBP_DEMUX_DONE && frame == NULL) return 0; |
582 | | |
583 | 481 | if (frame->width <= 0 || frame->height <= 0) return 0; |
584 | 481 | return 1; |
585 | 481 | } |
586 | | |
587 | | // If 'exact' is true, check that the image resolution matches the canvas. |
588 | | // If 'exact' is false, check that the x/y offsets do not exceed the canvas. |
589 | | static int CheckFrameBounds(const Frame* const frame, int exact, |
590 | 48 | int canvas_width, int canvas_height) { |
591 | 48 | if (exact) { |
592 | 48 | if (frame->x_offset != 0 || frame->y_offset != 0) { |
593 | 0 | return 0; |
594 | 0 | } |
595 | 48 | if (frame->width != canvas_width || frame->height != canvas_height) { |
596 | 47 | return 0; |
597 | 47 | } |
598 | 48 | } else { |
599 | 0 | if (frame->x_offset < 0 || frame->y_offset < 0) return 0; |
600 | 0 | if (frame->width + frame->x_offset > canvas_width) return 0; |
601 | 0 | if (frame->height + frame->y_offset > canvas_height) return 0; |
602 | 0 | } |
603 | 1 | return 1; |
604 | 48 | } |
605 | | |
606 | 786 | static int IsValidExtendedFormat(const WebPDemuxer* const dmux) { |
607 | 786 | const int is_animation = !!(dmux->feature_flags & ANIMATION_FLAG); |
608 | 786 | const Frame* f = dmux->frames; |
609 | | |
610 | 786 | if (dmux->state == WEBP_DEMUX_PARSING_HEADER) return 1; |
611 | | |
612 | 752 | if (dmux->canvas_width <= 0 || dmux->canvas_height <= 0) return 0; |
613 | 752 | if (dmux->loop_count < 0) return 0; |
614 | 752 | if (dmux->state == WEBP_DEMUX_DONE && dmux->frames == NULL) return 0; |
615 | 712 | if (dmux->feature_flags & ~ALL_VALID_FLAGS) return 0; // invalid bitstream |
616 | | |
617 | 312 | while (f != NULL) { |
618 | 65 | const int cur_frame_set = f->frame_num; |
619 | | |
620 | | // Check frame properties. |
621 | 83 | for (; f != NULL && f->frame_num == cur_frame_set; f = f->next) { |
622 | 65 | const ChunkData* const image = f->img_components; |
623 | 65 | const ChunkData* const alpha = f->img_components + 1; |
624 | | |
625 | 65 | if (!is_animation && f->frame_num > 1) return 0; |
626 | | |
627 | 65 | if (f->complete) { |
628 | 0 | if (alpha->size == 0 && image->size == 0) return 0; |
629 | | // Ensure alpha precedes image bitstream. |
630 | 0 | if (alpha->size > 0 && alpha->offset > image->offset) { |
631 | 0 | return 0; |
632 | 0 | } |
633 | | |
634 | 0 | if (f->width <= 0 || f->height <= 0) return 0; |
635 | 65 | } else { |
636 | | // There shouldn't be a partial frame in a complete file. |
637 | 65 | if (dmux->state == WEBP_DEMUX_DONE) return 0; |
638 | | |
639 | | // Ensure alpha precedes image bitstream. |
640 | 65 | if (alpha->size > 0 && image->size > 0 && |
641 | 0 | alpha->offset > image->offset) { |
642 | 0 | return 0; |
643 | 0 | } |
644 | | // There shouldn't be any frames after an incomplete one. |
645 | 65 | if (f->next != NULL) return 0; |
646 | 65 | } |
647 | | |
648 | 65 | if (f->width > 0 && f->height > 0 && |
649 | 48 | !CheckFrameBounds(f, !is_animation, dmux->canvas_width, |
650 | 48 | dmux->canvas_height)) { |
651 | 47 | return 0; |
652 | 47 | } |
653 | 65 | } |
654 | 65 | } |
655 | 247 | return 1; |
656 | 294 | } |
657 | | |
658 | | // ----------------------------------------------------------------------------- |
659 | | // WebPDemuxer object |
660 | | |
661 | 19.5k | static void InitDemux(WebPDemuxer* const dmux, const MemBuffer* const mem) { |
662 | 19.5k | dmux->state = WEBP_DEMUX_PARSING_HEADER; |
663 | 19.5k | dmux->loop_count = 1; |
664 | 19.5k | dmux->bgcolor = 0xFFFFFFFF; // White background by default. |
665 | 19.5k | dmux->canvas_width = -1; |
666 | 19.5k | dmux->canvas_height = -1; |
667 | 19.5k | dmux->frames_tail = &dmux->frames; |
668 | 19.5k | dmux->chunks_tail = &dmux->chunks; |
669 | 19.5k | dmux->mem = *mem; |
670 | 19.5k | } |
671 | | |
672 | | static ParseStatus CreateRawImageDemuxer(MemBuffer* const mem, |
673 | 19.0k | WebPDemuxer** demuxer) { |
674 | 19.0k | WebPBitstreamFeatures features; |
675 | 19.0k | const VP8StatusCode status = |
676 | 19.0k | WebPGetFeatures(mem->buf, mem->buf_size, &features); |
677 | 19.0k | *demuxer = NULL; |
678 | 19.0k | if (status != VP8_STATUS_OK) { |
679 | 504 | return (status == VP8_STATUS_NOT_ENOUGH_DATA) ? PARSE_NEED_MORE_DATA |
680 | 504 | : PARSE_ERROR; |
681 | 504 | } |
682 | | |
683 | 18.5k | { |
684 | 18.5k | WebPDemuxer* const dmux = (WebPDemuxer*)WebPSafeCalloc(1ULL, sizeof(*dmux)); |
685 | 18.5k | Frame* const frame = (Frame*)WebPSafeCalloc(1ULL, sizeof(*frame)); |
686 | 18.5k | if (dmux == NULL || frame == NULL) goto Error; |
687 | 17.5k | InitDemux(dmux, mem); |
688 | 17.5k | SetFrameInfo(0, mem->buf_size, 1 /*frame_num*/, 1 /*complete*/, &features, |
689 | 17.5k | frame); |
690 | 17.5k | if (!AddFrame(dmux, frame)) goto Error; |
691 | 17.5k | dmux->state = WEBP_DEMUX_DONE; |
692 | 17.5k | dmux->canvas_width = frame->width; |
693 | 17.5k | dmux->canvas_height = frame->height; |
694 | 17.5k | dmux->feature_flags |= frame->has_alpha ? ALPHA_FLAG : 0; |
695 | 17.5k | dmux->num_frames = 1; |
696 | 17.5k | assert(IsValidSimpleFormat(dmux)); |
697 | 17.5k | *demuxer = dmux; |
698 | 17.5k | return PARSE_OK; |
699 | | |
700 | 1.06k | Error: |
701 | 1.06k | WebPSafeFree(dmux); |
702 | 1.06k | WebPSafeFree(frame); |
703 | 1.06k | return PARSE_ERROR; |
704 | 17.5k | } |
705 | 17.5k | } |
706 | | |
707 | | WebPDemuxer* WebPDemuxInternal(const WebPData* data, int allow_partial, |
708 | 22.7k | WebPDemuxState* state, int version) { |
709 | 22.7k | const ChunkParser* parser; |
710 | 22.7k | int partial; |
711 | 22.7k | ParseStatus status = PARSE_ERROR; |
712 | 22.7k | MemBuffer mem; |
713 | 22.7k | WebPDemuxer* dmux; |
714 | | |
715 | 22.7k | if (state != NULL) *state = WEBP_DEMUX_PARSE_ERROR; |
716 | | |
717 | 22.7k | if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DEMUX_ABI_VERSION)) return NULL; |
718 | 22.7k | if (data == NULL || data->bytes == NULL || data->size == 0) return NULL; |
719 | | |
720 | 22.6k | if (!InitMemBuffer(&mem, data->bytes, data->size)) return NULL; |
721 | 22.6k | status = ReadHeader(&mem); |
722 | 22.6k | if (status != PARSE_OK) { |
723 | | // If parsing of the webp file header fails attempt to handle a raw |
724 | | // VP8/VP8L frame. Note 'allow_partial' is ignored in this case. |
725 | 20.5k | if (status == PARSE_ERROR) { |
726 | 19.0k | status = CreateRawImageDemuxer(&mem, &dmux); |
727 | 19.0k | if (status == PARSE_OK) { |
728 | 17.5k | if (state != NULL) *state = WEBP_DEMUX_DONE; |
729 | 17.5k | return dmux; |
730 | 17.5k | } |
731 | 19.0k | } |
732 | 3.04k | if (state != NULL) { |
733 | 71 | *state = (status == PARSE_NEED_MORE_DATA) ? WEBP_DEMUX_PARSING_HEADER |
734 | 71 | : WEBP_DEMUX_PARSE_ERROR; |
735 | 71 | } |
736 | 3.04k | return NULL; |
737 | 20.5k | } |
738 | | |
739 | 2.12k | partial = (mem.buf_size < mem.riff_end); |
740 | 2.12k | if (!allow_partial && partial) return NULL; |
741 | | |
742 | 1.98k | dmux = (WebPDemuxer*)WebPSafeCalloc(1ULL, sizeof(*dmux)); |
743 | 1.98k | if (dmux == NULL) return NULL; |
744 | 1.98k | InitDemux(dmux, &mem); |
745 | | |
746 | 1.98k | status = PARSE_ERROR; |
747 | 5.11k | for (parser = kMasterChunks; parser->parse != NULL; ++parser) { |
748 | 5.10k | if (!memcmp(parser->id, GetBuffer(&dmux->mem), TAG_SIZE)) { |
749 | 1.97k | status = parser->parse(dmux); |
750 | 1.97k | if (status == PARSE_OK) dmux->state = WEBP_DEMUX_DONE; |
751 | 1.97k | if (status == PARSE_NEED_MORE_DATA && !partial) status = PARSE_ERROR; |
752 | 1.97k | if (status != PARSE_ERROR && !parser->valid(dmux)) status = PARSE_ERROR; |
753 | 1.97k | if (status == PARSE_ERROR) dmux->state = WEBP_DEMUX_PARSE_ERROR; |
754 | 1.97k | break; |
755 | 1.97k | } |
756 | 5.10k | } |
757 | 1.98k | if (state != NULL) *state = dmux->state; |
758 | | |
759 | 1.98k | if (status == PARSE_ERROR) { |
760 | 1.18k | WebPDemuxDelete(dmux); |
761 | 1.18k | return NULL; |
762 | 1.18k | } |
763 | 801 | return dmux; |
764 | 1.98k | } |
765 | | |
766 | 21.8k | void WebPDemuxDelete(WebPDemuxer* dmux) { |
767 | 21.8k | Chunk* c; |
768 | 21.8k | Frame* f; |
769 | 21.8k | if (dmux == NULL) return; |
770 | | |
771 | 37.6k | for (f = dmux->frames; f != NULL;) { |
772 | 18.1k | Frame* const cur_frame = f; |
773 | 18.1k | f = f->next; |
774 | 18.1k | WebPSafeFree(cur_frame); |
775 | 18.1k | } |
776 | 22.7k | for (c = dmux->chunks; c != NULL;) { |
777 | 3.29k | Chunk* const cur_chunk = c; |
778 | 3.29k | c = c->next; |
779 | 3.29k | WebPSafeFree(cur_chunk); |
780 | 3.29k | } |
781 | 19.5k | WebPSafeFree(dmux); |
782 | 19.5k | } |
783 | | |
784 | | // ----------------------------------------------------------------------------- |
785 | | |
786 | 75.4k | uint32_t WebPDemuxGetI(const WebPDemuxer* dmux, WebPFormatFeature feature) { |
787 | 75.4k | if (dmux == NULL) return 0; |
788 | | |
789 | 75.4k | switch (feature) { |
790 | 237 | case WEBP_FF_FORMAT_FLAGS: |
791 | 237 | return dmux->feature_flags; |
792 | 17.3k | case WEBP_FF_CANVAS_WIDTH: |
793 | 17.3k | return (uint32_t)dmux->canvas_width; |
794 | 17.3k | case WEBP_FF_CANVAS_HEIGHT: |
795 | 17.3k | return (uint32_t)dmux->canvas_height; |
796 | 13.5k | case WEBP_FF_LOOP_COUNT: |
797 | 13.5k | return (uint32_t)dmux->loop_count; |
798 | 13.5k | case WEBP_FF_BACKGROUND_COLOR: |
799 | 13.5k | return dmux->bgcolor; |
800 | 13.5k | case WEBP_FF_FRAME_COUNT: |
801 | 13.5k | return (uint32_t)dmux->num_frames; |
802 | 75.4k | } |
803 | 0 | return 0; |
804 | 75.4k | } |
805 | | |
806 | | // ----------------------------------------------------------------------------- |
807 | | // Frame iteration |
808 | | |
809 | 17.4k | static const Frame* GetFrame(const WebPDemuxer* const dmux, int frame_num) { |
810 | 17.4k | const Frame* f; |
811 | 17.4k | for (f = dmux->frames; f != NULL; f = f->next) { |
812 | 17.4k | if (frame_num == f->frame_num) break; |
813 | 17.4k | } |
814 | 17.4k | return f; |
815 | 17.4k | } |
816 | | |
817 | | static const uint8_t* GetFramePayload(const uint8_t* const mem_buf, |
818 | | const Frame* const frame, |
819 | 17.4k | size_t* const data_size) { |
820 | 17.4k | *data_size = 0; |
821 | 17.4k | if (frame != NULL) { |
822 | 17.4k | const ChunkData* const image = frame->img_components; |
823 | 17.4k | const ChunkData* const alpha = frame->img_components + 1; |
824 | 17.4k | size_t start_offset = image->offset; |
825 | 17.4k | *data_size = image->size; |
826 | | |
827 | | // if alpha exists it precedes image, update the size allowing for |
828 | | // intervening chunks. |
829 | 17.4k | if (alpha->size > 0) { |
830 | 16 | const size_t inter_size = |
831 | 16 | (image->offset > 0) ? image->offset - (alpha->offset + alpha->size) |
832 | 16 | : 0; |
833 | 16 | start_offset = alpha->offset; |
834 | 16 | *data_size += alpha->size + inter_size; |
835 | 16 | } |
836 | 17.4k | return mem_buf + start_offset; |
837 | 17.4k | } |
838 | 0 | return NULL; |
839 | 17.4k | } |
840 | | |
841 | | // Create a whole 'frame' from VP8 (+ alpha) or lossless. |
842 | | static int SynthesizeFrame(const WebPDemuxer* const dmux, |
843 | 17.4k | const Frame* const frame, WebPIterator* const iter) { |
844 | 17.4k | const uint8_t* const mem_buf = dmux->mem.buf; |
845 | 17.4k | size_t payload_size = 0; |
846 | 17.4k | const uint8_t* const payload = GetFramePayload(mem_buf, frame, &payload_size); |
847 | 17.4k | if (payload == NULL) return 0; |
848 | 17.4k | assert(frame != NULL); |
849 | | |
850 | 17.4k | iter->frame_num = frame->frame_num; |
851 | 17.4k | iter->num_frames = dmux->num_frames; |
852 | 17.4k | iter->x_offset = frame->x_offset; |
853 | 17.4k | iter->y_offset = frame->y_offset; |
854 | 17.4k | iter->width = frame->width; |
855 | 17.4k | iter->height = frame->height; |
856 | 17.4k | iter->has_alpha = frame->has_alpha; |
857 | 17.4k | iter->duration = frame->duration; |
858 | 17.4k | iter->dispose_method = frame->dispose_method; |
859 | 17.4k | iter->blend_method = frame->blend_method; |
860 | 17.4k | iter->complete = frame->complete; |
861 | 17.4k | iter->fragment.bytes = payload; |
862 | 17.4k | iter->fragment.size = payload_size; |
863 | 17.4k | return 1; |
864 | 17.4k | } |
865 | | |
866 | 21.9k | static int SetFrame(int frame_num, WebPIterator* const iter) { |
867 | 21.9k | const Frame* frame; |
868 | 21.9k | const WebPDemuxer* const dmux = (WebPDemuxer*)iter->private_; |
869 | 21.9k | if (dmux == NULL || frame_num < 0) return 0; |
870 | 21.9k | if (frame_num > dmux->num_frames) return 0; |
871 | 17.4k | if (frame_num == 0) frame_num = dmux->num_frames; |
872 | | |
873 | 17.4k | frame = GetFrame(dmux, frame_num); |
874 | 17.4k | if (frame == NULL) return 0; |
875 | | |
876 | 17.4k | return SynthesizeFrame(dmux, frame, iter); |
877 | 17.4k | } |
878 | | |
879 | 21.5k | int WebPDemuxGetFrame(const WebPDemuxer* dmux, int frame, WebPIterator* iter) { |
880 | 21.5k | if (iter == NULL) return 0; |
881 | | |
882 | 21.5k | WEBP_UNSAFE_MEMSET(iter, 0, sizeof(*iter)); |
883 | 21.5k | iter->private_ = (void*)dmux; |
884 | 21.5k | return SetFrame(frame, iter); |
885 | 21.5k | } |
886 | | |
887 | 441 | int WebPDemuxNextFrame(WebPIterator* iter) { |
888 | 441 | if (iter == NULL) return 0; |
889 | 441 | return SetFrame(iter->frame_num + 1, iter); |
890 | 441 | } |
891 | | |
892 | 0 | int WebPDemuxPrevFrame(WebPIterator* iter) { |
893 | 0 | if (iter == NULL) return 0; |
894 | 0 | if (iter->frame_num <= 1) return 0; |
895 | 0 | return SetFrame(iter->frame_num - 1, iter); |
896 | 0 | } |
897 | | |
898 | 46.1k | void WebPDemuxReleaseIterator(WebPIterator* iter) { (void)iter; } |
899 | | |
900 | | // ----------------------------------------------------------------------------- |
901 | | // Chunk iteration |
902 | | |
903 | 1.36k | static int ChunkCount(const WebPDemuxer* const dmux, const char fourcc[4]) { |
904 | 1.36k | const uint8_t* const mem_buf = dmux->mem.buf; |
905 | 1.36k | const Chunk* c; |
906 | 1.36k | int count = 0; |
907 | 5.38k | for (c = dmux->chunks; c != NULL; c = c->next) { |
908 | 4.01k | const uint8_t* const header = mem_buf + c->data.offset; |
909 | 4.01k | if (!memcmp(header, fourcc, TAG_SIZE)) ++count; |
910 | 4.01k | } |
911 | 1.36k | return count; |
912 | 1.36k | } |
913 | | |
914 | | static const Chunk* GetChunk(const WebPDemuxer* const dmux, |
915 | 23 | const char fourcc[4], int chunk_num) { |
916 | 23 | const uint8_t* const mem_buf = dmux->mem.buf; |
917 | 23 | const Chunk* c; |
918 | 23 | int count = 0; |
919 | 218 | for (c = dmux->chunks; c != NULL; c = c->next) { |
920 | 218 | const uint8_t* const header = mem_buf + c->data.offset; |
921 | 218 | if (!memcmp(header, fourcc, TAG_SIZE)) ++count; |
922 | 218 | if (count == chunk_num) break; |
923 | 218 | } |
924 | 23 | return c; |
925 | 23 | } |
926 | | |
927 | | static int SetChunk(const char fourcc[4], int chunk_num, |
928 | 1.36k | WebPChunkIterator* const iter) { |
929 | 1.36k | const WebPDemuxer* const dmux = (WebPDemuxer*)iter->private_; |
930 | 1.36k | int count; |
931 | | |
932 | 1.36k | if (dmux == NULL || fourcc == NULL || chunk_num < 0) return 0; |
933 | 1.36k | count = ChunkCount(dmux, fourcc); |
934 | 1.36k | if (count == 0) return 0; |
935 | 45 | if (chunk_num == 0) chunk_num = count; |
936 | | |
937 | 45 | if (chunk_num <= count) { |
938 | 23 | const uint8_t* const mem_buf = dmux->mem.buf; |
939 | 23 | const Chunk* const chunk = GetChunk(dmux, fourcc, chunk_num); |
940 | 23 | iter->chunk.bytes = mem_buf + chunk->data.offset + CHUNK_HEADER_SIZE; |
941 | 23 | iter->chunk.size = chunk->data.size - CHUNK_HEADER_SIZE; |
942 | 23 | iter->num_chunks = count; |
943 | 23 | iter->chunk_num = chunk_num; |
944 | 23 | return 1; |
945 | 23 | } |
946 | 22 | return 0; |
947 | 45 | } |
948 | | |
949 | | int WebPDemuxGetChunk(const WebPDemuxer* dmux, const char fourcc[4], |
950 | 1.34k | int chunk_num, WebPChunkIterator* iter) { |
951 | 1.34k | if (iter == NULL) return 0; |
952 | | |
953 | 1.34k | WEBP_UNSAFE_MEMSET(iter, 0, sizeof(*iter)); |
954 | 1.34k | iter->private_ = (void*)dmux; |
955 | 1.34k | return SetChunk(fourcc, chunk_num, iter); |
956 | 1.34k | } |
957 | | |
958 | 22 | int WebPDemuxNextChunk(WebPChunkIterator* iter) { |
959 | 22 | if (iter != NULL) { |
960 | 22 | const char* const fourcc = |
961 | 22 | (const char*)iter->chunk.bytes - CHUNK_HEADER_SIZE; |
962 | 22 | return SetChunk(fourcc, iter->chunk_num + 1, iter); |
963 | 22 | } |
964 | 0 | return 0; |
965 | 22 | } |
966 | | |
967 | 1 | int WebPDemuxPrevChunk(WebPChunkIterator* iter) { |
968 | 1 | if (iter != NULL && iter->chunk_num > 1) { |
969 | 0 | const char* const fourcc = |
970 | 0 | (const char*)iter->chunk.bytes - CHUNK_HEADER_SIZE; |
971 | 0 | return SetChunk(fourcc, iter->chunk_num - 1, iter); |
972 | 0 | } |
973 | 1 | return 0; |
974 | 1 | } |
975 | | |
976 | 1.34k | void WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter) { (void)iter; } |