Determine how to read the hex stream based on the first token.
| 185 | |
| 186 | // Determine how to read the hex stream based on the first token. |
| 187 | void DetermineMode() { |
| 188 | SkipSpace(); |
| 189 | |
| 190 | // Read 11 bytes, that is the size of the biggest token (10) + one more. |
| 191 | char first_token[11]; |
| 192 | for (uint32_t i = 0; i < 11; ++i) { |
| 193 | first_token[i] = Next(); |
| 194 | } |
| 195 | |
| 196 | // Table of how to match the first token with a mode. |
| 197 | struct { |
| 198 | const char* expect; |
| 199 | bool must_have_delimiter; |
| 200 | HexMode mode; |
| 201 | } parse_info[] = { |
| 202 | {"0x07230203", true, HexMode::Words}, |
| 203 | {"0x7230203", true, HexMode::Words}, |
| 204 | {"x07230203", true, HexMode::Words}, |
| 205 | {"x7230203", true, HexMode::Words}, |
| 206 | |
| 207 | {"0x07", true, HexMode::BytesBigEndian}, |
| 208 | {"0x7", true, HexMode::BytesBigEndian}, |
| 209 | {"x07", true, HexMode::BytesBigEndian}, |
| 210 | {"x7", true, HexMode::BytesBigEndian}, |
| 211 | |
| 212 | {"0x03", true, HexMode::BytesLittleEndian}, |
| 213 | {"0x3", true, HexMode::BytesLittleEndian}, |
| 214 | {"x03", true, HexMode::BytesLittleEndian}, |
| 215 | {"x3", true, HexMode::BytesLittleEndian}, |
| 216 | |
| 217 | {"07", false, HexMode::StreamBigEndian}, |
| 218 | {"03", false, HexMode::StreamLittleEndian}, |
| 219 | }; |
| 220 | |
| 221 | // Check to see if any of the possible first tokens are matched. If not, |
| 222 | // this is not a recognized hex stream. |
| 223 | encountered_error_ = true; |
| 224 | for (const auto& info : parse_info) { |
| 225 | const size_t expect_len = strlen(info.expect); |
| 226 | const bool matches_expect = |
| 227 | MatchIgnoreCase(first_token, info.expect, expect_len); |
| 228 | const bool satisfies_delimeter = |
| 229 | !info.must_have_delimiter || IsSpace(first_token[expect_len]); |
| 230 | if (matches_expect && satisfies_delimeter) { |
| 231 | mode_ = info.mode; |
| 232 | encountered_error_ = false; |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (encountered_error_) { |
| 238 | fprintf(stderr, |
| 239 | "error: hex format detected, but pattern '%.11s' is not " |
| 240 | "recognized '%s'\n", |
| 241 | first_token, filename_); |
| 242 | } |
| 243 | |
| 244 | // Reset the position to restart parsing with the determined mode. |
nothing calls this directly
no test coverage detected