fix all bare malloc() realloc() etc calls to have a proper cast in front
[flac.git] / src / libOggFLAC / ogg_helper.c
1 /* libOggFLAC - Free Lossless Audio Codec + Ogg library
2  * Copyright (C) 2004  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <stdlib.h> /* for malloc() */
33 #include <string.h> /* for memcmp(), memcpy() */
34 #include "FLAC/assert.h"
35 #include "private/ogg_helper.h"
36 #include "protected/seekable_stream_encoder.h"
37
38
39 static FLAC__bool full_read_(OggFLAC__SeekableStreamEncoder *encoder, FLAC__byte *buffer, unsigned bytes, OggFLAC__SeekableStreamEncoderReadCallback read_callback, void *client_data)
40 {
41         while(bytes > 0) {
42                 unsigned bytes_read = bytes;
43                 switch(read_callback(encoder, buffer, &bytes_read, client_data)) {
44                         case OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_CONTINUE:
45                                 bytes -= bytes_read;
46                                 buffer += bytes_read;
47                                 break;
48                         case OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_END_OF_STREAM:
49                                 if(bytes_read == 0) {
50                                         encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_OGG_ERROR;
51                                         return false;
52                                 }
53                                 bytes -= bytes_read;
54                                 buffer += bytes_read;
55                                 break;
56                         case OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_ABORT:
57                                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_READ_ERROR;
58                                 return false;
59                         default:
60                                 /* double protection: */
61                                 FLAC__ASSERT(0);
62                                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_READ_ERROR;
63                                 return false;
64                 }
65         }
66
67         return true;
68 }
69
70 void simple_ogg_page__init(ogg_page *page)
71 {
72         page->header = 0;
73         page->header_len = 0;
74         page->body = 0;
75         page->body_len = 0;
76 }
77
78 void simple_ogg_page__clear(ogg_page *page)
79 {
80         if(page->header)
81                 free(page->header);
82         if(page->body)
83                 free(page->body);
84         simple_ogg_page__init(page);
85 }
86
87 FLAC__bool simple_ogg_page__get_at(OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 position, ogg_page *page, OggFLAC__SeekableStreamEncoderSeekCallback seek_callback, OggFLAC__SeekableStreamEncoderReadCallback read_callback, void *client_data)
88 {
89         static const unsigned OGG_HEADER_FIXED_PORTION_LEN = 27;
90         static const unsigned OGG_MAX_HEADER_LEN = 27/*OGG_HEADER_FIXED_PORTION_LEN*/ + 255;
91         FLAC__byte crc[4];
92
93         FLAC__ASSERT(page->header == 0);
94         FLAC__ASSERT(page->header_len == 0);
95         FLAC__ASSERT(page->body == 0);
96         FLAC__ASSERT(page->body_len == 0);
97
98         /* move the stream pointer to the supposed beginning of the page */
99         if(seek_callback(encoder, position, client_data) != FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK) {
100                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR;
101                 return false;
102         }
103
104         /* allocate space for the page header */
105         if(0 == (page->header = (unsigned char *)malloc(OGG_MAX_HEADER_LEN))) {
106                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
107                 return false;
108         }
109
110         /* read in the fixed part of the page header (up to but not including
111          * the segment table */
112         if(!full_read_(encoder, page->header, OGG_HEADER_FIXED_PORTION_LEN, read_callback, client_data))
113                 return false;
114
115         page->header_len = OGG_HEADER_FIXED_PORTION_LEN + page->header[26];
116
117         /* check to see if it's a correct, "simple" page (one packet only) */
118         if(
119                 memcmp(page->header, "OggS", 4) ||               /* doesn't start with OggS */
120                 (page->header[5] & 0x01) ||                      /* continued packet */
121                 memcmp(page->header+6, "\0\0\0\0\0\0\0\0", 8) || /* granulepos is non-zero */
122                 page->header[26] == 0                            /* packet is 0-size */
123         ) {
124                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_OGG_ERROR;
125                 return false;
126         }
127
128         /* read in the segment table */
129         if(!full_read_(encoder, page->header + OGG_HEADER_FIXED_PORTION_LEN, page->header[26], read_callback, client_data))
130                 return false;
131
132         {
133                 unsigned i;
134
135                 /* check to see that it specifies a single packet */
136                 for(i = 0; i < (unsigned)page->header[26] - 1; i++) {
137                         if(page->header[i + OGG_HEADER_FIXED_PORTION_LEN] != 255) {
138                                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_OGG_ERROR;
139                                 return false;
140                         }
141                 }
142
143                 page->body_len = 255 * i + page->header[i];
144         }
145
146         /* allocate space for the page body */
147         if(0 == (page->body = (unsigned char *)malloc(page->body_len))) {
148                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
149                 return false;
150         }
151
152         /* read in the page body */
153         if(!full_read_(encoder, page->body, page->body_len, read_callback, client_data))
154                 return false;
155
156         /* check the CRC */
157         memcpy(crc, page->header+22, 4);
158         ogg_page_checksum_set(page);
159         if(memcmp(crc, page->header+22, 4)) {
160                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_OGG_ERROR;
161                 return false;
162         }
163
164         return true;
165 }
166
167 FLAC__bool simple_ogg_page__set_at(OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 position, ogg_page *page, OggFLAC__SeekableStreamEncoderSeekCallback seek_callback, OggFLAC__SeekableStreamEncoderWriteCallback write_callback, void *client_data)
168 {
169         FLAC__ASSERT(page->header != 0);
170         FLAC__ASSERT(page->header_len != 0);
171         FLAC__ASSERT(page->body != 0);
172         FLAC__ASSERT(page->body_len != 0);
173
174         /* move the stream pointer to the supposed beginning of the page */
175         if(seek_callback(encoder, position, client_data) != FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK) {
176                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR;
177                 return false;
178         }
179
180         ogg_page_checksum_set(page);
181
182         /* re-write the page */
183         if(write_callback(encoder, page->header, page->header_len, 0, 0, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
184                 encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR;
185                 return false;
186         }
187
188         return true;
189 }