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