summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-30 22:06:08 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-30 22:06:08 +0100
commitc8e2b46eb9e13a9902f6c8ffbabb32bc1e473597 (patch)
treee112b6718ee4e6ab6e2a4745f5736ce87a0da2d9
parent433db0b34b386977ab50234d3b90833d981f6f04 (diff)
Prevent trigraphs
I hate trigraphs
-rw-r--r--bin2c.c28
1 files changed, 17 insertions, 11 deletions
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;
}
}