Example #1
0
ERROR_CODE encode_bencoding(struct type_t *type, unsigned char **encoded_puffer_pointer, size_t *encoded_buffer_length_pointer) {
    /* allocates space for the encoded buffer reference,
    for the string buffer reference and for its length */
    unsigned char *encoded_buffer;
    struct string_buffer_t *string_buffer;
    size_t string_length;

    /* creates the string buffer an then uses it to support
    the encoding of the provided type (top level type) then
    after the encoding is complete joins the string buffer and
    deletes it from memory*/
    create_string_buffer(&string_buffer);
    _encode_type(type, string_buffer);
    join_string_buffer(string_buffer, &encoded_buffer);
    string_length = string_buffer->string_length;
    delete_string_buffer(string_buffer);

    /* updates the references to the encoded buffer pointer and
    the reference to the encoded buffer length (from string length) */
    *encoded_puffer_pointer = encoded_buffer;
    *encoded_buffer_length_pointer = string_length;

    /* raises no error */
    RAISE_NO_ERROR;
}
Example #2
0
ERROR_CODE parameters_http(struct hash_map_t *hash_map, unsigned char **buffer_pointer, size_t *buffer_length_pointer) {
    /* allocates space for an iterator object for an hash map element
    and for the string buffer to be used to collect all the partial
    strings that compose the complete url parameters string */
    struct iterator_t *iterator;
    struct hash_map_element_t *element;
    struct string_buffer_t *string_buffer;

    /* allocates space for the string structure to hold the value of
    the element for the string value reference for the joined string,
    for the buffer string from the element and for the corresponding
    string lengths for both cases */
    struct string_t *string;
    unsigned char *string_value;
    unsigned char *_buffer;
    size_t string_length;
    size_t _length;

    /* allocates and sets the initial value on the flag that controls
    if the iteratio to generate key values is the first one */
    char is_first = 1;

    /* creates a new string buffer and a new hash map
    iterator, these structures are going to be used to
    handle the string from the hash map and to iterate
    over the hash map elements */
    create_string_buffer(&string_buffer);
    create_element_iterator_hash_map(hash_map, &iterator);

    /* iterates continuously arround the hash map element
    the iterator is going to stop the iteration */
    while(TRUE) {
        /* retrieves the next element from the iterator
        and in case such element is invalid breaks the loop */
        get_next_iterator(iterator, (void **) &element);
        if(element == NULL) { break; }

        /* checks if this is the first loop in the iteration
        in it's not emits the and character */
        if(is_first) { is_first = 0; }
        else { append_string_buffer(string_buffer, (unsigned char *) "&"); }

        /* retrieves the current element value as a string structure
        then encodes that value using the url encoding (percent encoding)
        and resets the string reference to contain the new buffer as it'
        own contents (avoids extra memory usage) */
        string = (struct string_t *) element->value;
        url_encode(string->buffer, string->length, &_buffer, &_length);
        string->buffer = _buffer;
        string->length = _length;

        /* adds the various elements for the value to the string buffer
        first the key the the attribution operator and then the value */
        append_string_buffer(string_buffer, (unsigned char *) element->key_string);
        append_string_l_buffer(string_buffer, (unsigned char *) "=", sizeof("=") - 1);
        _append_string_t_buffer(string_buffer, string);
    }

    /* "joins" the string buffer values into a single
    value (from the internal string list) and then
    retrieves the length of the string buffer */
    join_string_buffer(string_buffer, &string_value);
    string_length = string_buffer->string_length;

    /* deletes the hash map iterator and string buffer
    structures, to avoid memory leak */
    delete_iterator_hash_map(hash_map, iterator);
    delete_string_buffer(string_buffer);

    /* updates the buffer pointer reference and the
    buffer length pointer reference with the string
    value and the string length values */
    *buffer_pointer = string_value;
    *buffer_length_pointer = string_length;

    /* raises no error */
    RAISE_NO_ERROR;
}
Example #3
0
int main() {
  int32_t x, y;
  uint32_t a, b, n;
  char c;
  string_buffer_t *s;

  s = &buffer;
  init_string_buffer(s, 0);
  show_test("empty buffer", s);

  string_buffer_reset(s);
  for (c = 'a'; c <= 'z'; c++) {
    string_buffer_append_char(s, c);
  }
  show_test("alphabet", s);

  string_buffer_reset(s);
  for (c = 'a'; c <= 'z'; c++) {
    string_buffer_append_char(s, c);
  }
  string_buffer_append_string(s, "au898ue2bcc90219");
  show_test("alphabet+au898ue2bcc90219", s);

  x = INT32_MIN;
  for (;;){
    sprintf(aux, "signed number: %" PRId32, x);
    string_buffer_reset(s);
    string_buffer_append_int32(s, x);
    show_test(aux, s);
    y = x >> 1;
    if (y == x) break;
    x = y;
  }

  x = INT32_MAX;
  for (;;) {
    sprintf(aux, "signed number: %" PRId32, x);
    string_buffer_reset(s);
    string_buffer_append_int32(s, x);
    show_test(aux, s);
    y = x>>1;
    if (y == x) break;
    x = y;
  }

  a = UINT32_MAX;
  for (;;){
    sprintf(aux, "unsigned number: %" PRIu32, a);
    string_buffer_reset(s);
    string_buffer_append_uint32(s, a);
    show_test(aux, s);
    b = a >> 1;
    if (b == a) break;
    a = b;
  }

  mpz_init(z0);
  mpz_init(z1);
  mpq_init(q0);

  mpz_set_str(z0, "111102222033330123456789", 10);
  string_buffer_reset(s);
  string_buffer_append_mpz(s, z0);
  show_test("mpz: 111102222033330123456789", s);

  mpz_set_str(z0, "-111102222033330123456789", 10);
  string_buffer_reset(s);
  string_buffer_append_mpz(s, z0);
  show_test("mpz: -111102222033330123456789", s);

  string_buffer_reset(s);
  string_buffer_append_mpz(s, z1);
  show_test("mpz: 0", s);

  mpq_set_str(q0, "-98765432109876543210", 10);
  string_buffer_reset(s);
  string_buffer_append_mpq(s, q0);
  show_test("mpq: -98765432109876543210", s);

  mpq_set_str(q0, "-98765432109876543210/38192839777", 10);
  string_buffer_reset(s);
  string_buffer_append_mpq(s, q0);
  show_test("mpq: -98765432109876543210/38192839777", s);

  init_rationals();
  rational_t r0;
  q_init(&r0);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: 0", s);

  q_set_int32(&r0, -12, 73);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: -12/73", s);

  q_set_mpq(&r0, q0);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: -98765432109876543210/38192839777", s);

  q_set_mpz(&r0, z0);
  string_buffer_reset(s);
  string_buffer_append_rational(s, &r0);
  show_test("rational: -111102222033330123456789", s);


  printf("\nBit Vectors\n");
  init_bvconstants();
  bv0 = bvconst_alloc(1);
  bvconst_clear(bv0, 1);
  for (n=1; n<= 32; n++) {
    string_buffer_reset(s);
    string_buffer_append_bvconst(s, bv0, n);
    sprintf(aux, "bv[%" PRIu32"]: 0b000...", n);
    show_test(aux, s);
  }

  for (n=1; n <= 32; n++) {
    bvconst_clear(bv0, 1);
    bvconst_set_bit(bv0, n-1);
    string_buffer_reset(s);
    string_buffer_append_bvconst(s, bv0, n);
    sprintf(aux, "bv[%" PRIu32"]: 0b100...", n);
    show_test(aux, s);
  }


  bvconst_free(bv0, 1);

  cleanup_bvconstants();

  cleanup_rationals();

  mpz_clear(z0);
  mpz_clear(z1);
  mpq_clear(q0);

  delete_string_buffer(s);

  return 0;
}
Example #4
0
ERROR_CODE write_http_error_a(
    struct connection_t *connection,
    char *buffer,
    size_t size,
    enum http_version_e version,
    int error_code,
    char *error_message,
    char *error_description,
    char *realm,
    enum http_keep_alive_e keep_alive,
    connection_data_callback_hu callback,
    void *callback_parameters
) {
    /* allocates space for the result buffer related
    variables (for both the buffer pointer and size) */
    size_t result_length;
    unsigned char *result_buffer;

    /* allocates space for the references the string
    buffer and template handler structures */
    struct string_buffer_t *string_buffer;
    struct template_handler_t *template_handler;

    /* allocates space for the "possible" locally generated
    error description (format based generation) */
    char _error_description[1024];

    /* allocates space to the path to the template file to
    be used for template generation */
    unsigned char template_path[VIRIATUM_MAX_PATH_SIZE];

    /* retrieves the service from the connection structure and
    then uses it to retrieve its options */
    struct service_t *service = connection->service;
    struct service_options_t *options = service->options;

    /* allocates the headers buffer (it will be releases automatically by the writter)
    it need to be allocated in the heap so it gets throught the request cycle */
    char *headers_buffer = buffer == NULL ? MALLOC(VIRIATUM_HTTP_SIZE) : buffer;
    size = size == 0 ? VIRIATUM_HTTP_SIZE : size;

#ifndef VIRIATUM_DEBUG
    /* sets the error description as null in order to avoid any
    display of the (internal) message, otherwise a possible
    security hole would be created */
    error_description = NULL;
#endif

    /* in case the use template flag is set the error
    should be displayed using the template */
    if(options->use_template) {
        /* creates the complete path to the template file */
        SPRINTF(
            (char *) template_path,
            sizeof(template_path),
            "%s%s",
            VIRIATUM_RESOURCES_PATH,
            VIRIATUM_ERROR_PATH
        );

        /* prints a debug message */
        V_DEBUG_F("Processing template file '%s'\n", template_path);

        /* creates the template handler that will hold the error
        information contents */
        create_template_handler(&template_handler);

        /* assigns the various error related variables into the
        template handler to be used, they may be used to display
        information arround the error, note that the error description
        value is conditional and may not be set */
        assign_integer_template_handler(template_handler, (unsigned char *) "error_code", error_code);
        assign_string_template_handler(template_handler, (unsigned char *) "error_message", error_message);
        if(error_description != NULL) {
            assign_string_template_handler(template_handler, (unsigned char *) "error_description", error_description);
        }

        /* processes the file as a template handler, at this point
        the output buffer of the template engine should be populated
        with the complete header information, the apropriate header
        writing method is chosen based on the existence or not of
        the realm authorization field */
        process_template_handler(template_handler, template_path);
        realm == NULL ? write_http_headers_c(
            connection,
            headers_buffer,
            size,
            version,
            error_code,
            error_message,
            keep_alive,
            strlen((char *) template_handler->string_value),
            NO_CACHE,
            TRUE
        ) : write_http_headers_a(
            connection,
            headers_buffer,
            size,
            version,
            error_code,
            error_message,
            keep_alive,
            strlen((char *) template_handler->string_value),
            NO_CACHE,
            realm,
            TRUE
        );

        /* creates a new string buffer to hold the complete set of contents
        to be sent to the client then first writes the buffer containing
        the headers and then the resulting contents from the template handler */
        create_string_buffer(&string_buffer);
        append_string_buffer(string_buffer, (unsigned char *) headers_buffer);
        append_string_buffer(string_buffer, template_handler->string_value);
        join_string_buffer(string_buffer, &result_buffer);
        result_length = string_buffer->string_length;
        delete_string_buffer(string_buffer);

        /* deletes the template handler as all the processing on it
        has been done (buffer generated) */
        delete_template_handler(template_handler);

        /* releases the contents of the headers buffer, no more need to
        continue using them (not requried) */
        FREE(headers_buffer);

        /* writes the resulting buffer into the connection in order to be sent
        to the client (sends the template results) in one chunk */
        write_connection(
            connection,
            result_buffer,
            (unsigned int) result_length,
            (connection_data_callback) callback,
            callback_parameters
        );
    } else {
        /* "stringfies" a possible null error description into a description
        string in order to be correctly displayed then formats the error
        message using the code, message and description */
        error_description = error_description == NULL ? (char *) service->description : error_description;
        SPRINTF(
            _error_description,
            sizeof(_error_description),
            "%d - %s - %s",
            error_code,
            error_message,
            error_description
        );

        /* writes the http static headers to the response and
        then writes the error description itself */
        write_http_headers_m(
            connection,
            headers_buffer,
            size,
            version,
            error_code,
            error_message,
            keep_alive,
            strlen(_error_description),
            NO_CACHE,
            _error_description
        );

        /* writes both the headers to the connection, an then
        registers for the appropriate callbacks */
        write_connection(
            connection,
            (unsigned char *) headers_buffer,
            (unsigned int) strlen(headers_buffer),
            (connection_data_callback) callback,
            callback_parameters
        );
    }

    /* raises no error */
    RAISE_NO_ERROR;
}