1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "libbb.h"
- static unsigned hex_to_bin(unsigned char c)
- {
- unsigned v;
- v = c - '0';
- if (v <= 9)
- return v;
-
- v = (unsigned)(c | 0x20) - 'a';
- if (v <= 5)
- return v + 10;
- return ~0;
- }
- char* FAST_FUNC percent_decode_in_place(char *str, int strict)
- {
-
- char *src = str;
- char *dst = str;
- char c;
- while ((c = *src++) != '\0') {
- unsigned v;
- if (!strict && c == '+') {
- *dst++ = ' ';
- continue;
- }
- if (c != '%') {
- *dst++ = c;
- continue;
- }
- v = hex_to_bin(src[0]);
- if (v > 15) {
- bad_hex:
- if (strict)
- return NULL;
- *dst++ = '%';
- continue;
- }
- v = (v * 16) | hex_to_bin(src[1]);
- if (v > 255)
- goto bad_hex;
- if (strict && (v == '/' || v == '\0')) {
-
- return str + 1;
- }
- *dst++ = v;
- src += 2;
- }
- *dst = '\0';
- return str;
- }
|