| 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 7 | * * |
| 8 | * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2012 * |
| 9 | * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * |
| 10 | * * |
| 11 | ******************************************************************** |
| 12 |
| 13 | function: stdio-based convenience library for opening/seeking/decoding |
| 14 | last mod: $Id: vorbisfile.c 17573 2010-10-27 14:53:59Z xiphmont $ |
| 15 |
| 16 | ********************************************************************/ |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | #include "config.h" |
| 19 | #endif |
| 20 |
| 21 | #include "internal.h" |
| 22 | #include <sys/types.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <errno.h> |
| 26 | #include <string.h> |
| 27 | #if defined(_WIN32) |
| 28 | # include <io.h> |
| 29 | #endif |
| 30 |
| 31 | typedef struct OpusMemStream OpusMemStream; |
| 32 |
| 33 | #define OP_MEM_SIZE_MAX (~(size_t)0>>1) |
| 34 | #define OP_MEM_DIFF_MAX ((ptrdiff_t)OP_MEM_SIZE_MAX) |
| 35 |
| 36 | /*The context information needed to read from a block of memory as if it were a |
| 37 | file.*/ |
| 38 | struct OpusMemStream{ |
| 39 | /*The block of memory to read from.*/ |
| 40 | const unsigned char *data; |
| 41 | /*The total size of the block. |
| 42 | This must be at most OP_MEM_SIZE_MAX to prevent signed overflow while |
| 43 | seeking.*/ |
| 44 | ptrdiff_t size; |
| 45 | /*The current file position. |
| 46 | This is allowed to be set arbitrarily greater than size (i.e., past the end |
| 47 | of the block, though we will not read data past the end of the block), but |
| 48 | is not allowed to be negative (i.e., before the beginning of the block).*/ |
| 49 | ptrdiff_t pos; |
| 50 | }; |
| 51 |
| 52 | static int op_fread(void *_stream,unsigned char *_ptr,int _buf_size){ |
| 53 | FILE *stream; |
| 54 | size_t ret; |
| 55 | /*Check for empty read.*/ |
| 56 | if(_buf_size<=0)return 0; |
| 57 | stream=(FILE *)_stream; |
| 58 | ret=fread(_ptr,1,_buf_size,stream); |
| 59 | OP_ASSERT(ret<=(size_t)_buf_size); |
| 60 | /*If ret==0 and !feof(stream), there was a read error.*/ |
| 61 | return ret>0||feof(stream)?(int)ret:OP_EREAD; |
| 62 | } |
| 63 |
| 64 | static int op_fseek(void *_stream,opus_int64 _offset,int _whence){ |
| 65 | #if defined(_WIN32) |
| 66 | /*_fseeki64() is not exposed until MSCVCRT80. |
| 67 | This is the default starting with MSVC 2005 (_MSC_VER>=1400), but we want |
| 68 | to allow linking against older MSVCRT versions for compatibility back to |
| 69 | XP without installing extra runtime libraries. |
| 70 | i686-pc-mingw32 does not have fseeko() and requires |
| 71 | __MSVCRT_VERSION__>=0x800 for _fseeki64(), which screws up linking with |
| 72 | other libraries (that don't use MSVCRT80 from MSVC 2005 by default). |
| 73 | i686-w64-mingw32 does have fseeko() and respects _FILE_OFFSET_BITS, but I |
| 74 | don't know how to detect that at compile time. |
| 75 | We could just use fseeko64() (which is available in both), but its |
| 76 | implemented using fgetpos()/fsetpos() just like this code, except without |
| 77 | the overflow checking, so we prefer our version.*/ |
| 78 | opus_int64 pos; |
| 79 | /*We don't use fpos_t directly because it might be a struct if __STDC__ is |
| 80 | non-zero or _INTEGRAL_MAX_BITS < 64. |
| 81 | I'm not certain when the latter is true, but someone could in theory set |
| 82 | the former. |
| 83 | Either way, it should be binary compatible with a normal 64-bit int (this |
| 84 | assumption is not portable, but I believe it is true for MSVCRT).*/ |
| 85 | OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); |
| 86 | /*Translate the seek to an absolute one.*/ |
| 87 | if(_whence==SEEK_CUR){ |
| 88 | int ret; |
| 89 | ret=fgetpos((FILE *)_stream,(fpos_t *)&pos); |
| 90 | if(ret)return ret; |
| 91 | } |
| 92 | else if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream)); |
| 93 | else if(_whence==SEEK_SET)pos=0; |
| 94 | else return -1; |
| 95 | /*Check for errors or overflow.*/ |
| 96 | if(pos<0||_offset<-pos||_offset>OP_INT64_MAX-pos)return -1; |
| 97 | pos+=_offset; |
| 98 | return fsetpos((FILE *)_stream,(fpos_t *)&pos); |
| 99 | #else |
| 100 | /*This function actually conforms to the SUSv2 and POSIX.1-2001, so we prefer |
| 101 | it except on Windows.*/ |
| 102 | return fseeko((FILE *)_stream,(off_t)_offset,_whence); |
| 103 | #endif |
| 104 | } |
| 105 |
| 106 | static opus_int64 op_ftell(void *_stream){ |
| 107 | #if defined(_WIN32) |
| 108 | /*_ftelli64() is not exposed until MSCVCRT80, and ftello()/ftello64() have |
| 109 | the same problems as fseeko()/fseeko64() in MingW. |
| 110 | See above for a more detailed explanation.*/ |
| 111 | opus_int64 pos; |
| 112 | OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); |
| 113 | return fgetpos((FILE *)_stream,(fpos_t *)&pos)?-1:pos; |
| 114 | #else |
| 115 | /*This function actually conforms to the SUSv2 and POSIX.1-2001, so we prefer |
| 116 | it except on Windows.*/ |
| 117 | return ftello((FILE *)_stream); |
| 118 | #endif |
| 119 | } |
| 120 |
| 121 | static const OpusFileCallbacks OP_FILE_CALLBACKS={ |
| 122 | op_fread, |
| 123 | op_fseek, |
| 124 | op_ftell, |
| 125 | (op_close_func)fclose |
| 126 | }; |
| 127 |
| 128 | #if defined(_WIN32) |
| 129 | # include <stddef.h> |
| 130 | # include <errno.h> |
| 131 |
| 132 | /*Windows doesn't accept UTF-8 by default, and we don't have a wchar_t API, |
| 133 | so if we just pass the path to fopen(), then there'd be no way for a user |
| 134 | of our API to open a Unicode filename. |
| 135 | Instead, we translate from UTF-8 to UTF-16 and use Windows' wchar_t API. |
| 136 | This makes this API more consistent with platforms where the character set |
| 137 | used by fopen is the same as used on disk, which is generally UTF-8, and |
| 138 | with our metadata API, which always uses UTF-8.*/ |
| 139 | static wchar_t *op_utf8_to_utf16(const char *_src){ |
| 140 | wchar_t *dst; |
| 141 | size_t len; |
| 142 | len=strlen(_src); |
| 143 | /*Worst-case output is 1 wide character per 1 input character.*/ |
| 144 | dst=(wchar_t *)_ogg_malloc(sizeof(*dst)*(len+1)); |
| 145 | if(dst!=NULL){ |
| 146 | size_t si; |
| 147 | size_t di; |
| 148 | for(di=si=0;si<len;si++){ |
| 149 | int c0; |
| 150 | c0=(unsigned char)_src[si]; |
| 151 | if(!(c0&0x80)){ |
| 152 | /*Start byte says this is a 1-byte sequence.*/ |
| 153 | dst[di++]=(wchar_t)c0; |
| 154 | continue; |
| 155 | } |
| 156 | else{ |
| 157 | int c1; |
| 158 | /*This is safe, because c0 was not 0 and _src is NUL-terminated.*/ |
| 159 | c1=(unsigned char)_src[si+1]; |
| 160 | if((c1&0xC0)==0x80){ |
| 161 | /*Found at least one continuation byte.*/ |
| 162 | if((c0&0xE0)==0xC0){ |
| 163 | wchar_t w; |
| 164 | /*Start byte says this is a 2-byte sequence.*/ |
| 165 | w=(c0&0x1F)<<6|c1&0x3F; |
| 166 | if(w>=0x80U){ |
| 167 | /*This is a 2-byte sequence that is not overlong.*/ |
| 168 | dst[di++]=w; |
| 169 | si++; |
| 170 | continue; |
| 171 | } |
| 172 | } |
| 173 | else{ |
| 174 | int c2; |
| 175 | /*This is safe, because c1 was not 0 and _src is NUL-terminated.*/ |
| 176 | c2=(unsigned char)_src[si+2]; |
| 177 | if((c2&0xC0)==0x80){ |
| 178 | /*Found at least two continuation bytes.*/ |
| 179 | if((c0&0xF0)==0xE0){ |
| 180 | wchar_t w; |
| 181 | /*Start byte says this is a 3-byte sequence.*/ |
| 182 | w=(c0&0xF)<<12|(c1&0x3F)<<6|c2&0x3F; |
| 183 | if(w>=0x800U&&(w<0xD800||w>=0xE000)&&w<0xFFFE){ |
| 184 | /*This is a 3-byte sequence that is not overlong, not a |
| 185 | UTF-16 surrogate pair value, and not a 'not a character' |
| 186 | value.*/ |
| 187 | dst[di++]=w; |
| 188 | si+=2; |
| 189 | continue; |
| 190 | } |
| 191 | } |
| 192 | else{ |
| 193 | int c3; |
| 194 | /*This is safe, because c2 was not 0 and _src is |
| 195 | NUL-terminated.*/ |
| 196 | c3=(unsigned char)_src[si+3]; |
| 197 | if((c3&0xC0)==0x80){ |
| 198 | /*Found at least three continuation bytes.*/ |
| 199 | if((c0&0xF8)==0xF0){ |
| 200 | opus_uint32 w; |
| 201 | /*Start byte says this is a 4-byte sequence.*/ |
| 202 | w=(c0&7)<<18|(c1&0x3F)<<12|(c2&0x3F)<<6&(c3&0x3F); |
| 203 | if(w>=0x10000U&&w<0x110000U){ |
| 204 | /*This is a 4-byte sequence that is not overlong and not |
| 205 | greater than the largest valid Unicode code point. |
| 206 | Convert it to a surrogate pair.*/ |
| 207 | w-=0x10000; |
| 208 | dst[di++]=(wchar_t)(0xD800+(w>>10)); |
| 209 | dst[di++]=(wchar_t)(0xDC00+(w&0x3FF)); |
| 210 | si+=3; |
| 211 | continue; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | /*If we got here, we encountered an illegal UTF-8 sequence.*/ |
| 221 | _ogg_free(dst); |
| 222 | return NULL; |
| 223 | } |
| 224 | OP_ASSERT(di<=len); |
| 225 | dst[di]='\0'; |
| 226 | } |
| 227 | return dst; |
| 228 | } |
| 229 |
| 230 | #endif |
| 231 |
| 232 | void *op_fopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode){ |
| 233 | FILE *fp; |
| 234 | #if !defined(_WIN32) |
| 235 | fp=fopen(_path,_mode); |
| 236 | #else |
| 237 | fp=NULL; |
| 238 | if(_path==NULL||_mode==NULL)errno=EINVAL; |
| 239 | else{ |
| 240 | wchar_t *wpath; |
| 241 | wchar_t *wmode; |
| 242 | wpath=op_utf8_to_utf16(_path); |
| 243 | wmode=op_utf8_to_utf16(_mode); |
| 244 | if(wmode==NULL)errno=EINVAL; |
| 245 | else if(wpath==NULL)errno=ENOENT; |
| 246 | else fp=_wfopen(wpath,wmode); |
| 247 | _ogg_free(wmode); |
| 248 | _ogg_free(wpath); |
| 249 | } |
| 250 | #endif |
| 251 | if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; |
| 252 | return fp; |
| 253 | } |
| 254 |
| 255 | void *op_fdopen(OpusFileCallbacks *_cb,int _fd,const char *_mode){ |
| 256 | FILE *fp; |
| 257 | fp=fdopen(_fd,_mode); |
| 258 | if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; |
| 259 | return fp; |
| 260 | } |
| 261 |
| 262 | void *op_freopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode, |
| 263 | void *_stream){ |
| 264 | FILE *fp; |
| 265 | #if !defined(_WIN32) |
| 266 | fp=freopen(_path,_mode,(FILE *)_stream); |
| 267 | #else |
| 268 | fp=NULL; |
| 269 | if(_path==NULL||_mode==NULL)errno=EINVAL; |
| 270 | else{ |
| 271 | wchar_t *wpath; |
| 272 | wchar_t *wmode; |
| 273 | wpath=op_utf8_to_utf16(_path); |
| 274 | wmode=op_utf8_to_utf16(_mode); |
| 275 | if(wmode==NULL)errno=EINVAL; |
| 276 | else if(wpath==NULL)errno=ENOENT; |
| 277 | else fp=_wfreopen(wpath,wmode,(FILE *)_stream); |
| 278 | _ogg_free(wmode); |
| 279 | _ogg_free(wpath); |
| 280 | } |
| 281 | #endif |
| 282 | if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; |
| 283 | return fp; |
| 284 | } |
| 285 |
| 286 | static int op_mem_read(void *_stream,unsigned char *_ptr,int _buf_size){ |
| 287 | OpusMemStream *stream; |
| 288 | ptrdiff_t size; |
| 289 | ptrdiff_t pos; |
| 290 | stream=(OpusMemStream *)_stream; |
| 291 | /*Check for empty read.*/ |
| 292 | if(_buf_size<=0)return 0; |
| 293 | size=stream->size; |
| 294 | pos=stream->pos; |
| 295 | /*Check for EOF.*/ |
| 296 | if(pos>=size)return 0; |
| 297 | /*Check for a short read.*/ |
| 298 | _buf_size=(int)OP_MIN(size-pos,_buf_size); |
| 299 | memcpy(_ptr,stream->data+pos,_buf_size); |
| 300 | pos+=_buf_size; |
| 301 | stream->pos=pos; |
| 302 | return _buf_size; |
| 303 | } |
| 304 |
| 305 | static int op_mem_seek(void *_stream,opus_int64 _offset,int _whence){ |
| 306 | OpusMemStream *stream; |
| 307 | ptrdiff_t pos; |
| 308 | stream=(OpusMemStream *)_stream; |
| 309 | pos=stream->pos; |
| 310 | OP_ASSERT(pos>=0); |
| 311 | switch(_whence){ |
| 312 | case SEEK_SET:{ |
| 313 | /*Check for overflow:*/ |
| 314 | if(_offset<0||_offset>OP_MEM_DIFF_MAX)return -1; |
| 315 | pos=(ptrdiff_t)_offset; |
| 316 | }break; |
| 317 | case SEEK_CUR:{ |
| 318 | /*Check for overflow:*/ |
| 319 | if(_offset<-pos||_offset>OP_MEM_DIFF_MAX-pos)return -1; |
| 320 | pos=(ptrdiff_t)(pos+_offset); |
| 321 | }break; |
| 322 | case SEEK_END:{ |
| 323 | ptrdiff_t size; |
| 324 | size=stream->size; |
| 325 | OP_ASSERT(size>=0); |
| 326 | /*Check for overflow:*/ |
| 327 | if(_offset>size||_offset<size-OP_MEM_DIFF_MAX)return -1; |
| 328 | pos=(ptrdiff_t)(size-_offset); |
| 329 | }break; |
| 330 | default:return -1; |
| 331 | } |
| 332 | stream->pos=pos; |
| 333 | return 0; |
| 334 | } |
| 335 |
| 336 | static opus_int64 op_mem_tell(void *_stream){ |
| 337 | OpusMemStream *stream; |
| 338 | stream=(OpusMemStream *)_stream; |
| 339 | return (ogg_int64_t)stream->pos; |
| 340 | } |
| 341 |
| 342 | static int op_mem_close(void *_stream){ |
| 343 | _ogg_free(_stream); |
| 344 | return 0; |
| 345 | } |
| 346 |
| 347 | static const OpusFileCallbacks OP_MEM_CALLBACKS={ |
| 348 | op_mem_read, |
| 349 | op_mem_seek, |
| 350 | op_mem_tell, |
| 351 | op_mem_close |
| 352 | }; |
| 353 |
| 354 | void *op_mem_stream_create(OpusFileCallbacks *_cb, |
| 355 | const unsigned char *_data,size_t _size){ |
| 356 | OpusMemStream *stream; |
| 357 | if(_size>OP_MEM_SIZE_MAX)return NULL; |
| 358 | stream=(OpusMemStream *)_ogg_malloc(sizeof(*stream)); |
| 359 | if(stream!=NULL){ |
| 360 | *_cb=*&OP_MEM_CALLBACKS; |
| 361 | stream->data=_data; |
| 362 | stream->size=_size; |
| 363 | stream->pos=0; |
| 364 | } |
| 365 | return stream; |
| 366 | } |
| 367 |