| 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 7 | * * |
| 8 | * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2012 * |
| 9 | * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * |
| 10 | * * |
| 11 | ********************************************************************/ |
| 12 | #ifdef HAVE_CONFIG_H |
| 13 | #include "config.h" |
| 14 | #endif |
| 15 |
| 16 | #include "internal.h" |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | static unsigned op_parse_uint16le(const unsigned char *_data){ |
| 21 | return _data[0]|_data[1]<<8; |
| 22 | } |
| 23 |
| 24 | static int op_parse_int16le(const unsigned char *_data){ |
| 25 | int ret; |
| 26 | ret=_data[0]|_data[1]<<8; |
| 27 | return (ret^0x8000)-0x8000; |
| 28 | } |
| 29 |
| 30 | static opus_uint32 op_parse_uint32le(const unsigned char *_data){ |
| 31 | return _data[0]|_data[1]<<8|_data[2]<<16|_data[3]<<24; |
| 32 | } |
| 33 |
| 34 | static opus_uint32 op_parse_uint32be(const unsigned char *_data){ |
| 35 | return _data[3]|_data[2]<<8|_data[1]<<16|_data[0]<<24; |
| 36 | } |
| 37 |
| 38 | int opus_head_parse(OpusHead *_head,const unsigned char *_data,size_t _len){ |
| 39 | OpusHead head; |
| 40 | if(_len<8)return OP_ENOTFORMAT; |
| 41 | if(memcmp(_data,"OpusHead",8)!=0)return OP_ENOTFORMAT; |
| 42 | if(_len<9)return OP_EBADHEADER; |
| 43 | head.version=_data[8]; |
| 44 | if(head.version>15)return OP_EVERSION; |
| 45 | if(_len<19)return OP_EBADHEADER; |
| 46 | head.channel_count=_data[9]; |
| 47 | head.pre_skip=op_parse_uint16le(_data+10); |
| 48 | head.input_sample_rate=op_parse_uint32le(_data+12); |
| 49 | head.output_gain=op_parse_int16le(_data+16); |
| 50 | head.mapping_family=_data[18]; |
| 51 | if(head.mapping_family==0){ |
| 52 | if(head.channel_count<1||head.channel_count>2)return OP_EBADHEADER; |
| 53 | if(head.version<=1&&_len>19)return OP_EBADHEADER; |
| 54 | head.stream_count=1; |
| 55 | head.coupled_count=head.channel_count-1; |
| 56 | if(_head!=NULL){ |
| 57 | _head->mapping[0]=0; |
| 58 | _head->mapping[1]=1; |
| 59 | } |
| 60 | } |
| 61 | else if(head.mapping_family==1){ |
| 62 | size_t size; |
| 63 | int ci; |
| 64 | if(head.channel_count<1||head.channel_count>8)return OP_EBADHEADER; |
| 65 | size=21+head.channel_count; |
| 66 | if(_len<size||head.version<=1&&_len>size)return OP_EBADHEADER; |
| 67 | head.stream_count=_data[19]; |
| 68 | if(head.stream_count<1)return OP_EBADHEADER; |
| 69 | head.coupled_count=_data[20]; |
| 70 | if(head.coupled_count>head.stream_count)return OP_EBADHEADER; |
| 71 | for(ci=0;ci<head.channel_count;ci++){ |
| 72 | if(_data[21+ci]>=head.stream_count+head.coupled_count |
| 73 | &&_data[21+ci]!=255){ |
| 74 | return OP_EBADHEADER; |
| 75 | } |
| 76 | } |
| 77 | if(_head!=NULL)memcpy(_head->mapping,_data+21,head.channel_count); |
| 78 | } |
| 79 | /*General purpose players should not attempt to play back content with |
| 80 | channel mapping family 255.*/ |
| 81 | else if(head.mapping_family==255)return OP_EIMPL; |
| 82 | /*No other channel mapping families are currently defined.*/ |
| 83 | else return OP_EBADHEADER; |
| 84 | if(_head!=NULL)memcpy(_head,&head,head.mapping-(unsigned char *)&head); |
| 85 | return 0; |
| 86 | } |
| 87 |
| 88 | void opus_tags_init(OpusTags *_tags){ |
| 89 | memset(_tags,0,sizeof(*_tags)); |
| 90 | } |
| 91 |
| 92 | void opus_tags_clear(OpusTags *_tags){ |
| 93 | int ci; |
| 94 | for(ci=_tags->comments;ci-->0;)_ogg_free(_tags->user_comments[ci]); |
| 95 | _ogg_free(_tags->user_comments); |
| 96 | _ogg_free(_tags->comment_lengths); |
| 97 | _ogg_free(_tags->vendor); |
| 98 | } |
| 99 |
| 100 | /*Ensure there's room for up to _ncomments comments.*/ |
| 101 | static int op_tags_ensure_capacity(OpusTags *_tags,size_t _ncomments){ |
| 102 | char **user_comments; |
| 103 | int *comment_lengths; |
| 104 | size_t size; |
| 105 | if(OP_UNLIKELY(_ncomments>=(size_t)INT_MAX))return OP_EFAULT; |
| 106 | size=sizeof(*_tags->comment_lengths)*(_ncomments+1); |
| 107 | if(size/sizeof(*_tags->comment_lengths)!=_ncomments+1)return OP_EFAULT; |
| 108 | comment_lengths=(int *)_ogg_realloc(_tags->comment_lengths,size); |
| 109 | if(OP_UNLIKELY(comment_lengths==NULL))return OP_EFAULT; |
| 110 | comment_lengths[_ncomments]=0; |
| 111 | _tags->comment_lengths=comment_lengths; |
| 112 | size=sizeof(*_tags->user_comments)*(_ncomments+1); |
| 113 | if(size/sizeof(*_tags->user_comments)!=_ncomments+1)return OP_EFAULT; |
| 114 | user_comments=(char **)_ogg_realloc(_tags->user_comments,size); |
| 115 | if(OP_UNLIKELY(user_comments==NULL))return OP_EFAULT; |
| 116 | user_comments[_ncomments]=NULL; |
| 117 | _tags->user_comments=user_comments; |
| 118 | return 0; |
| 119 | } |
| 120 |
| 121 | /*Duplicate a (possibly non-NUL terminated) string with a known length.*/ |
| 122 | static char *op_strdup_with_len(const char *_s,size_t _len){ |
| 123 | size_t size; |
| 124 | char *ret; |
| 125 | size=sizeof(*ret)*(_len+1); |
| 126 | if(OP_UNLIKELY(size<_len))return NULL; |
| 127 | ret=(char *)_ogg_malloc(size); |
| 128 | if(OP_LIKELY(ret!=NULL)){ |
| 129 | ret=(char *)memcpy(ret,_s,sizeof(*ret)*_len); |
| 130 | ret[_len]='\0'; |
| 131 | } |
| 132 | return ret; |
| 133 | } |
| 134 |
| 135 | /*The actual implementation of opus_tags_parse(). |
| 136 | Unlike the public API, this function requires _tags to already be |
| 137 | initialized, modifies its contents before success is guaranteed, and assumes |
| 138 | the caller will clear it on error.*/ |
| 139 | static int opus_tags_parse_impl(OpusTags *_tags, |
| 140 | const unsigned char *_data,size_t _len){ |
| 141 | opus_uint32 count; |
| 142 | size_t len; |
| 143 | int ncomments; |
| 144 | int ci; |
| 145 | len=_len; |
| 146 | if(len<8)return OP_ENOTFORMAT; |
| 147 | if(memcmp(_data,"OpusTags",8)!=0)return OP_ENOTFORMAT; |
| 148 | if(len<16)return OP_EBADHEADER; |
| 149 | _data+=8; |
| 150 | len-=8; |
| 151 | count=op_parse_uint32le(_data); |
| 152 | _data+=4; |
| 153 | len-=4; |
| 154 | if(count>len)return OP_EBADHEADER; |
| 155 | if(_tags!=NULL){ |
| 156 | _tags->vendor=op_strdup_with_len((char *)_data,count); |
| 157 | if(_tags->vendor==NULL)return OP_EFAULT; |
| 158 | } |
| 159 | _data+=count; |
| 160 | len-=count; |
| 161 | if(len<4)return OP_EBADHEADER; |
| 162 | count=op_parse_uint32le(_data); |
| 163 | _data+=4; |
| 164 | len-=4; |
| 165 | /*Check to make sure there's minimally sufficient data left in the packet.*/ |
| 166 | if(count>len>>2)return OP_EBADHEADER; |
| 167 | /*Check for overflow (the API limits this to an int).*/ |
| 168 | if(count>(opus_uint32)INT_MAX-1)return OP_EFAULT; |
| 169 | if(_tags!=NULL){ |
| 170 | int ret; |
| 171 | ret=op_tags_ensure_capacity(_tags,count); |
| 172 | if(ret<0)return ret; |
| 173 | } |
| 174 | ncomments=(int)count; |
| 175 | for(ci=0;ci<ncomments;ci++){ |
| 176 | /*Check to make sure there's minimally sufficient data left in the packet.*/ |
| 177 | if((size_t)(ncomments-ci)>len>>2)return OP_EBADHEADER; |
| 178 | count=op_parse_uint32le(_data); |
| 179 | _data+=4; |
| 180 | len-=4; |
| 181 | if(count>len)return OP_EBADHEADER; |
| 182 | /*Check for overflow (the API limits this to an int).*/ |
| 183 | if(count>(opus_uint32)INT_MAX)return OP_EFAULT; |
| 184 | if(_tags!=NULL){ |
| 185 | _tags->user_comments[ci]=op_strdup_with_len((char *)_data,count); |
| 186 | if(_tags->user_comments[ci]==NULL)return OP_EFAULT; |
| 187 | _tags->comment_lengths[ci]=(int)count; |
| 188 | _tags->comments=ci+1; |
| 189 | } |
| 190 | _data+=count; |
| 191 | len-=count; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 |
| 196 | int opus_tags_parse(OpusTags *_tags,const unsigned char *_data,size_t _len){ |
| 197 | if(_tags!=NULL){ |
| 198 | OpusTags tags; |
| 199 | int ret; |
| 200 | opus_tags_init(&tags); |
| 201 | ret=opus_tags_parse_impl(&tags,_data,_len); |
| 202 | if(ret<0)opus_tags_clear(&tags); |
| 203 | else *_tags=*&tags; |
| 204 | return ret; |
| 205 | } |
| 206 | else return opus_tags_parse_impl(NULL,_data,_len); |
| 207 | } |
| 208 |
| 209 | /*The actual implementation of opus_tags_copy(). |
| 210 | Unlike the public API, this function requires _dst to already be |
| 211 | initialized, modifies its contents before success is guaranteed, and assumes |
| 212 | the caller will clear it on error.*/ |
| 213 | static int opus_tags_copy_impl(OpusTags *_dst,const OpusTags *_src){ |
| 214 | char *vendor; |
| 215 | int ncomments; |
| 216 | int ret; |
| 217 | int ci; |
| 218 | vendor=_src->vendor; |
| 219 | _dst->vendor=op_strdup_with_len(vendor,strlen(vendor)); |
| 220 | if(OP_UNLIKELY(_dst->vendor==NULL))return OP_EFAULT; |
| 221 | ncomments=_src->comments; |
| 222 | ret=op_tags_ensure_capacity(_dst,ncomments); |
| 223 | if(OP_UNLIKELY(ret<0))return ret; |
| 224 | for(ci=0;ci<ncomments;ci++){ |
| 225 | int len; |
| 226 | len=_src->comment_lengths[ci]; |
| 227 | OP_ASSERT(len>=0); |
| 228 | _dst->user_comments[ci]=op_strdup_with_len(_src->user_comments[ci],len); |
| 229 | if(OP_UNLIKELY(_dst->user_comments[ci]==NULL))return OP_EFAULT; |
| 230 | _dst->comment_lengths[ci]=len; |
| 231 | _dst->comments=ci+1; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 |
| 236 | int opus_tags_copy(OpusTags *_dst,const OpusTags *_src){ |
| 237 | OpusTags dst; |
| 238 | int ret; |
| 239 | opus_tags_init(&dst); |
| 240 | ret=opus_tags_copy_impl(&dst,_src); |
| 241 | if(OP_UNLIKELY(ret<0))opus_tags_clear(&dst); |
| 242 | else *_dst=*&dst; |
| 243 | return 0; |
| 244 | } |
| 245 |
| 246 | int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value){ |
| 247 | char *comment; |
| 248 | int tag_len; |
| 249 | int value_len; |
| 250 | int ncomments; |
| 251 | int ret; |
| 252 | ncomments=_tags->comments; |
| 253 | ret=op_tags_ensure_capacity(_tags,ncomments+1); |
| 254 | if(OP_UNLIKELY(ret<0))return ret; |
| 255 | tag_len=strlen(_tag); |
| 256 | value_len=strlen(_value); |
| 257 | /*+2 for '=' and '\0'.*/ |
| 258 | _tags->comment_lengths[ncomments]=0; |
| 259 | _tags->user_comments[ncomments]=comment= |
| 260 | (char *)_ogg_malloc(sizeof(*comment)*(tag_len+value_len+2)); |
| 261 | if(OP_UNLIKELY(comment==NULL))return OP_EFAULT; |
| 262 | _tags->comment_lengths[ncomments]=tag_len+value_len+1; |
| 263 | memcpy(comment,_tag,sizeof(*comment)*tag_len); |
| 264 | comment[tag_len]='='; |
| 265 | memcpy(comment+tag_len+1,_value,sizeof(*comment)*(value_len+1)); |
| 266 | return 0; |
| 267 | } |
| 268 |
| 269 | int opus_tags_add_comment(OpusTags *_tags,const char *_comment){ |
| 270 | int comment_len; |
| 271 | int ncomments; |
| 272 | int ret; |
| 273 | ncomments=_tags->comments; |
| 274 | ret=op_tags_ensure_capacity(_tags,ncomments+1); |
| 275 | if(OP_UNLIKELY(ret<0))return ret; |
| 276 | comment_len=(int)strlen(_comment); |
| 277 | _tags->comment_lengths[ncomments]=0; |
| 278 | _tags->user_comments[ncomments]=op_strdup_with_len(_comment,comment_len); |
| 279 | if(OP_UNLIKELY(_tags->user_comments[ncomments]==NULL))return OP_EFAULT; |
| 280 | _tags->comment_lengths[ncomments]=comment_len; |
| 281 | return 0; |
| 282 | } |
| 283 |
| 284 | int opus_tagcompare(const char *_tag_name,const char *_comment){ |
| 285 | return opus_tagncompare(_tag_name,strlen(_tag_name),_comment); |
| 286 | } |
| 287 |
| 288 | int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment){ |
| 289 | int ret; |
| 290 | OP_ASSERT(_tag_len>=0); |
| 291 | ret=op_strncasecmp(_tag_name,_comment,_tag_len); |
| 292 | return ret?ret:'='-_comment[_tag_len]; |
| 293 | } |
| 294 |
| 295 | const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count){ |
| 296 | char **user_comments; |
| 297 | int tag_len; |
| 298 | int found; |
| 299 | int ncomments; |
| 300 | int ci; |
| 301 | tag_len=strlen(_tag); |
| 302 | ncomments=_tags->comments; |
| 303 | user_comments=_tags->user_comments; |
| 304 | found=0; |
| 305 | for(ci=0;ci<ncomments;ci++){ |
| 306 | if(!opus_tagncompare(_tag,tag_len,user_comments[ci])){ |
| 307 | /*We return a pointer to the data, not a copy.*/ |
| 308 | if(_count==found++)return user_comments[ci]+tag_len+1; |
| 309 | } |
| 310 | } |
| 311 | /*Didn't find anything.*/ |
| 312 | return NULL; |
| 313 | } |
| 314 |
| 315 | int opus_tags_query_count(const OpusTags *_tags,const char *_tag){ |
| 316 | char **user_comments; |
| 317 | int tag_len; |
| 318 | int found; |
| 319 | int ncomments; |
| 320 | int ci; |
| 321 | tag_len=strlen(_tag); |
| 322 | ncomments=_tags->comments; |
| 323 | user_comments=_tags->user_comments; |
| 324 | found=0; |
| 325 | for(ci=0;ci<ncomments;ci++){ |
| 326 | if(!opus_tagncompare(_tag,tag_len,user_comments[ci]))found++; |
| 327 | } |
| 328 | return found; |
| 329 | } |
| 330 |
| 331 | int opus_tags_get_track_gain(const OpusTags *_tags,int *_gain_q8){ |
| 332 | char **comments; |
| 333 | int ncomments; |
| 334 | int ci; |
| 335 | comments=_tags->user_comments; |
| 336 | ncomments=_tags->comments; |
| 337 | /*Look for the first valid R128_TRACK_GAIN tag and use that.*/ |
| 338 | for(ci=0;ci<ncomments;ci++){ |
| 339 | if(opus_tagncompare("R128_TRACK_GAIN",15,comments[ci])==0){ |
| 340 | char *p; |
| 341 | opus_int32 gain_q8; |
| 342 | int negative; |
| 343 | p=comments[ci]+16; |
| 344 | negative=0; |
| 345 | if(*p=='-'){ |
| 346 | negative=-1; |
| 347 | p++; |
| 348 | } |
| 349 | else if(*p=='+')p++; |
| 350 | gain_q8=0; |
| 351 | while(*p>='0'&&*p<='9'){ |
| 352 | gain_q8=10*gain_q8+*p-'0'; |
| 353 | if(gain_q8>32767-negative)break; |
| 354 | p++; |
| 355 | } |
| 356 | /*This didn't look like a signed 16-bit decimal integer. |
| 357 | Not a valid R128_TRACK_GAIN tag.*/ |
| 358 | if(*p!='\0')continue; |
| 359 | *_gain_q8=(int)(gain_q8+negative^negative); |
| 360 | return 0; |
| 361 | } |
| 362 | } |
| 363 | return OP_FALSE; |
| 364 | } |
| 365 |
| 366 | static int op_is_jpeg(const unsigned char *_buf,size_t _buf_sz){ |
| 367 | return _buf_sz>=11&&memcmp(_buf,"\xFF\xD8\xFF\xE0",4)==0 |
| 368 | &&(_buf[4]<<8|_buf[5])>=16&&memcmp(_buf+6,"JFIF",5)==0; |
| 369 | } |
| 370 |
| 371 | /*Tries to extract the width, height, bits per pixel, and palette size of a |
| 372 | JPEG. |
| 373 | On failure, simply leaves its outputs unmodified.*/ |
| 374 | static void op_extract_jpeg_params(const unsigned char *_buf,size_t _buf_sz, |
| 375 | opus_uint32 *_width,opus_uint32 *_height, |
| 376 | opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ |
| 377 | if(op_is_jpeg(_buf,_buf_sz)){ |
| 378 | size_t offs; |
| 379 | offs=2; |
| 380 | for(;;){ |
| 381 | size_t segment_len; |
| 382 | int marker; |
| 383 | while(offs<_buf_sz&&_buf[offs]!=0xFF)offs++; |
| 384 | while(offs<_buf_sz&&_buf[offs]==0xFF)offs++; |
| 385 | marker=_buf[offs]; |
| 386 | offs++; |
| 387 | /*If we hit EOI* (end of image), or another SOI* (start of image), |
| 388 | or SOS (start of scan), then stop now.*/ |
| 389 | if(offs>=_buf_sz||(marker>=0xD8&&marker<=0xDA))break; |
| 390 | /*RST* (restart markers): skip (no segment length).*/ |
| 391 | else if(marker>=0xD0&&marker<=0xD7)continue; |
| 392 | /*Read the length of the marker segment.*/ |
| 393 | if(_buf_sz-offs<2)break; |
| 394 | segment_len=_buf[offs]<<8|_buf[offs+1]; |
| 395 | if(segment_len<2||_buf_sz-offs<segment_len)break; |
| 396 | if(marker==0xC0||(marker>0xC0&&marker<0xD0&&(marker&3)!=0)){ |
| 397 | /*Found a SOFn (start of frame) marker segment:*/ |
| 398 | if(segment_len>=8){ |
| 399 | *_height=_buf[offs+3]<<8|_buf[offs+4]; |
| 400 | *_width=_buf[offs+5]<<8|_buf[offs+6]; |
| 401 | *_depth=_buf[offs+2]*_buf[offs+7]; |
| 402 | *_colors=0; |
| 403 | *_has_palette=0; |
| 404 | } |
| 405 | break; |
| 406 | } |
| 407 | /*Other markers: skip the whole marker segment.*/ |
| 408 | offs+=segment_len; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 |
| 413 | static int op_is_png(const unsigned char *_buf,size_t _buf_sz){ |
| 414 | return _buf_sz>=8&&memcmp(_buf,"\x89PNG\x0D\x0A\x1A\x0A",8)==0; |
| 415 | } |
| 416 |
| 417 | /*Tries to extract the width, height, bits per pixel, and palette size of a |
| 418 | PNG. |
| 419 | On failure, simply leaves its outputs unmodified.*/ |
| 420 | static void op_extract_png_params(const unsigned char *_buf,size_t _buf_sz, |
| 421 | opus_uint32 *_width,opus_uint32 *_height, |
| 422 | opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ |
| 423 | if(op_is_png(_buf,_buf_sz)){ |
| 424 | size_t offs; |
| 425 | offs=8; |
| 426 | while(_buf_sz-offs>=12){ |
| 427 | ogg_uint32_t chunk_len; |
| 428 | chunk_len=op_parse_uint32be(_buf+offs); |
| 429 | if(chunk_len>_buf_sz-(offs+12))break; |
| 430 | else if(chunk_len==13&&memcmp(_buf+offs+4,"IHDR",4)==0){ |
| 431 | int color_type; |
| 432 | *_width=op_parse_uint32be(_buf+offs+8); |
| 433 | *_height=op_parse_uint32be(_buf+offs+12); |
| 434 | color_type=_buf[offs+17]; |
| 435 | if(color_type==3){ |
| 436 | *_depth=24; |
| 437 | *_has_palette=1; |
| 438 | } |
| 439 | else{ |
| 440 | int sample_depth; |
| 441 | sample_depth=_buf[offs+16]; |
| 442 | if(color_type==0)*_depth=sample_depth; |
| 443 | else if(color_type==2)*_depth=sample_depth*3; |
| 444 | else if(color_type==4)*_depth=sample_depth*2; |
| 445 | else if(color_type==6)*_depth=sample_depth*4; |
| 446 | *_colors=0; |
| 447 | *_has_palette=0; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | else if(*_has_palette>0&&memcmp(_buf+offs+4,"PLTE",4)==0){ |
| 452 | *_colors=chunk_len/3; |
| 453 | break; |
| 454 | } |
| 455 | offs+=12+chunk_len; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 |
| 460 | static int op_is_gif(const unsigned char *_buf,size_t _buf_sz){ |
| 461 | return _buf_sz>=6&&(memcmp(_buf,"GIF87a",6)==0||memcmp(_buf,"GIF89a",6)==0); |
| 462 | } |
| 463 |
| 464 | /*Tries to extract the width, height, bits per pixel, and palette size of a |
| 465 | GIF. |
| 466 | On failure, simply leaves its outputs unmodified.*/ |
| 467 | static void op_extract_gif_params(const unsigned char *_buf,size_t _buf_sz, |
| 468 | opus_uint32 *_width,opus_uint32 *_height, |
| 469 | opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ |
| 470 | if(op_is_gif(_buf,_buf_sz)&&_buf_sz>=14){ |
| 471 | *_width=_buf[6]|_buf[7]<<8; |
| 472 | *_height=_buf[8]|_buf[9]<<8; |
| 473 | /*libFLAC hard-codes the depth to 24.*/ |
| 474 | *_depth=24; |
| 475 | *_colors=1<<((_buf[10]&7)+1); |
| 476 | *_has_palette=1; |
| 477 | } |
| 478 | } |
| 479 |
| 480 | /*The actual implementation of opus_picture_tag_parse(). |
| 481 | Unlike the public API, this function requires _pic to already be |
| 482 | initialized, modifies its contents before success is guaranteed, and assumes |
| 483 | the caller will clear it on error.*/ |
| 484 | static int opus_picture_tag_parse_impl(OpusPictureTag *_pic,const char *_tag, |
| 485 | unsigned char *_buf,size_t _buf_sz,size_t _base64_sz){ |
| 486 | opus_int32 picture_type; |
| 487 | opus_uint32 mime_type_length; |
| 488 | char *mime_type; |
| 489 | opus_uint32 description_length; |
| 490 | char *description; |
| 491 | opus_uint32 width; |
| 492 | opus_uint32 height; |
| 493 | opus_uint32 depth; |
| 494 | opus_uint32 colors; |
| 495 | opus_uint32 data_length; |
| 496 | opus_uint32 file_width; |
| 497 | opus_uint32 file_height; |
| 498 | opus_uint32 file_depth; |
| 499 | opus_uint32 file_colors; |
| 500 | int format; |
| 501 | int has_palette; |
| 502 | int colors_set; |
| 503 | size_t i; |
| 504 | /*Decode the BASE64 data.*/ |
| 505 | for(i=0;i<_base64_sz;i++){ |
| 506 | opus_uint32 value; |
| 507 | int j; |
| 508 | value=0; |
| 509 | for(j=0;j<4;j++){ |
| 510 | unsigned c; |
| 511 | unsigned d; |
| 512 | c=(unsigned char)_tag[4*i+j]; |
| 513 | if(c=='+')d=62; |
| 514 | else if(c=='/')d=63; |
| 515 | else if(c>='0'&&c<='9')d=52+c-'0'; |
| 516 | else if(c>='a'&&c<='z')d=26+c-'a'; |
| 517 | else if(c>='A'&&c<='Z')d=c-'A'; |
| 518 | else if(c=='='&&3*i+j>_buf_sz)d=0; |
| 519 | else return OP_ENOTFORMAT; |
| 520 | value=value<<6|d; |
| 521 | } |
| 522 | _buf[3*i]=(unsigned char)(value>>16); |
| 523 | if(3*i+1<_buf_sz){ |
| 524 | _buf[3*i+1]=(unsigned char)(value>>8); |
| 525 | if(3*i+2<_buf_sz)_buf[3*i+2]=(unsigned char)value; |
| 526 | } |
| 527 | } |
| 528 | i=0; |
| 529 | picture_type=op_parse_uint32be(_buf+i); |
| 530 | i+=4; |
| 531 | /*Extract the MIME type.*/ |
| 532 | mime_type_length=op_parse_uint32be(_buf+i); |
| 533 | i+=4; |
| 534 | if(mime_type_length>_buf_sz-32)return OP_ENOTFORMAT; |
| 535 | mime_type=(char *)_ogg_malloc(sizeof(*_pic->mime_type)*(mime_type_length+1)); |
| 536 | if(mime_type==NULL)return OP_EFAULT; |
| 537 | memcpy(mime_type,_buf+i,sizeof(*mime_type)*mime_type_length); |
| 538 | mime_type[mime_type_length]='\0'; |
| 539 | _pic->mime_type=mime_type; |
| 540 | i+=mime_type_length; |
| 541 | /*Extract the description string.*/ |
| 542 | description_length=op_parse_uint32be(_buf+i); |
| 543 | i+=4; |
| 544 | if(description_length>_buf_sz-mime_type_length-32)return OP_ENOTFORMAT; |
| 545 | description= |
| 546 | (char *)_ogg_malloc(sizeof(*_pic->mime_type)*(description_length+1)); |
| 547 | if(description==NULL)return OP_EFAULT; |
| 548 | memcpy(description,_buf+i,sizeof(*description)*description_length); |
| 549 | description[description_length]='\0'; |
| 550 | _pic->description=description; |
| 551 | i+=description_length; |
| 552 | /*Extract the remaining fields.*/ |
| 553 | width=op_parse_uint32be(_buf+i); |
| 554 | i+=4; |
| 555 | height=op_parse_uint32be(_buf+i); |
| 556 | i+=4; |
| 557 | depth=op_parse_uint32be(_buf+i); |
| 558 | i+=4; |
| 559 | colors=op_parse_uint32be(_buf+i); |
| 560 | i+=4; |
| 561 | /*If one of these is set, they all must be, but colors==0 is a valid value.*/ |
| 562 | colors_set=width!=0||height!=0||depth!=0||colors!=0; |
| 563 | if(width==0||height==0||depth==0&&colors_set)return OP_ENOTFORMAT; |
| 564 | data_length=op_parse_uint32be(_buf+i); |
| 565 | i+=4; |
| 566 | if(data_length>_buf_sz-i)return OP_ENOTFORMAT; |
| 567 | /*Trim extraneous data so we don't copy it below.*/ |
| 568 | _buf_sz=i+data_length; |
| 569 | /*Attempt to determine the image format.*/ |
| 570 | format=OP_PIC_FORMAT_UNKNOWN; |
| 571 | if(mime_type_length==3&&strcmp(mime_type,"-->")==0){ |
| 572 | format=OP_PIC_FORMAT_URL; |
| 573 | /*Picture type 1 must be a 32x32 PNG.*/ |
| 574 | if(picture_type==1&&(width!=0||height!=0)&&(width!=32||height!=32)){ |
| 575 | return OP_ENOTFORMAT; |
| 576 | } |
| 577 | /*Append a terminating NUL for the convenience of our callers.*/ |
| 578 | _buf[_buf_sz++]='\0'; |
| 579 | } |
| 580 | else{ |
| 581 | if(mime_type_length==10 |
| 582 | &&op_strncasecmp(mime_type,"image/jpeg",mime_type_length)==0){ |
| 583 | if(op_is_jpeg(_buf+i,data_length))format=OP_PIC_FORMAT_JPEG; |
| 584 | } |
| 585 | else if(mime_type_length==9 |
| 586 | &&op_strncasecmp(mime_type,"image/png",mime_type_length)==0){ |
| 587 | if(op_is_png(_buf+i,data_length))format=OP_PIC_FORMAT_PNG; |
| 588 | } |
| 589 | else if(mime_type_length==9 |
| 590 | &&op_strncasecmp(mime_type,"image/gif",mime_type_length)==0){ |
| 591 | if(op_is_gif(_buf+i,data_length))format=OP_PIC_FORMAT_GIF; |
| 592 | } |
| 593 | else if(mime_type_length==0||(mime_type_length==6 |
| 594 | &&op_strncasecmp(mime_type,"image/",mime_type_length)==0)){ |
| 595 | if(op_is_jpeg(_buf+i,data_length))format=OP_PIC_FORMAT_JPEG; |
| 596 | else if(op_is_png(_buf+i,data_length))format=OP_PIC_FORMAT_PNG; |
| 597 | else if(op_is_gif(_buf+i,data_length))format=OP_PIC_FORMAT_GIF; |
| 598 | } |
| 599 | file_width=file_height=file_depth=file_colors=0; |
| 600 | has_palette=-1; |
| 601 | switch(format){ |
| 602 | case OP_PIC_FORMAT_JPEG:{ |
| 603 | op_extract_jpeg_params(_buf+i,data_length, |
| 604 | &file_width,&file_height,&file_depth,&file_colors,&has_palette); |
| 605 | }break; |
| 606 | case OP_PIC_FORMAT_PNG:{ |
| 607 | op_extract_png_params(_buf+i,data_length, |
| 608 | &file_width,&file_height,&file_depth,&file_colors,&has_palette); |
| 609 | }break; |
| 610 | case OP_PIC_FORMAT_GIF:{ |
| 611 | op_extract_gif_params(_buf+i,data_length, |
| 612 | &file_width,&file_height,&file_depth,&file_colors,&has_palette); |
| 613 | }break; |
| 614 | } |
| 615 | if(has_palette>=0){ |
| 616 | /*If we successfully extracted these parameters from the image, override |
| 617 | any declared values.*/ |
| 618 | width=file_width; |
| 619 | height=file_height; |
| 620 | depth=file_depth; |
| 621 | colors=file_colors; |
| 622 | } |
| 623 | /*Picture type 1 must be a 32x32 PNG.*/ |
| 624 | if(picture_type==1&&(format!=OP_PIC_FORMAT_PNG||width!=32||height!=32)){ |
| 625 | return OP_ENOTFORMAT; |
| 626 | } |
| 627 | } |
| 628 | /*Adjust _buf_sz instead of using data_length to capture the terminating NUL |
| 629 | for URLs.*/ |
| 630 | _buf_sz-=i; |
| 631 | memmove(_buf,_buf+i,sizeof(*_buf)*_buf_sz); |
| 632 | _buf=(unsigned char *)_ogg_realloc(_buf,_buf_sz); |
| 633 | if(_buf_sz>0&&_buf==NULL)return OP_EFAULT; |
| 634 | _pic->type=picture_type; |
| 635 | _pic->width=width; |
| 636 | _pic->height=height; |
| 637 | _pic->depth=depth; |
| 638 | _pic->colors=colors; |
| 639 | _pic->data_length=data_length; |
| 640 | _pic->data=_buf; |
| 641 | _pic->format=format; |
| 642 | return 0; |
| 643 | } |
| 644 |
| 645 | int opus_picture_tag_parse(OpusPictureTag *_pic,const char *_tag){ |
| 646 | OpusPictureTag pic; |
| 647 | unsigned char *buf; |
| 648 | size_t base64_sz; |
| 649 | size_t buf_sz; |
| 650 | size_t tag_length; |
| 651 | int ret; |
| 652 | if(opus_tagncompare("METADATA_BLOCK_PICTURE",22,_tag)==0)_tag+=23; |
| 653 | /*Figure out how much BASE64-encoded data we have.*/ |
| 654 | tag_length=strlen(_tag); |
| 655 | if(tag_length&3)return OP_ENOTFORMAT; |
| 656 | base64_sz=tag_length>>2; |
| 657 | buf_sz=3*base64_sz; |
| 658 | if(buf_sz<32)return OP_ENOTFORMAT; |
| 659 | if(_tag[tag_length-1]=='=')buf_sz--; |
| 660 | if(_tag[tag_length-2]=='=')buf_sz--; |
| 661 | if(buf_sz<32)return OP_ENOTFORMAT; |
| 662 | /*Allocate an extra byte to allow appending a terminating NUL to URL data.*/ |
| 663 | buf=(unsigned char *)_ogg_malloc(sizeof(*buf)*(buf_sz+1)); |
| 664 | if(buf==NULL)return OP_EFAULT; |
| 665 | opus_picture_tag_init(&pic); |
| 666 | ret=opus_picture_tag_parse_impl(&pic,_tag,buf,buf_sz,base64_sz); |
| 667 | if(ret<0){ |
| 668 | opus_picture_tag_clear(&pic); |
| 669 | _ogg_free(buf); |
| 670 | } |
| 671 | else *_pic=*&pic; |
| 672 | return ret; |
| 673 | } |
| 674 |
| 675 | void opus_picture_tag_init(OpusPictureTag *_pic){ |
| 676 | memset(_pic,0,sizeof(*_pic)); |
| 677 | } |
| 678 |
| 679 | void opus_picture_tag_clear(OpusPictureTag *_pic){ |
| 680 | _ogg_free(_pic->description); |
| 681 | _ogg_free(_pic->mime_type); |
| 682 | _ogg_free(_pic->data); |
| 683 | } |
| 684 |