projects
/
ruby-vorbistagger.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Made MainTest#test_open_non_ogg_file pass.
[ruby-vorbistagger.git]
/
ext
/
vcedit.c
diff --git
a/ext/vcedit.c
b/ext/vcedit.c
index 336ce26e94f531a299c045091016dc1bfe7d60d2..1aee14c25ed1fcf4cd0b0ba2fb2d53edd903b3fa 100644
(file)
--- a/
ext/vcedit.c
+++ b/
ext/vcedit.c
@@
-31,8
+31,6
@@
#define CHUNKSIZE 4096
#define CHUNKSIZE 4096
-static int vcedit_open (vcedit_state *state);
-
struct vcedit_state_St {
int refcount;
struct vcedit_state_St {
int refcount;
@@
-45,12
+43,12
@@
struct vcedit_state_St {
char filename[PATH_MAX];
FILE *in;
char filename[PATH_MAX];
FILE *in;
+ bool opened;
long serial;
unsigned char *mainbuf;
unsigned char *bookbuf;
int mainlen;
int booklen;
long serial;
unsigned char *mainbuf;
unsigned char *bookbuf;
int mainlen;
int booklen;
- const char *lasterror;
char *vendor;
int prevW;
int extrapage;
char *vendor;
int prevW;
int extrapage;
@@
-119,26
+117,13
@@
vcedit_state_new (const char *filename)
snprintf (state->filename, sizeof (state->filename),
"%s", filename);
snprintf (state->filename, sizeof (state->filename),
"%s", filename);
- state->in = fopen (state->filename, "rb");
-
- if (vcedit_open (state) < 0) {
- vcedit_state_free (state);
- return NULL;
- }
-
return state;
}
return state;
}
-const char *
-vcedit_error (vcedit_state *state)
-{
- return state->lasterror;
-}
-
vorbis_comment *
vcedit_comments (vcedit_state *state)
{
vorbis_comment *
vcedit_comments (vcedit_state *state)
{
- return state->
vc
;
+ return state->
opened ? state->vc : NULL
;
}
static void
}
static void
@@
-162,6
+147,7
@@
vcedit_clear_internals (vcedit_state *state)
state->booklen = 0;
state->serial = 0;
state->booklen = 0;
state->serial = 0;
+ state->opened = false;
}
void
}
void
@@
-176,7
+162,9
@@
vcedit_state_unref (vcedit_state *state)
if (--state->refcount)
return;
if (--state->refcount)
return;
- vcedit_clear_internals (state);
+ if (state->opened)
+ vcedit_clear_internals (state);
+
vcedit_state_free (state);
}
vcedit_state_free (state);
}
@@
-287,9
+275,10
@@
_fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
return _fetch_next_packet (s, p, page);
}
return _fetch_next_packet (s, p, page);
}
-static int
+vcedit_error
vcedit_open (vcedit_state *state)
{
vcedit_open (vcedit_state *state)
{
+ vcedit_error ret;
char *buffer;
int bytes, i;
int chunks = 0;
char *buffer;
int bytes, i;
int chunks = 0;
@@
-297,6
+286,10
@@
vcedit_open (vcedit_state *state)
ogg_packet header_main, header_comments, header_codebooks;
ogg_page og;
ogg_packet header_main, header_comments, header_codebooks;
ogg_page og;
+ state->in = fopen (state->filename, "rb");
+ if (!state->in)
+ return VCEDIT_ERR_OPEN;
+
ogg_sync_init (state->oy);
while (1) {
ogg_sync_init (state->oy);
while (1) {
@@
-310,12
+303,9
@@
vcedit_open (vcedit_state *state)
/* Bail if we don't find data in the first 40 kB */
if (chunks++ >= 10) {
/* Bail if we don't find data in the first 40 kB */
if (chunks++ >= 10) {
- if (bytes < CHUNKSIZE)
- state->lasterror = "Input truncated or empty.";
- else
- state->lasterror = "Input is not an Ogg bitstream.";
+ ogg_sync_clear (state->oy);
-
goto err
;
+
return VCEDIT_ERR_INVAL
;
}
}
}
}
@@
-326,17
+316,17
@@
vcedit_open (vcedit_state *state)
vorbis_comment_init (state->vc);
if (ogg_stream_pagein (state->os, &og) < 0) {
vorbis_comment_init (state->vc);
if (ogg_stream_pagein (state->os, &og) < 0) {
-
state->lasterror = "Error reading first page of Ogg bitstream."
;
+
ret = VCEDIT_ERR_INVAL
;
goto err;
}
if (ogg_stream_packetout (state->os, &header_main) != 1) {
goto err;
}
if (ogg_stream_packetout (state->os, &header_main) != 1) {
-
state->lasterror = "Error reading initial header packet."
;
+
ret = VCEDIT_ERR_INVAL
;
goto err;
}
if (vorbis_synthesis_headerin (state->vi, state->vc, &header_main) < 0) {
goto err;
}
if (vorbis_synthesis_headerin (state->vi, state->vc, &header_main) < 0) {
-
state->lasterror = "Ogg bitstream does not contain vorbis data."
;
+
ret = VCEDIT_ERR_INVAL
;
goto err;
}
goto err;
}
@@
-364,7
+354,7
@@
vcedit_open (vcedit_state *state)
break;
if (result == -1) {
break;
if (result == -1) {
-
state->lasterror = "Corrupt secondary header."
;
+
ret = VCEDIT_ERR_INVAL
;
goto err;
}
goto err;
}
@@
-386,7
+376,7
@@
vcedit_open (vcedit_state *state)
bytes = fread (buffer, 1, CHUNKSIZE, state->in);
if (bytes == 0 && i < 2) {
bytes = fread (buffer, 1, CHUNKSIZE, state->in);
if (bytes == 0 && i < 2) {
-
state->lasterror = "EOF before end of vorbis headers."
;
+
ret = VCEDIT_ERR_INVAL
;
goto err;
}
goto err;
}
@@
-397,15
+387,17
@@
vcedit_open (vcedit_state *state)
state->vendor = strdup (state->vc->vendor);
/* Headers are done! */
state->vendor = strdup (state->vc->vendor);
/* Headers are done! */
- return 0;
+ state->opened = true;
+
+ return VCEDIT_ERR_SUCCESS;
err:
vcedit_clear_internals (state);
err:
vcedit_clear_internals (state);
- return
-1
;
+ return
ret
;
}
}
-int
+vcedit_error
vcedit_write (vcedit_state *state)
{
ogg_stream_state streamout;
vcedit_write (vcedit_state *state)
{
ogg_stream_state streamout;
@@
-417,23
+409,22
@@
vcedit_write (vcedit_state *state)
int s, result, bytes, needflush = 0, needout = 0;
size_t tmp;
int s, result, bytes, needflush = 0, needout = 0;
size_t tmp;
+ if (!state->opened)
+ return VCEDIT_ERR_INVAL;
+
strcpy (tmpfile, state->filename);
strcat (tmpfile, ".XXXXXX");
s = mkstemp (tmpfile);
strcpy (tmpfile, state->filename);
strcat (tmpfile, ".XXXXXX");
s = mkstemp (tmpfile);
- if (s == -1) {
- state->lasterror = "Error writing stream to output. "
- "Cannot open temporary file.";
- return -1;
- }
+ if (s == -1)
+ return VCEDIT_ERR_TMPFILE;
out = fdopen (s, "wb");
if (!out) {
unlink (tmpfile);
close (s);
out = fdopen (s, "wb");
if (!out) {
unlink (tmpfile);
close (s);
- state->lasterror = "Error writing stream to output. "
- "Cannot open temporary file.";
- return -1;
+
+ return VCEDIT_ERR_TMPFILE;
}
state->prevW = state->extrapage = state->eosin = 0;
}
state->prevW = state->extrapage = state->eosin = 0;
@@
-551,9
+542,7
@@
vcedit_write (vcedit_state *state)
if (!result)
break;
if (!result)
break;
- if (result < 0)
- state->lasterror = "Corrupt or missing data, continuing...";
- else {
+ if (result >= 0) {
/* Don't bother going through the rest, we can just
* write the page out now
*/
/* Don't bother going through the rest, we can just
* write the page out now
*/
@@
-596,16
+585,11
@@
cleanup:
state->mainbuf = state->bookbuf = NULL;
state->mainbuf = state->bookbuf = NULL;
- if (!state->eosin) {
- state->lasterror = "Error writing stream to output. "
- "Output stream may be corrupted or truncated.";
- return -1;
- }
+ if (!state->eosin)
+ return VCEDIT_ERR_INVAL;
vcedit_clear_internals (state);
vcedit_clear_internals (state);
- state->in = fopen (state->filename, "rb");
- vcedit_open (state);
-
- return 0;
+ return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
+ VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;
}
}