Integrate Github Actions
[flac.git] / src / libFLAC / ogg_helper.c
blobcbe5bc6111e4e59468e569bfc6a40fe23ca4ea53
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
7 * are met:
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.
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
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)
47 while(bytes > 0) {
48 size_t bytes_read = bytes;
49 switch(read_callback(encoder, buffer, &bytes_read, client_data)) {
50 case FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE:
51 bytes -= bytes_read;
52 buffer += bytes_read;
53 break;
54 case FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM:
55 if(bytes_read == 0) {
56 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
57 return false;
59 bytes -= bytes_read;
60 buffer += bytes_read;
61 break;
62 case FLAC__STREAM_ENCODER_READ_STATUS_ABORT:
63 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
64 return false;
65 case FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED:
66 return false;
67 default:
68 /* double protection: */
69 FLAC__ASSERT(0);
70 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
71 return false;
75 return true;
78 void simple_ogg_page__init(ogg_page *page)
80 page->header = 0;
81 page->header_len = 0;
82 page->body = 0;
83 page->body_len = 0;
86 void simple_ogg_page__clear(ogg_page *page)
88 if(page->header)
89 free(page->header);
90 if(page->body)
91 free(page->body);
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;
99 FLAC__byte crc[4];
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)
109 return false;
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;
113 return false;
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;
119 return false;
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))
125 return false;
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;
137 return false;
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))
142 return false;
145 uint32_t i;
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;
151 return false;
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;
161 return false;
164 /* read in the page body */
165 if(!full_read_(encoder, page->body, page->body_len, read_callback, client_data))
166 return false;
168 /* check the CRC */
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;
173 return false;
176 return true;
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)
190 return false;
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;
194 return false;
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;
202 return false;
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;
206 return false;
209 return true;