| 267 | } |
| 268 | |
| 269 | static int validate_encoding(const char *path, const char *enc, |
| 270 | const char *data, size_t len, int die_on_error) |
| 271 | { |
| 272 | const char *stripped; |
| 273 | |
| 274 | /* We only check for UTF here as UTF?? can be an alias for UTF-?? */ |
| 275 | if (skip_iprefix(enc, "UTF", &stripped)) { |
| 276 | skip_prefix(stripped, "-", &stripped); |
| 277 | |
| 278 | /* |
| 279 | * Check for detectable errors in UTF encodings |
| 280 | */ |
| 281 | if (has_prohibited_utf_bom(enc, data, len)) { |
| 282 | const char *error_msg = _( |
| 283 | "BOM is prohibited in '%s' if encoded as %s"); |
| 284 | /* |
| 285 | * This advice is shown for UTF-??BE and UTF-??LE encodings. |
| 286 | * We cut off the last two characters of the encoding name |
| 287 | * to generate the encoding name suitable for BOMs. |
| 288 | */ |
| 289 | const char *advise_msg = _( |
| 290 | "The file '%s' contains a byte order " |
| 291 | "mark (BOM). Please use UTF-%.*s as " |
| 292 | "working-tree-encoding."); |
| 293 | int stripped_len = strlen(stripped) - strlen("BE"); |
| 294 | advise(advise_msg, path, stripped_len, stripped); |
| 295 | if (die_on_error) |
| 296 | die(error_msg, path, enc); |
| 297 | else { |
| 298 | return error(error_msg, path, enc); |
| 299 | } |
| 300 | |
| 301 | } else if (is_missing_required_utf_bom(enc, data, len)) { |
| 302 | const char *error_msg = _( |
| 303 | "BOM is required in '%s' if encoded as %s"); |
| 304 | const char *advise_msg = _( |
| 305 | "The file '%s' is missing a byte order " |
| 306 | "mark (BOM). Please use UTF-%sBE or UTF-%sLE " |
| 307 | "(depending on the byte order) as " |
| 308 | "working-tree-encoding."); |
| 309 | advise(advise_msg, path, stripped, stripped); |
| 310 | if (die_on_error) |
| 311 | die(error_msg, path, enc); |
| 312 | else { |
| 313 | return error(error_msg, path, enc); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static void trace_encoding(const char *context, const char *path, |
| 322 | const char *encoding, const char *buf, size_t len) |
no test coverage detected