ref: 4f76493c968b44b62575a9b0202c6864b9dac26d
parent: 0953079e921580b311e8253e5e35c12e92afb616
author: Jean-Marc Valin <[email protected]>
date: Thu May 18 19:20:37 EDT 2017
Return int error codes for pictures
--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -96,6 +96,8 @@
#define OPE_CANNOT_OPEN -30
#define OPE_TOO_LATE -31
#define OPE_UNRECOVERABLE -32
+#define OPE_INVALID_PICTURE -33
+#define OPE_INVALID_ICON -34
/*@}*/
/*@}*/
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -130,13 +130,11 @@
}
int ope_comments_add_picture(OggOpusComments *comments, const char *filename, int picture_type, const char *description) {
- const char *error_message;
char *picture_data;
- picture_data = parse_picture_specification(filename, picture_type, description, &error_message, &comments->seen_file_icons);
- if(picture_data==NULL){
- /* FIXME: return proper errors rather than printing a message. */
- fprintf(stderr,"Error parsing picture option: %s\n",error_message);
- return OPE_BAD_ARG;
+ int err;
+ picture_data = parse_picture_specification(filename, picture_type, description, &err, &comments->seen_file_icons);
+ if (picture_data == NULL || err != OPE_OK){
+ return err;
}
comment_add(&comments->comment, &comments->comment_length, "METADATA_BLOCK_PICTURE", picture_data);
free(picture_data);
--- a/src/picture.c
+++ b/src/picture.c
@@ -236,7 +236,7 @@
Return: A Base64-encoded string suitable for use in a METADATA_BLOCK_PICTURE
tag.*/
char *parse_picture_specification(const char *filename, int picture_type, const char *description,
- const char **error_message, int *seen_file_icons){
+ int *error, int *seen_file_icons){
FILE *picture_file;
opus_uint32 width;
opus_uint32 height;
@@ -250,6 +250,7 @@
size_t data_offset;
size_t data_length;
size_t b64_length;
+ *error = OPE_OK;
/*If a filename has a '|' in it, there's no way we can distinguish it from a
full specification just from the spec string.
Instead, try to open the file.
@@ -269,7 +270,7 @@
Read it in, attempt to parse the media type and image dimensions if
necessary, and validate what the user passed in.*/
if(picture_file==NULL){
- *error_message="error opening picture file";
+ *error = OPE_CANNOT_OPEN;
return NULL;
}
nbuf=data_offset;
@@ -282,7 +283,7 @@
if(new_buf==NULL){
fclose(picture_file);
free(buf);
- *error_message="insufficient memory";
+ *error = OPE_ALLOC_FAIL;
return NULL;
}
buf=new_buf;
@@ -289,12 +290,12 @@
nread=fread(buf+nbuf,1,cbuf-nbuf,picture_file);
nbuf+=nread;
if(nbuf<cbuf){
- int error;
- error=ferror(picture_file);
+ int file_error;
+ file_error=ferror(picture_file);
fclose(picture_file);
- if(error){
+ if(file_error){
free(buf);
- *error_message="error reading picture file";
+ *error = OPE_INVALID_PICTURE;
return NULL;
}
break;
@@ -302,7 +303,7 @@
if(cbuf==0xFFFFFFFF){
fclose(picture_file);
free(buf);
- *error_message="file too large";
+ *error = OPE_INVALID_PICTURE;
return NULL;
}
else if(cbuf>0x7FFFFFFFU)cbuf=0xFFFFFFFFU;
@@ -330,8 +331,7 @@
}
else{
free(buf);
- *error_message="unable to guess media type from file, "
- "must set it explicitly";
+ *error = OPE_INVALID_PICTURE;
return NULL;
}
}
@@ -344,7 +344,7 @@
||strlen(mime_type)!=9
||oi_strncasecmp("image/png",mime_type,9)!=0)){
free(buf);
- *error_message="pictures of type 1 MUST be 32x32 PNGs";
+ *error = OPE_INVALID_ICON;
return NULL;
}
/*Build the METADATA_BLOCK_PICTURE buffer.
--- a/src/picture.h
+++ b/src/picture.h
@@ -2,6 +2,7 @@
#define __PICTURE_H
#include <opus.h>
+#include "opusenc.h"
typedef enum{
PIC_FORMAT_JPEG,
@@ -35,7 +36,7 @@
int *has_palette);
char *parse_picture_specification(const char *filename, int picture_type, const char *description,
- const char **error_message, int *seen_file_icons);
+ int *error, int *seen_file_icons);
#define WRITE_U32_BE(buf, val) \
do{ \