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