gosu 0.15.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/COPYING +1 -1
  4. data/dependencies/SDL/include/SDL.h +138 -0
  5. data/dependencies/SDL/include/SDL_assert.h +293 -0
  6. data/dependencies/SDL/include/SDL_atomic.h +295 -0
  7. data/dependencies/SDL/include/SDL_audio.h +859 -0
  8. data/dependencies/SDL/include/SDL_bits.h +121 -0
  9. data/dependencies/SDL/include/SDL_blendmode.h +123 -0
  10. data/dependencies/SDL/include/SDL_clipboard.h +71 -0
  11. data/dependencies/SDL/include/SDL_config.h +55 -0
  12. data/dependencies/SDL/include/SDL_config_android.h +182 -0
  13. data/dependencies/SDL/include/SDL_config_iphoneos.h +207 -0
  14. data/dependencies/SDL/include/SDL_config_macosx.h +266 -0
  15. data/dependencies/SDL/include/SDL_config_minimal.h +85 -0
  16. data/dependencies/SDL/include/SDL_config_os2.h +188 -0
  17. data/dependencies/SDL/include/SDL_config_pandora.h +135 -0
  18. data/dependencies/SDL/include/SDL_config_psp.h +165 -0
  19. data/dependencies/SDL/include/SDL_config_windows.h +288 -0
  20. data/dependencies/SDL/include/SDL_config_winrt.h +243 -0
  21. data/dependencies/SDL/include/SDL_config_wiz.h +149 -0
  22. data/dependencies/SDL/include/SDL_copying.h +20 -0
  23. data/dependencies/SDL/include/SDL_cpuinfo.h +299 -0
  24. data/dependencies/SDL/include/SDL_egl.h +1676 -0
  25. data/dependencies/SDL/include/SDL_endian.h +263 -0
  26. data/dependencies/SDL/include/SDL_error.h +112 -0
  27. data/dependencies/SDL/include/SDL_events.h +827 -0
  28. data/dependencies/SDL/include/SDL_filesystem.h +136 -0
  29. data/dependencies/SDL/include/SDL_gamecontroller.h +541 -0
  30. data/dependencies/SDL/include/SDL_gesture.h +87 -0
  31. data/dependencies/SDL/include/SDL_haptic.h +1247 -0
  32. data/dependencies/SDL/include/SDL_hints.h +1578 -0
  33. data/dependencies/SDL/include/SDL_joystick.h +499 -0
  34. data/dependencies/SDL/include/SDL_keyboard.h +217 -0
  35. data/dependencies/SDL/include/SDL_keycode.h +351 -0
  36. data/dependencies/SDL/include/SDL_loadso.h +81 -0
  37. data/dependencies/SDL/include/SDL_locale.h +101 -0
  38. data/dependencies/SDL/include/SDL_log.h +211 -0
  39. data/dependencies/SDL/include/SDL_main.h +180 -0
  40. data/dependencies/SDL/include/SDL_messagebox.h +146 -0
  41. data/dependencies/SDL/include/SDL_metal.h +117 -0
  42. data/dependencies/SDL/include/SDL_misc.h +75 -0
  43. data/dependencies/SDL/include/SDL_mouse.h +302 -0
  44. data/dependencies/SDL/include/SDL_mutex.h +251 -0
  45. data/dependencies/SDL/include/SDL_name.h +33 -0
  46. data/dependencies/SDL/include/SDL_opengl.h +2183 -0
  47. data/dependencies/SDL/include/SDL_opengl_glext.h +11180 -0
  48. data/dependencies/SDL/include/SDL_opengles.h +39 -0
  49. data/dependencies/SDL/include/SDL_opengles2.h +52 -0
  50. data/dependencies/SDL/include/SDL_opengles2_gl2.h +621 -0
  51. data/dependencies/SDL/include/SDL_opengles2_gl2ext.h +2050 -0
  52. data/dependencies/SDL/include/SDL_opengles2_gl2platform.h +30 -0
  53. data/dependencies/SDL/include/SDL_opengles2_khrplatform.h +282 -0
  54. data/dependencies/SDL/include/SDL_pixels.h +479 -0
  55. data/dependencies/SDL/include/SDL_platform.h +198 -0
  56. data/dependencies/SDL/include/SDL_power.h +75 -0
  57. data/dependencies/SDL/include/SDL_quit.h +58 -0
  58. data/dependencies/SDL/include/SDL_rect.h +174 -0
  59. data/dependencies/SDL/include/SDL_render.h +1158 -0
  60. data/dependencies/SDL/include/SDL_revision.h +2 -0
  61. data/dependencies/SDL/include/SDL_rwops.h +283 -0
  62. data/dependencies/SDL/include/SDL_scancode.h +413 -0
  63. data/dependencies/SDL/include/SDL_sensor.h +267 -0
  64. data/dependencies/SDL/include/SDL_shape.h +144 -0
  65. data/dependencies/SDL/include/SDL_stdinc.h +647 -0
  66. data/dependencies/SDL/include/SDL_surface.h +563 -0
  67. data/dependencies/SDL/include/SDL_system.h +325 -0
  68. data/dependencies/SDL/include/SDL_syswm.h +354 -0
  69. data/dependencies/SDL/include/SDL_test.h +69 -0
  70. data/dependencies/SDL/include/SDL_test_assert.h +105 -0
  71. data/dependencies/SDL/include/SDL_test_common.h +218 -0
  72. data/dependencies/SDL/include/SDL_test_compare.h +69 -0
  73. data/dependencies/SDL/include/SDL_test_crc32.h +124 -0
  74. data/dependencies/SDL/include/SDL_test_font.h +81 -0
  75. data/dependencies/SDL/include/SDL_test_fuzzer.h +384 -0
  76. data/dependencies/SDL/include/SDL_test_harness.h +134 -0
  77. data/dependencies/SDL/include/SDL_test_images.h +78 -0
  78. data/dependencies/SDL/include/SDL_test_log.h +67 -0
  79. data/dependencies/SDL/include/SDL_test_md5.h +129 -0
  80. data/dependencies/SDL/include/SDL_test_memory.h +63 -0
  81. data/dependencies/SDL/include/SDL_test_random.h +115 -0
  82. data/dependencies/SDL/include/SDL_thread.h +366 -0
  83. data/dependencies/SDL/include/SDL_timer.h +115 -0
  84. data/dependencies/SDL/include/SDL_touch.h +102 -0
  85. data/dependencies/SDL/include/SDL_types.h +29 -0
  86. data/dependencies/SDL/include/SDL_version.h +162 -0
  87. data/dependencies/SDL/include/SDL_video.h +1282 -0
  88. data/dependencies/SDL/include/SDL_vulkan.h +276 -0
  89. data/dependencies/SDL/include/begin_code.h +166 -0
  90. data/dependencies/SDL/include/close_code.h +40 -0
  91. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  92. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  93. data/dependencies/SDL_sound/SDL_sound.c +795 -0
  94. data/dependencies/SDL_sound/SDL_sound.h +725 -0
  95. data/dependencies/SDL_sound/SDL_sound_aiff.c +537 -0
  96. data/dependencies/SDL_sound/SDL_sound_au.c +352 -0
  97. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +747 -0
  98. data/dependencies/SDL_sound/SDL_sound_flac.c +182 -0
  99. data/dependencies/SDL_sound/SDL_sound_internal.h +304 -0
  100. data/dependencies/SDL_sound/SDL_sound_modplug.c +228 -0
  101. data/dependencies/SDL_sound/SDL_sound_mp3.c +184 -0
  102. data/dependencies/SDL_sound/SDL_sound_raw.c +164 -0
  103. data/dependencies/SDL_sound/SDL_sound_shn.c +1309 -0
  104. data/dependencies/SDL_sound/SDL_sound_voc.c +550 -0
  105. data/dependencies/SDL_sound/SDL_sound_vorbis.c +223 -0
  106. data/dependencies/SDL_sound/SDL_sound_wav.c +783 -0
  107. data/dependencies/SDL_sound/dr_flac.h +5906 -0
  108. data/dependencies/SDL_sound/dr_mp3.h +2832 -0
  109. data/dependencies/SDL_sound/libmodplug/fastmix.c +1748 -0
  110. data/dependencies/SDL_sound/libmodplug/libmodplug.h +1001 -0
  111. data/dependencies/SDL_sound/libmodplug/load_669.c +188 -0
  112. data/dependencies/SDL_sound/libmodplug/load_abc.c +4725 -0
  113. data/dependencies/SDL_sound/libmodplug/load_amf.c +403 -0
  114. data/dependencies/SDL_sound/libmodplug/load_ams.c +587 -0
  115. data/dependencies/SDL_sound/libmodplug/load_dbm.c +357 -0
  116. data/dependencies/SDL_sound/libmodplug/load_dmf.c +531 -0
  117. data/dependencies/SDL_sound/libmodplug/load_dsm.c +232 -0
  118. data/dependencies/SDL_sound/libmodplug/load_far.c +253 -0
  119. data/dependencies/SDL_sound/libmodplug/load_it.c +796 -0
  120. data/dependencies/SDL_sound/libmodplug/load_mdl.c +488 -0
  121. data/dependencies/SDL_sound/libmodplug/load_med.c +757 -0
  122. data/dependencies/SDL_sound/libmodplug/load_mid.c +1405 -0
  123. data/dependencies/SDL_sound/libmodplug/load_mod.c +269 -0
  124. data/dependencies/SDL_sound/libmodplug/load_mt2.c +546 -0
  125. data/dependencies/SDL_sound/libmodplug/load_mtm.c +142 -0
  126. data/dependencies/SDL_sound/libmodplug/load_okt.c +192 -0
  127. data/dependencies/SDL_sound/libmodplug/load_pat.c +1143 -0
  128. data/dependencies/SDL_sound/libmodplug/load_pat.h +25 -0
  129. data/dependencies/SDL_sound/libmodplug/load_psm.c +350 -0
  130. data/dependencies/SDL_sound/libmodplug/load_ptm.c +204 -0
  131. data/dependencies/SDL_sound/libmodplug/load_s3m.c +325 -0
  132. data/dependencies/SDL_sound/libmodplug/load_stm.c +180 -0
  133. data/dependencies/SDL_sound/libmodplug/load_ult.c +206 -0
  134. data/dependencies/SDL_sound/libmodplug/load_umx.c +51 -0
  135. data/dependencies/SDL_sound/libmodplug/load_xm.c +554 -0
  136. data/dependencies/SDL_sound/libmodplug/mmcmp.c +382 -0
  137. data/dependencies/SDL_sound/libmodplug/modplug.c +170 -0
  138. data/dependencies/SDL_sound/libmodplug/modplug.h +90 -0
  139. data/dependencies/SDL_sound/libmodplug/snd_dsp.c +301 -0
  140. data/dependencies/SDL_sound/libmodplug/snd_flt.c +63 -0
  141. data/dependencies/SDL_sound/libmodplug/snd_fx.c +2350 -0
  142. data/dependencies/SDL_sound/libmodplug/sndfile.c +1169 -0
  143. data/dependencies/SDL_sound/libmodplug/sndmix.c +1034 -0
  144. data/dependencies/SDL_sound/libmodplug/tables.h +371 -0
  145. data/{src/stb_vorbis.c → dependencies/SDL_sound/stb_vorbis.h} +128 -23
  146. data/dependencies/al_soft/AL/al.h +655 -0
  147. data/dependencies/al_soft/AL/alc.h +270 -0
  148. data/dependencies/al_soft/AL/alext.h +585 -0
  149. data/dependencies/al_soft/AL/efx-creative.h +3 -0
  150. data/dependencies/al_soft/AL/efx-presets.h +402 -0
  151. data/dependencies/al_soft/AL/efx.h +762 -0
  152. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  153. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  154. data/{src → dependencies/stb}/stb_image.h +476 -176
  155. data/{src → dependencies/stb}/stb_image_write.h +253 -131
  156. data/{src → dependencies/stb}/stb_truetype.h +262 -104
  157. data/{src → dependencies/utf8proc}/utf8proc.c +47 -29
  158. data/{src → dependencies/utf8proc}/utf8proc.h +46 -24
  159. data/{src → dependencies/utf8proc}/utf8proc_data.h +10043 -9609
  160. data/ext/gosu/extconf.rb +53 -39
  161. data/{Gosu → include/Gosu}/Audio.hpp +6 -8
  162. data/include/Gosu/Bitmap.hpp +100 -0
  163. data/{Gosu → include/Gosu}/Buttons.hpp +104 -44
  164. data/include/Gosu/Channel.h +25 -0
  165. data/include/Gosu/Color.h +38 -0
  166. data/{Gosu → include/Gosu}/Color.hpp +0 -0
  167. data/{Gosu → include/Gosu}/Directories.hpp +0 -0
  168. data/include/Gosu/Font.h +36 -0
  169. data/{Gosu → include/Gosu}/Font.hpp +1 -1
  170. data/{Gosu → include/Gosu}/Fwd.hpp +0 -5
  171. data/include/Gosu/Gosu.h +82 -0
  172. data/{Gosu → include/Gosu}/Gosu.hpp +0 -0
  173. data/{Gosu → include/Gosu}/Graphics.hpp +0 -0
  174. data/{Gosu → include/Gosu}/GraphicsBase.hpp +0 -0
  175. data/{Gosu → include/Gosu}/IO.hpp +0 -0
  176. data/include/Gosu/Image.h +54 -0
  177. data/{Gosu → include/Gosu}/Image.hpp +7 -6
  178. data/{Gosu → include/Gosu}/ImageData.hpp +0 -0
  179. data/{Gosu → include/Gosu}/Input.hpp +39 -51
  180. data/{Gosu → include/Gosu}/Inspection.hpp +0 -0
  181. data/{Gosu → include/Gosu}/Math.hpp +0 -0
  182. data/{Gosu → include/Gosu}/Platform.hpp +0 -0
  183. data/include/Gosu/Sample.h +19 -0
  184. data/include/Gosu/Song.h +24 -0
  185. data/{Gosu → include/Gosu}/Text.hpp +0 -0
  186. data/include/Gosu/TextInput.h +30 -0
  187. data/{Gosu → include/Gosu}/TextInput.hpp +0 -0
  188. data/{Gosu → include/Gosu}/Timing.hpp +0 -0
  189. data/{Gosu → include/Gosu}/Utility.hpp +15 -4
  190. data/{Gosu → include/Gosu}/Version.hpp +2 -2
  191. data/include/Gosu/Window.h +63 -0
  192. data/{Gosu → include/Gosu}/Window.hpp +23 -25
  193. data/lib/OpenAL32.dll +0 -0
  194. data/lib/SDL2.dll +0 -0
  195. data/lib/gosu.rb +0 -3
  196. data/lib/gosu/patches.rb +0 -23
  197. data/lib/gosu/preview.rb +1 -3
  198. data/lib/gosu/swig_patches.rb +3 -2
  199. data/lib64/OpenAL32.dll +0 -0
  200. data/lib64/SDL2.dll +0 -0
  201. data/rdoc/gosu.rb +98 -22
  202. data/src/Audio.cpp +50 -224
  203. data/src/AudioFile.hpp +20 -37
  204. data/src/AudioFileAudioToolbox.cpp +237 -0
  205. data/src/AudioFileSDLSound.cpp +147 -0
  206. data/src/AudioImpl.cpp +3 -12
  207. data/src/AudioImpl.hpp +3 -1
  208. data/src/Bitmap.cpp +85 -83
  209. data/src/BitmapIO.cpp +52 -58
  210. data/src/ChannelWrapper.cpp +50 -0
  211. data/src/ColorWrapper.cpp +126 -0
  212. data/src/Constants.cpp +338 -0
  213. data/src/Font.cpp +4 -1
  214. data/src/FontWrapper.cpp +74 -0
  215. data/src/GosuWrapper.cpp +251 -0
  216. data/src/Graphics.cpp +7 -4
  217. data/src/Image.cpp +13 -16
  218. data/src/ImageWrapper.cpp +168 -0
  219. data/src/Input.cpp +412 -164
  220. data/src/LargeImageData.cpp +2 -1
  221. data/src/MarkupParser.cpp +2 -1
  222. data/src/RubyGosu.cxx +912 -172
  223. data/src/RubyGosu.h +4 -2
  224. data/src/SampleWrapper.cpp +30 -0
  225. data/src/SongWrapper.cpp +52 -0
  226. data/src/TexChunk.cpp +1 -1
  227. data/src/Text.cpp +1 -0
  228. data/src/TextBuilder.cpp +3 -1
  229. data/src/TextInputWrapper.cpp +101 -0
  230. data/src/Texture.cpp +1 -1
  231. data/src/TrueTypeFont.cpp +2 -1
  232. data/src/Utility.cpp +11 -7
  233. data/src/Window.cpp +30 -39
  234. data/src/WindowWrapper.cpp +317 -0
  235. metadata +212 -43
  236. data/Gosu/AutoLink.hpp +0 -14
  237. data/Gosu/Bitmap.hpp +0 -113
  238. data/lib/gosu/zen.rb +0 -89
  239. data/src/AudioToolboxFile.hpp +0 -210
  240. data/src/OggFile.hpp +0 -92
  241. data/src/SndFile.hpp +0 -174
  242. data/src/WinMain.cpp +0 -64
@@ -1,4 +1,4 @@
1
- /* stb_image - v2.19 - public domain image loader - http://nothings.org/stb
1
+ /* stb_image - v2.26 - public domain image loader - http://nothings.org/stb
2
2
  no warranty implied; use at your own risk
3
3
 
4
4
  Do this:
@@ -48,6 +48,13 @@ LICENSE
48
48
 
49
49
  RECENT REVISION HISTORY:
50
50
 
51
+ 2.26 (2020-07-13) many minor fixes
52
+ 2.25 (2020-02-02) fix warnings
53
+ 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
54
+ 2.23 (2019-08-11) fix clang static analysis warning
55
+ 2.22 (2019-03-04) gif fixes, fix warnings
56
+ 2.21 (2019-02-25) fix typo in comment
57
+ 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
51
58
  2.19 (2018-02-11) fix warning
52
59
  2.18 (2018-01-30) fix warnings
53
60
  2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
@@ -84,23 +91,33 @@ RECENT REVISION HISTORY:
84
91
  Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
85
92
  Arseny Kapoulkine
86
93
  John-Mark Allen
94
+ Carmelo J Fdez-Aguera
87
95
 
88
96
  Bug & warning fixes
89
- Marc LeBlanc David Woo Guillaume George Martins Mozeiko
90
- Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan
91
- Dave Moore Roy Eltham Hayaki Saito Nathan Reed
92
- Won Chun Luke Graham Johan Duparc Nick Verigakis
93
- the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh
94
- Janez Zemva John Bartholomew Michal Cichon github:romigrou
95
- Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
96
- Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar
97
- Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex
98
- Ryamond Barbiero Paul Du Bois Engin Manap github:grim210
99
- Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw
100
- Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus
101
- Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo
102
- Christian Floisand Kevin Schmidt github:darealshinji
103
- Blazej Dariusz Roszkowski github:Michaelangel007
97
+ Marc LeBlanc David Woo Guillaume George Martins Mozeiko
98
+ Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski
99
+ Phil Jordan Dave Moore Roy Eltham
100
+ Hayaki Saito Nathan Reed Won Chun
101
+ Luke Graham Johan Duparc Nick Verigakis the Horde3D community
102
+ Thomas Ruf Ronny Chevalier github:rlyeh
103
+ Janez Zemva John Bartholomew Michal Cichon github:romigrou
104
+ Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
105
+ Laurent Gomila Cort Stratton github:snagar
106
+ Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex
107
+ Cass Everitt Ryamond Barbiero github:grim210
108
+ Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw
109
+ Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus
110
+ Josh Tobin Matthew Gregan github:poppolopoppo
111
+ Julian Raschke Gregory Mullen Christian Floisand github:darealshinji
112
+ Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007
113
+ Brad Weinberger Matvey Cherevko [reserved]
114
+ Luca Sas Alexander Veselov Zack Middleton [reserved]
115
+ Ryan C. Gordon [reserved] [reserved]
116
+ DO NOT ADD YOUR NAME HERE
117
+
118
+ To add your name to the credits, pick a random blank space in the middle and fill it.
119
+ 80% of merge conflicts on stb PRs are due to people adding their name at the end
120
+ of the credits.
104
121
  */
105
122
 
106
123
  #ifndef STBI_INCLUDE_STB_IMAGE_H
@@ -161,6 +178,16 @@ RECENT REVISION HISTORY:
161
178
  //
162
179
  // ===========================================================================
163
180
  //
181
+ // UNICODE:
182
+ //
183
+ // If compiling for Windows and you wish to use Unicode filenames, compile
184
+ // with
185
+ // #define STBI_WINDOWS_UTF8
186
+ // and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert
187
+ // Windows wchar_t filenames to utf8.
188
+ //
189
+ // ===========================================================================
190
+ //
164
191
  // Philosophy
165
192
  //
166
193
  // stb libraries are designed with the following priorities:
@@ -171,12 +198,12 @@ RECENT REVISION HISTORY:
171
198
  //
172
199
  // Sometimes I let "good performance" creep up in priority over "easy to maintain",
173
200
  // and for best performance I may provide less-easy-to-use APIs that give higher
174
- // performance, in addition to the easy to use ones. Nevertheless, it's important
201
+ // performance, in addition to the easy-to-use ones. Nevertheless, it's important
175
202
  // to keep in mind that from the standpoint of you, a client of this library,
176
203
  // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
177
204
  //
178
205
  // Some secondary priorities arise directly from the first two, some of which
179
- // make more explicit reasons why performance can't be emphasized.
206
+ // provide more explicit reasons why performance can't be emphasized.
180
207
  //
181
208
  // - Portable ("ease of use")
182
209
  // - Small source code footprint ("easy to maintain")
@@ -219,11 +246,10 @@ RECENT REVISION HISTORY:
219
246
  //
220
247
  // HDR image support (disable by defining STBI_NO_HDR)
221
248
  //
222
- // stb_image now supports loading HDR images in general, and currently
223
- // the Radiance .HDR file format, although the support is provided
224
- // generically. You can still load any file through the existing interface;
225
- // if you attempt to load an HDR file, it will be automatically remapped to
226
- // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
249
+ // stb_image supports loading HDR images in general, and currently the Radiance
250
+ // .HDR file format specifically. You can still load any file through the existing
251
+ // interface; if you attempt to load an HDR file, it will be automatically remapped
252
+ // to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
227
253
  // both of these constants can be reconfigured through this interface:
228
254
  //
229
255
  // stbi_hdr_to_ldr_gamma(2.2f);
@@ -257,7 +283,7 @@ RECENT REVISION HISTORY:
257
283
  //
258
284
  // By default we convert iphone-formatted PNGs back to RGB, even though
259
285
  // they are internally encoded differently. You can disable this conversion
260
- // by by calling stbi_convert_iphone_png_to_rgb(0), in which case
286
+ // by calling stbi_convert_iphone_png_to_rgb(0), in which case
261
287
  // you will always just get the native iphone "format" through (which
262
288
  // is BGR stored in RGB).
263
289
  //
@@ -301,7 +327,14 @@ RECENT REVISION HISTORY:
301
327
  // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
302
328
  // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
303
329
  //
304
-
330
+ // - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater
331
+ // than that size (in either width or height) without further processing.
332
+ // This is to let programs in the wild set an upper bound to prevent
333
+ // denial-of-service attacks on untrusted data, as one could generate a
334
+ // valid image of gigantic dimensions and force stb_image to allocate a
335
+ // huge block of memory and spend disproportionate time decoding it. By
336
+ // default this is set to (1 << 24), which is 16777216, but that's still
337
+ // very big.
305
338
 
306
339
  #ifndef STBI_NO_STDIO
307
340
  #include <stdio.h>
@@ -319,6 +352,7 @@ enum
319
352
  STBI_rgb_alpha = 4
320
353
  };
321
354
 
355
+ #include <stdlib.h>
322
356
  typedef unsigned char stbi_uc;
323
357
  typedef unsigned short stbi_us;
324
358
 
@@ -326,11 +360,13 @@ typedef unsigned short stbi_us;
326
360
  extern "C" {
327
361
  #endif
328
362
 
363
+ #ifndef STBIDEF
329
364
  #ifdef STB_IMAGE_STATIC
330
365
  #define STBIDEF static
331
366
  #else
332
367
  #define STBIDEF extern
333
368
  #endif
369
+ #endif
334
370
 
335
371
  //////////////////////////////////////////////////////////////////////////////
336
372
  //
@@ -355,10 +391,6 @@ typedef struct
355
391
 
356
392
  STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
357
393
  STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);
358
- #ifndef STBI_NO_GIF
359
- STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
360
- #endif
361
-
362
394
 
363
395
  #ifndef STBI_NO_STDIO
364
396
  STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
@@ -366,6 +398,14 @@ STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in
366
398
  // for stbi_load_from_file, file pointer is left pointing immediately after image
367
399
  #endif
368
400
 
401
+ #ifndef STBI_NO_GIF
402
+ STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
403
+ #endif
404
+
405
+ #ifdef STBI_WINDOWS_UTF8
406
+ STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
407
+ #endif
408
+
369
409
  ////////////////////////////////////
370
410
  //
371
411
  // 16-bits-per-channel interface
@@ -413,7 +453,7 @@ STBIDEF int stbi_is_hdr_from_file(FILE *f);
413
453
 
414
454
 
415
455
  // get a VERY brief reason for failure
416
- // NOT THREADSAFE
456
+ // on most compilers (and ALL modern mainstream compilers) this is threadsafe
417
457
  STBIDEF const char *stbi_failure_reason (void);
418
458
 
419
459
  // free the loaded image -- this is just free()
@@ -446,6 +486,11 @@ STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
446
486
  // flip the image vertically, so the first pixel in the output array is the bottom left
447
487
  STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
448
488
 
489
+ // as above, but only applies to images loaded on the thread that calls the function
490
+ // this function is only available if your compiler supports thread-local variables;
491
+ // calling it will fail to link if your compiler doesn't
492
+ STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);
493
+
449
494
  // ZLIB client - used by PNG, available for other purposes
450
495
 
451
496
  STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
@@ -525,6 +570,12 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
525
570
  #define STBI_ASSERT(x) assert(x)
526
571
  #endif
527
572
 
573
+ #ifdef __cplusplus
574
+ #define STBI_EXTERN extern "C"
575
+ #else
576
+ #define STBI_EXTERN extern
577
+ #endif
578
+
528
579
 
529
580
  #ifndef _MSC_VER
530
581
  #ifdef __cplusplus
@@ -536,6 +587,23 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
536
587
  #define stbi_inline __forceinline
537
588
  #endif
538
589
 
590
+ #ifndef STBI_NO_THREAD_LOCALS
591
+ #if defined(__cplusplus) && __cplusplus >= 201103L
592
+ #define STBI_THREAD_LOCAL thread_local
593
+ #elif defined(__GNUC__) && __GNUC__ < 5
594
+ #define STBI_THREAD_LOCAL __thread
595
+ #elif defined(_MSC_VER)
596
+ #define STBI_THREAD_LOCAL __declspec(thread)
597
+ #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
598
+ #define STBI_THREAD_LOCAL _Thread_local
599
+ #endif
600
+
601
+ #ifndef STBI_THREAD_LOCAL
602
+ #if defined(__GNUC__)
603
+ #define STBI_THREAD_LOCAL __thread
604
+ #endif
605
+ #endif
606
+ #endif
539
607
 
540
608
  #ifdef _MSC_VER
541
609
  typedef unsigned short stbi__uint16;
@@ -649,14 +717,18 @@ static int stbi__cpuid3(void)
649
717
 
650
718
  #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
651
719
 
720
+ #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
652
721
  static int stbi__sse2_available(void)
653
722
  {
654
723
  int info3 = stbi__cpuid3();
655
724
  return ((info3 >> 26) & 1) != 0;
656
725
  }
726
+ #endif
727
+
657
728
  #else // assume GCC-style if not VC++
658
729
  #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
659
730
 
731
+ #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
660
732
  static int stbi__sse2_available(void)
661
733
  {
662
734
  // If we're even attempting to compile this on GCC/Clang, that means
@@ -664,6 +736,8 @@ static int stbi__sse2_available(void)
664
736
  // instructions at will, and so are we.
665
737
  return 1;
666
738
  }
739
+ #endif
740
+
667
741
  #endif
668
742
  #endif
669
743
 
@@ -682,6 +756,10 @@ static int stbi__sse2_available(void)
682
756
  #define STBI_SIMD_ALIGN(type, name) type name
683
757
  #endif
684
758
 
759
+ #ifndef STBI_MAX_DIMENSIONS
760
+ #define STBI_MAX_DIMENSIONS (1 << 24)
761
+ #endif
762
+
685
763
  ///////////////////////////////////////////////
686
764
  //
687
765
  // stbi__context struct and start_xxx functions
@@ -699,6 +777,7 @@ typedef struct
699
777
  int read_from_callbacks;
700
778
  int buflen;
701
779
  stbi_uc buffer_start[128];
780
+ int callback_already_read;
702
781
 
703
782
  stbi_uc *img_buffer, *img_buffer_end;
704
783
  stbi_uc *img_buffer_original, *img_buffer_original_end;
@@ -712,6 +791,7 @@ static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)
712
791
  {
713
792
  s->io.read = NULL;
714
793
  s->read_from_callbacks = 0;
794
+ s->callback_already_read = 0;
715
795
  s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;
716
796
  s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;
717
797
  }
@@ -723,7 +803,8 @@ static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *
723
803
  s->io_user_data = user;
724
804
  s->buflen = sizeof(s->buffer_start);
725
805
  s->read_from_callbacks = 1;
726
- s->img_buffer_original = s->buffer_start;
806
+ s->callback_already_read = 0;
807
+ s->img_buffer = s->img_buffer_original = s->buffer_start;
727
808
  stbi__refill_buffer(s);
728
809
  s->img_buffer_original_end = s->img_buffer_end;
729
810
  }
@@ -737,12 +818,17 @@ static int stbi__stdio_read(void *user, char *data, int size)
737
818
 
738
819
  static void stbi__stdio_skip(void *user, int n)
739
820
  {
821
+ int ch;
740
822
  fseek((FILE*) user, n, SEEK_CUR);
823
+ ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */
824
+ if (ch != EOF) {
825
+ ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */
826
+ }
741
827
  }
742
828
 
743
829
  static int stbi__stdio_eof(void *user)
744
830
  {
745
- return feof((FILE*) user);
831
+ return feof((FILE*) user) || ferror((FILE *) user);
746
832
  }
747
833
 
748
834
  static stbi_io_callbacks stbi__stdio_callbacks =
@@ -840,19 +926,24 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int
840
926
  static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
841
927
  #endif
842
928
 
843
- // this is not threadsafe
844
- static const char *stbi__g_failure_reason;
929
+ static
930
+ #ifdef STBI_THREAD_LOCAL
931
+ STBI_THREAD_LOCAL
932
+ #endif
933
+ const char *stbi__g_failure_reason;
845
934
 
846
935
  STBIDEF const char *stbi_failure_reason(void)
847
936
  {
848
937
  return stbi__g_failure_reason;
849
938
  }
850
939
 
940
+ #ifndef STBI_NO_FAILURE_STRINGS
851
941
  static int stbi__err(const char *str)
852
942
  {
853
943
  stbi__g_failure_reason = str;
854
944
  return 0;
855
945
  }
946
+ #endif
856
947
 
857
948
  static void *stbi__malloc(size_t size)
858
949
  {
@@ -891,11 +982,13 @@ static int stbi__mul2sizes_valid(int a, int b)
891
982
  return a <= INT_MAX/b;
892
983
  }
893
984
 
985
+ #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
894
986
  // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
895
987
  static int stbi__mad2sizes_valid(int a, int b, int add)
896
988
  {
897
989
  return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);
898
990
  }
991
+ #endif
899
992
 
900
993
  // returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow
901
994
  static int stbi__mad3sizes_valid(int a, int b, int c, int add)
@@ -913,12 +1006,14 @@ static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
913
1006
  }
914
1007
  #endif
915
1008
 
1009
+ #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
916
1010
  // mallocs with size overflow checking
917
1011
  static void *stbi__malloc_mad2(int a, int b, int add)
918
1012
  {
919
1013
  if (!stbi__mad2sizes_valid(a, b, add)) return NULL;
920
1014
  return stbi__malloc(a*b + add);
921
1015
  }
1016
+ #endif
922
1017
 
923
1018
  static void *stbi__malloc_mad3(int a, int b, int c, int add)
924
1019
  {
@@ -962,13 +1057,29 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
962
1057
  static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
963
1058
  #endif
964
1059
 
965
- static int stbi__vertically_flip_on_load = 0;
1060
+ static int stbi__vertically_flip_on_load_global = 0;
966
1061
 
967
1062
  STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
968
1063
  {
969
- stbi__vertically_flip_on_load = flag_true_if_should_flip;
1064
+ stbi__vertically_flip_on_load_global = flag_true_if_should_flip;
970
1065
  }
971
1066
 
1067
+ #ifndef STBI_THREAD_LOCAL
1068
+ #define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global
1069
+ #else
1070
+ static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;
1071
+
1072
+ STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)
1073
+ {
1074
+ stbi__vertically_flip_on_load_local = flag_true_if_should_flip;
1075
+ stbi__vertically_flip_on_load_set = 1;
1076
+ }
1077
+
1078
+ #define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \
1079
+ ? stbi__vertically_flip_on_load_local \
1080
+ : stbi__vertically_flip_on_load_global)
1081
+ #endif // STBI_THREAD_LOCAL
1082
+
972
1083
  static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
973
1084
  {
974
1085
  memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields
@@ -990,6 +1101,8 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
990
1101
  #endif
991
1102
  #ifndef STBI_NO_PSD
992
1103
  if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc);
1104
+ #else
1105
+ STBI_NOTUSED(bpc);
993
1106
  #endif
994
1107
  #ifndef STBI_NO_PIC
995
1108
  if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
@@ -1070,6 +1183,7 @@ static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
1070
1183
  }
1071
1184
  }
1072
1185
 
1186
+ #ifndef STBI_NO_GIF
1073
1187
  static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
1074
1188
  {
1075
1189
  int slice;
@@ -1077,10 +1191,11 @@ static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int byt
1077
1191
 
1078
1192
  stbi_uc *bytes = (stbi_uc *)image;
1079
1193
  for (slice = 0; slice < z; ++slice) {
1080
- stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
1081
- bytes += slice_size;
1194
+ stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
1195
+ bytes += slice_size;
1082
1196
  }
1083
1197
  }
1198
+ #endif
1084
1199
 
1085
1200
  static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1086
1201
  {
@@ -1090,8 +1205,10 @@ static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x,
1090
1205
  if (result == NULL)
1091
1206
  return NULL;
1092
1207
 
1208
+ // it is the responsibility of the loaders to make sure we get either 8 or 16 bit.
1209
+ STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);
1210
+
1093
1211
  if (ri.bits_per_channel != 8) {
1094
- STBI_ASSERT(ri.bits_per_channel == 16);
1095
1212
  result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1096
1213
  ri.bits_per_channel = 8;
1097
1214
  }
@@ -1114,8 +1231,10 @@ static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x,
1114
1231
  if (result == NULL)
1115
1232
  return NULL;
1116
1233
 
1234
+ // it is the responsibility of the loaders to make sure we get either 8 or 16 bit.
1235
+ STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);
1236
+
1117
1237
  if (ri.bits_per_channel != 16) {
1118
- STBI_ASSERT(ri.bits_per_channel == 8);
1119
1238
  result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1120
1239
  ri.bits_per_channel = 16;
1121
1240
  }
@@ -1131,7 +1250,7 @@ static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x,
1131
1250
  return (stbi__uint16 *) result;
1132
1251
  }
1133
1252
 
1134
- #if !defined(STBI_NO_HDR) || !defined(STBI_NO_LINEAR)
1253
+ #if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)
1135
1254
  static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
1136
1255
  {
1137
1256
  if (stbi__vertically_flip_on_load && result != NULL) {
@@ -1143,10 +1262,38 @@ static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, in
1143
1262
 
1144
1263
  #ifndef STBI_NO_STDIO
1145
1264
 
1265
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1266
+ STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
1267
+ STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
1268
+ #endif
1269
+
1270
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1271
+ STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
1272
+ {
1273
+ return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
1274
+ }
1275
+ #endif
1276
+
1146
1277
  static FILE *stbi__fopen(char const *filename, char const *mode)
1147
1278
  {
1148
1279
  FILE *f;
1149
- #if defined(_MSC_VER) && _MSC_VER >= 1400
1280
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1281
+ wchar_t wMode[64];
1282
+ wchar_t wFilename[1024];
1283
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
1284
+ return 0;
1285
+
1286
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
1287
+ return 0;
1288
+
1289
+ #if _MSC_VER >= 1400
1290
+ if (0 != _wfopen_s(&f, wFilename, wMode))
1291
+ f = 0;
1292
+ #else
1293
+ f = _wfopen(wFilename, wMode);
1294
+ #endif
1295
+
1296
+ #elif defined(_MSC_VER) && _MSC_VER >= 1400
1150
1297
  if (0 != fopen_s(&f, filename, mode))
1151
1298
  f=0;
1152
1299
  #else
@@ -1237,15 +1384,15 @@ STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *u
1237
1384
  STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
1238
1385
  {
1239
1386
  unsigned char *result;
1240
- stbi__context s;
1241
- stbi__start_mem(&s,buffer,len);
1242
-
1387
+ stbi__context s;
1388
+ stbi__start_mem(&s,buffer,len);
1389
+
1243
1390
  result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1244
1391
  if (stbi__vertically_flip_on_load) {
1245
- stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
1392
+ stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
1246
1393
  }
1247
1394
 
1248
- return result;
1395
+ return result;
1249
1396
  }
1250
1397
  #endif
1251
1398
 
@@ -1390,6 +1537,7 @@ enum
1390
1537
  static void stbi__refill_buffer(stbi__context *s)
1391
1538
  {
1392
1539
  int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
1540
+ s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original);
1393
1541
  if (n == 0) {
1394
1542
  // at end of file, treat same as if from memory, but need to handle case
1395
1543
  // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
@@ -1414,6 +1562,9 @@ stbi_inline static stbi_uc stbi__get8(stbi__context *s)
1414
1562
  return 0;
1415
1563
  }
1416
1564
 
1565
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1566
+ // nothing
1567
+ #else
1417
1568
  stbi_inline static int stbi__at_eof(stbi__context *s)
1418
1569
  {
1419
1570
  if (s->io.read) {
@@ -1425,9 +1576,14 @@ stbi_inline static int stbi__at_eof(stbi__context *s)
1425
1576
 
1426
1577
  return s->img_buffer >= s->img_buffer_end;
1427
1578
  }
1579
+ #endif
1428
1580
 
1581
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC)
1582
+ // nothing
1583
+ #else
1429
1584
  static void stbi__skip(stbi__context *s, int n)
1430
1585
  {
1586
+ if (n == 0) return; // already there!
1431
1587
  if (n < 0) {
1432
1588
  s->img_buffer = s->img_buffer_end;
1433
1589
  return;
@@ -1442,7 +1598,11 @@ static void stbi__skip(stbi__context *s, int n)
1442
1598
  }
1443
1599
  s->img_buffer += n;
1444
1600
  }
1601
+ #endif
1445
1602
 
1603
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)
1604
+ // nothing
1605
+ #else
1446
1606
  static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1447
1607
  {
1448
1608
  if (s->io.read) {
@@ -1466,18 +1626,27 @@ static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1466
1626
  } else
1467
1627
  return 0;
1468
1628
  }
1629
+ #endif
1469
1630
 
1631
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1632
+ // nothing
1633
+ #else
1470
1634
  static int stbi__get16be(stbi__context *s)
1471
1635
  {
1472
1636
  int z = stbi__get8(s);
1473
1637
  return (z << 8) + stbi__get8(s);
1474
1638
  }
1639
+ #endif
1475
1640
 
1641
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1642
+ // nothing
1643
+ #else
1476
1644
  static stbi__uint32 stbi__get32be(stbi__context *s)
1477
1645
  {
1478
1646
  stbi__uint32 z = stbi__get16be(s);
1479
1647
  return (z << 16) + stbi__get16be(s);
1480
1648
  }
1649
+ #endif
1481
1650
 
1482
1651
  #if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
1483
1652
  // nothing
@@ -1499,7 +1668,9 @@ static stbi__uint32 stbi__get32le(stbi__context *s)
1499
1668
 
1500
1669
  #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
1501
1670
 
1502
-
1671
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1672
+ // nothing
1673
+ #else
1503
1674
  //////////////////////////////////////////////////////////////////////////////
1504
1675
  //
1505
1676
  // generic converter from built-in img_n to req_comp
@@ -1515,7 +1686,11 @@ static stbi_uc stbi__compute_y(int r, int g, int b)
1515
1686
  {
1516
1687
  return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
1517
1688
  }
1689
+ #endif
1518
1690
 
1691
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1692
+ // nothing
1693
+ #else
1519
1694
  static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1520
1695
  {
1521
1696
  int i,j;
@@ -1539,19 +1714,19 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1539
1714
  // convert source image with img_n components to one with req_comp components;
1540
1715
  // avoid switch per pixel, so use switch per scanline and massive macros
1541
1716
  switch (STBI__COMBO(img_n, req_comp)) {
1542
- STBI__CASE(1,2) { dest[0]=src[0], dest[1]=255; } break;
1717
+ STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break;
1543
1718
  STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1544
- STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; } break;
1719
+ STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break;
1545
1720
  STBI__CASE(2,1) { dest[0]=src[0]; } break;
1546
1721
  STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1547
- STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
1548
- STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; } break;
1722
+ STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
1723
+ STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break;
1549
1724
  STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1550
- STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; } break;
1725
+ STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break;
1551
1726
  STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1552
- STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; } break;
1553
- STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
1554
- default: STBI_ASSERT(0);
1727
+ STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break;
1728
+ STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
1729
+ default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion");
1555
1730
  }
1556
1731
  #undef STBI__CASE
1557
1732
  }
@@ -1559,12 +1734,20 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1559
1734
  STBI_FREE(data);
1560
1735
  return good;
1561
1736
  }
1737
+ #endif
1562
1738
 
1739
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1740
+ // nothing
1741
+ #else
1563
1742
  static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
1564
1743
  {
1565
1744
  return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
1566
1745
  }
1746
+ #endif
1567
1747
 
1748
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1749
+ // nothing
1750
+ #else
1568
1751
  static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1569
1752
  {
1570
1753
  int i,j;
@@ -1588,19 +1771,19 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r
1588
1771
  // convert source image with img_n components to one with req_comp components;
1589
1772
  // avoid switch per pixel, so use switch per scanline and massive macros
1590
1773
  switch (STBI__COMBO(img_n, req_comp)) {
1591
- STBI__CASE(1,2) { dest[0]=src[0], dest[1]=0xffff; } break;
1774
+ STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break;
1592
1775
  STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1593
- STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=0xffff; } break;
1776
+ STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break;
1594
1777
  STBI__CASE(2,1) { dest[0]=src[0]; } break;
1595
1778
  STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1596
- STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
1597
- STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=0xffff; } break;
1779
+ STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
1780
+ STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break;
1598
1781
  STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1599
- STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = 0xffff; } break;
1782
+ STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break;
1600
1783
  STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1601
- STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = src[3]; } break;
1602
- STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
1603
- default: STBI_ASSERT(0);
1784
+ STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break;
1785
+ STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
1786
+ default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion");
1604
1787
  }
1605
1788
  #undef STBI__CASE
1606
1789
  }
@@ -1608,6 +1791,7 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r
1608
1791
  STBI_FREE(data);
1609
1792
  return good;
1610
1793
  }
1794
+ #endif
1611
1795
 
1612
1796
  #ifndef STBI_NO_LINEAR
1613
1797
  static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
@@ -1623,7 +1807,11 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
1623
1807
  for (k=0; k < n; ++k) {
1624
1808
  output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
1625
1809
  }
1626
- if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
1810
+ }
1811
+ if (n < comp) {
1812
+ for (i=0; i < x*y; ++i) {
1813
+ output[i*comp + n] = data[i*comp + n]/255.0f;
1814
+ }
1627
1815
  }
1628
1816
  STBI_FREE(data);
1629
1817
  return output;
@@ -1904,7 +2092,7 @@ stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
1904
2092
 
1905
2093
  sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB
1906
2094
  k = stbi_lrot(j->code_buffer, n);
1907
- STBI_ASSERT(n >= 0 && n < (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask)));
2095
+ if (n < 0 || n >= (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask))) return 0;
1908
2096
  j->code_buffer = k & ~stbi__bmask[n];
1909
2097
  k &= stbi__bmask[n];
1910
2098
  j->code_bits -= n;
@@ -2015,6 +2203,7 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__
2015
2203
  // first scan for DC coefficient, must be first
2016
2204
  memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
2017
2205
  t = stbi__jpeg_huff_decode(j, hdc);
2206
+ if (t == -1) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2018
2207
  diff = t ? stbi__extend_receive(j, t) : 0;
2019
2208
 
2020
2209
  dc = j->img_comp[b].dc_pred + diff;
@@ -3005,6 +3194,8 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan)
3005
3194
  p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
3006
3195
  s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
3007
3196
  s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
3197
+ if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
3198
+ if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
3008
3199
  c = stbi__get8(s);
3009
3200
  if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG");
3010
3201
  s->img_n = c;
@@ -3596,7 +3787,7 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp
3596
3787
  int k;
3597
3788
  unsigned int i,j;
3598
3789
  stbi_uc *output;
3599
- stbi_uc *coutput[4];
3790
+ stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL };
3600
3791
 
3601
3792
  stbi__resample res_comp[4];
3602
3793
 
@@ -3717,7 +3908,7 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp
3717
3908
  if (n == 1)
3718
3909
  for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
3719
3910
  else
3720
- for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
3911
+ for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; }
3721
3912
  }
3722
3913
  }
3723
3914
  }
@@ -3885,16 +4076,23 @@ typedef struct
3885
4076
  stbi__zhuffman z_length, z_distance;
3886
4077
  } stbi__zbuf;
3887
4078
 
4079
+ stbi_inline static int stbi__zeof(stbi__zbuf *z)
4080
+ {
4081
+ return (z->zbuffer >= z->zbuffer_end);
4082
+ }
4083
+
3888
4084
  stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z)
3889
4085
  {
3890
- if (z->zbuffer >= z->zbuffer_end) return 0;
3891
- return *z->zbuffer++;
4086
+ return stbi__zeof(z) ? 0 : *z->zbuffer++;
3892
4087
  }
3893
4088
 
3894
4089
  static void stbi__fill_bits(stbi__zbuf *z)
3895
4090
  {
3896
4091
  do {
3897
- STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
4092
+ if (z->code_buffer >= (1U << z->num_bits)) {
4093
+ z->zbuffer = z->zbuffer_end; /* treat this as EOF so we fail. */
4094
+ return;
4095
+ }
3898
4096
  z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits;
3899
4097
  z->num_bits += 8;
3900
4098
  } while (z->num_bits <= 24);
@@ -3919,10 +4117,11 @@ static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
3919
4117
  for (s=STBI__ZFAST_BITS+1; ; ++s)
3920
4118
  if (k < z->maxcode[s])
3921
4119
  break;
3922
- if (s == 16) return -1; // invalid code!
4120
+ if (s >= 16) return -1; // invalid code!
3923
4121
  // code size is s, so:
3924
4122
  b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
3925
- STBI_ASSERT(z->size[b] == s);
4123
+ if (b >= sizeof (z->size)) return -1; // some data was corrupt somewhere!
4124
+ if (z->size[b] != s) return -1; // was originally an assert, but report failure instead.
3926
4125
  a->code_buffer >>= s;
3927
4126
  a->num_bits -= s;
3928
4127
  return z->value[b];
@@ -3931,7 +4130,12 @@ static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
3931
4130
  stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
3932
4131
  {
3933
4132
  int b,s;
3934
- if (a->num_bits < 16) stbi__fill_bits(a);
4133
+ if (a->num_bits < 16) {
4134
+ if (stbi__zeof(a)) {
4135
+ return -1; /* report error for unexpected end of data. */
4136
+ }
4137
+ stbi__fill_bits(a);
4138
+ }
3935
4139
  b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
3936
4140
  if (b) {
3937
4141
  s = b >> 9;
@@ -3945,13 +4149,16 @@ stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
3945
4149
  static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes
3946
4150
  {
3947
4151
  char *q;
3948
- int cur, limit, old_limit;
4152
+ unsigned int cur, limit, old_limit;
3949
4153
  z->zout = zout;
3950
4154
  if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
3951
- cur = (int) (z->zout - z->zout_start);
3952
- limit = old_limit = (int) (z->zout_end - z->zout_start);
3953
- while (cur + n > limit)
4155
+ cur = (unsigned int) (z->zout - z->zout_start);
4156
+ limit = old_limit = (unsigned) (z->zout_end - z->zout_start);
4157
+ if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory");
4158
+ while (cur + n > limit) {
4159
+ if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory");
3954
4160
  limit *= 2;
4161
+ }
3955
4162
  q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
3956
4163
  STBI_NOTUSED(old_limit);
3957
4164
  if (q == NULL) return stbi__err("outofmem", "Out of memory");
@@ -4049,11 +4256,12 @@ static int stbi__compute_huffman_codes(stbi__zbuf *a)
4049
4256
  c = stbi__zreceive(a,2)+3;
4050
4257
  if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG");
4051
4258
  fill = lencodes[n-1];
4052
- } else if (c == 17)
4259
+ } else if (c == 17) {
4053
4260
  c = stbi__zreceive(a,3)+3;
4054
- else {
4055
- STBI_ASSERT(c == 18);
4261
+ } else if (c == 18) {
4056
4262
  c = stbi__zreceive(a,7)+11;
4263
+ } else {
4264
+ return stbi__err("bad codelengths", "Corrupt PNG");
4057
4265
  }
4058
4266
  if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG");
4059
4267
  memset(lencodes+n, fill, c);
@@ -4079,7 +4287,7 @@ static int stbi__parse_uncompressed_block(stbi__zbuf *a)
4079
4287
  a->code_buffer >>= 8;
4080
4288
  a->num_bits -= 8;
4081
4289
  }
4082
- STBI_ASSERT(a->num_bits == 0);
4290
+ if (a->num_bits < 0) return stbi__err("zlib corrupt","Corrupt PNG");
4083
4291
  // now fill header the normal way
4084
4292
  while (k < 4)
4085
4293
  header[k++] = stbi__zget8(a);
@@ -4101,6 +4309,7 @@ static int stbi__parse_zlib_header(stbi__zbuf *a)
4101
4309
  int cm = cmf & 15;
4102
4310
  /* int cinfo = cmf >> 4; */
4103
4311
  int flg = stbi__zget8(a);
4312
+ if (stbi__zeof(a)) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec
4104
4313
  if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec
4105
4314
  if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
4106
4315
  if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png
@@ -4362,7 +4571,7 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
4362
4571
  return stbi__err("invalid filter","Corrupt PNG");
4363
4572
 
4364
4573
  if (depth < 8) {
4365
- STBI_ASSERT(img_width_bytes <= x);
4574
+ if (img_width_bytes > x) return stbi__err("invalid width","Corrupt PNG");
4366
4575
  cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
4367
4576
  filter_bytes = 1;
4368
4577
  width = img_width_bytes;
@@ -4731,7 +4940,7 @@ static void stbi__de_iphone(stbi__png *z)
4731
4940
  static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
4732
4941
  {
4733
4942
  stbi_uc palette[1024], pal_img_n=0;
4734
- stbi_uc has_trans=0, tc[3];
4943
+ stbi_uc has_trans=0, tc[3]={0};
4735
4944
  stbi__uint16 tc16[3];
4736
4945
  stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
4737
4946
  int first=1,k,interlace=0, color=0, is_iphone=0;
@@ -4757,8 +4966,10 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
4757
4966
  if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
4758
4967
  first = 0;
4759
4968
  if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
4760
- s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
4761
- s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
4969
+ s->img_x = stbi__get32be(s);
4970
+ s->img_y = stbi__get32be(s);
4971
+ if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
4972
+ if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
4762
4973
  z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only");
4763
4974
  color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
4764
4975
  if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
@@ -4875,6 +5086,8 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
4875
5086
  ++s->img_n;
4876
5087
  }
4877
5088
  STBI_FREE(z->expanded); z->expanded = NULL;
5089
+ // end of PNG chunk, read and skip CRC
5090
+ stbi__get32be(s);
4878
5091
  return 1;
4879
5092
  }
4880
5093
 
@@ -4905,10 +5118,12 @@ static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, st
4905
5118
  void *result=NULL;
4906
5119
  if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
4907
5120
  if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
4908
- if (p->depth < 8)
5121
+ if (p->depth <= 8)
4909
5122
  ri->bits_per_channel = 8;
5123
+ else if (p->depth == 16)
5124
+ ri->bits_per_channel = 16;
4910
5125
  else
4911
- ri->bits_per_channel = p->depth;
5126
+ return stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth");
4912
5127
  result = p->out;
4913
5128
  p->out = NULL;
4914
5129
  if (req_comp && req_comp != p->s->img_out_n) {
@@ -5009,11 +5224,11 @@ static int stbi__high_bit(unsigned int z)
5009
5224
  {
5010
5225
  int n=0;
5011
5226
  if (z == 0) return -1;
5012
- if (z >= 0x10000) n += 16, z >>= 16;
5013
- if (z >= 0x00100) n += 8, z >>= 8;
5014
- if (z >= 0x00010) n += 4, z >>= 4;
5015
- if (z >= 0x00004) n += 2, z >>= 2;
5016
- if (z >= 0x00002) n += 1, z >>= 1;
5227
+ if (z >= 0x10000) { n += 16; z >>= 16; }
5228
+ if (z >= 0x00100) { n += 8; z >>= 8; }
5229
+ if (z >= 0x00010) { n += 4; z >>= 4; }
5230
+ if (z >= 0x00004) { n += 2; z >>= 2; }
5231
+ if (z >= 0x00002) { n += 1;/* >>= 1;*/ }
5017
5232
  return n;
5018
5233
  }
5019
5234
 
@@ -5030,7 +5245,7 @@ static int stbi__bitcount(unsigned int a)
5030
5245
  // extract an arbitrarily-aligned N-bit value (N=bits)
5031
5246
  // from v, and then make it 8-bits long and fractionally
5032
5247
  // extend it to full full range.
5033
- static int stbi__shiftsigned(int v, int shift, int bits)
5248
+ static int stbi__shiftsigned(unsigned int v, int shift, int bits)
5034
5249
  {
5035
5250
  static unsigned int mul_table[9] = {
5036
5251
  0,
@@ -5044,7 +5259,7 @@ static int stbi__shiftsigned(int v, int shift, int bits)
5044
5259
  v <<= -shift;
5045
5260
  else
5046
5261
  v >>= shift;
5047
- STBI_ASSERT(v >= 0 && v < 256);
5262
+ STBI_ASSERT(v < 256);
5048
5263
  v >>= (8-bits);
5049
5264
  STBI_ASSERT(bits >= 0 && bits <= 8);
5050
5265
  return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits];
@@ -5054,6 +5269,7 @@ typedef struct
5054
5269
  {
5055
5270
  int bpp, offset, hsz;
5056
5271
  unsigned int mr,mg,mb,ma, all_a;
5272
+ int extra_read;
5057
5273
  } stbi__bmp_data;
5058
5274
 
5059
5275
  static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
@@ -5066,6 +5282,9 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5066
5282
  info->offset = stbi__get32le(s);
5067
5283
  info->hsz = hsz = stbi__get32le(s);
5068
5284
  info->mr = info->mg = info->mb = info->ma = 0;
5285
+ info->extra_read = 14;
5286
+
5287
+ if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP");
5069
5288
 
5070
5289
  if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
5071
5290
  if (hsz == 12) {
@@ -5109,6 +5328,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5109
5328
  info->mr = stbi__get32le(s);
5110
5329
  info->mg = stbi__get32le(s);
5111
5330
  info->mb = stbi__get32le(s);
5331
+ info->extra_read += 12;
5112
5332
  // not documented, but generated by photoshop and handled by mspaint
5113
5333
  if (info->mr == info->mg && info->mg == info->mb) {
5114
5334
  // ?!?!?
@@ -5157,6 +5377,9 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5157
5377
  flip_vertically = ((int) s->img_y) > 0;
5158
5378
  s->img_y = abs((int) s->img_y);
5159
5379
 
5380
+ if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5381
+ if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5382
+
5160
5383
  mr = info.mr;
5161
5384
  mg = info.mg;
5162
5385
  mb = info.mb;
@@ -5165,13 +5388,22 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5165
5388
 
5166
5389
  if (info.hsz == 12) {
5167
5390
  if (info.bpp < 24)
5168
- psize = (info.offset - 14 - 24) / 3;
5391
+ psize = (info.offset - info.extra_read - 24) / 3;
5169
5392
  } else {
5170
5393
  if (info.bpp < 16)
5171
- psize = (info.offset - 14 - info.hsz) >> 2;
5394
+ psize = (info.offset - info.extra_read - info.hsz) >> 2;
5395
+ }
5396
+ if (psize == 0) {
5397
+ STBI_ASSERT(info.offset == s->callback_already_read + (int) (s->img_buffer - s->img_buffer_original));
5398
+ if (info.offset != s->callback_already_read + (s->img_buffer - s->buffer_start)) {
5399
+ return stbi__errpuc("bad offset", "Corrupt BMP");
5400
+ }
5172
5401
  }
5173
5402
 
5174
- s->img_n = ma ? 4 : 3;
5403
+ if (info.bpp == 24 && ma == 0xff000000)
5404
+ s->img_n = 3;
5405
+ else
5406
+ s->img_n = ma ? 4 : 3;
5175
5407
  if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
5176
5408
  target = req_comp;
5177
5409
  else
@@ -5193,7 +5425,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5193
5425
  if (info.hsz != 12) stbi__get8(s);
5194
5426
  pal[i][3] = 255;
5195
5427
  }
5196
- stbi__skip(s, info.offset - 14 - info.hsz - psize * (info.hsz == 12 ? 3 : 4));
5428
+ stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4));
5197
5429
  if (info.bpp == 1) width = (s->img_x + 7) >> 3;
5198
5430
  else if (info.bpp == 4) width = (s->img_x + 1) >> 1;
5199
5431
  else if (info.bpp == 8) width = s->img_x;
@@ -5207,6 +5439,8 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5207
5439
  out[z++] = pal[color][0];
5208
5440
  out[z++] = pal[color][1];
5209
5441
  out[z++] = pal[color][2];
5442
+ if (target == 4) out[z++] = 255;
5443
+ if (i+1 == (int) s->img_x) break;
5210
5444
  if((--bit_offset) < 0) {
5211
5445
  bit_offset = 7;
5212
5446
  v = stbi__get8(s);
@@ -5240,7 +5474,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5240
5474
  int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
5241
5475
  int z = 0;
5242
5476
  int easy=0;
5243
- stbi__skip(s, info.offset - 14 - info.hsz);
5477
+ stbi__skip(s, info.offset - info.extra_read - info.hsz);
5244
5478
  if (info.bpp == 24) width = 3 * s->img_x;
5245
5479
  else if (info.bpp == 16) width = 2*s->img_x;
5246
5480
  else /* bpp = 32 and pad = 0 */ width=0;
@@ -5258,6 +5492,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5258
5492
  gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
5259
5493
  bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
5260
5494
  ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
5495
+ if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
5261
5496
  }
5262
5497
  for (j=0; j < (int) s->img_y; ++j) {
5263
5498
  if (easy) {
@@ -5299,7 +5534,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5299
5534
  stbi_uc *p1 = out + j *s->img_x*target;
5300
5535
  stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
5301
5536
  for (i=0; i < (int) s->img_x*target; ++i) {
5302
- t = p1[i], p1[i] = p2[i], p2[i] = t;
5537
+ t = p1[i]; p1[i] = p2[i]; p2[i] = t;
5303
5538
  }
5304
5539
  }
5305
5540
  }
@@ -5479,6 +5714,11 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5479
5714
  int RLE_repeating = 0;
5480
5715
  int read_next_pixel = 1;
5481
5716
  STBI_NOTUSED(ri);
5717
+ STBI_NOTUSED(tga_x_origin); // @TODO
5718
+ STBI_NOTUSED(tga_y_origin); // @TODO
5719
+
5720
+ if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5721
+ if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5482
5722
 
5483
5723
  // do a tiny bit of precessing
5484
5724
  if ( tga_image_type >= 8 )
@@ -5519,6 +5759,11 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5519
5759
  // do I need to load a palette?
5520
5760
  if ( tga_indexed)
5521
5761
  {
5762
+ if (tga_palette_len == 0) { /* you have to have at least one entry! */
5763
+ STBI_FREE(tga_data);
5764
+ return stbi__errpuc("bad palette", "Corrupt TGA");
5765
+ }
5766
+
5522
5767
  // any data to skip? (offset usually = 0)
5523
5768
  stbi__skip(s, tga_palette_start );
5524
5769
  // load the palette
@@ -5642,6 +5887,7 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5642
5887
  // Microsoft's C compilers happy... [8^(
5643
5888
  tga_palette_start = tga_palette_len = tga_palette_bits =
5644
5889
  tga_x_origin = tga_y_origin = 0;
5890
+ STBI_NOTUSED(tga_palette_start);
5645
5891
  // OK, done
5646
5892
  return tga_data;
5647
5893
  }
@@ -5726,6 +5972,9 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req
5726
5972
  h = stbi__get32be(s);
5727
5973
  w = stbi__get32be(s);
5728
5974
 
5975
+ if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5976
+ if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5977
+
5729
5978
  // Make sure the depth is 8 bits.
5730
5979
  bitdepth = stbi__get16be(s);
5731
5980
  if (bitdepth != 8 && bitdepth != 16)
@@ -5789,7 +6038,7 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req
5789
6038
  // Else if n is 128, noop.
5790
6039
  // Endloop
5791
6040
 
5792
- // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
6041
+ // The RLE-compressed data is preceded by a 2-byte data count for each row in the data,
5793
6042
  // which we're going to just skip.
5794
6043
  stbi__skip(s, h * channelCount * 2 );
5795
6044
 
@@ -6080,6 +6329,10 @@ static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_c
6080
6329
 
6081
6330
  x = stbi__get16be(s);
6082
6331
  y = stbi__get16be(s);
6332
+
6333
+ if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6334
+ if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6335
+
6083
6336
  if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
6084
6337
  if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
6085
6338
 
@@ -6127,7 +6380,7 @@ typedef struct
6127
6380
  int w,h;
6128
6381
  stbi_uc *out; // output buffer (always 4 components)
6129
6382
  stbi_uc *background; // The current "background" as far as a gif is concerned
6130
- stbi_uc *history;
6383
+ stbi_uc *history;
6131
6384
  int flags, bgindex, ratio, transparent, eflags;
6132
6385
  stbi_uc pal[256][4];
6133
6386
  stbi_uc lpal[256][4];
@@ -6188,6 +6441,9 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in
6188
6441
  g->ratio = stbi__get8(s);
6189
6442
  g->transparent = -1;
6190
6443
 
6444
+ if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
6445
+ if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
6446
+
6191
6447
  if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
6192
6448
 
6193
6449
  if (is_info) return 1;
@@ -6215,7 +6471,7 @@ static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
6215
6471
  static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
6216
6472
  {
6217
6473
  stbi_uc *p, *c;
6218
- int idx;
6474
+ int idx;
6219
6475
 
6220
6476
  // recurse to decode the prefixes, since the linked-list is backwards,
6221
6477
  // and working backwards through an interleaved image would be nasty
@@ -6224,12 +6480,12 @@ static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
6224
6480
 
6225
6481
  if (g->cur_y >= g->max_y) return;
6226
6482
 
6227
- idx = g->cur_x + g->cur_y;
6483
+ idx = g->cur_x + g->cur_y;
6228
6484
  p = &g->out[idx];
6229
- g->history[idx / 4] = 1;
6485
+ g->history[idx / 4] = 1;
6230
6486
 
6231
6487
  c = &g->color_table[g->codes[code].suffix * 4];
6232
- if (c[3] > 128) { // don't render transparent pixels;
6488
+ if (c[3] > 128) { // don't render transparent pixels;
6233
6489
  p[0] = c[2];
6234
6490
  p[1] = c[1];
6235
6491
  p[2] = c[0];
@@ -6338,31 +6594,36 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6338
6594
  // two back is the image from two frames ago, used for a very specific disposal format
6339
6595
  static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
6340
6596
  {
6341
- int dispose;
6342
- int first_frame;
6343
- int pi;
6344
- int pcount;
6597
+ int dispose;
6598
+ int first_frame;
6599
+ int pi;
6600
+ int pcount;
6601
+ STBI_NOTUSED(req_comp);
6345
6602
 
6346
6603
  // on first frame, any non-written pixels get the background colour (non-transparent)
6347
- first_frame = 0;
6604
+ first_frame = 0;
6348
6605
  if (g->out == 0) {
6349
- if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6350
- g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6351
- g->background = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6352
- g->history = (stbi_uc *) stbi__malloc(g->w * g->h);
6353
- if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
6354
-
6355
- // image is treated as "tranparent" at the start - ie, nothing overwrites the current background;
6606
+ if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6607
+ if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
6608
+ return stbi__errpuc("too large", "GIF image is too large");
6609
+ pcount = g->w * g->h;
6610
+ g->out = (stbi_uc *) stbi__malloc(4 * pcount);
6611
+ g->background = (stbi_uc *) stbi__malloc(4 * pcount);
6612
+ g->history = (stbi_uc *) stbi__malloc(pcount);
6613
+ if (!g->out || !g->background || !g->history)
6614
+ return stbi__errpuc("outofmem", "Out of memory");
6615
+
6616
+ // image is treated as "transparent" at the start - ie, nothing overwrites the current background;
6356
6617
  // background colour is only used for pixels that are not rendered first frame, after that "background"
6357
- // color refers to teh color that was there the previous frame.
6358
- memset( g->out, 0x00, 4 * g->w * g->h );
6359
- memset( g->background, 0x00, 4 * g->w * g->h ); // state of the background (starts transparent)
6360
- memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6361
- first_frame = 1;
6618
+ // color refers to the color that was there the previous frame.
6619
+ memset(g->out, 0x00, 4 * pcount);
6620
+ memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
6621
+ memset(g->history, 0x00, pcount); // pixels that were affected previous frame
6622
+ first_frame = 1;
6362
6623
  } else {
6363
- // second frame - how do we dispoase of the previous one?
6364
- dispose = (g->eflags & 0x1C) >> 2;
6365
- pcount = g->w * g->h;
6624
+ // second frame - how do we dispose of the previous one?
6625
+ dispose = (g->eflags & 0x1C) >> 2;
6626
+ pcount = g->w * g->h;
6366
6627
 
6367
6628
  if ((dispose == 3) && (two_back == 0)) {
6368
6629
  dispose = 2; // if I don't have an image to revert back to, default to the old background
@@ -6371,32 +6632,32 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6371
6632
  if (dispose == 3) { // use previous graphic
6372
6633
  for (pi = 0; pi < pcount; ++pi) {
6373
6634
  if (g->history[pi]) {
6374
- memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
6635
+ memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
6375
6636
  }
6376
6637
  }
6377
- } else if (dispose == 2) {
6378
- // restore what was changed last frame to background before that frame;
6638
+ } else if (dispose == 2) {
6639
+ // restore what was changed last frame to background before that frame;
6379
6640
  for (pi = 0; pi < pcount; ++pi) {
6380
6641
  if (g->history[pi]) {
6381
- memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
6642
+ memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
6382
6643
  }
6383
6644
  }
6384
6645
  } else {
6385
- // This is a non-disposal case eithe way, so just
6646
+ // This is a non-disposal case eithe way, so just
6386
6647
  // leave the pixels as is, and they will become the new background
6387
6648
  // 1: do not dispose
6388
6649
  // 0: not specified.
6389
6650
  }
6390
6651
 
6391
- // background is what out is after the undoing of the previou frame;
6392
- memcpy( g->background, g->out, 4 * g->w * g->h );
6652
+ // background is what out is after the undoing of the previou frame;
6653
+ memcpy( g->background, g->out, 4 * g->w * g->h );
6393
6654
  }
6394
6655
 
6395
- // clear my history;
6656
+ // clear my history;
6396
6657
  memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6397
6658
 
6398
6659
  for (;;) {
6399
- int tag = stbi__get8(s);
6660
+ int tag = stbi__get8(s);
6400
6661
  switch (tag) {
6401
6662
  case 0x2C: /* Image Descriptor */
6402
6663
  {
@@ -6418,6 +6679,13 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6418
6679
  g->cur_x = g->start_x;
6419
6680
  g->cur_y = g->start_y;
6420
6681
 
6682
+ // if the width of the specified rectangle is 0, that means
6683
+ // we may not see *any* pixels or the image is malformed;
6684
+ // to make sure this is caught, move the current y down to
6685
+ // max_y (which is what out_gif_code checks).
6686
+ if (w == 0)
6687
+ g->cur_y = g->max_y;
6688
+
6421
6689
  g->lflags = stbi__get8(s);
6422
6690
 
6423
6691
  if (g->lflags & 0x40) {
@@ -6434,19 +6702,19 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6434
6702
  } else if (g->flags & 0x80) {
6435
6703
  g->color_table = (stbi_uc *) g->pal;
6436
6704
  } else
6437
- return stbi__errpuc("missing color table", "Corrupt GIF");
6438
-
6705
+ return stbi__errpuc("missing color table", "Corrupt GIF");
6706
+
6439
6707
  o = stbi__process_gif_raster(s, g);
6440
- if (o == NULL) return NULL;
6708
+ if (!o) return NULL;
6441
6709
 
6442
- // if this was the first frame,
6443
- pcount = g->w * g->h;
6710
+ // if this was the first frame,
6711
+ pcount = g->w * g->h;
6444
6712
  if (first_frame && (g->bgindex > 0)) {
6445
6713
  // if first frame, any pixel not drawn to gets the background color
6446
6714
  for (pi = 0; pi < pcount; ++pi) {
6447
6715
  if (g->history[pi] == 0) {
6448
- g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
6449
- memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
6716
+ g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
6717
+ memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
6450
6718
  }
6451
6719
  }
6452
6720
  }
@@ -6457,7 +6725,7 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6457
6725
  case 0x21: // Comment Extension.
6458
6726
  {
6459
6727
  int len;
6460
- int ext = stbi__get8(s);
6728
+ int ext = stbi__get8(s);
6461
6729
  if (ext == 0xF9) { // Graphic Control Extension.
6462
6730
  len = stbi__get8(s);
6463
6731
  if (len == 4) {
@@ -6466,23 +6734,23 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6466
6734
 
6467
6735
  // unset old transparent
6468
6736
  if (g->transparent >= 0) {
6469
- g->pal[g->transparent][3] = 255;
6470
- }
6737
+ g->pal[g->transparent][3] = 255;
6738
+ }
6471
6739
  if (g->eflags & 0x01) {
6472
6740
  g->transparent = stbi__get8(s);
6473
6741
  if (g->transparent >= 0) {
6474
- g->pal[g->transparent][3] = 0;
6742
+ g->pal[g->transparent][3] = 0;
6475
6743
  }
6476
6744
  } else {
6477
6745
  // don't need transparent
6478
- stbi__skip(s, 1);
6479
- g->transparent = -1;
6746
+ stbi__skip(s, 1);
6747
+ g->transparent = -1;
6480
6748
  }
6481
6749
  } else {
6482
6750
  stbi__skip(s, len);
6483
6751
  break;
6484
6752
  }
6485
- }
6753
+ }
6486
6754
  while ((len = stbi__get8(s)) != 0) {
6487
6755
  stbi__skip(s, len);
6488
6756
  }
@@ -6501,15 +6769,17 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6501
6769
  static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6502
6770
  {
6503
6771
  if (stbi__gif_test(s)) {
6504
- int layers = 0;
6772
+ int layers = 0;
6505
6773
  stbi_uc *u = 0;
6506
6774
  stbi_uc *out = 0;
6507
- stbi_uc *two_back = 0;
6775
+ stbi_uc *two_back = 0;
6508
6776
  stbi__gif g;
6509
- int stride;
6777
+ int stride;
6778
+ int out_size = 0;
6779
+ int delays_size = 0;
6510
6780
  memset(&g, 0, sizeof(g));
6511
6781
  if (delays) {
6512
- *delays = 0;
6782
+ *delays = 0;
6513
6783
  }
6514
6784
 
6515
6785
  do {
@@ -6519,44 +6789,58 @@ static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y,
6519
6789
  if (u) {
6520
6790
  *x = g.w;
6521
6791
  *y = g.h;
6522
- ++layers;
6523
- stride = g.w * g.h * 4;
6524
-
6792
+ ++layers;
6793
+ stride = g.w * g.h * 4;
6794
+
6525
6795
  if (out) {
6526
- out = (stbi_uc*) STBI_REALLOC( out, layers * stride );
6796
+ void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
6797
+ if (NULL == tmp) {
6798
+ STBI_FREE(g.out);
6799
+ STBI_FREE(g.history);
6800
+ STBI_FREE(g.background);
6801
+ return stbi__errpuc("outofmem", "Out of memory");
6802
+ }
6803
+ else {
6804
+ out = (stbi_uc*) tmp;
6805
+ out_size = layers * stride;
6806
+ }
6807
+
6527
6808
  if (delays) {
6528
- *delays = (int*) STBI_REALLOC( *delays, sizeof(int) * layers );
6809
+ *delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
6810
+ delays_size = layers * sizeof(int);
6529
6811
  }
6530
6812
  } else {
6531
- out = (stbi_uc*)stbi__malloc( layers * stride );
6813
+ out = (stbi_uc*)stbi__malloc( layers * stride );
6814
+ out_size = layers * stride;
6532
6815
  if (delays) {
6533
- *delays = (int*) stbi__malloc( layers * sizeof(int) );
6816
+ *delays = (int*) stbi__malloc( layers * sizeof(int) );
6817
+ delays_size = layers * sizeof(int);
6534
6818
  }
6535
6819
  }
6536
- memcpy( out + ((layers - 1) * stride), u, stride );
6820
+ memcpy( out + ((layers - 1) * stride), u, stride );
6537
6821
  if (layers >= 2) {
6538
- two_back = out - 2 * stride;
6822
+ two_back = out - 2 * stride;
6539
6823
  }
6540
6824
 
6541
6825
  if (delays) {
6542
- (*delays)[layers - 1U] = g.delay;
6826
+ (*delays)[layers - 1U] = g.delay;
6543
6827
  }
6544
6828
  }
6545
- } while (u != 0);
6829
+ } while (u != 0);
6546
6830
 
6547
- // free temp buffer;
6548
- STBI_FREE(g.out);
6549
- STBI_FREE(g.history);
6550
- STBI_FREE(g.background);
6831
+ // free temp buffer;
6832
+ STBI_FREE(g.out);
6833
+ STBI_FREE(g.history);
6834
+ STBI_FREE(g.background);
6551
6835
 
6552
- // do the final conversion after loading everything;
6836
+ // do the final conversion after loading everything;
6553
6837
  if (req_comp && req_comp != 4)
6554
6838
  out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h);
6555
6839
 
6556
- *z = layers;
6840
+ *z = layers;
6557
6841
  return out;
6558
6842
  } else {
6559
- return stbi__errpuc("not GIF", "Image was not as a gif type.");
6843
+ return stbi__errpuc("not GIF", "Image was not as a gif type.");
6560
6844
  }
6561
6845
  }
6562
6846
 
@@ -6565,6 +6849,7 @@ static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req
6565
6849
  stbi_uc *u = 0;
6566
6850
  stbi__gif g;
6567
6851
  memset(&g, 0, sizeof(g));
6852
+ STBI_NOTUSED(ri);
6568
6853
 
6569
6854
  u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6570
6855
  if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
@@ -6573,14 +6858,17 @@ static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req
6573
6858
  *y = g.h;
6574
6859
 
6575
6860
  // moved conversion to after successful load so that the same
6576
- // can be done for multiple frames.
6861
+ // can be done for multiple frames.
6577
6862
  if (req_comp && req_comp != 4)
6578
6863
  u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
6864
+ } else if (g.out) {
6865
+ // if there was an error and we allocated an image buffer, free it!
6866
+ STBI_FREE(g.out);
6579
6867
  }
6580
6868
 
6581
- // free buffers needed for multiple frame loading;
6869
+ // free buffers needed for multiple frame loading;
6582
6870
  STBI_FREE(g.history);
6583
- STBI_FREE(g.background);
6871
+ STBI_FREE(g.background);
6584
6872
 
6585
6873
  return u;
6586
6874
  }
@@ -6705,6 +6993,9 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
6705
6993
  token += 3;
6706
6994
  width = (int) strtol(token, NULL, 10);
6707
6995
 
6996
+ if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
6997
+ if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
6998
+
6708
6999
  *x = width;
6709
7000
  *y = height;
6710
7001
 
@@ -6852,7 +7143,12 @@ static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
6852
7143
  return 0;
6853
7144
  if (x) *x = s->img_x;
6854
7145
  if (y) *y = s->img_y;
6855
- if (comp) *comp = info.ma ? 4 : 3;
7146
+ if (comp) {
7147
+ if (info.bpp == 24 && info.ma == 0xff000000)
7148
+ *comp = 3;
7149
+ else
7150
+ *comp = info.ma ? 4 : 3;
7151
+ }
6856
7152
  return 1;
6857
7153
  }
6858
7154
  #endif
@@ -7014,6 +7310,9 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req
7014
7310
  if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n))
7015
7311
  return 0;
7016
7312
 
7313
+ if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7314
+ if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7315
+
7017
7316
  *x = s->img_x;
7018
7317
  *y = s->img_y;
7019
7318
  if (comp) *comp = s->img_n;
@@ -7238,6 +7537,7 @@ STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user
7238
7537
 
7239
7538
  /*
7240
7539
  revision history:
7540
+ 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
7241
7541
  2.19 (2018-02-11) fix warning
7242
7542
  2.18 (2018-01-30) fix warnings
7243
7543
  2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug