gosu 1.3.0 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (223) hide show
  1. checksums.yaml +4 -4
  2. data/dependencies/SDL/include/SDL.h +108 -14
  3. data/dependencies/SDL/include/SDL_assert.h +81 -50
  4. data/dependencies/SDL/include/SDL_atomic.h +135 -35
  5. data/dependencies/SDL/include/SDL_audio.h +960 -355
  6. data/dependencies/SDL/include/SDL_bits.h +11 -6
  7. data/dependencies/SDL/include/SDL_blendmode.h +91 -14
  8. data/dependencies/SDL/include/SDL_clipboard.h +30 -7
  9. data/dependencies/SDL/include/SDL_config.h +277 -27
  10. data/dependencies/SDL/include/SDL_config_android.h +13 -38
  11. data/dependencies/SDL/include/SDL_config_iphoneos.h +21 -62
  12. data/dependencies/SDL/include/SDL_config_macosx.h +23 -92
  13. data/dependencies/SDL/include/SDL_config_minimal.h +1 -4
  14. data/dependencies/SDL/include/SDL_config_pandora.h +15 -22
  15. data/dependencies/SDL/include/SDL_config_psp.h +16 -37
  16. data/dependencies/SDL/include/SDL_config_windows.h +28 -91
  17. data/dependencies/SDL/include/SDL_config_winrt.h +33 -61
  18. data/dependencies/SDL/include/SDL_config_wiz.h +28 -56
  19. data/dependencies/SDL/include/SDL_copying.h +1 -1
  20. data/dependencies/SDL/include/SDL_cpuinfo.h +331 -71
  21. data/dependencies/SDL/include/SDL_egl.h +906 -280
  22. data/dependencies/SDL/include/SDL_endian.h +101 -47
  23. data/dependencies/SDL/include/SDL_error.h +70 -19
  24. data/dependencies/SDL/include/SDL_events.h +387 -79
  25. data/dependencies/SDL/include/SDL_filesystem.h +73 -64
  26. data/dependencies/SDL/include/SDL_gamecontroller.h +585 -125
  27. data/dependencies/SDL/include/SDL_gesture.h +36 -6
  28. data/dependencies/SDL/include/SDL_haptic.h +304 -210
  29. data/dependencies/SDL/include/SDL_hidapi.h +451 -0
  30. data/dependencies/SDL/include/SDL_hints.h +1286 -897
  31. data/dependencies/SDL/include/SDL_joystick.h +577 -130
  32. data/dependencies/SDL/include/SDL_keyboard.h +162 -63
  33. data/dependencies/SDL/include/SDL_keycode.h +7 -5
  34. data/dependencies/SDL/include/SDL_loadso.h +42 -8
  35. data/dependencies/SDL/include/SDL_locale.h +34 -32
  36. data/dependencies/SDL/include/SDL_log.h +212 -19
  37. data/dependencies/SDL/include/SDL_main.h +72 -17
  38. data/dependencies/SDL/include/SDL_messagebox.h +70 -23
  39. data/dependencies/SDL/include/SDL_metal.h +27 -32
  40. data/dependencies/SDL/include/SDL_misc.h +19 -15
  41. data/dependencies/SDL/include/SDL_mouse.h +262 -110
  42. data/dependencies/SDL/include/SDL_mutex.h +286 -66
  43. data/dependencies/SDL/include/SDL_name.h +1 -1
  44. data/dependencies/SDL/include/SDL_opengl.h +1 -1
  45. data/dependencies/SDL/include/SDL_opengles.h +1 -1
  46. data/dependencies/SDL/include/SDL_opengles2.h +2 -2
  47. data/dependencies/SDL/include/SDL_pixels.h +199 -34
  48. data/dependencies/SDL/include/SDL_platform.h +39 -2
  49. data/dependencies/SDL/include/SDL_power.h +23 -10
  50. data/dependencies/SDL/include/SDL_quit.h +1 -1
  51. data/dependencies/SDL/include/SDL_rect.h +78 -28
  52. data/dependencies/SDL/include/SDL_render.h +1204 -472
  53. data/dependencies/SDL/include/SDL_revision.h +2 -2
  54. data/dependencies/SDL/include/SDL_rwops.h +605 -33
  55. data/dependencies/SDL/include/SDL_scancode.h +1 -1
  56. data/dependencies/SDL/include/SDL_sensor.h +76 -42
  57. data/dependencies/SDL/include/SDL_shape.h +38 -27
  58. data/dependencies/SDL/include/SDL_stdinc.h +96 -24
  59. data/dependencies/SDL/include/SDL_surface.h +571 -139
  60. data/dependencies/SDL/include/SDL_system.h +339 -101
  61. data/dependencies/SDL/include/SDL_syswm.h +50 -20
  62. data/dependencies/SDL/include/SDL_test.h +1 -1
  63. data/dependencies/SDL/include/SDL_test_assert.h +2 -2
  64. data/dependencies/SDL/include/SDL_test_common.h +23 -6
  65. data/dependencies/SDL/include/SDL_test_compare.h +1 -1
  66. data/dependencies/SDL/include/SDL_test_crc32.h +1 -1
  67. data/dependencies/SDL/include/SDL_test_font.h +3 -3
  68. data/dependencies/SDL/include/SDL_test_fuzzer.h +28 -26
  69. data/dependencies/SDL/include/SDL_test_harness.h +6 -6
  70. data/dependencies/SDL/include/SDL_test_images.h +1 -1
  71. data/dependencies/SDL/include/SDL_test_log.h +1 -1
  72. data/dependencies/SDL/include/SDL_test_md5.h +1 -1
  73. data/dependencies/SDL/include/SDL_test_memory.h +1 -1
  74. data/dependencies/SDL/include/SDL_test_random.h +2 -2
  75. data/dependencies/SDL/include/SDL_thread.h +226 -128
  76. data/dependencies/SDL/include/SDL_timer.h +129 -22
  77. data/dependencies/SDL/include/SDL_touch.h +48 -8
  78. data/dependencies/SDL/include/SDL_types.h +1 -1
  79. data/dependencies/SDL/include/SDL_version.h +72 -46
  80. data/dependencies/SDL/include/SDL_video.h +1266 -460
  81. data/dependencies/SDL/include/SDL_vulkan.h +100 -161
  82. data/dependencies/SDL/include/begin_code.h +22 -1
  83. data/dependencies/SDL/include/close_code.h +1 -1
  84. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  85. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  86. data/dependencies/SDL_sound/SDL_sound.c +104 -70
  87. data/dependencies/SDL_sound/SDL_sound.h +6 -6
  88. data/dependencies/SDL_sound/SDL_sound_aiff.c +32 -32
  89. data/dependencies/SDL_sound/SDL_sound_au.c +14 -14
  90. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +6 -7
  91. data/dependencies/SDL_sound/SDL_sound_flac.c +28 -30
  92. data/dependencies/SDL_sound/SDL_sound_internal.h +21 -14
  93. data/dependencies/SDL_sound/SDL_sound_modplug.c +26 -28
  94. data/dependencies/SDL_sound/SDL_sound_mp3.c +20 -36
  95. data/dependencies/SDL_sound/SDL_sound_raw.c +15 -16
  96. data/dependencies/SDL_sound/SDL_sound_shn.c +11 -10
  97. data/dependencies/SDL_sound/SDL_sound_voc.c +20 -18
  98. data/dependencies/SDL_sound/SDL_sound_vorbis.c +11 -9
  99. data/dependencies/SDL_sound/SDL_sound_wav.c +49 -43
  100. data/dependencies/SDL_sound/dr_flac.h +10925 -4466
  101. data/dependencies/SDL_sound/dr_mp3.h +2802 -844
  102. data/dependencies/SDL_sound/libmodplug/fastmix.c +63 -64
  103. data/dependencies/SDL_sound/libmodplug/libmodplug.h +25 -103
  104. data/dependencies/SDL_sound/libmodplug/load_669.c +14 -17
  105. data/dependencies/SDL_sound/libmodplug/load_amf.c +12 -7
  106. data/dependencies/SDL_sound/libmodplug/load_ams.c +103 -44
  107. data/dependencies/SDL_sound/libmodplug/load_dbm.c +8 -4
  108. data/dependencies/SDL_sound/libmodplug/load_dmf.c +55 -25
  109. data/dependencies/SDL_sound/libmodplug/load_far.c +9 -13
  110. data/dependencies/SDL_sound/libmodplug/load_gdm.c +448 -0
  111. data/dependencies/SDL_sound/libmodplug/load_it.c +61 -61
  112. data/dependencies/SDL_sound/libmodplug/load_mdl.c +94 -58
  113. data/dependencies/SDL_sound/libmodplug/load_med.c +20 -11
  114. data/dependencies/SDL_sound/libmodplug/load_mod.c +40 -15
  115. data/dependencies/SDL_sound/libmodplug/load_mt2.c +50 -19
  116. data/dependencies/SDL_sound/libmodplug/load_okt.c +59 -28
  117. data/dependencies/SDL_sound/libmodplug/load_psm.c +104 -79
  118. data/dependencies/SDL_sound/libmodplug/load_ptm.c +18 -17
  119. data/dependencies/SDL_sound/libmodplug/load_s3m.c +9 -7
  120. data/dependencies/SDL_sound/libmodplug/load_stm.c +3 -2
  121. data/dependencies/SDL_sound/libmodplug/load_ult.c +4 -3
  122. data/dependencies/SDL_sound/libmodplug/load_umx.c +315 -35
  123. data/dependencies/SDL_sound/libmodplug/load_xm.c +25 -21
  124. data/dependencies/SDL_sound/libmodplug/mmcmp.c +295 -149
  125. data/dependencies/SDL_sound/libmodplug/modplug.c +7 -123
  126. data/dependencies/SDL_sound/libmodplug/modplug.h +32 -29
  127. data/dependencies/SDL_sound/libmodplug/snd_dsp.c +0 -1
  128. data/dependencies/SDL_sound/libmodplug/snd_flt.c +2 -2
  129. data/dependencies/SDL_sound/libmodplug/snd_fx.c +32 -19
  130. data/dependencies/SDL_sound/libmodplug/sndfile.c +76 -160
  131. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -12
  132. data/dependencies/SDL_sound/libmodplug/tables.h +10 -15
  133. data/dependencies/SDL_sound/stb_vorbis.h +504 -329
  134. data/dependencies/{al_soft → mojoAL}/AL/al.h +38 -30
  135. data/dependencies/{al_soft → mojoAL}/AL/alc.h +27 -56
  136. data/dependencies/mojoAL/mojoal.c +4848 -0
  137. data/dependencies/stb/stb_image.h +208 -73
  138. data/dependencies/stb/stb_image_write.h +57 -23
  139. data/dependencies/stb/stb_truetype.h +345 -279
  140. data/dependencies/utf8proc/utf8proc.c +37 -18
  141. data/dependencies/utf8proc/utf8proc.h +17 -5
  142. data/dependencies/utf8proc/utf8proc_data.h +12012 -10089
  143. data/ext/gosu/extconf.rb +29 -30
  144. data/include/Gosu/Audio.hpp +70 -85
  145. data/include/Gosu/Buttons.hpp +103 -103
  146. data/include/Gosu/Color.hpp +19 -11
  147. data/include/Gosu/Directories.hpp +31 -24
  148. data/include/Gosu/Font.hpp +42 -44
  149. data/include/Gosu/Gosu.hpp +5 -8
  150. data/include/Gosu/Graphics.hpp +58 -71
  151. data/include/Gosu/GraphicsBase.hpp +26 -33
  152. data/include/Gosu/IO.hpp +0 -3
  153. data/include/Gosu/Image.hpp +56 -62
  154. data/include/Gosu/ImageData.hpp +23 -27
  155. data/include/Gosu/Inspection.hpp +1 -4
  156. data/include/Gosu/Math.hpp +0 -3
  157. data/include/Gosu/TextInput.hpp +34 -40
  158. data/include/Gosu/Timing.hpp +2 -8
  159. data/include/Gosu/Version.hpp +2 -2
  160. data/include/Gosu/Window.hpp +71 -70
  161. data/lib/SDL2.dll +0 -0
  162. data/lib/gosu/compat.rb +24 -37
  163. data/lib/gosu.rb +2 -2
  164. data/lib64/SDL2.dll +0 -0
  165. data/src/Audio.cpp +88 -86
  166. data/src/AudioFile.hpp +6 -6
  167. data/src/AudioFileAudioToolbox.cpp +1 -1
  168. data/src/AudioFileSDLSound.cpp +1 -1
  169. data/src/AudioImpl.cpp +0 -7
  170. data/src/AudioImpl.hpp +6 -8
  171. data/src/BitmapIO.cpp +23 -22
  172. data/src/BlockAllocator.cpp +2 -1
  173. data/src/Channel.cpp +22 -20
  174. data/src/Color.cpp +12 -9
  175. data/src/DirectoriesApple.cpp +25 -24
  176. data/src/DirectoriesUnix.cpp +14 -12
  177. data/src/DirectoriesWin.cpp +26 -30
  178. data/src/EmptyImageData.hpp +15 -17
  179. data/src/FileUnix.cpp +1 -1
  180. data/src/FileWin.cpp +1 -1
  181. data/src/Font.cpp +59 -54
  182. data/src/Graphics.cpp +135 -143
  183. data/src/Image.cpp +41 -47
  184. data/src/Input.cpp +1 -1
  185. data/src/InputUIKit.cpp +1 -1
  186. data/src/LargeImageData.cpp +108 -101
  187. data/src/LargeImageData.hpp +17 -15
  188. data/src/Log.hpp +6 -6
  189. data/src/Macro.cpp +35 -37
  190. data/src/Macro.hpp +11 -11
  191. data/src/Math.cpp +8 -1
  192. data/src/Resolution.cpp +12 -7
  193. data/src/RubyGosu.cxx +11 -39
  194. data/src/TexChunk.cpp +50 -41
  195. data/src/TexChunk.hpp +22 -22
  196. data/src/Text.cpp +37 -37
  197. data/src/TextBuilder.cpp +60 -57
  198. data/src/TextBuilder.hpp +20 -20
  199. data/src/TextInput.cpp +127 -135
  200. data/src/TimingApple.cpp +1 -7
  201. data/src/TimingUnix.cpp +0 -6
  202. data/src/TimingWin.cpp +0 -6
  203. data/src/TrueTypeFont.cpp +107 -107
  204. data/src/TrueTypeFont.hpp +39 -38
  205. data/src/TrueTypeFontApple.cpp +19 -22
  206. data/src/TrueTypeFontUnix.cpp +21 -26
  207. data/src/TrueTypeFontWin.cpp +30 -30
  208. data/src/Window.cpp +97 -87
  209. data/src/WindowUIKit.cpp +46 -49
  210. metadata +7 -17
  211. data/dependencies/SDL/include/SDL_config_os2.h +0 -188
  212. data/dependencies/SDL_sound/libmodplug/load_abc.c +0 -4725
  213. data/dependencies/SDL_sound/libmodplug/load_mid.c +0 -1405
  214. data/dependencies/SDL_sound/libmodplug/load_pat.c +0 -1143
  215. data/dependencies/SDL_sound/libmodplug/load_pat.h +0 -25
  216. data/dependencies/al_soft/AL/alext.h +0 -585
  217. data/dependencies/al_soft/AL/efx-creative.h +0 -3
  218. data/dependencies/al_soft/AL/efx-presets.h +0 -402
  219. data/dependencies/al_soft/AL/efx.h +0 -762
  220. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  221. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  222. data/lib/OpenAL32.dll +0 -0
  223. data/lib64/OpenAL32.dll +0 -0
@@ -1,4 +1,4 @@
1
- // Ogg Vorbis audio decoder - v1.14 - public domain
1
+ // Ogg Vorbis audio decoder - v1.22 - public domain
2
2
  // http://nothings.org/stb_vorbis/
3
3
  //
4
4
  // Original version written by Sean Barrett in 2007.
@@ -26,17 +26,29 @@
26
26
  // Terje Mathisen Niklas Frykholm Andy Hill
27
27
  // Casey Muratori John Bolton Gargaj
28
28
  // Laurent Gomila Marc LeBlanc Ronny Chevalier
29
- // Bernhard Wodo Evan Balster alxprd@github
29
+ // Bernhard Wodo Evan Balster github:alxprd
30
30
  // Tom Beaumont Ingo Leitgeb Nicolas Guillemot
31
31
  // Phillip Bennefall Rohit Thiago Goulart
32
- // manxorist@github saga musix github:infatum
33
- // Timur Gagiev
32
+ // github:manxorist Saga Musix github:infatum
33
+ // Timur Gagiev Maxwell Koo Peter Waller
34
+ // github:audinowho Dougall Johnson David Reid
35
+ // github:Clownacy Pedro J. Estebanez Remi Verschelde
36
+ // AnthoFoxo github:morlat Gabriel Ravier
37
+ // Alice Rowan
34
38
  //
35
39
  // Partial history:
40
+ // 1.22 - 2021-07-11 - various small fixes
41
+ // 1.21 - 2021-07-02 - fix bug for files with no comments
42
+ // 1.20 - 2020-07-11 - several small fixes
43
+ // 1.19 - 2020-02-05 - warnings
44
+ // 1.18 - 2020-02-02 - fix seek bugs; parse header comments; misc warnings etc.
45
+ // 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure)
46
+ // 1.16 - 2019-03-04 - fix warnings
47
+ // 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found
36
48
  // 1.14 - 2018-02-11 - delete bogus dealloca usage
37
49
  // 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)
38
50
  // 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
39
- // 1.11 - 2017-07-23 - fix MinGW compilation
51
+ // 1.11 - 2017-07-23 - fix MinGW compilation
40
52
  // 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory
41
53
  // 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version
42
54
  // 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame
@@ -127,9 +139,20 @@ typedef struct
127
139
  int max_frame_size;
128
140
  } stb_vorbis_info;
129
141
 
142
+ typedef struct
143
+ {
144
+ char *vendor;
145
+
146
+ int comment_list_length;
147
+ char **comment_list;
148
+ } stb_vorbis_comment;
149
+
130
150
  // get general information about the file
131
151
  extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
132
152
 
153
+ // get ogg comments
154
+ extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f);
155
+
133
156
  // get the last error detected (clears it, too)
134
157
  extern int stb_vorbis_get_error(stb_vorbis *f);
135
158
 
@@ -201,6 +224,12 @@ extern int stb_vorbis_decode_frame_pushdata(
201
224
  // channel. In other words, (*output)[0][0] contains the first sample from
202
225
  // the first channel, and (*output)[1][0] contains the first sample from
203
226
  // the second channel.
227
+ //
228
+ // *output points into stb_vorbis's internal output buffer storage; these
229
+ // buffers are owned by stb_vorbis and application code should not free
230
+ // them or modify their contents. They are transient and will be overwritten
231
+ // once you ask for more data to get decoded, so be sure to grab any data
232
+ // you need before then.
204
233
 
205
234
  extern void stb_vorbis_flush_pushdata(stb_vorbis *f);
206
235
  // inform stb_vorbis that your next datablock will not be contiguous with
@@ -253,7 +282,7 @@ extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,
253
282
  // create an ogg vorbis decoder from an open FILE *, looking for a stream at
254
283
  // the _current_ seek point (ftell). on failure, returns NULL and sets *error.
255
284
  // note that stb_vorbis must "own" this stream; if you seek it in between
256
- // calls to stb_vorbis, it will become confused. Morever, if you attempt to
285
+ // calls to stb_vorbis, it will become confused. Moreover, if you attempt to
257
286
  // perform stb_vorbis_seek_*() operations on this file, it will assume it
258
287
  // owns the _entire_ rest of the file after the start point. Use the next
259
288
  // function, stb_vorbis_open_file_section(), to limit it.
@@ -379,7 +408,8 @@ enum STBVorbisError
379
408
  VORBIS_invalid_first_page,
380
409
  VORBIS_bad_packet_type,
381
410
  VORBIS_cant_find_last_page,
382
- VORBIS_seek_failed
411
+ VORBIS_seek_failed,
412
+ VORBIS_ogg_skeleton_not_supported
383
413
  };
384
414
 
385
415
 
@@ -520,7 +550,9 @@ enum STBVorbisError
520
550
  // you'd ever want to do it except for debugging.
521
551
  // #define STB_VORBIS_NO_DEFER_FLOOR
522
552
 
523
-
553
+ // STB_VORBIS_NO_COMMENTS
554
+ // disables reading and storing user comments
555
+ // #define STB_VORBIS_NO_COMMENTS
524
556
 
525
557
 
526
558
  //////////////////////////////////////////////////////////////////////////////
@@ -559,14 +591,6 @@ enum STBVorbisError
559
591
  #include <string.h>
560
592
  #include <assert.h>
561
593
  #include <math.h>
562
-
563
- // find definition of alloca if it's not in stdlib.h:
564
- #if defined(_MSC_VER) || defined(__MINGW32__)
565
- #include <malloc.h>
566
- #endif
567
- #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)
568
- #include <alloca.h>
569
- #endif
570
594
  #else // STB_VORBIS_NO_CRT
571
595
  #ifndef NULL
572
596
  #define NULL 0
@@ -575,26 +599,22 @@ enum STBVorbisError
575
599
  #define malloc(s) 0
576
600
  #endif
577
601
  #ifndef free
578
- #define free(s) ((void) 0)
602
+ #define free(p) ((void) 0)
579
603
  #endif
580
604
  #ifndef realloc
581
- #define realloc(s) 0
605
+ #define realloc(p, s) 0
582
606
  #endif
583
607
  #endif // STB_VORBIS_NO_CRT
584
608
 
585
609
  #include <limits.h>
586
610
 
587
- #ifdef __MINGW32__
588
- #define alloca __builtin_alloca
589
- #endif
590
-
591
611
  #ifndef STB_FORCEINLINE
592
612
  #if defined(_MSC_VER)
593
613
  #define STB_FORCEINLINE __forceinline
594
- #elif defined(__GNUC__) || defined(__clang__)
595
- #define STB_FORCEINLINE __attribute__((always_inline))
614
+ #elif (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))) || defined(__clang__)
615
+ #define STB_FORCEINLINE static __inline __attribute__((always_inline))
596
616
  #else
597
- #define STB_FORCEINLINE
617
+ #define STB_FORCEINLINE static inline
598
618
  #endif
599
619
  #endif
600
620
 
@@ -611,7 +631,7 @@ enum STBVorbisError
611
631
  #include <crtdbg.h>
612
632
  #define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])
613
633
  #else
614
- #define CHECK(f) ((void) 0)
634
+ #define CHECK(f) do {} while(0)
615
635
  #endif
616
636
 
617
637
  #define MAX_BLOCKSIZE_LOG 13 // from specification
@@ -633,6 +653,17 @@ typedef unsigned int uint32;
633
653
  typedef signed int int32;
634
654
  #endif
635
655
 
656
+ #ifdef __has_feature
657
+ #if __has_feature(undefined_behavior_sanitizer)
658
+ #define HAS_UBSAN
659
+ #endif
660
+ #endif
661
+ #ifdef HAS_UBSAN
662
+ #define STB_NO_SANITIZE(s) __attribute__((no_sanitize(s)))
663
+ #else
664
+ #define STB_NO_SANITIZE(s)
665
+ #endif
666
+
636
667
  #ifndef TRUE
637
668
  #define TRUE 1
638
669
  #define FALSE 0
@@ -640,6 +671,12 @@ typedef signed int int32;
640
671
 
641
672
  typedef float codetype;
642
673
 
674
+ #ifdef _MSC_VER
675
+ #define STBV_NOTUSED(v) (void)(v)
676
+ #else
677
+ #define STBV_NOTUSED(v) (void)sizeof(v)
678
+ #endif
679
+
643
680
  // @NOTE
644
681
  //
645
682
  // Some arrays below are tagged "//varies", which means it's actually
@@ -771,13 +808,18 @@ struct stb_vorbis
771
808
  unsigned int temp_memory_required;
772
809
  unsigned int setup_temp_memory_required;
773
810
 
811
+ #ifndef STB_VORBIS_NO_COMMENTS
812
+ char *vendor;
813
+ int comment_list_length;
814
+ char **comment_list;
815
+ #endif
816
+
774
817
  // input config
775
818
  #ifndef STB_VORBIS_NO_STDIO
776
819
  FILE *f;
777
820
  uint32 f_start;
778
821
  int close_on_free;
779
822
  #endif
780
-
781
823
  #ifdef __SDL_SOUND_INTERNAL__
782
824
  SDL_RWops *rwops;
783
825
  uint32 rwops_start;
@@ -792,8 +834,11 @@ struct stb_vorbis
792
834
 
793
835
  uint8 push_mode;
794
836
 
837
+ // the page to seek to when seeking to start, may be zero
795
838
  uint32 first_audio_page_offset;
796
839
 
840
+ // p_first is the page on which the first audio packet ends
841
+ // (but not necessarily the page on which it starts)
797
842
  ProbedPage p_first, p_last;
798
843
 
799
844
  // memory management
@@ -842,7 +887,7 @@ struct stb_vorbis
842
887
  int current_loc_valid;
843
888
 
844
889
  // per-blocksize precomputed data
845
-
890
+
846
891
  // twiddle factors
847
892
  float *A[2],*B[2],*C[2];
848
893
  float *window[2];
@@ -876,6 +921,15 @@ struct stb_vorbis
876
921
  // sample-access
877
922
  int channel_buffer_start;
878
923
  int channel_buffer_end;
924
+
925
+ // hack: decode work buffer (used in inverse_mdct and decode_residues)
926
+ void *work_buffer;
927
+
928
+ // temporary buffers
929
+ void *temp_lengths;
930
+ void *temp_codewords;
931
+ void *temp_values;
932
+ void *temp_mults;
879
933
  };
880
934
 
881
935
  #if defined(STB_VORBIS_NO_PUSHDATA_API)
@@ -905,8 +959,8 @@ static int error(vorb *f, enum STBVorbisError e)
905
959
 
906
960
  #define array_size_required(count,size) (count*(sizeof(void *)+(size)))
907
961
 
908
- #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
909
- #define temp_free(f,p) 0
962
+ #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : f->work_buffer)
963
+ #define temp_free(f,p) do {} while (0)
910
964
  #define temp_alloc_save(f) ((f)->temp_offset)
911
965
  #define temp_alloc_restore(f,p) ((f)->temp_offset = (p))
912
966
 
@@ -927,7 +981,7 @@ static void *make_block_array(void *mem, int count, int size)
927
981
 
928
982
  static void *setup_malloc(vorb *f, int sz)
929
983
  {
930
- sz = (sz+3) & ~3;
984
+ sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
931
985
  f->setup_memory_required += sz;
932
986
  if (f->alloc.alloc_buffer) {
933
987
  void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
@@ -946,7 +1000,7 @@ static void setup_free(vorb *f, void *p)
946
1000
 
947
1001
  static void *setup_temp_malloc(vorb *f, int sz)
948
1002
  {
949
- sz = (sz+3) & ~3;
1003
+ sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
950
1004
  if (f->alloc.alloc_buffer) {
951
1005
  if (f->temp_offset - sz < f->setup_offset) return NULL;
952
1006
  f->temp_offset -= sz;
@@ -955,10 +1009,12 @@ static void *setup_temp_malloc(vorb *f, int sz)
955
1009
  return malloc(sz);
956
1010
  }
957
1011
 
958
- static void setup_temp_free(vorb *f, void *p, int sz)
1012
+ static void setup_temp_free(vorb *f, void **_p, int sz)
959
1013
  {
1014
+ void *p = *_p;
1015
+ *_p = NULL;
960
1016
  if (f->alloc.alloc_buffer) {
961
- f->temp_offset += (sz+3)&~3;
1017
+ f->temp_offset += (sz+7)&~7;
962
1018
  return;
963
1019
  }
964
1020
  free(p);
@@ -1039,7 +1095,7 @@ static float float32_unpack(uint32 x)
1039
1095
  uint32 sign = x & 0x80000000;
1040
1096
  uint32 exp = (x & 0x7fe00000) >> 21;
1041
1097
  double res = sign ? -(double)mantissa : (double)mantissa;
1042
- return (float) ldexp((float)res, exp-788);
1098
+ return (float) ldexp((float)res, (int)exp-788);
1043
1099
  }
1044
1100
 
1045
1101
 
@@ -1070,6 +1126,7 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1070
1126
  // find the first entry
1071
1127
  for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;
1072
1128
  if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
1129
+ assert(len[k] < 32); // no error return required, code reading lens checks this
1073
1130
  // add to the list
1074
1131
  add_entry(c, 0, k, m++, len[k], values);
1075
1132
  // add all available leaves
@@ -1083,6 +1140,7 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1083
1140
  uint32 res;
1084
1141
  int z = len[i], y;
1085
1142
  if (z == NO_CODE) continue;
1143
+ assert(z < 32); // no error return required, code reading lens checks this
1086
1144
  // find lowest available leaf (should always be earliest,
1087
1145
  // which is what the specification calls for)
1088
1146
  // note that this property, and the fact we can never have
@@ -1092,12 +1150,10 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1092
1150
  while (z > 0 && !available[z]) --z;
1093
1151
  if (z == 0) { return FALSE; }
1094
1152
  res = available[z];
1095
- assert(z >= 0 && z < 32);
1096
1153
  available[z] = 0;
1097
1154
  add_entry(c, bit_reverse(res), i, m++, len[i], values);
1098
- // propogate availability up the tree
1155
+ // propagate availability up the tree
1099
1156
  if (z != len[i]) {
1100
- assert(len[i] >= 0 && len[i] < 32);
1101
1157
  for (y=len[i]; y > z; --y) {
1102
1158
  assert(available[y] == 0);
1103
1159
  available[y] = res + (1 << (32-y));
@@ -1166,7 +1222,7 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
1166
1222
  if (!c->sparse) {
1167
1223
  int k = 0;
1168
1224
  for (i=0; i < c->entries; ++i)
1169
- if (include_in_sort(c, lengths[i]))
1225
+ if (include_in_sort(c, lengths[i]))
1170
1226
  c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
1171
1227
  assert(k == c->sorted_entries);
1172
1228
  } else {
@@ -1218,13 +1274,19 @@ static int vorbis_validate(uint8 *data)
1218
1274
 
1219
1275
  // called from setup only, once per code book
1220
1276
  // (formula implied by specification)
1277
+ //
1278
+ // hack: suppress an UBSan error caused by invalid input data.
1279
+ // see upstream: https://github.com/nothings/stb/issues/1168.
1280
+ STB_NO_SANITIZE("float-cast-overflow")
1221
1281
  static int lookup1_values(int entries, int dim)
1222
1282
  {
1223
1283
  int r = (int) floor(exp((float) log((float) entries) / dim));
1224
1284
  if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;
1225
1285
  ++r; // floor() to avoid _ftol() when non-CRT
1226
- assert(pow((float) r+1, dim) > entries);
1227
- assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above
1286
+ if (pow((float) r+1, dim) <= entries)
1287
+ return -1;
1288
+ if ((int) floor(pow((float) r, dim)) > entries)
1289
+ return -1;
1228
1290
  return r;
1229
1291
  }
1230
1292
 
@@ -1316,16 +1378,15 @@ static int STBV_CDECL point_compare(const void *p, const void *q)
1316
1378
 
1317
1379
  static uint8 get8(vorb *z)
1318
1380
  {
1319
- if (USE_MEMORY(z)) {
1320
- if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
1321
- return *z->stream++;
1322
- }
1323
-
1324
1381
  #ifdef __SDL_SOUND_INTERNAL__
1325
- {
1326
1382
  uint8 c;
1327
1383
  if (SDL_RWread(z->rwops, &c, 1, 1) != 1) { z->eof = TRUE; return 0; }
1328
1384
  return c;
1385
+
1386
+ #else
1387
+ if (USE_MEMORY(z)) {
1388
+ if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
1389
+ return *z->stream++;
1329
1390
  }
1330
1391
  #endif
1331
1392
 
@@ -1350,22 +1411,21 @@ static uint32 get32(vorb *f)
1350
1411
 
1351
1412
  static int getn(vorb *z, uint8 *data, int n)
1352
1413
  {
1414
+ #ifdef __SDL_SOUND_INTERNAL__
1415
+ if (SDL_RWread(z->rwops, data, n, 1) == 1) return 1;
1416
+ z->eof = 1;
1417
+ return 0;
1418
+
1419
+ #else
1353
1420
  if (USE_MEMORY(z)) {
1354
1421
  if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }
1355
1422
  memcpy(data, z->stream, n);
1356
1423
  z->stream += n;
1357
1424
  return 1;
1358
1425
  }
1359
-
1360
- #ifdef __SDL_SOUND_INTERNAL__
1361
- {
1362
- if (SDL_RWread(z->rwops, data, n, 1) == 1) { return 1; }
1363
- z->eof = 1;
1364
- return 0;
1365
- }
1366
1426
  #endif
1367
1427
 
1368
- #ifndef STB_VORBIS_NO_STDIO
1428
+ #ifndef STB_VORBIS_NO_STDIO
1369
1429
  if (fread(data, n, 1, z->f) == 1)
1370
1430
  return 1;
1371
1431
  else {
@@ -1377,15 +1437,15 @@ static int getn(vorb *z, uint8 *data, int n)
1377
1437
 
1378
1438
  static void skip(vorb *z, int n)
1379
1439
  {
1440
+ #ifdef __SDL_SOUND_INTERNAL__
1441
+ SDL_RWseek(z->rwops, n, RW_SEEK_CUR);
1442
+
1443
+ #else
1380
1444
  if (USE_MEMORY(z)) {
1381
1445
  z->stream += n;
1382
1446
  if (z->stream >= z->stream_end) z->eof = 1;
1383
1447
  return;
1384
1448
  }
1385
- #ifdef __SDL_SOUND_INTERNAL__
1386
- {
1387
- SDL_RWseek(z->rwops, n, RW_SEEK_CUR);
1388
- }
1389
1449
  #endif
1390
1450
 
1391
1451
  #ifndef STB_VORBIS_NO_STDIO
@@ -1402,19 +1462,8 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1402
1462
  if (f->push_mode) return 0;
1403
1463
  #endif
1404
1464
  f->eof = 0;
1405
- if (USE_MEMORY(f)) {
1406
- if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
1407
- f->stream = f->stream_end;
1408
- f->eof = 1;
1409
- return 0;
1410
- } else {
1411
- f->stream = f->stream_start + loc;
1412
- return 1;
1413
- }
1414
- }
1415
1465
 
1416
1466
  #ifdef __SDL_SOUND_INTERNAL__
1417
- {
1418
1467
  if (loc + f->rwops_start < loc || loc >= 0x80000000) {
1419
1468
  loc = 0x7fffffff;
1420
1469
  f->eof = 1;
@@ -1426,6 +1475,17 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1426
1475
  f->eof = 1;
1427
1476
  SDL_RWseek(f->rwops, f->rwops_start, RW_SEEK_END);
1428
1477
  return 0;
1478
+
1479
+ #else
1480
+ if (USE_MEMORY(f)) {
1481
+ if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
1482
+ f->stream = f->stream_end;
1483
+ f->eof = 1;
1484
+ return 0;
1485
+ } else {
1486
+ f->stream = f->stream_start + loc;
1487
+ return 1;
1488
+ }
1429
1489
  }
1430
1490
  #endif
1431
1491
 
@@ -1463,12 +1523,15 @@ static int capture_pattern(vorb *f)
1463
1523
  static int start_page_no_capturepattern(vorb *f)
1464
1524
  {
1465
1525
  uint32 loc0,loc1,n;
1526
+ if (f->first_decode && !IS_PUSH_MODE(f)) {
1527
+ f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4;
1528
+ }
1466
1529
  // stream structure version
1467
1530
  if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
1468
1531
  // header flag
1469
1532
  f->page_flag = get8(f);
1470
1533
  // absolute granule position
1471
- loc0 = get32(f);
1534
+ loc0 = get32(f);
1472
1535
  loc1 = get32(f);
1473
1536
  // @TODO: validate loc0,loc1 as valid positions?
1474
1537
  // stream serial number -- vorbis doesn't interleave, so discard
@@ -1499,15 +1562,12 @@ static int start_page_no_capturepattern(vorb *f)
1499
1562
  }
1500
1563
  if (f->first_decode) {
1501
1564
  int i,len;
1502
- ProbedPage p;
1503
1565
  len = 0;
1504
1566
  for (i=0; i < f->segment_count; ++i)
1505
1567
  len += f->segments[i];
1506
1568
  len += 27 + f->segment_count;
1507
- p.page_start = f->first_audio_page_offset;
1508
- p.page_end = p.page_start + len;
1509
- p.last_decoded_sample = loc0;
1510
- f->p_first = p;
1569
+ f->p_first.page_end = f->p_first.page_start + len;
1570
+ f->p_first.last_decoded_sample = loc0;
1511
1571
  }
1512
1572
  f->next_seg = 0;
1513
1573
  return TRUE;
@@ -1598,6 +1658,18 @@ static int get8_packet(vorb *f)
1598
1658
  return x;
1599
1659
  }
1600
1660
 
1661
+ #ifndef STB_VORBIS_NO_COMMENTS
1662
+ static int get32_packet(vorb *f)
1663
+ {
1664
+ uint32 x;
1665
+ x = get8_packet(f);
1666
+ x += get8_packet(f) << 8;
1667
+ x += get8_packet(f) << 16;
1668
+ x += (uint32) get8_packet(f) << 24;
1669
+ return x;
1670
+ }
1671
+ #endif
1672
+
1601
1673
  static void flush_packet(vorb *f)
1602
1674
  {
1603
1675
  while (get8_packet_raw(f) != EOP);
@@ -1628,7 +1700,8 @@ static uint32 get_bits(vorb *f, int n)
1628
1700
  f->valid_bits += 8;
1629
1701
  }
1630
1702
  }
1631
- if (f->valid_bits < 0) return 0;
1703
+
1704
+ assert(f->valid_bits >= n);
1632
1705
  z = f->acc & ((1 << n)-1);
1633
1706
  f->acc >>= n;
1634
1707
  f->valid_bits -= n;
@@ -1953,69 +2026,69 @@ static int predict_point(int x, int x0, int x1, int y0, int y1)
1953
2026
  // the following table is block-copied from the specification
1954
2027
  static float inverse_db_table[256] =
1955
2028
  {
1956
- 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
1957
- 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
1958
- 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
1959
- 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
1960
- 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
1961
- 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
1962
- 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
1963
- 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
1964
- 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
1965
- 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
1966
- 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
1967
- 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
1968
- 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
1969
- 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
1970
- 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
1971
- 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
1972
- 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
1973
- 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
1974
- 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
1975
- 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
1976
- 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
1977
- 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
1978
- 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
1979
- 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
1980
- 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
1981
- 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
1982
- 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
1983
- 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
1984
- 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
1985
- 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
1986
- 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
1987
- 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
1988
- 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
1989
- 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
1990
- 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
1991
- 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
1992
- 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
1993
- 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
1994
- 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
1995
- 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
1996
- 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
1997
- 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
1998
- 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
1999
- 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
2000
- 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
2001
- 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
2002
- 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
2003
- 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
2004
- 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
2005
- 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
2006
- 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
2007
- 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
2008
- 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
2009
- 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
2010
- 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
2011
- 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
2012
- 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
2013
- 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
2014
- 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
2015
- 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
2016
- 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
2017
- 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
2018
- 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
2029
+ 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
2030
+ 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
2031
+ 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
2032
+ 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
2033
+ 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
2034
+ 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
2035
+ 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
2036
+ 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
2037
+ 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
2038
+ 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
2039
+ 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
2040
+ 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
2041
+ 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
2042
+ 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
2043
+ 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
2044
+ 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
2045
+ 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
2046
+ 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
2047
+ 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
2048
+ 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
2049
+ 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
2050
+ 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
2051
+ 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
2052
+ 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
2053
+ 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
2054
+ 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
2055
+ 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
2056
+ 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
2057
+ 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
2058
+ 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
2059
+ 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
2060
+ 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
2061
+ 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
2062
+ 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
2063
+ 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
2064
+ 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
2065
+ 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
2066
+ 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
2067
+ 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
2068
+ 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
2069
+ 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
2070
+ 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
2071
+ 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
2072
+ 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
2073
+ 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
2074
+ 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
2075
+ 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
2076
+ 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
2077
+ 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
2078
+ 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
2079
+ 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
2080
+ 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
2081
+ 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
2082
+ 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
2083
+ 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
2084
+ 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
2085
+ 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
2086
+ 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
2087
+ 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
2088
+ 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
2089
+ 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
2090
+ 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
2091
+ 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
2019
2092
  0.82788260f, 0.88168307f, 0.9389798f, 1.0f
2020
2093
  };
2021
2094
 
@@ -2075,7 +2148,7 @@ STB_FORCEINLINE void draw_line(float *output, int x0, int y0, int x1, int y1, in
2075
2148
  ady -= abs(base) * adx;
2076
2149
  if (x1 > n) x1 = n;
2077
2150
  if (x < x1) {
2078
- LINE_OP(output[x], inverse_db_table[y]);
2151
+ LINE_OP(output[x], inverse_db_table[y&255]);
2079
2152
  for (++x; x < x1; ++x) {
2080
2153
  err += ady;
2081
2154
  if (err >= adx) {
@@ -2083,7 +2156,7 @@ STB_FORCEINLINE void draw_line(float *output, int x0, int y0, int x1, int y1, in
2083
2156
  y += sy;
2084
2157
  } else
2085
2158
  y += base;
2086
- LINE_OP(output[x], inverse_db_table[y]);
2159
+ LINE_OP(output[x], inverse_db_table[y&255]);
2087
2160
  }
2088
2161
  }
2089
2162
  }
@@ -2189,47 +2262,7 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int
2189
2262
  ++class_set;
2190
2263
  #endif
2191
2264
  }
2192
- } else if (ch == 1) {
2193
- while (pcount < part_read) {
2194
- int z = r->begin + pcount*r->part_size;
2195
- int c_inter = 0, p_inter = z;
2196
- if (pass == 0) {
2197
- Codebook *c = f->codebooks+r->classbook;
2198
- int q;
2199
- DECODE(q,f,c);
2200
- if (q == EOP) goto done;
2201
- #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2202
- part_classdata[0][class_set] = r->classdata[q];
2203
- #else
2204
- for (i=classwords-1; i >= 0; --i) {
2205
- classifications[0][i+pcount] = q % r->classifications;
2206
- q /= r->classifications;
2207
- }
2208
- #endif
2209
- }
2210
- for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2211
- int z = r->begin + pcount*r->part_size;
2212
- #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2213
- int c = part_classdata[0][class_set][i];
2214
- #else
2215
- int c = classifications[0][pcount];
2216
- #endif
2217
- int b = r->residue_books[c][pass];
2218
- if (b >= 0) {
2219
- Codebook *book = f->codebooks + b;
2220
- if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2221
- goto done;
2222
- } else {
2223
- z += r->part_size;
2224
- c_inter = 0;
2225
- p_inter = z;
2226
- }
2227
- }
2228
- #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2229
- ++class_set;
2230
- #endif
2231
- }
2232
- } else {
2265
+ } else if (ch > 2) {
2233
2266
  while (pcount < part_read) {
2234
2267
  int z = r->begin + pcount*r->part_size;
2235
2268
  int c_inter = z % ch, p_inter = z/ch;
@@ -2416,11 +2449,11 @@ void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype)
2416
2449
  #if LIBVORBIS_MDCT
2417
2450
  // directly call the vorbis MDCT using an interface documented
2418
2451
  // by Jeff Roberts... useful for performance comparison
2419
- typedef struct
2452
+ typedef struct
2420
2453
  {
2421
2454
  int n;
2422
2455
  int log2n;
2423
-
2456
+
2424
2457
  float *trig;
2425
2458
  int *bitrev;
2426
2459
 
@@ -2439,7 +2472,7 @@ void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2439
2472
  if (M1.n == n) M = &M1;
2440
2473
  else if (M2.n == n) M = &M2;
2441
2474
  else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; }
2442
- else {
2475
+ else {
2443
2476
  if (M2.n) __asm int 3;
2444
2477
  mdct_init(&M2, n);
2445
2478
  M = &M2;
@@ -2451,7 +2484,7 @@ void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2451
2484
 
2452
2485
 
2453
2486
  // the following were split out into separate functions while optimizing;
2454
- // they could be pushed back up but eh. STB_FORCEINLINE showed no change;
2487
+ // they could be pushed back up but eh. __forceinline showed no change;
2455
2488
  // they're probably already being inlined.
2456
2489
  static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A)
2457
2490
  {
@@ -2640,34 +2673,33 @@ static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A,
2640
2673
 
2641
2674
  while (z > base) {
2642
2675
  float k00,k11;
2643
-
2644
- k00 = z[-0] - z[-8];
2645
- k11 = z[-1] - z[-9];
2646
- z[-0] = z[-0] + z[-8];
2647
- z[-1] = z[-1] + z[-9];
2648
- z[-8] = k00;
2649
- z[-9] = k11 ;
2650
-
2651
- k00 = z[ -2] - z[-10];
2652
- k11 = z[ -3] - z[-11];
2653
- z[ -2] = z[ -2] + z[-10];
2654
- z[ -3] = z[ -3] + z[-11];
2655
- z[-10] = (k00+k11) * A2;
2656
- z[-11] = (k11-k00) * A2;
2657
-
2658
- k00 = z[-12] - z[ -4]; // reverse to avoid a unary negation
2676
+ float l00,l11;
2677
+
2678
+ k00 = z[-0] - z[ -8];
2679
+ k11 = z[-1] - z[ -9];
2680
+ l00 = z[-2] - z[-10];
2681
+ l11 = z[-3] - z[-11];
2682
+ z[ -0] = z[-0] + z[ -8];
2683
+ z[ -1] = z[-1] + z[ -9];
2684
+ z[ -2] = z[-2] + z[-10];
2685
+ z[ -3] = z[-3] + z[-11];
2686
+ z[ -8] = k00;
2687
+ z[ -9] = k11;
2688
+ z[-10] = (l00+l11) * A2;
2689
+ z[-11] = (l11-l00) * A2;
2690
+
2691
+ k00 = z[ -4] - z[-12];
2659
2692
  k11 = z[ -5] - z[-13];
2693
+ l00 = z[ -6] - z[-14];
2694
+ l11 = z[ -7] - z[-15];
2660
2695
  z[ -4] = z[ -4] + z[-12];
2661
2696
  z[ -5] = z[ -5] + z[-13];
2662
- z[-12] = k11;
2663
- z[-13] = k00;
2664
-
2665
- k00 = z[-14] - z[ -6]; // reverse to avoid a unary negation
2666
- k11 = z[ -7] - z[-15];
2667
2697
  z[ -6] = z[ -6] + z[-14];
2668
2698
  z[ -7] = z[ -7] + z[-15];
2669
- z[-14] = (k00+k11) * A2;
2670
- z[-15] = (k00-k11) * A2;
2699
+ z[-12] = k11;
2700
+ z[-13] = -k00;
2701
+ z[-14] = (l11-l00) * A2;
2702
+ z[-15] = (l00+l11) * -A2;
2671
2703
 
2672
2704
  iter_54(z);
2673
2705
  iter_54(z-8);
@@ -2702,7 +2734,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2702
2734
  // once I combined the passes.
2703
2735
 
2704
2736
  // so there's a missing 'times 2' here (for adding X to itself).
2705
- // this propogates through linearly to the end, where the numbers
2737
+ // this propagates through linearly to the end, where the numbers
2706
2738
  // are 1/2 too small, and need to be compensated for.
2707
2739
 
2708
2740
  {
@@ -2852,7 +2884,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2852
2884
  d1[0] = u[k4+1];
2853
2885
  d0[1] = u[k4+2];
2854
2886
  d0[0] = u[k4+3];
2855
-
2887
+
2856
2888
  d0 -= 4;
2857
2889
  d1 -= 4;
2858
2890
  bitrev += 2;
@@ -2933,7 +2965,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2933
2965
  float p0,p1,p2,p3;
2934
2966
 
2935
2967
  p3 = e[6]*B[7] - e[7]*B[6];
2936
- p2 = -e[6]*B[6] - e[7]*B[7];
2968
+ p2 = -e[6]*B[6] - e[7]*B[7];
2937
2969
 
2938
2970
  d0[0] = p3;
2939
2971
  d1[3] = - p3;
@@ -2941,7 +2973,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2941
2973
  d3[3] = p2;
2942
2974
 
2943
2975
  p1 = e[4]*B[5] - e[5]*B[4];
2944
- p0 = -e[4]*B[4] - e[5]*B[5];
2976
+ p0 = -e[4]*B[4] - e[5]*B[5];
2945
2977
 
2946
2978
  d0[1] = p1;
2947
2979
  d1[2] = - p1;
@@ -2949,7 +2981,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2949
2981
  d3[2] = p0;
2950
2982
 
2951
2983
  p3 = e[2]*B[3] - e[3]*B[2];
2952
- p2 = -e[2]*B[2] - e[3]*B[3];
2984
+ p2 = -e[2]*B[2] - e[3]*B[3];
2953
2985
 
2954
2986
  d0[2] = p3;
2955
2987
  d1[1] = - p3;
@@ -2957,7 +2989,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2957
2989
  d3[1] = p2;
2958
2990
 
2959
2991
  p1 = e[0]*B[1] - e[1]*B[0];
2960
- p0 = -e[0]*B[0] - e[1]*B[1];
2992
+ p0 = -e[0]*B[0] - e[1]*B[1];
2961
2993
 
2962
2994
  d0[3] = p1;
2963
2995
  d1[0] = - p1;
@@ -3110,7 +3142,6 @@ static float *get_window(vorb *f, int len)
3110
3142
  len <<= 1;
3111
3143
  if (len == f->blocksize_0) return f->window[0];
3112
3144
  if (len == f->blocksize_1) return f->window[1];
3113
- assert(0);
3114
3145
  return NULL;
3115
3146
  }
3116
3147
 
@@ -3133,6 +3164,7 @@ static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *f
3133
3164
  for (q=1; q < g->values; ++q) {
3134
3165
  j = g->sorted_order[q];
3135
3166
  #ifndef STB_VORBIS_NO_DEFER_FLOOR
3167
+ STBV_NOTUSED(step2_flag);
3136
3168
  if (finalY[j] >= 0)
3137
3169
  #else
3138
3170
  if (step2_flag[j])
@@ -3235,6 +3267,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3235
3267
 
3236
3268
  // WINDOWING
3237
3269
 
3270
+ STBV_NOTUSED(left_end);
3238
3271
  n = f->blocksize[m->blockflag];
3239
3272
  map = &f->mapping[m->mapping];
3240
3273
 
@@ -3432,7 +3465,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3432
3465
  // this isn't to spec, but spec would require us to read ahead
3433
3466
  // and decode the size of all current frames--could be done,
3434
3467
  // but presumably it's not a commonly used feature
3435
- f->current_loc = -n2; // start of first frame is positioned for discard
3468
+ f->current_loc = 0u - n2; // start of first frame is positioned for discard (NB this is an intentional unsigned overflow/wrap-around)
3436
3469
  // we might have to discard samples "from" the next frame too,
3437
3470
  // if we're lapping a large block then a small at the start?
3438
3471
  f->discard_samples_deferred = n - right_end;
@@ -3516,6 +3549,7 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
3516
3549
  if (f->previous_length) {
3517
3550
  int i,j, n = f->previous_length;
3518
3551
  float *w = get_window(f, n);
3552
+ if (w == NULL) return 0;
3519
3553
  for (i=0; i < f->channels; ++i) {
3520
3554
  for (j=0; j < n; ++j)
3521
3555
  f->channel_buffers[i][left+j] =
@@ -3563,7 +3597,7 @@ static int vorbis_pump_first_frame(stb_vorbis *f)
3563
3597
  }
3564
3598
 
3565
3599
  #ifndef STB_VORBIS_NO_PUSHDATA_API
3566
- static int is_whole_packet_present(stb_vorbis *f, int end_page)
3600
+ static int is_whole_packet_present(stb_vorbis *f)
3567
3601
  {
3568
3602
  // make sure that we have the packet available before continuing...
3569
3603
  // this requires a full ogg parse, but we know we can fetch from f->stream
@@ -3583,15 +3617,13 @@ static int is_whole_packet_present(stb_vorbis *f, int end_page)
3583
3617
  break;
3584
3618
  }
3585
3619
  // either this continues, or it ends it...
3586
- if (end_page)
3587
- if (s < f->segment_count-1) return error(f, VORBIS_invalid_stream);
3588
3620
  if (s == f->segment_count)
3589
3621
  s = -1; // set 'crosses page' flag
3590
3622
  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
3591
3623
  first = FALSE;
3592
3624
  }
3593
3625
  for (; s == -1;) {
3594
- uint8 *q;
3626
+ uint8 *q;
3595
3627
  int n;
3596
3628
 
3597
3629
  // check that we have the page header ready
@@ -3617,8 +3649,6 @@ static int is_whole_packet_present(stb_vorbis *f, int end_page)
3617
3649
  if (q[s] < 255)
3618
3650
  break;
3619
3651
  }
3620
- if (end_page)
3621
- if (s < n-1) return error(f, VORBIS_invalid_stream);
3622
3652
  if (s == n)
3623
3653
  s = -1; // set 'crosses page' flag
3624
3654
  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
@@ -3635,6 +3665,7 @@ static int start_decoder(vorb *f)
3635
3665
  int longest_floorlist=0;
3636
3666
 
3637
3667
  // first page, first packet
3668
+ f->first_decode = TRUE;
3638
3669
 
3639
3670
  if (!start_page(f)) return FALSE;
3640
3671
  // validate page flag
@@ -3643,7 +3674,22 @@ static int start_decoder(vorb *f)
3643
3674
  if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page);
3644
3675
  // check for expected packet length
3645
3676
  if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page);
3646
- if (f->segments[0] != 30) return error(f, VORBIS_invalid_first_page);
3677
+ if (f->segments[0] != 30) {
3678
+ // check for the Ogg skeleton fishead identifying header to refine our error
3679
+ if (f->segments[0] == 64 &&
3680
+ getn(f, header, 6) &&
3681
+ header[0] == 'f' &&
3682
+ header[1] == 'i' &&
3683
+ header[2] == 's' &&
3684
+ header[3] == 'h' &&
3685
+ header[4] == 'e' &&
3686
+ header[5] == 'a' &&
3687
+ get8(f) == 'd' &&
3688
+ get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported);
3689
+ else
3690
+ return error(f, VORBIS_invalid_first_page);
3691
+ }
3692
+
3647
3693
  // read packet
3648
3694
  // check packet header
3649
3695
  if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page);
@@ -3677,6 +3723,50 @@ static int start_decoder(vorb *f)
3677
3723
  if (!start_page(f)) return FALSE;
3678
3724
 
3679
3725
  if (!start_packet(f)) return FALSE;
3726
+
3727
+ #ifndef STB_VORBIS_NO_COMMENTS
3728
+ if (!next_segment(f)) return FALSE;
3729
+
3730
+ if (get8_packet(f) != VORBIS_packet_comment) return error(f, VORBIS_invalid_setup);
3731
+ for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3732
+ if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup);
3733
+ //file vendor
3734
+ len = get32_packet(f);
3735
+ f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1));
3736
+ if (f->vendor == NULL) return error(f, VORBIS_outofmem);
3737
+ for(i=0; i < len; ++i) {
3738
+ f->vendor[i] = get8_packet(f);
3739
+ }
3740
+ f->vendor[len] = (char)'\0';
3741
+ //user comments
3742
+ f->comment_list_length = get32_packet(f);
3743
+ f->comment_list = NULL;
3744
+ if (f->comment_list_length > 0)
3745
+ {
3746
+ f->comment_list = (char**) setup_malloc(f, sizeof(char*) * (f->comment_list_length));
3747
+ if (f->comment_list == NULL) return error(f, VORBIS_outofmem);
3748
+ }
3749
+
3750
+ for(i=0; i < f->comment_list_length; ++i) {
3751
+ len = get32_packet(f);
3752
+ f->comment_list[i] = (char*)setup_malloc(f, sizeof(char) * (len+1));
3753
+ if (f->comment_list[i] == NULL) return error(f, VORBIS_outofmem);
3754
+
3755
+ for(j=0; j < len; ++j) {
3756
+ f->comment_list[i][j] = get8_packet(f);
3757
+ }
3758
+ f->comment_list[i][len] = (char)'\0';
3759
+ }
3760
+
3761
+ // framing_flag
3762
+ x = get8_packet(f);
3763
+ if (!(x & 1)) return error(f, VORBIS_invalid_setup);
3764
+
3765
+
3766
+ skip(f, f->bytes_in_seg);
3767
+ f->bytes_in_seg = 0;
3768
+ #endif // STB_VORBIS_NO_COMMENTS
3769
+
3680
3770
  do {
3681
3771
  len = next_segment(f);
3682
3772
  skip(f, len);
@@ -3688,7 +3778,7 @@ static int start_decoder(vorb *f)
3688
3778
 
3689
3779
  #ifndef STB_VORBIS_NO_PUSHDATA_API
3690
3780
  if (IS_PUSH_MODE(f)) {
3691
- if (!is_whole_packet_present(f, TRUE)) {
3781
+ if (!is_whole_packet_present(f)) {
3692
3782
  // convert error in ogg header to write type
3693
3783
  if (f->error == VORBIS_invalid_stream)
3694
3784
  f->error = VORBIS_invalid_setup;
@@ -3706,6 +3796,7 @@ static int start_decoder(vorb *f)
3706
3796
  // codebooks
3707
3797
 
3708
3798
  f->codebook_count = get_bits(f,8) + 1;
3799
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3709
3800
  f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
3710
3801
  if (f->codebooks == NULL) return error(f, VORBIS_outofmem);
3711
3802
  memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
@@ -3728,10 +3819,12 @@ static int start_decoder(vorb *f)
3728
3819
  c->sparse = ordered ? 0 : get_bits(f,1);
3729
3820
 
3730
3821
  if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup);
3822
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3731
3823
 
3732
- if (c->sparse)
3824
+ if (c->sparse) {
3733
3825
  lengths = (uint8 *) setup_temp_malloc(f, c->entries);
3734
- else
3826
+ f->temp_lengths = lengths;
3827
+ } else
3735
3828
  lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
3736
3829
 
3737
3830
  if (!lengths) return error(f, VORBIS_outofmem);
@@ -3742,7 +3835,9 @@ static int start_decoder(vorb *f)
3742
3835
  while (current_entry < c->entries) {
3743
3836
  int limit = c->entries - current_entry;
3744
3837
  int n = get_bits(f, ilog(limit));
3745
- if (current_entry + n > (int) c->entries) { return error(f, VORBIS_invalid_setup); }
3838
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3839
+ if (current_length >= 32) return error(f, VORBIS_invalid_setup);
3840
+ if (current_entry + n > (int) c->entries) return error(f, VORBIS_invalid_setup);
3746
3841
  memset(lengths + current_entry, current_length, n);
3747
3842
  current_entry += n;
3748
3843
  ++current_length;
@@ -3750,11 +3845,11 @@ static int start_decoder(vorb *f)
3750
3845
  } else {
3751
3846
  for (j=0; j < c->entries; ++j) {
3752
3847
  int present = c->sparse ? get_bits(f,1) : 1;
3848
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3753
3849
  if (present) {
3754
3850
  lengths[j] = get_bits(f, 5) + 1;
3755
3851
  ++total;
3756
- if (lengths[j] == 32)
3757
- return error(f, VORBIS_invalid_setup);
3852
+ if (lengths[j] == 32) return error(f, VORBIS_invalid_setup);
3758
3853
  } else {
3759
3854
  lengths[j] = NO_CODE;
3760
3855
  }
@@ -3769,7 +3864,7 @@ static int start_decoder(vorb *f)
3769
3864
  c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
3770
3865
  if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem);
3771
3866
  memcpy(c->codeword_lengths, lengths, c->entries);
3772
- setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
3867
+ setup_temp_free(f, &f->temp_lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
3773
3868
  lengths = c->codeword_lengths;
3774
3869
  c->sparse = 0;
3775
3870
  }
@@ -3799,8 +3894,10 @@ static int start_decoder(vorb *f)
3799
3894
  c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries);
3800
3895
  if (!c->codeword_lengths) return error(f, VORBIS_outofmem);
3801
3896
  c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
3897
+ f->temp_codewords = c->codewords;
3802
3898
  if (!c->codewords) return error(f, VORBIS_outofmem);
3803
3899
  values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
3900
+ f->temp_values = values;
3804
3901
  if (!values) return error(f, VORBIS_outofmem);
3805
3902
  }
3806
3903
  size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
@@ -3809,7 +3906,6 @@ static int start_decoder(vorb *f)
3809
3906
  }
3810
3907
 
3811
3908
  if (!compute_codewords(c, lengths, c->entries, values)) {
3812
- if (c->sparse) setup_temp_free(f, values, 0);
3813
3909
  return error(f, VORBIS_invalid_setup);
3814
3910
  }
3815
3911
 
@@ -3827,9 +3923,9 @@ static int start_decoder(vorb *f)
3827
3923
  }
3828
3924
 
3829
3925
  if (c->sparse) {
3830
- setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
3831
- setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
3832
- setup_temp_free(f, lengths, c->entries);
3926
+ setup_temp_free(f, &f->temp_values, sizeof(*values)*c->sorted_entries);
3927
+ setup_temp_free(f, &f->temp_codewords, sizeof(*c->codewords)*c->sorted_entries);
3928
+ setup_temp_free(f, &f->temp_lengths, c->entries);
3833
3929
  c->codewords = NULL;
3834
3930
  }
3835
3931
 
@@ -3845,16 +3941,19 @@ static int start_decoder(vorb *f)
3845
3941
  c->value_bits = get_bits(f, 4)+1;
3846
3942
  c->sequence_p = get_bits(f,1);
3847
3943
  if (c->lookup_type == 1) {
3848
- c->lookup_values = lookup1_values(c->entries, c->dimensions);
3944
+ int values = lookup1_values(c->entries, c->dimensions);
3945
+ if (values < 0) return error(f, VORBIS_invalid_setup);
3946
+ c->lookup_values = (uint32) values;
3849
3947
  } else {
3850
3948
  c->lookup_values = c->entries * c->dimensions;
3851
3949
  }
3852
3950
  if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup);
3853
3951
  mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
3952
+ f->temp_mults = mults;
3854
3953
  if (mults == NULL) return error(f, VORBIS_outofmem);
3855
3954
  for (j=0; j < (int) c->lookup_values; ++j) {
3856
3955
  int q = get_bits(f, c->value_bits);
3857
- if (q == EOP) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); }
3956
+ if (f->valid_bits < 0) return error(f, VORBIS_invalid_setup);
3858
3957
  mults[j] = q;
3859
3958
  }
3860
3959
 
@@ -3868,21 +3967,19 @@ static int start_decoder(vorb *f)
3868
3967
  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
3869
3968
  } else
3870
3969
  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions);
3871
- if (c->multiplicands == NULL) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
3970
+ if (c->multiplicands == NULL) return error(f, VORBIS_outofmem);
3872
3971
  len = sparse ? c->sorted_entries : c->entries;
3873
3972
  for (j=0; j < len; ++j) {
3874
3973
  unsigned int z = sparse ? c->sorted_values[j] : j;
3875
3974
  unsigned int div=1;
3876
3975
  for (k=0; k < c->dimensions; ++k) {
3877
3976
  int off = (z / div) % c->lookup_values;
3878
- float val = mults[off];
3879
- val = mults[off]*c->delta_value + c->minimum_value + last;
3977
+ float val = mults[off]*c->delta_value + c->minimum_value + last;
3880
3978
  c->multiplicands[j*c->dimensions + k] = val;
3881
3979
  if (c->sequence_p)
3882
3980
  last = val;
3883
3981
  if (k+1 < c->dimensions) {
3884
3982
  if (div > UINT_MAX / (unsigned int) c->lookup_values) {
3885
- setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
3886
3983
  return error(f, VORBIS_invalid_setup);
3887
3984
  }
3888
3985
  div *= c->lookup_values;
@@ -3897,7 +3994,7 @@ static int start_decoder(vorb *f)
3897
3994
  float last=0;
3898
3995
  CHECK(f);
3899
3996
  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
3900
- if (c->multiplicands == NULL) { setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
3997
+ if (c->multiplicands == NULL) return error(f, VORBIS_outofmem);
3901
3998
  for (j=0; j < (int) c->lookup_values; ++j) {
3902
3999
  float val = mults[j] * c->delta_value + c->minimum_value + last;
3903
4000
  c->multiplicands[j] = val;
@@ -3908,7 +4005,7 @@ static int start_decoder(vorb *f)
3908
4005
  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3909
4006
  skip:;
3910
4007
  #endif
3911
- setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
4008
+ setup_temp_free(f, &f->temp_mults, sizeof(mults[0])*c->lookup_values);
3912
4009
 
3913
4010
  CHECK(f);
3914
4011
  }
@@ -3925,6 +4022,7 @@ static int start_decoder(vorb *f)
3925
4022
 
3926
4023
  // Floors
3927
4024
  f->floor_count = get_bits(f, 6)+1;
4025
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3928
4026
  f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
3929
4027
  if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
3930
4028
  for (i=0; i < f->floor_count; ++i) {
@@ -3944,7 +4042,7 @@ static int start_decoder(vorb *f)
3944
4042
  } else {
3945
4043
  stbv__floor_ordering p[31*8+2];
3946
4044
  Floor1 *g = &f->floor_config[i].floor1;
3947
- int max_class = -1;
4045
+ int max_class = -1;
3948
4046
  g->partitions = get_bits(f, 5);
3949
4047
  for (j=0; j < g->partitions; ++j) {
3950
4048
  g->partition_class_list[j] = get_bits(f, 4);
@@ -3954,12 +4052,13 @@ static int start_decoder(vorb *f)
3954
4052
  for (j=0; j <= max_class; ++j) {
3955
4053
  g->class_dimensions[j] = get_bits(f, 3)+1;
3956
4054
  g->class_subclasses[j] = get_bits(f, 2);
4055
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3957
4056
  if (g->class_subclasses[j]) {
3958
4057
  g->class_masterbooks[j] = get_bits(f, 8);
3959
4058
  if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
3960
4059
  }
3961
4060
  for (k=0; k < 1 << g->class_subclasses[j]; ++k) {
3962
- g->subclass_books[j][k] = get_bits(f,8)-1;
4061
+ g->subclass_books[j][k] = (int16)get_bits(f,8)-1;
3963
4062
  if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
3964
4063
  }
3965
4064
  }
@@ -3981,11 +4080,14 @@ static int start_decoder(vorb *f)
3981
4080
  p[j].id = j;
3982
4081
  }
3983
4082
  qsort(p, g->values, sizeof(p[0]), point_compare);
4083
+ for (j=0; j < g->values-1; ++j)
4084
+ if (p[j].x == p[j+1].x)
4085
+ return error(f, VORBIS_invalid_setup);
3984
4086
  for (j=0; j < g->values; ++j)
3985
4087
  g->sorted_order[j] = (uint8) p[j].id;
3986
4088
  // precompute the neighbors
3987
4089
  for (j=2; j < g->values; ++j) {
3988
- int low,hi;
4090
+ int low = 0,hi = 0;
3989
4091
  neighbors(g->Xlist, j, &low,&hi);
3990
4092
  g->neighbors[j][0] = low;
3991
4093
  g->neighbors[j][1] = hi;
@@ -3998,6 +4100,7 @@ static int start_decoder(vorb *f)
3998
4100
 
3999
4101
  // Residue
4000
4102
  f->residue_count = get_bits(f, 6)+1;
4103
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4001
4104
  f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
4002
4105
  if (f->residue_config == NULL) return error(f, VORBIS_outofmem);
4003
4106
  memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
@@ -4012,6 +4115,7 @@ static int start_decoder(vorb *f)
4012
4115
  r->part_size = get_bits(f,24)+1;
4013
4116
  r->classifications = get_bits(f,6)+1;
4014
4117
  r->classbook = get_bits(f,8);
4118
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4015
4119
  if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4016
4120
  for (j=0; j < r->classifications; ++j) {
4017
4121
  uint8 high_bits=0;
@@ -4020,12 +4124,14 @@ static int start_decoder(vorb *f)
4020
4124
  high_bits = get_bits(f,5);
4021
4125
  residue_cascade[j] = high_bits*8 + low_bits;
4022
4126
  }
4127
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4023
4128
  r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
4024
4129
  if (r->residue_books == NULL) return error(f, VORBIS_outofmem);
4025
4130
  for (j=0; j < r->classifications; ++j) {
4026
4131
  for (k=0; k < 8; ++k) {
4027
4132
  if (residue_cascade[j] & (1 << k)) {
4028
4133
  r->residue_books[j][k] = get_bits(f, 8);
4134
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4029
4135
  if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4030
4136
  } else {
4031
4137
  r->residue_books[j][k] = -1;
@@ -4050,11 +4156,12 @@ static int start_decoder(vorb *f)
4050
4156
  }
4051
4157
 
4052
4158
  f->mapping_count = get_bits(f,6)+1;
4159
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4053
4160
  f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
4054
4161
  if (f->mapping == NULL) return error(f, VORBIS_outofmem);
4055
4162
  memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
4056
4163
  for (i=0; i < f->mapping_count; ++i) {
4057
- Mapping *m = f->mapping + i;
4164
+ Mapping *m = f->mapping + i;
4058
4165
  int mapping_type = get_bits(f,16);
4059
4166
  if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
4060
4167
  m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
@@ -4067,9 +4174,11 @@ static int start_decoder(vorb *f)
4067
4174
  max_submaps = m->submaps;
4068
4175
  if (get_bits(f,1)) {
4069
4176
  m->coupling_steps = get_bits(f,8)+1;
4177
+ if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup);
4070
4178
  for (k=0; k < m->coupling_steps; ++k) {
4071
4179
  m->chan[k].magnitude = get_bits(f, ilog(f->channels-1));
4072
4180
  m->chan[k].angle = get_bits(f, ilog(f->channels-1));
4181
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4073
4182
  if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup);
4074
4183
  if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup);
4075
4184
  if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup);
@@ -4106,6 +4215,7 @@ static int start_decoder(vorb *f)
4106
4215
  m->windowtype = get_bits(f,16);
4107
4216
  m->transformtype = get_bits(f,16);
4108
4217
  m->mapping = get_bits(f,8);
4218
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4109
4219
  if (m->windowtype != 0) return error(f, VORBIS_invalid_setup);
4110
4220
  if (m->transformtype != 0) return error(f, VORBIS_invalid_setup);
4111
4221
  if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup);
@@ -4169,16 +4279,28 @@ static int start_decoder(vorb *f)
4169
4279
  f->temp_memory_required = imdct_mem;
4170
4280
  }
4171
4281
 
4172
- f->first_decode = TRUE;
4173
4282
 
4174
4283
  if (f->alloc.alloc_buffer) {
4175
4284
  assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
4176
4285
  // check if there's enough temp memory so we don't error later
4177
4286
  if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)
4178
4287
  return error(f, VORBIS_outofmem);
4288
+ } else {
4289
+ f->work_buffer = setup_malloc(f, f->temp_memory_required);
4290
+ if (f->work_buffer == NULL) return error(f, VORBIS_outofmem);
4179
4291
  }
4180
4292
 
4181
- f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
4293
+ // @TODO: stb_vorbis_seek_start expects first_audio_page_offset to point to a page
4294
+ // without PAGEFLAG_continued_packet, so this either points to the first page, or
4295
+ // the page after the end of the headers. It might be cleaner to point to a page
4296
+ // in the middle of the headers, when that's the page where the first audio packet
4297
+ // starts, but we'd have to also correctly skip the end of any continued packet in
4298
+ // stb_vorbis_seek_start.
4299
+ if (f->next_seg == -1) {
4300
+ f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
4301
+ } else {
4302
+ f->first_audio_page_offset = 0;
4303
+ }
4182
4304
 
4183
4305
  return TRUE;
4184
4306
  }
@@ -4186,6 +4308,15 @@ static int start_decoder(vorb *f)
4186
4308
  static void vorbis_deinit(stb_vorbis *p)
4187
4309
  {
4188
4310
  int i,j;
4311
+
4312
+ #ifndef STB_VORBIS_NO_COMMENTS
4313
+ setup_free(p, p->vendor);
4314
+ for (i=0; i < p->comment_list_length; ++i) {
4315
+ setup_free(p, p->comment_list[i]);
4316
+ }
4317
+ setup_free(p, p->comment_list);
4318
+ #endif
4319
+
4189
4320
  if (p->residue_config) {
4190
4321
  for (i=0; i < p->residue_count; ++i) {
4191
4322
  Residue *r = p->residue_config+i;
@@ -4204,7 +4335,8 @@ static void vorbis_deinit(stb_vorbis *p)
4204
4335
  Codebook *c = p->codebooks + i;
4205
4336
  setup_free(p, c->codeword_lengths);
4206
4337
  setup_free(p, c->multiplicands);
4207
- setup_free(p, c->codewords);
4338
+ if (c->codewords != p->temp_codewords) // Might be the temporary buffer-allocated array.
4339
+ setup_free(p, c->codewords);
4208
4340
  setup_free(p, c->sorted_codewords);
4209
4341
  // c->sorted_values[-1] is the first entry in the array
4210
4342
  setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL);
@@ -4234,6 +4366,13 @@ static void vorbis_deinit(stb_vorbis *p)
4234
4366
  setup_free(p, p->window[i]);
4235
4367
  setup_free(p, p->bit_reverse[i]);
4236
4368
  }
4369
+ if (!p->alloc.alloc_buffer) {
4370
+ setup_free(p, p->work_buffer);
4371
+ setup_temp_free(p, &p->temp_lengths, 0);
4372
+ setup_temp_free(p, &p->temp_codewords, 0);
4373
+ setup_temp_free(p, &p->temp_values, 0);
4374
+ setup_temp_free(p, &p->temp_mults, 0);
4375
+ }
4237
4376
  #ifdef __SDL_SOUND_INTERNAL__
4238
4377
  if (p->close_on_free) SDL_RWclose(p->rwops);
4239
4378
  #endif
@@ -4254,7 +4393,7 @@ static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
4254
4393
  memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
4255
4394
  if (z) {
4256
4395
  p->alloc = *z;
4257
- p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes+3) & ~3;
4396
+ p->alloc.alloc_buffer_length_in_bytes &= ~7;
4258
4397
  p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
4259
4398
  }
4260
4399
  p->eof = 0;
@@ -4292,6 +4431,17 @@ stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
4292
4431
  return d;
4293
4432
  }
4294
4433
 
4434
+ #ifndef STB_VORBIS_NO_COMMENTS
4435
+ stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f)
4436
+ {
4437
+ stb_vorbis_comment d;
4438
+ d.vendor = f->vendor;
4439
+ d.comment_list_length = f->comment_list_length;
4440
+ d.comment_list = f->comment_list;
4441
+ return d;
4442
+ }
4443
+ #endif
4444
+
4295
4445
  int stb_vorbis_get_error(stb_vorbis *f)
4296
4446
  {
4297
4447
  int e = f->error;
@@ -4433,7 +4583,7 @@ int stb_vorbis_decode_frame_pushdata(
4433
4583
  f->error = VORBIS__no_error;
4434
4584
 
4435
4585
  // check that we have the entire packet in memory
4436
- if (!is_whole_packet_present(f, FALSE)) {
4586
+ if (!is_whole_packet_present(f)) {
4437
4587
  *samples = 0;
4438
4588
  return 0;
4439
4589
  }
@@ -4495,6 +4645,7 @@ stb_vorbis *stb_vorbis_open_pushdata(
4495
4645
  *error = VORBIS_need_more_data;
4496
4646
  else
4497
4647
  *error = p.error;
4648
+ vorbis_deinit(&p);
4498
4649
  return NULL;
4499
4650
  }
4500
4651
  f = vorbis_alloc(&p);
@@ -4515,9 +4666,10 @@ unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
4515
4666
  #ifndef STB_VORBIS_NO_PUSHDATA_API
4516
4667
  if (f->push_mode) return 0;
4517
4668
  #endif
4518
- if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
4519
4669
  #ifdef __SDL_SOUND_INTERNAL__
4520
4670
  return (unsigned int) (SDL_RWtell(f->rwops) - f->rwops_start);
4671
+ #else
4672
+ if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
4521
4673
  #endif
4522
4674
  #ifndef STB_VORBIS_NO_STDIO
4523
4675
  return (unsigned int) (ftell(f->f) - f->f_start);
@@ -4555,7 +4707,7 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last)
4555
4707
  header[i] = get8(f);
4556
4708
  if (f->eof) return 0;
4557
4709
  if (header[4] != 0) goto invalid;
4558
- goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24);
4710
+ goal = header[22] + (header[23] << 8) + (header[24]<<16) + ((uint32)header[25]<<24);
4559
4711
  for (i=22; i < 26; ++i)
4560
4712
  header[i] = 0;
4561
4713
  crc = 0;
@@ -4620,10 +4772,12 @@ static int get_seek_page_info(stb_vorbis *f, ProbedPage *z)
4620
4772
  z->page_start = stb_vorbis_get_file_offset(f);
4621
4773
 
4622
4774
  // parse the header
4623
- getn(f, header, 27);
4775
+ if (!getn(f, header, 27))
4776
+ return 0;
4624
4777
  if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
4625
4778
  return 0;
4626
- getn(f, lacing, header[26]);
4779
+ if (!getn(f, lacing, header[26]))
4780
+ return 0;
4627
4781
 
4628
4782
  // determine the length of the payload
4629
4783
  len = 0;
@@ -4641,11 +4795,12 @@ static int get_seek_page_info(stb_vorbis *f, ProbedPage *z)
4641
4795
  return 1;
4642
4796
  }
4643
4797
 
4644
- // rarely used function to seek back to the preceeding page while finding the
4798
+ // rarely used function to seek back to the preceding page while finding the
4645
4799
  // start of a packet
4646
4800
  static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset)
4647
4801
  {
4648
- unsigned int previous_safe, end;
4802
+ unsigned int previous_safe;
4803
+ uint32 end;
4649
4804
 
4650
4805
  // now we want to seek back 64K from the limit
4651
4806
  if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset)
@@ -4672,8 +4827,8 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4672
4827
  {
4673
4828
  ProbedPage left, right, mid;
4674
4829
  int i, start_seg_with_known_loc, end_pos, page_start;
4675
- uint32 delta, stream_length, padding;
4676
- double offset, bytes_per_sample;
4830
+ uint32 delta, stream_length, padding, last_sample_limit;
4831
+ double offset = 0.0, bytes_per_sample = 0.0;
4677
4832
  int probe = 0;
4678
4833
 
4679
4834
  // find the last page and validate the target sample
@@ -4686,9 +4841,9 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4686
4841
  // indicates should be the granule position (give or take one)).
4687
4842
  padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
4688
4843
  if (sample_number < padding)
4689
- sample_number = 0;
4844
+ last_sample_limit = 0;
4690
4845
  else
4691
- sample_number -= padding;
4846
+ last_sample_limit = sample_number - padding;
4692
4847
 
4693
4848
  left = f->p_first;
4694
4849
  while (left.last_decoded_sample == ~0U) {
@@ -4701,9 +4856,12 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4701
4856
  assert(right.last_decoded_sample != ~0U);
4702
4857
 
4703
4858
  // starting from the start is handled differently
4704
- if (sample_number <= left.last_decoded_sample) {
4705
- if (stb_vorbis_seek_start(f))
4859
+ if (last_sample_limit <= left.last_decoded_sample) {
4860
+ if (stb_vorbis_seek_start(f)) {
4861
+ if (f->current_loc > sample_number)
4862
+ return error(f, VORBIS_seek_failed);
4706
4863
  return 1;
4864
+ }
4707
4865
  return 0;
4708
4866
  }
4709
4867
 
@@ -4720,10 +4878,10 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4720
4878
  // first probe (interpolate)
4721
4879
  double data_bytes = right.page_end - left.page_start;
4722
4880
  bytes_per_sample = data_bytes / right.last_decoded_sample;
4723
- offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
4881
+ offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample);
4724
4882
  } else {
4725
4883
  // second probe (try to bound the other side)
4726
- double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
4884
+ double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample;
4727
4885
  if (error >= 0 && error < 8000) error = 8000;
4728
4886
  if (error < 0 && error > -8000) error = -8000;
4729
4887
  offset += error * 2;
@@ -4754,14 +4912,16 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4754
4912
  }
4755
4913
 
4756
4914
  // if we've just found the last page again then we're in a tricky file,
4757
- // and we're close enough.
4758
- if (mid.page_start == right.page_start)
4759
- break;
4760
-
4761
- if (sample_number < mid.last_decoded_sample)
4762
- right = mid;
4763
- else
4764
- left = mid;
4915
+ // and we're close enough (if it wasn't an interpolation probe).
4916
+ if (mid.page_start == right.page_start) {
4917
+ if (probe >= 2 || delta <= 65536)
4918
+ break;
4919
+ } else {
4920
+ if (last_sample_limit < mid.last_decoded_sample)
4921
+ right = mid;
4922
+ else
4923
+ left = mid;
4924
+ }
4765
4925
 
4766
4926
  ++probe;
4767
4927
  }
@@ -4877,8 +5037,8 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
4877
5037
  flush_packet(f);
4878
5038
  }
4879
5039
  }
4880
- // the next frame will start with the sample
4881
- assert(f->current_loc == sample_number);
5040
+ // the next frame should start with the sample
5041
+ if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);
4882
5042
  return 1;
4883
5043
  }
4884
5044
 
@@ -4912,11 +5072,11 @@ int stb_vorbis_seek_start(stb_vorbis *f)
4912
5072
  unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
4913
5073
  {
4914
5074
  unsigned int restore_offset, previous_safe;
4915
- unsigned int end, last_page_loc;
5075
+ unsigned int last_page_loc;
4916
5076
 
4917
5077
  if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
4918
5078
  if (!f->total_samples) {
4919
- unsigned int last;
5079
+ uint32 end,last;
4920
5080
  uint32 lo,hi;
4921
5081
  char header[6];
4922
5082
 
@@ -4954,7 +5114,7 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
4954
5114
  // set. whoops!
4955
5115
  break;
4956
5116
  }
4957
- previous_safe = last_page_loc+1;
5117
+ //previous_safe = last_page_loc+1; // NOTE: not used after this point, but note for debugging
4958
5118
  last_page_loc = stb_vorbis_get_file_offset(f);
4959
5119
  }
4960
5120
 
@@ -5048,8 +5208,14 @@ stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, con
5048
5208
 
5049
5209
  stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc)
5050
5210
  {
5051
- FILE *f = fopen(filename, "rb");
5052
- if (f)
5211
+ FILE *f;
5212
+ #if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)
5213
+ if (0 != fopen_s(&f, filename, "rb"))
5214
+ f = NULL;
5215
+ #else
5216
+ f = fopen(filename, "rb");
5217
+ #endif
5218
+ if (f)
5053
5219
  return stb_vorbis_open_file(f, TRUE, error, alloc);
5054
5220
  if (error) *error = VORBIS_file_open_failure;
5055
5221
  return NULL;
@@ -5089,7 +5255,10 @@ stb_vorbis * stb_vorbis_open_rwops(SDL_RWops *rwops, int close_on_free, int *err
5089
5255
  stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc)
5090
5256
  {
5091
5257
  stb_vorbis *f, p;
5092
- if (data == NULL) return NULL;
5258
+ if (!data) {
5259
+ if (error) *error = VORBIS_unexpected_eof;
5260
+ return NULL;
5261
+ }
5093
5262
  vorbis_init(&p, alloc);
5094
5263
  p.stream = (uint8 *) data;
5095
5264
  p.stream_end = (uint8 *) data + len;
@@ -5134,15 +5303,17 @@ static int8 channel_position[7][6] =
5134
5303
  #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
5135
5304
  typedef union {
5136
5305
  float f;
5137
- int i;
5306
+ // hack: changed this to unsigned to suppress an UBSan error.
5307
+ // see upstream: https://github.com/nothings/stb/issues/1168.
5308
+ unsigned int i;
5138
5309
  } float_conv;
5139
5310
  typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
5140
5311
  #define FASTDEF(x) float_conv x
5141
5312
  // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
5142
5313
  #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
5143
5314
  #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
5144
- #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
5145
- #define check_endianness()
5315
+ #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (int)(temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
5316
+ #define check_endianness()
5146
5317
  #else
5147
5318
  #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
5148
5319
  #define check_endianness()
@@ -5156,7 +5327,7 @@ static void copy_samples(short *dest, float *src, int len)
5156
5327
  for (i=0; i < len; ++i) {
5157
5328
  FASTDEF(temp);
5158
5329
  int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
5159
- if ((unsigned int) (v + 32768) > 65535)
5330
+ if ((unsigned int)v + 32768 > 65535)
5160
5331
  v = v < 0 ? -32768 : 32767;
5161
5332
  dest[i] = v;
5162
5333
  }
@@ -5164,11 +5335,11 @@ static void copy_samples(short *dest, float *src, int len)
5164
5335
 
5165
5336
  static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
5166
5337
  {
5167
- #define BUFFER_SIZE 32
5168
- float buffer[BUFFER_SIZE];
5169
- int i,j,o,n = BUFFER_SIZE;
5338
+ #define STB_BUFFER_SIZE 32
5339
+ float buffer[STB_BUFFER_SIZE];
5340
+ int i,j,o,n = STB_BUFFER_SIZE;
5170
5341
  check_endianness();
5171
- for (o = 0; o < len; o += BUFFER_SIZE) {
5342
+ for (o = 0; o < len; o += STB_BUFFER_SIZE) {
5172
5343
  memset(buffer, 0, sizeof(buffer));
5173
5344
  if (o + n > len) n = len - o;
5174
5345
  for (j=0; j < num_c; ++j) {
@@ -5180,21 +5351,22 @@ static void compute_samples(int mask, short *output, int num_c, float **data, in
5180
5351
  for (i=0; i < n; ++i) {
5181
5352
  FASTDEF(temp);
5182
5353
  int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5183
- if ((unsigned int) (v + 32768) > 65535)
5354
+ if ((unsigned int)v + 32768 > 65535)
5184
5355
  v = v < 0 ? -32768 : 32767;
5185
5356
  output[o+i] = v;
5186
5357
  }
5187
5358
  }
5359
+ #undef STB_BUFFER_SIZE
5188
5360
  }
5189
5361
 
5190
5362
  static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len)
5191
5363
  {
5192
- #define BUFFER_SIZE 32
5193
- float buffer[BUFFER_SIZE];
5194
- int i,j,o,n = BUFFER_SIZE >> 1;
5364
+ #define STB_BUFFER_SIZE 32
5365
+ float buffer[STB_BUFFER_SIZE];
5366
+ int i,j,o,n = STB_BUFFER_SIZE >> 1;
5195
5367
  // o is the offset in the source data
5196
5368
  check_endianness();
5197
- for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
5369
+ for (o = 0; o < len; o += STB_BUFFER_SIZE >> 1) {
5198
5370
  // o2 is the offset in the output data
5199
5371
  int o2 = o << 1;
5200
5372
  memset(buffer, 0, sizeof(buffer));
@@ -5219,11 +5391,12 @@ static void compute_stereo_samples(short *output, int num_c, float **data, int d
5219
5391
  for (i=0; i < (n<<1); ++i) {
5220
5392
  FASTDEF(temp);
5221
5393
  int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5222
- if ((unsigned int) (v + 32768) > 65535)
5394
+ if ((unsigned int)v + 32768 > 65535)
5223
5395
  v = v < 0 ? -32768 : 32767;
5224
5396
  output[o2+i] = v;
5225
5397
  }
5226
5398
  }
5399
+ #undef STB_BUFFER_SIZE
5227
5400
  }
5228
5401
 
5229
5402
  static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples)
@@ -5244,7 +5417,7 @@ static void convert_samples_short(int buf_c, short **buffer, int b_offset, int d
5244
5417
 
5245
5418
  int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples)
5246
5419
  {
5247
- float **output;
5420
+ float **output = NULL;
5248
5421
  int len = stb_vorbis_get_frame_float(f, NULL, &output);
5249
5422
  if (len > num_samples) len = num_samples;
5250
5423
  if (len)
@@ -5268,7 +5441,7 @@ static void convert_channels_short_interleaved(int buf_c, short *buffer, int dat
5268
5441
  FASTDEF(temp);
5269
5442
  float f = data[i][d_offset+j];
5270
5443
  int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15);
5271
- if ((unsigned int) (v + 32768) > 65535)
5444
+ if ((unsigned int)v + 32768 > 65535)
5272
5445
  v = v < 0 ? -32768 : 32767;
5273
5446
  *buffer++ = v;
5274
5447
  }
@@ -5296,8 +5469,6 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short
5296
5469
  float **outputs;
5297
5470
  int len = num_shorts / channels;
5298
5471
  int n=0;
5299
- int z = f->channels;
5300
- if (z > channels) z = channels;
5301
5472
  while (n < len) {
5302
5473
  int k = f->channel_buffer_end - f->channel_buffer_start;
5303
5474
  if (n+k >= len) k = len - n;
@@ -5316,8 +5487,6 @@ int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, in
5316
5487
  {
5317
5488
  float **outputs;
5318
5489
  int n=0;
5319
- int z = f->channels;
5320
- if (z > channels) z = channels;
5321
5490
  while (n < len) {
5322
5491
  int k = f->channel_buffer_end - f->channel_buffer_start;
5323
5492
  if (n+k >= len) k = len - n;
@@ -5467,14 +5636,20 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in
5467
5636
  #endif // STB_VORBIS_NO_PULLDATA_API
5468
5637
 
5469
5638
  /* Version history
5639
+ 1.17 - 2019-07-08 - fix CVE-2019-13217, -13218, -13219, -13220, -13221, -13222, -13223
5640
+ found with Mayhem by ForAllSecure
5641
+ 1.16 - 2019-03-04 - fix warnings
5642
+ 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found
5643
+ 1.14 - 2018-02-11 - delete bogus dealloca usage
5644
+ 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)
5470
5645
  1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
5471
- 1.11 - 2017-07-23 - fix MinGW compilation
5646
+ 1.11 - 2017-07-23 - fix MinGW compilation
5472
5647
  1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory
5473
5648
  1.09 - 2016-04-04 - back out 'avoid discarding last frame' fix from previous version
5474
5649
  1.08 - 2016-04-02 - fixed multiple warnings; fix setup memory leaks;
5475
5650
  avoid discarding last frame of audio data
5476
5651
  1.07 - 2015-01-16 - fixed some warnings, fix mingw, const-correct API
5477
- some more crash fixes when out of memory or with corrupt files
5652
+ some more crash fixes when out of memory or with corrupt files
5478
5653
  1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
5479
5654
  some crash fixes when out of memory or with corrupt files
5480
5655
  1.05 - 2015-04-19 - don't define __forceinline if it's redundant
@@ -5530,38 +5705,38 @@ This software is available under 2 licenses -- choose whichever you prefer.
5530
5705
  ------------------------------------------------------------------------------
5531
5706
  ALTERNATIVE A - MIT License
5532
5707
  Copyright (c) 2017 Sean Barrett
5533
- Permission is hereby granted, free of charge, to any person obtaining a copy of
5534
- this software and associated documentation files (the "Software"), to deal in
5535
- the Software without restriction, including without limitation the rights to
5536
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
5537
- of the Software, and to permit persons to whom the Software is furnished to do
5708
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5709
+ this software and associated documentation files (the "Software"), to deal in
5710
+ the Software without restriction, including without limitation the rights to
5711
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
5712
+ of the Software, and to permit persons to whom the Software is furnished to do
5538
5713
  so, subject to the following conditions:
5539
- The above copyright notice and this permission notice shall be included in all
5714
+ The above copyright notice and this permission notice shall be included in all
5540
5715
  copies or substantial portions of the Software.
5541
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5542
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5543
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5544
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5545
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5546
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5716
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5717
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5718
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5719
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5720
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5721
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5547
5722
  SOFTWARE.
5548
5723
  ------------------------------------------------------------------------------
5549
5724
  ALTERNATIVE B - Public Domain (www.unlicense.org)
5550
5725
  This is free and unencumbered software released into the public domain.
5551
- Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
5552
- software, either in source code form or as a compiled binary, for any purpose,
5726
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
5727
+ software, either in source code form or as a compiled binary, for any purpose,
5553
5728
  commercial or non-commercial, and by any means.
5554
- In jurisdictions that recognize copyright laws, the author or authors of this
5555
- software dedicate any and all copyright interest in the software to the public
5556
- domain. We make this dedication for the benefit of the public at large and to
5557
- the detriment of our heirs and successors. We intend this dedication to be an
5558
- overt act of relinquishment in perpetuity of all present and future rights to
5729
+ In jurisdictions that recognize copyright laws, the author or authors of this
5730
+ software dedicate any and all copyright interest in the software to the public
5731
+ domain. We make this dedication for the benefit of the public at large and to
5732
+ the detriment of our heirs and successors. We intend this dedication to be an
5733
+ overt act of relinquishment in perpetuity of all present and future rights to
5559
5734
  this software under copyright law.
5560
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5561
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5562
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5563
- AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
5564
- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
5735
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5736
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5737
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5738
+ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
5739
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
5565
5740
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5566
5741
  ------------------------------------------------------------------------------
5567
5742
  */