1 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */
2 /*======================================================================
3 Copyright (C) 2004,2005,2009 Walter Doekes <walter+tthsum@wjd.nu>
4 This file is part of tthsum.
5 
6 tthsum is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 tthsum is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with tthsum.  If not, see <http://www.gnu.org/licenses/>.
18 ======================================================================*/
19 #include "escape.h"
20 
21 #include <stdio.h>
22 
23 #ifdef USE_TEXTS
24 #   include "texts.h"
25 #endif
26 
27 
strtoctrlesc(char * dest,const char * src)28 void strtoctrlesc(char* dest, const char* src) {
29     while (*src != '\0') {
30 	if (0) { /* the optimizer will take care of this */
31 #define ELIF(ctrl, ch) } else if (*src == ctrl) { *dest++ = '\\'; *dest++ = ch
32 	ELIF('\a', 'a');
33 	ELIF('\b', 'b');
34 	ELIF('\t', 't');
35 	ELIF('\n', 'n');
36 	ELIF('\v', 'v');
37 	ELIF('\f', 'f');
38 	ELIF('\r', 'r');
39 	ELIF('\\', '\\');
40 #undef ELIF
41 	} else if ((unsigned char)*src < 0x20) {
42 	    sprintf(dest, "\\x%02hx", (short)*src); /* %hhx is not C90 */
43 	    dest += 4;
44 	} else {
45 	    *dest++ = *src;
46 	}
47 	++src;
48     }
49     *dest = '\0';
50 }
51 
ctrlesctostr(char * dest,const char * src)52 int ctrlesctostr(char* dest, const char* src) {
53     while (*src != '\0') {
54 	if (*src == '\\') {
55 	    ++src;
56 	    if (0) { /* the optimizer will take care of this */
57 #define ELIF(ch, ctrl) } else if (*src == ch) { *dest = ctrl
58 	    ELIF('a', '\a');
59 	    ELIF('b', '\b');
60 	    ELIF('t', '\t');
61 	    ELIF('n', '\n');
62 	    ELIF('v', '\v');
63 	    ELIF('f', '\f');
64 	    ELIF('r', '\r');
65 	    ELIF('\\', '\\');
66 	    } else if (*src == 'x') {
67 		int i;
68 		*dest = 0;
69 		for (i = 0; i < 2; ++i) {
70 		    ++src;
71 		    if ((unsigned char)*src >= '0'
72 			    && (unsigned char)*src <= '9')
73 			*dest += *src - '0';
74 		    else if ((unsigned char)*src >= 'A'
75 			    && (unsigned char)*src <= 'F')
76 			*dest += *src - 'A' + 10;
77 		    else if ((unsigned char)*src >= 'a'
78 			    && (unsigned char)*src <= 'f')
79 			*dest += *src - 'a' + 10;
80 		    else {
81 #ifdef USE_TEXTS
82 			set_error("ctrlesctostr", ESCAPE_INVALID_ESCAPE);
83 #endif /* USE_TEXTS */
84 			return -1;
85 		    }
86 		    if (i == 0)
87 			*dest <<= 4;
88 		}
89 	    } else {
90 #ifdef USE_TEXTS
91 		set_error("ctrlesctostr", ESCAPE_INVALID_ESCAPE);
92 #endif /* USE_TEXTS */
93 		return -1;
94 	    }
95 	} else {
96 	    *dest = *src;
97 	}
98 	++dest;
99 	++src;
100     }
101     *dest = '\0';
102     return 0;
103 }
104