Code cleanup.
[ruby-vorbistagger.git] / ext / vcedit.c
1 /*
2  * Copyright (C) 2000-2001 Michael Smith (msmith at xiph org)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation, version 2.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16  * MA 02110-1301 USA
17  */
18
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <unistd.h>
26 #include <ogg/ogg.h>
27 #include <vorbis/codec.h>
28 #include <assert.h>
29
30 #include "vcedit.h"
31
32 #define CHUNKSIZE 4096
33
34 struct vcedit_state_St {
35         int refcount;
36
37         ogg_sync_state oy;
38         ogg_stream_state os;
39
40         vorbis_comment vc;
41         vorbis_info vi;
42
43         FILE *in;
44         bool opened;
45         long serial;
46         unsigned char *mainbuf;
47         unsigned char *bookbuf;
48         int     mainlen;
49         int     booklen;
50         char *vendor;
51         int prevW;
52         int extrapage;
53         int eosin;
54
55         char filename[0];
56 };
57
58 static void
59 vcedit_state_free (vcedit_state *state)
60 {
61         free (state->mainbuf);
62         free (state->bookbuf);
63         free (state->vendor);
64
65         if (state->in) {
66                 fclose (state->in);
67                 state->in = NULL;
68         }
69
70         free (state);
71 }
72
73 static bool
74 vcedit_state_init (vcedit_state *state, const char *filename)
75 {
76         state->refcount = 1;
77
78         strcpy (state->filename, filename);
79
80         return true;
81 }
82
83 vcedit_state *
84 vcedit_state_new (const char *filename)
85 {
86         vcedit_state *state;
87         size_t len;
88
89         len = strlen (filename);
90         if (len > PATH_MAX)
91                 return NULL;
92
93         state = malloc (sizeof (vcedit_state) + len + 1);
94         if (!state)
95                 return NULL;
96
97         memset (state, 0, sizeof (vcedit_state));
98
99         if (!vcedit_state_init (state, filename)) {
100                 vcedit_state_free (state);
101                 return NULL;
102         }
103
104         return state;
105 }
106
107 vorbis_comment *
108 vcedit_comments (vcedit_state *state)
109 {
110         return state->opened ? &state->vc : NULL;
111 }
112
113 static void
114 vcedit_clear_internals (vcedit_state *state)
115 {
116         ogg_stream_clear (&state->os);
117         ogg_sync_clear (&state->oy);
118
119         vorbis_info_clear (&state->vi);
120         vorbis_comment_clear (&state->vc);
121
122         free (state->vendor);
123         state->vendor = NULL;
124
125         free (state->mainbuf);
126         state->mainbuf = NULL;
127         state->mainlen = 0;
128
129         free (state->bookbuf);
130         state->bookbuf = NULL;
131         state->booklen = 0;
132
133         state->serial = 0;
134         state->opened = false;
135 }
136
137 void
138 vcedit_state_ref (vcedit_state *state)
139 {
140         state->refcount++;
141 }
142
143 void
144 vcedit_state_unref (vcedit_state *state)
145 {
146         if (--state->refcount)
147                 return;
148
149         if (state->opened)
150                 vcedit_clear_internals (state);
151
152         vcedit_state_free (state);
153 }
154
155 /* Next two functions pulled straight from libvorbis, apart from one change
156  * - we don't want to overwrite the vendor string.
157  */
158 static void
159 _v_writestring (oggpack_buffer *o, char *s, int len)
160 {
161         while (len--) {
162                 oggpack_write (o, *s++, 8);
163         }
164 }
165
166 static int
167 _commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
168 {
169         int i;
170
171         oggpack_buffer opb;
172
173         oggpack_writeinit (&opb);
174
175         /* preamble */
176         oggpack_write (&opb, 0x03, 8);
177         _v_writestring (&opb, "vorbis", 6);
178
179         /* vendor */
180         oggpack_write (&opb, strlen (vendor), 32);
181         _v_writestring (&opb, vendor, strlen (vendor));
182
183         /* comments */
184         oggpack_write (&opb, vc->comments, 32);
185
186         for (i = 0; i < vc->comments; i++) {
187                 if (!vc->user_comments[i])
188                         oggpack_write (&opb, 0, 32);
189                 else {
190                         oggpack_write (&opb, vc->comment_lengths[i], 32);
191                         _v_writestring (&opb, vc->user_comments[i],
192                                         vc->comment_lengths[i]);
193                 }
194         }
195
196         oggpack_write (&opb, 1, 1);
197
198         op->packet = _ogg_malloc (oggpack_bytes (&opb));
199         memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
200
201         op->bytes = oggpack_bytes (&opb);
202         op->b_o_s = 0;
203         op->e_o_s = 0;
204         op->granulepos = 0;
205
206         oggpack_writeclear (&opb);
207
208         return 0;
209 }
210
211 static int
212 _blocksize (vcedit_state *s, ogg_packet *p)
213 {
214         int this, ret = 0;
215
216         this = vorbis_packet_blocksize (&s->vi, p);
217
218         if (s->prevW)
219                 ret = (this + s->prevW) / 4;
220
221         s->prevW = this;
222
223         return ret;
224 }
225
226 static int
227 _fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
228 {
229         char *buffer;
230         int result, bytes;
231
232         result = ogg_stream_packetout (&s->os, p);
233
234         if (result > 0)
235                 return 1;
236
237         if (s->eosin)
238                 return 0;
239
240         while (ogg_sync_pageout (&s->oy, page) <= 0) {
241                 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
242                 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
243                 ogg_sync_wrote (&s->oy, bytes);
244
245                 if (!bytes)
246                         return 0;
247         }
248
249         if (ogg_page_eos (page))
250                 s->eosin = 1;
251         else if (ogg_page_serialno (page) != s->serial) {
252                 s->eosin = 1;
253                 s->extrapage = 1;
254                 return 0;
255         }
256
257         ogg_stream_pagein (&s->os, page);
258
259         return _fetch_next_packet (s, p, page);
260 }
261
262 vcedit_error
263 vcedit_open (vcedit_state *state)
264 {
265         vcedit_error ret;
266         char *buffer;
267         int bytes, i;
268         int chunks = 0;
269         ogg_packet *header;
270         ogg_packet header_main, header_comments, header_codebooks;
271         ogg_page og;
272
273         state->in = fopen (state->filename, "rb");
274         if (!state->in)
275                 return VCEDIT_ERR_OPEN;
276
277         ogg_sync_init (&state->oy);
278
279         while (1) {
280                 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
281                 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
282
283                 ogg_sync_wrote (&state->oy, bytes);
284
285                 if (ogg_sync_pageout (&state->oy, &og) == 1)
286                         break;
287
288                 /* Bail if we don't find data in the first 40 kB */
289                 if (chunks++ >= 10) {
290                         ogg_sync_clear (&state->oy);
291
292                         return VCEDIT_ERR_INVAL;
293                 }
294         }
295
296         state->serial = ogg_page_serialno (&og);
297
298         ogg_stream_init (&state->os, state->serial);
299         vorbis_info_init (&state->vi);
300         vorbis_comment_init (&state->vc);
301
302         if (ogg_stream_pagein (&state->os, &og) < 0) {
303                 ret = VCEDIT_ERR_INVAL;
304                 goto err;
305         }
306
307         if (ogg_stream_packetout (&state->os, &header_main) != 1) {
308                 ret = VCEDIT_ERR_INVAL;
309                 goto err;
310         }
311
312         if (vorbis_synthesis_headerin (&state->vi, &state->vc, &header_main) < 0) {
313                 ret = VCEDIT_ERR_INVAL;
314                 goto err;
315         }
316
317         state->mainlen = header_main.bytes;
318         state->mainbuf = malloc (state->mainlen);
319         memcpy (state->mainbuf, header_main.packet, header_main.bytes);
320
321         i = 0;
322         header = &header_comments;
323
324         while (i < 2) {
325                 while (i < 2) {
326                         int result = ogg_sync_pageout (&state->oy, &og);
327
328                         if (!result)
329                                 break; /* Too little data so far */
330
331                         if (result == 1) {
332                                 ogg_stream_pagein (&state->os, &og);
333
334                                 while (i < 2) {
335                                         result = ogg_stream_packetout (&state->os, header);
336
337                                         if (!result)
338                                                 break;
339
340                                         if (result == -1) {
341                                                 ret = VCEDIT_ERR_INVAL;
342                                                 goto err;
343                                         }
344
345                                         vorbis_synthesis_headerin (&state->vi, &state->vc, header);
346
347                                         if (i == 1) {
348                                                 state->booklen = header->bytes;
349                                                 state->bookbuf = malloc (state->booklen);
350                                                 memcpy (state->bookbuf, header->packet, header->bytes);
351                                         }
352
353                                         i++;
354                                         header = &header_codebooks;
355                                 }
356                         }
357                 }
358
359                 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
360                 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
361
362                 if (bytes == 0 && i < 2) {
363                         ret = VCEDIT_ERR_INVAL;
364                         goto err;
365                 }
366
367                 ogg_sync_wrote (&state->oy, bytes);
368         }
369
370         /* Copy the vendor tag */
371         state->vendor = strdup (state->vc.vendor);
372
373         /* Headers are done! */
374         state->opened = true;
375
376         return VCEDIT_ERR_SUCCESS;
377
378 err:
379         vcedit_clear_internals (state);
380
381         return ret;
382 }
383
384 vcedit_error
385 vcedit_write (vcedit_state *state)
386 {
387         ogg_stream_state streamout;
388         ogg_packet header_main, header_comments, header_codebooks, op;
389         ogg_page ogout, ogin;
390         ogg_int64_t granpos = 0;
391         FILE *out;
392         char *buffer, tmpfile[PATH_MAX];
393         int s, result, bytes, needflush = 0, needout = 0;
394         size_t tmp;
395
396         if (!state->opened)
397                 return VCEDIT_ERR_INVAL;
398
399         strcpy (tmpfile, state->filename);
400         strcat (tmpfile, ".XXXXXX");
401
402         s = mkstemp (tmpfile);
403         if (s == -1)
404                 return VCEDIT_ERR_TMPFILE;
405
406         out = fdopen (s, "wb");
407         if (!out) {
408                 unlink (tmpfile);
409                 close (s);
410
411                 return VCEDIT_ERR_TMPFILE;
412         }
413
414         state->prevW = state->extrapage = state->eosin = 0;
415
416         header_main.bytes = state->mainlen;
417         header_main.packet = state->mainbuf;
418         header_main.b_o_s = 1;
419         header_main.e_o_s = 0;
420         header_main.granulepos = 0;
421
422         header_codebooks.bytes = state->booklen;
423         header_codebooks.packet = state->bookbuf;
424         header_codebooks.b_o_s = 0;
425         header_codebooks.e_o_s = 0;
426         header_codebooks.granulepos = 0;
427
428         ogg_stream_init (&streamout, state->serial);
429
430         _commentheader_out (&state->vc, state->vendor, &header_comments);
431
432         ogg_stream_packetin (&streamout, &header_main);
433         ogg_stream_packetin (&streamout, &header_comments);
434         ogg_stream_packetin (&streamout, &header_codebooks);
435
436         while ((result = ogg_stream_flush (&streamout, &ogout))) {
437                 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
438                 if (tmp != (size_t) ogout.header_len)
439                         goto cleanup;
440
441                 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
442                 if (tmp != (size_t) ogout.body_len)
443                         goto cleanup;
444         }
445
446         while (_fetch_next_packet (state, &op, &ogin)) {
447                 int size;
448
449                 size = _blocksize (state, &op);
450                 granpos += size;
451
452                 if (needflush) {
453                         if (ogg_stream_flush (&streamout, &ogout)) {
454                                 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
455                                 if (tmp != (size_t) ogout.header_len)
456                                         goto cleanup;
457
458                                 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
459                                 if (tmp != (size_t) ogout.body_len)
460                                         goto cleanup;
461                         }
462                 } else if (needout) {
463                         if (ogg_stream_pageout (&streamout, &ogout)) {
464                                 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
465                                 if (tmp != (size_t) ogout.header_len)
466                                         goto cleanup;
467
468                                 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
469                                 if (tmp != (size_t) ogout.body_len)
470                                         goto cleanup;
471                         }
472                 }
473
474                 needflush = needout = 0;
475
476                 if (op.granulepos == -1) {
477                         op.granulepos = granpos;
478                         ogg_stream_packetin (&streamout, &op);
479                 } else {
480                         /* granulepos is set, validly. Use it, and force a flush to
481                          * account for shortened blocks (vcut) when appropriate
482                          */
483                         if (granpos > op.granulepos) {
484                                 granpos = op.granulepos;
485                                 ogg_stream_packetin (&streamout, &op);
486                                 needflush = 1;
487                         } else {
488                                 ogg_stream_packetin (&streamout, &op);
489                                 needout = 1;
490                         }
491                 }
492         }
493
494         streamout.e_o_s = 1;
495
496         while (ogg_stream_flush (&streamout, &ogout)) {
497                 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
498                 if (tmp != (size_t) ogout.header_len)
499                         goto cleanup;
500
501                 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
502                 if (tmp != (size_t) ogout.body_len)
503                         goto cleanup;
504         }
505
506         if (state->extrapage) {
507                 tmp = fwrite (ogin.header, 1, ogin.header_len, out);
508                 if (tmp != (size_t) ogin.header_len)
509                         goto cleanup;
510
511                 tmp = fwrite (ogin.body, 1, ogin.body_len, out);
512                 if (tmp != (size_t) ogin.body_len)
513                         goto cleanup;
514         }
515
516         /* clear it, because not all paths to here do */
517         state->eosin = 0;
518
519         while (!state->eosin) { /* We reached eos, not eof */
520                 /* We copy the rest of the stream (other logical streams)
521                  * through, a page at a time.
522                  */
523                 while (1) {
524                         result = ogg_sync_pageout (&state->oy, &ogout);
525
526                         if (!result)
527                 break;
528
529                         if (result >= 0) {
530                                 /* Don't bother going through the rest, we can just
531                                  * write the page out now
532                                  */
533                                 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
534                                 if (tmp != (size_t) ogout.header_len)
535                                         goto cleanup;
536
537                                 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
538                                 if (tmp != (size_t) ogout.body_len)
539                                         goto cleanup;
540                         }
541                 }
542
543                 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
544                 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
545                 ogg_sync_wrote (&state->oy, bytes);
546
547                 if (!bytes) {
548                         state->eosin = 1;
549                         break;
550                 }
551         }
552
553         fclose (out);
554         fclose (state->in);
555
556         unlink (state->filename);
557         rename (tmpfile, state->filename);
558
559 cleanup:
560         ogg_stream_clear (&streamout);
561
562     /* We don't ogg_packet_clear() this, because the memory was
563          * allocated in _commentheader_out(), so we mirror that here
564          */
565     _ogg_free (header_comments.packet);
566
567         free (state->mainbuf);
568         free (state->bookbuf);
569
570     state->mainbuf = state->bookbuf = NULL;
571
572         if (!state->eosin)
573                 return VCEDIT_ERR_INVAL;
574
575         vcedit_clear_internals (state);
576
577         return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
578                VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;
579 }