void locality_profile_destroy() { delete_hash_map(locality_profile.context_hash_map); internal_free(locality_profile.s2t_map); }
ERROR_CODE parameters_http_c(char *buffer, size_t size, size_t count, ...) { /* allocates space for the variable list of arguments provided as arguments to this function and tha should contain sequences of key, value and length */ va_list arguments; /* allocates space for the various indexes to be used in the iteration including the sequence offset and the global index counter */ size_t index; size_t index_g; size_t offset; /* allocates space for the three components of the parameter the key, the value and the length of the provided value buffer (it's not null terminated) */ char *key_s; char *buffer_s; size_t length_s; /* allocates space for the pointer to the buffer that will hold the created parameters string */ char *params_buffer; size_t params_size; /* creates space for the pointer to the map that will contain all the arranjed sequence of keys and values representing the various parameters */ struct hash_map_t *parameters_map; /* statically allocates space in the stack for the various value strings representing the parameter values */ struct string_t strings[256]; /* in case the number of tuples provided as arguments is more that the space available for the value strings must fail with an error otherwise a buffer overflow occurs */ if(count > 256) { RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "Problem creating parameters" ); } /* sets the inial key string value as null so that the value is started initialized, required for safe execution */ key_s = NULL; /* multiplies the count by three as it must contain the real number of arguments and not just the number of tuples of three in it */ count *= 3; /* creates the hash map that is going to be used to temporarly store the various key value associations */ create_hash_map(¶meters_map, 0); /* iterates over the sequence of dynamic arguments provided to the function to retrieve them as sequences of key, value and length, then sets them in the map representing the various parameters */ va_start(arguments, count); for(index = 0; index < count; index++) { offset = index % 3; index_g = index / 3; switch(offset) { case 0: key_s = va_arg(arguments, char *); break; case 1: buffer_s = va_arg(arguments, char *); strings[index_g].buffer = (unsigned char *) buffer_s; break; case 2: length_s = va_arg(arguments, size_t); strings[index_g].length = length_s; set_value_string_hash_map( parameters_map, (unsigned char *) key_s, (void *) &strings[index_g] ); break; } } va_end(arguments); /* generates the (get) parameters for an http request from the provided hash map of key values, the returned buffer is owned by the caller and must be released */ parameters_http( parameters_map, (unsigned char **) ¶ms_buffer, ¶ms_size ); delete_hash_map(parameters_map); /* in case the amount of bytes to be copied from the dynamically created params buffer to the buffer is greater than the size provided raises an error */ if(params_size > size - 1) { FREE(params_buffer); RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "Problem creating parameters" ); } /* copies the generated params buffer into the final buffer defined in the parameters structure, then closes the string with the final character and releases the temporary buffer (params buffer) */ memcpy(buffer, params_buffer, params_size); buffer[params_size] = '\0'; FREE(params_buffer); /* raises no error as the creation of the parameters buffer has completed with success */ RAISE_NO_ERROR; }
ERROR_CODE free_type(struct type_t *type) { /* allocats space for the current type in iteration for the iterator and for the possible hash map element */ struct type_t *current; struct iterator_t *iterator; struct hash_map_element_t *element; /* switches over the type's type in order to execute the proper free operation */ switch(type->type) { case INTEGER_TYPE: /* breaks the switch */ break; case FLOAT_TYPE: /* breaks the switch */ break; case STRING_TYPE: FREE(type->value.value_string); /* breaks the switch */ break; case LIST_TYPE: create_iterator_linked_list(type->value.value_list, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) ¤t); if(current == NULL) { break; } free_type(current); } delete_iterator_linked_list(type->value.value_list, iterator); delete_linked_list(type->value.value_list); /* breaks the switch */ break; case MAP_TYPE: create_element_iterator_hash_map(type->value.value_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } free_type((struct type_t *) element->value); } delete_iterator_hash_map(type->value.value_map, iterator); delete_hash_map(type->value.value_map); /* breaks the switch */ break; case SORT_MAP_TYPE: create_element_iterator_sort_map(type->value.value_sort_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } free_type((struct type_t *) element->value); } delete_iterator_sort_map(type->value.value_sort_map, iterator); delete_sort_map(type->value.value_sort_map); /* breaks the switch */ break; default: /* breaks the switch */ break; } /* deltes the base type structure for the current type, this applies to all the types */ delete_type(type); /* raises no error */ RAISE_NO_ERROR; }
ERROR_CODE auth_file_http(char *auth_file, char *authorization, unsigned char *result) { /* allocates space for the error return value to be used in error checking for function calls */ ERROR_CODE return_value; /* allocates space for the pointer to the passwd key value structure to be created by parsing the auth file */ struct hash_map_t *passwd; /* allocates space to the various pointer values to be used for the separation and treatment of the auth value */ char *pointer; char *authorization_b64; char *authorization_d; char *password_pointer; /* allocates space for the buffers to be used for the username and password values extracted from the authorization token */ char username[128]; char password[128]; char *password_v; /* allocates the various size relates values for the buffer variables creation */ size_t authorization_size; size_t username_size; size_t password_size; /* tries to find the token that separates the authentication type from the authorization base 64 value in case the value is not found raises an error indicating the problem */ pointer = strchr(authorization, ' '); if(pointer == NULL) { RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "Authorization value not valid" ); } authorization_b64 = pointer + 1; /* tries to decode the authorization base 64 value into a plain text value in case the decoding fails, re-raises the error to the upper levels for caller information */ return_value = decode_base64( (unsigned char *) authorization_b64, strlen(authorization_b64), (unsigned char **) &authorization_d, &authorization_size ); if(IS_ERROR_CODE(return_value)) { RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "Problem decoding base 64 authorization" ); } /* tries to find the token that separates the username part of the authorization from the password part in case the value is not found raises an error to the upper levels */ pointer = memchr(authorization_d, ':', authorization_size); if(pointer == NULL) { FREE(authorization_d); RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "No password separator found in authorization" ); } password_pointer = pointer + 1; /* calculates the size of both the username and the password from the diference between the various pointers */ username_size = password_pointer - authorization_d - 1; password_size = authorization_d + authorization_size - password_pointer; /* copies both the username and the password values to the apropriate internal buffers (to be used in comparision) */ memcpy(username, authorization_d, username_size); username[username_size] = '\0'; memcpy(password, password_pointer, password_size); password[password_size] = '\0'; /* processes the passwd file using the provided file path for it, this is an expensive io driven operation, and must be used wth care */ process_passwd_file(auth_file, &passwd); /* retrieves the password verification value for the retrieved username and in case it's valid compares it and sets the result value accordingly */ get_value_string_hash_map( passwd, (unsigned char *) username, (void **) &password_v ); if(password_v != NULL && strcmp(password, password_v) == 0) { *result = TRUE; } else { *result = FALSE; } /* releases the memory associated with the complete set of values in the passwd structure and then releases the memory from the hash map structure itself, then releases the memory associated with the authorization decoded string */ delete_values_hash_map(passwd); delete_hash_map(passwd); FREE(authorization_d); /* raises no error, as everything has been done as possible with no problems created in the processing */ RAISE_NO_ERROR; }