diff options
Diffstat (limited to 'src/lib/tng_io.c')
-rw-r--r-- | src/lib/tng_io.c | 5344 |
1 files changed, 2664 insertions, 2680 deletions
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index d0028bd..6e0f915 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -9,6 +9,8 @@ * modify it under the terms of the Revised BSD License. */ +#include "tng/tng_io.h" + #ifdef USE_STD_INTTYPES_H #include <inttypes.h> #endif @@ -22,11 +24,16 @@ #include <zlib.h> #endif -#include "tng/tng_io.h" #include "tng/md5.h" #include "compression/tng_compress.h" #include "tng/version.h" +#ifdef _MSC_VER +#define fseeko _fseeki64 +#define ftello _ftelli64 +#endif + + struct tng_bond { /** One of the atoms of the bond */ @@ -260,7 +267,7 @@ struct tng_trajectory { /** A handle to the input file */ FILE *input_file; /** The length of the input file */ - long input_file_len; + int64_t input_file_len; /** The path of the output trajectory file */ char *output_file_path; /** A handle to the output file */ @@ -345,9 +352,9 @@ struct tng_trajectory { /** The currently active frame set */ struct tng_trajectory_frame_set current_trajectory_frame_set; /** The pos in the src file of the current frame set */ - long current_trajectory_frame_set_input_file_pos; + int64_t current_trajectory_frame_set_input_file_pos; /** The pos in the dest file of the current frame set */ - long current_trajectory_frame_set_output_file_pos; + int64_t current_trajectory_frame_set_output_file_pos; /** The number of frame sets in the trajectory N.B. Not saved in file and * cannot be trusted to be up-to-date */ int64_t n_trajectory_frame_sets; @@ -613,54 +620,110 @@ static tng_function_status tng_swap_byte_order_little_endian_64 return(TNG_FAILURE); } } -/** Generate the md5 hash of a block. - * The hash is created based on the actual block contents. - * @param block is a general block container. - * @return TNG_SUCCESS (0) if successful. - */ -static tng_function_status tng_block_md5_hash_generate(tng_gen_block_t block) + +static TNG_INLINE void tng_var_32_bit_input_swap(tng_trajectory_t tng_data, int32_t *var) { - md5_state_t md5_state; + if(tng_data->input_endianness_swap_func_32) + { + if(tng_data->input_endianness_swap_func_32(tng_data, var) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } + } +} - md5_init(&md5_state); - md5_append(&md5_state, (md5_byte_t *)block->block_contents, - (int)block->block_contents_size); - md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); +static TNG_INLINE void tng_var_64_bit_input_swap(tng_trajectory_t tng_data, int64_t *var) +{ + if(tng_data->input_endianness_swap_func_64) + { + if(tng_data->input_endianness_swap_func_64(tng_data, var) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } + } +} - return(TNG_SUCCESS); +static TNG_INLINE void tng_var_32_bit_output_swap(tng_trajectory_t tng_data, int32_t *var) +{ + if(tng_data->output_endianness_swap_func_32) + { + if(tng_data->output_endianness_swap_func_32(tng_data, var) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } + } } -/** Compare the current block md5 hash (e.g. read from file) with the md5 hash - * calculated from the current contents. - * If the current md5 hash is not set skip the comparison. - * @param block is a general block container. - * @param results If the hashes match results is set to TNG_TRUE, otherwise it is - * set to TNG_FALSE. If the hash was not set results is set to TNG_TRUE. - * @return TNG_SUCCESS (0) if successful or TNG_FAILURE (1) if the hash was not - * set. - */ -static tng_function_status tng_md5_hash_match_verify(tng_gen_block_t block, - tng_bool *results) +static TNG_INLINE void tng_var_64_bit_output_swap(tng_trajectory_t tng_data, int64_t *var) { - md5_state_t md5_state; - char hash[TNG_MD5_HASH_LEN]; + if(tng_data->output_endianness_swap_func_64) + { + if(tng_data->output_endianness_swap_func_64(tng_data, var) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } + } +} - TNG_ASSERT(block->block_contents_size > 0, "The block contents size must be > 0"); +static int tng_freadstr(FILE* f, char** str) +{ + char temp[TNG_MAX_STR_LEN], *temp_alloc; + int c, count = 0; - *results = TNG_TRUE; - if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0) + do { - return(TNG_FAILURE); + c = fgetc(f); + + if (c == EOF) + { + /* Clear file error flag and return -1 if EOF is read.*/ + clearerr(f); + return -1; + } + else + { + /* Cast c to char */ + temp[count++] = (char) c; + } + } while ((temp[count-1] != '\0') && (count < TNG_MAX_STR_LEN)); + + temp_alloc = realloc(*str, count); + if(!temp_alloc) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", count, + __FILE__, __LINE__); + free(*str); + *str = 0; + return -1; } + *str = temp_alloc; + + strncpy(*str, temp, count); + + return count; +} + +/** Generate the md5 hash of a block. + * The hash is created based on the actual block contents. + * @param block is a general block container. + * @return TNG_SUCCESS (0) if successful. + */ +static tng_function_status tng_block_md5_hash_generate(tng_gen_block_t block) +{ + md5_state_t md5_state; + md5_init(&md5_state); md5_append(&md5_state, (md5_byte_t *)block->block_contents, (int)block->block_contents_size); - md5_finish(&md5_state, (md5_byte_t *)hash); - - if(strncmp(block->md5_hash, hash, 16) != 0) - { - *results = TNG_FALSE; - } + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); return(TNG_SUCCESS); } @@ -741,7 +804,7 @@ static tng_function_status tng_block_init(struct tng_gen_block **block_p) block->id = -1; /* Reset the md5_hash */ - memcpy(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", TNG_MD5_HASH_LEN); + memset(block->md5_hash, '\0', TNG_MD5_HASH_LEN); block->name = 0; block->block_version = TNG_API_VERSION; block->header_contents = 0; @@ -801,7 +864,7 @@ static tng_function_status tng_block_destroy(struct tng_gen_block **block_p) static tng_function_status tng_block_header_read (tng_trajectory_t tng_data, tng_gen_block_t block) { - int len, offset = 0; + int64_t start_pos; TNG_ASSERT(block != 0, "TNG library: Trying to read to uninitialized block (NULL pointer)."); @@ -810,6 +873,8 @@ static tng_function_status tng_block_header_read return(TNG_CRITICAL); } + start_pos = ftello(tng_data->input_file); + /* First read the header size to be able to read the whole header. */ if(fread(&block->header_contents_size, sizeof(block->header_contents_size), 1, tng_data->input_file) == 0) @@ -826,7 +891,7 @@ static tng_function_status tng_block_header_read } /* If this was the size of the general info block check the endianness */ - if(ftell(tng_data->input_file) < 9) + if(ftello(tng_data->input_file) < 9) { /* File is little endian */ if ( *((const char*)&block->header_contents_size) != 0x00 && @@ -881,114 +946,41 @@ static tng_function_status tng_block_header_read } } - if(tng_data->input_endianness_swap_func_64) - { - if(tng_data->input_endianness_swap_func_64(tng_data, - &block->header_contents_size) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - - /* Move the reading position to the beginning of the header. */ - fseek(tng_data->input_file, -(long)sizeof(block->header_contents_size), - SEEK_CUR); - - /* If there is already memory allocated for the contents free it (we do not - * know if the size is correct). */ - if(block->header_contents) - { - free(block->header_contents); - } + tng_var_64_bit_input_swap(tng_data, &block->header_contents_size); - block->header_contents = malloc(block->header_contents_size); - if(!block->header_contents) + if(fread(&block->block_contents_size, sizeof(block->block_contents_size), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->header_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + tng_var_64_bit_input_swap(tng_data, &block->block_contents_size); - /* Read the whole header into header_contents. This way it can be saved - * even if it cannot be interpreted - * for one reason or another. */ - if(fread(block->header_contents, block->header_contents_size, 1, - tng_data->input_file) == 0) + if(fread(&block->id, sizeof(block->id), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot read header. %s: %d\n", __FILE__, __LINE__); + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + tng_var_64_bit_input_swap(tng_data, &block->id); - /* The header contents size has already been read. Skip ahead. */ - offset = sizeof(block->header_contents_size); - - - /* Copy the respective parameters from the header contents block */ - memcpy(&block->block_contents_size, block->header_contents+offset, - sizeof(block->block_contents_size)); - if(tng_data->input_endianness_swap_func_64) - { - if(tng_data->input_endianness_swap_func_64(tng_data, - &block->block_contents_size) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - - offset += sizeof(block->block_contents_size); - - memcpy(&block->id, block->header_contents+offset, sizeof(block->id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &block->id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(block->id); + tng_freadstr(tng_data->input_file, &block->name); - memcpy(block->md5_hash, block->header_contents+offset, TNG_MD5_HASH_LEN); - offset += TNG_MD5_HASH_LEN; - - if(block->name && strcmp(block->name, block->header_contents+offset) != 0) - { - free(block->name); - block->name = 0; - } - len = tng_min_i((int)strlen(block->header_contents+offset) + 1, TNG_MAX_STR_LEN); - if(!block->name) + if(fread(&block->block_version, sizeof(block->block_version), + 1, tng_data->input_file) == 0) { - block->name = malloc(len); - if(!block->name) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - return(TNG_CRITICAL); - } - strncpy(block->name, block->header_contents+offset, len); + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += len; + tng_var_64_bit_input_swap(tng_data, &block->block_version); - memcpy(&block->block_version, block->header_contents+offset, - sizeof(block->block_version)); - if(tng_data->input_endianness_swap_func_64) - { - if(tng_data->input_endianness_swap_func_64(tng_data, - &block->block_version) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } + fseeko(tng_data->input_file, start_pos + block->header_contents_size, SEEK_SET); return(TNG_SUCCESS); } @@ -1061,7 +1053,7 @@ static tng_function_status tng_md5_hash_update(tng_trajectory_t tng_data, return(TNG_CRITICAL); } - fseek(tng_data->output_file, (long)contents_start_pos, SEEK_SET); + fseeko(tng_data->output_file, contents_start_pos, SEEK_SET); if(fread(block->block_contents, block->block_contents_size, 1, tng_data->output_file) == 0) { @@ -1071,7 +1063,7 @@ static tng_function_status tng_md5_hash_update(tng_trajectory_t tng_data, tng_block_md5_hash_generate(block); - fseek(tng_data->output_file, (long)header_start_pos + 3 * sizeof(int64_t), + fseeko(tng_data->output_file, header_start_pos + 3 * sizeof(int64_t), SEEK_SET); fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file); @@ -1104,8 +1096,8 @@ static tng_function_status tng_header_pointers_update tng_block_init(&block); - output_file_pos = ftell(tng_data->output_file); - fseek(tng_data->output_file, 0, SEEK_SET); + output_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, 0, SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -1116,10 +1108,10 @@ static tng_function_status tng_header_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - 5 * - sizeof(int64_t), SEEK_CUR); + fseeko(tng_data->output_file, block->block_contents_size - 5 * + sizeof(int64_t), SEEK_CUR); tng_data->input_file = temp; @@ -1169,7 +1161,7 @@ static tng_function_status tng_header_pointers_update tng_block_destroy(&block); - fseek(tng_data->output_file, (long)output_file_pos, SEEK_SET); + fseeko(tng_data->output_file, output_file_pos, SEEK_SET); return(TNG_SUCCESS); } @@ -1199,7 +1191,7 @@ static tng_function_status tng_frame_set_pointers_update } tng_block_init(&block); - output_file_pos = ftell(tng_data->output_file); + output_file_pos = ftello(tng_data->output_file); tng_data->input_file = tng_data->output_file; @@ -1210,8 +1202,7 @@ static tng_function_status tng_frame_set_pointers_update /* Update next frame set */ if(frame_set->next_frame_set_file_pos > 0) { - fseek(tng_data->output_file, (long)frame_set->next_frame_set_file_pos, - SEEK_SET); + fseeko(tng_data->output_file, frame_set->next_frame_set_file_pos, SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -1222,9 +1213,9 @@ static tng_function_status tng_frame_set_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - (5 * + fseeko(tng_data->output_file, block->block_contents_size - (5 * sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(tng_data->input_endianness_swap_func_64) @@ -1250,12 +1241,12 @@ static tng_function_status tng_frame_set_pointers_update tng_md5_hash_update(tng_data, block, frame_set->next_frame_set_file_pos, contents_start_pos); } - fseek(tng_data->output_file, (long)output_file_pos, SEEK_SET); + fseeko(tng_data->output_file, output_file_pos, SEEK_SET); } /* Update previous frame set */ if(frame_set->prev_frame_set_file_pos > 0) { - fseek(tng_data->output_file, (long)frame_set->prev_frame_set_file_pos, + fseeko(tng_data->output_file, frame_set->prev_frame_set_file_pos, SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) @@ -1267,9 +1258,9 @@ static tng_function_status tng_frame_set_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - (6 * + fseeko(tng_data->output_file, block->block_contents_size - (6 * sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(tng_data->input_endianness_swap_func_64) @@ -1295,14 +1286,14 @@ static tng_function_status tng_frame_set_pointers_update tng_md5_hash_update(tng_data, block, frame_set->prev_frame_set_file_pos, contents_start_pos); } - fseek(tng_data->output_file, (long)output_file_pos, SEEK_SET); + fseeko(tng_data->output_file, output_file_pos, SEEK_SET); } /* Update the frame set one medium stride step after */ if(frame_set->medium_stride_next_frame_set_file_pos > 0) { - fseek(tng_data->output_file, - (long)frame_set->medium_stride_next_frame_set_file_pos, + fseeko(tng_data->output_file, + frame_set->medium_stride_next_frame_set_file_pos, SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) @@ -1314,9 +1305,9 @@ static tng_function_status tng_frame_set_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - (3 * + fseeko(tng_data->output_file, block->block_contents_size - (3 * sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(tng_data->input_endianness_swap_func_64) @@ -1347,9 +1338,9 @@ static tng_function_status tng_frame_set_pointers_update /* Update the frame set one medium stride step before */ if(frame_set->medium_stride_prev_frame_set_file_pos > 0) { - fseek(tng_data->output_file, - (long)frame_set->medium_stride_prev_frame_set_file_pos, - SEEK_SET); + fseeko(tng_data->output_file, + frame_set->medium_stride_prev_frame_set_file_pos, + SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -1360,10 +1351,10 @@ static tng_function_status tng_frame_set_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - (4 * - sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); + fseeko(tng_data->output_file, block->block_contents_size - (4 * + sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(tng_data->input_endianness_swap_func_64) { @@ -1394,9 +1385,9 @@ static tng_function_status tng_frame_set_pointers_update /* Update the frame set one long stride step after */ if(frame_set->long_stride_next_frame_set_file_pos > 0) { - fseek(tng_data->output_file, - (long)frame_set->long_stride_next_frame_set_file_pos, - SEEK_SET); + fseeko(tng_data->output_file, + frame_set->long_stride_next_frame_set_file_pos, + SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -1407,10 +1398,10 @@ static tng_function_status tng_frame_set_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - (1 * - sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); + fseeko(tng_data->output_file, block->block_contents_size - (1 * + sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(tng_data->input_endianness_swap_func_64) { @@ -1440,9 +1431,9 @@ static tng_function_status tng_frame_set_pointers_update /* Update the frame set one long stride step before */ if(frame_set->long_stride_prev_frame_set_file_pos > 0) { - fseek(tng_data->output_file, - (long)frame_set->long_stride_prev_frame_set_file_pos, - SEEK_SET); + fseeko(tng_data->output_file, + frame_set->long_stride_prev_frame_set_file_pos, + SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -1453,10 +1444,10 @@ static tng_function_status tng_frame_set_pointers_update return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); + contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, (long)block->block_contents_size - (2 * - sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); + fseeko(tng_data->output_file, block->block_contents_size - (2 * + sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(tng_data->input_endianness_swap_func_64) { @@ -1484,7 +1475,7 @@ static tng_function_status tng_frame_set_pointers_update } } - fseek(tng_data->output_file, (long)output_file_pos, SEEK_SET); + fseeko(tng_data->output_file, output_file_pos, SEEK_SET); tng_data->input_file = temp; @@ -1502,7 +1493,7 @@ static tng_function_status tng_reread_frame_set_at_file_pos tng_block_init(&block); - fseek(tng_data->input_file, pos, SEEK_SET); + fseeko(tng_data->input_file, pos, SEEK_SET); if(pos > 0) { stat = tng_block_header_read(tng_data, block); @@ -1537,7 +1528,7 @@ static tng_function_status tng_file_pos_of_subsequent_trajectory_block_get tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; - orig_pos = ftell(tng_data->input_file); + orig_pos = ftello(tng_data->input_file); curr_frame_set_pos = tng_data->current_trajectory_frame_set_input_file_pos; *pos = tng_data->first_trajectory_frame_set_input_file_pos; @@ -1547,7 +1538,7 @@ static tng_function_status tng_file_pos_of_subsequent_trajectory_block_get return(TNG_SUCCESS); } - fseek(tng_data->input_file, *pos, SEEK_SET); + fseeko(tng_data->input_file, *pos, SEEK_SET); tng_block_init(&block); /* Read block headers first to see that a frame set block is found. */ @@ -1570,7 +1561,7 @@ static tng_function_status tng_file_pos_of_subsequent_trajectory_block_get /* Read all frame set blocks (not the blocks between them) */ while(frame_set->next_frame_set_file_pos > 0) { - fseek(tng_data->input_file, frame_set->next_frame_set_file_pos, SEEK_SET); + fseeko(tng_data->input_file, frame_set->next_frame_set_file_pos, SEEK_SET); stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL) { @@ -1601,7 +1592,7 @@ static tng_function_status tng_file_pos_of_subsequent_trajectory_block_get /* Re-read the frame set that used to be the current one */ tng_reread_frame_set_at_file_pos(tng_data, curr_frame_set_pos); - fseek(tng_data->input_file, orig_pos, SEEK_SET); + fseeko(tng_data->input_file, orig_pos, SEEK_SET); tng_block_destroy(&block); @@ -1612,9 +1603,9 @@ static tng_function_status tng_frame_set_complete_migrate (tng_trajectory_t tng_data, int64_t block_start_pos, int64_t block_len, - int64_t new_pos) + int64_t new_pos, + const char hash_mode) { - int64_t i; tng_bool updated = TNG_FALSE; char *contents; @@ -1624,7 +1615,7 @@ static tng_function_status tng_frame_set_complete_migrate return(TNG_CRITICAL); } - fseek(tng_data->input_file, block_start_pos, SEEK_SET); + fseeko(tng_data->input_file, block_start_pos, SEEK_SET); contents = malloc(block_len); if(!contents) @@ -1641,7 +1632,7 @@ static tng_function_status tng_frame_set_complete_migrate free(contents); return(TNG_CRITICAL); } - fseek(tng_data->output_file, new_pos, SEEK_SET); + fseeko(tng_data->output_file, new_pos, SEEK_SET); if(fwrite(contents, block_len, 1, tng_data->output_file) != 1) { @@ -1653,7 +1644,7 @@ static tng_function_status tng_frame_set_complete_migrate tng_data->current_trajectory_frame_set_output_file_pos = new_pos; - tng_frame_set_pointers_update(tng_data, TNG_USE_HASH); + tng_frame_set_pointers_update(tng_data, hash_mode); /* Update the general info block if needed */ if(block_start_pos == tng_data->first_trajectory_frame_set_output_file_pos) @@ -1668,15 +1659,12 @@ static tng_function_status tng_frame_set_complete_migrate } if(updated) { - tng_header_pointers_update(tng_data, TNG_USE_HASH); + tng_header_pointers_update(tng_data, hash_mode); } /* Fill the block with NULL to avoid confusion. */ - for(i = 0; i < block_len; i++) - { - contents[i] = '\0'; - } - fseek(tng_data->output_file, block_start_pos, SEEK_SET); + memset(contents, '\0', block_len); + fseeko(tng_data->output_file, block_start_pos, SEEK_SET); /* FIXME: casting block_len to size_t is dangerous */ fwrite(contents, 1, block_len, tng_data->output_file); @@ -1694,12 +1682,12 @@ static tng_function_status tng_length_of_current_frame_set_contents_get tng_gen_block_t block; tng_function_status stat; - orig_pos = ftell(tng_data->input_file); + orig_pos = ftello(tng_data->input_file); curr_frame_set_pos = pos = tng_data->current_trajectory_frame_set_input_file_pos; *len = 0; - fseek(tng_data->input_file, curr_frame_set_pos, SEEK_SET); + fseeko(tng_data->input_file, curr_frame_set_pos, SEEK_SET); tng_block_init(&block); /* Read block headers first to see that a frame set block is found. */ @@ -1715,7 +1703,7 @@ static tng_function_status tng_length_of_current_frame_set_contents_get /* Read the headers of all blocks in the frame set (not the actual contents of them) */ while(stat == TNG_SUCCESS) { - fseek(tng_data->input_file, block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); *len += block->header_contents_size + block->block_contents_size; pos += block->header_contents_size + block->block_contents_size; if(pos >= tng_data->input_file_len) @@ -1732,7 +1720,7 @@ static tng_function_status tng_length_of_current_frame_set_contents_get /* Re-read the frame set that used to be the current one */ tng_reread_frame_set_at_file_pos(tng_data, curr_frame_set_pos); - fseek(tng_data->input_file, orig_pos, SEEK_SET); + fseeko(tng_data->input_file, orig_pos, SEEK_SET); tng_block_destroy(&block); @@ -1753,7 +1741,8 @@ static tng_function_status tng_length_of_current_frame_set_contents_get static tng_function_status tng_migrate_data_in_file (tng_trajectory_t tng_data, int64_t start_pos, - int64_t offset) + int64_t offset, + const char hash_mode) { int64_t traj_start_pos, empty_space, orig_file_pos, frame_set_length; tng_gen_block_t block; @@ -1783,12 +1772,12 @@ static tng_function_status tng_migrate_data_in_file return(TNG_SUCCESS); } - orig_file_pos = ftell(tng_data->input_file); + orig_file_pos = ftello(tng_data->input_file); tng_block_init(&block); while(empty_space < offset) { - fseek(tng_data->input_file, traj_start_pos, SEEK_SET); + fseeko(tng_data->input_file, traj_start_pos, SEEK_SET); stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL) { @@ -1812,7 +1801,8 @@ static tng_function_status tng_migrate_data_in_file return(stat); } stat = tng_frame_set_complete_migrate(tng_data, traj_start_pos, - frame_set_length, tng_data->input_file_len); + frame_set_length, tng_data->input_file_len, + hash_mode); if(stat != TNG_SUCCESS) { tng_data->input_file = temp; @@ -1822,7 +1812,7 @@ static tng_function_status tng_migrate_data_in_file empty_space += frame_set_length; } - fseek(tng_data->input_file, orig_file_pos, SEEK_SET); + fseeko(tng_data->input_file, orig_file_pos, SEEK_SET); tng_block_destroy(&block); return(TNG_SUCCESS); @@ -1850,7 +1840,7 @@ static tng_function_status tng_block_header_len_calculate block->name[0] = 0; } - name_len = tng_min_i((int)strlen(block->name) + 1, TNG_MAX_STR_LEN); + name_len = tng_min_i((unsigned int)strlen(block->name) + 1, TNG_MAX_STR_LEN); /* Calculate the size of the header to write */ *len = sizeof(block->header_contents_size) + @@ -1873,10 +1863,10 @@ static tng_function_status tng_block_header_len_calculate */ static tng_function_status tng_block_header_write (tng_trajectory_t tng_data, - tng_gen_block_t block, - const char hash_mode) + tng_gen_block_t block) { - int name_len, offset = 0; + int name_len; + int64_t temp_i64; TNG_ASSERT(block != 0, "TNG library: Trying to write uninitialized block (NULL pointer)."); @@ -1895,94 +1885,52 @@ static tng_function_status tng_block_header_write return(TNG_CRITICAL); } - if(hash_mode == TNG_USE_HASH) - { - tng_block_md5_hash_generate(block); - } - - if(block->header_contents) - { - free(block->header_contents); - } + name_len = tng_min_i((unsigned int)strlen(block->name) + 1, TNG_MAX_STR_LEN); - block->header_contents = malloc(block->header_contents_size); - if(!block->header_contents) + temp_i64 = block->header_contents_size; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->header_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write header data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - name_len = tng_min_i((int)strlen(block->name) + 1, TNG_MAX_STR_LEN); - - /* First copy all data into the header_contents block and finally write - * the whole block at once. */ - memcpy(block->header_contents, &block->header_contents_size, - sizeof(block->header_contents_size)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = block->block_contents_size; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write header data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(block->header_contents_size); - memcpy(block->header_contents+offset, &block->block_contents_size, - sizeof(block->block_contents_size)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = block->id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write header data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(block->block_contents_size); - memcpy(block->header_contents+offset, &block->id, sizeof(block->id)); - if(tng_data->output_endianness_swap_func_64) + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write header data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(block->id); - memcpy(block->header_contents+offset, block->md5_hash, TNG_MD5_HASH_LEN); - offset += TNG_MD5_HASH_LEN; - - strncpy(block->header_contents+offset, block->name, name_len); - offset += name_len; - - memcpy(block->header_contents+offset, &block->block_version, - sizeof(block->block_version)); - if(tng_data->output_endianness_swap_func_64) + if(fwrite(block->name, name_len, 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write header data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - if(fwrite(block->header_contents, block->header_contents_size, - 1, tng_data->output_file) != 1) + temp_i64 = block->block_version; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Could not write all header data. %s: %d\n", __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write header data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + return(TNG_SUCCESS); } @@ -2098,23 +2046,23 @@ static tng_function_status tng_general_info_block_len_calculate tng_data->forcefield_name[0] = 0; } - first_program_name_len = tng_min_i((int)strlen(tng_data->first_program_name) + 1, + first_program_name_len = tng_min_i((unsigned int)strlen(tng_data->first_program_name) + 1, TNG_MAX_STR_LEN); - last_program_name_len = tng_min_i((int)strlen(tng_data->last_program_name) + 1, + last_program_name_len = tng_min_i((unsigned int)strlen(tng_data->last_program_name) + 1, TNG_MAX_STR_LEN); - first_user_name_len = tng_min_i((int)strlen(tng_data->first_user_name) + 1, + first_user_name_len = tng_min_i((unsigned int)strlen(tng_data->first_user_name) + 1, TNG_MAX_STR_LEN); - last_user_name_len = tng_min_i((int)strlen(tng_data->last_user_name) + 1, + last_user_name_len = tng_min_i((unsigned int)strlen(tng_data->last_user_name) + 1, TNG_MAX_STR_LEN); - first_computer_name_len = tng_min_i((int)strlen(tng_data->first_computer_name) + 1, + first_computer_name_len = tng_min_i((unsigned int)strlen(tng_data->first_computer_name) + 1, TNG_MAX_STR_LEN); - last_computer_name_len = tng_min_i((int)strlen(tng_data->last_computer_name) + 1, + last_computer_name_len = tng_min_i((unsigned int)strlen(tng_data->last_computer_name) + 1, TNG_MAX_STR_LEN); - first_pgp_signature_len = tng_min_i((int)strlen(tng_data->first_pgp_signature) + 1, + first_pgp_signature_len = tng_min_i((unsigned int)strlen(tng_data->first_pgp_signature) + 1, TNG_MAX_STR_LEN); - last_pgp_signature_len = tng_min_i((int)strlen(tng_data->last_pgp_signature) + 1, + last_pgp_signature_len = tng_min_i((unsigned int)strlen(tng_data->last_pgp_signature) + 1, TNG_MAX_STR_LEN); - forcefield_name_len = tng_min_i((int)strlen(tng_data->forcefield_name) + 1, + forcefield_name_len = tng_min_i((unsigned int)strlen(tng_data->forcefield_name) + 1, TNG_MAX_STR_LEN); *len = sizeof(tng_data->time) + @@ -2152,10 +2100,9 @@ static tng_function_status tng_general_info_block_read (tng_trajectory_t tng_data, tng_gen_block_t block, const char hash_mode) { - int len, offset = 0; - tng_bool same_hash; - - void *temp; + int64_t start_pos, curr_file_pos; + char hash[TNG_MD5_HASH_LEN], *temp_data; + md5_state_t md5_state; TNG_ASSERT(block != 0, "TNG library: Trying to read data to an uninitialized block (NULL pointer)"); @@ -2164,277 +2111,191 @@ static tng_function_status tng_general_info_block_read return(TNG_CRITICAL); } - temp = realloc(block->block_contents, block->block_contents_size); - if(!temp) + start_pos = ftello(tng_data->input_file); + + tng_freadstr(tng_data->input_file, &tng_data->first_program_name); + + tng_freadstr(tng_data->input_file, &tng_data->last_program_name); + + tng_freadstr(tng_data->input_file, &tng_data->first_user_name); + + tng_freadstr(tng_data->input_file, &tng_data->last_user_name); + + tng_freadstr(tng_data->input_file, &tng_data->first_computer_name); + + tng_freadstr(tng_data->input_file, &tng_data->last_computer_name); + + tng_freadstr(tng_data->input_file, &tng_data->first_pgp_signature); + + tng_freadstr(tng_data->input_file, &tng_data->last_pgp_signature); + + tng_freadstr(tng_data->input_file, &tng_data->forcefield_name); + + /* Add all the above strings to the MD5 hash at the same time. */ + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - free(block->block_contents); - block->block_contents = 0; - return(TNG_CRITICAL); + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_program_name, strlen(tng_data->first_program_name) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_program_name, strlen(tng_data->last_program_name) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_user_name, strlen(tng_data->first_user_name) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_user_name, strlen(tng_data->last_user_name) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_computer_name, strlen(tng_data->first_computer_name) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_computer_name, strlen(tng_data->last_computer_name) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_pgp_signature, strlen(tng_data->first_pgp_signature) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_pgp_signature, strlen(tng_data->last_pgp_signature) + 1); + md5_append(&md5_state, (md5_byte_t *)tng_data->forcefield_name, strlen(tng_data->forcefield_name) + 1); } - block->block_contents = temp; - /* Read the whole block into block_contents to be able to write it to disk - * even if it cannot be interpreted. */ - if(fread(block->block_contents, block->block_contents_size, 1, - tng_data->input_file) == 0) + if(fread(&tng_data->time, sizeof(tng_data->time), + 1, tng_data->input_file) == 0) { fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - - /* FIXME: Does not check if the size of the contents matches the expected - * size or if the contents can be read. */ - if(hash_mode == TNG_USE_HASH) { - tng_md5_hash_match_verify(block, &same_hash); - if(same_hash != TNG_TRUE) - { - fprintf(stderr, "TNG library: General info block contents corrupt. Hashes do not match. " - "%s: %d\n", - __FILE__, __LINE__); - /* return(TNG_FAILURE); */ - } + md5_append(&md5_state, (md5_byte_t *)&tng_data->time, sizeof(tng_data->time)); } + tng_var_64_bit_input_swap(tng_data, &tng_data->time); - len = tng_min_i((int)strlen(block->block_contents) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->first_program_name, len); - if(!temp) + if(fread(&tng_data->var_num_atoms_flag, sizeof(tng_data->var_num_atoms_flag), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->first_program_name); - tng_data->first_program_name = 0; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - tng_data->first_program_name = temp; - strncpy(tng_data->first_program_name, block->block_contents, len); - offset += len; - - len = tng_min_i((int)strlen(block->block_contents + offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->last_program_name, len); - if(!temp) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->last_program_name); - tng_data->last_program_name = 0; - return(TNG_CRITICAL); + md5_append(&md5_state, (md5_byte_t *)&tng_data->var_num_atoms_flag, + sizeof(tng_data->var_num_atoms_flag)); } - tng_data->last_program_name = temp; - strncpy(tng_data->last_program_name, block->block_contents + offset, len); - offset += len; - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->first_user_name, len); - if(!temp) + if(fread(&tng_data->frame_set_n_frames, sizeof(tng_data->frame_set_n_frames), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->first_user_name); - tng_data->first_user_name = 0; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - tng_data->first_user_name = temp; - strncpy(tng_data->first_user_name, block->block_contents+offset, len); - offset += len; - - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->last_user_name, len); - if(!temp) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->last_user_name); - tng_data->last_user_name = 0; - return(TNG_CRITICAL); + md5_append(&md5_state, (md5_byte_t *)&tng_data->frame_set_n_frames, sizeof(tng_data->frame_set_n_frames)); } - tng_data->last_user_name = temp; - strncpy(tng_data->last_user_name, block->block_contents+offset, len); - offset += len; + tng_var_64_bit_input_swap(tng_data, &tng_data->frame_set_n_frames); - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->first_computer_name, len); - if(!temp) + if(fread(&tng_data->first_trajectory_frame_set_input_file_pos, + sizeof(tng_data->first_trajectory_frame_set_input_file_pos), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->first_computer_name); - tng_data->first_computer_name = 0; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - tng_data->first_computer_name = temp; - strncpy(tng_data->first_computer_name, block->block_contents+offset, len); - offset += len; - - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->last_computer_name, len); - if(!temp) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->last_computer_name); - tng_data->last_computer_name = 0; - return(TNG_CRITICAL); + md5_append(&md5_state, (md5_byte_t *)&tng_data->first_trajectory_frame_set_input_file_pos, + sizeof(tng_data->first_trajectory_frame_set_input_file_pos)); } - tng_data->last_computer_name = temp; - strncpy(tng_data->last_computer_name, block->block_contents+offset, len); - offset += len; + tng_var_64_bit_input_swap(tng_data, &tng_data->first_trajectory_frame_set_input_file_pos); - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->first_pgp_signature, len); - if(!temp) + tng_data->current_trajectory_frame_set.next_frame_set_file_pos = + tng_data->first_trajectory_frame_set_input_file_pos; + + if(fread(&tng_data->last_trajectory_frame_set_input_file_pos, + sizeof(tng_data->last_trajectory_frame_set_input_file_pos), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->first_pgp_signature); - tng_data->first_pgp_signature = 0; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - tng_data->first_pgp_signature = temp; - strncpy(tng_data->first_pgp_signature, block->block_contents+offset, len); - offset += len; - - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->last_pgp_signature, len); - if(!temp) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->last_pgp_signature); - tng_data->last_pgp_signature = 0; - return(TNG_CRITICAL); + md5_append(&md5_state, (md5_byte_t *)&tng_data->last_trajectory_frame_set_input_file_pos, + sizeof(tng_data->last_trajectory_frame_set_input_file_pos)); } - tng_data->last_pgp_signature = temp; - strncpy(tng_data->last_pgp_signature, block->block_contents+offset, len); - offset += len; + tng_var_64_bit_input_swap(tng_data, &tng_data->last_trajectory_frame_set_input_file_pos); - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - temp = realloc(tng_data->forcefield_name, len); - if(!temp) + if(fread(&tng_data->medium_stride_length, + sizeof(tng_data->medium_stride_length), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, - __FILE__, __LINE__); - free(tng_data->forcefield_name); - tng_data->forcefield_name = 0; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - tng_data->forcefield_name = temp; - strncpy(tng_data->forcefield_name, block->block_contents+offset, len); - offset += len; - - memcpy(&tng_data->time, block->block_contents+offset, - sizeof(tng_data->time)); - if(tng_data->input_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->time) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_append(&md5_state, (md5_byte_t *)&tng_data->medium_stride_length, + sizeof(tng_data->medium_stride_length)); } - offset += sizeof(tng_data->time); - - memcpy(&tng_data->var_num_atoms_flag, block->block_contents+offset, - sizeof(tng_data->var_num_atoms_flag)); - offset += sizeof(tng_data->var_num_atoms_flag); + tng_var_64_bit_input_swap(tng_data, &tng_data->medium_stride_length); - memcpy(&tng_data->frame_set_n_frames, block->block_contents+offset, - sizeof(tng_data->frame_set_n_frames)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&tng_data->long_stride_length, + sizeof(tng_data->long_stride_length), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->frame_set_n_frames) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(tng_data->frame_set_n_frames); - - memcpy(&tng_data->first_trajectory_frame_set_input_file_pos, - block->block_contents+offset, - sizeof(tng_data->first_trajectory_frame_set_input_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->first_trajectory_frame_set_input_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_append(&md5_state, (md5_byte_t *)&tng_data->long_stride_length, + sizeof(tng_data->long_stride_length)); } - offset += sizeof(tng_data->first_trajectory_frame_set_input_file_pos); + tng_var_64_bit_input_swap(tng_data, &tng_data->long_stride_length); - tng_data->current_trajectory_frame_set.next_frame_set_file_pos = - tng_data->first_trajectory_frame_set_input_file_pos; - - - memcpy(&tng_data->last_trajectory_frame_set_input_file_pos, - block->block_contents+offset, - sizeof(tng_data->last_trajectory_frame_set_input_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(block->block_version >= 3) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->last_trajectory_frame_set_input_file_pos) - != TNG_SUCCESS) + if(fread(&tng_data->distance_unit_exponential, + sizeof(tng_data->distance_unit_exponential), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - } - offset += sizeof(tng_data->last_trajectory_frame_set_input_file_pos); - - memcpy(&tng_data->medium_stride_length, block->block_contents+offset, - sizeof(tng_data->medium_stride_length)); - if(tng_data->input_endianness_swap_func_64) - { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->medium_stride_length) - != TNG_SUCCESS) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + md5_append(&md5_state, (md5_byte_t *)&tng_data->distance_unit_exponential, + sizeof(tng_data->distance_unit_exponential)); } + tng_var_64_bit_input_swap(tng_data, &tng_data->distance_unit_exponential); } - offset += sizeof(tng_data->medium_stride_length); - memcpy(&tng_data->long_stride_length, block->block_contents+offset, - sizeof(tng_data->long_stride_length)); - if(tng_data->input_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->long_stride_length) - != TNG_SUCCESS) + /* If there is data left in the block that the current version of the library + * cannot interpret still read that to generate the MD5 hash. */ + curr_file_pos = ftello(tng_data->input_file); + if(curr_file_pos < start_pos + block->block_contents_size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + temp_data = malloc(start_pos + block->block_contents_size - curr_file_pos); + if(!temp_data) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + start_pos + block->block_contents_size - curr_file_pos, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(fread(temp_data, start_pos + block->block_contents_size - curr_file_pos, + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read remaining part of block to generate MD5 sum. %s: %d\n", __FILE__, __LINE__); + free(temp_data); + return(TNG_CRITICAL); + } + md5_append(&md5_state, (md5_byte_t *)temp_data, + start_pos + block->block_contents_size - curr_file_pos); + free(temp_data); } - } - offset += sizeof(tng_data->long_stride_length); - - if(block->block_version >= 3) - { - memcpy(&tng_data->distance_unit_exponential, block->block_contents+offset, - sizeof(tng_data->distance_unit_exponential)); - if(tng_data->input_endianness_swap_func_64) + md5_finish(&md5_state, (md5_byte_t *)hash); + if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", TNG_MD5_HASH_LEN) != 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->distance_unit_exponential) - != TNG_SUCCESS) + if(strncmp(block->md5_hash, hash, TNG_MD5_HASH_LEN) != 0) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + fprintf(stderr, "TNG library: General info block contents corrupt. Hashes do not match. " + "%s: %d\n", __FILE__, __LINE__); } } } + fseeko(tng_data->input_file, start_pos + block->block_contents_size, SEEK_SET); + return(TNG_SUCCESS); } @@ -2449,24 +2310,25 @@ static tng_function_status tng_general_info_block_write (tng_trajectory_t tng_data, const char hash_mode) { + int64_t temp_i64, header_file_pos, curr_file_pos; int first_program_name_len, first_user_name_len; int first_computer_name_len, first_pgp_signature_len; int last_program_name_len, last_user_name_len; int last_computer_name_len, last_pgp_signature_len; int forcefield_name_len, name_len; - int offset = 0; tng_gen_block_t block; + md5_state_t md5_state; if(tng_output_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); } - fseek(tng_data->output_file, 0, SEEK_SET); + fseeko(tng_data->output_file, 0, SEEK_SET); tng_block_init(&block); - name_len = (int)strlen("GENERAL INFO"); + name_len = (unsigned int)strlen("GENERAL INFO"); block->name = malloc(name_len + 1); if(!block->name) @@ -2489,188 +2351,210 @@ static tng_function_status tng_general_info_block_write return(TNG_CRITICAL); } - first_program_name_len = tng_min_i((int)strlen(tng_data->first_program_name) + 1, + header_file_pos = 0; + + if(tng_block_header_write(tng_data, block) != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", + tng_data->output_file_path, __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } + + first_program_name_len = tng_min_i((unsigned int)strlen(tng_data->first_program_name) + 1, TNG_MAX_STR_LEN); - last_program_name_len = tng_min_i((int)strlen(tng_data->last_program_name) + 1, + last_program_name_len = tng_min_i((unsigned int)strlen(tng_data->last_program_name) + 1, TNG_MAX_STR_LEN); - first_user_name_len = tng_min_i((int)strlen(tng_data->first_user_name) + 1, + first_user_name_len = tng_min_i((unsigned int)strlen(tng_data->first_user_name) + 1, TNG_MAX_STR_LEN); - last_user_name_len = tng_min_i((int)strlen(tng_data->last_user_name) + 1, + last_user_name_len = tng_min_i((unsigned int)strlen(tng_data->last_user_name) + 1, TNG_MAX_STR_LEN); - first_computer_name_len = tng_min_i((int)strlen(tng_data->first_computer_name) + 1, + first_computer_name_len = tng_min_i((unsigned int)strlen(tng_data->first_computer_name) + 1, TNG_MAX_STR_LEN); - last_computer_name_len = tng_min_i((int)strlen(tng_data->last_computer_name) + 1, + last_computer_name_len = tng_min_i((unsigned int)strlen(tng_data->last_computer_name) + 1, TNG_MAX_STR_LEN); - first_pgp_signature_len = tng_min_i((int)strlen(tng_data->first_pgp_signature) + 1, + first_pgp_signature_len = tng_min_i((unsigned int)strlen(tng_data->first_pgp_signature) + 1, TNG_MAX_STR_LEN); - last_pgp_signature_len = tng_min_i((int)strlen(tng_data->last_pgp_signature) + 1, + last_pgp_signature_len = tng_min_i((unsigned int)strlen(tng_data->last_pgp_signature) + 1, TNG_MAX_STR_LEN); - forcefield_name_len = tng_min_i((int)strlen(tng_data->forcefield_name) + 1, + forcefield_name_len = tng_min_i((unsigned int)strlen(tng_data->forcefield_name) + 1, TNG_MAX_STR_LEN); - if(block->block_contents) + if(fwrite(tng_data->first_program_name, first_program_name_len, 1, tng_data->output_file) != 1) { - free(block->block_contents); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) + + if(fwrite(tng_data->last_program_name, last_program_name_len, 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - tng_block_destroy(&block); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - strncpy(block->block_contents, tng_data->first_program_name, first_program_name_len); - offset += first_program_name_len; - - strncpy(block->block_contents+offset, tng_data->last_program_name, last_program_name_len); - offset += last_program_name_len; - - strncpy(block->block_contents+offset, tng_data->first_user_name, first_user_name_len); - offset += first_user_name_len; - - strncpy(block->block_contents+offset, tng_data->last_user_name, last_user_name_len); - offset += last_user_name_len; + if(fwrite(tng_data->first_user_name, first_user_name_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } - strncpy(block->block_contents+offset, tng_data->first_computer_name, - first_computer_name_len); - offset += first_computer_name_len; + if(fwrite(tng_data->last_user_name, last_user_name_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } - strncpy(block->block_contents+offset, tng_data->last_computer_name, - last_computer_name_len); - offset += last_computer_name_len; + if(fwrite(tng_data->first_computer_name, first_computer_name_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } - strncpy(block->block_contents+offset, tng_data->first_pgp_signature, - first_pgp_signature_len); - offset += first_pgp_signature_len; + if(fwrite(tng_data->last_computer_name, last_computer_name_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } - strncpy(block->block_contents+offset, tng_data->last_pgp_signature, - last_pgp_signature_len); - offset += last_pgp_signature_len; + if(fwrite(tng_data->first_pgp_signature, first_pgp_signature_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } - strncpy(block->block_contents+offset, tng_data->forcefield_name, - forcefield_name_len); - offset += forcefield_name_len; + if(fwrite(tng_data->last_pgp_signature, last_pgp_signature_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } - memcpy(block->block_contents+offset, &tng_data->time, - sizeof(tng_data->time)); - if(tng_data->output_endianness_swap_func_64) + if(fwrite(tng_data->forcefield_name, forcefield_name_len, 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(tng_data->time); - memcpy(block->block_contents+offset, &tng_data->var_num_atoms_flag, - sizeof(tng_data->var_num_atoms_flag)); - offset += sizeof(tng_data->var_num_atoms_flag); + /* Add all the above strings to the MD5 hash at the same time. */ + if(hash_mode == TNG_USE_HASH) + { + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_program_name, first_program_name_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_program_name, last_program_name_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_user_name, first_user_name_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_user_name, last_user_name_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_computer_name, first_computer_name_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_computer_name, last_computer_name_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->first_pgp_signature, first_pgp_signature_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->last_pgp_signature, last_pgp_signature_len); + md5_append(&md5_state, (md5_byte_t *)tng_data->forcefield_name, forcefield_name_len); + } - memcpy(block->block_contents+offset, &tng_data->frame_set_n_frames, - sizeof(tng_data->frame_set_n_frames)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->time; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(tng_data->frame_set_n_frames); - memcpy(block->block_contents+offset, - &tng_data->first_trajectory_frame_set_output_file_pos, - sizeof(tng_data->first_trajectory_frame_set_output_file_pos)); - if(tng_data->output_endianness_swap_func_64) + if(fwrite(&tng_data->var_num_atoms_flag, sizeof(tng_data->var_num_atoms_flag), + 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&tng_data->var_num_atoms_flag, + sizeof(tng_data->var_num_atoms_flag)); } - offset += sizeof(tng_data->first_trajectory_frame_set_output_file_pos); - memcpy(block->block_contents+offset, - &tng_data->last_trajectory_frame_set_output_file_pos, - sizeof(tng_data->last_trajectory_frame_set_output_file_pos)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->frame_set_n_frames; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(tng_data->last_trajectory_frame_set_output_file_pos); - memcpy(block->block_contents+offset, &tng_data->medium_stride_length, - sizeof(tng_data->medium_stride_length)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->first_trajectory_frame_set_output_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(tng_data->medium_stride_length); - memcpy(block->block_contents+offset, &tng_data->long_stride_length, - sizeof(tng_data->long_stride_length)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->last_trajectory_frame_set_output_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(tng_data->long_stride_length); - memcpy(block->block_contents+offset, &tng_data->distance_unit_exponential, - sizeof(tng_data->distance_unit_exponential)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->medium_stride_length; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - if(tng_block_header_write(tng_data, block, hash_mode) != TNG_SUCCESS) + temp_i64 = tng_data->long_stride_length; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", - tng_data->output_file_path, __FILE__, __LINE__); - tng_block_destroy(&block); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); + } - if(fwrite(block->block_contents, block->block_contents_size, 1, - tng_data->output_file) != 1) + temp_i64 = tng_data->distance_unit_exponential; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", __FILE__, __LINE__); - tng_block_destroy(&block); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + + 3 * sizeof(int64_t), SEEK_SET); + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write MD5 hash. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); + } tng_block_destroy(&block); @@ -2681,52 +2565,42 @@ static tng_function_status tng_general_info_block_write * @param tng_data is a trajectory data container. * @param block is a general block container. * @param chain is the chain data container. - * @param offset is the offset of the block input and is updated when reading. * @return TNG_SUCCESS(0) is successful. */ static tng_function_status tng_chain_data_read(tng_trajectory_t tng_data, - tng_gen_block_t block, tng_chain_t chain, - int *offset) + const char hash_mode, + md5_state_t *md5_state) { - int len; - - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); - - memcpy(&chain->id, block->block_contents+*offset, - sizeof(chain->id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&chain->id, sizeof(chain->id), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &chain->id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&chain->id, sizeof(chain->id)); } - *offset += sizeof(chain->id); + tng_var_64_bit_input_swap(tng_data, &chain->id); - len = tng_min_i((int)strlen(block->block_contents+*offset) + 1, - TNG_MAX_STR_LEN); - chain->name = malloc(len); - strncpy(chain->name, - block->block_contents+*offset, len); - *offset += len; + tng_freadstr(tng_data->input_file, &chain->name); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)chain->name, strlen(chain->name) + 1); + } - memcpy(&chain->n_residues, block->block_contents+*offset, - sizeof(chain->n_residues)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&chain->n_residues, sizeof(chain->n_residues), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &chain->n_residues) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&chain->n_residues, sizeof(chain->n_residues)); } - *offset += sizeof(chain->n_residues); + tng_var_64_bit_input_swap(tng_data, &chain->n_residues); return(TNG_SUCCESS); } @@ -2739,44 +2613,47 @@ static tng_function_status tng_chain_data_read(tng_trajectory_t tng_data, * @return TNG_SUCCESS(0) is successful. */ static tng_function_status tng_chain_data_write(tng_trajectory_t tng_data, - tng_gen_block_t block, tng_chain_t chain, - int *offset) + const char hash_mode, + md5_state_t *md5_state) { + int64_t temp_i64; int len; - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); - - memcpy(block->block_contents+*offset, &chain->id, sizeof(chain->id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = chain->id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+*offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - *offset += sizeof(chain->id); - len = tng_min_i((int)strlen(chain->name) + 1, TNG_MAX_STR_LEN); - strncpy(block->block_contents + *offset, chain->name, len); - *offset += len; + len = tng_min_i((unsigned int)strlen(chain->name) + 1, TNG_MAX_STR_LEN); + if(fwrite(chain->name, len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)chain->name, len); + } - memcpy(block->block_contents+*offset, &chain->n_residues, - sizeof(chain->n_residues)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = chain->n_residues; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+*offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - *offset += sizeof(chain->n_residues); return(TNG_SUCCESS); } @@ -2789,48 +2666,39 @@ static tng_function_status tng_chain_data_write(tng_trajectory_t tng_data, * @return TNG_SUCCESS(0) is successful. */ static tng_function_status tng_residue_data_read(tng_trajectory_t tng_data, - tng_gen_block_t block, tng_residue_t residue, - int *offset) + const char hash_mode, + md5_state_t *md5_state) { - int len; - - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); - - memcpy(&residue->id, block->block_contents+*offset, - sizeof(residue->id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&residue->id, sizeof(residue->id), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &residue->id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += sizeof(residue->id); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&residue->id, sizeof(residue->id)); + } + tng_var_64_bit_input_swap(tng_data, &residue->id); - len = tng_min_i((int)strlen(block->block_contents+*offset) + 1, - TNG_MAX_STR_LEN); - residue->name = malloc(len); - strncpy(residue->name, - block->block_contents+*offset, len); - *offset += len; + tng_freadstr(tng_data->input_file, &residue->name); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)residue->name, strlen(residue->name) + 1); + } - memcpy(&residue->n_atoms, block->block_contents+*offset, - sizeof(residue->n_atoms)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&residue->n_atoms, sizeof(residue->n_atoms), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &residue->n_atoms) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block header. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += sizeof(residue->n_atoms); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&residue->n_atoms, sizeof(residue->n_atoms)); + } + tng_var_64_bit_input_swap(tng_data, &residue->n_atoms); return(TNG_SUCCESS); } @@ -2843,44 +2711,47 @@ static tng_function_status tng_residue_data_read(tng_trajectory_t tng_data, * @return TNG_SUCCESS(0) is successful. */ static tng_function_status tng_residue_data_write(tng_trajectory_t tng_data, - tng_gen_block_t block, tng_residue_t residue, - int *offset) + const char hash_mode, + md5_state_t *md5_state) { + int64_t temp_i64; int len; - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); - - memcpy(block->block_contents+*offset, &residue->id, sizeof(residue->id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = residue->id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+*offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - *offset += sizeof(residue->id); - len = tng_min_i((int)strlen(residue->name) + 1, TNG_MAX_STR_LEN); - strncpy(block->block_contents + *offset, residue->name, len); - *offset += len; + len = tng_min_i((unsigned int)strlen(residue->name) + 1, TNG_MAX_STR_LEN); + if(fwrite(residue->name, len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)residue->name, len); + } - memcpy(block->block_contents+*offset, &residue->n_atoms, - sizeof(residue->n_atoms)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = residue->n_atoms; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+*offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - *offset += sizeof(residue->n_atoms); return(TNG_SUCCESS); } @@ -2893,41 +2764,33 @@ static tng_function_status tng_residue_data_write(tng_trajectory_t tng_data, * @return TNG_SUCCESS(0) is successful. */ static tng_function_status tng_atom_data_read(tng_trajectory_t tng_data, - tng_gen_block_t block, tng_atom_t atom, - int *offset) + const char hash_mode, + md5_state_t *md5_state) { - int len; - - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); - - memcpy(&atom->id, block->block_contents+*offset, - sizeof(atom->id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&atom->id, sizeof(atom->id), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &atom->id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += sizeof(atom->id); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&atom->id, sizeof(atom->id)); + } + tng_var_64_bit_input_swap(tng_data, &atom->id); - len = tng_min_i((int)strlen(block->block_contents+*offset) + 1, - TNG_MAX_STR_LEN); - atom->name = malloc(len); - strncpy(atom->name, - block->block_contents+*offset, len); - *offset += len; + tng_freadstr(tng_data->input_file, &atom->name); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)atom->name, strlen(atom->name) + 1); + } - len = tng_min_i((int)strlen(block->block_contents+*offset) + 1, - TNG_MAX_STR_LEN); - atom->atom_type = malloc(len); - strncpy(atom->atom_type, - block->block_contents+*offset, len); - *offset += len; + tng_freadstr(tng_data->input_file, &atom->atom_type); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)atom->atom_type, strlen(atom->atom_type) + 1); + } return(TNG_SUCCESS); } @@ -2940,35 +2803,46 @@ static tng_function_status tng_atom_data_read(tng_trajectory_t tng_data, * @return TNG_SUCCESS(0) is successful. */ static tng_function_status tng_atom_data_write(tng_trajectory_t tng_data, - tng_gen_block_t block, tng_atom_t atom, - int *offset) + const char hash_mode, + md5_state_t *md5_state) { + int64_t temp_i64; int len; - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); - - memcpy(block->block_contents+*offset, &atom->id, - sizeof(atom->id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = atom->id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+*offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - *offset += sizeof(atom->id); - len = tng_min_i((int)strlen(atom->name) + 1, TNG_MAX_STR_LEN); - strncpy(block->block_contents + *offset, atom->name, len); - *offset += len; + len = tng_min_i((unsigned int)strlen(atom->name) + 1, TNG_MAX_STR_LEN); + if(fwrite(atom->name, len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)atom->name, len); + } - len = tng_min_i((int)strlen(atom->atom_type) + 1, TNG_MAX_STR_LEN); - strncpy(block->block_contents + *offset, atom->atom_type, len); - *offset += len; + len = tng_min_i((unsigned int)strlen(atom->atom_type) + 1, TNG_MAX_STR_LEN); + if(fwrite(atom->atom_type, len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)atom->atom_type, len); + } return(TNG_SUCCESS); } @@ -3000,7 +2874,7 @@ static tng_function_status tng_molecules_block_len_calculate } molecule->name[0] = 0; } - *len += tng_min_i((int)strlen(molecule->name) + 1, TNG_MAX_STR_LEN); + *len += tng_min_i((unsigned int)strlen(molecule->name) + 1, TNG_MAX_STR_LEN); chain = molecule->chains; for(j = 0; j < molecule->n_chains; j++) @@ -3018,7 +2892,7 @@ static tng_function_status tng_molecules_block_len_calculate } chain->name[0] = 0; } - *len += tng_min_i((int)strlen(chain->name) + 1, TNG_MAX_STR_LEN); + *len += tng_min_i((unsigned int)strlen(chain->name) + 1, TNG_MAX_STR_LEN); *len += sizeof(chain->n_residues); @@ -3041,7 +2915,7 @@ static tng_function_status tng_molecules_block_len_calculate } residue->name[0] = 0; } - *len += tng_min_i((int)strlen(residue->name) + 1, TNG_MAX_STR_LEN); + *len += tng_min_i((unsigned int)strlen(residue->name) + 1, TNG_MAX_STR_LEN); *len += sizeof(residue->n_atoms); @@ -3063,7 +2937,7 @@ static tng_function_status tng_molecules_block_len_calculate } atom->name[0] = 0; } - *len += tng_min_i((int)strlen(atom->name) + 1, TNG_MAX_STR_LEN); + *len += tng_min_i((unsigned int)strlen(atom->name) + 1, TNG_MAX_STR_LEN); if(!atom->atom_type) { @@ -3076,7 +2950,7 @@ static tng_function_status tng_molecules_block_len_calculate } atom->atom_type[0] = 0; } - *len += tng_min_i((int)strlen(atom->atom_type) + 1, TNG_MAX_STR_LEN); + *len += tng_min_i((unsigned int)strlen(atom->atom_type) + 1, TNG_MAX_STR_LEN); atom++; } @@ -3117,55 +2991,25 @@ static tng_function_status tng_molecules_block_read tng_gen_block_t block, const char hash_mode) { - int64_t i, j, k, l; - int len, offset = 0; + int64_t start_pos, curr_file_pos, i, j, k, l; tng_molecule_t molecule; tng_chain_t chain; tng_residue_t residue; tng_atom_t atom; tng_bond_t bond; - tng_bool same_hash; + char hash[TNG_MD5_HASH_LEN], *temp_data; + md5_state_t md5_state; if(tng_input_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); } - if(block->block_contents) - { - free(block->block_contents); - } - - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - - /* Read the whole block into block_contents to be able to write it to disk - * even if it cannot be interpreted. */ - if(fread(block->block_contents, block->block_contents_size, 1, - tng_data->input_file) == 0) - { - fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); - } + start_pos = ftello(tng_data->input_file); /* FIXME: Does not check if the size of the contents matches the expected * size or if the contents can be read. */ - if(hash_mode == TNG_USE_HASH) - { - tng_md5_hash_match_verify(block, &same_hash); - if(same_hash != TNG_TRUE) - { - fprintf(stderr, "TNG library: Molecules block contents corrupt. Hashes do not match. " - "%s: %d\n", - __FILE__, __LINE__); - } - } - if(tng_data->molecules) { for(i=0; i<tng_data->n_molecules; i++) @@ -3177,19 +3021,18 @@ static tng_function_status tng_molecules_block_read tng_data->n_molecules = 0; } - memcpy(&tng_data->n_molecules, block->block_contents, - sizeof(tng_data->n_molecules)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&tng_data->n_molecules, sizeof(tng_data->n_molecules), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->n_molecules) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&tng_data->n_molecules, sizeof(tng_data->n_molecules)); } - offset += sizeof(tng_data->n_molecules); + tng_var_64_bit_input_swap(tng_data, &tng_data->n_molecules); if(tng_data->molecules) { @@ -3230,100 +3073,89 @@ static tng_function_status tng_molecules_block_read { molecule = &tng_data->molecules[i]; - memcpy(&molecule->id, block->block_contents+offset, - sizeof(molecule->id)); - if(tng_data->input_endianness_swap_func_64) + molecule->name = 0; + + if(fread(&molecule->id, sizeof(molecule->id), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &molecule->id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(molecule->id); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&molecule->id, sizeof(molecule->id)); + } + tng_var_64_bit_input_swap(tng_data, &molecule->id); /* fprintf(stderr, "TNG library: Read id: %"PRId64" offset: %d\n", molecule->id, offset);*/ - len = tng_min_i((int)strlen(block->block_contents+offset) + 1, TNG_MAX_STR_LEN); - molecule->name = malloc(len); - strncpy(molecule->name, block->block_contents+offset, len); - offset += len; + tng_freadstr(tng_data->input_file, &molecule->name); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)molecule->name, strlen(molecule->name) + 1); + } - memcpy(&molecule->quaternary_str, block->block_contents+offset, - sizeof(molecule->quaternary_str)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&molecule->quaternary_str, sizeof(molecule->quaternary_str), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &molecule->quaternary_str) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(molecule->quaternary_str); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&molecule->quaternary_str, sizeof(molecule->quaternary_str)); + } + tng_var_64_bit_input_swap(tng_data, &molecule->quaternary_str); if(!tng_data->var_num_atoms_flag) { - memcpy(&tng_data->molecule_cnt_list[i], - block->block_contents+offset, - sizeof(int64_t)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&tng_data->molecule_cnt_list[i], sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &tng_data->molecule_cnt_list[i]) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&tng_data->molecule_cnt_list[i], sizeof(int64_t)); } - offset += sizeof(int64_t); + tng_var_64_bit_input_swap(tng_data, &tng_data->molecule_cnt_list[i]); } - - memcpy(&molecule->n_chains, block->block_contents+offset, - sizeof(molecule->n_chains)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&molecule->n_chains, sizeof(molecule->n_chains), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &molecule->n_chains) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(molecule->n_chains); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&molecule->n_chains, sizeof(molecule->n_chains)); + } + tng_var_64_bit_input_swap(tng_data, &molecule->n_chains); - memcpy(&molecule->n_residues, block->block_contents+offset, - sizeof(molecule->n_residues)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&molecule->n_residues, sizeof(molecule->n_residues), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &molecule->n_residues) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&molecule->n_residues, sizeof(molecule->n_residues)); } - offset += sizeof(molecule->n_residues); + tng_var_64_bit_input_swap(tng_data, &molecule->n_residues); - memcpy(&molecule->n_atoms, block->block_contents+offset, - sizeof(molecule->n_atoms)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&molecule->n_atoms, sizeof(molecule->n_atoms), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &molecule->n_atoms) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(molecule->n_atoms); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&molecule->n_atoms, sizeof(molecule->n_atoms)); + } + tng_var_64_bit_input_swap(tng_data, &molecule->n_atoms); tng_data->n_particles += molecule->n_atoms * tng_data->molecule_cnt_list[i]; @@ -3400,17 +3232,28 @@ static tng_function_status tng_molecules_block_read { chain->molecule = molecule; - tng_chain_data_read(tng_data, block, chain, &offset); + chain->name = 0; - chain->residues = molecule->residues; - residue = chain->residues; + tng_chain_data_read(tng_data, chain, hash_mode, &md5_state); + + if(j==0) + { + chain->residues = molecule->residues; + residue = chain->residues; + } + else + { + chain->residues = residue; + } /* Read the residues of the chain */ for(k=0; k<chain->n_residues; k++) { residue->chain = chain; - tng_residue_data_read(tng_data, block, residue, &offset); + residue->name = 0; + + tng_residue_data_read(tng_data, residue, hash_mode, &md5_state); residue->atoms_offset = atom - molecule->atoms; /* Read the atoms of the residue */ @@ -3418,7 +3261,10 @@ static tng_function_status tng_molecules_block_read { atom->residue = residue; - tng_atom_data_read(tng_data, block, atom, &offset); + atom->name = 0; + atom->atom_type = 0; + + tng_atom_data_read(tng_data,atom, hash_mode, &md5_state); atom++; } @@ -3435,7 +3281,9 @@ static tng_function_status tng_molecules_block_read { residue->chain = 0; - tng_residue_data_read(tng_data, block, residue, &offset); + residue->name = 0; + + tng_residue_data_read(tng_data, residue, hash_mode, &md5_state); residue->atoms_offset = atom - molecule->atoms; /* Read the atoms of the residue */ @@ -3443,7 +3291,7 @@ static tng_function_status tng_molecules_block_read { atom->residue = residue; - tng_atom_data_read(tng_data, block, atom, &offset); + tng_atom_data_read(tng_data, atom, hash_mode, &md5_state); atom++; } @@ -3456,26 +3304,27 @@ static tng_function_status tng_molecules_block_read { atom->residue = 0; - tng_atom_data_read(tng_data, block, atom, &offset); + atom->name = 0; + atom->atom_type = 0; + + tng_atom_data_read(tng_data, atom, hash_mode, &md5_state); atom++; } } } - memcpy(&molecule->n_bonds, block->block_contents+offset, - sizeof(molecule->n_bonds)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&molecule->n_bonds, sizeof(molecule->n_bonds), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &molecule->n_bonds) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(molecule->n_bonds); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&molecule->n_bonds, sizeof(molecule->n_bonds)); + } + tng_var_64_bit_input_swap(tng_data, &molecule->n_bonds); if(molecule->n_bonds > 0) { @@ -3508,33 +3357,29 @@ static tng_function_status tng_molecules_block_read for(j=0; j<molecule->n_bonds; j++) { - memcpy(&bond->from_atom_id, block->block_contents+offset, - sizeof(bond->from_atom_id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&bond->from_atom_id, sizeof(bond->from_atom_id), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &bond->from_atom_id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(bond->from_atom_id); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&bond->from_atom_id, sizeof(bond->from_atom_id)); + } + tng_var_64_bit_input_swap(tng_data, &bond->from_atom_id); - memcpy(&bond->to_atom_id, block->block_contents+offset, - sizeof(bond->to_atom_id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&bond->to_atom_id, sizeof(bond->to_atom_id), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &bond->to_atom_id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(bond->to_atom_id); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&bond->to_atom_id, sizeof(bond->to_atom_id)); + } + tng_var_64_bit_input_swap(tng_data, &bond->to_atom_id); bond++; } @@ -3545,6 +3390,44 @@ static tng_function_status tng_molecules_block_read } } + if(hash_mode == TNG_USE_HASH) + { + /* If there is data left in the block that the current version of the library + * cannot interpret still read that to generate the MD5 hash. */ + curr_file_pos = ftello(tng_data->input_file); + if(curr_file_pos < start_pos + block->block_contents_size) + { + temp_data = malloc(start_pos + block->block_contents_size - curr_file_pos); + if(!temp_data) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + start_pos + block->block_contents_size - curr_file_pos, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(fread(temp_data, start_pos + block->block_contents_size - curr_file_pos, + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read remaining part of block to generate MD5 sum. %s: %d\n", __FILE__, __LINE__); + free(temp_data); + return(TNG_CRITICAL); + } + md5_append(&md5_state, (md5_byte_t *)temp_data, + start_pos + block->block_contents_size - curr_file_pos); + free(temp_data); + } + md5_finish(&md5_state, (md5_byte_t *)hash); + if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", TNG_MD5_HASH_LEN) != 0) + { + if(strncmp(block->md5_hash, hash, TNG_MD5_HASH_LEN) != 0) + { + fprintf(stderr, "TNG library: Molecules block contents corrupt. Hashes do not match. " + "%s: %d\n", __FILE__, __LINE__); + } + } + } + + fseeko(tng_data->input_file, start_pos + block->block_contents_size, SEEK_SET); + return(TNG_SUCCESS); } @@ -3559,14 +3442,15 @@ static tng_function_status tng_molecules_block_write (tng_trajectory_t tng_data, const char hash_mode) { - int len = 0, name_len, offset = 0; - int64_t i, j, k, l; + int len = 0, name_len; + int64_t temp_i64, i, j, k, l, header_file_pos, curr_file_pos; tng_molecule_t molecule; tng_chain_t chain; tng_residue_t residue; tng_atom_t atom; tng_bond_t bond; tng_gen_block_t block; + md5_state_t md5_state; if(tng_output_file_init(tng_data) != TNG_SUCCESS) { @@ -3575,7 +3459,7 @@ static tng_function_status tng_molecules_block_write tng_block_init(&block); - name_len = (int)strlen("MOLECULES"); + name_len = (unsigned int)strlen("MOLECULES"); block->name = malloc(name_len + 1); if(!block->name) @@ -3598,140 +3482,136 @@ static tng_function_status tng_molecules_block_write return(TNG_CRITICAL); } - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) + header_file_pos = ftello(tng_data->output_file); + + if(tng_block_header_write(tng_data, block) != TNG_SUCCESS) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", + tng_data->output_file_path, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); } - memcpy(block->block_contents+offset, &tng_data->n_molecules, - sizeof(tng_data->n_molecules)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->n_molecules; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(tng_data->n_molecules); for(i = 0; i < tng_data->n_molecules; i++) { molecule = &tng_data->molecules[i]; - memcpy(block->block_contents+offset, &molecule->id, - sizeof(molecule->id)); - if(tng_data->output_endianness_swap_func_64) + + temp_i64 = molecule->id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(molecule->id); /* fprintf(stderr, "TNG library: Wrote id: %"PRId64" offset: %d\n", molecule->id, offset); */ - len = tng_min_i((int)strlen(molecule->name) + 1, TNG_MAX_STR_LEN); - strncpy(block->block_contents + offset, molecule->name, len); - offset += len; + len = tng_min_i((unsigned int)strlen(molecule->name) + 1, TNG_MAX_STR_LEN); + if(fwrite(molecule->name, len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)molecule->name, len); + } - memcpy(block->block_contents+offset, &molecule->quaternary_str, - sizeof(molecule->quaternary_str)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = molecule->quaternary_str; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(molecule->quaternary_str); if(!tng_data->var_num_atoms_flag) { - memcpy(block->block_contents+offset, - &tng_data->molecule_cnt_list[i], sizeof(int64_t)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = tng_data->molecule_cnt_list[i]; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(int64_t); } - memcpy(block->block_contents+offset, &molecule->n_chains, - sizeof(molecule->n_chains)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = molecule->n_chains; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(molecule->n_chains); - memcpy(block->block_contents+offset, &molecule->n_residues, - sizeof(molecule->n_residues)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = molecule->n_residues; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(molecule->n_residues); - memcpy(block->block_contents+offset, &molecule->n_atoms, - sizeof(molecule->n_atoms)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = molecule->n_atoms; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(molecule->n_atoms); if(molecule->n_chains > 0) { chain = molecule->chains; for(j = 0; j < molecule->n_chains; j++) { - tng_chain_data_write(tng_data, block, chain, &offset); + tng_chain_data_write(tng_data, chain, hash_mode, &md5_state); residue = chain->residues; for(k = 0; k < chain->n_residues; k++) { - tng_residue_data_write(tng_data, block, residue, &offset); + tng_residue_data_write(tng_data, residue, hash_mode, &md5_state); atom = molecule->atoms + residue->atoms_offset; for(l = 0; l < residue->n_atoms; l++) { - tng_atom_data_write(tng_data, block, atom, &offset); + tng_atom_data_write(tng_data, atom, hash_mode, &md5_state); atom++; } @@ -3747,12 +3627,12 @@ static tng_function_status tng_molecules_block_write residue = molecule->residues; for(k = 0; k < molecule->n_residues; k++) { - tng_residue_data_write(tng_data, block, residue, &offset); + tng_residue_data_write(tng_data, residue, hash_mode, &md5_state); atom = molecule->atoms + residue->atoms_offset; for(l = 0; l < residue->n_atoms; l++) { - tng_atom_data_write(tng_data, block, atom, &offset); + tng_atom_data_write(tng_data, atom, hash_mode, &md5_state); atom++; } @@ -3764,77 +3644,68 @@ static tng_function_status tng_molecules_block_write atom = molecule->atoms; for(l = 0; l < molecule->n_atoms; l++) { - tng_atom_data_write(tng_data, block, atom, &offset); + tng_atom_data_write(tng_data, atom, hash_mode, &md5_state); atom++; } } } - memcpy(block->block_contents+offset, &molecule->n_bonds, - sizeof(molecule->n_bonds)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = molecule->n_bonds; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(molecule->n_bonds); bond = molecule->bonds; for(j = 0; j < molecule->n_bonds; j++) { - memcpy(block->block_contents+offset, &bond->from_atom_id, - sizeof(bond->from_atom_id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = bond->from_atom_id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(bond->from_atom_id); - memcpy(block->block_contents+offset, &bond->to_atom_id, - sizeof(bond->to_atom_id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = bond->to_atom_id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(bond->to_atom_id); bond++; } } - - if(tng_block_header_write(tng_data, block, hash_mode) != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", - tng_data->output_file_path, __FILE__, __LINE__); - tng_block_destroy(&block); - return(TNG_CRITICAL); - } - - if(fwrite(block->block_contents, block->block_contents_size, 1, - tng_data->output_file) != 1) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", - __FILE__, __LINE__); - tng_block_destroy(&block); - return(TNG_CRITICAL); + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + + 3 * sizeof(int64_t), SEEK_SET); + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write MD5 hash. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); } tng_block_destroy(&block); @@ -3870,100 +3741,52 @@ static tng_function_status tng_frame_set_block_read tng_gen_block_t block, const char hash_mode) { - long file_pos; - int offset = 0; - int64_t i, prev_n_particles; - tng_bool same_hash; + int64_t file_pos, start_pos, i, prev_n_particles; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; + char hash[TNG_MD5_HASH_LEN], *temp_data; + md5_state_t md5_state; if(tng_input_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); } - if(block->block_contents) - { - free(block->block_contents); - } - - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - - /* Read the whole block into block_contents to be able to write it to - * disk even if it cannot be interpreted. */ - if(fread(block->block_contents, block->block_contents_size, 1, - tng_data->input_file) == 0) - { - fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); - return(TNG_CRITICAL); - } + start_pos = ftello(tng_data->input_file); /* FIXME: Does not check if the size of the contents matches the expected * size or if the contents can be read. */ - file_pos = (int64_t)ftell(tng_data->input_file) - - (long)(block->block_contents_size + block->header_contents_size); - - if(hash_mode == TNG_USE_HASH) - { - tng_md5_hash_match_verify(block, &same_hash); - if(same_hash != TNG_TRUE) - { - fprintf(stderr, "TNG library: Frame set block contents corrupt. File pos %ld Hashes do not match. " - "%s: %d\n", - file_pos, __FILE__, __LINE__); - /* return(TNG_FAILURE); */ - } - } + file_pos = start_pos - block->header_contents_size; tng_data->current_trajectory_frame_set_input_file_pos = file_pos; tng_frame_set_particle_mapping_free(tng_data); - if(tng_data->first_trajectory_frame_set_input_file_pos <= 0) + if(fread(&frame_set->first_frame, sizeof(frame_set->first_frame), + 1, tng_data->input_file) == 0) { - tng_data->first_trajectory_frame_set_input_file_pos = file_pos; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - /* FIXME: Should check the frame number instead of the file_pos, in case - * frame sets are not in order */ - if(tng_data->last_trajectory_frame_set_input_file_pos < file_pos) + if(hash_mode == TNG_USE_HASH) { - tng_data->last_trajectory_frame_set_input_file_pos = file_pos; + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&frame_set->first_frame, sizeof(frame_set->first_frame)); } + tng_var_64_bit_input_swap(tng_data, &frame_set->first_frame); - memcpy(&frame_set->first_frame, block->block_contents, - sizeof(frame_set->first_frame)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->n_frames, sizeof(frame_set->n_frames), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->first_frame) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->first_frame); - - memcpy(&frame_set->n_frames, block->block_contents + offset, - sizeof(frame_set->n_frames)); - if(tng_data->input_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->n_frames) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_append(&md5_state, (md5_byte_t *)&frame_set->n_frames, sizeof(frame_set->n_frames)); } - offset += sizeof(frame_set->n_frames); + tng_var_64_bit_input_swap(tng_data, &frame_set->n_frames); if(tng_data->var_num_atoms_flag) { @@ -3986,20 +3809,18 @@ static tng_function_status tng_frame_set_block_read } for(i = 0; i < tng_data->n_molecules; i++) { - memcpy(&frame_set->molecule_cnt_list[i], - block->block_contents + offset, - sizeof(int64_t)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->molecule_cnt_list[i], sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->molecule_cnt_list[i]) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(int64_t); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->molecule_cnt_list[i], sizeof(int64_t)); + } + tng_var_64_bit_input_swap(tng_data, &frame_set->molecule_cnt_list[i]); + frame_set->n_particles += tng_data->molecules[i].n_atoms * frame_set->molecule_cnt_list[i]; } @@ -4009,126 +3830,117 @@ static tng_function_status tng_frame_set_block_read } } - memcpy(&frame_set->next_frame_set_file_pos, - block->block_contents + offset, - sizeof(frame_set->next_frame_set_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->next_frame_set_file_pos, sizeof(frame_set->next_frame_set_file_pos), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->next_frame_set_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->next_frame_set_file_pos); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->next_frame_set_file_pos, sizeof(frame_set->next_frame_set_file_pos)); + } + tng_var_64_bit_input_swap(tng_data, &frame_set->next_frame_set_file_pos); - memcpy(&frame_set->prev_frame_set_file_pos, - block->block_contents + offset, - sizeof(frame_set->prev_frame_set_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->prev_frame_set_file_pos, sizeof(frame_set->prev_frame_set_file_pos), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->prev_frame_set_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->prev_frame_set_file_pos, + sizeof(frame_set->prev_frame_set_file_pos)); } - offset += sizeof(frame_set->prev_frame_set_file_pos); + tng_var_64_bit_input_swap(tng_data, &frame_set->prev_frame_set_file_pos); - memcpy(&frame_set->medium_stride_next_frame_set_file_pos, - block->block_contents + offset, - sizeof(frame_set->medium_stride_next_frame_set_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->medium_stride_next_frame_set_file_pos, + sizeof(frame_set->medium_stride_next_frame_set_file_pos), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->medium_stride_next_frame_set_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->medium_stride_next_frame_set_file_pos); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->medium_stride_next_frame_set_file_pos, + sizeof(frame_set->medium_stride_next_frame_set_file_pos)); + } + tng_var_64_bit_input_swap(tng_data, &frame_set->medium_stride_next_frame_set_file_pos); - memcpy(&frame_set->medium_stride_prev_frame_set_file_pos, - block->block_contents + offset, - sizeof(frame_set->medium_stride_prev_frame_set_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->medium_stride_prev_frame_set_file_pos, + sizeof(frame_set->medium_stride_prev_frame_set_file_pos), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->medium_stride_prev_frame_set_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->medium_stride_prev_frame_set_file_pos); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->medium_stride_prev_frame_set_file_pos, + sizeof(frame_set->medium_stride_prev_frame_set_file_pos)); + } + tng_var_64_bit_input_swap(tng_data, &frame_set->medium_stride_prev_frame_set_file_pos); - memcpy(&frame_set->long_stride_next_frame_set_file_pos, - block->block_contents + offset, - sizeof(frame_set->long_stride_next_frame_set_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->long_stride_next_frame_set_file_pos, + sizeof(frame_set->long_stride_next_frame_set_file_pos), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->long_stride_next_frame_set_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->long_stride_next_frame_set_file_pos, + sizeof(frame_set->long_stride_next_frame_set_file_pos)); } - offset += sizeof(frame_set->long_stride_next_frame_set_file_pos); + tng_var_64_bit_input_swap(tng_data, &frame_set->long_stride_next_frame_set_file_pos); - memcpy(&frame_set->long_stride_prev_frame_set_file_pos, - block->block_contents + offset, - sizeof(frame_set->long_stride_prev_frame_set_file_pos)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->long_stride_prev_frame_set_file_pos, + sizeof(frame_set->long_stride_prev_frame_set_file_pos), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &frame_set->long_stride_prev_frame_set_file_pos) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->long_stride_prev_frame_set_file_pos); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->long_stride_prev_frame_set_file_pos, + sizeof(frame_set->long_stride_prev_frame_set_file_pos)); + } + tng_var_64_bit_input_swap(tng_data, &frame_set->long_stride_prev_frame_set_file_pos); + if(block->block_version >= 3) { - memcpy(&frame_set->first_frame_time, - block->block_contents + offset, - sizeof(frame_set->first_frame_time)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&frame_set->first_frame_time, + sizeof(frame_set->first_frame_time), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)&frame_set->first_frame_time) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->first_frame_time); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&frame_set->first_frame_time, + sizeof(frame_set->first_frame_time)); + } + tng_var_64_bit_input_swap(tng_data, (int64_t *)&frame_set->first_frame_time); - memcpy(&tng_data->time_per_frame, - block->block_contents + offset, - sizeof(tng_data->time_per_frame)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&tng_data->time_per_frame, + sizeof(tng_data->time_per_frame), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)&tng_data->time_per_frame) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&tng_data->time_per_frame, + sizeof(tng_data->time_per_frame)); } + tng_var_64_bit_input_swap(tng_data, (int64_t *)&tng_data->time_per_frame); } else { @@ -4136,6 +3948,42 @@ static tng_function_status tng_frame_set_block_read tng_data->time_per_frame = -1; } + if(hash_mode == TNG_USE_HASH) + { + /* If there is data left in the block that the current version of the library + * cannot interpret still read that to generate the MD5 hash. */ + file_pos = ftello(tng_data->input_file); + if(file_pos < start_pos + block->block_contents_size) + { + temp_data = malloc(start_pos + block->block_contents_size - file_pos); + if(!temp_data) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + start_pos + block->block_contents_size - file_pos, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(fread(temp_data, start_pos + block->block_contents_size - file_pos, + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read remaining part of block to generate MD5 sum. %s: %d\n", __FILE__, __LINE__); + free(temp_data); + return(TNG_CRITICAL); + } + md5_append(&md5_state, (md5_byte_t *)temp_data, + start_pos + block->block_contents_size - file_pos); + free(temp_data); + } + md5_finish(&md5_state, (md5_byte_t *)hash); + if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", TNG_MD5_HASH_LEN) != 0) + { + if(strncmp(block->md5_hash, hash, TNG_MD5_HASH_LEN) != 0) + { + fprintf(stderr, "TNG library: Frame set block contents corrupt (first frame %"PRId64"). Hashes do not match. " + "%s: %d\n", frame_set->first_frame, __FILE__, __LINE__); + } + } + } + /* If the output file and the input files are the same the number of * frames in the file are the same number as has just been read. * This is updated here to later on see if there have been new frames @@ -4162,18 +4010,19 @@ static tng_function_status tng_frame_set_block_write const char hash_mode) { char *temp_name; - int64_t i; - int offset = 0; + int64_t temp_i64, i, header_file_pos, curr_file_pos; + double temp_d; unsigned int name_len; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; + md5_state_t md5_state; if(tng_output_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); } - name_len = (int)strlen("TRAJECTORY FRAME SET"); + name_len = (unsigned int)strlen("TRAJECTORY FRAME SET"); if(!block->name || strlen(block->name) < name_len) { @@ -4199,198 +4048,164 @@ static tng_function_status tng_frame_set_block_write return(TNG_CRITICAL); } - if(block->block_contents) + header_file_pos = ftello(tng_data->output_file); + + if(tng_block_header_write(tng_data, block) != TNG_SUCCESS) { - free(block->block_contents); + fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", + tng_data->output_file_path, __FILE__, __LINE__); + return(TNG_CRITICAL); } - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) + + temp_i64 = frame_set->first_frame; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - - memcpy(block->block_contents, &frame_set->first_frame, - sizeof(frame_set->first_frame)); - if(tng_data->output_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->first_frame); - memcpy(block->block_contents+offset, &frame_set->n_frames, - sizeof(frame_set->n_frames)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->n_frames; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->n_frames); if(tng_data->var_num_atoms_flag) { for(i = 0; i < tng_data->n_molecules; i++) { - memcpy(block->block_contents+offset, - &frame_set->molecule_cnt_list[i], - sizeof(int64_t)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->molecule_cnt_list[i]; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(int64_t); } } - - memcpy(block->block_contents+offset, &frame_set->next_frame_set_file_pos, - sizeof(frame_set->next_frame_set_file_pos)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->next_frame_set_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->next_frame_set_file_pos); - - memcpy(block->block_contents+offset, &frame_set->prev_frame_set_file_pos, - sizeof(frame_set->prev_frame_set_file_pos)); - if(tng_data->output_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->prev_frame_set_file_pos); - memcpy(block->block_contents+offset, - &frame_set->medium_stride_next_frame_set_file_pos, - sizeof(frame_set->medium_stride_next_frame_set_file_pos)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->prev_frame_set_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(frame_set->medium_stride_next_frame_set_file_pos); - - memcpy(block->block_contents+offset, - &frame_set->medium_stride_prev_frame_set_file_pos, - sizeof(frame_set->medium_stride_prev_frame_set_file_pos)); - if(tng_data->output_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->medium_stride_prev_frame_set_file_pos); - memcpy(block->block_contents+offset, - &frame_set->long_stride_next_frame_set_file_pos, - sizeof(frame_set->long_stride_next_frame_set_file_pos)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->medium_stride_next_frame_set_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->long_stride_next_frame_set_file_pos); - memcpy(block->block_contents+offset, - &frame_set->long_stride_prev_frame_set_file_pos, - sizeof(frame_set->long_stride_prev_frame_set_file_pos)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->medium_stride_prev_frame_set_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->long_stride_prev_frame_set_file_pos); - memcpy(block->block_contents+offset, - &frame_set->first_frame_time, - sizeof(frame_set->first_frame_time)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->long_stride_next_frame_set_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(frame_set->first_frame_time); - memcpy(block->block_contents+offset, - &tng_data->time_per_frame, - sizeof(tng_data->time_per_frame)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = frame_set->long_stride_prev_frame_set_file_pos; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - if(tng_block_header_write(tng_data, block, hash_mode) != TNG_SUCCESS) + temp_d = frame_set->first_frame_time; + tng_var_64_bit_output_swap(tng_data, (int64_t *)&temp_d); + if(fwrite(&temp_d, sizeof(temp_d), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", - tng_data->output_file_path, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_d, sizeof(temp_d)); + } - if(fwrite(block->block_contents, block->block_contents_size, 1, - tng_data->output_file) != 1) + temp_d = tng_data->time_per_frame; + tng_var_64_bit_output_swap(tng_data, (int64_t *)&temp_d); + if(fwrite(&temp_d, sizeof(temp_d), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_d, sizeof(temp_d)); + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + + 3 * sizeof(int64_t), SEEK_SET); + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write MD5 hash. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); + } return(TNG_SUCCESS); } @@ -4421,56 +4236,23 @@ static tng_function_status tng_trajectory_mapping_block_read tng_gen_block_t block, const char hash_mode) { - int64_t i; - int offset = 0; - tng_bool same_hash; + int64_t start_pos, curr_file_pos, i; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; - tng_particle_mapping_t mapping, mappings; + char hash[TNG_MD5_HASH_LEN], *temp_data; + md5_state_t md5_state; if(tng_input_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); } - if(block->block_contents) - { - free(block->block_contents); - } - - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - - /* Read the whole block into block_contents to be able to write it to disk - * even if it cannot be interpreted. */ - if(fread(block->block_contents, block->block_contents_size, 1, - tng_data->input_file) == 0) - { - fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); - return(TNG_CRITICAL); - } + start_pos = ftello(tng_data->input_file); /* FIXME: Does not check if the size of the contents matches the expected * size or if the contents can be read. */ - if(hash_mode == TNG_USE_HASH) - { - tng_md5_hash_match_verify(block, &same_hash); - if(same_hash != TNG_TRUE) - { - fprintf(stderr, "TNG library: Particle mapping block contents corrupt. Hashes do not match. " - "%s: %d\n", - __FILE__, __LINE__); - /* return(TNG_FAILURE); */ - } - } - frame_set->n_mapping_blocks++; mappings = realloc(frame_set->mappings, sizeof(struct tng_particle_mapping) * @@ -4487,33 +4269,30 @@ static tng_function_status tng_trajectory_mapping_block_read mapping = &mappings[frame_set->n_mapping_blocks - 1]; - memcpy(&mapping->num_first_particle, block->block_contents+offset, - sizeof(mapping->num_first_particle)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&mapping->num_first_particle, sizeof(mapping->num_first_particle), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &mapping->num_first_particle) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&mapping->num_first_particle, sizeof(mapping->num_first_particle)); } - offset += sizeof(mapping->num_first_particle); + tng_var_64_bit_input_swap(tng_data, &mapping->num_first_particle); - memcpy(&mapping->n_particles, block->block_contents+offset, - sizeof(mapping->n_particles)); - if(tng_data->input_endianness_swap_func_64) + if(fread(&mapping->n_particles, sizeof(mapping->n_particles), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - &mapping->n_particles) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&mapping->n_particles, sizeof(mapping->n_particles)); } - offset += sizeof(mapping->n_particles); + tng_var_64_bit_input_swap(tng_data, &mapping->n_particles); mapping->real_particle_numbers = malloc(mapping->n_particles * sizeof(int64_t)); @@ -4530,26 +4309,72 @@ static tng_function_status tng_trajectory_mapping_block_read { for(i = 0; i < mapping->n_particles; i++) { - memcpy(&mapping->real_particle_numbers[i], - block->block_contents + offset, - sizeof(int64_t)); - if(tng_data->input_endianness_swap_func_64(tng_data, - &mapping->real_particle_numbers[i]) - != TNG_SUCCESS) + if(fread(&mapping->real_particle_numbers[i], sizeof(int64_t), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - offset += sizeof(int64_t); + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&mapping->real_particle_numbers[i], + sizeof(mapping->real_particle_numbers[i])); + } + tng_var_64_bit_input_swap(tng_data, &mapping->real_particle_numbers[i]); } } /* Otherwise the data can be read all at once */ else { - memcpy(mapping->real_particle_numbers, block->block_contents + offset, - mapping->n_particles * sizeof(int64_t)); + if(fread(mapping->real_particle_numbers, mapping->n_particles * sizeof(int64_t), + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)mapping->real_particle_numbers, mapping->n_particles * sizeof(int64_t)); + } + } + + if(hash_mode == TNG_USE_HASH) + { + /* If there is data left in the block that the current version of the library + * cannot interpret still read that to generate the MD5 hash. */ + curr_file_pos = ftello(tng_data->input_file); + if(curr_file_pos < start_pos + block->block_contents_size) + { + temp_data = malloc(start_pos + block->block_contents_size - curr_file_pos); + if(!temp_data) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + start_pos + block->block_contents_size - curr_file_pos, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(fread(temp_data, start_pos + block->block_contents_size - curr_file_pos, + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read remaining part of block to generate MD5 sum. %s: %d\n", __FILE__, __LINE__); + free(temp_data); + return(TNG_CRITICAL); + } + md5_append(&md5_state, (md5_byte_t *)temp_data, + start_pos + block->block_contents_size - curr_file_pos); + free(temp_data); + } + md5_finish(&md5_state, (md5_byte_t *)hash); + if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", TNG_MD5_HASH_LEN) != 0) + { + if(strncmp(block->md5_hash, hash, TNG_MD5_HASH_LEN) != 0) + { + fprintf(stderr, "TNG library: Particle mapping block contents corrupt. Hashes do not match. " + "%s: %d\n", __FILE__, __LINE__); + } + } } + fseeko(tng_data->input_file, start_pos + block->block_contents_size, SEEK_SET); return(TNG_SUCCESS); } @@ -4569,9 +4394,11 @@ static tng_function_status tng_trajectory_mapping_block_write int mapping_block_nr, const char hash_mode) { + int64_t temp_i64, header_file_pos, curr_file_pos; char *temp_name; - int i, offset = 0; + int i; unsigned int name_len; + md5_state_t md5_state; tng_particle_mapping_t mapping = &tng_data->current_trajectory_frame_set.mappings[mapping_block_nr]; @@ -4588,7 +4415,7 @@ static tng_function_status tng_trajectory_mapping_block_write return(TNG_CRITICAL); } - name_len = (int)strlen("PARTICLE MAPPING"); + name_len = (unsigned int)strlen("PARTICLE MAPPING"); if(!block->name || strlen(block->name) < name_len) { @@ -4616,81 +4443,86 @@ static tng_function_status tng_trajectory_mapping_block_write return(TNG_CRITICAL); } - if(block->block_contents) + header_file_pos = ftello(tng_data->output_file); + + if(tng_block_header_write(tng_data, block) != TNG_SUCCESS) { - free(block->block_contents); + fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", + tng_data->output_file_path, __FILE__, __LINE__); + return(TNG_CRITICAL); } - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) + + temp_i64 = mapping->num_first_particle; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - - memcpy(block->block_contents, &mapping->num_first_particle, - sizeof(mapping->num_first_particle)); - if(tng_data->output_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(mapping->num_first_particle); - memcpy(block->block_contents+offset, &mapping->n_particles, - sizeof(mapping->n_particles)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = mapping->n_particles; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(mapping->n_particles); if(tng_data->output_endianness_swap_func_64) { for(i = 0; i < mapping->n_particles; i++) { - memcpy(block->block_contents+offset, &mapping->real_particle_numbers[i], - sizeof(int64_t)); - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) + temp_i64 = mapping->real_particle_numbers[i]; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(int64_t); } } else { - memcpy(block->block_contents+offset, mapping->real_particle_numbers, - mapping->n_particles * sizeof(int64_t)); - } - - - if(tng_block_header_write(tng_data, block, hash_mode) != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", - tng_data->output_file_path, __FILE__, __LINE__); - return(TNG_CRITICAL); + if(fwrite(mapping->real_particle_numbers, + mapping->n_particles * sizeof(int64_t), + 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)mapping->real_particle_numbers, + mapping->n_particles * sizeof(int64_t)); + } } - if(fwrite(block->block_contents, block->block_contents_size, 1, - tng_data->output_file) != 1) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", __FILE__, __LINE__); - return(TNG_CRITICAL); + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + + 3 * sizeof(int64_t), SEEK_SET); + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write MD5 hash. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); } return(TNG_SUCCESS); @@ -4757,14 +4589,14 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, const int64_t n_frames, const int64_t n_particles, const char type, - void *start_pos) + char **data, + int64_t *new_len) { int nalgo; - int new_len; + int compressed_len; int *alt_algo = 0; - char *dest, *temp; - int64_t algo_find_n_frames; - unsigned long offset; + char *dest; + int64_t algo_find_n_frames = -1; float f_precision; double d_precision; @@ -4799,28 +4631,57 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, if(n_frames == 1 && tng_data->frame_set_n_frames > 1) { nalgo = tng_compress_nalgo(); - alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_pos); + alt_algo = malloc(nalgo * sizeof *tng_data->compress_algo_pos); + + /* If we have already determined the initial coding and + * initial coding parameter do not determine them again. */ + if(tng_data->compress_algo_pos) + { + alt_algo[0] = tng_data->compress_algo_pos[0]; + alt_algo[1] = tng_data->compress_algo_pos[1]; + } + else + { + alt_algo[0] = -1; + alt_algo[1] = -1; + } + + /* If the initial coding and initial coding parameter are -1 + * they will be determined in tng_compress_pos/_float/. */ if(type == TNG_FLOAT_DATA) { - dest = tng_compress_pos_float_find_algo(start_pos, (int)n_particles, - (int)n_frames, - f_precision, - 0, alt_algo, - &new_len); + dest = tng_compress_pos_float((float *)*data, (int)n_particles, + (int)n_frames, + f_precision, + 0, alt_algo, + &compressed_len); } else { - dest = tng_compress_pos_find_algo(start_pos, (int)n_particles, - (int)n_frames, - d_precision, - 0, alt_algo, - &new_len); + dest = tng_compress_pos((double *)*data, (int)n_particles, + (int)n_frames, + d_precision, + 0, alt_algo, + &compressed_len); + } + /* If there had been no algorithm determined before keep the initial coding + * and initial coding parameter so that they won't have to be determined again. */ + if(!tng_data->compress_algo_pos) + { + nalgo = tng_compress_nalgo(); + tng_data->compress_algo_pos=malloc(nalgo * + sizeof *tng_data->compress_algo_pos); + tng_data->compress_algo_pos[0] = alt_algo[0]; + tng_data->compress_algo_pos[1] = alt_algo[1]; + tng_data->compress_algo_pos[2] = -1; + tng_data->compress_algo_pos[3] = -1; } } - else if(!tng_data->compress_algo_pos) + else if(!tng_data->compress_algo_pos || tng_data->compress_algo_pos[2] == -1 || + tng_data->compress_algo_pos[2] == -1) { - if(n_frames > 10) + if(n_frames > 6) { algo_find_n_frames = 5; } @@ -4829,43 +4690,54 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, algo_find_n_frames = n_frames; } - nalgo = tng_compress_nalgo(); - tng_data->compress_algo_pos=malloc(nalgo * - sizeof *tng_data->compress_algo_pos); + /* If the algorithm parameters are -1 they will be determined during the + * compression. */ + if(!tng_data->compress_algo_pos) + { + nalgo = tng_compress_nalgo(); + tng_data->compress_algo_pos=malloc(nalgo * + sizeof *tng_data->compress_algo_pos); + tng_data->compress_algo_pos[0] = -1; + tng_data->compress_algo_pos[1] = -1; + tng_data->compress_algo_pos[2] = -1; + tng_data->compress_algo_pos[3] = -1; + } if(type == TNG_FLOAT_DATA) { - dest = tng_compress_pos_float_find_algo(start_pos, (int)n_particles, - (int)algo_find_n_frames, - f_precision, - 0, tng_data-> - compress_algo_pos, - &new_len); + dest = tng_compress_pos_float((float *)*data, (int)n_particles, + (int)algo_find_n_frames, + f_precision, + 0, tng_data-> + compress_algo_pos, + &compressed_len); if(algo_find_n_frames < n_frames) { - dest = tng_compress_pos_float(start_pos, (int)n_particles, + free(dest); + dest = tng_compress_pos_float((float *)*data, (int)n_particles, (int)n_frames, f_precision, 0, tng_data->compress_algo_pos, - &new_len); + &compressed_len); } } else { - dest = tng_compress_pos_find_algo(start_pos, (int)n_particles, - (int)algo_find_n_frames, - d_precision, - 0, tng_data-> - compress_algo_pos, - &new_len); + dest = tng_compress_pos((double *)*data, (int)n_particles, + (int)algo_find_n_frames, + d_precision, + 0, tng_data-> + compress_algo_pos, + &compressed_len); if(algo_find_n_frames < n_frames) { - dest = tng_compress_pos(start_pos, (int)n_particles, + free(dest); + dest = tng_compress_pos((double *)*data, (int)n_particles, (int)n_frames, d_precision, 0, tng_data->compress_algo_pos, - &new_len); + &compressed_len); } } } @@ -4873,18 +4745,18 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, { if(type == TNG_FLOAT_DATA) { - dest = tng_compress_pos_float(start_pos, (int)n_particles, + dest = tng_compress_pos_float((float *)*data, (int)n_particles, (int)n_frames, f_precision, 0, - tng_data->compress_algo_pos, &new_len); + tng_data->compress_algo_pos, &compressed_len); } else { - dest = tng_compress_pos(start_pos, (int)n_particles, + dest = tng_compress_pos((double *)*data, (int)n_particles, (int)n_frames, d_precision, 0, tng_data->compress_algo_pos, - &new_len); + &compressed_len); } } } @@ -4896,28 +4768,57 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, if(n_frames == 1 && tng_data->frame_set_n_frames > 1) { nalgo = tng_compress_nalgo(); - alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_pos); + alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_vel); + + /* If we have already determined the initial coding and + * initial coding parameter do not determine them again. */ + if(tng_data->compress_algo_pos) + { + alt_algo[0] = tng_data->compress_algo_vel[0]; + alt_algo[1] = tng_data->compress_algo_vel[1]; + } + else + { + alt_algo[0] = -1; + alt_algo[1] = -1; + } + + /* If the initial coding and initial coding parameter are -1 + * they will be determined in tng_compress_pos/_float/. */ if(type == TNG_FLOAT_DATA) { - dest = tng_compress_vel_float_find_algo(start_pos, (int)n_particles, - (int)n_frames, - f_precision, - 0, alt_algo, - &new_len); + dest = tng_compress_vel_float((float *)*data, (int)n_particles, + (int)n_frames, + f_precision, + 0, alt_algo, + &compressed_len); } else { - dest = tng_compress_vel_find_algo(start_pos, (int)n_particles, - (int)n_frames, - d_precision, - 0, alt_algo, - &new_len); + dest = tng_compress_vel((double *)*data, (int)n_particles, + (int)n_frames, + d_precision, + 0, alt_algo, + &compressed_len); + } + /* If there had been no algorithm determined before keep the initial coding + * and initial coding parameter so that they won't have to be determined again. */ + if(!tng_data->compress_algo_vel) + { + nalgo = tng_compress_nalgo(); + tng_data->compress_algo_vel=malloc(nalgo * + sizeof *tng_data->compress_algo_vel); + tng_data->compress_algo_vel[0] = alt_algo[0]; + tng_data->compress_algo_vel[1] = alt_algo[1]; + tng_data->compress_algo_vel[2] = -1; + tng_data->compress_algo_vel[3] = -1; } } - else if(!tng_data->compress_algo_vel) + else if(!tng_data->compress_algo_vel || tng_data->compress_algo_vel[2] == -1 || + tng_data->compress_algo_vel[2] == -1) { - if(n_frames > 10) + if(n_frames > 6) { algo_find_n_frames = 5; } @@ -4926,42 +4827,52 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, algo_find_n_frames = n_frames; } - nalgo = tng_compress_nalgo(); - tng_data->compress_algo_vel=malloc(nalgo * - sizeof *tng_data->compress_algo_vel); - + /* If the algorithm parameters are -1 they will be determined during the + * compression. */ + if(!tng_data->compress_algo_vel) + { + nalgo = tng_compress_nalgo(); + tng_data->compress_algo_vel=malloc(nalgo * + sizeof *tng_data->compress_algo_vel); + tng_data->compress_algo_vel[0] = -1; + tng_data->compress_algo_vel[1] = -1; + tng_data->compress_algo_vel[2] = -1; + tng_data->compress_algo_vel[3] = -1; + } if(type == TNG_FLOAT_DATA) { - dest = tng_compress_vel_float_find_algo(start_pos, (int)n_particles, - (int)algo_find_n_frames, - f_precision, - 0, tng_data-> - compress_algo_vel, - &new_len); + dest = tng_compress_vel_float((float *)*data, (int)n_particles, + (int)algo_find_n_frames, + f_precision, + 0, tng_data-> + compress_algo_vel, + &compressed_len); if(algo_find_n_frames < n_frames) { - dest = tng_compress_vel_float(start_pos, (int)n_particles, + free(dest); + dest = tng_compress_vel_float((float *)*data, (int)n_particles, (int)n_frames, f_precision, 0, tng_data->compress_algo_vel, - &new_len); + &compressed_len); } } else { - dest = tng_compress_vel_find_algo(start_pos, (int)n_particles, - (int)algo_find_n_frames, - d_precision, - 0, tng_data-> - compress_algo_vel, - &new_len); + dest = tng_compress_vel((double *)*data, (int)n_particles, + (int)algo_find_n_frames, + d_precision, + 0, tng_data-> + compress_algo_vel, + &compressed_len); if(algo_find_n_frames < n_frames) { - dest = tng_compress_vel(start_pos, (int)n_particles, + free(dest); + dest = tng_compress_vel((double *)*data, (int)n_particles, (int)n_frames, d_precision, 0, tng_data->compress_algo_vel, - &new_len); + &compressed_len); } } } @@ -4969,21 +4880,21 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, { if(type == TNG_FLOAT_DATA) { - dest = tng_compress_vel_float(start_pos, (int)n_particles, + dest = tng_compress_vel_float((float *)*data, (int)n_particles, (int)n_frames, f_precision, 0, tng_data-> compress_algo_vel, - &new_len); + &compressed_len); } else { - dest = tng_compress_vel(start_pos, (int)n_particles, + dest = tng_compress_vel((double *)*data, (int)n_particles, (int)n_frames, d_precision, 0, tng_data-> compress_algo_vel, - &new_len); + &compressed_len); } } } @@ -4993,35 +4904,16 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, return(TNG_FAILURE); } - offset = (unsigned long)((char *)start_pos - block->block_contents); - if(alt_algo) { free(alt_algo); } - block->block_contents_size = new_len + offset; + free(*data); - temp = realloc(block->block_contents, block->block_contents_size); - if(!temp) - { - free(block->block_contents); - block->block_contents = 0; - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - block->block_contents = temp; - if(dest) - { - memcpy(temp + offset, dest, new_len); - free(dest); - } - else - { - fprintf(stderr, "TNG library: Error during TNG compression. %s: %d\n", __FILE__, __LINE__); - return(TNG_FAILURE); - } + *data = (char *)dest; + + *new_len = compressed_len; return(TNG_SUCCESS); } @@ -5029,13 +4921,11 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, static tng_function_status tng_uncompress(tng_trajectory_t tng_data, tng_gen_block_t block, const char type, - void *start_pos, - const unsigned long uncompressed_len) + char **data, + const int64_t uncompressed_len) { - char *temp; double *d_dest = 0; float *f_dest = 0; - unsigned long offset; int result; (void)tng_data; @@ -5059,22 +4949,30 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, f_dest = malloc(uncompressed_len); if(!f_dest) { - fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n", + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", uncompressed_len, __FILE__, __LINE__); return(TNG_CRITICAL); } - result = tng_compress_uncompress_float(start_pos, f_dest); + result = tng_compress_uncompress_float(*data, f_dest); + + free(*data); + + *data = (char *)f_dest; } else { d_dest = malloc(uncompressed_len); if(!d_dest) { - fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n", + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", uncompressed_len, __FILE__, __LINE__); return(TNG_CRITICAL); } - result = tng_compress_uncompress(start_pos, d_dest); + result = tng_compress_uncompress(*data, d_dest); + + free(*data); + + *data = (char *)d_dest; } if(result == 1) @@ -5083,58 +4981,16 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, return(TNG_FAILURE); } - offset = (unsigned long)((char *)start_pos - (char *)block->block_contents); - - block->block_contents_size = (int64_t)(uncompressed_len + offset); - - temp = realloc(block->block_contents, uncompressed_len + offset); - if(!temp) - { - free(block->block_contents); - block->block_contents = 0; - if(d_dest) - { - free(d_dest); - } - if(f_dest) - { - free(f_dest); - } - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - - if(type == TNG_FLOAT_DATA) - { - memcpy(temp + offset, f_dest, uncompressed_len); - } - else - { - memcpy(temp + offset, d_dest, uncompressed_len); - } - - block->block_contents = temp; - - if(d_dest) - { - free(d_dest); - } - if(f_dest) - { - free(f_dest); - } return(TNG_SUCCESS); } #ifdef USE_ZLIB static tng_function_status tng_gzip_compress(tng_trajectory_t tng_data, - tng_gen_block_t block, - void *start_pos, const int len) + char **data, const int64_t len, + int64_t *new_len) { Bytef *dest; - char *temp; - unsigned long max_len, stat, offset; + uLongf stat, max_len; (void)tng_data; max_len = compressBound(len); @@ -5146,7 +5002,7 @@ static tng_function_status tng_gzip_compress(tng_trajectory_t tng_data, return(TNG_CRITICAL); } - stat = compress(dest, &max_len, start_pos, len); + stat = compress(dest, &max_len, (Bytef *)*data, len); if(stat != (unsigned long)Z_OK) { free(dest); @@ -5162,53 +5018,35 @@ static tng_function_status tng_gzip_compress(tng_trajectory_t tng_data, return(TNG_FAILURE); } - offset = (char *)start_pos - block->block_contents; - - block->block_contents_size = max_len + offset; - - temp = realloc(block->block_contents, block->block_contents_size); - if(!temp) - { - free(block->block_contents); - free(dest); - block->block_contents = 0; - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - - block->block_contents = temp; + *new_len = max_len; - memcpy(temp + offset, dest, max_len); + free(*data); - free(dest); + *data = (char *)dest; return(TNG_SUCCESS); } static tng_function_status tng_gzip_uncompress(tng_trajectory_t tng_data, - tng_gen_block_t block, - void *start_pos, - unsigned long uncompressed_len) + char **data, + const int64_t compressed_len, + const int64_t uncompressed_len) { Bytef *dest; - char *temp; unsigned long stat; - int offset; (void)tng_data; - - offset = (char *)start_pos - (char *)block->block_contents; + uLongf new_len = uncompressed_len; dest = malloc(uncompressed_len); if(!dest) { - fprintf(stderr, "TNG library: Cannot allocate memory (%lud bytes). %s: %d\n", + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", uncompressed_len, __FILE__, __LINE__); return(TNG_CRITICAL); } - stat = uncompress(dest, &uncompressed_len, (Bytef *) start_pos, - block->block_contents_size - offset); + stat = uncompress(dest, &new_len, (Bytef *) *data, + compressed_len); if(stat != Z_OK) { @@ -5230,25 +5068,10 @@ static tng_function_status tng_gzip_uncompress(tng_trajectory_t tng_data, return(TNG_FAILURE); } + free(*data); - block->block_contents_size = uncompressed_len + offset; - - temp = realloc(block->block_contents, uncompressed_len + offset); - if(!temp) - { - free(block->block_contents); - block->block_contents = 0; - free(dest); - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - - memcpy(temp + offset, dest, uncompressed_len); - - block->block_contents = temp; + *data = (char *)dest; - free(dest); return(TNG_SUCCESS); } #endif @@ -5620,7 +5443,7 @@ static tng_function_status tng_data_block_len_calculate static tng_function_status tng_particle_data_read (tng_trajectory_t tng_data, tng_gen_block_t block, - int *offset, + int64_t block_data_len, const char datatype, const int64_t num_first_particle, const int64_t n_particles, @@ -5629,18 +5452,18 @@ static tng_function_status tng_particle_data_read int64_t n_frames, const int64_t n_values, const int64_t codec_id, - const double multiplier) + const double multiplier, + const char hash_mode, + md5_state_t *md5_state) { int64_t i, j, k, tot_n_particles, n_frames_div; + int64_t offset, full_data_len; int size, len; - unsigned long data_size; char ***first_dim_values, **second_dim_values; tng_particle_data_t data; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; - char block_type_flag; - - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); + char block_type_flag, *contents; switch(datatype) { @@ -5693,7 +5516,7 @@ static tng_function_status tng_particle_data_read if(!data->block_name) { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - (int)strlen(block->name)+1, __FILE__, __LINE__); + (unsigned int)strlen(block->name)+1, __FILE__, __LINE__); return(TNG_CRITICAL); } strcpy(data->block_name, block->name); @@ -5722,9 +5545,28 @@ static tng_function_status tng_particle_data_read n_frames_div = (n_frames % stride_length) ? n_frames / stride_length + 1 : n_frames / stride_length; + contents = malloc(block_data_len); + if(!contents) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + block_data_len, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + if(fread(contents, block_data_len, 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)contents, block_data_len); + } + if(codec_id != TNG_UNCOMPRESSED) { - data_size = (unsigned long)(n_frames_div * size * n_particles * n_values); + full_data_len = n_frames_div * size * n_particles * n_values; switch(codec_id) { case TNG_XTC_COMPRESSION: @@ -5733,11 +5575,11 @@ static tng_function_status tng_particle_data_read case TNG_TNG_COMPRESSION: /* fprintf(stderr, "TNG library: Before TNG uncompression: %"PRId64"\n", block->block_contents_size);*/ if(tng_uncompress(tng_data, block, datatype, - block->block_contents + *offset, - data_size) != TNG_SUCCESS) + &contents, full_data_len) != TNG_SUCCESS) { fprintf(stderr, "TNG library: Could not read tng compressed block data. %s: %d\n", __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } /* fprintf(stderr, "TNG library: After TNG uncompression: %"PRId64"\n", block->block_contents_size);*/ @@ -5745,12 +5587,12 @@ static tng_function_status tng_particle_data_read #ifdef USE_ZLIB case TNG_GZIP_COMPRESSION: /* fprintf(stderr, "TNG library: Before GZIP uncompression: %"PRId64"\n", block->block_contents_size);*/ - if(tng_gzip_uncompress(tng_data, block, - block->block_contents + *offset, - data_size) != TNG_SUCCESS) + if(tng_gzip_uncompress(tng_data, &contents, block_data_len, + full_data_len) != TNG_SUCCESS) { fprintf(stderr, "TNG library: Could not read gzipped block data. %s: %d\n", __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } /* fprintf(stderr, "TNG library: After GZIP uncompression: %"PRId64"\n", block->block_contents_size);*/ @@ -5758,6 +5600,11 @@ static tng_function_status tng_particle_data_read #endif } } + else + { + full_data_len = block_data_len; + } + /* Allocate memory */ if(!data->values || data->n_frames != n_frames || data->n_values_per_frame != n_values) @@ -5769,6 +5616,7 @@ static tng_function_status tng_particle_data_read { fprintf(stderr, "TNG library: Cannot allocate memory for particle data. %s: %d\n", __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } } @@ -5777,6 +5625,7 @@ static tng_function_status tng_particle_data_read if(datatype == TNG_CHAR_DATA) { + offset = 0; for(i = 0; i < n_frames_div; i++) { first_dim_values = data->strings[i]; @@ -5786,7 +5635,7 @@ static tng_function_status tng_particle_data_read second_dim_values = first_dim_values[j]; for(k = 0; k < n_values; k++) { - len = tng_min_i((int)strlen(block->block_contents+*offset) + 1, + len = tng_min_i((unsigned int)strlen(contents+offset) + 1, TNG_MAX_STR_LEN); if(second_dim_values[k]) { @@ -5797,11 +5646,11 @@ static tng_function_status tng_particle_data_read { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } - strncpy(second_dim_values[k], - block->block_contents+*offset, len); - *offset += len; + strncpy(second_dim_values[k], contents+offset, len); + offset += len; } } } @@ -5810,14 +5659,13 @@ static tng_function_status tng_particle_data_read { memcpy((char *)data->values + n_frames_div * size * n_values * num_first_particle, - block->block_contents + *offset, - block->block_contents_size - *offset); + contents, full_data_len); switch(datatype) { case TNG_FLOAT_DATA: if(tng_data->input_endianness_swap_func_32) { - for(i = 0; i < (block->block_contents_size - *offset); i+=size) + for(i = 0; i < full_data_len; i+=size) { if(tng_data->input_endianness_swap_func_32(tng_data, (int32_t *)((char *)data->values + i)) @@ -5833,7 +5681,7 @@ static tng_function_status tng_particle_data_read case TNG_DOUBLE_DATA: if(tng_data->input_endianness_swap_func_64) { - for(i = 0; i < (block->block_contents_size - *offset); i+=size) + for(i = 0; i < full_data_len; i+=size) { if(tng_data->input_endianness_swap_func_64(tng_data, (int64_t *)((char *)data->values + i)) @@ -5849,6 +5697,9 @@ static tng_function_status tng_particle_data_read break; } } + + free(contents); + return(TNG_SUCCESS); } @@ -5871,18 +5722,18 @@ static tng_function_status tng_particle_data_block_write const char hash_mode) { int64_t n_particles, num_first_particle, n_frames, stride_length; - int64_t frame_step, data_start_pos; - int64_t i, j, k; + int64_t full_data_len, block_data_len, frame_step, data_start_pos; + int64_t temp_i64, i, j, k, curr_file_pos, header_file_pos; int size; - size_t len, offset = 0; + size_t len; char dependency, temp, *temp_name; - double multiplier; - char ***first_dim_values, **second_dim_values; + double multiplier, temp_d; + char ***first_dim_values, **second_dim_values, *contents; tng_trajectory_frame_set_t frame_set; tng_function_status stat; - tng_particle_data_t data; char block_type_flag; + md5_state_t md5_state; frame_set = &tng_data->current_trajectory_frame_set; @@ -6023,24 +5874,35 @@ static tng_function_status tng_particle_data_block_write return(TNG_CRITICAL); } - if(block->block_contents) + header_file_pos = ftello(tng_data->output_file); + + if(tng_block_header_write(tng_data, block) != TNG_SUCCESS) { - free(block->block_contents); + fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", + tng_data->output_file_path, __FILE__, __LINE__); + return(TNG_CRITICAL); } - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) + + if(fwrite(&data->datatype, sizeof(data->datatype), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&data->datatype, sizeof(data->datatype)); + } - - memcpy(block->block_contents, &data->datatype, sizeof(char)); - offset += sizeof(char); - - memcpy(block->block_contents+offset, &dependency, sizeof(char)); - offset += sizeof(char); + if(fwrite(&dependency, sizeof(dependency), 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&dependency, sizeof(dependency)); + } if(dependency & TNG_FRAME_DEPENDENT) { @@ -6052,53 +5914,54 @@ static tng_function_status tng_particle_data_block_write { temp = 0; } - memcpy(block->block_contents+offset, &temp, sizeof(char)); - offset += sizeof(char); + if(fwrite(&temp, sizeof(temp), 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp, sizeof(temp)); + } } - memcpy(block->block_contents+offset, &data->n_values_per_frame, - sizeof(data->n_values_per_frame)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = data->n_values_per_frame; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->n_values_per_frame); - memcpy(block->block_contents+offset, &data->codec_id, - sizeof(data->codec_id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = data->codec_id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->codec_id); if(data->codec_id != TNG_UNCOMPRESSED) { - memcpy(block->block_contents+offset, &data->compression_multiplier, - sizeof(data->compression_multiplier)); - if(tng_data->output_endianness_swap_func_64) + temp_d = data->compression_multiplier; + tng_var_64_bit_output_swap(tng_data, (int64_t *)&temp_d); + if(fwrite(&temp_d, sizeof(temp_d), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_d, sizeof(temp_d)); } - offset += sizeof(data->compression_multiplier); } if(data->n_frames > 0 && stride_length > 1) @@ -6108,62 +5971,54 @@ static tng_function_status tng_particle_data_block_write { data->first_frame_with_data = frame_set->first_frame; } - memcpy(block->block_contents+offset, &data->first_frame_with_data, - sizeof(data->first_frame_with_data)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = data->first_frame_with_data; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->first_frame_with_data); - memcpy(block->block_contents+offset, &stride_length, - sizeof(stride_length)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = stride_length; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(stride_length); } - - memcpy(block->block_contents+offset, &num_first_particle, - sizeof(num_first_particle)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = num_first_particle; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(num_first_particle); - memcpy(block->block_contents+offset, &n_particles, sizeof(n_particles)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = n_particles; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(n_particles); if(data->datatype == TNG_CHAR_DATA) { @@ -6178,129 +6033,143 @@ static tng_function_status tng_particle_data_block_write second_dim_values = first_dim_values[j]; for(k = 0; k < data->n_values_per_frame; k++) { - len = (unsigned int)strlen(second_dim_values[k]) + 1; - strncpy(block->block_contents+offset, - second_dim_values[k], len); - offset += len; + len = tng_min_i((unsigned int)strlen(second_dim_values[k]) + 1, + TNG_MAX_STR_LEN); + if(fwrite(second_dim_values[k], len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)second_dim_values[k], len); + } } } } } } - else if(data->values) + else { - memcpy(block->block_contents + offset, data->values, - block->block_contents_size - offset); + full_data_len = size * frame_step * n_particles * data->n_values_per_frame; + contents = malloc(full_data_len); + if(!contents) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + full_data_len, __FILE__, __LINE__); + return(TNG_CRITICAL); + } - switch(data->datatype) + if(data->values) { - case TNG_FLOAT_DATA: - if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || - data->codec_id == TNG_TNG_COMPRESSION) + memcpy(contents, data->values, full_data_len); + + switch(data->datatype) { - if(tng_data->input_endianness_swap_func_32) + case TNG_FLOAT_DATA: + if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || + data->codec_id == TNG_TNG_COMPRESSION) { - for(i = offset; i < block->block_contents_size; i+=size) + if(tng_data->output_endianness_swap_func_32) { - if(tng_data->input_endianness_swap_func_32(tng_data, - (int32_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + if(tng_data->output_endianness_swap_func_32(tng_data, + (int32_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } - } - else - { - multiplier = data->compression_multiplier; - if(fabs(multiplier - 1.0) > 0.00001 || - tng_data->input_endianness_swap_func_32) + else { - for(i = offset; i < block->block_contents_size; i+=size) + multiplier = data->compression_multiplier; + if(fabs(multiplier - 1.0) > 0.00001 || + tng_data->output_endianness_swap_func_32) { - *(float *)(block->block_contents + i) *= (float)multiplier; - if(tng_data->input_endianness_swap_func_32 && - tng_data->input_endianness_swap_func_32(tng_data, - (int32_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + *(float *)(contents + i) *= (float)multiplier; + if(tng_data->output_endianness_swap_func_32 && + tng_data->output_endianness_swap_func_32(tng_data, + (int32_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } - } - break; - case TNG_INT_DATA: - if(tng_data->input_endianness_swap_func_64) - { - for(i = offset; i < block->block_contents_size; i+=size) + break; + case TNG_INT_DATA: + if(tng_data->output_endianness_swap_func_64) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + if(tng_data->output_endianness_swap_func_64(tng_data, + (int64_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } - } - break; - case TNG_DOUBLE_DATA: - if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || - data->codec_id == TNG_TNG_COMPRESSION) - { - if(tng_data->input_endianness_swap_func_64) + break; + case TNG_DOUBLE_DATA: + if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || + data->codec_id == TNG_TNG_COMPRESSION) { - for(i = offset; i < block->block_contents_size; i+=size) + if(tng_data->output_endianness_swap_func_64) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + if(tng_data->output_endianness_swap_func_64(tng_data, + (int64_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } - } - else - { - multiplier = data->compression_multiplier; - if(fabs(multiplier - 1.0) > 0.00001 || - tng_data->input_endianness_swap_func_64) + else { - for(i = offset; i < block->block_contents_size; i+=size) + multiplier = data->compression_multiplier; + if(fabs(multiplier - 1.0) > 0.00001 || + tng_data->output_endianness_swap_func_64) { - *(double *)(block->block_contents + i) *= multiplier; - if(tng_data->input_endianness_swap_func_64 && - tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + *(double *)(contents + i) *= multiplier; + if(tng_data->output_endianness_swap_func_64 && + tng_data->output_endianness_swap_func_64(tng_data, + (int64_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } + break; + case TNG_CHAR_DATA: + break; } - break; - case TNG_CHAR_DATA: - break; } - } - else - { - memset(block->block_contents+offset, 0, block->block_contents_size - offset); - } + else + { + memset(contents, 0, full_data_len); + } - frame_set->n_written_frames += frame_set->n_unwritten_frames; - frame_set->n_unwritten_frames = 0; + block_data_len = full_data_len; - if(block_type_flag == TNG_NON_TRAJECTORY_BLOCK || frame_set->n_written_frames > 0) - { switch(data->codec_id) { case TNG_XTC_COMPRESSION: @@ -6310,7 +6179,7 @@ static tng_function_status tng_particle_data_block_write case TNG_TNG_COMPRESSION: stat = tng_compress(tng_data, block, frame_step, n_particles, data->datatype, - block->block_contents + data_start_pos); + &contents, &block_data_len); if(stat != TNG_SUCCESS) { fprintf(stderr, "TNG library: Could not write tng compressed block data. %s: %d\n", @@ -6325,15 +6194,17 @@ static tng_function_status tng_particle_data_block_write stat = tng_particle_data_block_write(tng_data, block, block_index, mapping, hash_mode); + free(contents); return(stat); } break; #ifdef USE_ZLIB case TNG_GZIP_COMPRESSION: /* fprintf(stderr, "TNG library: Before compression: %"PRId64"\n", block->block_contents_size);*/ - stat = tng_gzip_compress(tng_data, block, - block->block_contents + data_start_pos, - block->block_contents_size - data_start_pos); + stat = tng_gzip_compress(tng_data, + &contents, + full_data_len, + &block_data_len); if(stat != TNG_SUCCESS) { fprintf(stderr, "TNG library: Could not write gzipped block data. %s: %d\n", __FILE__, @@ -6348,29 +6219,61 @@ static tng_function_status tng_particle_data_block_write stat = tng_particle_data_block_write(tng_data, block, block_index, mapping, hash_mode); + free(contents); return(stat); } /* fprintf(stderr, "TNG library: After compression: %"PRId64"\n", block->block_contents_size);*/ break; #endif } - } + if(block_data_len != full_data_len) + { + block->block_contents_size -= full_data_len - block_data_len; - if(tng_block_header_write(tng_data, block, hash_mode) != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", - tng_data->output_file_path, __FILE__, __LINE__); - return(TNG_CRITICAL); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + sizeof(block->header_contents_size), SEEK_SET); + + temp_i64 = block->block_contents_size; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); + } + if(fwrite(contents, block_data_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)contents, block_data_len); + } + + free(contents); } - if(fwrite(block->block_contents, block->block_contents_size, 1, - tng_data->output_file) != 1) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", __FILE__, - __LINE__); - return(TNG_CRITICAL); + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + + 3 * sizeof(int64_t), SEEK_SET); + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write MD5 hash. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); } + frame_set->n_written_frames += frame_set->n_unwritten_frames; + frame_set->n_unwritten_frames = 0; + return(TNG_SUCCESS); } @@ -6534,9 +6437,6 @@ static tng_function_status tng_allocate_data_mem * @param tng_data is a trajectory data container. * @param block is the block to store the data (should already contain * the block headers and the block contents). - * @param offset is the reading offset to point at the place where the actual - * values are stored, starting from the beginning of the block_contents. The - * offset is changed during the reading. * @param datatype is the type of data of the data block (char, int, float or * double). * @param first_frame_with_data is the frame number of the first frame with data @@ -6553,26 +6453,24 @@ static tng_function_status tng_allocate_data_mem */ static tng_function_status tng_data_read(tng_trajectory_t tng_data, tng_gen_block_t block, - int *offset, + int64_t block_data_len, const char datatype, const int64_t first_frame_with_data, const int64_t stride_length, int64_t n_frames, const int64_t n_values, const int64_t codec_id, - const double multiplier) + const double multiplier, + const char hash_mode, + md5_state_t *md5_state) { - int64_t i, j, n_frames_div; + int64_t i, j, n_frames_div, offset; int size, len; -#ifdef USE_ZLIB - unsigned long data_size; -#endif + int64_t full_data_len; tng_non_particle_data_t data; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; - char block_type_flag; - - TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer."); + char block_type_flag, *contents; /* fprintf(stderr, "TNG library: %s\n", block->name);*/ @@ -6625,7 +6523,7 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, if(!data->block_name) { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - (int)strlen(block->name)+1, __FILE__, __LINE__); + (unsigned int)strlen(block->name)+1, __FILE__, __LINE__); return(TNG_CRITICAL); } strcpy(data->block_name, block->name); @@ -6643,20 +6541,39 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, n_frames_div = (n_frames % stride_length) ? n_frames / stride_length + 1 : n_frames / stride_length; + contents = malloc(block_data_len); + if(!contents) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + block_data_len, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + if(fread(contents, block_data_len, 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)contents, block_data_len); + } + if(codec_id != TNG_UNCOMPRESSED) { + full_data_len = n_frames_div * size * n_values; switch(codec_id) { #ifdef USE_ZLIB case TNG_GZIP_COMPRESSION: - data_size = n_frames_div * size * n_values; /* fprintf(stderr, "TNG library: Before compression: %"PRId64"\n", block->block_contents_size); */ - if(tng_gzip_uncompress(tng_data, block, - block->block_contents + *offset, - data_size) != TNG_SUCCESS) + if(tng_gzip_uncompress(tng_data, &contents, + block_data_len, full_data_len) != TNG_SUCCESS) { fprintf(stderr, "TNG library: Could not read gzipped block data. %s: %d\n", __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } /* fprintf(stderr, "TNG library: After compression: %"PRId64"\n", block->block_contents_size); */ @@ -6664,6 +6581,10 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, #endif } } + else + { + full_data_len = block_data_len; + } /* Allocate memory */ if(!data->values || data->n_frames != n_frames || @@ -6675,6 +6596,7 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, { fprintf(stderr, "TNG library: Cannot allocate memory for data. %s: %d\n", __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } } @@ -6683,12 +6605,13 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, if(datatype == TNG_CHAR_DATA) { + offset = 0; for(i = 0; i < n_frames_div; i++) { for(j = 0; j < n_values; j++) { - len = tng_min_i((int)strlen(block->block_contents+*offset) + 1, - TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(contents+offset) + 1, + TNG_MAX_STR_LEN); if(data->strings[i][j]) { free(data->strings[i][j]); @@ -6698,24 +6621,23 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len, __FILE__, __LINE__); + free(contents); return(TNG_CRITICAL); } - strncpy(data->strings[i][j], block->block_contents+*offset, - len); - *offset += len; + strncpy(data->strings[i][j], contents+offset, len); + offset += len; } } } else { - memcpy(data->values, block->block_contents + *offset, - block->block_contents_size - *offset); + memcpy(data->values, contents, full_data_len); switch(datatype) { case TNG_FLOAT_DATA: if(tng_data->input_endianness_swap_func_32) { - for(i = 0; i < (block->block_contents_size - *offset); i+=size) + for(i = 0; i < full_data_len; i+=size) { if(tng_data->input_endianness_swap_func_32(tng_data, (int32_t *)((char *)data->values + i)) @@ -6731,7 +6653,7 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, case TNG_DOUBLE_DATA: if(tng_data->input_endianness_swap_func_64) { - for(i = 0; i < (block->block_contents_size - *offset); i+=size) + for(i = 0; i < full_data_len; i+=size) { if(tng_data->input_endianness_swap_func_64(tng_data, (int64_t *)((char *)data->values + i)) @@ -6747,6 +6669,9 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, break; } } + + free(contents); + return(TNG_SUCCESS); } @@ -6766,19 +6691,20 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, const char hash_mode) { int64_t n_frames, stride_length, frame_step, data_start_pos; - int64_t i, j; - int offset = 0, size; + int64_t temp_i64, full_data_len, block_data_len, i, j; + int64_t curr_file_pos, header_file_pos; + int size; unsigned int len; #ifdef USE_ZLIB tng_function_status stat; #endif - char temp, dependency, *temp_name; - double multiplier; + char temp, dependency, *temp_name, *contents; + double multiplier, temp_d; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; - tng_non_particle_data_t data; char block_type_flag; + md5_state_t md5_state; /* If we have already started writing frame sets it is too late to write * non-trajectory data blocks */ @@ -6899,24 +6825,35 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, return(TNG_CRITICAL); } - if(block->block_contents) + header_file_pos = ftello(tng_data->output_file); + + if(tng_block_header_write(tng_data, block) != TNG_SUCCESS) { - free(block->block_contents); + fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", + tng_data->output_file_path, __FILE__, __LINE__); + return(TNG_CRITICAL); } - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) + + if(fwrite(&data->datatype, sizeof(data->datatype), 1, tng_data->output_file) != 1) { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_init(&md5_state); + md5_append(&md5_state, (md5_byte_t *)&data->datatype, sizeof(data->datatype)); + } - - memcpy(block->block_contents, &data->datatype, sizeof(char)); - offset += sizeof(char); - - memcpy(block->block_contents+offset, &dependency, sizeof(char)); - offset += sizeof(char); + if(fwrite(&dependency, sizeof(dependency), 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&dependency, sizeof(dependency)); + } if(dependency & TNG_FRAME_DEPENDENT) { @@ -6928,53 +6865,54 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, { temp = 0; } - memcpy(block->block_contents+offset, &temp, sizeof(char)); - offset += sizeof(char); + if(fwrite(&temp, sizeof(temp), 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp, sizeof(temp)); + } } - memcpy(block->block_contents+offset, &data->n_values_per_frame, - sizeof(data->n_values_per_frame)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = data->n_values_per_frame; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->n_values_per_frame); - memcpy(block->block_contents+offset, &data->codec_id, - sizeof(data->codec_id)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = data->codec_id; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->codec_id); if(data->codec_id != TNG_UNCOMPRESSED) { - memcpy(block->block_contents+offset, &data->compression_multiplier, - sizeof(data->compression_multiplier)); - if(tng_data->output_endianness_swap_func_64) + temp_d = data->compression_multiplier; + tng_var_64_bit_output_swap(tng_data, (int64_t *)&temp_d); + if(fwrite(&temp_d, sizeof(temp_d), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_d, sizeof(temp_d)); } - offset += sizeof(data->compression_multiplier); } if(data->n_frames > 0 && stride_length > 1) @@ -6984,33 +6922,29 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, { data->first_frame_with_data = frame_set->first_frame; } - memcpy(block->block_contents+offset, &data->first_frame_with_data, - sizeof(data->first_frame_with_data)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = data->first_frame_with_data; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->first_frame_with_data); - memcpy(block->block_contents+offset, &stride_length, - sizeof(data->stride_length)); - if(tng_data->output_endianness_swap_func_64) + temp_i64 = stride_length; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)&temp_i64, sizeof(temp_i64)); } - offset += sizeof(data->stride_length); } if(data->datatype == TNG_CHAR_DATA) @@ -7022,134 +6956,148 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, for(j = 0; j < data->n_values_per_frame; j++) { len = (unsigned int)strlen(data->strings[i][j]) + 1; - strncpy(block->block_contents+offset, data->strings[i][j], - len); - offset += len; + if(fwrite(data->strings[i][j], len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)data->strings[i][j], len); + } } } } } - else if(data->values) + else { - memcpy(block->block_contents + offset, data->values, - block->block_contents_size - offset); - switch(data->datatype) + full_data_len = size * frame_step * data->n_values_per_frame; + contents = malloc(full_data_len); + if(!contents) { - case TNG_FLOAT_DATA: - if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || - data->codec_id == TNG_TNG_COMPRESSION) + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + full_data_len, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + if(data->values) + { + memcpy(contents, data->values, full_data_len); + switch(data->datatype) { - if(tng_data->input_endianness_swap_func_32) + case TNG_FLOAT_DATA: + if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || + data->codec_id == TNG_TNG_COMPRESSION) { - for(i = offset; i < block->block_contents_size; i+=size) + if(tng_data->output_endianness_swap_func_32) { - if(tng_data->input_endianness_swap_func_32(tng_data, - (int32_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + if(tng_data->output_endianness_swap_func_32(tng_data, + (int32_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } - } - else - { - multiplier = data->compression_multiplier; - if(fabs(multiplier - 1.0) > 0.00001 || - tng_data->input_endianness_swap_func_32) + else { - for(i = offset; block->block_contents_size; i+=size) + multiplier = data->compression_multiplier; + if(fabs(multiplier - 1.0) > 0.00001 || + tng_data->output_endianness_swap_func_32) { - *(float *)(block->block_contents + i) *= (float)multiplier; - if(tng_data->input_endianness_swap_func_32 && - tng_data->input_endianness_swap_func_32(tng_data, - (int32_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + *(float *)(contents + i) *= (float)multiplier; + if(tng_data->output_endianness_swap_func_32 && + tng_data->output_endianness_swap_func_32(tng_data, + (int32_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } - } - break; - case TNG_INT_DATA: - if(tng_data->input_endianness_swap_func_64) - { - for(i = offset; i < block->block_contents_size; i+=size) + break; + case TNG_INT_DATA: + if(tng_data->output_endianness_swap_func_64) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + if(tng_data->output_endianness_swap_func_64(tng_data, + (int64_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } - } - break; - case TNG_DOUBLE_DATA: - if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || - data->codec_id == TNG_TNG_COMPRESSION) - { - if(tng_data->input_endianness_swap_func_64) + break; + case TNG_DOUBLE_DATA: + if(data->codec_id == TNG_UNCOMPRESSED || data-> codec_id == TNG_GZIP_COMPRESSION || + data->codec_id == TNG_TNG_COMPRESSION) { - for(i = offset; i < block->block_contents_size; i+=size) + if(tng_data->output_endianness_swap_func_64) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + if(tng_data->output_endianness_swap_func_64(tng_data, + (int64_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } - } - else - { - multiplier = data->compression_multiplier; - if(fabs(multiplier - 1.0) > 0.00001 || - tng_data->input_endianness_swap_func_64) + else { - for(i = offset; i < block->block_contents_size; i+=size) + multiplier = data->compression_multiplier; + if(fabs(multiplier - 1.0) > 0.00001 || + tng_data->output_endianness_swap_func_64) { - *(double *)(block->block_contents + i) *= multiplier; - if(tng_data->input_endianness_swap_func_64 && - tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)(block->block_contents + i)) - != TNG_SUCCESS) + for(i = 0; i < full_data_len; i+=size) { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); + *(double *)(contents + i) *= multiplier; + if(tng_data->output_endianness_swap_func_64 && + tng_data->output_endianness_swap_func_64(tng_data, + (int64_t *)(contents + i)) + != TNG_SUCCESS) + { + fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } } } } + break; + case TNG_CHAR_DATA: + break; } - break; - case TNG_CHAR_DATA: - break; } - } - else - { - memset(block->block_contents+offset, 0, block->block_contents_size - offset); - } + else + { + memset(contents, 0, full_data_len); + } - frame_set->n_written_frames += frame_set->n_unwritten_frames; - frame_set->n_unwritten_frames = 0; + block_data_len = full_data_len; - if(block_type_flag == TNG_NON_TRAJECTORY_BLOCK || frame_set->n_written_frames > 0) - { switch(data->codec_id) { #ifdef USE_ZLIB case TNG_GZIP_COMPRESSION: /* fprintf(stderr, "TNG library: Before compression: %"PRId64"\n", block->block_contents_size); */ - stat = tng_gzip_compress(tng_data, block, - block->block_contents + data_start_pos, - block->block_contents_size - data_start_pos); + stat = tng_gzip_compress(tng_data, + &contents, + full_data_len, + &block_data_len); if(stat != TNG_SUCCESS) { fprintf(stderr, "TNG library: Could not write gzipped block data. %s: %d\n", __FILE__, @@ -7164,23 +7112,54 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, break; #endif } - } + if(block_data_len != full_data_len) + { + block->block_contents_size -= full_data_len - block_data_len; - if(tng_block_header_write(tng_data, block, hash_mode) != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot write header of file %s. %s: %d\n", - tng_data->output_file_path, __FILE__, __LINE__); - return(TNG_CRITICAL); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + sizeof(block->header_contents_size), SEEK_SET); + + temp_i64 = block->block_contents_size; + tng_var_64_bit_output_swap(tng_data, &temp_i64); + if(fwrite(&temp_i64, sizeof(temp_i64), 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write block data. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); + } + if(fwrite(contents, block_data_len, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(&md5_state, (md5_byte_t *)contents, block_data_len); + } + + free(contents); } - if(fwrite(block->block_contents, block->block_contents_size, 1, - tng_data->output_file) != 1) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Could not write all block data. %s: %d\n", - __FILE__, __LINE__); - return(TNG_CRITICAL); + md5_finish(&md5_state, (md5_byte_t *)block->md5_hash); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, header_file_pos + + 3 * sizeof(int64_t), SEEK_SET); + if(fwrite(block->md5_hash, TNG_MD5_HASH_LEN, 1, tng_data->output_file) != 1) + { + fprintf(stderr, "TNG library: Could not write MD5 hash. %s: %d\n", __FILE__, + __LINE__); + return(TNG_CRITICAL); + } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); } + frame_set->n_written_frames += frame_set->n_unwritten_frames; + frame_set->n_unwritten_frames = 0; + return(TNG_SUCCESS); } @@ -7193,8 +7172,6 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, */ static tng_function_status tng_data_block_meta_information_read (tng_trajectory_t tng_data, - tng_gen_block_t block, - int *offset, char *datatype, char *dependency, char *sparse_data, @@ -7205,91 +7182,83 @@ static tng_function_status tng_data_block_meta_information_read int64_t *n_frames, int64_t *num_first_particle, int64_t *block_n_particles, - double *multiplier) + double *multiplier, + const char hash_mode, + md5_state_t *md5_state) { - int meta_size; - char *contents; + if(fread(datatype, sizeof(char), + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)datatype, sizeof(char)); + } - if(block->block_contents) + if(fread(dependency, sizeof(char), + 1, tng_data->input_file) == 0) { - contents = block->block_contents; + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - else + if(hash_mode == TNG_USE_HASH) { - meta_size = 3 * sizeof(char) + sizeof(double) + 6 * sizeof(int64_t); - contents = malloc(meta_size); - if(!contents) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - meta_size, __FILE__, __LINE__); - } + md5_append(md5_state, (md5_byte_t *)dependency, sizeof(char)); + } - if(fread(contents, meta_size, 1, tng_data->input_file) == 0) + if(*dependency & TNG_FRAME_DEPENDENT) + { + if(fread(sparse_data, sizeof(char), + 1, tng_data->input_file) == 0) { - fprintf(stderr, "TNG library: Cannot read data block meta information. %s: %d\n", __FILE__, __LINE__); - free(contents); + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)sparse_data, sizeof(char)); + } } - memcpy(datatype, contents+*offset, - sizeof(*datatype)); - *offset += sizeof(*datatype); - - memcpy(dependency, contents+*offset, - sizeof(*dependency)); - *offset += sizeof(*dependency); - - if(*dependency & TNG_FRAME_DEPENDENT) + if(fread(n_values, sizeof(int64_t), + 1, tng_data->input_file) == 0) { - memcpy(sparse_data, contents+*offset, - sizeof(*sparse_data)); - *offset += sizeof(*sparse_data); + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - - memcpy(n_values, contents+*offset, - sizeof(*n_values)); - if(tng_data->input_endianness_swap_func_64) + if(hash_mode == TNG_USE_HASH) { - if(tng_data->input_endianness_swap_func_64(tng_data, - n_values) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + md5_append(md5_state, (md5_byte_t *)n_values, sizeof(*n_values)); } - *offset += sizeof(*n_values); + tng_var_64_bit_input_swap(tng_data, n_values); - memcpy(codec_id, contents+*offset, - sizeof(*codec_id)); - if(tng_data->input_endianness_swap_func_64) + if(fread(codec_id, sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - codec_id) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)codec_id, sizeof(*codec_id)); } - *offset += sizeof(*codec_id); + tng_var_64_bit_input_swap(tng_data, codec_id); if(*codec_id != TNG_UNCOMPRESSED) { - memcpy(multiplier, contents+*offset, - sizeof(*multiplier)); - if(tng_data->input_endianness_swap_func_64) + if(fread(multiplier, sizeof(double), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *) multiplier) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += sizeof(*multiplier); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)multiplier, sizeof(*multiplier)); + } + tng_var_64_bit_input_swap(tng_data, (int64_t *)multiplier); } else { @@ -7300,33 +7269,30 @@ static tng_function_status tng_data_block_meta_information_read { if(*sparse_data) { - memcpy(first_frame_with_data, contents+*offset, - sizeof(*first_frame_with_data)); - if(tng_data->input_endianness_swap_func_64) + if(fread(first_frame_with_data, sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - first_frame_with_data) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += sizeof(*first_frame_with_data); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)first_frame_with_data, sizeof(*first_frame_with_data)); + } + tng_var_64_bit_input_swap(tng_data, first_frame_with_data); - memcpy(stride_length, contents+*offset, - sizeof(*stride_length)); - if(tng_data->input_endianness_swap_func_64) + if(fread(stride_length, sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - stride_length) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)stride_length, sizeof(*stride_length)); } - *offset += sizeof(*stride_length); + tng_var_64_bit_input_swap(tng_data, stride_length); + *n_frames = tng_data->current_trajectory_frame_set.n_frames - (*first_frame_with_data - tng_data->current_trajectory_frame_set.first_frame); @@ -7347,39 +7313,31 @@ static tng_function_status tng_data_block_meta_information_read if (*dependency & TNG_PARTICLE_DEPENDENT) { - memcpy(num_first_particle, contents+*offset, - sizeof(*num_first_particle)); - if(tng_data->input_endianness_swap_func_64) + if(fread(num_first_particle, sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - num_first_particle) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)num_first_particle, sizeof(*num_first_particle)); } - *offset += sizeof(*num_first_particle); + tng_var_64_bit_input_swap(tng_data, num_first_particle); - memcpy(block_n_particles, contents+*offset, - sizeof(*block_n_particles)); - if(tng_data->input_endianness_swap_func_64) + if(fread(block_n_particles, sizeof(int64_t), + 1, tng_data->input_file) == 0) { - if(tng_data->input_endianness_swap_func_64(tng_data, - block_n_particles) - != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += sizeof(*block_n_particles); + if(hash_mode == TNG_USE_HASH) + { + md5_append(md5_state, (md5_byte_t *)block_n_particles, sizeof(*block_n_particles)); + } + tng_var_64_bit_input_swap(tng_data, block_n_particles); } - if(!block->block_contents) - { - free(contents); - } return(TNG_SUCCESS); } @@ -7398,89 +7356,114 @@ static tng_function_status tng_data_block_contents_read tng_gen_block_t block, const char hash_mode) { - int64_t n_values, codec_id, n_frames, first_frame_with_data; - int64_t stride_length, block_n_particles, num_first_particle; + int64_t start_pos, curr_file_pos, n_values, codec_id, n_frames, first_frame_with_data; + int64_t remaining_len, stride_length, block_n_particles, num_first_particle; double multiplier; char datatype, dependency, sparse_data; - int offset = 0; - tng_bool same_hash; + tng_function_status stat = TNG_SUCCESS; + char hash[TNG_MD5_HASH_LEN], *temp_data; + md5_state_t md5_state; if(tng_input_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); } - if(block->block_contents) - { - free(block->block_contents); - } - - block->block_contents = malloc(block->block_contents_size); - if(!block->block_contents) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - block->block_contents_size, __FILE__, __LINE__); - return(TNG_CRITICAL); - } + start_pos = ftello(tng_data->input_file); - /* Read the whole block into block_contents to be able to write it to - * disk even if it cannot be interpreted. */ - if(fread(block->block_contents, block->block_contents_size, 1, - tng_data->input_file) == 0) + if(hash_mode == TNG_USE_HASH) { - fprintf(stderr, "TNG library: Cannot read block. %s: %d\n", __FILE__, __LINE__); - return(TNG_CRITICAL); + md5_init(&md5_state); } /* FIXME: Does not check if the size of the contents matches the expected * size or if the contents can be read. */ - if(hash_mode == TNG_USE_HASH) - { - tng_md5_hash_match_verify(block, &same_hash); - if(same_hash != TNG_TRUE) - { - fprintf(stderr, "TNG library: '%s' data block contents corrupt. Hashes do not match. %s: %d\n", - block->name, __FILE__, __LINE__); - /* return(TNG_FAILURE); */ - } - } - - if(tng_data_block_meta_information_read(tng_data, block, - &offset, &datatype, + if(tng_data_block_meta_information_read(tng_data, + &datatype, &dependency, &sparse_data, &n_values, &codec_id, &first_frame_with_data, &stride_length, &n_frames, &num_first_particle, &block_n_particles, - &multiplier) == TNG_CRITICAL) + &multiplier, + hash_mode, + &md5_state) == TNG_CRITICAL) { fprintf(stderr, "TNG library: Cannot read data block (%s) meta information. %s: %d\n", block->name, __FILE__, __LINE__); return(TNG_CRITICAL); } + remaining_len = block->block_contents_size - (ftello(tng_data->input_file) - start_pos); + if (dependency & TNG_PARTICLE_DEPENDENT) { - return(tng_particle_data_read(tng_data, block, - &offset, datatype, + stat = tng_particle_data_read(tng_data, block, + remaining_len, + datatype, num_first_particle, block_n_particles, first_frame_with_data, stride_length, n_frames, n_values, - codec_id, multiplier)); + codec_id, multiplier, + hash_mode, + &md5_state); } else { - return(tng_data_read(tng_data, block, - &offset, datatype, + stat = tng_data_read(tng_data, block, + remaining_len, + datatype, first_frame_with_data, stride_length, n_frames, n_values, - codec_id, multiplier)); + codec_id, multiplier, + hash_mode, + &md5_state); + } + + if(hash_mode == TNG_USE_HASH) + { + /* If there is data left in the block that the current version of the library + * cannot interpret still read that to generate the MD5 hash. */ + curr_file_pos = ftello(tng_data->input_file); + if(curr_file_pos < start_pos + block->block_contents_size) + { + temp_data = malloc(start_pos + block->block_contents_size - curr_file_pos); + if(!temp_data) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + start_pos + block->block_contents_size - curr_file_pos, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + if(fread(temp_data, start_pos + block->block_contents_size - curr_file_pos, + 1, tng_data->input_file) == 0) + { + fprintf(stderr, "TNG library: Cannot read remaining part of block to generate MD5 sum. %s: %d\n", __FILE__, __LINE__); + free(temp_data); + return(TNG_CRITICAL); + } + md5_append(&md5_state, (md5_byte_t *)temp_data, + start_pos + block->block_contents_size - curr_file_pos); + free(temp_data); + } + md5_finish(&md5_state, (md5_byte_t *)hash); + if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", TNG_MD5_HASH_LEN) != 0) + { + if(strncmp(block->md5_hash, hash, TNG_MD5_HASH_LEN) != 0) + { + fprintf(stderr, "TNG library: Data block contents corrupt (%s). Hashes do not match. " + "%s: %d\n", block->name, __FILE__, __LINE__); + } + } } + + fseeko(tng_data->input_file, start_pos + block->block_contents_size, SEEK_SET); + + return(stat); } /* @@ -7521,13 +7504,13 @@ static tng_function_status tng_data_block_contents_read // } // // tng_block_init(&block); -// // output_file_pos = ftell(tng_data->output_file); +// // output_file_pos = ftello(tng_data->output_file); // // tng_data->input_file = tng_data->output_file; // // pos = tng_data->current_trajectory_frame_set_output_file_pos; // -// fseek(tng_data->output_file, pos, SEEK_SET); +// fseeko(tng_data->output_file, pos, SEEK_SET); // if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) // { // fprintf(stderr, "TNG library: Cannot read frame set header. %s: %d\n", @@ -7537,12 +7520,12 @@ static tng_function_status tng_data_block_contents_read // return(TNG_CRITICAL); // } // -// contents_start_pos = ftell(tng_data->output_file); +// contents_start_pos = ftello(tng_data->output_file); // -// fseek(tng_data->output_file, 0, SEEK_END); -// output_file_len = ftell(tng_data->output_file); +// fseeko(tng_data->output_file, 0, SEEK_END); +// output_file_len = ftello(tng_data->output_file); // pos = contents_start_pos + block->block_contents_size; -// fseek(tng_data->output_file, pos, +// fseeko(tng_data->output_file, pos, // SEEK_SET); // // while(pos < output_file_len) @@ -7556,7 +7539,7 @@ static tng_function_status tng_data_block_contents_read // return(TNG_CRITICAL); // } // pos += block->header_contents_size + block->block_contents_size; -// fseek(tng_data->output_file, pos, SEEK_SET); +// fseeko(tng_data->output_file, pos, SEEK_SET); // } // // return(TNG_SUCCESS); @@ -7577,7 +7560,7 @@ static tng_function_status tng_frame_set_finalize tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; FILE *temp = tng_data->input_file; - int64_t pos, contents_start_pos, output_file_len; + int64_t pos, curr_file_pos; frame_set = &tng_data->current_trajectory_frame_set; @@ -7586,6 +7569,8 @@ static tng_function_status tng_frame_set_finalize return(TNG_SUCCESS); } + frame_set->n_written_frames = frame_set->n_frames; + if(tng_output_file_init(tng_data) != TNG_SUCCESS) { fprintf(stderr, "TNG library: Cannot initialise destination file. %s: %d\n", @@ -7594,13 +7579,15 @@ static tng_function_status tng_frame_set_finalize } tng_block_init(&block); -/* output_file_pos = ftell(tng_data->output_file); */ +/* output_file_pos = ftello(tng_data->output_file); */ tng_data->input_file = tng_data->output_file; + curr_file_pos = ftello(tng_data->output_file); + pos = tng_data->current_trajectory_frame_set_output_file_pos; - fseek(tng_data->output_file, (long)pos, SEEK_SET); + fseeko(tng_data->output_file, pos, SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -7611,10 +7598,10 @@ static tng_function_status tng_frame_set_finalize return(TNG_CRITICAL); } - contents_start_pos = ftell(tng_data->output_file); +// contents_start_pos = ftello(tng_data->output_file); - fseek(tng_data->output_file, sizeof(frame_set->first_frame), SEEK_CUR); - if(fwrite(&frame_set->n_written_frames, sizeof(frame_set->n_frames), + fseeko(tng_data->output_file, sizeof(frame_set->first_frame), SEEK_CUR); + if(fwrite(&frame_set->n_frames, sizeof(frame_set->n_frames), 1, tng_data->output_file) != 1) { tng_data->input_file = temp; @@ -7629,30 +7616,32 @@ static tng_function_status tng_frame_set_finalize pos + block->header_contents_size); } - fseek(tng_data->output_file, 0, SEEK_END); - output_file_len = ftell(tng_data->output_file); - pos = contents_start_pos + block->block_contents_size; - fseek(tng_data->output_file, (long)pos, SEEK_SET); +// fseeko(tng_data->output_file, 0, SEEK_END); +// output_file_len = ftello(tng_data->output_file); +// pos = contents_start_pos + block->block_contents_size; +// fseeko(tng_data->output_file, pos, SEEK_SET); - while(pos < output_file_len) - { - if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) - { - fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", pos, - __FILE__, __LINE__); - tng_data->input_file = temp; - tng_block_destroy(&block); - return(TNG_CRITICAL); - } +// while(pos < output_file_len) +// { +// if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) +// { +// fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", pos, +// __FILE__, __LINE__); +// tng_data->input_file = temp; +// tng_block_destroy(&block); +// return(TNG_CRITICAL); +// } +// +// if(hash_mode == TNG_USE_HASH) +// { +// tng_md5_hash_update(tng_data, block, pos, +// pos + block->header_contents_size); +// } +// pos += block->header_contents_size + block->block_contents_size; +// fseeko(tng_data->output_file, pos, SEEK_SET); +// } - if(hash_mode == TNG_USE_HASH) - { - tng_md5_hash_update(tng_data, block, pos, - pos + block->header_contents_size); - } - pos += block->header_contents_size + block->block_contents_size; - fseek(tng_data->output_file, (long)pos, SEEK_SET); - } + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); tng_data->input_file = temp; tng_block_destroy(&block); @@ -7673,7 +7662,7 @@ static tng_function_status tng_frame_set_finalize // { // int len; // -// len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); +// len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); // // * If the currently stored string length is not enough to store the new // * string it is freed and reallocated. * @@ -7741,7 +7730,7 @@ tng_function_status tng_atom_name_set(tng_trajectory_t tng_data, TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer."); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -7795,7 +7784,7 @@ tng_function_status tng_atom_type_set(tng_trajectory_t tng_data, TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_type, "TNG library: new_type must not be a NULL pointer."); - len = tng_min_i((int)strlen(new_type) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_type) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -8081,7 +8070,7 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer."); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -8651,7 +8640,7 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_name_set TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer."); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -8883,7 +8872,7 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_name_set(tng_trajectory_t tng_ TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -9899,12 +9888,6 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t frame_set = &tng_data->current_trajectory_frame_set; - if(tng_data->input_file_path) - { - free(tng_data->input_file_path); - tng_data->input_file_path = 0; - } - if(tng_data->input_file) { if(tng_data->output_file == tng_data->input_file) @@ -9916,10 +9899,10 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t tng_data->input_file = 0; } - if(tng_data->output_file_path) + if(tng_data->input_file_path) { - free(tng_data->output_file_path); - tng_data->output_file_path = 0; + free(tng_data->input_file_path); + tng_data->input_file_path = 0; } if(tng_data->output_file) @@ -9930,6 +9913,12 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t tng_data->output_file = 0; } + if(tng_data->output_file_path) + { + free(tng_data->output_file_path); + tng_data->output_file_path = 0; + } + if(tng_data->first_program_name) { free(tng_data->first_program_name); @@ -10260,7 +10249,7 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init_from_src(tng_trajector if(!dest->input_file_path) { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - (int)strlen(src->input_file_path) + 1, __FILE__, __LINE__); + (unsigned int)strlen(src->input_file_path) + 1, __FILE__, __LINE__); return(TNG_CRITICAL); } strcpy(dest->input_file_path, src->input_file_path); @@ -10270,7 +10259,7 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init_from_src(tng_trajector if(!dest->output_file_path) { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - (int)strlen(src->output_file_path) + 1, __FILE__, __LINE__); + (unsigned int)strlen(src->output_file_path) + 1, __FILE__, __LINE__); return(TNG_CRITICAL); } strcpy(dest->output_file_path, src->output_file_path); @@ -10392,7 +10381,7 @@ tng_function_status DECLSPECDLLEXPORT tng_input_file_set(tng_trajectory_t tng_da fclose(tng_data->input_file); } - len = tng_min_i((int)strlen(file_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(file_name) + 1, TNG_MAX_STR_LEN); temp = realloc(tng_data->input_file_path, len); if(!temp) { @@ -10445,7 +10434,7 @@ tng_function_status DECLSPECDLLEXPORT tng_output_file_set(tng_trajectory_t tng_d fclose(tng_data->output_file); } - len = tng_min_i((int)strlen(file_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(file_name) + 1, TNG_MAX_STR_LEN); temp = realloc(tng_data->output_file_path, len); if(!temp) { @@ -10483,7 +10472,7 @@ tng_function_status DECLSPECDLLEXPORT tng_output_append_file_set fclose(tng_data->output_file); } - len = tng_min_i((int)strlen(file_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(file_name) + 1, TNG_MAX_STR_LEN); temp = realloc(tng_data->output_file_path, len); if(!temp) { @@ -10594,7 +10583,7 @@ tng_function_status DECLSPECDLLEXPORT tng_output_file_endianness_set /* Tne endianness cannot be changed if the data has already been written * to the output file. */ - if(ftell(tng_data->output_file) > 0) + if(ftello(tng_data->output_file) > 0) { return(TNG_FAILURE); } @@ -10674,7 +10663,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_program_name_set(tng_trajectory_ TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); if(tng_data->first_program_name && strlen(tng_data->first_program_name) < len) { @@ -10723,7 +10712,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_program_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); if(tng_data->last_program_name && strlen(tng_data->last_program_name) < len) { @@ -10772,7 +10761,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_user_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -10823,7 +10812,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_user_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -10874,7 +10863,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -10925,7 +10914,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -10977,7 +10966,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_signature_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(signature, "TNG library: signature must not be a NULL pointer"); - len = tng_min_i((int)strlen(signature) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(signature) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -11029,7 +11018,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_signature_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(signature, "TNG library: signature must not be a NULL pointer"); - len = tng_min_i((int)strlen(signature) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(signature) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -11081,7 +11070,7 @@ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_set TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(new_name, "TNG library: new_name must not be a NULL pointer"); - len = tng_min_i((int)strlen(new_name) + 1, TNG_MAX_STR_LEN); + len = tng_min_i((unsigned int)strlen(new_name) + 1, TNG_MAX_STR_LEN); /* If the currently stored string length is not enough to store the new * string it is freed and reallocated. */ @@ -11218,14 +11207,13 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frames_get { tng_gen_block_t block; tng_function_status stat; - long file_pos; - int64_t last_file_pos, first_frame, n_frames; + int64_t file_pos, last_file_pos, first_frame, n_frames; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(tng_data->input_file, "TNG library: An input file must be open to find the next frame set"); TNG_ASSERT(n, "TNG library: n must not be a NULL pointer"); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); last_file_pos = tng_data->last_trajectory_frame_set_input_file_pos; if(last_file_pos <= 0) @@ -11234,9 +11222,9 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frames_get } tng_block_init(&block); - fseek(tng_data->input_file, - (long)last_file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + last_file_pos, + SEEK_SET); /* Read block headers first to see that a frame set block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11254,13 +11242,17 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frames_get __FILE__, __LINE__); return(TNG_CRITICAL); } + tng_var_64_bit_input_swap(tng_data, &first_frame); + if(fread(&n_frames, sizeof(int64_t), 1, tng_data->input_file) == 0) { fprintf(stderr, "TNG library: Cannot read n frames of frame set. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } - fseek(tng_data->input_file, file_pos, SEEK_SET); + tng_var_64_bit_input_swap(tng_data, &n_frames); + + fseeko(tng_data->input_file, file_pos, SEEK_SET); *n = first_frame + n_frames; @@ -11523,7 +11515,7 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get int64_t *n) { int64_t long_stride_length, medium_stride_length; - long file_pos, orig_frame_set_file_pos; + int64_t file_pos, orig_frame_set_file_pos; tng_trajectory_frame_set_t frame_set; struct tng_trajectory_frame_set orig_frame_set; tng_gen_block_t block; @@ -11538,7 +11530,7 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get frame_set = &tng_data->current_trajectory_frame_set; orig_frame_set_file_pos = tng_data->current_trajectory_frame_set_input_file_pos; - file_pos = (long)tng_data->first_trajectory_frame_set_input_file_pos; + file_pos = tng_data->first_trajectory_frame_set_input_file_pos; if(file_pos < 0) { @@ -11547,15 +11539,15 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get } tng_block_init(&block); - fseek(tng_data->input_file, - file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); tng_data->current_trajectory_frame_set_input_file_pos = file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", file_pos, + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); @@ -11575,18 +11567,18 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get /* Take long steps forward until a long step forward would be too long or * the last frame set is found */ - file_pos = (long)frame_set->long_stride_next_frame_set_file_pos; + file_pos = frame_set->long_stride_next_frame_set_file_pos; while(file_pos > 0) { if(file_pos > 0) { cnt += long_stride_length; - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, file_pos, SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); @@ -11599,25 +11591,25 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get return(TNG_CRITICAL); } } - file_pos = (long)frame_set->long_stride_next_frame_set_file_pos; + file_pos = frame_set->long_stride_next_frame_set_file_pos; } /* Take medium steps forward until a medium step forward would be too long * or the last frame set is found */ - file_pos = (long)frame_set->medium_stride_next_frame_set_file_pos; + file_pos = frame_set->medium_stride_next_frame_set_file_pos; while(file_pos > 0) { if(file_pos > 0) { cnt += medium_stride_length; - fseek(tng_data->input_file, - file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); @@ -11630,24 +11622,24 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get return(TNG_CRITICAL); } } - file_pos = (long)frame_set->medium_stride_next_frame_set_file_pos; + file_pos = frame_set->medium_stride_next_frame_set_file_pos; } /* Take one step forward until the last frame set is found */ - file_pos = (long)frame_set->next_frame_set_file_pos; + file_pos = frame_set->next_frame_set_file_pos; while(file_pos > 0) { if(file_pos > 0) { ++cnt; - fseek(tng_data->input_file, - file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); @@ -11660,7 +11652,7 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get return(TNG_CRITICAL); } } - file_pos = (long)frame_set->next_frame_set_file_pos; + file_pos = frame_set->next_frame_set_file_pos; } tng_block_destroy(&block); @@ -11668,10 +11660,14 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get *n = tng_data->n_trajectory_frame_sets = cnt; *frame_set = orig_frame_set; + /* The mapping block in the original frame set has been freed when reading + * other frame sets. */ + frame_set->mappings = 0; + frame_set->n_mapping_blocks = 0; - fseek(tng_data->input_file, - (long)tng_data->first_trajectory_frame_set_input_file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + tng_data->first_trajectory_frame_set_input_file_pos, + SEEK_SET); tng_data->current_trajectory_frame_set_input_file_pos = orig_frame_set_file_pos; @@ -11738,10 +11734,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find } tng_block_init(&block); - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); - tng_data->current_trajectory_frame_set_input_file_pos = (long)file_pos; + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); + tng_data->current_trajectory_frame_set_input_file_pos = file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11775,7 +11771,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { curr_nr += long_stride_length; - fseek(tng_data->input_file, (long)file_pos, SEEK_SET); + fseeko(tng_data->input_file, file_pos, SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11808,9 +11804,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { curr_nr += medium_stride_length; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11843,9 +11839,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { ++curr_nr; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11878,9 +11874,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { curr_nr -= long_stride_length; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11913,9 +11909,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { curr_nr -= medium_stride_length; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11947,9 +11943,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { --curr_nr; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -11982,9 +11978,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find if(file_pos > 0) { ++curr_nr; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12034,10 +12030,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find if(tng_data->current_trajectory_frame_set_input_file_pos < 0) { file_pos = tng_data->first_trajectory_frame_set_input_file_pos; - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); - tng_data->current_trajectory_frame_set_input_file_pos = (long)file_pos; + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); + tng_data->current_trajectory_frame_set_input_file_pos = file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12117,10 +12113,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); - tng_data->current_trajectory_frame_set_input_file_pos = (long)file_pos; + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); + tng_data->current_trajectory_frame_set_input_file_pos = file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12159,7 +12155,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->long_stride_next_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, (long)file_pos, SEEK_SET); + fseeko(tng_data->input_file, file_pos, SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12194,9 +12190,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->medium_stride_next_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12229,9 +12225,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->next_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12266,9 +12262,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->long_stride_prev_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12303,9 +12299,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->medium_stride_prev_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12338,9 +12334,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->prev_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12374,9 +12370,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find file_pos = frame_set->next_frame_set_file_pos; if(file_pos > 0) { - fseek(tng_data->input_file, - (long)file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -12543,14 +12539,14 @@ static tng_function_status tng_file_headers_len_get *len = 0; - orig_pos = ftell(tng_data->input_file); + orig_pos = ftello(tng_data->input_file); if(!tng_data->input_file_len) { - fseek(tng_data->input_file, 0, SEEK_END); - tng_data->input_file_len = ftell(tng_data->input_file); + fseeko(tng_data->input_file, 0, SEEK_END); + tng_data->input_file_len = ftello(tng_data->input_file); } - fseek(tng_data->input_file, 0, SEEK_SET); + fseeko(tng_data->input_file, 0, SEEK_SET); tng_block_init(&block); /* Read through the headers of non-trajectory blocks (they come before the @@ -12561,10 +12557,10 @@ static tng_function_status tng_file_headers_len_get block->id != TNG_TRAJECTORY_FRAME_SET) { *len += block->header_contents_size + block->block_contents_size; - fseek(tng_data->input_file, block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); } - fseek(tng_data->input_file, orig_pos, SEEK_SET); + fseeko(tng_data->input_file, orig_pos, SEEK_SET); tng_block_destroy(&block); @@ -12589,10 +12585,10 @@ tng_function_status DECLSPECDLLEXPORT tng_file_headers_read if(!tng_data->input_file_len) { - fseek(tng_data->input_file, 0, SEEK_END); - tng_data->input_file_len = ftell(tng_data->input_file); + fseeko(tng_data->input_file, 0, SEEK_END); + tng_data->input_file_len = ftello(tng_data->input_file); } - fseek(tng_data->input_file, 0, SEEK_SET); + fseeko(tng_data->input_file, 0, SEEK_SET); tng_block_init(&block); /* Non trajectory blocks (they come before the trajectory @@ -12603,13 +12599,13 @@ tng_function_status DECLSPECDLLEXPORT tng_file_headers_read block->id != TNG_TRAJECTORY_FRAME_SET) { tng_block_read_next(tng_data, block, hash_mode); - prev_pos = ftell(tng_data->input_file); + prev_pos = ftello(tng_data->input_file); } /* Go back if a trajectory block was encountered */ if(block->id == TNG_TRAJECTORY_FRAME_SET) { - fseek(tng_data->input_file, prev_pos, SEEK_SET); + fseeko(tng_data->input_file, prev_pos, SEEK_SET); } tng_block_destroy(&block); @@ -12690,7 +12686,7 @@ tng_function_status DECLSPECDLLEXPORT tng_file_headers_write if(tot_len > orig_len) { - tng_migrate_data_in_file(tng_data, orig_len+1, tot_len - orig_len); + tng_migrate_data_in_file(tng_data, orig_len+1, tot_len - orig_len, hash_mode); } tng_data->current_trajectory_frame_set_output_file_pos = -1; @@ -12763,7 +12759,7 @@ tng_function_status DECLSPECDLLEXPORT tng_block_read_next(tng_trajectory_t tng_d else { /* Skip to the next block */ - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); return(TNG_FAILURE); } } @@ -12773,7 +12769,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read (tng_trajectory_t tng_data, const char hash_mode) { - long file_pos; + int64_t file_pos; tng_gen_block_t block; tng_function_status stat; @@ -12784,15 +12780,15 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read return(TNG_CRITICAL); } - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); tng_block_init(&block); if(!tng_data->input_file_len) { - fseek(tng_data->input_file, 0, SEEK_END); - tng_data->input_file_len = ftell(tng_data->input_file); - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, 0, SEEK_END); + tng_data->input_file_len = ftello(tng_data->input_file); + fseeko(tng_data->input_file, file_pos, SEEK_SET); } /* Read block headers first to see what block is found. */ @@ -12800,7 +12796,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET || block->id == -1) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); @@ -12812,7 +12808,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read hash_mode) == TNG_SUCCESS) { tng_data->n_trajectory_frame_sets++; - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -12824,7 +12820,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read hash_mode); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -12833,7 +12829,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read } if(stat == TNG_CRITICAL) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(stat); @@ -12841,7 +12837,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read if(block->id == TNG_TRAJECTORY_FRAME_SET) { - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, file_pos, SEEK_SET); } } @@ -12856,7 +12852,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ const char hash_mode, const int64_t block_id) { - long file_pos; + int64_t file_pos; tng_gen_block_t block; tng_function_status stat; int found_flag = 1; @@ -12868,19 +12864,19 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ return(TNG_CRITICAL); } - file_pos = (long)tng_data->current_trajectory_frame_set_input_file_pos; + file_pos = tng_data->current_trajectory_frame_set_input_file_pos; if(file_pos < 0) { /* No current frame set. This means that the first frame set must be * read */ found_flag = 0; - file_pos = (long)tng_data->first_trajectory_frame_set_input_file_pos; + file_pos = tng_data->first_trajectory_frame_set_input_file_pos; } if(file_pos > 0) { - fseek(tng_data->input_file, + fseeko(tng_data->input_file, file_pos, SEEK_SET); } @@ -12893,16 +12889,16 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ if(!tng_data->input_file_len) { - fseek(tng_data->input_file, 0, SEEK_END); - tng_data->input_file_len = ftell(tng_data->input_file); - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, 0, SEEK_END); + tng_data->input_file_len = ftello(tng_data->input_file); + fseeko(tng_data->input_file, file_pos, SEEK_SET); } /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); @@ -12910,9 +12906,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ /* If the current frame set had already been read skip its block contents */ if(found_flag) { - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); } - /* Otherwiese read the frame set block */ + /* Otherwise read the frame set block */ else { stat = tng_block_read_next(tng_data, block, @@ -12924,7 +12920,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ return(stat); } } - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); found_flag = 0; @@ -12932,9 +12928,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ * until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && - stat != TNG_CRITICAL && - block->id != TNG_TRAJECTORY_FRAME_SET && - block->id != -1) + stat != TNG_CRITICAL && + block->id != TNG_TRAJECTORY_FRAME_SET && + block->id != -1) { if(block->id == block_id) { @@ -12942,7 +12938,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ hash_mode); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); found_flag = 1; if(file_pos < tng_data->input_file_len) { @@ -12952,8 +12948,8 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ } else { - file_pos += (long)(block->block_contents_size + block->header_contents_size); - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + file_pos += block->block_contents_size + block->header_contents_size; + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -12962,7 +12958,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ } if(stat == TNG_CRITICAL) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(stat); @@ -12970,7 +12966,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_current_only_data_from_ if(block->id == TNG_TRAJECTORY_FRAME_SET) { - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, file_pos, SEEK_SET); } tng_block_destroy(&block); @@ -12989,7 +12985,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next (tng_trajectory_t tng_data, const char hash_mode) { - long file_pos; + int64_t file_pos; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); @@ -12998,18 +12994,18 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next return(TNG_CRITICAL); } - file_pos = (long)tng_data->current_trajectory_frame_set.next_frame_set_file_pos; + file_pos = tng_data->current_trajectory_frame_set.next_frame_set_file_pos; if(file_pos < 0 && tng_data->current_trajectory_frame_set_input_file_pos <= 0) { - file_pos = (long)tng_data->first_trajectory_frame_set_input_file_pos; + file_pos = tng_data->first_trajectory_frame_set_input_file_pos; } if(file_pos > 0) { - fseek(tng_data->input_file, - file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); } else { @@ -13024,7 +13020,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next_only_data_from_blo const char hash_mode, const int64_t block_id) { - long file_pos; + int64_t file_pos; tng_gen_block_t block; tng_function_status stat; @@ -13035,18 +13031,18 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next_only_data_from_blo return(TNG_CRITICAL); } - file_pos = (long)tng_data->current_trajectory_frame_set.next_frame_set_file_pos; + file_pos = tng_data->current_trajectory_frame_set.next_frame_set_file_pos; if(file_pos < 0 && tng_data->current_trajectory_frame_set_input_file_pos <= 0) { - file_pos = (long)tng_data->first_trajectory_frame_set_input_file_pos; + file_pos = tng_data->first_trajectory_frame_set_input_file_pos; } if(file_pos > 0) { - fseek(tng_data->input_file, - file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + file_pos, + SEEK_SET); } else { @@ -13057,17 +13053,17 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next_only_data_from_blo if(!tng_data->input_file_len) { - fseek(tng_data->input_file, 0, SEEK_END); - tng_data->input_file_len = ftell(tng_data->input_file); - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, 0, SEEK_END); + tng_data->input_file_len = ftello(tng_data->input_file); + fseeko(tng_data->input_file, file_pos, SEEK_SET); } /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", - file_pos, __FILE__, __LINE__); + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", + file_pos, __FILE__, __LINE__); tng_block_destroy(&block); return(TNG_CRITICAL); } @@ -13103,7 +13099,7 @@ tng_function_status tng_frame_set_write(tng_trajectory_t tng_data, } tng_data->current_trajectory_frame_set_output_file_pos = - ftell(tng_data->output_file); + ftello(tng_data->output_file); tng_data->last_trajectory_frame_set_output_file_pos = tng_data->current_trajectory_frame_set_output_file_pos; @@ -13206,7 +13202,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; FILE *temp = tng_data->input_file; - int64_t curr_pos; + int64_t curr_file_pos; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(first_frame >= 0, "TNG library: first_frame must be >= 0."); @@ -13214,9 +13210,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new frame_set = &tng_data->current_trajectory_frame_set; - curr_pos = ftell(tng_data->output_file); + curr_file_pos = ftello(tng_data->output_file); - if(curr_pos <= 10) + if(curr_file_pos <= 10) { tng_file_headers_write(tng_data, TNG_USE_HASH); } @@ -13232,7 +13228,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new } tng_data->current_trajectory_frame_set_output_file_pos = - ftell(tng_data->output_file); + ftello(tng_data->output_file); tng_data->n_trajectory_frame_sets++; @@ -13252,10 +13248,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new tng_block_init(&block); tng_data->input_file = tng_data->output_file; - curr_pos = ftell(tng_data->output_file); - fseek(tng_data->output_file, - (long)frame_set->medium_stride_prev_frame_set_file_pos, - SEEK_SET); + curr_file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, + frame_set->medium_stride_prev_frame_set_file_pos, + SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -13268,7 +13264,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new /* Read the next frame set from the previous frame set and one * medium stride step back */ - fseek(tng_data->output_file, (long)block->block_contents_size - (6 * + fseeko(tng_data->output_file, block->block_contents_size - (6 * sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); if(fread(&frame_set->medium_stride_prev_frame_set_file_pos, sizeof(frame_set->medium_stride_prev_frame_set_file_pos), @@ -13309,9 +13305,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new tng_block_init(&block); tng_data->input_file = tng_data->output_file; - fseek(tng_data->output_file, - (long)frame_set->long_stride_prev_frame_set_file_pos, - SEEK_SET); + fseeko(tng_data->output_file, + frame_set->long_stride_prev_frame_set_file_pos, + SEEK_SET); if(tng_block_header_read(tng_data, block) != TNG_SUCCESS) { @@ -13324,7 +13320,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new /* Read the next frame set from the previous frame set and one * long stride step back */ - fseek(tng_data->output_file, (long)block->block_contents_size - (6 * + fseeko(tng_data->output_file, block->block_contents_size - (6 * sizeof(int64_t) + 2 * sizeof(double)), SEEK_CUR); tng_block_destroy(&block); @@ -13353,7 +13349,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new } tng_data->input_file = temp; - fseek(tng_data->output_file, (long)curr_pos, SEEK_SET); + fseeko(tng_data->output_file, curr_file_pos, SEEK_SET); } } @@ -13423,7 +13419,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_frame_nr_of_next_frame_set_get (const tng_trajectory_t tng_data, int64_t *frame) { - long file_pos, next_frame_set_file_pos; + int64_t file_pos, next_frame_set_file_pos; tng_gen_block_t block; tng_function_status stat; @@ -13433,16 +13429,16 @@ tng_function_status DECLSPECDLLEXPORT tng_first_frame_nr_of_next_frame_set_get TNG_ASSERT(tng_data->input_file, "TNG library: An input file must be open to find the next frame set"); TNG_ASSERT(frame, "TNG library: frame must not be a NULL pointer"); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(tng_data->current_trajectory_frame_set_input_file_pos <= 0) { - next_frame_set_file_pos = (long)tng_data->first_trajectory_frame_set_input_file_pos; + next_frame_set_file_pos = tng_data->first_trajectory_frame_set_input_file_pos; } else { frame_set = &tng_data->current_trajectory_frame_set; - next_frame_set_file_pos = (long)frame_set->next_frame_set_file_pos; + next_frame_set_file_pos = frame_set->next_frame_set_file_pos; } if(next_frame_set_file_pos <= 0) @@ -13450,13 +13446,13 @@ tng_function_status DECLSPECDLLEXPORT tng_first_frame_nr_of_next_frame_set_get return(TNG_FAILURE); } - fseek(tng_data->input_file, (long)next_frame_set_file_pos, SEEK_SET); + fseeko(tng_data->input_file, next_frame_set_file_pos, SEEK_SET); /* Read block headers first to see that a frame set block is found. */ tng_block_init(&block); stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); return(TNG_CRITICAL); } @@ -13472,7 +13468,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_frame_nr_of_next_frame_set_get __FILE__, __LINE__); return(TNG_CRITICAL); } - fseek(tng_data->input_file, file_pos, SEEK_SET); + fseeko(tng_data->input_file, file_pos, SEEK_SET); return(TNG_SUCCESS); } @@ -13531,7 +13527,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_block_add if(!data->block_name) { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - (int)strlen(block_name)+1, __FILE__, __LINE__); + (unsigned int)strlen(block_name)+1, __FILE__, __LINE__); return(TNG_CRITICAL); } strncpy(data->block_name, block_name, strlen(block_name) + 1); @@ -13593,7 +13589,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_block_add first_dim_values = data->strings[i]; for(j = 0; j < n_values_per_frame; j++) { - len = tng_min_i((int)strlen(new_data_c) + 1, + len = tng_min_i((unsigned int)strlen(new_data_c) + 1, TNG_MAX_STR_LEN); if(first_dim_values[j]) { @@ -13684,7 +13680,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add if(!data->block_name) { fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", - (int)strlen(block_name)+1, __FILE__, __LINE__); + (unsigned int)strlen(block_name)+1, __FILE__, __LINE__); return(TNG_CRITICAL); } strncpy(data->block_name, block_name, strlen(block_name) + 1); @@ -13748,7 +13744,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add second_dim_values = first_dim_values[j]; for(k = 0; k < n_values_per_frame; k++) { - len = tng_min_i((int)strlen(new_data_c) + 1, + len = tng_min_i((unsigned int)strlen(new_data_c) + 1, TNG_MAX_STR_LEN); if(second_dim_values[k]) { @@ -14052,8 +14048,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write int64_t header_pos, file_pos; int64_t output_file_len, n_values_per_frame, size, contents_size; int64_t header_size, temp_first, temp_last; - int64_t i, last_frame; - long temp_current; + int64_t i, last_frame, temp_current; tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; FILE *temp = tng_data->input_file; @@ -14108,10 +14103,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write tng_frame_set_new(tng_data, last_frame+1, tng_data->frame_set_n_frames); - file_pos = ftell(tng_data->output_file); - fseek(tng_data->output_file, 0, SEEK_END); - output_file_len = ftell(tng_data->output_file); - fseek(tng_data->output_file, (long)file_pos, SEEK_SET); + file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, 0, SEEK_END); + output_file_len = ftello(tng_data->output_file); + fseeko(tng_data->output_file, file_pos, SEEK_SET); /* Read mapping blocks from the last frame set */ tng_block_init(&block); @@ -14129,10 +14124,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write } else { - fseek(tng_data->output_file, (long)block->block_contents_size, + fseeko(tng_data->output_file, block->block_contents_size, SEEK_CUR); } - file_pos = ftell(tng_data->output_file); + file_pos = ftello(tng_data->output_file); if(file_pos < output_file_len) { stat = tng_block_header_read(tng_data, block); @@ -14161,9 +14156,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write file_pos = tng_data->current_trajectory_frame_set_output_file_pos; - fseek(tng_data->output_file, 0, SEEK_END); - output_file_len = ftell(tng_data->output_file); - fseek(tng_data->output_file, (long)file_pos, SEEK_SET); + fseeko(tng_data->output_file, 0, SEEK_END); + output_file_len = ftello(tng_data->output_file); + fseeko(tng_data->output_file, file_pos, SEEK_SET); /* Read past the frame set block first */ stat = tng_block_header_read(tng_data, block); @@ -14179,7 +14174,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write tng_data->current_trajectory_frame_set_input_file_pos = temp_current; return(stat); } - fseek(tng_data->output_file, (long)block->block_contents_size, + fseeko(tng_data->output_file, block->block_contents_size, SEEK_CUR); /* Read all block headers until next frame set block or @@ -14191,8 +14186,8 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write block->id != TNG_TRAJECTORY_FRAME_SET && block->id != -1) { - fseek(tng_data->output_file, (long)block->block_contents_size, SEEK_CUR); - file_pos = ftell(tng_data->output_file); + fseeko(tng_data->output_file, block->block_contents_size, SEEK_CUR); + file_pos = ftello(tng_data->output_file); if(file_pos < output_file_len) { stat = tng_block_header_read(tng_data, block); @@ -14213,7 +14208,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write contents_size = block->block_contents_size; header_size = block->header_contents_size; - header_pos = ftell(tng_data->output_file) - header_size; + header_pos = ftello(tng_data->output_file) - header_size; frame_set = &tng_data->current_trajectory_frame_set; if(fread(&datatype, sizeof(datatype), 1, tng_data->input_file) == 0) @@ -14395,7 +14390,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write return(TNG_FAILURE); } - fseek(tng_data->output_file, (long)file_pos, SEEK_CUR); + fseeko(tng_data->output_file, file_pos, SEEK_CUR); /* If the endianness is not big endian the data needs to be swapped */ if((data.datatype == TNG_INT_DATA || @@ -14478,8 +14473,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write int64_t output_file_len, n_values_per_frame, size, contents_size; int64_t header_size, temp_first, temp_last; int64_t mapping_block_end_pos, num_first_particle, block_n_particles; - int64_t i, last_frame; - long temp_current; + int64_t i, last_frame, temp_current; tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; FILE *temp = tng_data->input_file; @@ -14538,10 +14532,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write last_frame+1, tng_data->frame_set_n_frames); - file_pos = ftell(tng_data->output_file); - fseek(tng_data->output_file, 0, SEEK_END); - output_file_len = ftell(tng_data->output_file); - fseek(tng_data->output_file, (long)file_pos, SEEK_SET); + file_pos = ftello(tng_data->output_file); + fseeko(tng_data->output_file, 0, SEEK_END); + output_file_len = ftello(tng_data->output_file); + fseeko(tng_data->output_file, file_pos, SEEK_SET); /* Read mapping blocks from the last frame set */ tng_block_init(&block); @@ -14559,10 +14553,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write } else { - fseek(tng_data->output_file, (long)block->block_contents_size, + fseeko(tng_data->output_file, block->block_contents_size, SEEK_CUR); } - file_pos = ftell(tng_data->output_file); + file_pos = ftello(tng_data->output_file); if(file_pos < output_file_len) { stat = tng_block_header_read(tng_data, block); @@ -14592,9 +14586,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write file_pos = tng_data->current_trajectory_frame_set_output_file_pos; - fseek(tng_data->output_file, 0, SEEK_END); - output_file_len = ftell(tng_data->output_file); - fseek(tng_data->output_file, (long)file_pos, SEEK_SET); + fseeko(tng_data->output_file, 0, SEEK_END); + output_file_len = ftello(tng_data->output_file); + fseeko(tng_data->output_file, file_pos, SEEK_SET); /* Read past the frame set block first */ stat = tng_block_header_read(tng_data, block); @@ -14610,7 +14604,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write tng_data->current_trajectory_frame_set_input_file_pos = temp_current; return(stat); } - fseek(tng_data->output_file, (long)block->block_contents_size, + fseeko(tng_data->output_file, block->block_contents_size, SEEK_CUR); if(tng_data->var_num_atoms_flag) @@ -14638,10 +14632,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write } else { - fseek(tng_data->output_file, (long)block->block_contents_size, - SEEK_CUR); + fseeko(tng_data->output_file, block->block_contents_size, + SEEK_CUR); } - file_pos = ftell(tng_data->output_file); + file_pos = ftello(tng_data->output_file); if(block->id == TNG_PARTICLE_MAPPING) { mapping = &frame_set->mappings[frame_set->n_mapping_blocks - 1]; @@ -14681,7 +14675,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write tng_data->current_trajectory_frame_set_input_file_pos = temp_current; return(TNG_FAILURE); } - fseek(tng_data->output_file, (long)mapping_block_end_pos, SEEK_SET); + fseeko(tng_data->output_file, mapping_block_end_pos, SEEK_SET); } /* Read all block headers until next frame set block or @@ -14694,8 +14688,8 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write block->id != TNG_TRAJECTORY_FRAME_SET && block->id != -1) { - fseek(tng_data->output_file, (long)block->block_contents_size, SEEK_CUR); - file_pos = ftell(tng_data->output_file); + fseeko(tng_data->output_file, block->block_contents_size, SEEK_CUR); + file_pos = ftello(tng_data->output_file); if(file_pos < output_file_len) { stat = tng_block_header_read(tng_data, block); @@ -14717,7 +14711,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write contents_size = block->block_contents_size; header_size = block->header_contents_size; - header_pos = ftell(tng_data->output_file) - header_size; + header_pos = ftello(tng_data->output_file) - header_size; frame_set = &tng_data->current_trajectory_frame_set; if(fread(&datatype, sizeof(datatype), 1, tng_data->input_file) == 0) @@ -14941,7 +14935,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write return(TNG_FAILURE); } - fseek(tng_data->output_file, (long)file_pos, SEEK_CUR); + fseeko(tng_data->output_file, file_pos, SEEK_CUR); /* If the endianness is not big endian the data needs to be swapped */ if((data.datatype == TNG_INT_DATA || @@ -15249,7 +15243,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get if(tng_data_find(tng_data, block_id, &data) != TNG_SUCCESS) { tng_block_init(&block); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -15262,7 +15256,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get TNG_USE_HASH); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -15368,7 +15362,7 @@ tng_function_status tng_data_vector_get(tng_trajectory_t tng_data, int64_t *n_values_per_frame, char *type) { - int64_t file_pos, data_size, n_frames_div, block_index; + int64_t file_pos, full_data_len, n_frames_div, block_index; int i, size; tng_non_particle_data_t data; tng_trajectory_frame_set_t frame_set; @@ -15390,7 +15384,7 @@ tng_function_status tng_data_vector_get(tng_trajectory_t tng_data, if(tng_data_find(tng_data, block_id, &data) != TNG_SUCCESS) { tng_block_init(&block); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -15403,7 +15397,7 @@ tng_function_status tng_data_vector_get(tng_trajectory_t tng_data, TNG_USE_HASH); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -15456,14 +15450,14 @@ tng_function_status tng_data_vector_get(tng_trajectory_t tng_data, n_frames_div = (*n_frames % *stride_length) ? *n_frames / *stride_length + 1: *n_frames / *stride_length; - data_size = n_frames_div * size * + full_data_len = n_frames_div * size * *n_values_per_frame; - temp = realloc(*values, data_size); + temp = realloc(*values, full_data_len); if(!temp) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - data_size, __FILE__, __LINE__); + full_data_len, __FILE__, __LINE__); free(*values); *values = 0; return(TNG_CRITICAL); @@ -15471,7 +15465,7 @@ tng_function_status tng_data_vector_get(tng_trajectory_t tng_data, *values = temp; - memcpy(*values, data->values, data_size); + memcpy(*values, data->values, full_data_len); data->last_retrieved_frame = frame_set->first_frame + data->n_frames - 1; @@ -15519,7 +15513,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get frame_set->n_data_blocks <= 0) { tng_block_init(&block); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -15531,7 +15525,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get hash_mode); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -15693,7 +15687,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_vector_interval_get char *type) { int64_t n_frames, tot_n_frames, n_frames_div, n_frames_div_2, first_frame; - int64_t file_pos, current_frame_pos, data_size, frame_size; + int64_t file_pos, current_frame_pos, full_data_len, frame_size; int64_t last_frame_pos; int size; tng_trajectory_frame_set_t frame_set; @@ -15726,9 +15720,9 @@ tng_function_status DECLSPECDLLEXPORT tng_data_vector_interval_get tng_block_init(&block); if(stat != TNG_SUCCESS) { - fseek(tng_data->input_file, - (long)tng_data->current_trajectory_frame_set_input_file_pos, - SEEK_SET); + fseeko(tng_data->input_file, + tng_data->current_trajectory_frame_set_input_file_pos, + SEEK_SET); stat = tng_block_header_read(tng_data, block); if(stat != TNG_SUCCESS) { @@ -15737,9 +15731,9 @@ tng_function_status DECLSPECDLLEXPORT tng_data_vector_interval_get return(stat); } - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); } - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -15753,7 +15747,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_vector_interval_get hash_mode); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -15763,7 +15757,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_vector_interval_get else { file_pos += block->block_contents_size + block->header_contents_size; - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -15825,16 +15819,16 @@ tng_function_status DECLSPECDLLEXPORT tng_data_vector_interval_get n_frames_div = (tot_n_frames % *stride_length) ? tot_n_frames / *stride_length + 1: tot_n_frames / *stride_length; - data_size = n_frames_div * size * (*n_values_per_frame); + full_data_len = n_frames_div * size * (*n_values_per_frame); -/* fprintf(stderr, "TNG library: size: %d, n_frames_div: %"PRId64", data_size: %"PRId64"\n", - size, n_frames_div, data_size); +/* fprintf(stderr, "TNG library: size: %d, n_frames_div: %"PRId64", full_data_len: %"PRId64"\n", + size, n_frames_div, full_data_len); */ - temp = realloc(*values, data_size); + temp = realloc(*values, full_data_len); if(!temp) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - data_size, __FILE__, __LINE__); + full_data_len, __FILE__, __LINE__); free(*values); *values = 0; return(TNG_CRITICAL); @@ -15963,7 +15957,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get } tng_block_init(&block); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -15976,7 +15970,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get TNG_USE_HASH); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -16135,7 +16129,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_get int64_t *n_values_per_frame, char *type) { - int64_t i, j, mapping, file_pos, i_step, data_size, n_frames_div; + int64_t i, j, mapping, file_pos, i_step, full_data_len, n_frames_div; int64_t block_index; int size; tng_particle_data_t data; @@ -16159,7 +16153,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_get if(tng_particle_data_find(tng_data, block_id, &data) != TNG_SUCCESS) { tng_block_init(&block); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -16172,7 +16166,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_get TNG_USE_HASH); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -16246,14 +16240,14 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_get *n_frames / *stride_length + 1: *n_frames / *stride_length; - data_size = n_frames_div * size * (*n_particles) * + full_data_len = n_frames_div * size * (*n_particles) * (*n_values_per_frame); - temp = realloc(*values, data_size); + temp = realloc(*values, full_data_len); if(!temp) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - data_size, __FILE__, __LINE__); + full_data_len, __FILE__, __LINE__); free(*values); *values = 0; return(TNG_CRITICAL); @@ -16263,7 +16257,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_get if(frame_set->n_mapping_blocks <= 0) { - memcpy(*values, data->values, data_size); + memcpy(*values, data->values, full_data_len); } else { @@ -16330,7 +16324,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get frame_set->n_particle_data_blocks <= 0) { tng_block_init(&block); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read all blocks until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -16342,7 +16336,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get hash_mode); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -16541,7 +16535,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_interval_get char *type) { int64_t n_frames, tot_n_frames, n_frames_div, n_frames_div_2, first_frame; - int64_t file_pos, current_frame_pos, last_frame_pos, data_size, frame_size; + int64_t file_pos, current_frame_pos, last_frame_pos, full_data_len, frame_size; int size; tng_trajectory_frame_set_t frame_set; tng_particle_data_t p_data; @@ -16574,8 +16568,8 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_interval_get tng_block_init(&block); if(stat != TNG_SUCCESS) { - fseek(tng_data->input_file, - (long)tng_data->current_trajectory_frame_set_input_file_pos, + fseeko(tng_data->input_file, + tng_data->current_trajectory_frame_set_input_file_pos, SEEK_SET); stat = tng_block_header_read(tng_data, block); if(stat != TNG_SUCCESS) @@ -16585,9 +16579,9 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_interval_get return(stat); } - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); } - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); /* Read until next frame set block */ stat = tng_block_header_read(tng_data, block); while(file_pos < tng_data->input_file_len && @@ -16601,7 +16595,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_interval_get hash_mode); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -16611,7 +16605,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_interval_get else { file_pos += block->block_contents_size + block->header_contents_size; - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -16673,14 +16667,14 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_vector_interval_get tot_n_frames / *stride_length + 1: tot_n_frames / *stride_length; - data_size = n_frames_div * size * (*n_particles) * + full_data_len = n_frames_div * size * (*n_particles) * (*n_values_per_frame); - temp = realloc(*values, data_size); + temp = realloc(*values, full_data_len); if(!temp) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - data_size, __FILE__, __LINE__); + full_data_len, __FILE__, __LINE__); free(*values); *values = 0; return(TNG_CRITICAL); @@ -16778,7 +16772,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get_stride_length tng_function_status stat; tng_non_particle_data_t np_data; tng_particle_data_t p_data; - long orig_file_pos, file_pos; + int64_t orig_file_pos, file_pos; int is_particle_data; if(tng_data->current_trajectory_frame_set_input_file_pos <= 0) @@ -16805,11 +16799,11 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get_stride_length /* If no specific frame was required read until this data block is found */ if(frame < 0) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); while(stat != TNG_SUCCESS && file_pos < tng_data->input_file_len) { stat = tng_frame_set_read_next_only_data_from_block_id(tng_data, TNG_USE_HASH, block_id); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); } } if(stat != TNG_SUCCESS) @@ -16919,7 +16913,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_open if(mode == 'w') { - tng_output_file_set(*tng_data_p, filename); + stat = tng_output_file_set(*tng_data_p, filename); } else if(mode == 'a') { @@ -16928,8 +16922,8 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_open fclose((*tng_data_p)->output_file); } (*tng_data_p)->output_file = (*tng_data_p)->input_file; - fseek((*tng_data_p)->input_file, - (long)(*tng_data_p)->last_trajectory_frame_set_input_file_pos, + fseeko((*tng_data_p)->input_file, + (*tng_data_p)->last_trajectory_frame_set_input_file_pos, SEEK_SET); stat = tng_frame_set_read(*tng_data_p, TNG_USE_HASH); @@ -16958,10 +16952,10 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_open } tng_output_append_file_set(*tng_data_p, filename); - fseek((*tng_data_p)->output_file, 0, SEEK_END); + fseeko((*tng_data_p)->output_file, 0, SEEK_END); } - return(TNG_SUCCESS); + return(stat); } tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_close @@ -17306,9 +17300,9 @@ tng_function_status DECLSPECDLLEXPORT tng_util_particle_data_next_frame_read tng_particle_data_t data = 0; tng_function_status stat; int size; - int64_t i, data_size, n_particles; + int64_t i, full_data_len, n_particles; void *temp; - long file_pos; + int64_t file_pos; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(values, "TNG library: The pointer to the values array must not be a NULL pointer"); @@ -17322,11 +17316,11 @@ tng_function_status DECLSPECDLLEXPORT tng_util_particle_data_next_frame_read if(stat != TNG_SUCCESS) { stat = tng_frame_set_read_current_only_data_from_block_id(tng_data, TNG_USE_HASH, block_id); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); while(stat != TNG_SUCCESS && file_pos < tng_data->input_file_len) { stat = tng_frame_set_read_next_only_data_from_block_id(tng_data, TNG_USE_HASH, block_id); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); } if(stat != TNG_SUCCESS) { @@ -17340,8 +17334,8 @@ tng_function_status DECLSPECDLLEXPORT tng_util_particle_data_next_frame_read } if(data->last_retrieved_frame < 0) { - fseek(tng_data->input_file, - (long)tng_data->first_trajectory_frame_set_input_file_pos, + fseeko(tng_data->input_file, + tng_data->first_trajectory_frame_set_input_file_pos, SEEK_SET); stat = tng_frame_set_read(tng_data, TNG_USE_HASH); if(stat != TNG_SUCCESS) @@ -17436,16 +17430,16 @@ tng_function_status DECLSPECDLLEXPORT tng_util_particle_data_next_frame_read size = sizeof(double); } - data_size = size * n_particles * data->n_values_per_frame; + full_data_len = size * n_particles * data->n_values_per_frame; -// fprintf(stderr, "TNG library: TEMP: i = %"PRId64", data_size = %"PRId64", size = %d, n_particles = %"PRId64", n_values_per_frame = %"PRId64"\n", -// i, data_size, size, n_particles, data->n_values_per_frame); +// fprintf(stderr, "TNG library: TEMP: i = %"PRId64", full_data_len = %"PRId64", size = %d, n_particles = %"PRId64", n_values_per_frame = %"PRId64"\n", +// i, full_data_len, size, n_particles, data->n_values_per_frame); - temp = realloc(*values, data_size); + temp = realloc(*values, full_data_len); if(!temp) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - data_size, __FILE__, __LINE__); + full_data_len, __FILE__, __LINE__); free(*values); *values = 0; return(TNG_CRITICAL); @@ -17453,7 +17447,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_particle_data_next_frame_read *values = temp; - memcpy(*values, (char *)data->values + i * data_size, data_size); + memcpy(*values, (char *)data->values + i * full_data_len, full_data_len); return(TNG_SUCCESS); } @@ -17470,9 +17464,9 @@ tng_function_status DECLSPECDLLEXPORT tng_util_non_particle_data_next_frame_read tng_non_particle_data_t data = 0; tng_function_status stat; int size; - int64_t i, data_size; + int64_t i, full_data_len; void *temp; - long file_pos; + int64_t file_pos; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(values, "TNG library: The pointer to the values array must not be a NULL pointer"); @@ -17486,11 +17480,11 @@ tng_function_status DECLSPECDLLEXPORT tng_util_non_particle_data_next_frame_read if(stat != TNG_SUCCESS) { stat = tng_frame_set_read_current_only_data_from_block_id(tng_data, TNG_USE_HASH, block_id); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); while(stat != TNG_SUCCESS && file_pos < tng_data->input_file_len) { stat = tng_frame_set_read_next_only_data_from_block_id(tng_data, TNG_USE_HASH, block_id); - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); } if(stat != TNG_SUCCESS) { @@ -17504,8 +17498,8 @@ tng_function_status DECLSPECDLLEXPORT tng_util_non_particle_data_next_frame_read } if(data->last_retrieved_frame < 0) { - fseek(tng_data->input_file, - (long)tng_data->first_trajectory_frame_set_input_file_pos, + fseeko(tng_data->input_file, + tng_data->first_trajectory_frame_set_input_file_pos, SEEK_SET); stat = tng_frame_set_read(tng_data, TNG_USE_HASH); if(stat != TNG_SUCCESS) @@ -17598,13 +17592,13 @@ tng_function_status DECLSPECDLLEXPORT tng_util_non_particle_data_next_frame_read size = sizeof(double); } - data_size = size * data->n_values_per_frame; + full_data_len = size * data->n_values_per_frame; - temp = realloc(*values, data_size); + temp = realloc(*values, full_data_len); if(!temp) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - data_size, __FILE__, __LINE__); + full_data_len, __FILE__, __LINE__); free(*values); *values = 0; return(TNG_CRITICAL); @@ -17612,7 +17606,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_non_particle_data_next_frame_read *values = temp; - memcpy(*values, (char *)data->values + i * data_size, data_size); + memcpy(*values, (char *)data->values + i * full_data_len, full_data_len); return(TNG_SUCCESS); } @@ -18174,7 +18168,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_generic_write tng_function_status stat; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(values, "TNG library: values must not be a NULL pointer"); if(particle_dependency == TNG_PARTICLE_BLOCK_DATA) @@ -18388,7 +18381,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_generic_double_write tng_function_status stat; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(values, "TNG library: values must not be a NULL pointer"); if(particle_dependency == TNG_PARTICLE_BLOCK_DATA) @@ -18591,7 +18583,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_pos_write const float *positions) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(positions, "TNG library: positions must not be a NULL pointer"); return(tng_util_generic_write(tng_data, frame_nr, positions, 3, @@ -18606,7 +18597,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_pos_double_write const double *positions) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(positions, "TNG library: positions must not be a NULL pointer"); return(tng_util_generic_double_write(tng_data, frame_nr, positions, 3, @@ -18621,7 +18611,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_vel_write const float *velocities) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(velocities, "TNG library: velocities must not be a NULL pointer"); return(tng_util_generic_write(tng_data, frame_nr, velocities, 3, @@ -18636,7 +18625,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_vel_double_write const double *velocities) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(velocities, "TNG library: velocities must not be a NULL pointer"); return(tng_util_generic_double_write(tng_data, frame_nr, velocities, 3, @@ -18651,7 +18639,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_force_write const float *forces) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(forces, "TNG library: forces must not be a NULL pointer"); return(tng_util_generic_write(tng_data, frame_nr, forces, 3, @@ -18666,7 +18653,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_force_double_write const double *forces) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(forces, "TNG library: forces must not be a NULL pointer"); return(tng_util_generic_double_write(tng_data, frame_nr, forces, 3, @@ -18681,7 +18667,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_box_shape_write const float *box_shape) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(box_shape, "TNG library: box_shape must not be a NULL pointer"); return(tng_util_generic_write(tng_data, frame_nr, box_shape, 9, @@ -18696,7 +18681,6 @@ tng_function_status DECLSPECDLLEXPORT tng_util_box_shape_double_write const double *box_shape) { TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); - TNG_ASSERT(frame_nr >= 0, "TNG library: frame_nr must be >= 0."); TNG_ASSERT(box_shape, "TNG library: box_shape must not be a NULL pointer"); return(tng_util_generic_double_write(tng_data, frame_nr, box_shape, 9, @@ -19079,7 +19063,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_next_frame_present_dat int64_t data_frame, frame_diff, min_diff; int64_t size, frame_set_file_pos; int found, read_all = 0; - long file_pos; + int64_t file_pos; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(next_frame, "TNG library: The pointer to the next frame must not be NULL."); @@ -19130,7 +19114,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_next_frame_present_dat /* Check for data blocks only if they have not already been found. */ if(frame_set->n_particle_data_blocks <= 0 && frame_set->n_data_blocks <= 0) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { tng_block_init(&block); @@ -19144,7 +19128,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_next_frame_present_dat TNG_USE_HASH); if(stat != TNG_CRITICAL) { - file_pos = ftell(tng_data->input_file); + file_pos = ftello(tng_data->input_file); if(file_pos < tng_data->input_file_len) { stat = tng_block_header_read(tng_data, block); @@ -19154,7 +19138,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_next_frame_present_dat tng_block_destroy(&block); if(stat == TNG_CRITICAL) { - fprintf(stderr, "TNG library: Cannot read block header at pos %ld. %s: %d\n", + fprintf(stderr, "TNG library: Cannot read block header at pos %"PRId64". %s: %d\n", file_pos, __FILE__, __LINE__); return(stat); } @@ -19362,7 +19346,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_all_data_block_types_g char **compressions) { tng_gen_block_t block; - long orig_file_pos, file_pos; + int64_t orig_file_pos, file_pos; TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup."); TNG_ASSERT(n_data_blocks, "TNG library: The pointer to n_data_blocks must not be NULL."); @@ -19370,15 +19354,15 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_all_data_block_types_g TNG_ASSERT(data_block_names, "TNG library: The pointer to the list of data block names must not be NULL."); TNG_ASSERT(stride_lengths, "TNG library: The pointer to the list of stride lengths must not be NULL."); - orig_file_pos = ftell(tng_data->input_file); + orig_file_pos = ftello(tng_data->input_file); if(!tng_data->input_file_len) { - fseek(tng_data->input_file, 0, SEEK_END); - tng_data->input_file_len = ftell(tng_data->input_file); + fseeko(tng_data->input_file, 0, SEEK_END); + tng_data->input_file_len = ftello(tng_data->input_file); } - fseek(tng_data->input_file, 0, SEEK_SET); + fseeko(tng_data->input_file, 0, SEEK_SET); file_pos = 0; *n_data_blocks = 0; @@ -19392,11 +19376,11 @@ tng_function_status DECLSPECDLLEXPORT tng_util_trajectory_all_data_block_types_g { } - file_pos += (long)(block->block_contents_size + block->header_contents_size); - fseek(tng_data->input_file, (long)block->block_contents_size, SEEK_CUR); + file_pos += (block->block_contents_size + block->header_contents_size); + fseeko(tng_data->input_file, block->block_contents_size, SEEK_CUR); } - fseek(tng_data->input_file, orig_file_pos, SEEK_SET); + fseeko(tng_data->input_file, orig_file_pos, SEEK_SET); return(TNG_SUCCESS); } |