1 /* libFLAC - Free Lossless Audio Codec
2 * Copyright (C) 2004-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <stdlib.h> /* for malloc() */
38 #include <string.h> /* for memcmp(), memcpy() */
39 #include "FLAC/assert.h"
40 #include "share/alloc.h"
41 #include "private/ogg_helper.h"
42 #include "protected/stream_encoder.h"
45 static FLAC__bool
full_read_(FLAC__StreamEncoder
*encoder
, FLAC__byte
*buffer
, size_t bytes
, FLAC__StreamEncoderReadCallback read_callback
, void *client_data
)
48 size_t bytes_read
= bytes
;
49 switch(read_callback(encoder
, buffer
, &bytes_read
, client_data
)) {
50 case FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE
:
54 case FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM
:
56 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_OGG_ERROR
;
62 case FLAC__STREAM_ENCODER_READ_STATUS_ABORT
:
63 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_CLIENT_ERROR
;
65 case FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED
:
68 /* double protection: */
70 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_CLIENT_ERROR
;
78 void simple_ogg_page__init(ogg_page
*page
)
86 void simple_ogg_page__clear(ogg_page
*page
)
92 simple_ogg_page__init(page
);
95 FLAC__bool
simple_ogg_page__get_at(FLAC__StreamEncoder
*encoder
, FLAC__uint64 position
, ogg_page
*page
, FLAC__StreamEncoderSeekCallback seek_callback
, FLAC__StreamEncoderReadCallback read_callback
, void *client_data
)
97 static const uint32_t OGG_HEADER_FIXED_PORTION_LEN
= 27;
98 static const uint32_t OGG_MAX_HEADER_LEN
= 27/*OGG_HEADER_FIXED_PORTION_LEN*/ + 255;
100 FLAC__StreamEncoderSeekStatus seek_status
;
102 FLAC__ASSERT(page
->header
== 0);
103 FLAC__ASSERT(page
->header_len
== 0);
104 FLAC__ASSERT(page
->body
== 0);
105 FLAC__ASSERT(page
->body_len
== 0);
107 /* move the stream pointer to the supposed beginning of the page */
108 if(0 == seek_callback
)
110 if((seek_status
= seek_callback((FLAC__StreamEncoder
*)encoder
, position
, client_data
)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK
) {
111 if(seek_status
== FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR
)
112 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_CLIENT_ERROR
;
116 /* allocate space for the page header */
117 if(0 == (page
->header
= safe_malloc_(OGG_MAX_HEADER_LEN
))) {
118 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR
;
122 /* read in the fixed part of the page header (up to but not including
123 * the segment table */
124 if(!full_read_(encoder
, page
->header
, OGG_HEADER_FIXED_PORTION_LEN
, read_callback
, client_data
))
127 page
->header_len
= OGG_HEADER_FIXED_PORTION_LEN
+ page
->header
[26];
129 /* check to see if it's a correct, "simple" page (one packet only) */
131 memcmp(page
->header
, "OggS", 4) || /* doesn't start with OggS */
132 (page
->header
[5] & 0x01) || /* continued packet */
133 memcmp(page
->header
+6, "\0\0\0\0\0\0\0\0", 8) || /* granulepos is non-zero */
134 page
->header
[26] == 0 /* packet is 0-size */
136 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_OGG_ERROR
;
140 /* read in the segment table */
141 if(!full_read_(encoder
, page
->header
+ OGG_HEADER_FIXED_PORTION_LEN
, page
->header
[26], read_callback
, client_data
))
147 /* check to see that it specifies a single packet */
148 for(i
= 0; i
< (uint32_t)page
->header
[26] - 1; i
++) {
149 if(page
->header
[i
+ OGG_HEADER_FIXED_PORTION_LEN
] != 255) {
150 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_OGG_ERROR
;
155 page
->body_len
= 255 * i
+ page
->header
[i
+ OGG_HEADER_FIXED_PORTION_LEN
];
158 /* allocate space for the page body */
159 if(0 == (page
->body
= safe_malloc_(page
->body_len
))) {
160 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR
;
164 /* read in the page body */
165 if(!full_read_(encoder
, page
->body
, page
->body_len
, read_callback
, client_data
))
169 memcpy(crc
, page
->header
+22, 4);
170 ogg_page_checksum_set(page
);
171 if(memcmp(crc
, page
->header
+22, 4)) {
172 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_OGG_ERROR
;
179 FLAC__bool
simple_ogg_page__set_at(FLAC__StreamEncoder
*encoder
, FLAC__uint64 position
, ogg_page
*page
, FLAC__StreamEncoderSeekCallback seek_callback
, FLAC__StreamEncoderWriteCallback write_callback
, void *client_data
)
181 FLAC__StreamEncoderSeekStatus seek_status
;
183 FLAC__ASSERT(page
->header
!= 0);
184 FLAC__ASSERT(page
->header_len
!= 0);
185 FLAC__ASSERT(page
->body
!= 0);
186 FLAC__ASSERT(page
->body_len
!= 0);
188 /* move the stream pointer to the supposed beginning of the page */
189 if(0 == seek_callback
)
191 if((seek_status
= seek_callback((FLAC__StreamEncoder
*)encoder
, position
, client_data
)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK
) {
192 if(seek_status
== FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR
)
193 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_CLIENT_ERROR
;
197 ogg_page_checksum_set(page
);
199 /* re-write the page */
200 if(write_callback((FLAC__StreamEncoder
*)encoder
, page
->header
, page
->header_len
, 0, 0, client_data
) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK
) {
201 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_CLIENT_ERROR
;
204 if(write_callback((FLAC__StreamEncoder
*)encoder
, page
->body
, page
->body_len
, 0, 0, client_data
) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK
) {
205 encoder
->protected_
->state
= FLAC__STREAM_ENCODER_CLIENT_ERROR
;