gosu 1.4.6 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (320) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +2 -1
  3. data/dependencies/SDL/include/SDL3/SDL.h +91 -0
  4. data/dependencies/SDL/include/SDL3/SDL_assert.h +710 -0
  5. data/dependencies/SDL/include/SDL3/SDL_asyncio.h +550 -0
  6. data/dependencies/SDL/include/SDL3/SDL_atomic.h +692 -0
  7. data/dependencies/SDL/include/SDL3/SDL_audio.h +2382 -0
  8. data/dependencies/SDL/include/SDL3/SDL_begin_code.h +553 -0
  9. data/dependencies/SDL/include/{SDL_bits.h → SDL3/SDL_bits.h} +47 -27
  10. data/dependencies/SDL/include/{SDL_blendmode.h → SDL3/SDL_blendmode.h} +59 -55
  11. data/dependencies/SDL/include/SDL3/SDL_camera.h +535 -0
  12. data/dependencies/SDL/include/SDL3/SDL_clipboard.h +331 -0
  13. data/dependencies/SDL/include/{close_code.h → SDL3/SDL_close_code.h} +9 -8
  14. data/dependencies/SDL/include/{SDL_types.h → SDL3/SDL_copying.h} +2 -9
  15. data/dependencies/SDL/include/SDL3/SDL_cpuinfo.h +374 -0
  16. data/dependencies/SDL/include/SDL3/SDL_dialog.h +343 -0
  17. data/dependencies/SDL/include/SDL3/SDL_dlopennote.h +234 -0
  18. data/dependencies/SDL/include/{SDL_egl.h → SDL3/SDL_egl.h} +11 -8
  19. data/dependencies/SDL/include/SDL3/SDL_endian.h +645 -0
  20. data/dependencies/SDL/include/SDL3/SDL_error.h +226 -0
  21. data/dependencies/SDL/include/SDL3/SDL_events.h +1651 -0
  22. data/dependencies/SDL/include/SDL3/SDL_filesystem.h +537 -0
  23. data/dependencies/SDL/include/SDL3/SDL_gamepad.h +1657 -0
  24. data/dependencies/SDL/include/SDL3/SDL_gpu.h +4637 -0
  25. data/dependencies/SDL/include/SDL3/SDL_guid.h +106 -0
  26. data/dependencies/SDL/include/SDL3/SDL_haptic.h +1461 -0
  27. data/dependencies/SDL/include/SDL3/SDL_hidapi.h +571 -0
  28. data/dependencies/SDL/include/SDL3/SDL_hints.h +5041 -0
  29. data/dependencies/SDL/include/SDL3/SDL_init.h +508 -0
  30. data/dependencies/SDL/include/SDL3/SDL_intrin.h +410 -0
  31. data/dependencies/SDL/include/SDL3/SDL_iostream.h +1379 -0
  32. data/dependencies/SDL/include/SDL3/SDL_joystick.h +1385 -0
  33. data/dependencies/SDL/include/SDL3/SDL_keyboard.h +615 -0
  34. data/dependencies/SDL/include/SDL3/SDL_keycode.h +348 -0
  35. data/dependencies/SDL/include/SDL3/SDL_loadso.h +145 -0
  36. data/dependencies/SDL/include/{SDL_locale.h → SDL3/SDL_locale.h} +40 -24
  37. data/dependencies/SDL/include/SDL3/SDL_log.h +541 -0
  38. data/dependencies/SDL/include/SDL3/SDL_main.h +704 -0
  39. data/dependencies/SDL/include/SDL3/SDL_main_impl.h +149 -0
  40. data/dependencies/SDL/include/{SDL_messagebox.h → SDL3/SDL_messagebox.h} +85 -48
  41. data/dependencies/SDL/include/{SDL_metal.h → SDL3/SDL_metal.h} +29 -29
  42. data/dependencies/SDL/include/{SDL_misc.h → SDL3/SDL_misc.h} +13 -12
  43. data/dependencies/SDL/include/SDL3/SDL_mouse.h +813 -0
  44. data/dependencies/SDL/include/SDL3/SDL_mutex.h +1117 -0
  45. data/dependencies/SDL/include/SDL3/SDL_oldnames.h +1339 -0
  46. data/dependencies/SDL/include/{SDL_opengl.h → SDL3/SDL_opengl.h} +1247 -278
  47. data/dependencies/SDL/include/{SDL_opengl_glext.h → SDL3/SDL_opengl_glext.h} +5 -1
  48. data/dependencies/SDL/include/{SDL_opengles.h → SDL3/SDL_opengles.h} +6 -7
  49. data/dependencies/SDL/include/{SDL_opengles2.h → SDL3/SDL_opengles2.h} +10 -11
  50. data/dependencies/SDL/include/SDL3/SDL_pen.h +198 -0
  51. data/dependencies/SDL/include/SDL3/SDL_pixels.h +1441 -0
  52. data/dependencies/SDL/include/{SDL_test_log.h → SDL3/SDL_platform.h} +25 -26
  53. data/dependencies/SDL/include/SDL3/SDL_platform_defines.h +497 -0
  54. data/dependencies/SDL/include/{SDL_power.h → SDL3/SDL_power.h} +40 -20
  55. data/dependencies/SDL/include/SDL3/SDL_process.h +441 -0
  56. data/dependencies/SDL/include/SDL3/SDL_properties.h +572 -0
  57. data/dependencies/SDL/include/SDL3/SDL_rect.h +528 -0
  58. data/dependencies/SDL/include/SDL3/SDL_render.h +3034 -0
  59. data/dependencies/SDL/include/SDL3/SDL_revision.h +56 -0
  60. data/dependencies/SDL/include/{SDL_scancode.h → SDL3/SDL_scancode.h} +60 -68
  61. data/dependencies/SDL/include/SDL3/SDL_sensor.h +321 -0
  62. data/dependencies/SDL/include/SDL3/SDL_stdinc.h +6186 -0
  63. data/dependencies/SDL/include/SDL3/SDL_storage.h +686 -0
  64. data/dependencies/SDL/include/SDL3/SDL_surface.h +1769 -0
  65. data/dependencies/SDL/include/SDL3/SDL_system.h +840 -0
  66. data/dependencies/SDL/include/{SDL_test.h → SDL3/SDL_test.h} +16 -22
  67. data/dependencies/SDL/include/{SDL_test_assert.h → SDL3/SDL_test_assert.h} +27 -34
  68. data/dependencies/SDL/include/{SDL_test_common.h → SDL3/SDL_test_common.h} +109 -52
  69. data/dependencies/SDL/include/{SDL_test_compare.h → SDL3/SDL_test_compare.h} +22 -14
  70. data/dependencies/SDL/include/{SDL_test_crc32.h → SDL3/SDL_test_crc32.h} +27 -30
  71. data/dependencies/SDL/include/{SDL_test_font.h → SDL3/SDL_test_font.h} +50 -49
  72. data/dependencies/SDL/include/{SDL_test_fuzzer.h → SDL3/SDL_test_fuzzer.h} +64 -79
  73. data/dependencies/SDL/include/{SDL_test_harness.h → SDL3/SDL_test_harness.h} +47 -30
  74. data/dependencies/SDL/include/SDL3/SDL_test_log.h +83 -0
  75. data/dependencies/SDL/include/{SDL_test_md5.h → SDL3/SDL_test_md5.h} +21 -28
  76. data/dependencies/SDL/include/{SDL_test_memory.h → SDL3/SDL_test_memory.h} +19 -16
  77. data/dependencies/SDL/include/SDL3/SDL_thread.h +602 -0
  78. data/dependencies/SDL/include/SDL3/SDL_time.h +249 -0
  79. data/dependencies/SDL/include/SDL3/SDL_timer.h +454 -0
  80. data/dependencies/SDL/include/SDL3/SDL_touch.h +184 -0
  81. data/dependencies/SDL/include/SDL3/SDL_tray.h +544 -0
  82. data/dependencies/SDL/include/SDL3/SDL_version.h +198 -0
  83. data/dependencies/SDL/include/SDL3/SDL_video.h +3497 -0
  84. data/dependencies/SDL/include/SDL3/SDL_vulkan.h +287 -0
  85. data/dependencies/SDL/lib/x64/libSDL3.dll.a +0 -0
  86. data/dependencies/SDL/lib/x86/libSDL3.dll.a +0 -0
  87. data/dependencies/SDL_sound/SDL3_sound/SDL_sound.h +755 -0
  88. data/dependencies/SDL_sound/SDL_sound.c +136 -215
  89. data/dependencies/SDL_sound/SDL_sound_aiff.c +55 -54
  90. data/dependencies/SDL_sound/SDL_sound_au.c +32 -32
  91. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +24 -41
  92. data/dependencies/SDL_sound/SDL_sound_flac.c +32 -12
  93. data/dependencies/SDL_sound/SDL_sound_internal.h +33 -48
  94. data/dependencies/SDL_sound/SDL_sound_modplug.c +21 -29
  95. data/dependencies/SDL_sound/SDL_sound_mp3.c +35 -13
  96. data/dependencies/SDL_sound/SDL_sound_raw.c +14 -15
  97. data/dependencies/SDL_sound/SDL_sound_shn.c +82 -82
  98. data/dependencies/SDL_sound/SDL_sound_voc.c +30 -30
  99. data/dependencies/SDL_sound/SDL_sound_vorbis.c +34 -25
  100. data/dependencies/SDL_sound/SDL_sound_wav.c +86 -82
  101. data/dependencies/SDL_sound/dr_flac.h +1890 -1301
  102. data/dependencies/SDL_sound/dr_mp3.h +1098 -660
  103. data/dependencies/SDL_sound/libmodplug/fastmix.c +1 -1
  104. data/dependencies/SDL_sound/libmodplug/libmodplug.h +5 -4
  105. data/dependencies/SDL_sound/libmodplug/load_ams.c +6 -3
  106. data/dependencies/SDL_sound/libmodplug/load_dsm.c +2 -1
  107. data/dependencies/SDL_sound/libmodplug/load_mt2.c +2 -2
  108. data/dependencies/SDL_sound/libmodplug/modplug.h +2 -1
  109. data/dependencies/SDL_sound/libmodplug/snd_fx.c +3 -3
  110. data/dependencies/SDL_sound/libmodplug/sndfile.c +3 -0
  111. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -3
  112. data/dependencies/SDL_sound/stb_vorbis.h +397 -342
  113. data/dependencies/mojoAL/AL/alc.h +2 -2
  114. data/dependencies/mojoAL/mojoal.c +2120 -1885
  115. data/dependencies/stb/stb_image.h +1190 -1379
  116. data/dependencies/stb/stb_image_write.h +329 -129
  117. data/dependencies/stb/stb_truetype.h +304 -60
  118. data/dependencies/utf8proc/utf8proc.c +133 -75
  119. data/dependencies/utf8proc/utf8proc.h +131 -50
  120. data/dependencies/utf8proc/utf8proc_data.h +16213 -15658
  121. data/ext/gosu-ffi/extconf.rb +103 -0
  122. data/ext/gosu-ffi/gosu-ffi.def +467 -0
  123. data/ffi/Gosu.cpp +330 -0
  124. data/ffi/Gosu.h +84 -0
  125. data/ffi/Gosu_Channel.cpp +62 -0
  126. data/ffi/Gosu_Channel.h +17 -0
  127. data/ffi/Gosu_Color.cpp +176 -0
  128. data/ffi/Gosu_Color.h +31 -0
  129. data/ffi/Gosu_Constants.cpp +334 -0
  130. data/ffi/Gosu_FFI.h +34 -0
  131. data/ffi/Gosu_FFI_internal.h +160 -0
  132. data/ffi/Gosu_Font.cpp +97 -0
  133. data/ffi/Gosu_Font.h +32 -0
  134. data/ffi/Gosu_Image.cpp +239 -0
  135. data/ffi/Gosu_Image.h +71 -0
  136. data/ffi/Gosu_Sample.cpp +29 -0
  137. data/ffi/Gosu_Sample.h +14 -0
  138. data/ffi/Gosu_Song.cpp +69 -0
  139. data/ffi/Gosu_Song.h +18 -0
  140. data/ffi/Gosu_TextInput.cpp +94 -0
  141. data/ffi/Gosu_TextInput.h +25 -0
  142. data/ffi/Gosu_Window.cpp +314 -0
  143. data/ffi/Gosu_Window.h +78 -0
  144. data/include/Gosu/Audio.hpp +6 -11
  145. data/include/Gosu/Bitmap.hpp +38 -53
  146. data/include/Gosu/Buffer.hpp +54 -0
  147. data/include/Gosu/Color.hpp +29 -36
  148. data/include/Gosu/Directories.hpp +25 -28
  149. data/include/Gosu/Drawable.hpp +58 -0
  150. data/include/Gosu/Font.hpp +6 -5
  151. data/include/Gosu/Fwd.hpp +4 -6
  152. data/include/Gosu/Gosu.hpp +5 -5
  153. data/include/Gosu/Graphics.hpp +53 -61
  154. data/include/Gosu/GraphicsBase.hpp +1 -11
  155. data/include/Gosu/Image.hpp +12 -15
  156. data/include/Gosu/Math.hpp +50 -72
  157. data/include/Gosu/Transform.hpp +32 -0
  158. data/include/Gosu/Utility.hpp +77 -1
  159. data/include/Gosu/Version.hpp +2 -2
  160. data/include/Gosu/Window.hpp +15 -9
  161. data/lib/SDL3.dll +0 -0
  162. data/lib/gosu/channel.rb +50 -0
  163. data/lib/gosu/color.rb +173 -0
  164. data/lib/gosu/compat.rb +130 -17
  165. data/lib/gosu/constants.rb +386 -0
  166. data/lib/gosu/ffi.rb +269 -0
  167. data/lib/gosu/font.rb +62 -0
  168. data/lib/gosu/gl_tex_info.rb +33 -0
  169. data/lib/gosu/gosu.rb +250 -0
  170. data/lib/gosu/image.rb +179 -0
  171. data/lib/gosu/numeric.rb +17 -0
  172. data/lib/gosu/preview.rb +6 -6
  173. data/lib/gosu/sample.rb +25 -0
  174. data/lib/gosu/song.rb +57 -0
  175. data/lib/gosu/text_input.rb +83 -0
  176. data/lib/gosu/window.rb +249 -0
  177. data/lib/gosu.rb +29 -8
  178. data/lib64/SDL3.dll +0 -0
  179. data/rdoc/gosu.rb +2 -2
  180. data/src/Audio.cpp +30 -24
  181. data/src/AudioFile.hpp +5 -4
  182. data/src/AudioFileAudioToolbox.cpp +8 -8
  183. data/src/AudioFileSDLSound.cpp +12 -15
  184. data/src/BinPacker.cpp +188 -0
  185. data/src/BinPacker.hpp +55 -0
  186. data/src/Bitmap.cpp +172 -143
  187. data/src/BitmapIO.cpp +60 -86
  188. data/src/Buffer.cpp +150 -0
  189. data/src/ClipRectStack.cpp +38 -0
  190. data/src/ClipRectStack.hpp +17 -83
  191. data/src/Color.cpp +75 -80
  192. data/src/Directories.cpp +47 -0
  193. data/src/DirectoriesUIKit.cpp +50 -0
  194. data/src/DrawOp.hpp +20 -18
  195. data/src/DrawOpQueue.hpp +27 -33
  196. data/src/Drawable.cpp +95 -0
  197. data/src/EmptyDrawable.hpp +38 -0
  198. data/src/FPS.cpp +31 -0
  199. data/src/Font.cpp +104 -74
  200. data/src/GosuGLView.cpp +13 -11
  201. data/src/GosuViewController.cpp +2 -10
  202. data/src/Graphics.cpp +86 -145
  203. data/src/GraphicsImpl.hpp +14 -68
  204. data/src/Image.cpp +41 -35
  205. data/src/Input.cpp +73 -78
  206. data/src/Macro.cpp +46 -29
  207. data/src/Macro.hpp +23 -17
  208. data/src/MarkupParser.cpp +5 -5
  209. data/src/Math.cpp +48 -22
  210. data/src/OffScreenTarget.cpp +47 -50
  211. data/src/OffScreenTarget.hpp +13 -11
  212. data/src/OpenGLContext.cpp +116 -0
  213. data/src/OpenGLContext.hpp +41 -0
  214. data/src/RenderState.hpp +49 -71
  215. data/src/Resolution.cpp +65 -53
  216. data/src/TexChunk.cpp +35 -80
  217. data/src/TexChunk.hpp +44 -35
  218. data/src/Text.cpp +1 -1
  219. data/src/TextBuilder.cpp +35 -21
  220. data/src/TextBuilder.hpp +6 -9
  221. data/src/TextInput.cpp +9 -9
  222. data/src/Texture.cpp +62 -80
  223. data/src/Texture.hpp +25 -23
  224. data/src/TiledDrawable.cpp +149 -0
  225. data/src/TiledDrawable.hpp +47 -0
  226. data/src/TimingApple.cpp +1 -1
  227. data/src/Transform.cpp +45 -50
  228. data/src/TransformStack.hpp +16 -16
  229. data/src/TrueTypeFont.cpp +59 -51
  230. data/src/TrueTypeFont.hpp +6 -7
  231. data/src/TrueTypeFontApple.cpp +28 -19
  232. data/src/TrueTypeFontUnix.cpp +39 -32
  233. data/src/TrueTypeFontWin.cpp +30 -30
  234. data/src/Utility.cpp +113 -37
  235. data/src/UtilityWin.cpp +49 -0
  236. data/src/Window.cpp +121 -151
  237. data/src/WindowUIKit.cpp +14 -14
  238. metadata +165 -118
  239. data/dependencies/SDL/include/SDL.h +0 -233
  240. data/dependencies/SDL/include/SDL_assert.h +0 -320
  241. data/dependencies/SDL/include/SDL_atomic.h +0 -415
  242. data/dependencies/SDL/include/SDL_audio.h +0 -1500
  243. data/dependencies/SDL/include/SDL_clipboard.h +0 -141
  244. data/dependencies/SDL/include/SDL_config.h +0 -331
  245. data/dependencies/SDL/include/SDL_cpuinfo.h +0 -594
  246. data/dependencies/SDL/include/SDL_endian.h +0 -348
  247. data/dependencies/SDL/include/SDL_error.h +0 -163
  248. data/dependencies/SDL/include/SDL_events.h +0 -1166
  249. data/dependencies/SDL/include/SDL_filesystem.h +0 -149
  250. data/dependencies/SDL/include/SDL_gamecontroller.h +0 -1074
  251. data/dependencies/SDL/include/SDL_gesture.h +0 -117
  252. data/dependencies/SDL/include/SDL_guid.h +0 -100
  253. data/dependencies/SDL/include/SDL_haptic.h +0 -1341
  254. data/dependencies/SDL/include/SDL_hidapi.h +0 -451
  255. data/dependencies/SDL/include/SDL_hints.h +0 -2569
  256. data/dependencies/SDL/include/SDL_joystick.h +0 -1066
  257. data/dependencies/SDL/include/SDL_keyboard.h +0 -353
  258. data/dependencies/SDL/include/SDL_keycode.h +0 -358
  259. data/dependencies/SDL/include/SDL_loadso.h +0 -115
  260. data/dependencies/SDL/include/SDL_log.h +0 -404
  261. data/dependencies/SDL/include/SDL_main.h +0 -275
  262. data/dependencies/SDL/include/SDL_mouse.h +0 -465
  263. data/dependencies/SDL/include/SDL_mutex.h +0 -471
  264. data/dependencies/SDL/include/SDL_name.h +0 -33
  265. data/dependencies/SDL/include/SDL_pixels.h +0 -644
  266. data/dependencies/SDL/include/SDL_platform.h +0 -261
  267. data/dependencies/SDL/include/SDL_quit.h +0 -58
  268. data/dependencies/SDL/include/SDL_rect.h +0 -376
  269. data/dependencies/SDL/include/SDL_render.h +0 -1919
  270. data/dependencies/SDL/include/SDL_revision.h +0 -7
  271. data/dependencies/SDL/include/SDL_rwops.h +0 -841
  272. data/dependencies/SDL/include/SDL_sensor.h +0 -322
  273. data/dependencies/SDL/include/SDL_shape.h +0 -155
  274. data/dependencies/SDL/include/SDL_stdinc.h +0 -830
  275. data/dependencies/SDL/include/SDL_surface.h +0 -997
  276. data/dependencies/SDL/include/SDL_system.h +0 -623
  277. data/dependencies/SDL/include/SDL_syswm.h +0 -386
  278. data/dependencies/SDL/include/SDL_test_images.h +0 -78
  279. data/dependencies/SDL/include/SDL_test_random.h +0 -115
  280. data/dependencies/SDL/include/SDL_thread.h +0 -464
  281. data/dependencies/SDL/include/SDL_timer.h +0 -222
  282. data/dependencies/SDL/include/SDL_touch.h +0 -150
  283. data/dependencies/SDL/include/SDL_version.h +0 -204
  284. data/dependencies/SDL/include/SDL_video.h +0 -2150
  285. data/dependencies/SDL/include/SDL_vulkan.h +0 -215
  286. data/dependencies/SDL/include/begin_code.h +0 -187
  287. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  288. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  289. data/dependencies/SDL_sound/SDL_sound.h +0 -725
  290. data/ext/gosu/extconf.rb +0 -119
  291. data/include/Gosu/IO.hpp +0 -254
  292. data/include/Gosu/ImageData.hpp +0 -53
  293. data/include/Gosu/Inspection.hpp +0 -7
  294. data/lib/SDL2.dll +0 -0
  295. data/lib/gosu/patches.rb +0 -66
  296. data/lib/gosu/run.rb +0 -20
  297. data/lib/gosu/swig_patches.rb +0 -110
  298. data/lib64/SDL2.dll +0 -0
  299. data/src/BlockAllocator.cpp +0 -131
  300. data/src/BlockAllocator.hpp +0 -32
  301. data/src/DirectoriesApple.cpp +0 -69
  302. data/src/DirectoriesUnix.cpp +0 -46
  303. data/src/DirectoriesWin.cpp +0 -65
  304. data/src/EmptyImageData.hpp +0 -52
  305. data/src/FileUnix.cpp +0 -99
  306. data/src/FileWin.cpp +0 -88
  307. data/src/IO.cpp +0 -60
  308. data/src/Iconv.hpp +0 -51
  309. data/src/Inspection.cpp +0 -27
  310. data/src/LargeImageData.cpp +0 -215
  311. data/src/LargeImageData.hpp +0 -39
  312. data/src/Log.hpp +0 -19
  313. data/src/RubyGosu.cxx +0 -13100
  314. data/src/RubyGosu.h +0 -49
  315. data/src/WinUtility.cpp +0 -61
  316. data/src/WinUtility.hpp +0 -27
  317. /data/dependencies/SDL/include/{SDL_opengles2_gl2.h → SDL3/SDL_opengles2_gl2.h} +0 -0
  318. /data/dependencies/SDL/include/{SDL_opengles2_gl2ext.h → SDL3/SDL_opengles2_gl2ext.h} +0 -0
  319. /data/dependencies/SDL/include/{SDL_opengles2_gl2platform.h → SDL3/SDL_opengles2_gl2platform.h} +0 -0
  320. /data/dependencies/SDL/include/{SDL_opengles2_khrplatform.h → SDL3/SDL_opengles2_khrplatform.h} +0 -0
@@ -1,4 +1,4 @@
1
- // Ogg Vorbis audio decoder - v1.22 - public domain
1
+ // Ogg Vorbis audio decoder - v1.24 - public domain
2
2
  // http://nothings.org/stb_vorbis/
3
3
  //
4
4
  // Original version written by Sean Barrett in 2007.
@@ -21,6 +21,7 @@
21
21
  //
22
22
  // Feature contributors:
23
23
  // Dougall Johnson (sample-exact seeking)
24
+ // Vitaly Novichkov (sample-accurate tell)
24
25
  //
25
26
  // Bugfix/warning contributors:
26
27
  // Terje Mathisen Niklas Frykholm Andy Hill
@@ -37,6 +38,8 @@
37
38
  // Alice Rowan
38
39
  //
39
40
  // Partial history:
41
+ // 1.24 - 2026-11-12 - fixes
42
+ // 1.23 - 2026-11-08 - many fixes, support for 64-bit seek/offsets
40
43
  // 1.22 - 2021-07-11 - various small fixes
41
44
  // 1.21 - 2021-07-02 - fix bug for files with no comments
42
45
  // 1.20 - 2020-07-11 - several small fixes
@@ -66,6 +69,14 @@
66
69
  //
67
70
  // See end of file for full version history.
68
71
 
72
+ #ifdef STB_VORBIS_NO_PULLDATA_API
73
+ #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
74
+ #define STB_VORBIS_NO_INTEGER_CONVERSION
75
+ #endif
76
+ #ifndef STB_VORBIS_NO_STDIO
77
+ #define STB_VORBIS_NO_STDIO
78
+ #endif
79
+ #endif
69
80
 
70
81
  //////////////////////////////////////////////////////////////////////////////
71
82
  //
@@ -75,10 +86,6 @@
75
86
  #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
76
87
  #define STB_VORBIS_INCLUDE_STB_VORBIS_H
77
88
 
78
- #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
79
- #define STB_VORBIS_NO_STDIO 1
80
- #endif
81
-
82
89
  #ifndef STB_VORBIS_NO_STDIO
83
90
  #include <stdio.h>
84
91
  #endif
@@ -139,6 +146,8 @@ typedef struct
139
146
  int max_frame_size;
140
147
  } stb_vorbis_info;
141
148
 
149
+ #ifndef STB_VORBIS_NO_COMMENTS
150
+
142
151
  typedef struct
143
152
  {
144
153
  char *vendor;
@@ -147,11 +156,15 @@ typedef struct
147
156
  char **comment_list;
148
157
  } stb_vorbis_comment;
149
158
 
159
+ #endif
160
+
150
161
  // get general information about the file
151
162
  extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
152
163
 
164
+ #ifndef STB_VORBIS_NO_COMMENTS
153
165
  // get ogg comments
154
166
  extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f);
167
+ #endif
155
168
 
156
169
  // get the last error detected (clears it, too)
157
170
  extern int stb_vorbis_get_error(stb_vorbis *f);
@@ -166,6 +179,12 @@ extern void stb_vorbis_close(stb_vorbis *f);
166
179
  // NOT WORKING YET after a seek with PULLDATA API
167
180
  extern int stb_vorbis_get_sample_offset(stb_vorbis *f);
168
181
 
182
+ // this function returns the count of returned samples from the beginning of the
183
+ // file. Functions "stb_vorbis_get_samples_*", "stb_vorbis_seek_*()" will
184
+ // affect the returned value. Use this call to get the accurate sample position
185
+ // during playback.
186
+ extern int stb_vorbis_get_playback_sample_offset(stb_vorbis *f);
187
+
169
188
  // returns the current seek point within the file, or offset from the beginning
170
189
  // of the memory buffer. In pushdata mode it returns 0.
171
190
  extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
@@ -296,9 +315,10 @@ extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_cl
296
315
  // confused.
297
316
  #endif
298
317
 
299
- #ifdef __SDL_SOUND_INTERNAL__
300
- extern stb_vorbis * stb_vorbis_open_rwops_section(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length);
301
- extern stb_vorbis * stb_vorbis_open_rwops(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc);
318
+ #ifdef STB_VORBIS_SDL
319
+ extern stb_vorbis * stb_vorbis_open_io_section(SDL_IOStream *io, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length);
320
+ extern stb_vorbis * stb_vorbis_open_io(SDL_IOStream *io, int close_on_free, int *error, const stb_vorbis_alloc *alloc);
321
+ #define IO_BUFFER_SIZE 2048
302
322
  #endif
303
323
 
304
324
  extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
@@ -448,12 +468,12 @@ enum STBVorbisError
448
468
  // does not compile the code for converting audio sample data from
449
469
  // float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
450
470
  // #define STB_VORBIS_NO_INTEGER_CONVERSION
451
-
452
- // STB_VORBIS_NO_FAST_SCALED_FLOAT
453
- // does not use a fast float-to-int trick to accelerate float-to-int on
454
- // most platforms which requires endianness be defined correctly.
455
- //#define STB_VORBIS_NO_FAST_SCALED_FLOAT
456
-
471
+ //
472
+ // STB_VORBIS_NO_COMMENTS
473
+ // disables reading and storing vendor and comment information,
474
+ // which can large amounts of memory with adversarial inputs and is
475
+ // not necessary for pure audio playback.
476
+ // #define STB_VORBIS_NO_COMMENTS
457
477
 
458
478
  // STB_VORBIS_MAX_CHANNELS [number]
459
479
  // globally define this to the maximum number of channels you need.
@@ -550,67 +570,26 @@ enum STBVorbisError
550
570
  // you'd ever want to do it except for debugging.
551
571
  // #define STB_VORBIS_NO_DEFER_FLOOR
552
572
 
553
- // STB_VORBIS_NO_COMMENTS
554
- // disables reading and storing user comments
555
- // #define STB_VORBIS_NO_COMMENTS
556
-
557
-
558
- //////////////////////////////////////////////////////////////////////////////
559
-
560
- #ifdef STB_VORBIS_NO_PULLDATA_API
561
- #define STB_VORBIS_NO_INTEGER_CONVERSION
562
- #define STB_VORBIS_NO_STDIO
563
- #endif
564
-
565
- #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
566
- #define STB_VORBIS_NO_STDIO 1
567
- #endif
568
-
569
- #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
570
- #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
571
-
572
- // only need endianness for fast-float-to-int, which we don't
573
- // use for pushdata
574
573
 
575
- #ifndef STB_VORBIS_BIG_ENDIAN
576
- #define STB_VORBIS_ENDIAN 0
577
- #else
578
- #define STB_VORBIS_ENDIAN 1
579
- #endif
580
574
 
581
- #endif
582
- #endif
583
575
 
576
+ //////////////////////////////////////////////////////////////////////////////
584
577
 
585
578
  #ifndef STB_VORBIS_NO_STDIO
586
579
  #include <stdio.h>
587
580
  #endif
588
581
 
589
- #ifndef STB_VORBIS_NO_CRT
590
- #include <stdlib.h>
591
- #include <string.h>
592
- #include <assert.h>
593
- #include <math.h>
594
- #else // STB_VORBIS_NO_CRT
595
- #ifndef NULL
596
- #define NULL 0
597
- #endif
598
- #ifndef malloc
599
- #define malloc(s) 0
600
- #endif
601
- #ifndef free
602
- #define free(p) ((void) 0)
603
- #endif
604
- #ifndef realloc
605
- #define realloc(p, s) 0
606
- #endif
607
- #endif // STB_VORBIS_NO_CRT
608
-
582
+ #include <stdlib.h>
583
+ #include <string.h>
584
+ #include <assert.h>
585
+ #include <math.h>
609
586
  #include <limits.h>
610
587
 
611
588
  #ifndef STB_FORCEINLINE
612
- #if defined(_MSC_VER)
589
+ #if defined(_MSC_VER) && (_MSC_VER >= 1200)
613
590
  #define STB_FORCEINLINE __forceinline
591
+ #elif defined(_MSC_VER)
592
+ #define STB_FORCEINLINE static __inline
614
593
  #elif (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))) || defined(__clang__)
615
594
  #define STB_FORCEINLINE static __inline __attribute__((always_inline))
616
595
  #else
@@ -637,7 +616,7 @@ enum STBVorbisError
637
616
  #define MAX_BLOCKSIZE_LOG 13 // from specification
638
617
  #define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)
639
618
 
640
- #ifdef __SDL_SOUND_INTERNAL__
619
+ #ifdef STB_VORBIS_SDL
641
620
  typedef Uint8 uint8;
642
621
  typedef Sint8 int8;
643
622
  typedef Uint16 uint16;
@@ -645,13 +624,22 @@ typedef Sint16 int16;
645
624
  typedef Uint32 uint32;
646
625
  typedef Sint32 int32;
647
626
  #else
627
+
628
+ #if defined B_BEOS_VERSION
629
+ #include <SupportDefs.h>
630
+ #elif defined __amigaos4__
631
+ #include <exec/types.h>
632
+ #elif defined _arch_dreamcast /* KallistiOS */
633
+ #include <arch/types.h>
634
+ #else
648
635
  typedef unsigned char uint8;
649
636
  typedef signed char int8;
650
637
  typedef unsigned short uint16;
651
638
  typedef signed short int16;
652
639
  typedef unsigned int uint32;
653
640
  typedef signed int int32;
654
- #endif
641
+ #endif /* !BeOS && !AmigaOS4 && !KallistiOS */
642
+ #endif /* !STB_VORBIS_SDL */
655
643
 
656
644
  #ifdef __has_feature
657
645
  #if __has_feature(undefined_behavior_sanitizer)
@@ -673,6 +661,8 @@ typedef float codetype;
673
661
 
674
662
  #ifdef _MSC_VER
675
663
  #define STBV_NOTUSED(v) (void)(v)
664
+ #elif defined(__VBCC__)
665
+ #define STBV_NOTUSED(v)
676
666
  #else
677
667
  #define STBV_NOTUSED(v) (void)sizeof(v)
678
668
  #endif
@@ -768,7 +758,6 @@ typedef struct
768
758
 
769
759
  typedef struct
770
760
  {
771
- // https://github.com/nothings/stb/pull/1312
772
761
  MappingChannel *chan;
773
762
  uint16 coupling_steps;
774
763
  uint8 submaps;
@@ -821,15 +810,19 @@ struct stb_vorbis
821
810
  uint32 f_start;
822
811
  int close_on_free;
823
812
  #endif
824
- #ifdef __SDL_SOUND_INTERNAL__
825
- SDL_RWops *rwops;
826
- uint32 rwops_start;
813
+ #ifdef STB_VORBIS_SDL
814
+ SDL_IOStream *io;
815
+ uint32 io_start;
816
+ uint32 io_virtual_pos;
817
+ uint32 io_buffer_pos;
818
+ uint32 io_buffer_fill;
819
+ uint8 io_buffer[IO_BUFFER_SIZE];
827
820
  int close_on_free;
828
821
  #endif
829
822
 
830
- uint8 *stream;
831
- uint8 *stream_start;
832
- uint8 *stream_end;
823
+ const uint8 *stream;
824
+ const uint8 *stream_start;
825
+ const uint8 *stream_end;
833
826
 
834
827
  uint32 stream_len;
835
828
 
@@ -887,6 +880,9 @@ struct stb_vorbis
887
880
  uint32 current_loc; // sample location of next frame to decode
888
881
  int current_loc_valid;
889
882
 
883
+ int32 current_playback_loc; // sample location of played samples
884
+ int current_playback_loc_valid;
885
+
890
886
  // per-blocksize precomputed data
891
887
 
892
888
  // twiddle factors
@@ -970,6 +966,8 @@ static int error(vorb *f, enum STBVorbisError e)
970
966
  // given a sufficiently large block of memory, make an array of pointers to subblocks of it
971
967
  static void *make_block_array(void *mem, int count, int size)
972
968
  {
969
+ if (!mem) return NULL;
970
+ else {
973
971
  int i;
974
972
  void ** p = (void **) mem;
975
973
  char *q = (char *) (p + count);
@@ -978,15 +976,17 @@ static void *make_block_array(void *mem, int count, int size)
978
976
  q += size;
979
977
  }
980
978
  return p;
979
+ }
981
980
  }
982
981
 
983
982
  static void *setup_malloc(vorb *f, int sz)
984
983
  {
984
+ if (sz <= 0 || INT_MAX - 7 < sz) return NULL;
985
985
  sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
986
986
  f->setup_memory_required += sz;
987
987
  if (f->alloc.alloc_buffer) {
988
988
  void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
989
- if (f->setup_offset + sz > f->temp_offset) return NULL;
989
+ if (f->temp_offset < sz || f->temp_offset - sz < f->setup_offset) return NULL;
990
990
  f->setup_offset += sz;
991
991
  return p;
992
992
  }
@@ -1001,13 +1001,14 @@ static void setup_free(vorb *f, void *p)
1001
1001
 
1002
1002
  static void *setup_temp_malloc(vorb *f, int sz)
1003
1003
  {
1004
+ if (sz <= 0 || INT_MAX - 7 < sz) return NULL;
1004
1005
  sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
1005
1006
  if (f->alloc.alloc_buffer) {
1006
- if (f->temp_offset - sz < f->setup_offset) return NULL;
1007
+ if (f->temp_offset < sz || f->temp_offset - sz < f->setup_offset) return NULL;
1007
1008
  f->temp_offset -= sz;
1008
1009
  return (char *) f->alloc.alloc_buffer + f->temp_offset;
1009
1010
  }
1010
- return malloc(sz);
1011
+ return sz ? malloc(sz) : NULL;
1011
1012
  }
1012
1013
 
1013
1014
  static void setup_temp_free(vorb *f, void **_p, int sz)
@@ -1061,7 +1062,7 @@ static float square(float x)
1061
1062
  // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
1062
1063
  static int ilog(int32 n)
1063
1064
  {
1064
- static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
1065
+ static const signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
1065
1066
 
1066
1067
  if (n < 0) return 0; // signed n returns 0
1067
1068
 
@@ -1113,7 +1114,7 @@ static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int
1113
1114
  c->codewords [symbol] = huff_code;
1114
1115
  } else {
1115
1116
  c->codewords [count] = huff_code;
1116
- c->codeword_lengths[count] = len;
1117
+ c->codeword_lengths[count] = (uint8)len;
1117
1118
  values [count] = symbol;
1118
1119
  }
1119
1120
  }
@@ -1181,7 +1182,7 @@ static void compute_accelerated_huffman(Codebook *c)
1181
1182
  uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
1182
1183
  // set table entries for all bit combinations in the higher bits
1183
1184
  while (z < FAST_HUFFMAN_TABLE_SIZE) {
1184
- c->fast_huffman[z] = i;
1185
+ c->fast_huffman[z] = (int16)i;
1185
1186
  z += 1 << c->codeword_lengths[i];
1186
1187
  }
1187
1188
  }
@@ -1198,8 +1199,8 @@ static void compute_accelerated_huffman(Codebook *c)
1198
1199
 
1199
1200
  static int STBV_CDECL uint32_compare(const void *p, const void *q)
1200
1201
  {
1201
- uint32 x = * (uint32 *) p;
1202
- uint32 y = * (uint32 *) q;
1202
+ uint32 x = * (const uint32 *) p;
1203
+ uint32 y = * (const uint32 *) q;
1203
1204
  return x < y ? -1 : x > y;
1204
1205
  }
1205
1206
 
@@ -1241,7 +1242,7 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
1241
1242
  // #3: for each original entry, find the sorted entry
1242
1243
  // #1 requires extra storage, #2 is slow, #3 can use binary search!
1243
1244
  for (i=0; i < len; ++i) {
1244
- int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
1245
+ uint8 huff_len = c->sparse ? lengths[values[i]] : lengths[i];
1245
1246
  if (include_in_sort(c,huff_len)) {
1246
1247
  uint32 code = bit_reverse(c->codewords[i]);
1247
1248
  int x=0, n=c->sorted_entries;
@@ -1269,7 +1270,7 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
1269
1270
  // only run while parsing the header (3 times)
1270
1271
  static int vorbis_validate(uint8 *data)
1271
1272
  {
1272
- static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };
1273
+ static const uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };
1273
1274
  return memcmp(data, vorbis, 6) == 0;
1274
1275
  }
1275
1276
 
@@ -1281,12 +1282,14 @@ static int vorbis_validate(uint8 *data)
1281
1282
  STB_NO_SANITIZE("float-cast-overflow")
1282
1283
  static int lookup1_values(int entries, int dim)
1283
1284
  {
1284
- int r = (int) floor(exp((float) log((float) entries) / dim));
1285
- if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;
1286
- ++r; // floor() to avoid _ftol() when non-CRT
1287
- if (pow((float) r+1, dim) <= entries)
1285
+ double entriesd = (double)entries; // exact (all int32 exact in IEEE double)
1286
+ // dim >= 1, so r <= entries (definitely fits in int)
1287
+ int r = (int) floor(exp(log(entriesd) / dim));
1288
+ if (floor(pow((double) r+1, dim)) <= entriesd)
1289
+ ++r;
1290
+ if (pow((double) r+1, dim) <= entriesd)
1288
1291
  return -1;
1289
- if ((int) floor(pow((float) r, dim)) > entries)
1292
+ if (floor(pow((double) r, dim)) > entriesd)
1290
1293
  return -1;
1291
1294
  return r;
1292
1295
  }
@@ -1321,7 +1324,7 @@ static void compute_bitreverse(int n, uint16 *rev)
1321
1324
  int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
1322
1325
  int i, n8 = n >> 3;
1323
1326
  for (i=0; i < n8; ++i)
1324
- rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2;
1327
+ rev[i] = (uint16) ((bit_reverse(i) >> (32-ld+3)) << 2);
1325
1328
  }
1326
1329
 
1327
1330
  static int init_blocksize(vorb *f, int b, int n)
@@ -1360,8 +1363,8 @@ typedef struct
1360
1363
 
1361
1364
  static int STBV_CDECL point_compare(const void *p, const void *q)
1362
1365
  {
1363
- stbv__floor_ordering *a = (stbv__floor_ordering *) p;
1364
- stbv__floor_ordering *b = (stbv__floor_ordering *) q;
1366
+ const stbv__floor_ordering *a = (const stbv__floor_ordering *) p;
1367
+ const stbv__floor_ordering *b = (const stbv__floor_ordering *) q;
1365
1368
  return a->x < b->x ? -1 : a->x > b->x;
1366
1369
  }
1367
1370
 
@@ -1369,7 +1372,7 @@ static int STBV_CDECL point_compare(const void *p, const void *q)
1369
1372
  /////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
1370
1373
 
1371
1374
 
1372
- #ifdef __SDL_SOUND_INTERNAL__
1375
+ #ifdef STB_VORBIS_SDL
1373
1376
  #define USE_MEMORY(z) FALSE
1374
1377
  #elif defined(STB_VORBIS_NO_STDIO)
1375
1378
  #define USE_MEMORY(z) TRUE
@@ -1379,25 +1382,30 @@ static int STBV_CDECL point_compare(const void *p, const void *q)
1379
1382
 
1380
1383
  static uint8 get8(vorb *z)
1381
1384
  {
1382
- #ifdef __SDL_SOUND_INTERNAL__
1383
- uint8 c;
1384
- if (SDL_RWread(z->rwops, &c, 1, 1) != 1) { z->eof = TRUE; return 0; }
1385
- return c;
1385
+ #ifdef STB_VORBIS_SDL
1386
+ if (z->io_buffer_pos >= z->io_buffer_fill) {
1387
+ z->io_buffer_fill = SDL_ReadIO(z->io, z->io_buffer, IO_BUFFER_SIZE);
1388
+ z->io_buffer_pos = 0;
1389
+ if (z->io_buffer_fill == 0) { z->eof = TRUE; return 0; }
1390
+ }
1391
+ z->io_virtual_pos++;
1392
+ return z->io_buffer[z->io_buffer_pos++];
1386
1393
 
1387
- #else
1394
+ #else /**/
1388
1395
  if (USE_MEMORY(z)) {
1389
1396
  if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
1390
1397
  return *z->stream++;
1391
1398
  }
1392
- #endif
1393
1399
 
1394
1400
  #ifndef STB_VORBIS_NO_STDIO
1395
1401
  {
1396
1402
  int c = fgetc(z->f);
1397
1403
  if (c == EOF) { z->eof = TRUE; return 0; }
1398
- return c;
1404
+ return (uint8)c;
1399
1405
  }
1400
1406
  #endif
1407
+ return 0; /* silence warnings */
1408
+ #endif /* !STB_VORBIS_SDL */
1401
1409
  }
1402
1410
 
1403
1411
  static uint32 get32(vorb *f)
@@ -1412,19 +1420,37 @@ static uint32 get32(vorb *f)
1412
1420
 
1413
1421
  static int getn(vorb *z, uint8 *data, int n)
1414
1422
  {
1415
- #ifdef __SDL_SOUND_INTERNAL__
1416
- if (SDL_RWread(z->rwops, data, n, 1) == 1) return 1;
1417
- z->eof = 1;
1418
- return 0;
1423
+ #ifdef STB_VORBIS_SDL
1424
+ while (n > 0) {
1425
+ int chunk;
1419
1426
 
1420
- #else
1427
+ if (z->io_buffer_pos >= z->io_buffer_fill) {
1428
+ z->io_buffer_fill = SDL_ReadIO(z->io, z->io_buffer, IO_BUFFER_SIZE);
1429
+ z->io_buffer_pos = 0;
1430
+ if (z->io_buffer_fill == 0) {
1431
+ z->eof = 1;
1432
+ return 0;
1433
+ }
1434
+ }
1435
+
1436
+ chunk = z->io_buffer_fill - z->io_buffer_pos;
1437
+ if (chunk > n) chunk = n;
1438
+
1439
+ memcpy(data, z->io_buffer + z->io_buffer_pos, chunk);
1440
+ z->io_buffer_pos += chunk;
1441
+ z->io_virtual_pos += chunk;
1442
+ data += chunk;
1443
+ n -= chunk;
1444
+ }
1445
+ return 1;
1446
+
1447
+ #else /**/
1421
1448
  if (USE_MEMORY(z)) {
1422
1449
  if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }
1423
1450
  memcpy(data, z->stream, n);
1424
1451
  z->stream += n;
1425
1452
  return 1;
1426
1453
  }
1427
- #endif
1428
1454
 
1429
1455
  #ifndef STB_VORBIS_NO_STDIO
1430
1456
  if (fread(data, n, 1, z->f) == 1)
@@ -1434,29 +1460,33 @@ static int getn(vorb *z, uint8 *data, int n)
1434
1460
  return 0;
1435
1461
  }
1436
1462
  #endif
1463
+ return 0; /* silence warnings */
1464
+ #endif /* !STB_VORBIS_SDL */
1437
1465
  }
1438
1466
 
1467
+ static int set_file_offset(stb_vorbis *f, unsigned int loc);
1468
+
1439
1469
  static void skip(vorb *z, int n)
1440
1470
  {
1441
- #ifdef __SDL_SOUND_INTERNAL__
1442
- SDL_RWseek(z->rwops, n, RW_SEEK_CUR);
1443
-
1444
- #else
1471
+ #ifdef STB_VORBIS_SDL
1472
+ set_file_offset(z, z->io_virtual_pos + n);
1473
+ #else /**/
1445
1474
  if (USE_MEMORY(z)) {
1446
1475
  z->stream += n;
1447
1476
  if (z->stream >= z->stream_end) z->eof = 1;
1448
1477
  return;
1449
1478
  }
1450
- #endif
1451
-
1452
1479
  #ifndef STB_VORBIS_NO_STDIO
1453
1480
  {
1454
1481
  long x = ftell(z->f);
1455
1482
  fseek(z->f, x+n, SEEK_SET);
1456
1483
  }
1457
1484
  #endif
1485
+ #endif /* !STB_VORBIS_SDL */
1458
1486
  }
1459
1487
 
1488
+ #ifndef STB_VORBIS_NO_PULLDATA_API
1489
+
1460
1490
  static int set_file_offset(stb_vorbis *f, unsigned int loc)
1461
1491
  {
1462
1492
  #ifndef STB_VORBIS_NO_PUSHDATA_API
@@ -1464,22 +1494,36 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1464
1494
  #endif
1465
1495
  f->eof = 0;
1466
1496
 
1467
- #ifdef __SDL_SOUND_INTERNAL__
1468
- if (loc + f->rwops_start < loc || loc >= 0x80000000) {
1469
- loc = 0x7fffffff;
1497
+ #ifdef STB_VORBIS_SDL
1498
+ { unsigned int io_pos;
1499
+ uint32 buffer_start = f->io_virtual_pos - f->io_buffer_pos;
1500
+ uint32 buffer_end = buffer_start + f->io_buffer_fill;
1501
+ f->io_virtual_pos = loc;
1502
+
1503
+ // Move within buffer if possible
1504
+ if (loc >= buffer_start && loc < buffer_end)
1505
+ {
1506
+ f->io_buffer_pos = loc - buffer_start;
1507
+ return 1;
1508
+ }
1509
+
1510
+ io_pos = loc + f->io_start;
1511
+ if (io_pos < loc || loc >= 0x80000000) {
1512
+ io_pos = 0x7fffffff;
1470
1513
  f->eof = 1;
1471
- } else {
1472
- loc += f->rwops_start;
1473
1514
  }
1474
- if (SDL_RWseek(f->rwops, loc, RW_SEEK_SET) != -1)
1515
+
1516
+ f->io_buffer_pos = f->io_buffer_fill = 0; // Invalidate buffer
1517
+ if (SDL_SeekIO(f->io, io_pos, SDL_IO_SEEK_SET) != -1)
1475
1518
  return 1;
1476
1519
  f->eof = 1;
1477
- SDL_RWseek(f->rwops, f->rwops_start, RW_SEEK_END);
1520
+ SDL_SeekIO(f->io, f->io_start, SDL_IO_SEEK_END);
1478
1521
  return 0;
1522
+ }
1479
1523
 
1480
- #else
1524
+ #else /**/
1481
1525
  if (USE_MEMORY(f)) {
1482
- if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
1526
+ if (loc >= f->stream_len) {
1483
1527
  f->stream = f->stream_end;
1484
1528
  f->eof = 1;
1485
1529
  return 0;
@@ -1488,10 +1532,8 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1488
1532
  return 1;
1489
1533
  }
1490
1534
  }
1491
- #endif
1492
-
1493
1535
  #ifndef STB_VORBIS_NO_STDIO
1494
- if (loc + f->f_start < loc || loc >= 0x80000000) {
1536
+ if (loc >= ~0u - f->f_start || loc >= 0x80000000) {
1495
1537
  loc = 0x7fffffff;
1496
1538
  f->eof = 1;
1497
1539
  } else {
@@ -1503,10 +1545,14 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
1503
1545
  fseek(f->f, f->f_start, SEEK_END);
1504
1546
  return 0;
1505
1547
  #endif
1548
+ return 0; /* silence warnings */
1549
+ #endif /* !STB_VORBIS_SDL */
1506
1550
  }
1507
1551
 
1552
+ #endif // STBI_VORBIS_NO_PULLDATA_API
1508
1553
 
1509
- static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };
1554
+
1555
+ static const uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };
1510
1556
 
1511
1557
  static int capture_pattern(vorb *f)
1512
1558
  {
@@ -1618,7 +1664,7 @@ static int maybe_start_packet(vorb *f)
1618
1664
 
1619
1665
  static int next_segment(vorb *f)
1620
1666
  {
1621
- int len;
1667
+ uint8 len;
1622
1668
  if (f->last_seg) return 0;
1623
1669
  if (f->next_seg == -1) {
1624
1670
  f->last_seg_which = f->segment_count-1; // in case start_page fails
@@ -1662,11 +1708,10 @@ static int get8_packet(vorb *f)
1662
1708
  #ifndef STB_VORBIS_NO_COMMENTS
1663
1709
  static int get32_packet(vorb *f)
1664
1710
  {
1665
- uint32 x;
1666
- x = get8_packet(f);
1667
- x += get8_packet(f) << 8;
1668
- x += get8_packet(f) << 16;
1669
- x += (uint32) get8_packet(f) << 24;
1711
+ int sh;
1712
+ uint32 x = 0;
1713
+ for (sh = 0; sh < 32; sh += 8)
1714
+ x |= (uint32) get8_packet(f) << sh;
1670
1715
  return x;
1671
1716
  }
1672
1717
  #endif
@@ -1692,12 +1737,12 @@ static uint32 get_bits(vorb *f, int n)
1692
1737
  }
1693
1738
  if (f->valid_bits == 0) f->acc = 0;
1694
1739
  while (f->valid_bits < n) {
1695
- int z = get8_packet_raw(f);
1696
- if (z == EOP) {
1740
+ int p = get8_packet_raw(f);
1741
+ if (p == EOP) {
1697
1742
  f->valid_bits = INVALID_BITS;
1698
1743
  return 0;
1699
1744
  }
1700
- f->acc += z << f->valid_bits;
1745
+ f->acc += (uint32)p << f->valid_bits;
1701
1746
  f->valid_bits += 8;
1702
1747
  }
1703
1748
  }
@@ -1777,10 +1822,12 @@ static int codebook_decode_scalar_raw(vorb *f, Codebook *c)
1777
1822
  // if small, linear search
1778
1823
  assert(!c->sparse);
1779
1824
  for (i=0; i < c->entries; ++i) {
1825
+ uint32 mask;
1780
1826
  if (c->codeword_lengths[i] == NO_CODE) continue;
1781
- /* unsigned left shift for 32-bit codewords.
1782
- * https://github.com/nothings/stb/issues/1168 */
1783
- if (c->codewords[i] == (f->acc & ((1U << c->codeword_lengths[i])-1))) {
1827
+ // codeword lengths are in [1,32]. this expression gives the correct mask for all choices.
1828
+ // (for length 32, this involves intentional unsigned overflow).
1829
+ mask = (2u << (c->codeword_lengths[i] - 1)) - 1u;
1830
+ if (c->codewords[i] == (f->acc & mask)) {
1784
1831
  if (f->valid_bits >= c->codeword_lengths[i]) {
1785
1832
  f->acc >>= c->codeword_lengths[i];
1786
1833
  f->valid_bits -= c->codeword_lengths[i];
@@ -1804,9 +1851,9 @@ static int codebook_decode_scalar_raw(vorb *f, Codebook *c)
1804
1851
  var = f->acc & FAST_HUFFMAN_TABLE_MASK; \
1805
1852
  var = c->fast_huffman[var]; \
1806
1853
  if (var >= 0) { \
1807
- int n = c->codeword_lengths[var]; \
1808
- f->acc >>= n; \
1809
- f->valid_bits -= n; \
1854
+ int cl = c->codeword_lengths[var]; \
1855
+ f->acc >>= cl; \
1856
+ f->valid_bits -= cl; \
1810
1857
  if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
1811
1858
  } else { \
1812
1859
  var = codebook_decode_scalar_raw(f,c); \
@@ -1837,7 +1884,7 @@ static int codebook_decode_scalar(vorb *f, Codebook *c)
1837
1884
 
1838
1885
  #define DECODE(var,f,c) \
1839
1886
  DECODE_RAW(var,f,c) \
1840
- if (c->sparse) var = c->sorted_values[var];
1887
+ if (c->sparse && var >= 0) var = c->sorted_values[var];
1841
1888
 
1842
1889
  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1843
1890
  #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)
@@ -1972,7 +2019,7 @@ static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **out
1972
2019
  // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
1973
2020
  // and the length we'll be using (effective)
1974
2021
  if (c_inter + p_inter*ch + effective > len * ch) {
1975
- effective = len*ch - (p_inter*ch - c_inter);
2022
+ effective = len*ch - p_inter*ch - c_inter;
1976
2023
  }
1977
2024
 
1978
2025
  #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
@@ -2027,7 +2074,7 @@ static int predict_point(int x, int x0, int x1, int y0, int y1)
2027
2074
  }
2028
2075
 
2029
2076
  // the following table is block-copied from the specification
2030
- static float inverse_db_table[256] =
2077
+ static const float inverse_db_table[256] =
2031
2078
  {
2032
2079
  1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
2033
2080
  1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
@@ -2151,7 +2198,7 @@ STB_FORCEINLINE void draw_line(float *output, int x0, int y0, int x1, int y1, in
2151
2198
  ady -= abs(base) * adx;
2152
2199
  if (x1 > n) x1 = n;
2153
2200
  if (x < x1) {
2154
- LINE_OP(output[x], inverse_db_table[y&255]);
2201
+ LINE_OP(output[x], inverse_db_table[(uint32)y&255]);
2155
2202
  for (++x; x < x1; ++x) {
2156
2203
  err += ady;
2157
2204
  if (err >= adx) {
@@ -2159,7 +2206,7 @@ STB_FORCEINLINE void draw_line(float *output, int x0, int y0, int x1, int y1, in
2159
2206
  y += sy;
2160
2207
  } else
2161
2208
  y += base;
2162
- LINE_OP(output[x], inverse_db_table[y&255]);
2209
+ LINE_OP(output[x], inverse_db_table[(uint32)y&255]);
2163
2210
  }
2164
2211
  }
2165
2212
  }
@@ -2190,9 +2237,8 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int
2190
2237
  int i,j,pass;
2191
2238
  Residue *r = f->residue_config + rn;
2192
2239
  int rtype = f->residue_types[rn];
2193
- int c = r->classbook;
2194
- int classwords = f->codebooks[c].dimensions;
2195
- unsigned int actual_size = rtype == 2 ? n*2 : n;
2240
+ int classwords = f->codebooks[r->classbook].dimensions;
2241
+ unsigned int actual_size = rtype == 2 ? n*ch : n;
2196
2242
  unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size);
2197
2243
  unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size);
2198
2244
  int n_read = limit_r_end - limit_r_begin;
@@ -2221,8 +2267,8 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int
2221
2267
  int pcount = 0, class_set = 0;
2222
2268
  if (ch == 2) {
2223
2269
  while (pcount < part_read) {
2224
- int z = r->begin + pcount*r->part_size;
2225
- int c_inter = (z & 1), p_inter = z>>1;
2270
+ int z0 = r->begin + pcount*r->part_size;
2271
+ int c_inter = (z0 & 1), p_inter = z0>>1;
2226
2272
  if (pass == 0) {
2227
2273
  Codebook *c = f->codebooks+r->classbook;
2228
2274
  int q;
@@ -2267,8 +2313,8 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int
2267
2313
  }
2268
2314
  } else if (ch > 2) {
2269
2315
  while (pcount < part_read) {
2270
- int z = r->begin + pcount*r->part_size;
2271
- int c_inter = z % ch, p_inter = z/ch;
2316
+ int z0 = r->begin + pcount*r->part_size;
2317
+ int c_inter = z0 % ch, p_inter = z0 / ch;
2272
2318
  if (pass == 0) {
2273
2319
  Codebook *c = f->codebooks+r->classbook;
2274
2320
  int q;
@@ -2344,9 +2390,8 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int
2344
2390
  if (b >= 0) {
2345
2391
  float *target = residue_buffers[j];
2346
2392
  int offset = r->begin + pcount * r->part_size;
2347
- int n = r->part_size;
2348
2393
  Codebook *book = f->codebooks + b;
2349
- if (!residue_decode(f, book, target, offset, n, rtype))
2394
+ if (!residue_decode(f, book, target, offset, r->part_size, rtype))
2350
2395
  goto done;
2351
2396
  }
2352
2397
  }
@@ -3184,7 +3229,7 @@ static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *f
3184
3229
  if (lx < n2) {
3185
3230
  // optimization of: draw_line(target, lx,ly, n,ly, n2);
3186
3231
  for (j=lx; j < n2; ++j)
3187
- LINE_OP(target[j], inverse_db_table[ly]);
3232
+ LINE_OP(target[j], inverse_db_table[ly & 255]);
3188
3233
  CHECK(f);
3189
3234
  }
3190
3235
  }
@@ -3290,12 +3335,12 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3290
3335
  if (get_bits(f, 1)) {
3291
3336
  short *finalY;
3292
3337
  uint8 step2_flag[256];
3293
- static int range_list[4] = { 256, 128, 86, 64 };
3338
+ static const int range_list[4] = { 256, 128, 86, 64 };
3294
3339
  int range = range_list[g->floor1_multiplier-1];
3295
3340
  int offset = 2;
3296
3341
  finalY = f->finalY[i];
3297
- finalY[0] = get_bits(f, ilog(range)-1);
3298
- finalY[1] = get_bits(f, ilog(range)-1);
3342
+ finalY[0] = (short) get_bits(f, ilog(range)-1);
3343
+ finalY[1] = (short) get_bits(f, ilog(range)-1);
3299
3344
  for (j=0; j < g->partitions; ++j) {
3300
3345
  int pclass = g->partition_class_list[j];
3301
3346
  int cdim = g->class_dimensions[pclass];
@@ -3313,7 +3358,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3313
3358
  int temp;
3314
3359
  Codebook *c = f->codebooks + book;
3315
3360
  DECODE(temp,f,c);
3316
- finalY[offset++] = temp;
3361
+ finalY[offset++] = (short)temp;
3317
3362
  } else
3318
3363
  finalY[offset++] = 0;
3319
3364
  }
@@ -3338,17 +3383,17 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3338
3383
  step2_flag[j] = 1;
3339
3384
  if (val >= room)
3340
3385
  if (highroom > lowroom)
3341
- finalY[j] = val - lowroom + pred;
3386
+ finalY[j] = (short) (val - lowroom + pred);
3342
3387
  else
3343
- finalY[j] = pred - val + highroom - 1;
3388
+ finalY[j] = (short) (pred - val + highroom - 1);
3344
3389
  else
3345
3390
  if (val & 1)
3346
- finalY[j] = pred - ((val+1)>>1);
3391
+ finalY[j] = (short) (pred - ((val+1)>>1));
3347
3392
  else
3348
- finalY[j] = pred + (val>>1);
3393
+ finalY[j] = (short) (pred + (val>>1));
3349
3394
  } else {
3350
3395
  step2_flag[j] = 0;
3351
- finalY[j] = pred;
3396
+ finalY[j] = (short) pred;
3352
3397
  }
3353
3398
  }
3354
3399
 
@@ -3412,23 +3457,22 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start,
3412
3457
 
3413
3458
  // INVERSE COUPLING
3414
3459
  for (i = map->coupling_steps-1; i >= 0; --i) {
3415
- int n2 = n >> 1;
3416
- float *m = f->channel_buffers[map->chan[i].magnitude];
3417
- float *a = f->channel_buffers[map->chan[i].angle ];
3460
+ float *m1 = f->channel_buffers[map->chan[i].magnitude];
3461
+ float *a1 = f->channel_buffers[map->chan[i].angle ];
3418
3462
  for (j=0; j < n2; ++j) {
3419
3463
  float a2,m2;
3420
- if (m[j] > 0)
3421
- if (a[j] > 0)
3422
- m2 = m[j], a2 = m[j] - a[j];
3464
+ if (m1[j] > 0)
3465
+ if (a1[j] > 0)
3466
+ m2 = m1[j], a2 = m1[j] - a1[j];
3423
3467
  else
3424
- a2 = m[j], m2 = m[j] + a[j];
3468
+ a2 = m1[j], m2 = m1[j] + a1[j];
3425
3469
  else
3426
- if (a[j] > 0)
3427
- m2 = m[j], a2 = m[j] + a[j];
3470
+ if (a1[j] > 0)
3471
+ m2 = m1[j], a2 = m1[j] + a1[j];
3428
3472
  else
3429
- a2 = m[j], m2 = m[j] - a[j];
3430
- m[j] = m2;
3431
- a[j] = a2;
3473
+ a2 = m1[j], m2 = m1[j] - a1[j];
3474
+ m1[j] = m2;
3475
+ a1[j] = a2;
3432
3476
  }
3433
3477
  }
3434
3478
  CHECK(f);
@@ -3550,7 +3594,7 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
3550
3594
 
3551
3595
  // mixin from previous window
3552
3596
  if (f->previous_length) {
3553
- int i,j, n = f->previous_length;
3597
+ int n = f->previous_length;
3554
3598
  float *w = get_window(f, n);
3555
3599
  if (w == NULL) return 0;
3556
3600
  for (i=0; i < f->channels; ++i) {
@@ -3590,14 +3634,18 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
3590
3634
  return right - left;
3591
3635
  }
3592
3636
 
3637
+ #ifndef STB_VORBIS_NO_PULLDATA_API
3593
3638
  static int vorbis_pump_first_frame(stb_vorbis *f)
3594
3639
  {
3595
3640
  int len, right, left, res;
3596
3641
  res = vorbis_decode_packet(f, &len, &left, &right);
3597
3642
  if (res)
3598
3643
  vorbis_finish_frame(f, len, left, right);
3644
+ f->current_playback_loc = 0;
3645
+ f->current_playback_loc_valid = TRUE;
3599
3646
  return res;
3600
3647
  }
3648
+ #endif
3601
3649
 
3602
3650
  #ifndef STB_VORBIS_NO_PUSHDATA_API
3603
3651
  static int is_whole_packet_present(stb_vorbis *f)
@@ -3611,7 +3659,7 @@ static int is_whole_packet_present(stb_vorbis *f)
3611
3659
  // of state to restore (primarily the page segment table)
3612
3660
 
3613
3661
  int s = f->next_seg, first = TRUE;
3614
- uint8 *p = f->stream;
3662
+ const uint8 *p = f->stream;
3615
3663
 
3616
3664
  if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag
3617
3665
  for (; s < f->segment_count; ++s) {
@@ -3626,7 +3674,7 @@ static int is_whole_packet_present(stb_vorbis *f)
3626
3674
  first = FALSE;
3627
3675
  }
3628
3676
  for (; s == -1;) {
3629
- uint8 *q;
3677
+ const uint8 *q;
3630
3678
  int n;
3631
3679
 
3632
3680
  // check that we have the page header ready
@@ -3661,6 +3709,8 @@ static int is_whole_packet_present(stb_vorbis *f)
3661
3709
  }
3662
3710
  #endif // !STB_VORBIS_NO_PUSHDATA_API
3663
3711
 
3712
+ #define CODEBOOK_LIMIT (1 << 18)
3713
+
3664
3714
  static int start_decoder(vorb *f)
3665
3715
  {
3666
3716
  uint8 header[6], x,y;
@@ -3713,8 +3763,9 @@ static int start_decoder(vorb *f)
3713
3763
  log1 = x >> 4;
3714
3764
  f->blocksize_0 = 1 << log0;
3715
3765
  f->blocksize_1 = 1 << log1;
3716
- if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup);
3717
- if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup);
3766
+ // actual min is 6, but libvorbis never seems to emit below 8 and this path appears buggy; just bail on those
3767
+ if (log0 < 8 || log0 > 13) return error(f, VORBIS_invalid_setup);
3768
+ if (log1 < 8 || log1 > 13) return error(f, VORBIS_invalid_setup);
3718
3769
  if (log0 > log1) return error(f, VORBIS_invalid_setup);
3719
3770
  }
3720
3771
 
@@ -3731,14 +3782,15 @@ static int start_decoder(vorb *f)
3731
3782
  if (!next_segment(f)) return FALSE;
3732
3783
 
3733
3784
  if (get8_packet(f) != VORBIS_packet_comment) return error(f, VORBIS_invalid_setup);
3734
- for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3785
+ for (i=0; i < 6; ++i) header[i] = (uint8) get8_packet(f);
3735
3786
  if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup);
3736
3787
  //file vendor
3737
3788
  len = get32_packet(f);
3789
+ if (len < 0 || len == INT_MAX) return error(f, VORBIS_invalid_setup);
3738
3790
  f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1));
3739
3791
  if (f->vendor == NULL) return error(f, VORBIS_outofmem);
3740
3792
  for(i=0; i < len; ++i) {
3741
- f->vendor[i] = get8_packet(f);
3793
+ f->vendor[i] = (char) get8_packet(f);
3742
3794
  }
3743
3795
  f->vendor[len] = (char)'\0';
3744
3796
  //user comments
@@ -3746,8 +3798,11 @@ static int start_decoder(vorb *f)
3746
3798
  f->comment_list = NULL;
3747
3799
  if (f->comment_list_length > 0)
3748
3800
  {
3749
- f->comment_list = (char**) setup_malloc(f, sizeof(char*) * (f->comment_list_length));
3750
- if (f->comment_list == NULL) return error(f, VORBIS_outofmem);
3801
+ if (INT_MAX / (int)sizeof(char*) < f->comment_list_length) return error(f, VORBIS_invalid_setup);
3802
+ len = sizeof(char*) * f->comment_list_length;
3803
+ f->comment_list = (char**) setup_malloc(f, len);
3804
+ if (f->comment_list == NULL) { f->comment_list_length = 0; return error(f, VORBIS_outofmem); }
3805
+ memset(f->comment_list, 0, len);
3751
3806
  }
3752
3807
 
3753
3808
  for(i=0; i < f->comment_list_length; ++i) {
@@ -3756,13 +3811,13 @@ static int start_decoder(vorb *f)
3756
3811
  if (f->comment_list[i] == NULL) return error(f, VORBIS_outofmem);
3757
3812
 
3758
3813
  for(j=0; j < len; ++j) {
3759
- f->comment_list[i][j] = get8_packet(f);
3814
+ f->comment_list[i][j] = (char) get8_packet(f);
3760
3815
  }
3761
3816
  f->comment_list[i][len] = (char)'\0';
3762
3817
  }
3763
3818
 
3764
3819
  // framing_flag
3765
- x = get8_packet(f);
3820
+ x = (uint8) get8_packet(f);
3766
3821
  if (!(x & 1)) return error(f, VORBIS_invalid_setup);
3767
3822
 
3768
3823
 
@@ -3793,7 +3848,7 @@ static int start_decoder(vorb *f)
3793
3848
  crc32_init(); // always init it, to avoid multithread race conditions
3794
3849
 
3795
3850
  if (get8_packet(f) != VORBIS_packet_setup) return error(f, VORBIS_invalid_setup);
3796
- for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3851
+ for (i=0; i < 6; ++i) header[i] = (uint8) get8_packet(f);
3797
3852
  if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup);
3798
3853
 
3799
3854
  // codebooks
@@ -3810,18 +3865,19 @@ static int start_decoder(vorb *f)
3810
3865
  uint8 *lengths;
3811
3866
  Codebook *c = f->codebooks+i;
3812
3867
  CHECK(f);
3813
- x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup);
3814
- x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup);
3815
- x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup);
3816
- x = get_bits(f, 8);
3868
+ x = (uint8) get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup);
3869
+ x = (uint8) get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup);
3870
+ x = (uint8) get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup);
3871
+ x = (uint8) get_bits(f, 8);
3817
3872
  c->dimensions = (get_bits(f, 8)<<8) + x;
3818
- x = get_bits(f, 8);
3819
- y = get_bits(f, 8);
3873
+ x = (uint8) get_bits(f, 8);
3874
+ y = (uint8) get_bits(f, 8);
3820
3875
  c->entries = (get_bits(f, 8)<<16) + (y<<8) + x;
3821
3876
  ordered = get_bits(f,1);
3822
- c->sparse = ordered ? 0 : get_bits(f,1);
3877
+ c->sparse = ordered ? 0 : (uint8) get_bits(f,1);
3823
3878
 
3824
3879
  if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup);
3880
+ if (c->entries && CODEBOOK_LIMIT / c->entries < c->dimensions) return error(f, VORBIS_invalid_setup);
3825
3881
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3826
3882
 
3827
3883
  if (c->sparse) {
@@ -3850,7 +3906,7 @@ static int start_decoder(vorb *f)
3850
3906
  int present = c->sparse ? get_bits(f,1) : 1;
3851
3907
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
3852
3908
  if (present) {
3853
- lengths[j] = get_bits(f, 5) + 1;
3909
+ lengths[j] = (uint8) (get_bits(f, 5) + 1);
3854
3910
  ++total;
3855
3911
  if (lengths[j] == 32) return error(f, VORBIS_invalid_setup);
3856
3912
  } else {
@@ -3935,18 +3991,18 @@ static int start_decoder(vorb *f)
3935
3991
  compute_accelerated_huffman(c);
3936
3992
 
3937
3993
  CHECK(f);
3938
- c->lookup_type = get_bits(f, 4);
3994
+ c->lookup_type = (uint8) get_bits(f, 4);
3939
3995
  if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup);
3940
3996
  if (c->lookup_type > 0) {
3941
3997
  uint16 *mults;
3942
3998
  c->minimum_value = float32_unpack(get_bits(f, 32));
3943
3999
  c->delta_value = float32_unpack(get_bits(f, 32));
3944
- c->value_bits = get_bits(f, 4)+1;
3945
- c->sequence_p = get_bits(f,1);
4000
+ c->value_bits = (uint8) (get_bits(f, 4)+1);
4001
+ c->sequence_p = (uint8) get_bits(f,1);
3946
4002
  if (c->lookup_type == 1) {
3947
- int values = lookup1_values(c->entries, c->dimensions);
3948
- if (values < 0) return error(f, VORBIS_invalid_setup);
3949
- c->lookup_values = (uint32) values;
4003
+ int nvalues = lookup1_values(c->entries, c->dimensions);
4004
+ if (nvalues < 0) return error(f, VORBIS_invalid_setup);
4005
+ c->lookup_values = (uint32) nvalues;
3950
4006
  } else {
3951
4007
  /* unsigned multiply to suppress (legitimate) warning.
3952
4008
  * https://github.com/nothings/stb/issues/1168 */
@@ -3959,12 +4015,12 @@ static int start_decoder(vorb *f)
3959
4015
  for (j=0; j < (int) c->lookup_values; ++j) {
3960
4016
  int q = get_bits(f, c->value_bits);
3961
4017
  if (f->valid_bits < 0) return error(f, VORBIS_invalid_setup);
3962
- mults[j] = q;
4018
+ mults[j] = (uint16) q;
3963
4019
  }
3964
4020
 
3965
4021
  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3966
4022
  if (c->lookup_type == 1) {
3967
- int len, sparse = c->sparse;
4023
+ int cblen, sparse = c->sparse;
3968
4024
  float last=0;
3969
4025
  // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
3970
4026
  if (sparse) {
@@ -3973,8 +4029,8 @@ static int start_decoder(vorb *f)
3973
4029
  } else
3974
4030
  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions);
3975
4031
  if (c->multiplicands == NULL) return error(f, VORBIS_outofmem);
3976
- len = sparse ? c->sorted_entries : c->entries;
3977
- for (j=0; j < len; ++j) {
4032
+ cblen = sparse ? c->sorted_entries : c->entries;
4033
+ for (j=0; j < cblen; ++j) {
3978
4034
  unsigned int z = sparse ? c->sorted_values[j] : j;
3979
4035
  unsigned int div=1;
3980
4036
  for (k=0; k < c->dimensions; ++k) {
@@ -4019,7 +4075,7 @@ static int start_decoder(vorb *f)
4019
4075
 
4020
4076
  // time domain transfers (notused)
4021
4077
 
4022
- x = get_bits(f, 6) + 1;
4078
+ x = (uint8) (get_bits(f, 6) + 1);
4023
4079
  for (i=0; i < x; ++i) {
4024
4080
  uint32 z = get_bits(f, 16);
4025
4081
  if (z != 0) return error(f, VORBIS_invalid_setup);
@@ -4031,35 +4087,35 @@ static int start_decoder(vorb *f)
4031
4087
  f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
4032
4088
  if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
4033
4089
  for (i=0; i < f->floor_count; ++i) {
4034
- f->floor_types[i] = get_bits(f, 16);
4090
+ f->floor_types[i] = (uint16) get_bits(f, 16);
4035
4091
  if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup);
4036
4092
  if (f->floor_types[i] == 0) {
4037
4093
  Floor0 *g = &f->floor_config[i].floor0;
4038
- g->order = get_bits(f,8);
4039
- g->rate = get_bits(f,16);
4040
- g->bark_map_size = get_bits(f,16);
4041
- g->amplitude_bits = get_bits(f,6);
4042
- g->amplitude_offset = get_bits(f,8);
4043
- g->number_of_books = get_bits(f,4) + 1;
4094
+ g->order = (uint8) get_bits(f,8);
4095
+ g->rate = (uint16) get_bits(f,16);
4096
+ g->bark_map_size = (uint16) get_bits(f,16);
4097
+ g->amplitude_bits = (uint8) get_bits(f,6);
4098
+ g->amplitude_offset = (uint8) get_bits(f,8);
4099
+ g->number_of_books = (uint8) (get_bits(f,4) + 1);
4044
4100
  for (j=0; j < g->number_of_books; ++j)
4045
- g->book_list[j] = get_bits(f,8);
4101
+ g->book_list[j] = (uint8) get_bits(f,8);
4046
4102
  return error(f, VORBIS_feature_not_supported);
4047
4103
  } else {
4048
4104
  stbv__floor_ordering p[31*8+2];
4049
4105
  Floor1 *g = &f->floor_config[i].floor1;
4050
4106
  int max_class = -1;
4051
- g->partitions = get_bits(f, 5);
4107
+ g->partitions = (uint8) get_bits(f, 5);
4052
4108
  for (j=0; j < g->partitions; ++j) {
4053
- g->partition_class_list[j] = get_bits(f, 4);
4109
+ g->partition_class_list[j] = (uint8) get_bits(f, 4);
4054
4110
  if (g->partition_class_list[j] > max_class)
4055
4111
  max_class = g->partition_class_list[j];
4056
4112
  }
4057
4113
  for (j=0; j <= max_class; ++j) {
4058
- g->class_dimensions[j] = get_bits(f, 3)+1;
4059
- g->class_subclasses[j] = get_bits(f, 2);
4114
+ g->class_dimensions[j] = (uint8) (get_bits(f, 3)+1);
4115
+ g->class_subclasses[j] = (uint8) get_bits(f, 2);
4060
4116
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4061
4117
  if (g->class_subclasses[j]) {
4062
- g->class_masterbooks[j] = get_bits(f, 8);
4118
+ g->class_masterbooks[j] = (uint8) get_bits(f, 8);
4063
4119
  if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4064
4120
  }
4065
4121
  for (k=0; k < 1 << g->class_subclasses[j]; ++k) {
@@ -4067,22 +4123,22 @@ static int start_decoder(vorb *f)
4067
4123
  if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4068
4124
  }
4069
4125
  }
4070
- g->floor1_multiplier = get_bits(f,2)+1;
4071
- g->rangebits = get_bits(f,4);
4126
+ g->floor1_multiplier = (uint8) (get_bits(f,2)+1);
4127
+ g->rangebits = (uint8) get_bits(f,4);
4072
4128
  g->Xlist[0] = 0;
4073
4129
  g->Xlist[1] = 1 << g->rangebits;
4074
4130
  g->values = 2;
4075
4131
  for (j=0; j < g->partitions; ++j) {
4076
4132
  int c = g->partition_class_list[j];
4077
4133
  for (k=0; k < g->class_dimensions[c]; ++k) {
4078
- g->Xlist[g->values] = get_bits(f, g->rangebits);
4134
+ g->Xlist[g->values] = (uint16) get_bits(f, g->rangebits);
4079
4135
  ++g->values;
4080
4136
  }
4081
4137
  }
4082
4138
  // precompute the sorting
4083
4139
  for (j=0; j < g->values; ++j) {
4084
4140
  p[j].x = g->Xlist[j];
4085
- p[j].id = j;
4141
+ p[j].id = (uint16) j;
4086
4142
  }
4087
4143
  qsort(p, g->values, sizeof(p[0]), point_compare);
4088
4144
  for (j=0; j < g->values-1; ++j)
@@ -4094,8 +4150,8 @@ static int start_decoder(vorb *f)
4094
4150
  for (j=2; j < g->values; ++j) {
4095
4151
  int low = 0,hi = 0;
4096
4152
  neighbors(g->Xlist, j, &low,&hi);
4097
- g->neighbors[j][0] = low;
4098
- g->neighbors[j][1] = hi;
4153
+ g->neighbors[j][0] = (uint8) low;
4154
+ g->neighbors[j][1] = (uint8) hi;
4099
4155
  }
4100
4156
 
4101
4157
  if (g->values > longest_floorlist)
@@ -4112,22 +4168,22 @@ static int start_decoder(vorb *f)
4112
4168
  for (i=0; i < f->residue_count; ++i) {
4113
4169
  uint8 residue_cascade[64];
4114
4170
  Residue *r = f->residue_config+i;
4115
- f->residue_types[i] = get_bits(f, 16);
4171
+ f->residue_types[i] = (uint16) get_bits(f, 16);
4116
4172
  if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup);
4117
4173
  r->begin = get_bits(f, 24);
4118
4174
  r->end = get_bits(f, 24);
4119
4175
  if (r->end < r->begin) return error(f, VORBIS_invalid_setup);
4120
4176
  r->part_size = get_bits(f,24)+1;
4121
- r->classifications = get_bits(f,6)+1;
4122
- r->classbook = get_bits(f,8);
4177
+ r->classifications = (uint8) (get_bits(f,6)+1);
4178
+ r->classbook = (uint8) get_bits(f,8);
4123
4179
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4124
4180
  if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4125
4181
  for (j=0; j < r->classifications; ++j) {
4126
4182
  uint8 high_bits=0;
4127
- uint8 low_bits=get_bits(f,3);
4183
+ uint8 low_bits=(uint8) get_bits(f,3);
4128
4184
  if (get_bits(f,1))
4129
- high_bits = get_bits(f,5);
4130
- residue_cascade[j] = high_bits*8 + low_bits;
4185
+ high_bits = (uint8) get_bits(f,5);
4186
+ residue_cascade[j] = (uint8) (high_bits*8 + low_bits);
4131
4187
  }
4132
4188
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4133
4189
  r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
@@ -4135,7 +4191,7 @@ static int start_decoder(vorb *f)
4135
4191
  for (j=0; j < r->classifications; ++j) {
4136
4192
  for (k=0; k < 8; ++k) {
4137
4193
  if (residue_cascade[j] & (1 << k)) {
4138
- r->residue_books[j][k] = get_bits(f, 8);
4194
+ r->residue_books[j][k] = (int16) get_bits(f, 8);
4139
4195
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4140
4196
  if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4141
4197
  } else {
@@ -4172,17 +4228,17 @@ static int start_decoder(vorb *f)
4172
4228
  m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
4173
4229
  if (m->chan == NULL) return error(f, VORBIS_outofmem);
4174
4230
  if (get_bits(f,1))
4175
- m->submaps = get_bits(f,4)+1;
4231
+ m->submaps = (uint8) (get_bits(f,4)+1);
4176
4232
  else
4177
4233
  m->submaps = 1;
4178
4234
  if (m->submaps > max_submaps)
4179
4235
  max_submaps = m->submaps;
4180
4236
  if (get_bits(f,1)) {
4181
- m->coupling_steps = get_bits(f,8)+1;
4237
+ m->coupling_steps = (uint16) (get_bits(f,8)+1);
4182
4238
  if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup);
4183
4239
  for (k=0; k < m->coupling_steps; ++k) {
4184
- m->chan[k].magnitude = get_bits(f, ilog(f->channels-1));
4185
- m->chan[k].angle = get_bits(f, ilog(f->channels-1));
4240
+ m->chan[k].magnitude = (uint8) get_bits(f, ilog(f->channels-1));
4241
+ m->chan[k].angle = (uint8) get_bits(f, ilog(f->channels-1));
4186
4242
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4187
4243
  if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup);
4188
4244
  if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup);
@@ -4195,7 +4251,7 @@ static int start_decoder(vorb *f)
4195
4251
  if (get_bits(f,2)) return error(f, VORBIS_invalid_setup);
4196
4252
  if (m->submaps > 1) {
4197
4253
  for (j=0; j < f->channels; ++j) {
4198
- m->chan[j].mux = get_bits(f, 4);
4254
+ m->chan[j].mux = (uint8) get_bits(f, 4);
4199
4255
  if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup);
4200
4256
  }
4201
4257
  } else
@@ -4204,9 +4260,9 @@ static int start_decoder(vorb *f)
4204
4260
  m->chan[j].mux = 0;
4205
4261
 
4206
4262
  for (j=0; j < m->submaps; ++j) {
4207
- get_bits(f,8); // discard
4208
- m->submap_floor[j] = get_bits(f,8);
4209
- m->submap_residue[j] = get_bits(f,8);
4263
+ (void)get_bits(f,8); // discard
4264
+ m->submap_floor[j] = (uint8) get_bits(f,8);
4265
+ m->submap_residue[j] = (uint8) get_bits(f,8);
4210
4266
  if (m->submap_floor[j] >= f->floor_count) return error(f, VORBIS_invalid_setup);
4211
4267
  if (m->submap_residue[j] >= f->residue_count) return error(f, VORBIS_invalid_setup);
4212
4268
  }
@@ -4216,10 +4272,10 @@ static int start_decoder(vorb *f)
4216
4272
  f->mode_count = get_bits(f, 6)+1;
4217
4273
  for (i=0; i < f->mode_count; ++i) {
4218
4274
  Mode *m = f->mode_config+i;
4219
- m->blockflag = get_bits(f,1);
4220
- m->windowtype = get_bits(f,16);
4221
- m->transformtype = get_bits(f,16);
4222
- m->mapping = get_bits(f,8);
4275
+ m->blockflag = (uint8) get_bits(f,1);
4276
+ m->windowtype = (uint16) get_bits(f,16);
4277
+ m->transformtype = (uint16) get_bits(f,16);
4278
+ m->mapping = (uint8) get_bits(f,8);
4223
4279
  if (f->valid_bits < 0) return error(f, VORBIS_unexpected_eof);
4224
4280
  if (m->windowtype != 0) return error(f, VORBIS_invalid_setup);
4225
4281
  if (m->transformtype != 0) return error(f, VORBIS_invalid_setup);
@@ -4260,10 +4316,10 @@ static int start_decoder(vorb *f)
4260
4316
  {
4261
4317
  uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1);
4262
4318
  uint32 classify_mem;
4263
- int i,max_part_read=0;
4319
+ int max_part_read=0;
4264
4320
  for (i=0; i < f->residue_count; ++i) {
4265
4321
  Residue *r = f->residue_config + i;
4266
- unsigned int actual_size = f->blocksize_1 / 2;
4322
+ unsigned int actual_size = (f->blocksize_1 * (f->residue_types[i] == 2 ? f->channels : 1)) / 2;
4267
4323
  unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size;
4268
4324
  unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size;
4269
4325
  int n_read = limit_r_end - limit_r_begin;
@@ -4378,8 +4434,8 @@ static void vorbis_deinit(stb_vorbis *p)
4378
4434
  setup_temp_free(p, &p->temp_values, 0);
4379
4435
  setup_temp_free(p, &p->temp_mults, 0);
4380
4436
  }
4381
- #ifdef __SDL_SOUND_INTERNAL__
4382
- if (p->close_on_free) SDL_RWclose(p->rwops);
4437
+ #ifdef STB_VORBIS_SDL
4438
+ if (p->close_on_free) SDL_CloseIO(p->io);
4383
4439
  #endif
4384
4440
  #ifndef STB_VORBIS_NO_STDIO
4385
4441
  if (p->close_on_free) fclose(p->f);
@@ -4406,9 +4462,13 @@ static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
4406
4462
  p->stream = NULL;
4407
4463
  p->codebooks = NULL;
4408
4464
  p->page_crc_tests = -1;
4409
- #ifdef __SDL_SOUND_INTERNAL__
4465
+ #ifdef STB_VORBIS_SDL
4410
4466
  p->close_on_free = FALSE;
4411
- p->rwops = NULL;
4467
+ p->io = NULL;
4468
+ p->io_start = 0;
4469
+ p->io_virtual_pos = 0;
4470
+ p->io_buffer_pos = 0;
4471
+ p->io_buffer_fill = 0;
4412
4472
  #endif
4413
4473
  #ifndef STB_VORBIS_NO_STDIO
4414
4474
  p->close_on_free = FALSE;
@@ -4424,6 +4484,14 @@ int stb_vorbis_get_sample_offset(stb_vorbis *f)
4424
4484
  return -1;
4425
4485
  }
4426
4486
 
4487
+ int stb_vorbis_get_playback_sample_offset(stb_vorbis *f)
4488
+ {
4489
+ if (f->current_playback_loc_valid)
4490
+ return f->current_playback_loc;
4491
+ else
4492
+ return -1;
4493
+ }
4494
+
4427
4495
  stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
4428
4496
  {
4429
4497
  stb_vorbis_info d;
@@ -4474,9 +4542,9 @@ void stb_vorbis_flush_pushdata(stb_vorbis *f)
4474
4542
  f->channel_buffer_end = 0;
4475
4543
  }
4476
4544
 
4477
- static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len)
4545
+ static int vorbis_search_for_page_pushdata(vorb *f, const uint8 *data, int data_len)
4478
4546
  {
4479
- int i,n;
4547
+ int i;
4480
4548
  for (i=0; i < f->page_crc_tests; ++i)
4481
4549
  f->scan[i].bytes_done = 0;
4482
4550
 
@@ -4489,7 +4557,7 @@ static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len)
4489
4557
  for (i=0; i < data_len; ++i) {
4490
4558
  if (data[i] == 0x4f) {
4491
4559
  if (0==memcmp(data+i, ogg_page_header, 4)) {
4492
- int j,len;
4560
+ int j,n,len;
4493
4561
  uint32 crc;
4494
4562
  // make sure we have the whole page header
4495
4563
  if (i+26 >= data_len || i+27+data[i+26] >= data_len) {
@@ -4517,7 +4585,7 @@ static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len)
4517
4585
  // if the last frame on a page is continued to the next, then
4518
4586
  // we can't recover the sample_loc immediately
4519
4587
  if (data[i+27+data[i+26]-1] == 255)
4520
- f->scan[n].sample_loc = ~0;
4588
+ f->scan[n].sample_loc = ~0u;
4521
4589
  else
4522
4590
  f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24);
4523
4591
  f->scan[n].bytes_done = i+j;
@@ -4580,11 +4648,11 @@ int stb_vorbis_decode_frame_pushdata(
4580
4648
 
4581
4649
  if (f->page_crc_tests >= 0) {
4582
4650
  *samples = 0;
4583
- return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len);
4651
+ return vorbis_search_for_page_pushdata(f, data, data_len);
4584
4652
  }
4585
4653
 
4586
- f->stream = (uint8 *) data;
4587
- f->stream_end = (uint8 *) data + data_len;
4654
+ f->stream = (const uint8 *) data;
4655
+ f->stream_end = (const uint8 *) data + data_len;
4588
4656
  f->error = VORBIS__no_error;
4589
4657
 
4590
4658
  // check that we have the entire packet in memory
@@ -4642,8 +4710,8 @@ stb_vorbis *stb_vorbis_open_pushdata(
4642
4710
  {
4643
4711
  stb_vorbis *f, p;
4644
4712
  vorbis_init(&p, alloc);
4645
- p.stream = (uint8 *) data;
4646
- p.stream_end = (uint8 *) data + data_len;
4713
+ p.stream = (const uint8 *) data;
4714
+ p.stream_end = (const uint8 *) data + data_len;
4647
4715
  p.push_mode = TRUE;
4648
4716
  if (!start_decoder(&p)) {
4649
4717
  if (p.eof)
@@ -4671,14 +4739,15 @@ unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
4671
4739
  #ifndef STB_VORBIS_NO_PUSHDATA_API
4672
4740
  if (f->push_mode) return 0;
4673
4741
  #endif
4674
- #ifdef __SDL_SOUND_INTERNAL__
4675
- return (unsigned int) (SDL_RWtell(f->rwops) - f->rwops_start);
4676
- #else
4742
+ #ifdef STB_VORBIS_SDL
4743
+ return f->io_virtual_pos;
4744
+ #else /**/
4677
4745
  if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
4678
- #endif
4679
4746
  #ifndef STB_VORBIS_NO_STDIO
4680
4747
  return (unsigned int) (ftell(f->f) - f->f_start);
4681
4748
  #endif
4749
+ return 0; /* silence warnings */
4750
+ #endif /* !STB_VORBIS_SDL */
4682
4751
  }
4683
4752
 
4684
4753
  #ifndef STB_VORBIS_NO_PULLDATA_API
@@ -4694,7 +4763,7 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last)
4694
4763
  n = get8(f);
4695
4764
  if (n == 0x4f) { // page header candidate
4696
4765
  unsigned int retry_loc = stb_vorbis_get_file_offset(f);
4697
- int i;
4766
+ uint32 i;
4698
4767
  // check if we're off the end of a file_section stream
4699
4768
  if (retry_loc - 25 > f->stream_len)
4700
4769
  return 0;
@@ -4705,7 +4774,7 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last)
4705
4774
  if (f->eof) return 0;
4706
4775
  if (i == 4) {
4707
4776
  uint8 header[27];
4708
- uint32 i, crc, goal, len;
4777
+ uint32 crc, goal, len;
4709
4778
  for (i=0; i < 4; ++i)
4710
4779
  header[i] = ogg_page_header[i];
4711
4780
  for (; i < 27; ++i)
@@ -4720,7 +4789,7 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last)
4720
4789
  crc = crc32_update(crc, header[i]);
4721
4790
  len = 0;
4722
4791
  for (i=0; i < header[26]; ++i) {
4723
- int s = get8(f);
4792
+ uint8 s = get8(f);
4724
4793
  crc = crc32_update(crc, s);
4725
4794
  len += s;
4726
4795
  }
@@ -4870,8 +4939,11 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4870
4939
  return 0;
4871
4940
  }
4872
4941
 
4942
+ mid = left; // just make sure it's initialized to quiet some warnings
4943
+
4873
4944
  while (left.page_end != right.page_start) {
4874
- assert(left.page_end < right.page_start);
4945
+ if (left.page_end >= right.page_start)
4946
+ return error(f, VORBIS_seek_failed);
4875
4947
  // search range in bytes
4876
4948
  delta = right.page_start - left.page_end;
4877
4949
  if (delta <= 65536) {
@@ -4996,7 +5068,7 @@ static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int
4996
5068
  bits_read += 2;
4997
5069
  bytes_read = (bits_read + 7) / 8;
4998
5070
 
4999
- f->bytes_in_seg += bytes_read;
5071
+ f->bytes_in_seg += (uint8) bytes_read;
5000
5072
  f->packet_bytes -= bytes_read;
5001
5073
  skip(f, -bytes_read);
5002
5074
  if (f->next_seg == -1)
@@ -5044,23 +5116,29 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
5044
5116
  }
5045
5117
  // the next frame should start with the sample
5046
5118
  if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);
5119
+ f->current_playback_loc = sample_number;
5047
5120
  return 1;
5048
5121
  }
5049
5122
 
5050
5123
  int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
5051
5124
  {
5052
- if (!stb_vorbis_seek_frame(f, sample_number))
5125
+ if (!stb_vorbis_seek_frame(f, sample_number)) {
5126
+ f->current_playback_loc_valid = FALSE;
5053
5127
  return 0;
5128
+ }
5054
5129
 
5055
5130
  if (sample_number != f->current_loc) {
5056
5131
  int n;
5057
5132
  uint32 frame_start = f->current_loc;
5058
- stb_vorbis_get_frame_float(f, &n, NULL);
5133
+ (void)stb_vorbis_get_frame_float(f, &n, NULL);
5059
5134
  assert(sample_number > frame_start);
5060
5135
  assert(f->channel_buffer_start + (int) (sample_number-frame_start) <= f->channel_buffer_end);
5061
5136
  f->channel_buffer_start += (sample_number - frame_start);
5062
5137
  }
5063
5138
 
5139
+ f->current_playback_loc_valid = TRUE;
5140
+ f->current_playback_loc = sample_number;
5141
+
5064
5142
  return 1;
5065
5143
  }
5066
5144
 
@@ -5227,13 +5305,13 @@ stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const st
5227
5305
  }
5228
5306
  #endif // STB_VORBIS_NO_STDIO
5229
5307
 
5230
- #ifdef __SDL_SOUND_INTERNAL__
5231
- stb_vorbis * stb_vorbis_open_rwops_section(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length)
5308
+ #ifdef STB_VORBIS_SDL
5309
+ stb_vorbis * stb_vorbis_open_io_section(SDL_IOStream *io, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length)
5232
5310
  {
5233
5311
  stb_vorbis *f, p;
5234
5312
  vorbis_init(&p, alloc);
5235
- p.rwops = rwops;
5236
- p.rwops_start = (uint32) SDL_RWtell(rwops);
5313
+ p.io = io;
5314
+ p.io_start = (uint32) SDL_TellIO(io);
5237
5315
  p.stream_len = length;
5238
5316
  p.close_on_free = close_on_free;
5239
5317
  if (start_decoder(&p)) {
@@ -5249,13 +5327,13 @@ stb_vorbis * stb_vorbis_open_rwops_section(SDL_RWops *rwops, int close_on_free,
5249
5327
  return NULL;
5250
5328
  }
5251
5329
 
5252
- stb_vorbis * stb_vorbis_open_rwops(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc)
5330
+ stb_vorbis * stb_vorbis_open_io(SDL_IOStream *io, int close_on_free, int *error, const stb_vorbis_alloc *alloc)
5253
5331
  {
5254
- const unsigned int start = (unsigned int) SDL_RWtell(rwops);
5255
- const unsigned int len = (unsigned int) (SDL_RWsize(rwops) - start);
5256
- return stb_vorbis_open_rwops_section(rwops, close_on_free, error, alloc, len);
5332
+ const unsigned int start = (unsigned int) SDL_TellIO(io);
5333
+ const unsigned int len = (unsigned int) (SDL_GetIOSize(io) - start);
5334
+ return stb_vorbis_open_io_section(io, close_on_free, error, alloc, len);
5257
5335
  }
5258
- #endif
5336
+ #endif /* STB_VORBIS_SDL */
5259
5337
 
5260
5338
  stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc)
5261
5339
  {
@@ -5265,9 +5343,9 @@ stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *err
5265
5343
  return NULL;
5266
5344
  }
5267
5345
  vorbis_init(&p, alloc);
5268
- p.stream = (uint8 *) data;
5269
- p.stream_end = (uint8 *) data + len;
5270
- p.stream_start = (uint8 *) p.stream;
5346
+ p.stream = (const uint8 *) data;
5347
+ p.stream_end = (const uint8 *) data + len;
5348
+ p.stream_start = (const uint8 *) p.stream;
5271
5349
  p.stream_len = len;
5272
5350
  p.push_mode = FALSE;
5273
5351
  if (start_decoder(&p)) {
@@ -5284,6 +5362,21 @@ stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *err
5284
5362
  return NULL;
5285
5363
  }
5286
5364
 
5365
+ static int fixup_current_playback_loc(stb_vorbis *f, int n)
5366
+ {
5367
+ unsigned int lgs;
5368
+
5369
+ f->current_playback_loc += n;
5370
+ lgs = stb_vorbis_stream_length_in_samples(f);
5371
+ if (lgs != 0 && lgs != SAMPLE_unknown && f->current_playback_loc > (int)lgs) {
5372
+ int r = n - (f->current_playback_loc - (int)lgs);
5373
+ f->current_playback_loc = lgs;
5374
+ return r;
5375
+ }
5376
+
5377
+ return n;
5378
+ }
5379
+
5287
5380
  #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
5288
5381
  #define PLAYBACK_MONO 1
5289
5382
  #define PLAYBACK_LEFT 2
@@ -5293,7 +5386,7 @@ stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *err
5293
5386
  #define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO)
5294
5387
  #define R (PLAYBACK_RIGHT | PLAYBACK_MONO)
5295
5388
 
5296
- static int8 channel_position[7][6] =
5389
+ static const int8 channel_position[7][6] =
5297
5390
  {
5298
5391
  { 0 },
5299
5392
  { C },
@@ -5304,38 +5397,20 @@ static int8 channel_position[7][6] =
5304
5397
  { L, C, R, L, R, C },
5305
5398
  };
5306
5399
 
5307
-
5308
- #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
5309
- typedef union {
5310
- float f;
5311
- // changed this to unsigned to suppress an UBSan error.
5312
- // upstream: https://github.com/nothings/stb/issues/1168.
5313
- unsigned int i;
5314
- } float_conv;
5315
- typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
5316
- #define FASTDEF(x) float_conv x
5317
- // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
5318
- #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
5319
- #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
5320
- #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (int)(temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
5321
- #define check_endianness()
5322
- #else
5323
- #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
5324
- #define check_endianness()
5325
- #define FASTDEF(x)
5326
- #endif
5400
+ STB_FORCEINLINE short float_to_sample(float f)
5401
+ {
5402
+ // clamp f to [-1,1]
5403
+ f = (f < 1.0f) ? f : 1.0f;
5404
+ f = (f > -1.0f) ? f : -1.0f;
5405
+ // convert with rounding
5406
+ return (short)(f * 32767.0f + (f > 0.0f ? 0.5f : -0.5f));
5407
+ }
5327
5408
 
5328
5409
  static void copy_samples(short *dest, float *src, int len)
5329
5410
  {
5330
5411
  int i;
5331
- check_endianness();
5332
- for (i=0; i < len; ++i) {
5333
- FASTDEF(temp);
5334
- int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
5335
- if ((unsigned int)v + 32768 > 65535)
5336
- v = v < 0 ? -32768 : 32767;
5337
- dest[i] = v;
5338
- }
5412
+ for (i=0; i < len; ++i)
5413
+ dest[i] = float_to_sample(src[i]);
5339
5414
  }
5340
5415
 
5341
5416
  static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
@@ -5343,7 +5418,6 @@ static void compute_samples(int mask, short *output, int num_c, float **data, in
5343
5418
  #define STB_BUFFER_SIZE 32
5344
5419
  float buffer[STB_BUFFER_SIZE];
5345
5420
  int i,j,o,n = STB_BUFFER_SIZE;
5346
- check_endianness();
5347
5421
  for (o = 0; o < len; o += STB_BUFFER_SIZE) {
5348
5422
  memset(buffer, 0, sizeof(buffer));
5349
5423
  if (o + n > len) n = len - o;
@@ -5353,13 +5427,8 @@ static void compute_samples(int mask, short *output, int num_c, float **data, in
5353
5427
  buffer[i] += data[j][d_offset+o+i];
5354
5428
  }
5355
5429
  }
5356
- for (i=0; i < n; ++i) {
5357
- FASTDEF(temp);
5358
- int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5359
- if ((unsigned int)v + 32768 > 65535)
5360
- v = v < 0 ? -32768 : 32767;
5361
- output[o+i] = v;
5362
- }
5430
+ for (i=0; i < n; ++i)
5431
+ output[o+i] = float_to_sample(buffer[i]);
5363
5432
  }
5364
5433
  #undef STB_BUFFER_SIZE
5365
5434
  }
@@ -5370,7 +5439,6 @@ static void compute_stereo_samples(short *output, int num_c, float **data, int d
5370
5439
  float buffer[STB_BUFFER_SIZE];
5371
5440
  int i,j,o,n = STB_BUFFER_SIZE >> 1;
5372
5441
  // o is the offset in the source data
5373
- check_endianness();
5374
5442
  for (o = 0; o < len; o += STB_BUFFER_SIZE >> 1) {
5375
5443
  // o2 is the offset in the output data
5376
5444
  int o2 = o << 1;
@@ -5393,13 +5461,8 @@ static void compute_stereo_samples(short *output, int num_c, float **data, int d
5393
5461
  }
5394
5462
  }
5395
5463
  }
5396
- for (i=0; i < (n<<1); ++i) {
5397
- FASTDEF(temp);
5398
- int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5399
- if ((unsigned int)v + 32768 > 65535)
5400
- v = v < 0 ? -32768 : 32767;
5401
- output[o2+i] = v;
5402
- }
5464
+ for (i=0; i < (n<<1); ++i)
5465
+ output[o2+i] = float_to_sample(buffer[i]);
5403
5466
  }
5404
5467
  #undef STB_BUFFER_SIZE
5405
5468
  }
@@ -5408,7 +5471,7 @@ static void convert_samples_short(int buf_c, short **buffer, int b_offset, int d
5408
5471
  {
5409
5472
  int i;
5410
5473
  if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5411
- static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
5474
+ static const int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
5412
5475
  for (i=0; i < buf_c; ++i)
5413
5476
  compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples);
5414
5477
  } else {
@@ -5433,23 +5496,15 @@ int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num
5433
5496
  static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len)
5434
5497
  {
5435
5498
  int i;
5436
- check_endianness();
5437
- if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5438
- assert(buf_c == 2);
5499
+ if (buf_c != data_c && buf_c == 2 && data_c <= 6) {
5439
5500
  for (i=0; i < buf_c; ++i)
5440
5501
  compute_stereo_samples(buffer, data_c, data, d_offset, len);
5441
5502
  } else {
5442
5503
  int limit = buf_c < data_c ? buf_c : data_c;
5443
5504
  int j;
5444
5505
  for (j=0; j < len; ++j) {
5445
- for (i=0; i < limit; ++i) {
5446
- FASTDEF(temp);
5447
- float f = data[i][d_offset+j];
5448
- int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15);
5449
- if ((unsigned int)v + 32768 > 65535)
5450
- v = v < 0 ? -32768 : 32767;
5451
- *buffer++ = v;
5452
- }
5506
+ for (i=0; i < limit; ++i)
5507
+ *buffer++ = float_to_sample(data[i][d_offset+j]);
5453
5508
  for ( ; i < buf_c; ++i)
5454
5509
  *buffer++ = 0;
5455
5510
  }
@@ -5485,7 +5540,7 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short
5485
5540
  if (n == len) break;
5486
5541
  if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
5487
5542
  }
5488
- return n;
5543
+ return fixup_current_playback_loc(f, n);
5489
5544
  }
5490
5545
 
5491
5546
  int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len)
@@ -5502,7 +5557,7 @@ int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, in
5502
5557
  if (n == len) break;
5503
5558
  if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
5504
5559
  }
5505
- return n;
5560
+ return fixup_current_playback_loc(f, n);
5506
5561
  }
5507
5562
 
5508
5563
  #ifndef STB_VORBIS_NO_STDIO
@@ -5610,7 +5665,7 @@ int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float
5610
5665
  if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
5611
5666
  break;
5612
5667
  }
5613
- return n;
5668
+ return fixup_current_playback_loc(f, n);
5614
5669
  }
5615
5670
 
5616
5671
  int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples)
@@ -5636,7 +5691,7 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in
5636
5691
  if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
5637
5692
  break;
5638
5693
  }
5639
- return n;
5694
+ return fixup_current_playback_loc(f, n);
5640
5695
  }
5641
5696
  #endif // STB_VORBIS_NO_PULLDATA_API
5642
5697