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