Exemplo n.º 1
0
// Reads untill the end of the line, returns a
// chunk header size if it sees it.
int HttpTcpConnection::readChunkHeader()
{

    char line[100];
    char * start = line;
    char * end = NULL;

    // Reads untill the end of the line,
    // resulting line buffer will be null
    // terminated without including new lines
    // \r\n
    if (readLine(line, sizeof(line)) > 0)
    {
        // Trim string, cut at first space
        end = strchr(line, ' ');
        if (end != NULL)
            line[end - start] = '\0'; // make null

        // convert the resulting hex string
        // to a number
        return hex2decimal(line);
    }

    return HTTP_RECV_CHNK_ERROR;
}
Exemplo n.º 2
0
/* Compare the binary fingerprint (hash of host key) with the
 * host_key_check parameter.
 */
static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
                               const char *host_key_check)
{
    unsigned c;

    while (len > 0) {
        while (*host_key_check == ':')
            host_key_check++;
        if (!qemu_isxdigit(host_key_check[0]) ||
            !qemu_isxdigit(host_key_check[1]))
            return 1;
        c = hex2decimal(host_key_check[0]) * 16 +
            hex2decimal(host_key_check[1]);
        if (c - *fingerprint != 0)
            return c - *fingerprint;
        fingerprint++;
        len--;
        host_key_check += 2;
    }
    return *host_key_check - '\0';
}
Exemplo n.º 3
0
void main() {
   char* hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
   mpz_t decimal;
   mpz_init(decimal);
   int exponent = 0;
   for (int i = strlen(hex)-1; i >= 0; i--) {
      int number = hex2decimal(hex[i]);
      // decimal += number * (int)Math.pow(16, exponent);
      mpz_t power;
      // mpz_t must be initialized before mpz_ui_pow_ui()
      mpz_init(power);
      mpz_ui_pow_ui(power, 16, exponent);
      mpz_addmul_ui(decimal, power, number);
      // printf("hex: %c, number: %d, exponent: %d, decimal: ", hex[i], number, exponent);
      // mpz_out_str(stdout, 10, decimal);
      // printf("\n");
      exponent += 1;
   }

   char* builder = malloc(sizeof(strlen(hex)));
   int divider = 64;
   mpz_t number;
   mpz_init_set(number, decimal);
   int index = 0;
   while (mpz_cmp_ui(number, divider) > 0) {
      mpz_t quotient;
      int remainder = mpz_tdiv_q_ui(quotient, number, divider);
      char letter = decimal2base64(remainder);
      builder[index] = letter;
      index++;
      mpz_set(number, quotient);
   }
   mpz_t quotient;
   int remainder = mpz_tdiv_q_ui(quotient, number, divider);
   char letter = decimal2base64(remainder);
   builder[index] = letter;
   index++;
   builder[index] = '\0';
   char* result = malloc(sizeof(strlen(builder)+1));
   strcpy(result, builder);
   reverse(result);
   puts(result);
   char* base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t";
   printf("hex:     %s\n", hex);
   printf("decimal: ");
   mpz_out_str(stdout, 10, decimal);
   printf("\n");
   printf("base64:  %s\n", base64);
   printf(strcmp(result, base64) == 0 ? "passed\n" : "failed\n");
}