Decode an ASCII string, e.g., "\x15\x1b\x19\x04\xaf\x0c\x28\x0a", into the binary string.
| 166 | // Decode an ASCII string, e.g., "\x15\x1b\x19\x04\xaf\x0c\x28\x0a", |
| 167 | // into the binary string. |
| 168 | std::string decode_hex_string(std::string line) { |
| 169 | size_t i = 0; |
| 170 | std::string ret; |
| 171 | |
| 172 | while (i <= line.length()) { |
| 173 | switch (line[i]) { |
| 174 | case '\\': |
| 175 | if (i + 2 > line.length()) { |
| 176 | std::cerr << "Invalid hex string at: " << i << "\n"; |
| 177 | return ret; |
| 178 | } |
| 179 | switch (line[i + 1]) { |
| 180 | char ent, save; |
| 181 | case '"': |
| 182 | case '\\': |
| 183 | case ' ': |
| 184 | case ';': |
| 185 | line.erase(i, 1); |
| 186 | break; |
| 187 | case 'x': |
| 188 | if (i + 4 > line.length()) { |
| 189 | std::cerr << "Invalid hex string at: " << i << "\n"; |
| 190 | return ret; |
| 191 | } |
| 192 | char* pEnd; |
| 193 | save = line[i + 4]; |
| 194 | line[i + 4] = 0; |
| 195 | ent = char(strtoul(line.data() + i + 2, &pEnd, 16)); |
| 196 | if (*pEnd) { |
| 197 | std::cerr << "Invalid hex string at: " << i << "\n"; |
| 198 | return ret; |
| 199 | } |
| 200 | line[i + 4] = save; |
| 201 | line.replace(i, 4, 1, ent); |
| 202 | break; |
| 203 | default: |
| 204 | std::cerr << "Invalid hex string at: " << i << "\n"; |
| 205 | return ret; |
| 206 | } |
| 207 | default: |
| 208 | i++; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return line.substr(0, i); |
| 213 | } |
| 214 | |
| 215 | int parseDecodeCommandLine(DecodeParams* param, CSimpleOpt* args) { |
| 216 | while (args->Next()) { |