AceCommon  1.5.2
Arduino library for low-level common functions and features with no external dependencies
backslash_x_encoding.cpp
1 /*
2 MIT License
3 
4 Copyright (c) 2022 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #include "backslash_x_encoding.h"
26 #include "../url_encoding/url_encoding.h"
27 
28 namespace ace_common {
29 
30 uint8_t backslashXEncode(char* t, size_t tcap, const char* s, size_t* written) {
31  char* tt = t;
32  char* const tend = t + tcap - 1;
33 
34  // The code becomes smaller if we assume an error status as the default.
35  uint8_t status = 1;
36 
37  while (true) {
38  // Check for end of 's', before checking for 'tend'.
39  uint8_t c = *s++;
40  if (c == '\0') {
41  status = 0;
42  break;
43  }
44 
45  // Check for end of 't' before every write to 't'.
46  if (tt >= tend) break;
47 
48  if (c >= ' ' && c < 127) {
49  *tt++ = c;
50  if (c == '\\') {
51  if (tt >= tend) break;
52  *tt++ = '\\';
53  }
54  } else {
55  *tt++ = '\\';
56 
57  if (tt >= tend) break;
58  *tt++ = 'x';
59 
60  char high;
61  char low;
62  byteToHexChar(c, &high, &low);
63 
64  if (tt >= tend) break;
65  *tt++ = high;
66 
67  if (tt >= tend) break;
68  *tt++ = low;
69  }
70  }
71 
72  *tt = '\0';
73  if (written) {
74  *written = tt - t;
75  }
76  return status;
77 }
78 
79 uint8_t backslashXDecode(char* t, size_t tcap, const char* s, size_t* written) {
80  char* tt = t;
81  char* const tend = t + tcap - 1;
82 
83  // The code becomes smaller if we assume an error status as the default.
84  uint8_t status = 1;
85 
86  while (true) {
87  uint8_t c = *s++;
88  if (c == '\0') {
89  status = 0;
90  break;
91  }
92 
93  if (tt >= tend) break;
94 
95  // Decode the next character.
96  if (c == '\\') {
97  char escape = *s++;
98  // Reaching the end of string in the middle of the encoding is an error.
99  if (escape == '\0') break;
100 
101  // Convert \\ or \xHH to character
102  if (escape == '\\') {
103  c = '\\';
104  } else {
105  if (escape != 'x') break;
106 
107  // Verify that "HH" characters are valid hexadecimal.
108  char high = *s++;
109  if (high == '\0') break;
110  if (! isHexChar(high)) break;
111 
112  char low = *s++;
113  if (low == '\0') break;
114  if (! isHexChar(low)) break;
115 
116  c = (hexCharToByte(high) << 4) | hexCharToByte(low);
117  }
118  } else if (c < ' ' || c >= 127) {
119  // If source string contains any unprintable character, mark as error.
120  break;
121  }
122 
123  *tt++ = c;
124  }
125 
126  *tt = '\0';
127  if (written) {
128  *written = tt - t;
129  }
130  return status;
131 }
132 
133 }
Provides 2 functions to perform backslash-x encoding and decoding.
bool isHexChar(char c)
Return true if 'c' is a hex character [0-9a-fA-F].