From 7f51929dc40aaba9a3a6574d90b3138cace5884b Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 23 Sep 2016 09:06:09 +0200 Subject: [PATCH] Tools: binary_to_compressed_c: Avoid ?? trigraphs sequences in string outputs (#839) --- extra_fonts/binary_to_compressed_c.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/extra_fonts/binary_to_compressed_c.cpp b/extra_fonts/binary_to_compressed_c.cpp index 57b24079e..338acc5b1 100644 --- a/extra_fonts/binary_to_compressed_c.cpp +++ b/extra_fonts/binary_to_compressed_c.cpp @@ -79,11 +79,18 @@ bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_b if (use_base85_encoding) { fprintf(out, "static const char %s_%sdata_base85[%d+1] =\n \"", symbol, compressed_str, (int)((compressed_sz+3)/4)*5); - for (int i = 0; i < compressed_sz; i += 4) + char prev_c = 0; + for (int src_i = 0; src_i < compressed_sz; src_i += 4) { - unsigned int d = *(unsigned int*)(compressed + i); - fprintf(out, "%c%c%c%c%c", Encode85Byte(d), Encode85Byte(d/85), Encode85Byte(d/7225), Encode85Byte(d/614125), Encode85Byte(d/52200625)); - if ((i % 112) == 112-4) + // This is made a little more complicated by the fact that ??X sequences are interpreted as trigraphs by old C/C++ compilers. So we need to escape pairs of ??. + unsigned int d = *(unsigned int*)(compressed + src_i); + for (unsigned int n5 = 0; n5 < 5; n5++, d /= 85) + { + char c = Encode85Byte(d); + fprintf(out, (c == '?' && prev_c == '?') ? "\\%c" : "%c", c); + prev_c = c; + } + if ((src_i % 112) == 112-4) fprintf(out, "\"\n \""); } fprintf(out, "\";\n\n");