gosu 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) 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 +83 -7
  87. data/dependencies/SDL_sound/SDL_sound.h +4 -4
  88. data/dependencies/SDL_sound/SDL_sound_aiff.c +9 -12
  89. data/dependencies/SDL_sound/SDL_sound_au.c +7 -7
  90. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +3 -3
  91. data/dependencies/SDL_sound/SDL_sound_flac.c +1 -1
  92. data/dependencies/SDL_sound/SDL_sound_internal.h +17 -10
  93. data/dependencies/SDL_sound/SDL_sound_modplug.c +25 -27
  94. data/dependencies/SDL_sound/SDL_sound_mp3.c +5 -17
  95. data/dependencies/SDL_sound/SDL_sound_raw.c +11 -11
  96. data/dependencies/SDL_sound/SDL_sound_shn.c +8 -7
  97. data/dependencies/SDL_sound/SDL_sound_voc.c +6 -4
  98. data/dependencies/SDL_sound/SDL_sound_vorbis.c +6 -11
  99. data/dependencies/SDL_sound/SDL_sound_wav.c +35 -29
  100. data/dependencies/SDL_sound/dr_flac.h +618 -220
  101. data/dependencies/SDL_sound/dr_mp3.h +263 -94
  102. data/dependencies/SDL_sound/libmodplug/fastmix.c +58 -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 +11 -7
  106. data/dependencies/SDL_sound/libmodplug/load_ams.c +65 -22
  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 +45 -49
  112. data/dependencies/SDL_sound/libmodplug/load_mdl.c +80 -53
  113. data/dependencies/SDL_sound/libmodplug/load_med.c +20 -12
  114. data/dependencies/SDL_sound/libmodplug/load_mod.c +40 -15
  115. data/dependencies/SDL_sound/libmodplug/load_mt2.c +29 -17
  116. data/dependencies/SDL_sound/libmodplug/load_okt.c +12 -8
  117. data/dependencies/SDL_sound/libmodplug/load_psm.c +101 -78
  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 +2 -2
  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 +24 -18
  130. data/dependencies/SDL_sound/libmodplug/sndfile.c +55 -156
  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 +508 -325
  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 +4594 -0
  137. data/ext/gosu/extconf.rb +29 -30
  138. data/include/Gosu/Audio.hpp +70 -85
  139. data/include/Gosu/Color.hpp +19 -11
  140. data/include/Gosu/Font.hpp +40 -44
  141. data/include/Gosu/Graphics.hpp +58 -71
  142. data/include/Gosu/GraphicsBase.hpp +26 -33
  143. data/include/Gosu/Image.hpp +56 -62
  144. data/include/Gosu/ImageData.hpp +23 -27
  145. data/include/Gosu/Inspection.hpp +1 -4
  146. data/include/Gosu/TextInput.hpp +34 -40
  147. data/include/Gosu/Version.hpp +1 -1
  148. data/include/Gosu/Window.hpp +71 -70
  149. data/lib/SDL2.dll +0 -0
  150. data/lib/gosu/compat.rb +24 -37
  151. data/lib/gosu.rb +2 -2
  152. data/lib64/SDL2.dll +0 -0
  153. data/src/Audio.cpp +86 -86
  154. data/src/AudioFile.hpp +6 -6
  155. data/src/AudioFileAudioToolbox.cpp +1 -1
  156. data/src/AudioFileSDLSound.cpp +1 -1
  157. data/src/AudioImpl.hpp +5 -5
  158. data/src/BitmapIO.cpp +0 -20
  159. data/src/BlockAllocator.cpp +2 -1
  160. data/src/Channel.cpp +22 -20
  161. data/src/Color.cpp +12 -9
  162. data/src/EmptyImageData.hpp +15 -17
  163. data/src/FileUnix.cpp +1 -1
  164. data/src/FileWin.cpp +1 -1
  165. data/src/Font.cpp +48 -53
  166. data/src/Graphics.cpp +135 -143
  167. data/src/Image.cpp +41 -42
  168. data/src/Input.cpp +1 -1
  169. data/src/InputUIKit.cpp +1 -1
  170. data/src/LargeImageData.cpp +108 -101
  171. data/src/LargeImageData.hpp +17 -15
  172. data/src/Log.hpp +6 -6
  173. data/src/Macro.cpp +35 -37
  174. data/src/Macro.hpp +11 -11
  175. data/src/Math.cpp +8 -1
  176. data/src/Resolution.cpp +12 -7
  177. data/src/RubyGosu.cxx +5 -5
  178. data/src/TexChunk.cpp +50 -41
  179. data/src/TexChunk.hpp +22 -22
  180. data/src/Text.cpp +37 -37
  181. data/src/TextBuilder.cpp +60 -57
  182. data/src/TextBuilder.hpp +20 -20
  183. data/src/TextInput.cpp +127 -135
  184. data/src/TrueTypeFont.cpp +107 -107
  185. data/src/TrueTypeFont.hpp +39 -38
  186. data/src/TrueTypeFontApple.cpp +19 -22
  187. data/src/TrueTypeFontUnix.cpp +21 -26
  188. data/src/TrueTypeFontWin.cpp +30 -30
  189. data/src/Window.cpp +95 -86
  190. data/src/WindowUIKit.cpp +46 -49
  191. metadata +7 -17
  192. data/dependencies/SDL/include/SDL_config_os2.h +0 -188
  193. data/dependencies/SDL_sound/libmodplug/load_abc.c +0 -4725
  194. data/dependencies/SDL_sound/libmodplug/load_mid.c +0 -1405
  195. data/dependencies/SDL_sound/libmodplug/load_pat.c +0 -1143
  196. data/dependencies/SDL_sound/libmodplug/load_pat.h +0 -25
  197. data/dependencies/al_soft/AL/alext.h +0 -585
  198. data/dependencies/al_soft/AL/efx-creative.h +0 -3
  199. data/dependencies/al_soft/AL/efx-presets.h +0 -402
  200. data/dependencies/al_soft/AL/efx.h +0 -762
  201. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  202. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  203. data/lib/OpenAL32.dll +0 -0
  204. 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,37 @@ 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
611
+ /* we need alloca() regardless of STB_VORBIS_NO_CRT,
612
+ * because there is not a corresponding 'dealloca' */
613
+ #if !defined(alloca)
614
+ # if defined(HAVE_ALLOCA_H)
615
+ # include <alloca.h>
616
+ # elif defined(__GNUC__)
617
+ # define alloca __builtin_alloca
618
+ # elif defined(_MSC_VER)
619
+ # include <malloc.h>
620
+ # define alloca _alloca
621
+ # elif defined(__WATCOMC__)
622
+ # include <malloc.h>
623
+ # endif
589
624
  #endif
590
625
 
591
626
  #ifndef STB_FORCEINLINE
592
627
  #if defined(_MSC_VER)
593
628
  #define STB_FORCEINLINE __forceinline
594
- #elif defined(__GNUC__) || defined(__clang__)
595
- #define STB_FORCEINLINE __attribute__((always_inline))
629
+ #elif (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))) || defined(__clang__)
630
+ #define STB_FORCEINLINE static __inline __attribute__((always_inline))
596
631
  #else
597
- #define STB_FORCEINLINE
632
+ #define STB_FORCEINLINE static inline
598
633
  #endif
599
634
  #endif
600
635
 
@@ -633,6 +668,17 @@ typedef unsigned int uint32;
633
668
  typedef signed int int32;
634
669
  #endif
635
670
 
671
+ #ifdef __has_feature
672
+ #if __has_feature(undefined_behavior_sanitizer)
673
+ #define HAS_UBSAN
674
+ #endif
675
+ #endif
676
+ #ifdef HAS_UBSAN
677
+ #define STB_NO_SANITIZE(s) __attribute__((no_sanitize(s)))
678
+ #else
679
+ #define STB_NO_SANITIZE(s)
680
+ #endif
681
+
636
682
  #ifndef TRUE
637
683
  #define TRUE 1
638
684
  #define FALSE 0
@@ -640,6 +686,12 @@ typedef signed int int32;
640
686
 
641
687
  typedef float codetype;
642
688
 
689
+ #ifdef _MSC_VER
690
+ #define STBV_NOTUSED(v) (void)(v)
691
+ #else
692
+ #define STBV_NOTUSED(v) (void)sizeof(v)
693
+ #endif
694
+
643
695
  // @NOTE
644
696
  //
645
697
  // Some arrays below are tagged "//varies", which means it's actually
@@ -771,13 +823,18 @@ struct stb_vorbis
771
823
  unsigned int temp_memory_required;
772
824
  unsigned int setup_temp_memory_required;
773
825
 
826
+ #ifndef STB_VORBIS_NO_COMMENTS
827
+ char *vendor;
828
+ int comment_list_length;
829
+ char **comment_list;
830
+ #endif
831
+
774
832
  // input config
775
833
  #ifndef STB_VORBIS_NO_STDIO
776
834
  FILE *f;
777
835
  uint32 f_start;
778
836
  int close_on_free;
779
837
  #endif
780
-
781
838
  #ifdef __SDL_SOUND_INTERNAL__
782
839
  SDL_RWops *rwops;
783
840
  uint32 rwops_start;
@@ -792,8 +849,11 @@ struct stb_vorbis
792
849
 
793
850
  uint8 push_mode;
794
851
 
852
+ // the page to seek to when seeking to start, may be zero
795
853
  uint32 first_audio_page_offset;
796
854
 
855
+ // p_first is the page on which the first audio packet ends
856
+ // (but not necessarily the page on which it starts)
797
857
  ProbedPage p_first, p_last;
798
858
 
799
859
  // memory management
@@ -842,7 +902,7 @@ struct stb_vorbis
842
902
  int current_loc_valid;
843
903
 
844
904
  // per-blocksize precomputed data
845
-
905
+
846
906
  // twiddle factors
847
907
  float *A[2],*B[2],*C[2];
848
908
  float *window[2];
@@ -876,6 +936,12 @@ struct stb_vorbis
876
936
  // sample-access
877
937
  int channel_buffer_start;
878
938
  int channel_buffer_end;
939
+
940
+ // temporary buffers
941
+ void *temp_lengths;
942
+ void *temp_codewords;
943
+ void *temp_values;
944
+ void *temp_mults;
879
945
  };
880
946
 
881
947
  #if defined(STB_VORBIS_NO_PUSHDATA_API)
@@ -906,7 +972,7 @@ static int error(vorb *f, enum STBVorbisError e)
906
972
  #define array_size_required(count,size) (count*(sizeof(void *)+(size)))
907
973
 
908
974
  #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
909
- #define temp_free(f,p) 0
975
+ #define temp_free(f,p) (void)0
910
976
  #define temp_alloc_save(f) ((f)->temp_offset)
911
977
  #define temp_alloc_restore(f,p) ((f)->temp_offset = (p))
912
978
 
@@ -927,7 +993,7 @@ static void *make_block_array(void *mem, int count, int size)
927
993
 
928
994
  static void *setup_malloc(vorb *f, int sz)
929
995
  {
930
- sz = (sz+3) & ~3;
996
+ sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
931
997
  f->setup_memory_required += sz;
932
998
  if (f->alloc.alloc_buffer) {
933
999
  void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
@@ -946,7 +1012,7 @@ static void setup_free(vorb *f, void *p)
946
1012
 
947
1013
  static void *setup_temp_malloc(vorb *f, int sz)
948
1014
  {
949
- sz = (sz+3) & ~3;
1015
+ sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
950
1016
  if (f->alloc.alloc_buffer) {
951
1017
  if (f->temp_offset - sz < f->setup_offset) return NULL;
952
1018
  f->temp_offset -= sz;
@@ -955,10 +1021,12 @@ static void *setup_temp_malloc(vorb *f, int sz)
955
1021
  return malloc(sz);
956
1022
  }
957
1023
 
958
- static void setup_temp_free(vorb *f, void *p, int sz)
1024
+ static void setup_temp_free(vorb *f, void **_p, int sz)
959
1025
  {
1026
+ void *p = *_p;
1027
+ *_p = NULL;
960
1028
  if (f->alloc.alloc_buffer) {
961
- f->temp_offset += (sz+3)&~3;
1029
+ f->temp_offset += (sz+7)&~7;
962
1030
  return;
963
1031
  }
964
1032
  free(p);
@@ -1039,7 +1107,7 @@ static float float32_unpack(uint32 x)
1039
1107
  uint32 sign = x & 0x80000000;
1040
1108
  uint32 exp = (x & 0x7fe00000) >> 21;
1041
1109
  double res = sign ? -(double)mantissa : (double)mantissa;
1042
- return (float) ldexp((float)res, exp-788);
1110
+ return (float) ldexp((float)res, (int)exp-788);
1043
1111
  }
1044
1112
 
1045
1113
 
@@ -1070,6 +1138,7 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1070
1138
  // find the first entry
1071
1139
  for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;
1072
1140
  if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
1141
+ assert(len[k] < 32); // no error return required, code reading lens checks this
1073
1142
  // add to the list
1074
1143
  add_entry(c, 0, k, m++, len[k], values);
1075
1144
  // add all available leaves
@@ -1083,6 +1152,7 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1083
1152
  uint32 res;
1084
1153
  int z = len[i], y;
1085
1154
  if (z == NO_CODE) continue;
1155
+ assert(z < 32); // no error return required, code reading lens checks this
1086
1156
  // find lowest available leaf (should always be earliest,
1087
1157
  // which is what the specification calls for)
1088
1158
  // note that this property, and the fact we can never have
@@ -1092,12 +1162,10 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1092
1162
  while (z > 0 && !available[z]) --z;
1093
1163
  if (z == 0) { return FALSE; }
1094
1164
  res = available[z];
1095
- assert(z >= 0 && z < 32);
1096
1165
  available[z] = 0;
1097
1166
  add_entry(c, bit_reverse(res), i, m++, len[i], values);
1098
- // propogate availability up the tree
1167
+ // propagate availability up the tree
1099
1168
  if (z != len[i]) {
1100
- assert(len[i] >= 0 && len[i] < 32);
1101
1169
  for (y=len[i]; y > z; --y) {
1102
1170
  assert(available[y] == 0);
1103
1171
  available[y] = res + (1 << (32-y));
@@ -1166,7 +1234,7 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
1166
1234
  if (!c->sparse) {
1167
1235
  int k = 0;
1168
1236
  for (i=0; i < c->entries; ++i)
1169
- if (include_in_sort(c, lengths[i]))
1237
+ if (include_in_sort(c, lengths[i]))
1170
1238
  c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
1171
1239
  assert(k == c->sorted_entries);
1172
1240
  } else {
@@ -1218,13 +1286,19 @@ static int vorbis_validate(uint8 *data)
1218
1286
 
1219
1287
  // called from setup only, once per code book
1220
1288
  // (formula implied by specification)
1289
+ //
1290
+ // hack: suppress an UBSan error caused by invalid input data.
1291
+ // see upstream: https://github.com/nothings/stb/issues/1168.
1292
+ STB_NO_SANITIZE("float-cast-overflow")
1221
1293
  static int lookup1_values(int entries, int dim)
1222
1294
  {
1223
1295
  int r = (int) floor(exp((float) log((float) entries) / dim));
1224
1296
  if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;
1225
1297
  ++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
1298
+ if (pow((float) r+1, dim) <= entries)
1299
+ return -1;
1300
+ if ((int) floor(pow((float) r, dim)) > entries)
1301
+ return -1;
1228
1302
  return r;
1229
1303
  }
1230
1304
 
@@ -1316,16 +1390,15 @@ static int STBV_CDECL point_compare(const void *p, const void *q)
1316
1390
 
1317
1391
  static uint8 get8(vorb *z)
1318
1392
  {
1319
- if (USE_MEMORY(z)) {
1320
- if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
1321
- return *z->stream++;
1322
- }
1323
-
1324
1393
  #ifdef __SDL_SOUND_INTERNAL__
1325
- {
1326
1394
  uint8 c;
1327
1395
  if (SDL_RWread(z->rwops, &c, 1, 1) != 1) { z->eof = TRUE; return 0; }
1328
1396
  return c;
1397
+
1398
+ #else
1399
+ if (USE_MEMORY(z)) {
1400
+ if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
1401
+ return *z->stream++;
1329
1402
  }
1330
1403
  #endif
1331
1404
 
@@ -1350,22 +1423,21 @@ static uint32 get32(vorb *f)
1350
1423
 
1351
1424
  static int getn(vorb *z, uint8 *data, int n)
1352
1425
  {
1426
+ #ifdef __SDL_SOUND_INTERNAL__
1427
+ if (SDL_RWread(z->rwops, data, n, 1) == 1) return 1;
1428
+ z->eof = 1;
1429
+ return 0;
1430
+
1431
+ #else
1353
1432
  if (USE_MEMORY(z)) {
1354
1433
  if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }
1355
1434
  memcpy(data, z->stream, n);
1356
1435
  z->stream += n;
1357
1436
  return 1;
1358
1437
  }
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
1438
  #endif
1367
1439
 
1368
- #ifndef STB_VORBIS_NO_STDIO
1440
+ #ifndef STB_VORBIS_NO_STDIO
1369
1441
  if (fread(data, n, 1, z->f) == 1)
1370
1442
  return 1;
1371
1443
  else {
@@ -1377,15 +1449,15 @@ static int getn(vorb *z, uint8 *data, int n)
1377
1449
 
1378
1450
  static void skip(vorb *z, int n)
1379
1451
  {
1452
+ #ifdef __SDL_SOUND_INTERNAL__
1453
+ SDL_RWseek(z->rwops, n, RW_SEEK_CUR);
1454
+
1455
+ #else
1380
1456
  if (USE_MEMORY(z)) {
1381
1457
  z->stream += n;
1382
1458
  if (z->stream >= z->stream_end) z->eof = 1;
1383
1459
  return;
1384
1460
  }
1385
- #ifdef __SDL_SOUND_INTERNAL__
1386
- {
1387
- SDL_RWseek(z->rwops, n, RW_SEEK_CUR);
1388
- }
1389
1461
  #endif
1390
1462
 
1391
1463
  #ifndef STB_VORBIS_NO_STDIO
@@ -1402,19 +1474,8 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1402
1474
  if (f->push_mode) return 0;
1403
1475
  #endif
1404
1476
  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
1477
 
1416
1478
  #ifdef __SDL_SOUND_INTERNAL__
1417
- {
1418
1479
  if (loc + f->rwops_start < loc || loc >= 0x80000000) {
1419
1480
  loc = 0x7fffffff;
1420
1481
  f->eof = 1;
@@ -1426,6 +1487,17 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1426
1487
  f->eof = 1;
1427
1488
  SDL_RWseek(f->rwops, f->rwops_start, RW_SEEK_END);
1428
1489
  return 0;
1490
+
1491
+ #else
1492
+ if (USE_MEMORY(f)) {
1493
+ if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
1494
+ f->stream = f->stream_end;
1495
+ f->eof = 1;
1496
+ return 0;
1497
+ } else {
1498
+ f->stream = f->stream_start + loc;
1499
+ return 1;
1500
+ }
1429
1501
  }
1430
1502
  #endif
1431
1503
 
@@ -1463,12 +1535,15 @@ static int capture_pattern(vorb *f)
1463
1535
  static int start_page_no_capturepattern(vorb *f)
1464
1536
  {
1465
1537
  uint32 loc0,loc1,n;
1538
+ if (f->first_decode && !IS_PUSH_MODE(f)) {
1539
+ f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4;
1540
+ }
1466
1541
  // stream structure version
1467
1542
  if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
1468
1543
  // header flag
1469
1544
  f->page_flag = get8(f);
1470
1545
  // absolute granule position
1471
- loc0 = get32(f);
1546
+ loc0 = get32(f);
1472
1547
  loc1 = get32(f);
1473
1548
  // @TODO: validate loc0,loc1 as valid positions?
1474
1549
  // stream serial number -- vorbis doesn't interleave, so discard
@@ -1499,15 +1574,12 @@ static int start_page_no_capturepattern(vorb *f)
1499
1574
  }
1500
1575
  if (f->first_decode) {
1501
1576
  int i,len;
1502
- ProbedPage p;
1503
1577
  len = 0;
1504
1578
  for (i=0; i < f->segment_count; ++i)
1505
1579
  len += f->segments[i];
1506
1580
  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;
1581
+ f->p_first.page_end = f->p_first.page_start + len;
1582
+ f->p_first.last_decoded_sample = loc0;
1511
1583
  }
1512
1584
  f->next_seg = 0;
1513
1585
  return TRUE;
@@ -1598,6 +1670,18 @@ static int get8_packet(vorb *f)
1598
1670
  return x;
1599
1671
  }
1600
1672
 
1673
+ #ifndef STB_VORBIS_NO_COMMENTS
1674
+ static int get32_packet(vorb *f)
1675
+ {
1676
+ uint32 x;
1677
+ x = get8_packet(f);
1678
+ x += get8_packet(f) << 8;
1679
+ x += get8_packet(f) << 16;
1680
+ x += (uint32) get8_packet(f) << 24;
1681
+ return x;
1682
+ }
1683
+ #endif
1684
+
1601
1685
  static void flush_packet(vorb *f)
1602
1686
  {
1603
1687
  while (get8_packet_raw(f) != EOP);
@@ -1628,7 +1712,8 @@ static uint32 get_bits(vorb *f, int n)
1628
1712
  f->valid_bits += 8;
1629
1713
  }
1630
1714
  }
1631
- if (f->valid_bits < 0) return 0;
1715
+
1716
+ assert(f->valid_bits >= n);
1632
1717
  z = f->acc & ((1 << n)-1);
1633
1718
  f->acc >>= n;
1634
1719
  f->valid_bits -= n;
@@ -1953,69 +2038,69 @@ static int predict_point(int x, int x0, int x1, int y0, int y1)
1953
2038
  // the following table is block-copied from the specification
1954
2039
  static float inverse_db_table[256] =
1955
2040
  {
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,
2041
+ 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
2042
+ 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
2043
+ 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
2044
+ 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
2045
+ 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
2046
+ 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
2047
+ 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
2048
+ 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
2049
+ 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
2050
+ 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
2051
+ 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
2052
+ 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
2053
+ 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
2054
+ 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
2055
+ 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
2056
+ 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
2057
+ 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
2058
+ 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
2059
+ 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
2060
+ 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
2061
+ 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
2062
+ 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
2063
+ 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
2064
+ 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
2065
+ 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
2066
+ 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
2067
+ 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
2068
+ 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
2069
+ 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
2070
+ 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
2071
+ 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
2072
+ 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
2073
+ 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
2074
+ 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
2075
+ 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
2076
+ 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
2077
+ 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
2078
+ 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
2079
+ 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
2080
+ 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
2081
+ 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
2082
+ 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
2083
+ 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
2084
+ 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
2085
+ 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
2086
+ 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
2087
+ 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
2088
+ 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
2089
+ 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
2090
+ 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
2091
+ 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
2092
+ 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
2093
+ 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
2094
+ 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
2095
+ 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
2096
+ 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
2097
+ 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
2098
+ 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
2099
+ 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
2100
+ 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
2101
+ 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
2102
+ 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
2103
+ 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
2019
2104
  0.82788260f, 0.88168307f, 0.9389798f, 1.0f
2020
2105
  };
2021
2106
 
@@ -2075,7 +2160,7 @@ STB_FORCEINLINE void draw_line(float *output, int x0, int y0, int x1, int y1, in
2075
2160
  ady -= abs(base) * adx;
2076
2161
  if (x1 > n) x1 = n;
2077
2162
  if (x < x1) {
2078
- LINE_OP(output[x], inverse_db_table[y]);
2163
+ LINE_OP(output[x], inverse_db_table[y&255]);
2079
2164
  for (++x; x < x1; ++x) {
2080
2165
  err += ady;
2081
2166
  if (err >= adx) {
@@ -2083,7 +2168,7 @@ STB_FORCEINLINE void draw_line(float *output, int x0, int y0, int x1, int y1, in
2083
2168
  y += sy;
2084
2169
  } else
2085
2170
  y += base;
2086
- LINE_OP(output[x], inverse_db_table[y]);
2171
+ LINE_OP(output[x], inverse_db_table[y&255]);
2087
2172
  }
2088
2173
  }
2089
2174
  }
@@ -2189,47 +2274,7 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int
2189
2274
  ++class_set;
2190
2275
  #endif
2191
2276
  }
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 {
2277
+ } else if (ch > 2) {
2233
2278
  while (pcount < part_read) {
2234
2279
  int z = r->begin + pcount*r->part_size;
2235
2280
  int c_inter = z % ch, p_inter = z/ch;
@@ -2416,11 +2461,11 @@ void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype)
2416
2461
  #if LIBVORBIS_MDCT
2417
2462
  // directly call the vorbis MDCT using an interface documented
2418
2463
  // by Jeff Roberts... useful for performance comparison
2419
- typedef struct
2464
+ typedef struct
2420
2465
  {
2421
2466
  int n;
2422
2467
  int log2n;
2423
-
2468
+
2424
2469
  float *trig;
2425
2470
  int *bitrev;
2426
2471
 
@@ -2439,7 +2484,7 @@ void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2439
2484
  if (M1.n == n) M = &M1;
2440
2485
  else if (M2.n == n) M = &M2;
2441
2486
  else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; }
2442
- else {
2487
+ else {
2443
2488
  if (M2.n) __asm int 3;
2444
2489
  mdct_init(&M2, n);
2445
2490
  M = &M2;
@@ -2451,7 +2496,7 @@ void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2451
2496
 
2452
2497
 
2453
2498
  // the following were split out into separate functions while optimizing;
2454
- // they could be pushed back up but eh. STB_FORCEINLINE showed no change;
2499
+ // they could be pushed back up but eh. __forceinline showed no change;
2455
2500
  // they're probably already being inlined.
2456
2501
  static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A)
2457
2502
  {
@@ -2640,34 +2685,33 @@ static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A,
2640
2685
 
2641
2686
  while (z > base) {
2642
2687
  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
2688
+ float l00,l11;
2689
+
2690
+ k00 = z[-0] - z[ -8];
2691
+ k11 = z[-1] - z[ -9];
2692
+ l00 = z[-2] - z[-10];
2693
+ l11 = z[-3] - z[-11];
2694
+ z[ -0] = z[-0] + z[ -8];
2695
+ z[ -1] = z[-1] + z[ -9];
2696
+ z[ -2] = z[-2] + z[-10];
2697
+ z[ -3] = z[-3] + z[-11];
2698
+ z[ -8] = k00;
2699
+ z[ -9] = k11;
2700
+ z[-10] = (l00+l11) * A2;
2701
+ z[-11] = (l11-l00) * A2;
2702
+
2703
+ k00 = z[ -4] - z[-12];
2659
2704
  k11 = z[ -5] - z[-13];
2705
+ l00 = z[ -6] - z[-14];
2706
+ l11 = z[ -7] - z[-15];
2660
2707
  z[ -4] = z[ -4] + z[-12];
2661
2708
  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
2709
  z[ -6] = z[ -6] + z[-14];
2668
2710
  z[ -7] = z[ -7] + z[-15];
2669
- z[-14] = (k00+k11) * A2;
2670
- z[-15] = (k00-k11) * A2;
2711
+ z[-12] = k11;
2712
+ z[-13] = -k00;
2713
+ z[-14] = (l11-l00) * A2;
2714
+ z[-15] = (l00+l11) * -A2;
2671
2715
 
2672
2716
  iter_54(z);
2673
2717
  iter_54(z-8);
@@ -2702,7 +2746,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2702
2746
  // once I combined the passes.
2703
2747
 
2704
2748
  // so there's a missing 'times 2' here (for adding X to itself).
2705
- // this propogates through linearly to the end, where the numbers
2749
+ // this propagates through linearly to the end, where the numbers
2706
2750
  // are 1/2 too small, and need to be compensated for.
2707
2751
 
2708
2752
  {
@@ -2852,7 +2896,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2852
2896
  d1[0] = u[k4+1];
2853
2897
  d0[1] = u[k4+2];
2854
2898
  d0[0] = u[k4+3];
2855
-
2899
+
2856
2900
  d0 -= 4;
2857
2901
  d1 -= 4;
2858
2902
  bitrev += 2;
@@ -2933,7 +2977,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2933
2977
  float p0,p1,p2,p3;
2934
2978
 
2935
2979
  p3 = e[6]*B[7] - e[7]*B[6];
2936
- p2 = -e[6]*B[6] - e[7]*B[7];
2980
+ p2 = -e[6]*B[6] - e[7]*B[7];
2937
2981
 
2938
2982
  d0[0] = p3;
2939
2983
  d1[3] = - p3;
@@ -2941,7 +2985,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2941
2985
  d3[3] = p2;
2942
2986
 
2943
2987
  p1 = e[4]*B[5] - e[5]*B[4];
2944
- p0 = -e[4]*B[4] - e[5]*B[5];
2988
+ p0 = -e[4]*B[4] - e[5]*B[5];
2945
2989
 
2946
2990
  d0[1] = p1;
2947
2991
  d1[2] = - p1;
@@ -2949,7 +2993,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2949
2993
  d3[2] = p0;
2950
2994
 
2951
2995
  p3 = e[2]*B[3] - e[3]*B[2];
2952
- p2 = -e[2]*B[2] - e[3]*B[3];
2996
+ p2 = -e[2]*B[2] - e[3]*B[3];
2953
2997
 
2954
2998
  d0[2] = p3;
2955
2999
  d1[1] = - p3;
@@ -2957,7 +3001,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2957
3001
  d3[1] = p2;
2958
3002
 
2959
3003
  p1 = e[0]*B[1] - e[1]*B[0];
2960
- p0 = -e[0]*B[0] - e[1]*B[1];
3004
+ p0 = -e[0]*B[0] - e[1]*B[1];
2961
3005
 
2962
3006
  d0[3] = p1;
2963
3007
  d1[0] = - p1;
@@ -3110,7 +3154,6 @@ static float *get_window(vorb *f, int len)
3110
3154
  len <<= 1;
3111
3155
  if (len == f->blocksize_0) return f->window[0];
3112
3156
  if (len == f->blocksize_1) return f->window[1];
3113
- assert(0);
3114
3157
  return NULL;
3115
3158
  }
3116
3159
 
@@ -3133,6 +3176,7 @@ static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *f
3133
3176
  for (q=1; q < g->values; ++q) {
3134
3177
  j = g->sorted_order[q];
3135
3178
  #ifndef STB_VORBIS_NO_DEFER_FLOOR
3179
+ STBV_NOTUSED(step2_flag);
3136
3180
  if (finalY[j] >= 0)
3137
3181
  #else
3138
3182
  if (step2_flag[j])
@@ -3235,6 +3279,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3235
3279
 
3236
3280
  // WINDOWING
3237
3281
 
3282
+ STBV_NOTUSED(left_end);
3238
3283
  n = f->blocksize[m->blockflag];
3239
3284
  map = &f->mapping[m->mapping];
3240
3285
 
@@ -3432,7 +3477,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3432
3477
  // this isn't to spec, but spec would require us to read ahead
3433
3478
  // and decode the size of all current frames--could be done,
3434
3479
  // but presumably it's not a commonly used feature
3435
- f->current_loc = -n2; // start of first frame is positioned for discard
3480
+ f->current_loc = 0u - n2; // start of first frame is positioned for discard (NB this is an intentional unsigned overflow/wrap-around)
3436
3481
  // we might have to discard samples "from" the next frame too,
3437
3482
  // if we're lapping a large block then a small at the start?
3438
3483
  f->discard_samples_deferred = n - right_end;
@@ -3516,6 +3561,7 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
3516
3561
  if (f->previous_length) {
3517
3562
  int i,j, n = f->previous_length;
3518
3563
  float *w = get_window(f, n);
3564
+ if (w == NULL) return 0;
3519
3565
  for (i=0; i < f->channels; ++i) {
3520
3566
  for (j=0; j < n; ++j)
3521
3567
  f->channel_buffers[i][left+j] =
@@ -3563,7 +3609,7 @@ static int vorbis_pump_first_frame(stb_vorbis *f)
3563
3609
  }
3564
3610
 
3565
3611
  #ifndef STB_VORBIS_NO_PUSHDATA_API
3566
- static int is_whole_packet_present(stb_vorbis *f, int end_page)
3612
+ static int is_whole_packet_present(stb_vorbis *f)
3567
3613
  {
3568
3614
  // make sure that we have the packet available before continuing...
3569
3615
  // this requires a full ogg parse, but we know we can fetch from f->stream
@@ -3583,15 +3629,13 @@ static int is_whole_packet_present(stb_vorbis *f, int end_page)
3583
3629
  break;
3584
3630
  }
3585
3631
  // 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
3632
  if (s == f->segment_count)
3589
3633
  s = -1; // set 'crosses page' flag
3590
3634
  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
3591
3635
  first = FALSE;
3592
3636
  }
3593
3637
  for (; s == -1;) {
3594
- uint8 *q;
3638
+ uint8 *q;
3595
3639
  int n;
3596
3640
 
3597
3641
  // check that we have the page header ready
@@ -3617,8 +3661,6 @@ static int is_whole_packet_present(stb_vorbis *f, int end_page)
3617
3661
  if (q[s] < 255)
3618
3662
  break;
3619
3663
  }
3620
- if (end_page)
3621
- if (s < n-1) return error(f, VORBIS_invalid_stream);
3622
3664
  if (s == n)
3623
3665
  s = -1; // set 'crosses page' flag
3624
3666
  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
@@ -3635,6 +3677,7 @@ static int start_decoder(vorb *f)
3635
3677
  int longest_floorlist=0;
3636
3678
 
3637
3679
  // first page, first packet
3680
+ f->first_decode = TRUE;
3638
3681
 
3639
3682
  if (!start_page(f)) return FALSE;
3640
3683
  // validate page flag
@@ -3643,7 +3686,22 @@ static int start_decoder(vorb *f)
3643
3686
  if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page);
3644
3687
  // check for expected packet length
3645
3688
  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);
3689
+ if (f->segments[0] != 30) {
3690
+ // check for the Ogg skeleton fishead identifying header to refine our error
3691
+ if (f->segments[0] == 64 &&
3692
+ getn(f, header, 6) &&
3693
+ header[0] == 'f' &&
3694
+ header[1] == 'i' &&
3695
+ header[2] == 's' &&
3696
+ header[3] == 'h' &&
3697
+ header[4] == 'e' &&
3698
+ header[5] == 'a' &&
3699
+ get8(f) == 'd' &&
3700
+ get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported);
3701
+ else
3702
+ return error(f, VORBIS_invalid_first_page);
3703
+ }
3704
+
3647
3705
  // read packet
3648
3706
  // check packet header
3649
3707
  if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page);
@@ -3677,6 +3735,50 @@ static int start_decoder(vorb *f)
3677
3735
  if (!start_page(f)) return FALSE;
3678
3736
 
3679
3737
  if (!start_packet(f)) return FALSE;
3738
+
3739
+ #ifndef STB_VORBIS_NO_COMMENTS
3740
+ if (!next_segment(f)) return FALSE;
3741
+
3742
+ if (get8_packet(f) != VORBIS_packet_comment) return error(f, VORBIS_invalid_setup);
3743
+ for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3744
+ if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup);
3745
+ //file vendor
3746
+ len = get32_packet(f);
3747
+ f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1));
3748
+ if (f->vendor == NULL) return error(f, VORBIS_outofmem);
3749
+ for(i=0; i < len; ++i) {
3750
+ f->vendor[i] = get8_packet(f);
3751
+ }
3752
+ f->vendor[len] = (char)'\0';
3753
+ //user comments
3754
+ f->comment_list_length = get32_packet(f);
3755
+ f->comment_list = NULL;
3756
+ if (f->comment_list_length > 0)
3757
+ {
3758
+ f->comment_list = (char**) setup_malloc(f, sizeof(char*) * (f->comment_list_length));
3759
+ if (f->comment_list == NULL) return error(f, VORBIS_outofmem);
3760
+ }
3761
+
3762
+ for(i=0; i < f->comment_list_length; ++i) {
3763
+ len = get32_packet(f);
3764
+ f->comment_list[i] = (char*)setup_malloc(f, sizeof(char) * (len+1));
3765
+ if (f->comment_list[i] == NULL) return error(f, VORBIS_outofmem);
3766
+
3767
+ for(j=0; j < len; ++j) {
3768
+ f->comment_list[i][j] = get8_packet(f);
3769
+ }
3770
+ f->comment_list[i][len] = (char)'\0';
3771
+ }
3772
+
3773
+ // framing_flag
3774
+ x = get8_packet(f);
3775
+ if (!(x & 1)) return error(f, VORBIS_invalid_setup);
3776
+
3777
+
3778
+ skip(f, f->bytes_in_seg);
3779
+ f->bytes_in_seg = 0;
3780
+ #endif // STB_VORBIS_NO_COMMENTS
3781
+
3680
3782
  do {
3681
3783
  len = next_segment(f);
3682
3784
  skip(f, len);
@@ -3688,7 +3790,7 @@ static int start_decoder(vorb *f)
3688
3790
 
3689
3791
  #ifndef STB_VORBIS_NO_PUSHDATA_API
3690
3792
  if (IS_PUSH_MODE(f)) {
3691
- if (!is_whole_packet_present(f, TRUE)) {
3793
+ if (!is_whole_packet_present(f)) {
3692
3794
  // convert error in ogg header to write type
3693
3795
  if (f->error == VORBIS_invalid_stream)
3694
3796
  f->error = VORBIS_invalid_setup;
@@ -3706,6 +3808,7 @@ static int start_decoder(vorb *f)
3706
3808
  // codebooks
3707
3809
 
3708
3810
  f->codebook_count = get_bits(f,8) + 1;
3811
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3709
3812
  f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
3710
3813
  if (f->codebooks == NULL) return error(f, VORBIS_outofmem);
3711
3814
  memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
@@ -3728,10 +3831,12 @@ static int start_decoder(vorb *f)
3728
3831
  c->sparse = ordered ? 0 : get_bits(f,1);
3729
3832
 
3730
3833
  if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup);
3834
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3731
3835
 
3732
- if (c->sparse)
3836
+ if (c->sparse) {
3733
3837
  lengths = (uint8 *) setup_temp_malloc(f, c->entries);
3734
- else
3838
+ f->temp_lengths = lengths;
3839
+ } else
3735
3840
  lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
3736
3841
 
3737
3842
  if (!lengths) return error(f, VORBIS_outofmem);
@@ -3742,7 +3847,9 @@ static int start_decoder(vorb *f)
3742
3847
  while (current_entry < c->entries) {
3743
3848
  int limit = c->entries - current_entry;
3744
3849
  int n = get_bits(f, ilog(limit));
3745
- if (current_entry + n > (int) c->entries) { return error(f, VORBIS_invalid_setup); }
3850
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3851
+ if (current_length >= 32) return error(f, VORBIS_invalid_setup);
3852
+ if (current_entry + n > (int) c->entries) return error(f, VORBIS_invalid_setup);
3746
3853
  memset(lengths + current_entry, current_length, n);
3747
3854
  current_entry += n;
3748
3855
  ++current_length;
@@ -3750,11 +3857,11 @@ static int start_decoder(vorb *f)
3750
3857
  } else {
3751
3858
  for (j=0; j < c->entries; ++j) {
3752
3859
  int present = c->sparse ? get_bits(f,1) : 1;
3860
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3753
3861
  if (present) {
3754
3862
  lengths[j] = get_bits(f, 5) + 1;
3755
3863
  ++total;
3756
- if (lengths[j] == 32)
3757
- return error(f, VORBIS_invalid_setup);
3864
+ if (lengths[j] == 32) return error(f, VORBIS_invalid_setup);
3758
3865
  } else {
3759
3866
  lengths[j] = NO_CODE;
3760
3867
  }
@@ -3769,7 +3876,7 @@ static int start_decoder(vorb *f)
3769
3876
  c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
3770
3877
  if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem);
3771
3878
  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!
3879
+ setup_temp_free(f, &f->temp_lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
3773
3880
  lengths = c->codeword_lengths;
3774
3881
  c->sparse = 0;
3775
3882
  }
@@ -3799,8 +3906,10 @@ static int start_decoder(vorb *f)
3799
3906
  c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries);
3800
3907
  if (!c->codeword_lengths) return error(f, VORBIS_outofmem);
3801
3908
  c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
3909
+ f->temp_codewords = c->codewords;
3802
3910
  if (!c->codewords) return error(f, VORBIS_outofmem);
3803
3911
  values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
3912
+ f->temp_values = values;
3804
3913
  if (!values) return error(f, VORBIS_outofmem);
3805
3914
  }
3806
3915
  size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
@@ -3809,7 +3918,6 @@ static int start_decoder(vorb *f)
3809
3918
  }
3810
3919
 
3811
3920
  if (!compute_codewords(c, lengths, c->entries, values)) {
3812
- if (c->sparse) setup_temp_free(f, values, 0);
3813
3921
  return error(f, VORBIS_invalid_setup);
3814
3922
  }
3815
3923
 
@@ -3827,9 +3935,9 @@ static int start_decoder(vorb *f)
3827
3935
  }
3828
3936
 
3829
3937
  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);
3938
+ setup_temp_free(f, &f->temp_values, sizeof(*values)*c->sorted_entries);
3939
+ setup_temp_free(f, &f->temp_codewords, sizeof(*c->codewords)*c->sorted_entries);
3940
+ setup_temp_free(f, &f->temp_lengths, c->entries);
3833
3941
  c->codewords = NULL;
3834
3942
  }
3835
3943
 
@@ -3845,16 +3953,19 @@ static int start_decoder(vorb *f)
3845
3953
  c->value_bits = get_bits(f, 4)+1;
3846
3954
  c->sequence_p = get_bits(f,1);
3847
3955
  if (c->lookup_type == 1) {
3848
- c->lookup_values = lookup1_values(c->entries, c->dimensions);
3956
+ int values = lookup1_values(c->entries, c->dimensions);
3957
+ if (values < 0) return error(f, VORBIS_invalid_setup);
3958
+ c->lookup_values = (uint32) values;
3849
3959
  } else {
3850
3960
  c->lookup_values = c->entries * c->dimensions;
3851
3961
  }
3852
3962
  if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup);
3853
3963
  mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
3964
+ f->temp_mults = mults;
3854
3965
  if (mults == NULL) return error(f, VORBIS_outofmem);
3855
3966
  for (j=0; j < (int) c->lookup_values; ++j) {
3856
3967
  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); }
3968
+ if (f->valid_bits < 0) return error(f, VORBIS_invalid_setup);
3858
3969
  mults[j] = q;
3859
3970
  }
3860
3971
 
@@ -3868,21 +3979,19 @@ static int start_decoder(vorb *f)
3868
3979
  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
3869
3980
  } else
3870
3981
  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); }
3982
+ if (c->multiplicands == NULL) return error(f, VORBIS_outofmem);
3872
3983
  len = sparse ? c->sorted_entries : c->entries;
3873
3984
  for (j=0; j < len; ++j) {
3874
3985
  unsigned int z = sparse ? c->sorted_values[j] : j;
3875
3986
  unsigned int div=1;
3876
3987
  for (k=0; k < c->dimensions; ++k) {
3877
3988
  int off = (z / div) % c->lookup_values;
3878
- float val = mults[off];
3879
- val = mults[off]*c->delta_value + c->minimum_value + last;
3989
+ float val = mults[off]*c->delta_value + c->minimum_value + last;
3880
3990
  c->multiplicands[j*c->dimensions + k] = val;
3881
3991
  if (c->sequence_p)
3882
3992
  last = val;
3883
3993
  if (k+1 < c->dimensions) {
3884
3994
  if (div > UINT_MAX / (unsigned int) c->lookup_values) {
3885
- setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
3886
3995
  return error(f, VORBIS_invalid_setup);
3887
3996
  }
3888
3997
  div *= c->lookup_values;
@@ -3897,7 +4006,7 @@ static int start_decoder(vorb *f)
3897
4006
  float last=0;
3898
4007
  CHECK(f);
3899
4008
  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); }
4009
+ if (c->multiplicands == NULL) return error(f, VORBIS_outofmem);
3901
4010
  for (j=0; j < (int) c->lookup_values; ++j) {
3902
4011
  float val = mults[j] * c->delta_value + c->minimum_value + last;
3903
4012
  c->multiplicands[j] = val;
@@ -3908,7 +4017,7 @@ static int start_decoder(vorb *f)
3908
4017
  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3909
4018
  skip:;
3910
4019
  #endif
3911
- setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
4020
+ setup_temp_free(f, &f->temp_mults, sizeof(mults[0])*c->lookup_values);
3912
4021
 
3913
4022
  CHECK(f);
3914
4023
  }
@@ -3925,6 +4034,7 @@ static int start_decoder(vorb *f)
3925
4034
 
3926
4035
  // Floors
3927
4036
  f->floor_count = get_bits(f, 6)+1;
4037
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3928
4038
  f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
3929
4039
  if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
3930
4040
  for (i=0; i < f->floor_count; ++i) {
@@ -3944,7 +4054,7 @@ static int start_decoder(vorb *f)
3944
4054
  } else {
3945
4055
  stbv__floor_ordering p[31*8+2];
3946
4056
  Floor1 *g = &f->floor_config[i].floor1;
3947
- int max_class = -1;
4057
+ int max_class = -1;
3948
4058
  g->partitions = get_bits(f, 5);
3949
4059
  for (j=0; j < g->partitions; ++j) {
3950
4060
  g->partition_class_list[j] = get_bits(f, 4);
@@ -3954,12 +4064,13 @@ static int start_decoder(vorb *f)
3954
4064
  for (j=0; j <= max_class; ++j) {
3955
4065
  g->class_dimensions[j] = get_bits(f, 3)+1;
3956
4066
  g->class_subclasses[j] = get_bits(f, 2);
4067
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3957
4068
  if (g->class_subclasses[j]) {
3958
4069
  g->class_masterbooks[j] = get_bits(f, 8);
3959
4070
  if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
3960
4071
  }
3961
4072
  for (k=0; k < 1 << g->class_subclasses[j]; ++k) {
3962
- g->subclass_books[j][k] = get_bits(f,8)-1;
4073
+ g->subclass_books[j][k] = (int16)get_bits(f,8)-1;
3963
4074
  if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
3964
4075
  }
3965
4076
  }
@@ -3981,11 +4092,14 @@ static int start_decoder(vorb *f)
3981
4092
  p[j].id = j;
3982
4093
  }
3983
4094
  qsort(p, g->values, sizeof(p[0]), point_compare);
4095
+ for (j=0; j < g->values-1; ++j)
4096
+ if (p[j].x == p[j+1].x)
4097
+ return error(f, VORBIS_invalid_setup);
3984
4098
  for (j=0; j < g->values; ++j)
3985
4099
  g->sorted_order[j] = (uint8) p[j].id;
3986
4100
  // precompute the neighbors
3987
4101
  for (j=2; j < g->values; ++j) {
3988
- int low,hi;
4102
+ int low = 0,hi = 0;
3989
4103
  neighbors(g->Xlist, j, &low,&hi);
3990
4104
  g->neighbors[j][0] = low;
3991
4105
  g->neighbors[j][1] = hi;
@@ -3998,6 +4112,7 @@ static int start_decoder(vorb *f)
3998
4112
 
3999
4113
  // Residue
4000
4114
  f->residue_count = get_bits(f, 6)+1;
4115
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4001
4116
  f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
4002
4117
  if (f->residue_config == NULL) return error(f, VORBIS_outofmem);
4003
4118
  memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
@@ -4012,6 +4127,7 @@ static int start_decoder(vorb *f)
4012
4127
  r->part_size = get_bits(f,24)+1;
4013
4128
  r->classifications = get_bits(f,6)+1;
4014
4129
  r->classbook = get_bits(f,8);
4130
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4015
4131
  if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4016
4132
  for (j=0; j < r->classifications; ++j) {
4017
4133
  uint8 high_bits=0;
@@ -4020,12 +4136,14 @@ static int start_decoder(vorb *f)
4020
4136
  high_bits = get_bits(f,5);
4021
4137
  residue_cascade[j] = high_bits*8 + low_bits;
4022
4138
  }
4139
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4023
4140
  r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
4024
4141
  if (r->residue_books == NULL) return error(f, VORBIS_outofmem);
4025
4142
  for (j=0; j < r->classifications; ++j) {
4026
4143
  for (k=0; k < 8; ++k) {
4027
4144
  if (residue_cascade[j] & (1 << k)) {
4028
4145
  r->residue_books[j][k] = get_bits(f, 8);
4146
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4029
4147
  if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4030
4148
  } else {
4031
4149
  r->residue_books[j][k] = -1;
@@ -4050,11 +4168,12 @@ static int start_decoder(vorb *f)
4050
4168
  }
4051
4169
 
4052
4170
  f->mapping_count = get_bits(f,6)+1;
4171
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4053
4172
  f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
4054
4173
  if (f->mapping == NULL) return error(f, VORBIS_outofmem);
4055
4174
  memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
4056
4175
  for (i=0; i < f->mapping_count; ++i) {
4057
- Mapping *m = f->mapping + i;
4176
+ Mapping *m = f->mapping + i;
4058
4177
  int mapping_type = get_bits(f,16);
4059
4178
  if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
4060
4179
  m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
@@ -4067,9 +4186,11 @@ static int start_decoder(vorb *f)
4067
4186
  max_submaps = m->submaps;
4068
4187
  if (get_bits(f,1)) {
4069
4188
  m->coupling_steps = get_bits(f,8)+1;
4189
+ if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup);
4070
4190
  for (k=0; k < m->coupling_steps; ++k) {
4071
4191
  m->chan[k].magnitude = get_bits(f, ilog(f->channels-1));
4072
4192
  m->chan[k].angle = get_bits(f, ilog(f->channels-1));
4193
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4073
4194
  if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup);
4074
4195
  if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup);
4075
4196
  if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup);
@@ -4106,6 +4227,7 @@ static int start_decoder(vorb *f)
4106
4227
  m->windowtype = get_bits(f,16);
4107
4228
  m->transformtype = get_bits(f,16);
4108
4229
  m->mapping = get_bits(f,8);
4230
+ if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4109
4231
  if (m->windowtype != 0) return error(f, VORBIS_invalid_setup);
4110
4232
  if (m->transformtype != 0) return error(f, VORBIS_invalid_setup);
4111
4233
  if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup);
@@ -4169,7 +4291,6 @@ static int start_decoder(vorb *f)
4169
4291
  f->temp_memory_required = imdct_mem;
4170
4292
  }
4171
4293
 
4172
- f->first_decode = TRUE;
4173
4294
 
4174
4295
  if (f->alloc.alloc_buffer) {
4175
4296
  assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
@@ -4178,7 +4299,17 @@ static int start_decoder(vorb *f)
4178
4299
  return error(f, VORBIS_outofmem);
4179
4300
  }
4180
4301
 
4181
- f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
4302
+ // @TODO: stb_vorbis_seek_start expects first_audio_page_offset to point to a page
4303
+ // without PAGEFLAG_continued_packet, so this either points to the first page, or
4304
+ // the page after the end of the headers. It might be cleaner to point to a page
4305
+ // in the middle of the headers, when that's the page where the first audio packet
4306
+ // starts, but we'd have to also correctly skip the end of any continued packet in
4307
+ // stb_vorbis_seek_start.
4308
+ if (f->next_seg == -1) {
4309
+ f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
4310
+ } else {
4311
+ f->first_audio_page_offset = 0;
4312
+ }
4182
4313
 
4183
4314
  return TRUE;
4184
4315
  }
@@ -4186,6 +4317,15 @@ static int start_decoder(vorb *f)
4186
4317
  static void vorbis_deinit(stb_vorbis *p)
4187
4318
  {
4188
4319
  int i,j;
4320
+
4321
+ #ifndef STB_VORBIS_NO_COMMENTS
4322
+ setup_free(p, p->vendor);
4323
+ for (i=0; i < p->comment_list_length; ++i) {
4324
+ setup_free(p, p->comment_list[i]);
4325
+ }
4326
+ setup_free(p, p->comment_list);
4327
+ #endif
4328
+
4189
4329
  if (p->residue_config) {
4190
4330
  for (i=0; i < p->residue_count; ++i) {
4191
4331
  Residue *r = p->residue_config+i;
@@ -4204,7 +4344,8 @@ static void vorbis_deinit(stb_vorbis *p)
4204
4344
  Codebook *c = p->codebooks + i;
4205
4345
  setup_free(p, c->codeword_lengths);
4206
4346
  setup_free(p, c->multiplicands);
4207
- setup_free(p, c->codewords);
4347
+ if (c->codewords != p->temp_codewords) // Might be the temporary buffer-allocated array.
4348
+ setup_free(p, c->codewords);
4208
4349
  setup_free(p, c->sorted_codewords);
4209
4350
  // c->sorted_values[-1] is the first entry in the array
4210
4351
  setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL);
@@ -4234,6 +4375,12 @@ static void vorbis_deinit(stb_vorbis *p)
4234
4375
  setup_free(p, p->window[i]);
4235
4376
  setup_free(p, p->bit_reverse[i]);
4236
4377
  }
4378
+ if (!p->alloc.alloc_buffer) {
4379
+ setup_temp_free(p, &p->temp_lengths, 0);
4380
+ setup_temp_free(p, &p->temp_codewords, 0);
4381
+ setup_temp_free(p, &p->temp_values, 0);
4382
+ setup_temp_free(p, &p->temp_mults, 0);
4383
+ }
4237
4384
  #ifdef __SDL_SOUND_INTERNAL__
4238
4385
  if (p->close_on_free) SDL_RWclose(p->rwops);
4239
4386
  #endif
@@ -4254,7 +4401,7 @@ static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
4254
4401
  memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
4255
4402
  if (z) {
4256
4403
  p->alloc = *z;
4257
- p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes+3) & ~3;
4404
+ p->alloc.alloc_buffer_length_in_bytes &= ~7;
4258
4405
  p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
4259
4406
  }
4260
4407
  p->eof = 0;
@@ -4292,6 +4439,17 @@ stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
4292
4439
  return d;
4293
4440
  }
4294
4441
 
4442
+ #ifndef STB_VORBIS_NO_COMMENTS
4443
+ stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f)
4444
+ {
4445
+ stb_vorbis_comment d;
4446
+ d.vendor = f->vendor;
4447
+ d.comment_list_length = f->comment_list_length;
4448
+ d.comment_list = f->comment_list;
4449
+ return d;
4450
+ }
4451
+ #endif
4452
+
4295
4453
  int stb_vorbis_get_error(stb_vorbis *f)
4296
4454
  {
4297
4455
  int e = f->error;
@@ -4433,7 +4591,7 @@ int stb_vorbis_decode_frame_pushdata(
4433
4591
  f->error = VORBIS__no_error;
4434
4592
 
4435
4593
  // check that we have the entire packet in memory
4436
- if (!is_whole_packet_present(f, FALSE)) {
4594
+ if (!is_whole_packet_present(f)) {
4437
4595
  *samples = 0;
4438
4596
  return 0;
4439
4597
  }
@@ -4495,6 +4653,7 @@ stb_vorbis *stb_vorbis_open_pushdata(
4495
4653
  *error = VORBIS_need_more_data;
4496
4654
  else
4497
4655
  *error = p.error;
4656
+ vorbis_deinit(&p);
4498
4657
  return NULL;
4499
4658
  }
4500
4659
  f = vorbis_alloc(&p);
@@ -4515,9 +4674,10 @@ unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
4515
4674
  #ifndef STB_VORBIS_NO_PUSHDATA_API
4516
4675
  if (f->push_mode) return 0;
4517
4676
  #endif
4518
- if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
4519
4677
  #ifdef __SDL_SOUND_INTERNAL__
4520
4678
  return (unsigned int) (SDL_RWtell(f->rwops) - f->rwops_start);
4679
+ #else
4680
+ if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
4521
4681
  #endif
4522
4682
  #ifndef STB_VORBIS_NO_STDIO
4523
4683
  return (unsigned int) (ftell(f->f) - f->f_start);
@@ -4555,7 +4715,7 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last)
4555
4715
  header[i] = get8(f);
4556
4716
  if (f->eof) return 0;
4557
4717
  if (header[4] != 0) goto invalid;
4558
- goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24);
4718
+ goal = header[22] + (header[23] << 8) + (header[24]<<16) + ((uint32)header[25]<<24);
4559
4719
  for (i=22; i < 26; ++i)
4560
4720
  header[i] = 0;
4561
4721
  crc = 0;
@@ -4620,10 +4780,12 @@ static int get_seek_page_info(stb_vorbis *f, ProbedPage *z)
4620
4780
  z->page_start = stb_vorbis_get_file_offset(f);
4621
4781
 
4622
4782
  // parse the header
4623
- getn(f, header, 27);
4783
+ if (!getn(f, header, 27))
4784
+ return 0;
4624
4785
  if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
4625
4786
  return 0;
4626
- getn(f, lacing, header[26]);
4787
+ if (!getn(f, lacing, header[26]))
4788
+ return 0;
4627
4789
 
4628
4790
  // determine the length of the payload
4629
4791
  len = 0;
@@ -4641,11 +4803,12 @@ static int get_seek_page_info(stb_vorbis *f, ProbedPage *z)
4641
4803
  return 1;
4642
4804
  }
4643
4805
 
4644
- // rarely used function to seek back to the preceeding page while finding the
4806
+ // rarely used function to seek back to the preceding page while finding the
4645
4807
  // start of a packet
4646
4808
  static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset)
4647
4809
  {
4648
- unsigned int previous_safe, end;
4810
+ unsigned int previous_safe;
4811
+ uint32 end;
4649
4812
 
4650
4813
  // now we want to seek back 64K from the limit
4651
4814
  if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset)
@@ -4672,8 +4835,8 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4672
4835
  {
4673
4836
  ProbedPage left, right, mid;
4674
4837
  int i, start_seg_with_known_loc, end_pos, page_start;
4675
- uint32 delta, stream_length, padding;
4676
- double offset, bytes_per_sample;
4838
+ uint32 delta, stream_length, padding, last_sample_limit;
4839
+ double offset = 0.0, bytes_per_sample = 0.0;
4677
4840
  int probe = 0;
4678
4841
 
4679
4842
  // find the last page and validate the target sample
@@ -4686,9 +4849,9 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4686
4849
  // indicates should be the granule position (give or take one)).
4687
4850
  padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
4688
4851
  if (sample_number < padding)
4689
- sample_number = 0;
4852
+ last_sample_limit = 0;
4690
4853
  else
4691
- sample_number -= padding;
4854
+ last_sample_limit = sample_number - padding;
4692
4855
 
4693
4856
  left = f->p_first;
4694
4857
  while (left.last_decoded_sample == ~0U) {
@@ -4701,9 +4864,12 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4701
4864
  assert(right.last_decoded_sample != ~0U);
4702
4865
 
4703
4866
  // starting from the start is handled differently
4704
- if (sample_number <= left.last_decoded_sample) {
4705
- if (stb_vorbis_seek_start(f))
4867
+ if (last_sample_limit <= left.last_decoded_sample) {
4868
+ if (stb_vorbis_seek_start(f)) {
4869
+ if (f->current_loc > sample_number)
4870
+ return error(f, VORBIS_seek_failed);
4706
4871
  return 1;
4872
+ }
4707
4873
  return 0;
4708
4874
  }
4709
4875
 
@@ -4720,10 +4886,10 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4720
4886
  // first probe (interpolate)
4721
4887
  double data_bytes = right.page_end - left.page_start;
4722
4888
  bytes_per_sample = data_bytes / right.last_decoded_sample;
4723
- offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
4889
+ offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample);
4724
4890
  } else {
4725
4891
  // second probe (try to bound the other side)
4726
- double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
4892
+ double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample;
4727
4893
  if (error >= 0 && error < 8000) error = 8000;
4728
4894
  if (error < 0 && error > -8000) error = -8000;
4729
4895
  offset += error * 2;
@@ -4754,14 +4920,16 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4754
4920
  }
4755
4921
 
4756
4922
  // 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;
4923
+ // and we're close enough (if it wasn't an interpolation probe).
4924
+ if (mid.page_start == right.page_start) {
4925
+ if (probe >= 2 || delta <= 65536)
4926
+ break;
4927
+ } else {
4928
+ if (last_sample_limit < mid.last_decoded_sample)
4929
+ right = mid;
4930
+ else
4931
+ left = mid;
4932
+ }
4765
4933
 
4766
4934
  ++probe;
4767
4935
  }
@@ -4877,8 +5045,8 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
4877
5045
  flush_packet(f);
4878
5046
  }
4879
5047
  }
4880
- // the next frame will start with the sample
4881
- assert(f->current_loc == sample_number);
5048
+ // the next frame should start with the sample
5049
+ if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);
4882
5050
  return 1;
4883
5051
  }
4884
5052
 
@@ -4912,11 +5080,11 @@ int stb_vorbis_seek_start(stb_vorbis *f)
4912
5080
  unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
4913
5081
  {
4914
5082
  unsigned int restore_offset, previous_safe;
4915
- unsigned int end, last_page_loc;
5083
+ unsigned int last_page_loc;
4916
5084
 
4917
5085
  if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
4918
5086
  if (!f->total_samples) {
4919
- unsigned int last;
5087
+ uint32 end,last;
4920
5088
  uint32 lo,hi;
4921
5089
  char header[6];
4922
5090
 
@@ -4954,7 +5122,7 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
4954
5122
  // set. whoops!
4955
5123
  break;
4956
5124
  }
4957
- previous_safe = last_page_loc+1;
5125
+ //previous_safe = last_page_loc+1; // NOTE: not used after this point, but note for debugging
4958
5126
  last_page_loc = stb_vorbis_get_file_offset(f);
4959
5127
  }
4960
5128
 
@@ -5048,8 +5216,14 @@ stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, con
5048
5216
 
5049
5217
  stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc)
5050
5218
  {
5051
- FILE *f = fopen(filename, "rb");
5052
- if (f)
5219
+ FILE *f;
5220
+ #if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)
5221
+ if (0 != fopen_s(&f, filename, "rb"))
5222
+ f = NULL;
5223
+ #else
5224
+ f = fopen(filename, "rb");
5225
+ #endif
5226
+ if (f)
5053
5227
  return stb_vorbis_open_file(f, TRUE, error, alloc);
5054
5228
  if (error) *error = VORBIS_file_open_failure;
5055
5229
  return NULL;
@@ -5089,7 +5263,10 @@ stb_vorbis * stb_vorbis_open_rwops(SDL_RWops *rwops, int close_on_free, int *err
5089
5263
  stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc)
5090
5264
  {
5091
5265
  stb_vorbis *f, p;
5092
- if (data == NULL) return NULL;
5266
+ if (!data) {
5267
+ if (error) *error = VORBIS_unexpected_eof;
5268
+ return NULL;
5269
+ }
5093
5270
  vorbis_init(&p, alloc);
5094
5271
  p.stream = (uint8 *) data;
5095
5272
  p.stream_end = (uint8 *) data + len;
@@ -5134,15 +5311,17 @@ static int8 channel_position[7][6] =
5134
5311
  #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
5135
5312
  typedef union {
5136
5313
  float f;
5137
- int i;
5314
+ // hack: changed this to unsigned to suppress an UBSan error.
5315
+ // see upstream: https://github.com/nothings/stb/issues/1168.
5316
+ unsigned int i;
5138
5317
  } float_conv;
5139
5318
  typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
5140
5319
  #define FASTDEF(x) float_conv x
5141
5320
  // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
5142
5321
  #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
5143
5322
  #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()
5323
+ #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (int)(temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
5324
+ #define check_endianness()
5146
5325
  #else
5147
5326
  #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
5148
5327
  #define check_endianness()
@@ -5156,7 +5335,7 @@ static void copy_samples(short *dest, float *src, int len)
5156
5335
  for (i=0; i < len; ++i) {
5157
5336
  FASTDEF(temp);
5158
5337
  int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
5159
- if ((unsigned int) (v + 32768) > 65535)
5338
+ if ((unsigned int)v + 32768 > 65535)
5160
5339
  v = v < 0 ? -32768 : 32767;
5161
5340
  dest[i] = v;
5162
5341
  }
@@ -5164,11 +5343,11 @@ static void copy_samples(short *dest, float *src, int len)
5164
5343
 
5165
5344
  static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
5166
5345
  {
5167
- #define BUFFER_SIZE 32
5168
- float buffer[BUFFER_SIZE];
5169
- int i,j,o,n = BUFFER_SIZE;
5346
+ #define STB_BUFFER_SIZE 32
5347
+ float buffer[STB_BUFFER_SIZE];
5348
+ int i,j,o,n = STB_BUFFER_SIZE;
5170
5349
  check_endianness();
5171
- for (o = 0; o < len; o += BUFFER_SIZE) {
5350
+ for (o = 0; o < len; o += STB_BUFFER_SIZE) {
5172
5351
  memset(buffer, 0, sizeof(buffer));
5173
5352
  if (o + n > len) n = len - o;
5174
5353
  for (j=0; j < num_c; ++j) {
@@ -5180,21 +5359,22 @@ static void compute_samples(int mask, short *output, int num_c, float **data, in
5180
5359
  for (i=0; i < n; ++i) {
5181
5360
  FASTDEF(temp);
5182
5361
  int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5183
- if ((unsigned int) (v + 32768) > 65535)
5362
+ if ((unsigned int)v + 32768 > 65535)
5184
5363
  v = v < 0 ? -32768 : 32767;
5185
5364
  output[o+i] = v;
5186
5365
  }
5187
5366
  }
5367
+ #undef STB_BUFFER_SIZE
5188
5368
  }
5189
5369
 
5190
5370
  static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len)
5191
5371
  {
5192
- #define BUFFER_SIZE 32
5193
- float buffer[BUFFER_SIZE];
5194
- int i,j,o,n = BUFFER_SIZE >> 1;
5372
+ #define STB_BUFFER_SIZE 32
5373
+ float buffer[STB_BUFFER_SIZE];
5374
+ int i,j,o,n = STB_BUFFER_SIZE >> 1;
5195
5375
  // o is the offset in the source data
5196
5376
  check_endianness();
5197
- for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
5377
+ for (o = 0; o < len; o += STB_BUFFER_SIZE >> 1) {
5198
5378
  // o2 is the offset in the output data
5199
5379
  int o2 = o << 1;
5200
5380
  memset(buffer, 0, sizeof(buffer));
@@ -5219,11 +5399,12 @@ static void compute_stereo_samples(short *output, int num_c, float **data, int d
5219
5399
  for (i=0; i < (n<<1); ++i) {
5220
5400
  FASTDEF(temp);
5221
5401
  int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5222
- if ((unsigned int) (v + 32768) > 65535)
5402
+ if ((unsigned int)v + 32768 > 65535)
5223
5403
  v = v < 0 ? -32768 : 32767;
5224
5404
  output[o2+i] = v;
5225
5405
  }
5226
5406
  }
5407
+ #undef STB_BUFFER_SIZE
5227
5408
  }
5228
5409
 
5229
5410
  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 +5425,7 @@ static void convert_samples_short(int buf_c, short **buffer, int b_offset, int d
5244
5425
 
5245
5426
  int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples)
5246
5427
  {
5247
- float **output;
5428
+ float **output = NULL;
5248
5429
  int len = stb_vorbis_get_frame_float(f, NULL, &output);
5249
5430
  if (len > num_samples) len = num_samples;
5250
5431
  if (len)
@@ -5268,7 +5449,7 @@ static void convert_channels_short_interleaved(int buf_c, short *buffer, int dat
5268
5449
  FASTDEF(temp);
5269
5450
  float f = data[i][d_offset+j];
5270
5451
  int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15);
5271
- if ((unsigned int) (v + 32768) > 65535)
5452
+ if ((unsigned int)v + 32768 > 65535)
5272
5453
  v = v < 0 ? -32768 : 32767;
5273
5454
  *buffer++ = v;
5274
5455
  }
@@ -5296,8 +5477,6 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short
5296
5477
  float **outputs;
5297
5478
  int len = num_shorts / channels;
5298
5479
  int n=0;
5299
- int z = f->channels;
5300
- if (z > channels) z = channels;
5301
5480
  while (n < len) {
5302
5481
  int k = f->channel_buffer_end - f->channel_buffer_start;
5303
5482
  if (n+k >= len) k = len - n;
@@ -5316,8 +5495,6 @@ int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, in
5316
5495
  {
5317
5496
  float **outputs;
5318
5497
  int n=0;
5319
- int z = f->channels;
5320
- if (z > channels) z = channels;
5321
5498
  while (n < len) {
5322
5499
  int k = f->channel_buffer_end - f->channel_buffer_start;
5323
5500
  if (n+k >= len) k = len - n;
@@ -5467,14 +5644,20 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in
5467
5644
  #endif // STB_VORBIS_NO_PULLDATA_API
5468
5645
 
5469
5646
  /* Version history
5647
+ 1.17 - 2019-07-08 - fix CVE-2019-13217, -13218, -13219, -13220, -13221, -13222, -13223
5648
+ found with Mayhem by ForAllSecure
5649
+ 1.16 - 2019-03-04 - fix warnings
5650
+ 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found
5651
+ 1.14 - 2018-02-11 - delete bogus dealloca usage
5652
+ 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)
5470
5653
  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
5654
+ 1.11 - 2017-07-23 - fix MinGW compilation
5472
5655
  1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory
5473
5656
  1.09 - 2016-04-04 - back out 'avoid discarding last frame' fix from previous version
5474
5657
  1.08 - 2016-04-02 - fixed multiple warnings; fix setup memory leaks;
5475
5658
  avoid discarding last frame of audio data
5476
5659
  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
5660
+ some more crash fixes when out of memory or with corrupt files
5478
5661
  1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
5479
5662
  some crash fixes when out of memory or with corrupt files
5480
5663
  1.05 - 2015-04-19 - don't define __forceinline if it's redundant
@@ -5530,38 +5713,38 @@ This software is available under 2 licenses -- choose whichever you prefer.
5530
5713
  ------------------------------------------------------------------------------
5531
5714
  ALTERNATIVE A - MIT License
5532
5715
  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
5716
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5717
+ this software and associated documentation files (the "Software"), to deal in
5718
+ the Software without restriction, including without limitation the rights to
5719
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
5720
+ of the Software, and to permit persons to whom the Software is furnished to do
5538
5721
  so, subject to the following conditions:
5539
- The above copyright notice and this permission notice shall be included in all
5722
+ The above copyright notice and this permission notice shall be included in all
5540
5723
  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
5724
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5725
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5726
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5727
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5728
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5729
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5547
5730
  SOFTWARE.
5548
5731
  ------------------------------------------------------------------------------
5549
5732
  ALTERNATIVE B - Public Domain (www.unlicense.org)
5550
5733
  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,
5734
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
5735
+ software, either in source code form or as a compiled binary, for any purpose,
5553
5736
  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
5737
+ In jurisdictions that recognize copyright laws, the author or authors of this
5738
+ software dedicate any and all copyright interest in the software to the public
5739
+ domain. We make this dedication for the benefit of the public at large and to
5740
+ the detriment of our heirs and successors. We intend this dedication to be an
5741
+ overt act of relinquishment in perpetuity of all present and future rights to
5559
5742
  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
5743
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5744
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5745
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5746
+ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
5747
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
5565
5748
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5566
5749
  ------------------------------------------------------------------------------
5567
5750
  */