static int decode_5(const char *url_data, char *dest) { char *pos = dest; for (size_t i = 0; url_data[i] != '\0'; i++) { if (url_data[i] == '+') // decode space *(pos++) = ' '; else if (url_data[i] == '%') { // decode hex value if (is_hex(url_data[i + 1]) && is_hex(url_data[i + 2])) { // this is a percent encoded value. *(pos++) = (hex_val(url_data[i + 1]) << 4) | hex_val(url_data[i + 2]); i += 2; } else { // there was an error in the URL encoding... return -1; } } else *(pos++) = url_data[i]; } *pos = '\0'; return pos - dest; }
static int build_unifont_glyph(struct unifont_glyph *g, const char *buf) { int val; const char *orig = buf; val = 0; while (*buf && *buf != ':') { val <<= 4; val += hex_val(*buf++); } if (*buf++ != ':') { fprintf(stderr, "genunifont: invalid file format: %s\n", orig); return -EFAULT; } g->codepoint = val; g->len = 0; while (*buf && *buf != '\n' && g->len < MAX_DATA_SIZE) { g->data[g->len] = *buf++; ++g->len; } return 0; }
ssize_t http_decode_url_unsafe(char* dest, const char* url_data) { char* pos = dest; while (*url_data) { if (*url_data == '+') { // decode space *(pos++) = ' '; ++url_data; } else if (*url_data == '%') { // decode hex value // this is a percent encoded value. *(pos++) = (hex_val(url_data[1]) << 4) | hex_val(url_data[2]); url_data += 3; } else *(pos++) = *(url_data++); } *pos = 0; return pos - dest; }
static acpi_status acpi_str_to_uuid(char *str, u8 *uuid) { int i; static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21, 24, 26, 28, 30, 32, 34}; if (strlen(str) != 36) return AE_BAD_PARAMETER; for (i = 0; i < 36; i++) { if (i == 8 || i == 13 || i == 18 || i == 23) { if (str[i] != '-') return AE_BAD_PARAMETER; } else if (!isxdigit(str[i])) return AE_BAD_PARAMETER; } for (i = 0; i < 16; i++) { uuid[i] = hex_val(str[opc_map_to_uuid[i]]) << 4; uuid[i] |= hex_val(str[opc_map_to_uuid[i] + 1]); } return AE_OK; }
int dec_to_hex(int n,char hex[]) { int helper,i; helper = n; for(i=0; helper; i++) { hex[i]=hex_val(helper%16); helper=helper/16; } return 0; }
static void print_unifont_glyph(FILE *out, const struct unifont_glyph *g) { size_t i; uint8_t val; switch (g->len) { case 32: case 64: break; default: fprintf(stderr, "genunifont: invalid data size %d for %x", g->len, g->codepoint); return; } fprintf(out, "%c", g->len / 2); for (i = 0; i < g->len; i += 2) { val = hex_val(g->data[i]) << 4; val |= hex_val(g->data[i + 1]); fprintf(out, "%c", val); } for ( ; i < 64; i += 2) fprintf(out, "%c", 0); }
static int decode_6(const char *url_data, char *dest) { char *pos = dest; while (*url_data != '\0') { if (*url_data == '+') { // decode space *(pos++) = ' '; ++url_data; } else if (*url_data == '%') { // decode hex value // this is a percent encoded value. *(pos++) = (hex_val(url_data[1]) << 4) | hex_val(url_data[2]); url_data += 3; } else *(pos++) = *(url_data++); } *pos = '\0'; return pos - dest; }
static char* unescape_string(char* s, char** end, nx_json_unicode_encoder encoder) { char* p=s; char* d=s; char c; while ((c=*p++)) { if (c=='"') { *d='\0'; *end=p; return s; } else if (c=='\\') { switch (*p) { case '\\': case '/': case '"': *d++=*p++; break; case 'b': *d++='\b'; p++; break; case 'f': *d++='\f'; p++; break; case 'n': *d++='\n'; p++; break; case 'r': *d++='\r'; p++; break; case 't': *d++='\t'; p++; break; case 'u': // unicode { char *ps; int h1, h2, h3, h4; unsigned int codepoint; if (!encoder) { // leave untouched *d++=c; break; } ps=p-1; if ((h1=hex_val(p[1]))<0 || (h2=hex_val(p[2]))<0 || (h3=hex_val(p[3]))<0 || (h4=hex_val(p[4]))<0) { NX_JSON_REPORT_ERROR("invalid unicode escape", p-1); return 0; } codepoint=h1<<12|h2<<8|h3<<4|h4; if ((codepoint & 0xfc00)==0xd800) { // high surrogate; need one more unicode to succeed unsigned int codepoint2; p+=6; if (p[-1]!='\\' || *p!='u' || (h1=hex_val(p[1]))<0 || (h2=hex_val(p[2]))<0 || (h3=hex_val(p[3]))<0 || (h4=hex_val(p[4]))<0) { NX_JSON_REPORT_ERROR("invalid unicode surrogate", ps); return 0; } codepoint2=h1<<12|h2<<8|h3<<4|h4; if ((codepoint2 & 0xfc00)!=0xdc00) { NX_JSON_REPORT_ERROR("invalid unicode surrogate", ps); return 0; } codepoint=0x10000+((codepoint-0xd800)<<10)+(codepoint2-0xdc00); } if (!encoder(codepoint, d, &d)) { NX_JSON_REPORT_ERROR("invalid codepoint", ps); return 0; } p+=5; break; } default: // leave untouched *d++=c; break; } } else { *d++=c; } } NX_JSON_REPORT_ERROR("no closing quote for string", s); return 0; }