int hashify_process_file(stream_t* input_file, stream_t* output_file, string_t output_filename, bool check_only, hashify_string_t** history) { int result = HASHIFY_RESULT_OK; char line_buffer[HASHIFY_LINEBUFFER_LENGTH]; hashify_string_t* local_hashes = 0; hashify_string_t* local_generated = 0; if (check_only) result = hashify_read_hashes(output_file, &local_hashes); else result = hashify_generate_preamble(output_file, output_filename); memset(line_buffer, 0, sizeof(line_buffer)); while (!stream_eos(input_file) && (result == HASHIFY_RESULT_OK)) { string_t line_string; string_const_t def_string; string_const_t value_string; line_string = stream_read_line_buffer(input_file, line_buffer, sizeof(line_buffer), '\n'); string_split(STRING_ARGS(line_string), STRING_CONST(" \t"), &def_string, &value_string, false); def_string = string_strip(STRING_ARGS(def_string), STRING_CONST(STRING_WHITESPACE)); value_string = string_strip(STRING_ARGS(value_string), STRING_CONST(STRING_WHITESPACE)); if (value_string.length && (value_string.str[0] == '"') && (value_string.str[ value_string.length - 1 ] == '"')) { ++value_string.str; value_string.length -= 2; } if (def_string.length) { hash_t hash_value = hash(STRING_ARGS(value_string)); log_infof(0, STRING_CONST(" %.*s: %.*s -> 0x%" PRIx64), STRING_FORMAT(def_string), STRING_FORMAT(value_string), hash_value); if (check_only) { //Check local consistency result = hashify_check_local_consistency(value_string, hash_value, local_hashes); } else { stream_write_format(output_file, STRING_CONST("#define %.*s static_hash_string(\"%.*s\", %" PRIsize ", 0x%" PRIx64 "ULL)\n"), STRING_FORMAT(def_string), STRING_FORMAT(value_string), value_string.length, hash_value); } if (result == HASHIFY_RESULT_OK) { hashify_string_t hash_string; //Check history result = hashify_check_collisions(value_string, hash_value, *history); //Add to history hash_string.string = string_copy(hash_string.buffer, HASHIFY_STRING_LENGTH, STRING_ARGS(value_string)); hash_string.hash = hash_value; array_push_memcpy(*history, &hash_string); array_push_memcpy(local_generated, &hash_string); } } } if (check_only) { //Check local consistency result = hashify_check_match(local_hashes, local_generated); } array_deallocate(local_hashes); array_deallocate(local_generated); return result; }
int hashify_process_file( stream_t* input_file, stream_t* output_file, bool check_only, hashify_string_t** history ) { int result = HASHIFY_RESULT_OK; char line_buffer[HASHIFY_LINEBUFFER_LENGTH]; hashify_string_t* local_hashes = 0; hashify_string_t* local_generated = 0; if( check_only ) result = hashify_read_hashes( output_file, &local_hashes ); else result = hashify_generate_preamble( output_file ); while( !stream_eos( input_file ) && ( result == HASHIFY_RESULT_OK ) ) { char* def_string = 0; char* value_string = 0; stream_read_line_buffer( input_file, line_buffer, HASHIFY_LINEBUFFER_LENGTH, '\n' ); string_split( line_buffer, " \t", &def_string, &value_string, false ); string_strip( def_string, STRING_WHITESPACE ); string_strip( value_string, STRING_WHITESPACE ); if( string_length( value_string ) && ( value_string[0] == '"' ) && ( value_string[ string_length( value_string ) - 1 ] == '"' ) ) { unsigned int len = string_length( value_string ); memmove( value_string, value_string + 1, len - 2 ); value_string[len-2] = 0; } if( string_length( def_string ) ) { hash_t hash_value = hash( value_string, string_length( value_string ) ); log_infof( " %s: %s -> 0x%llx", def_string, value_string, hash_value ); if( check_only ) { //Check local consistency result = hashify_check_local_consistency( value_string, hash_value, local_hashes ); } else { stream_write_format( output_file, "#define %s static_hash_string( \"%s\", 0x%llxULL )\n", def_string, value_string, hash_value ); } if( result == HASHIFY_RESULT_OK ) { hashify_string_t hash_string; //Check history result = hashify_check_collisions( value_string, hash_value, *history ); //Add to history string_copy( hash_string.string, value_string, HASHIFY_STRING_LENGTH ); hash_string.hash = hash_value; array_push_memcpy( *history, &hash_string ); array_push_memcpy( local_generated, &hash_string ); } } string_deallocate( def_string ); string_deallocate( value_string ); } if( check_only ) { //Check local consistency result = hashify_check_match( local_hashes, local_generated ); } array_deallocate( local_hashes ); array_deallocate( local_generated ); return result; }