| 1 | /******************************************************************** |
| 2 | * * |
| 3 | * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * |
| 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
| 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
| 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 7 | * * |
| 8 | * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2018 * |
| 9 | * by the Xiph.Org Foundation and contributors https://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 "opus__config.h" |
| 19 | //#endif |
| 20 | |
| 21 | #include "opusfile_internal.h" |
| 22 | |
| 23 | typedef struct OpusMemStream OpusMemStream; |
| 24 | |
| 25 | #define OP_MEM_SIZE_MAX (~(size_t)0>>1) |
| 26 | #define OP_MEM_DIFF_MAX ((ptrdiff_t)OP_MEM_SIZE_MAX) |
| 27 | |
| 28 | /*The context information needed to read from a block of memory as if it were a |
| 29 | file.*/ |
| 30 | struct OpusMemStream{ |
| 31 | /*The block of memory to read from.*/ |
| 32 | const unsigned char *data; |
| 33 | /*The total size of the block. |
| 34 | This must be at most OP_MEM_SIZE_MAX to prevent signed overflow while |
| 35 | seeking.*/ |
| 36 | ptrdiff_t size; |
| 37 | /*The current file position. |
| 38 | This is allowed to be set arbitrarily greater than size (i.e., past the end |
| 39 | of the block, though we will not read data past the end of the block), but |
| 40 | is not allowed to be negative (i.e., before the beginning of the block).*/ |
| 41 | ptrdiff_t pos; |
| 42 | }; |
| 43 | |
| 44 | static int op_fread(void *_stream,unsigned char *_ptr,int _buf_size){ |
| 45 | FILE *stream; |
| 46 | size_t ret; |
| 47 | /*Check for empty read.*/ |
| 48 | if(_buf_size<=0)return 0; |
| 49 | stream=(FILE *)_stream; |
| 50 | ret=fread(_ptr,1,_buf_size,stream); |
| 51 | OP_ASSERT(ret<=(size_t)_buf_size); |
| 52 | /*If ret==0 and !feof(stream), there was a read error.*/ |
| 53 | return ret>0||feof(stream)?(int)ret:OP_EREAD; |
| 54 | } |
| 55 | |
| 56 | static int op_fseek(void *_stream,opus_int64 _offset,int _whence){ |
| 57 | #if defined(_WIN32xxx) // ppgb comment this out because Praat is compiled with x86_64-w64-mingw32, which does have fseeko but no _filelengthi64 |
| 58 | /*_fseeki64() is not exposed until MSVCRT80. |
| 59 | This is the default starting with MSVC 2005 (_MSC_VER>=1400), but we want |
| 60 | to allow linking against older MSVCRT versions for compatibility back to |
| 61 | XP without installing extra runtime libraries. |
| 62 | i686-pc-mingw32 does not have fseeko() and requires |
| 63 | __MSVCRT_VERSION__>=0x800 for _fseeki64(), which screws up linking with |
| 64 | other libraries (that don't use MSVCRT80 from MSVC 2005 by default). |
| 65 | i686-w64-mingw32 does have fseeko() and respects _FILE_OFFSET_BITS, but I |
| 66 | don't know how to detect that at compile time. |
| 67 | We could just use fseeko64() (which is available in both), but it's |
| 68 | implemented using fgetpos()/fsetpos() just like this code, except without |
| 69 | the overflow checking, so we prefer our version.*/ |
| 70 | opus_int64 pos; |
| 71 | /*We don't use fpos_t directly because it might be a struct if __STDC__ is |
| 72 | non-zero or _INTEGRAL_MAX_BITS < 64. |
| 73 | I'm not certain when the latter is true, but someone could in theory set |
| 74 | the former. |
| 75 | Either way, it should be binary compatible with a normal 64-bit int (this |
| 76 | assumption is not portable, but I believe it is true for MSVCRT).*/ |
| 77 | OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); |
| 78 | /*Translate the seek to an absolute one.*/ |
| 79 | if(_whence==SEEK_CUR){ |
| 80 | int ret; |
| 81 | ret=fgetpos((FILE *)_stream,(fpos_t *)&pos); |
| 82 | if(ret)return ret; |
| 83 | } |
| 84 | else if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream)); |
| 85 | else if(_whence==SEEK_SET)pos=0; |
| 86 | else return -1; |
| 87 | /*Check for errors or overflow.*/ |
| 88 | if(pos<0||_offset<-pos||_offset>OP_INT64_MAX-pos)return -1; |
| 89 | pos+=_offset; |
| 90 | return fsetpos((FILE *)_stream,(fpos_t *)&pos); |
| 91 | #else |
| 92 | /*This function actually conforms to the SUSv2 and POSIX.1-2001, so we prefer |
| 93 | it except on Windows.*/ |
| 94 | return fseeko((FILE *)_stream,(off_t)_offset,_whence); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | static opus_int64 op_ftell(void *_stream){ |
| 99 | #if defined(_WIN32) |
| 100 | /*_ftelli64() is not exposed until MSVCRT80, and ftello()/ftello64() have |
| 101 | the same problems as fseeko()/fseeko64() in MingW. |
| 102 | See above for a more detailed explanation.*/ |
| 103 | opus_int64 pos; |
| 104 | OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); |
| 105 | return fgetpos((FILE *)_stream,(fpos_t *)&pos)?-1:pos; |
| 106 | #else |
| 107 | /*This function actually conforms to the SUSv2 and POSIX.1-2001, so we prefer |
| 108 | it except on Windows.*/ |
| 109 | return ftello((FILE *)_stream); |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | static const OpusFileCallbacks OP_FILE_CALLBACKS={ |
| 114 | op_fread, |
| 115 | op_fseek, |
| 116 | op_ftell, |
| 117 | (op_close_func)fclose |
| 118 | }; |
| 119 | |
| 120 | #if defined(_WIN32) |
| 121 | # include <stddef.h> |
| 122 | # include <errno.h> |
| 123 | |
| 124 | /*Windows doesn't accept UTF-8 by default, and we don't have a wchar_t API, |
| 125 | so if we just pass the path to fopen(), then there'd be no way for a user |
| 126 | of our API to open a Unicode filename. |
| 127 | Instead, we translate from UTF-8 to UTF-16 and use Windows' wchar_t API. |
| 128 | This makes this API more consistent with platforms where the character set |
| 129 | used by fopen is the same as used on disk, which is generally UTF-8, and |
| 130 | with our metadata API, which always uses UTF-8.*/ |
| 131 | static wchar_t *op_utf8_to_utf16(const char *_src){ |
| 132 | wchar_t *dst; |
| 133 | size_t len; |
| 134 | len=strlen(_src); |
| 135 | /*Worst-case output is 1 wide character per 1 input character.*/ |
| 136 | dst=(wchar_t *)_Melder_malloc(sizeof(*dst)*(len+1)); |
| 137 | if(dst!=NULL){ |
| 138 | size_t si; |
| 139 | size_t di; |
| 140 | for(di=si=0;si<len;si++){ |
| 141 | int c0; |
| 142 | c0=(unsigned char)_src[si]; |
| 143 | if(!(c0&0x80)){ |
| 144 | /*Start byte says this is a 1-byte sequence.*/ |
| 145 | dst[di++]=(wchar_t)c0; |
| 146 | continue; |
| 147 | } |
| 148 | else{ |
| 149 | int c1; |
| 150 | /*This is safe, because c0 was not 0 and _src is NUL-terminated.*/ |
| 151 | c1=(unsigned char)_src[si+1]; |
| 152 | if((c1&0xC0)==0x80){ |
| 153 | /*Found at least one continuation byte.*/ |
| 154 | if((c0&0xE0)==0xC0){ |
| 155 | wchar_t w; |
| 156 | /*Start byte says this is a 2-byte sequence.*/ |
| 157 | w=(c0&0x1F)<<6|c1&0x3F; |
| 158 | if(w>=0x80U){ |
| 159 | /*This is a 2-byte sequence that is not overlong.*/ |
| 160 | dst[di++]=w; |
| 161 | si++; |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | else{ |
| 166 | int c2; |
| 167 | /*This is safe, because c1 was not 0 and _src is NUL-terminated.*/ |
| 168 | c2=(unsigned char)_src[si+2]; |
| 169 | if((c2&0xC0)==0x80){ |
| 170 | /*Found at least two continuation bytes.*/ |
| 171 | if((c0&0xF0)==0xE0){ |
| 172 | wchar_t w; |
| 173 | /*Start byte says this is a 3-byte sequence.*/ |
| 174 | w=(c0&0xF)<<12|(c1&0x3F)<<6|c2&0x3F; |
| 175 | if(w>=0x800U&&(w<0xD800||w>=0xE000)&&w<0xFFFE){ |
| 176 | /*This is a 3-byte sequence that is not overlong, not a |
| 177 | UTF-16 surrogate pair value, and not a 'not a character' |
| 178 | value.*/ |
| 179 | dst[di++]=w; |
| 180 | si+=2; |
| 181 | continue; |
| 182 | } |
| 183 | } |
| 184 | else{ |
| 185 | int c3; |
| 186 | /*This is safe, because c2 was not 0 and _src is |
| 187 | NUL-terminated.*/ |
| 188 | c3=(unsigned char)_src[si+3]; |
| 189 | if((c3&0xC0)==0x80){ |
| 190 | /*Found at least three continuation bytes.*/ |
| 191 | if((c0&0xF8)==0xF0){ |
| 192 | opus_uint32 w; |
| 193 | /*Start byte says this is a 4-byte sequence.*/ |
| 194 | w=(c0&7)<<18|(c1&0x3F)<<12|(c2&0x3F)<<6&(c3&0x3F); |
| 195 | if(w>=0x10000U&&w<0x110000U){ |
| 196 | /*This is a 4-byte sequence that is not overlong and not |
| 197 | greater than the largest valid Unicode code point. |
| 198 | Convert it to a surrogate pair.*/ |
| 199 | w-=0x10000; |
| 200 | dst[di++]=(wchar_t)(0xD800+(w>>10)); |
| 201 | dst[di++]=(wchar_t)(0xDC00+(w&0x3FF)); |
| 202 | si+=3; |
| 203 | continue; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | /*If we got here, we encountered an illegal UTF-8 sequence.*/ |
| 213 | Melder_free(dst); |
| 214 | return NULL; |
| 215 | } |
| 216 | OP_ASSERT(di<=len); |
| 217 | dst[di]='\0'; |
| 218 | } |
| 219 | return dst; |
| 220 | } |
| 221 | |
| 222 | /*fsetpos() internally dispatches to the win32 API call SetFilePointer(). |
| 223 | According to SetFilePointer()'s documentation [0], the behavior is |
| 224 | undefined if you do not call it on "a file stored on a seeking device". |
| 225 | However, none of the MSVCRT seeking functions verify what kind of file is |
| 226 | being used before calling it (which I believe is a bug, since they are |
| 227 | supposed to fail and return an error, but it is a bug that has been there |
| 228 | for multiple decades now). |
| 229 | In practice, SetFilePointer() appears to succeed for things like stdin, |
| 230 | even when you are not just piping in a regular file, which prevents the use |
| 231 | of this API to determine whether it is possible to seek in a file at all. |
| 232 | Therefore, we take the approach recommended by the SetFilePointer() |
| 233 | documentation and confirm the type of file using GetFileType() first. |
| 234 | We do this once, when the file is opened, and return the corresponding |
| 235 | callback in order to avoid an extra win32 API call on every seek in the |
| 236 | common case. |
| 237 | Hopefully the return value of GetFileType() cannot actually change for the |
| 238 | lifetime of a file handle. |
| 239 | [0] https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-setfilepointer |
| 240 | */ |
| 241 | static int op_fseek_fail(void *_stream,opus_int64 _offset,int _whence){ |
| 242 | (void)_stream; |
| 243 | (void)_offset; |
| 244 | (void)_whence; |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | static const OpusFileCallbacks OP_UNSEEKABLE_FILE_CALLBACKS={ |
| 249 | op_fread, |
| 250 | op_fseek_fail, |
| 251 | op_ftell, |
| 252 | (op_close_func)fclose |
| 253 | }; |
| 254 | |
| 255 | # define WIN32_LEAN_AND_MEAN |
| 256 | # define WIN32_EXTRA_LEAN |
| 257 | # include <windows.h> |
| 258 | #include <io.h> |
| 259 | |
| 260 | static const OpusFileCallbacks *op_get_file_callbacks(FILE *_fp){ |
| 261 | intptr_t h_file; |
| 262 | h_file=_get_osfhandle(_fileno(_fp)); |
| 263 | if(h_file!=-1 |
| 264 | &&(GetFileType((HANDLE)h_file)&~FILE_TYPE_REMOTE)==FILE_TYPE_DISK){ |
| 265 | return &OP_FILE_CALLBACKS; |
| 266 | } |
| 267 | return &OP_UNSEEKABLE_FILE_CALLBACKS; |
| 268 | } |
| 269 | #else |
| 270 | static const OpusFileCallbacks *op_get_file_callbacks(FILE *_fp){ |
| 271 | (void)_fp; |
| 272 | return &OP_FILE_CALLBACKS; |
| 273 | } |
| 274 | #endif |
| 275 | |
| 276 | void *op_fopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode){ |
| 277 | FILE *fp; |
| 278 | #if !defined(_WIN32) |
| 279 | fp=fopen(_path,_mode); |
| 280 | #else |
| 281 | fp=NULL; |
| 282 | { |
| 283 | wchar_t *wpath; |
| 284 | wchar_t *wmode; |
| 285 | wpath=op_utf8_to_utf16(_path); |
| 286 | wmode=op_utf8_to_utf16(_mode); |
| 287 | if(wmode==NULL)errno=EINVAL; |
| 288 | else if(wpath==NULL)errno=ENOENT; |
| 289 | else fp=_wfopen(wpath,wmode); |
| 290 | Melder_free(wmode); |
| 291 | Melder_free(wpath); |
| 292 | } |
| 293 | #endif |
| 294 | if(fp!=NULL)*_cb=*op_get_file_callbacks(fp); |
| 295 | return fp; |
| 296 | } |
| 297 | |
| 298 | void *op_fdopen(OpusFileCallbacks *_cb,int _fd,const char *_mode){ |
| 299 | FILE *fp; |
| 300 | fp=fdopen(_fd,_mode); |
| 301 | if(fp!=NULL)*_cb=*op_get_file_callbacks(fp); |
| 302 | return fp; |
| 303 | } |
| 304 | |
| 305 | void *op_freopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode, |
| 306 | void *_stream){ |
| 307 | FILE *fp; |
| 308 | #if !defined(_WIN32) |
| 309 | fp=freopen(_path,_mode,(FILE *)_stream); |
| 310 | #else |
| 311 | fp=NULL; |
| 312 | { |
| 313 | wchar_t *wpath; |
| 314 | wchar_t *wmode; |
| 315 | wpath=op_utf8_to_utf16(_path); |
| 316 | wmode=op_utf8_to_utf16(_mode); |
| 317 | if(wmode==NULL)errno=EINVAL; |
| 318 | else if(wpath==NULL)errno=ENOENT; |
| 319 | else fp=_wfreopen(wpath,wmode,(FILE *)_stream); |
| 320 | Melder_free(wmode); |
| 321 | Melder_free(wpath); |
| 322 | } |
| 323 | #endif |
| 324 | if(fp!=NULL)*_cb=*op_get_file_callbacks(fp); |
| 325 | return fp; |
| 326 | } |
| 327 | |
| 328 | static int op_mem_read(void *_stream,unsigned char *_ptr,int _buf_size){ |
| 329 | OpusMemStream *stream; |
| 330 | ptrdiff_t size; |
| 331 | ptrdiff_t pos; |
| 332 | stream=(OpusMemStream *)_stream; |
| 333 | /*Check for empty read.*/ |
| 334 | if(_buf_size<=0)return 0; |
| 335 | size=stream->size; |
| 336 | pos=stream->pos; |
| 337 | /*Check for EOF.*/ |
| 338 | if(pos>=size)return 0; |
| 339 | /*Check for a short read.*/ |
| 340 | _buf_size=(int)OP_MIN(size-pos,_buf_size); |
| 341 | memcpy(_ptr,stream->data+pos,_buf_size); |
| 342 | pos+=_buf_size; |
| 343 | stream->pos=pos; |
| 344 | return _buf_size; |
| 345 | } |
| 346 | |
| 347 | static int op_mem_seek(void *_stream,opus_int64 _offset,int _whence){ |
| 348 | OpusMemStream *stream; |
| 349 | ptrdiff_t pos; |
| 350 | stream=(OpusMemStream *)_stream; |
| 351 | pos=stream->pos; |
| 352 | OP_ASSERT(pos>=0); |
| 353 | switch(_whence){ |
| 354 | case SEEK_SET:{ |
| 355 | /*Check for overflow:*/ |
| 356 | if(_offset<0||_offset>OP_MEM_DIFF_MAX)return -1; |
| 357 | pos=(ptrdiff_t)_offset; |
| 358 | }break; |
| 359 | case SEEK_CUR:{ |
| 360 | /*Check for overflow:*/ |
| 361 | if(_offset<-pos||_offset>OP_MEM_DIFF_MAX-pos)return -1; |
| 362 | pos=(ptrdiff_t)(pos+_offset); |
| 363 | }break; |
| 364 | case SEEK_END:{ |
| 365 | ptrdiff_t size; |
| 366 | size=stream->size; |
| 367 | OP_ASSERT(size>=0); |
| 368 | /*Check for overflow:*/ |
| 369 | if(_offset<-size||_offset>OP_MEM_DIFF_MAX-size)return -1; |
| 370 | pos=(ptrdiff_t)(size+_offset); |
| 371 | }break; |
| 372 | default:return -1; |
| 373 | } |
| 374 | stream->pos=pos; |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static opus_int64 op_mem_tell(void *_stream){ |
| 379 | OpusMemStream *stream; |
| 380 | stream=(OpusMemStream *)_stream; |
| 381 | return (ogg_int64_t)stream->pos; |
| 382 | } |
| 383 | |
| 384 | static int op_mem_close(void *_stream){ |
| 385 | Melder_free(_stream); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static const OpusFileCallbacks OP_MEM_CALLBACKS={ |
| 390 | op_mem_read, |
| 391 | op_mem_seek, |
| 392 | op_mem_tell, |
| 393 | op_mem_close |
| 394 | }; |
| 395 | |
| 396 | void *op_mem_stream_create(OpusFileCallbacks *_cb, |
| 397 | const unsigned char *_data,size_t _size){ |
| 398 | OpusMemStream *stream; |
| 399 | if(_size>OP_MEM_SIZE_MAX)return NULL; |
| 400 | stream=(OpusMemStream *)_Melder_malloc(sizeof(*stream)); |
| 401 | if(stream!=NULL){ |
| 402 | *_cb=*&OP_MEM_CALLBACKS; |
| 403 | stream->data=_data; |
| 404 | stream->size=_size; |
| 405 | stream->pos=0; |
| 406 | } |
| 407 | return stream; |
| 408 | } |