/* Decodes percent-encoded byte string |value| with length |valuelen| and returns the decoded byte string in allocated buffer. The return value is NULL terminated. The caller must free the returned string. */ static char* percent_decode(const uint8_t *value, size_t valuelen) { char *res; res = malloc(valuelen + 1); if(valuelen > 3) { size_t i, j; for(i = 0, j = 0; i < valuelen - 2;) { if(value[i] != '%' || !isxdigit(value[i + 1]) || !isxdigit(value[i + 2])) { res[j++] = value[i++]; continue; } res[j++] = (hex_to_uint(value[i + 1]) << 4) + hex_to_uint(value[i + 2]); i += 3; } memcpy(&res[j], &value[i], 2); res[j + 2] = '\0'; } else { memcpy(res, value, valuelen); res[valuelen] = '\0'; } return res; }
static metalink_checksum *new_metalink_checksum_from_hex_digest (const metalink_digest_def *digest_def, const char *hex_digest) { metalink_checksum *chksum; unsigned char *digest; size_t i; size_t len = strlen(hex_digest); digest = malloc(len/2); for(i = 0; i < len; i += 2) { digest[i/2] = hex_to_uint(hex_digest+i); } chksum = malloc(sizeof(metalink_checksum)); chksum->digest_def = digest_def; chksum->digest = digest; return chksum; }
sse_uint32 sse_hextou32(sse_char *s) { return hex_to_uint(s, sse_strlen(s)); }
static int greet(char *token) { char *user_sig, *tok; char calc_sig[32], hex_sig[64], message[200]; char name[MAX_NAME_LENGTH + 1]; unsigned int *counter = NULL; size_t sig_size, i; int matched = 0; size_t (*sig_methods[])(const char *, size_t, char *) = { #ifndef PATCHED xor_sig, adler32_sig, md5_sig #else md5_hmac_sig #endif }; if ((user_sig = strrchr(token, '|')) == NULL) return -1; *user_sig++ = '\0'; for (i = 0; i < sizeof(sig_methods) / sizeof(sig_methods[0]); i++) { sig_size = sig_methods[i](token, cgc_strlen(token), calc_sig); bin_to_hex(hex_sig, calc_sig, sig_size); if (strncmp(user_sig, hex_sig, 2 * sig_size) == 0) { matched = 1; break; } } if (!matched) return -1; cgc_memset(name, '\x00', MAX_NAME_LENGTH + 1); for (tok = strtok(token, '|'); tok != NULL; tok = strtok(NULL, '|')) { if (strncmp(tok, "name=", 5) == 0) { #ifndef PATCHED strcpy(name, tok + 5); #else strncpy(name, tok + 5, MAX_NAME_LENGTH); #endif } else if (strncmp(tok, "counter=", 8) == 0) { if (cgc_strlen(tok + 8) == 8) counter = (unsigned int *)hex_to_uint(tok + 8); } } if (cgc_strlen(name) == 0 || counter == NULL) return -1; cgc_memset(message, '\x00', sizeof(message)); (*counter)++; strcpy(message, "Hello "); strcat(message, name); strcat(message, ", we've seen you "); itoa(*counter, message + cgc_strlen(message)); strcat(message, " times!\n"); if (write_all(STDOUT, message, cgc_strlen(message)) != cgc_strlen(message)) return -1; return 0; }