* C-style name unquoting. * * Quoted should point at the opening double quote. * + Returns 0 if it was able to unquote the string properly, and appends the * result in the strbuf `sb'. * + Returns -1 in case of error, and doesn't touch the strbuf. Though note * that this function will allocate memory in the strbuf, so calling * strbuf_release is mandatory whichever result unquote_c_st
| 384 | * Updates endp pointer to point at one past the ending double quote if given. |
| 385 | */ |
| 386 | int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp) |
| 387 | { |
| 388 | size_t oldlen = sb->len, len; |
| 389 | int ch, ac; |
| 390 | |
| 391 | if (*quoted++ != '"') |
| 392 | return -1; |
| 393 | |
| 394 | for (;;) { |
| 395 | len = strcspn(quoted, "\"\\"); |
| 396 | strbuf_add(sb, quoted, len); |
| 397 | quoted += len; |
| 398 | |
| 399 | switch (*quoted++) { |
| 400 | case '"': |
| 401 | if (endp) |
| 402 | *endp = quoted; |
| 403 | return 0; |
| 404 | case '\\': |
| 405 | break; |
| 406 | default: |
| 407 | goto error; |
| 408 | } |
| 409 | |
| 410 | switch ((ch = *quoted++)) { |
| 411 | case 'a': ch = '\a'; break; |
| 412 | case 'b': ch = '\b'; break; |
| 413 | case 'f': ch = '\f'; break; |
| 414 | case 'n': ch = '\n'; break; |
| 415 | case 'r': ch = '\r'; break; |
| 416 | case 't': ch = '\t'; break; |
| 417 | case 'v': ch = '\v'; break; |
| 418 | |
| 419 | case '\\': case '"': |
| 420 | break; /* verbatim */ |
| 421 | |
| 422 | /* octal values with first digit over 4 overflow */ |
| 423 | case '0': case '1': case '2': case '3': |
| 424 | ac = ((ch - '0') << 6); |
| 425 | if ((ch = *quoted++) < '0' || '7' < ch) |
| 426 | goto error; |
| 427 | ac |= ((ch - '0') << 3); |
| 428 | if ((ch = *quoted++) < '0' || '7' < ch) |
| 429 | goto error; |
| 430 | ac |= (ch - '0'); |
| 431 | ch = ac; |
| 432 | break; |
| 433 | default: |
| 434 | goto error; |
| 435 | } |
| 436 | strbuf_addch(sb, ch); |
| 437 | } |
| 438 | |
| 439 | error: |
| 440 | strbuf_setlen(sb, oldlen); |
| 441 | return -1; |
| 442 | } |
| 443 |