MCPcopy Create free account
hub / github.com/FirebirdSQL/firebird / CEscapeString

Function CEscapeString

extern/re2/util/strutil.cc:24–61  ·  view source on GitHub ↗

---------------------------------------------------------------------- CEscapeString() Copies 'src' to 'dest', escaping dangerous characters using C-style escape sequences. 'src' and 'dest' should not overlap. Returns the number of bytes written to 'dest' (not including the \0) or (size_t)-1 if there was insufficient space. ----------------------------------------------------------------------

Source from the content-addressed store, hash-verified

22// or (size_t)-1 if there was insufficient space.
23// ----------------------------------------------------------------------
24static size_t CEscapeString(const char* src, size_t src_len,
25 char* dest, size_t dest_len) {
26 const char* src_end = src + src_len;
27 size_t used = 0;
28
29 for (; src < src_end; src++) {
30 if (dest_len - used < 2) // space for two-character escape
31 return (size_t)-1;
32
33 unsigned char c = *src;
34 switch (c) {
35 case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break;
36 case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break;
37 case '\t': dest[used++] = '\\'; dest[used++] = 't'; break;
38 case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break;
39 case '\'': dest[used++] = '\\'; dest[used++] = '\''; break;
40 case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break;
41 default:
42 // Note that if we emit \xNN and the src character after that is a hex
43 // digit then that digit must be escaped too to prevent it being
44 // interpreted as part of the character code by C.
45 if (c < ' ' || c > '~') {
46 if (dest_len - used < 5) // space for four-character escape + \0
47 return (size_t)-1;
48 snprintf(dest + used, 5, "\\%03o", c);
49 used += 4;
50 } else {
51 dest[used++] = c; break;
52 }
53 }
54 }
55
56 if (dest_len - used < 1) // make sure that there is room for \0
57 return (size_t)-1;
58
59 dest[used] = '\0'; // doesn't count towards return value though
60 return used;
61}
62
63// ----------------------------------------------------------------------
64// CEscape()

Callers 1

CEscapeFunction · 0.85

Calls 1

snprintfFunction · 0.50

Tested by

no test coverage detected