From c8e2b46eb9e13a9902f6c8ffbabb32bc1e473597 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 30 Dec 2016 22:06:08 +0100 Subject: Prevent trigraphs I hate trigraphs --- bin2c.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'bin2c.c') diff --git a/bin2c.c b/bin2c.c index dcc0b7c..48ebae7 100644 --- a/bin2c.c +++ b/bin2c.c @@ -16,21 +16,22 @@ void usage(){ } // Returns serialised string length -// Keeps state on the previous few characters to aid compression +// Keeps state on the previous few characters to aid compression and prevent +// trigraphs int serialisechar(char *dst,unsigned char c){ static bool lastwasoctal=false; - static unsigned char prevc; // Only used if lastwasoctal==true + static unsigned char prevc; switch(c){ - case '\a': lastwasoctal=false; dst[0]='\\'; dst[1]='a'; return 2; - case '\b': lastwasoctal=false; dst[0]='\\'; dst[1]='b'; return 2; - case '\f': lastwasoctal=false; dst[0]='\\'; dst[1]='f'; return 2; - case '\n': lastwasoctal=false; dst[0]='\\'; dst[1]='n'; return 2; - case '\r': lastwasoctal=false; dst[0]='\\'; dst[1]='r'; return 2; - case '\t': lastwasoctal=false; dst[0]='\\'; dst[1]='t'; return 2; - case '\v': lastwasoctal=false; dst[0]='\\'; dst[1]='v'; return 2; - case '"': lastwasoctal=false; dst[0]='\\'; dst[1]='"'; return 2; - case '\\': lastwasoctal=false; dst[0]='\\'; dst[1]='\\'; return 2; + case '\a': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='a'; return 2; + case '\b': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='b'; return 2; + case '\f': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='f'; return 2; + case '\n': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='n'; return 2; + case '\r': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='r'; return 2; + case '\t': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='t'; return 2; + case '\v': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='v'; return 2; + case '"': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='"'; return 2; + case '\\': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='\\'; return 2; default: if(c<32||c>=127){ lastwasoctal=true; @@ -39,9 +40,14 @@ int serialisechar(char *dst,unsigned char c){ } else if(isdigit(c)&&lastwasoctal&&prevc<'\100'){ prevc=c; return sprintf(dst,"\\%o",(int)c); + } else if(c=='?'&&prevc=='?'){ + lastwasoctal=false; + dst[0]='\\'; dst[1]='?'; + return 2; } else { lastwasoctal=false; dst[0]=c; + prevc=c; return 1; } } -- cgit v1.2.3-70-g09d2