11/*
22 * This is a modified version of binary_to_compressed_c.cpp
3- * it returns the generated C array as a string.
3+ * from the imgui library see:
4+ * https://github.com/ocornut/imgui/tree/master/misc/fonts
45 */
56#include " FTB.hpp"
67
7- #define _CRT_SECURE_NO_WARNINGS
88#include < assert.h>
9+ #include < sstream>
910#include < stdio.h>
1011#include < stdlib.h>
1112#include < string.h>
@@ -17,9 +18,15 @@ typedef unsigned char stb_uchar;
1718stb_uint stb_compress (stb_uchar *out, stb_uchar *in, stb_uint len);
1819
1920std::string Convert (const char *filename, const char *symbol) {
20- FILE *f = fopen (filename, " rb" );
21+ FILE *f;
22+ errno_t err;
23+ err = fopen_s (&f, filename, " rb" );
2124
22- if (!f) return " " ;
25+ if (!f)
26+ return " " ;
27+
28+ if (err != 0 )
29+ return " " ;
2330
2431 int data_sz;
2532
@@ -102,7 +109,8 @@ static stb_uint stb_adler32(stb_uint adler32, stb_uchar *buffer,
102109 buffer += 8 ;
103110 }
104111
105- for (; i < blocklen; ++i) s1 += *buffer++, s2 += s1;
112+ for (; i < blocklen; ++i)
113+ s1 += *buffer++, s2 += s1;
106114
107115 s1 %= ADLER_MOD , s2 %= ADLER_MOD ;
108116 buflen -= blocklen;
@@ -115,12 +123,11 @@ static unsigned int stb_matchlen(stb_uchar *m1, stb_uchar *m2,
115123 stb_uint maxlen) {
116124 stb_uint i;
117125 for (i = 0 ; i < maxlen; ++i)
118- if (m1[i] != m2[i]) return i;
126+ if (m1[i] != m2[i])
127+ return i;
119128 return i;
120129}
121130
122- // simple implementation that just takes the source data in a big block
123-
124131static stb_uchar *stb__out;
125132static FILE *stb__outfile;
126133static stb_uint stb__outbytes;
@@ -130,14 +137,12 @@ static void stb__write(unsigned char v) {
130137 ++stb__outbytes;
131138}
132139
133- // #define stb_out(v) (stb__out ? *stb__out++ = (stb_uchar) (v) :
134- // stb__write((stb_uchar) (v)))
135- #define stb_out (v ) \
136- do { \
137- if (stb__out) \
138- *stb__out++ = (stb_uchar)(v); \
139- else \
140- stb__write ((stb_uchar)(v)); \
140+ #define stb_out (v ) \
141+ do { \
142+ if (stb__out) \
143+ *stb__out++ = (stb_uchar)(v); \
144+ else \
145+ stb__write ((stb_uchar)(v)); \
141146 } while (0 )
142147
143148static void stb_out2 (stb_uint v) {
@@ -169,7 +174,7 @@ static void outliterals(stb_uchar *in, int numlit) {
169174 stb_out (0x000020 + numlit - 1 );
170175 else if (numlit <= 2048 )
171176 stb_out2 (0x000800 + numlit - 1 );
172- else /* numlit <= 65536) */
177+ else
173178 stb_out3 (0x070000 + numlit - 1 );
174179
175180 if (stb__out) {
@@ -179,7 +184,7 @@ static void outliterals(stb_uchar *in, int numlit) {
179184 fwrite (in, 1 , numlit, stb__outfile);
180185}
181186
182- static int stb__window = 0x40000 ; // 256K
187+ static int stb__window = 0x40000 ;
183188
184189static int stb_not_crap (int best, int dist) {
185190 return ((best > 2 && dist <= 0x00100 ) || (best > 5 && dist <= 0x04000 ) ||
@@ -188,8 +193,6 @@ static int stb_not_crap(int best, int dist) {
188193
189194static stb_uint stb__hashsize = 32768 ;
190195
191- // note that you can play with the hashing functions all you
192- // want without needing to change the decompressor
193196#define stb__hc (q, h, c ) (((h) << 7 ) + ((h) >> 25 ) + q[c])
194197#define stb__hc2 (q, h, c, d ) (((h) << 14 ) + ((h) >> 18 ) + (q[c] << 7 ) + q[d])
195198#define stb__hc3 (q, c, d, e ) ((q[c] << 14 ) + (q[d] << 7 ) + q[e])
@@ -207,9 +210,6 @@ static int stb_compress_chunk(stb_uchar *history, stb_uchar *start,
207210
208211#define STB__SCRAMBLE (h ) (((h) + ((h) >> 16 )) & mask)
209212
210- // stop short of the end so we don't scan off the end doing
211- // the hashing; this means we won't compress the last few bytes
212- // unless they were part of something longer
213213 while (q < start + length && q + 12 < end) {
214214 int m;
215215 stb_uint h1, h2, h3, h4, h;
@@ -221,45 +221,44 @@ static int stb_compress_chunk(stb_uchar *history, stb_uchar *start,
221221 else
222222 match_max = 65536 ;
223223
224- #define stb__nc (b, d ) \
224+ #define stb__nc (b, d ) \
225225 ((d) <= window && ((b) > 9 || stb_not_crap ((int )(b), (int )(d))))
226226
227- #define STB__TRY (t, p ) /* avoid retrying a match we already tried */ \
228- if (p ? dist != (int )(q - t) : 1 ) \
229- if ((m = stb_matchlen (t, q, match_max)) > best) \
230- if (stb__nc (m, q - (t))) best = m, dist = (int )(q - (t))
227+ #define STB__TRY (t, p ) \
228+ if (p ? dist != (int )(q - t) : 1 ) \
229+ if ((m = stb_matchlen (t, q, match_max)) > best) \
230+ if (stb__nc (m, q - (t))) \
231+ best = m, dist = (int )(q - (t))
231232
232- // rather than search for all matches, only try 4 candidate locations,
233- // chosen based on 4 different hash functions of different lengths.
234- // this strategy is inspired by LZO; hashing is unrolled here using the
235- // 'hc' macro
236233 h = stb__hc3 (q, 0 , 1 , 2 );
237234 h1 = STB__SCRAMBLE (h);
238235 t = chash[h1];
239- if (t) STB__TRY (t, 0 );
236+ if (t)
237+ STB__TRY (t, 0 );
240238 h = stb__hc2 (q, h, 3 , 4 );
241239 h2 = STB__SCRAMBLE (h);
242240 h = stb__hc2 (q, h, 5 , 6 );
243241 t = chash[h2];
244- if (t) STB__TRY (t, 1 );
242+ if (t)
243+ STB__TRY (t, 1 );
245244 h = stb__hc2 (q, h, 7 , 8 );
246245 h3 = STB__SCRAMBLE (h);
247246 h = stb__hc2 (q, h, 9 , 10 );
248247 t = chash[h3];
249- if (t) STB__TRY (t, 1 );
248+ if (t)
249+ STB__TRY (t, 1 );
250250 h = stb__hc2 (q, h, 11 , 12 );
251251 h4 = STB__SCRAMBLE (h);
252252 t = chash[h4];
253- if (t) STB__TRY (t, 1 );
253+ if (t)
254+ STB__TRY (t, 1 );
254255
255- // because we use a shared hash table, can only update it
256- // _after_ we've probed all of them
257256 chash[h1] = chash[h2] = chash[h3] = chash[h4] = q;
258257
259- if (best > 2 ) assert (dist > 0 );
258+ if (best > 2 )
259+ assert (dist > 0 );
260260
261- // see if our best match qualifies
262- if (best < 3 ) { // fast path literals
261+ if (best < 3 ) {
263262 ++q;
264263 } else if (best > 2 && best <= 0x80 && dist <= 0x100 ) {
265264 outliterals (lit_start, (int )(q - lit_start));
@@ -282,7 +281,8 @@ static int stb_compress_chunk(stb_uchar *history, stb_uchar *start,
282281 stb_out3 (0x100000 + dist - 1 );
283282 stb_out2 (best - 1 );
284283 } else if (best > 9 && dist <= 0x1000000 ) {
285- if (best > 65536 ) best = 65536 ;
284+ if (best > 65536 )
285+ best = 65536 ;
286286 outliterals (lit_start, (int )(q - lit_start));
287287 lit_start = (q += best);
288288 if (best <= 0x100 ) {
@@ -294,15 +294,14 @@ static int stb_compress_chunk(stb_uchar *history, stb_uchar *start,
294294 stb_out3 (dist - 1 );
295295 stb_out2 (best - 1 );
296296 }
297- } else { // fallback literals if no match was a balanced tradeoff
297+ } else {
298298 ++q;
299299 }
300300 }
301301
302- // if we didn't get all the way, add the rest to literals
303- if (q - start < length) q = start + length;
302+ if (q - start < length)
303+ q = start + length;
304304
305- // the literals are everything from lit_start to q
306305 *pending_literals = (int )(q - lit_start);
307306
308307 stb__running_adler =
@@ -316,15 +315,17 @@ static int stb_compress_inner(stb_uchar *input, stb_uint length) {
316315
317316 stb_uchar **chash;
318317 chash = (stb_uchar **)malloc (stb__hashsize * sizeof (stb_uchar *));
319- if (chash == nullptr ) return 0 ; // failure
320- for (i = 0 ; i < stb__hashsize; ++i) chash[i] = nullptr ;
318+ if (chash == nullptr )
319+ return 0 ; // failure
320+ for (i = 0 ; i < stb__hashsize; ++i)
321+ chash[i] = nullptr ;
321322
322323 // stream signature
323324 stb_out (0x57 );
324325 stb_out (0xbc );
325326 stb_out2 (0 );
326327
327- stb_out4 (0 ); // 64-bit length requires 32-bit leading 0
328+ stb_out4 (0 ); // 64-bit length requires 32-bit leading 0
328329 stb_out4 (length);
329330 stb_out4 (stb__window);
330331
@@ -338,11 +339,11 @@ static int stb_compress_inner(stb_uchar *input, stb_uint length) {
338339
339340 free (chash);
340341
341- stb_out2 (0x05fa ); // end opcode
342+ stb_out2 (0x05fa ); // end opcode
342343
343344 stb_out4 (stb__running_adler);
344345
345- return 1 ; // success
346+ return 1 ; // success
346347}
347348
348349stb_uint stb_compress (stb_uchar *out, stb_uchar *input, stb_uint length) {
0 commit comments