#include "compression/tng_compress.h"
#include "tng/version.h"
-#ifdef _MSC_VER
-#define fseeko _fseeki64
-#define ftello _ftelli64
+#if defined( _WIN32 ) || defined( _WIN64 )
+ #ifndef fseeko
+ #define fseeko _fseeki64
+ #endif
+ #ifndef ftello
+ #ifdef __MINGW32__
+ #define ftello ftello64
+ #else
+ #define ftello _ftelli64
+ #endif
+ #endif
#endif
struct tng_bond {
*block_p = malloc(sizeof(struct tng_gen_block));
if(!*block_p)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_gen_block), __FILE__, __LINE__);
return(TNG_CRITICAL);
}
sizeof(struct tng_molecule));
if(!tng_data->molecules)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
tng_data->n_molecules * sizeof(struct tng_molecule),
__FILE__, __LINE__);
return(TNG_CRITICAL);
tng_data->n_molecules);
if(!tng_data->molecule_cnt_list)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
tng_data->n_molecules * sizeof(struct tng_molecule),
__FILE__, __LINE__);
return(TNG_CRITICAL);
sizeof(struct tng_chain));
if(!molecule->chains)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
molecule->n_chains * sizeof(struct tng_chain),
__FILE__, __LINE__);
return(TNG_CRITICAL);
sizeof(struct tng_residue));
if(!molecule->residues)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
molecule->n_residues * sizeof(struct tng_residue),
__FILE__, __LINE__);
if(molecule->chains)
sizeof(struct tng_atom));
if(!molecule->atoms)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
molecule->n_atoms * sizeof(struct tng_atom),
__FILE__, __LINE__);
if(molecule->chains)
sizeof(struct tng_bond));
if(!molecule->bonds)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
molecule->n_bonds * sizeof(struct tng_bond),
__FILE__, __LINE__);
if(molecule->chains)
if(!frame_set->molecule_cnt_list)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * tng_data->n_molecules,
__FILE__, __LINE__);
return(TNG_CRITICAL);
temp_name = realloc(block->name, name_len + 1);
if(!temp_name)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%u bytes). %s: %d\n",
name_len+1, __FILE__, __LINE__);
free(block->name);
block->name = 0;
sizeof(int64_t));
if(!mapping->real_particle_numbers)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
mapping->n_particles * sizeof(int64_t), __FILE__, __LINE__);
return(TNG_CRITICAL);
}
temp_name = realloc(block->name, name_len + 1);
if(!temp_name)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%u bytes). %s: %d\n",
name_len+1, __FILE__, __LINE__);
free(block->name);
block->name = 0;
frame_set->n_particle_data_blocks);
if(!data)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_particle_data) *
frame_set->n_particle_data_blocks,
__FILE__, __LINE__);
tng_data->n_particle_data_blocks);
if(!data)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_particle_data) *
tng_data->n_particle_data_blocks,
__FILE__, __LINE__);
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(type == TNG_FLOAT_DATA)
{
dest = tng_compress_vel_float_find_algo((float *)temp_data_contents, (int)n_particles,
const int64_t uncompressed_len)
{
char *temp, *temp_data_contents;
+ int64_t compressed_len;
double *d_dest = 0;
float *f_dest = 0;
int64_t offset;
return(TNG_FAILURE);
}
- temp_data_contents = malloc(uncompressed_len);
+ compressed_len = block->block_contents_size - (int64_t)((char *)start_pos - (char *)block->block_contents);
+ temp_data_contents = malloc(compressed_len);
if(!temp_data_contents)
{
fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
return(TNG_CRITICAL);
}
- memcpy(temp_data_contents, (char *)start_pos, uncompressed_len);
+ memcpy(temp_data_contents, (char *)start_pos, compressed_len);
if(type == TNG_FLOAT_DATA)
{
dest = malloc(max_len);
if(!dest)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%ld bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
max_len, __FILE__, __LINE__);
return(TNG_CRITICAL);
}
n_particles);
if(!data->strings[i])
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values *) * n_particles,
__FILE__, __LINE__);
return(TNG_CRITICAL);
n_values_per_frame);
if(!data->strings[i][j])
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values) * n_values_per_frame,
__FILE__, __LINE__);
return(TNG_CRITICAL);
TNG_ASSERT(offset != 0, "TNG library: offset must not be a NULL pointer.");
+ /* This must be caught early to avoid creating a data block if not necessary. */
+#ifndef USE_ZLIB
+ if(codec_id == TNG_GZIP_COMPRESSION)
+ {
+ fprintf(stderr, "TNG library: Cannot uncompress data block. %s: %d\n", __FILE__,
+ __LINE__);
+ return(TNG_FAILURE);
+ }
+#endif
+
switch(datatype)
{
case TNG_CHAR_DATA:
temp_name = realloc(block->name, len);
if(!temp_name)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lud bytes). %s: %d\n", len,
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n", len,
__FILE__, __LINE__);
free(block->name);
block->name = 0;
frame_set->n_data_blocks);
if(!data)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_non_particle_data) * frame_set->n_data_blocks,
__FILE__, __LINE__);
free(frame_set->tr_data);
tng_data->n_data_blocks);
if(!data)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_non_particle_data) * tng_data->n_data_blocks,
__FILE__, __LINE__);
free(tng_data->non_tr_data);
/* fprintf(stderr, "TNG library: %s\n", block->name);*/
+ /* This must be caught early to avoid creating a data block if not necessary. */
+#ifndef USE_ZLIB
+ if(codec_id == TNG_GZIP_COMPRESSION)
+ {
+ fprintf(stderr, "TNG library: Cannot uncompress data block. %s: %d\n", __FILE__,
+ __LINE__);
+ return(TNG_FAILURE);
+ }
+#endif
+
switch(datatype)
{
case TNG_CHAR_DATA:
temp_name = realloc(block->name, len);
if(!temp_name)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%d bytes). %s: %d\n", len+1,
+ fprintf(stderr, "TNG library: Cannot allocate memory (%u bytes). %s: %d\n", len+1,
__FILE__, __LINE__);
free(block->name);
block->name = 0;
if(!new_molecules)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_molecule) * (tng_data->n_molecules + 1),
__FILE__, __LINE__);
free(tng_data->molecules);
if(!new_molecule_cnt_list)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (tng_data->n_molecules + 1),
__FILE__, __LINE__);
free(tng_data->molecule_cnt_list);
if(!new_molecules)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_molecule) * (tng_data->n_molecules + 1),
__FILE__, __LINE__);
free(tng_data->molecules);
if(!new_molecule_cnt_list)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (tng_data->n_molecules + 1),
__FILE__, __LINE__);
free(tng_data->molecule_cnt_list);
sizeof(struct tng_molecule) * tng_data_src->n_molecules);
if(!molecule_temp)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_molecule) * tng_data_src->n_molecules,
__FILE__, __LINE__);
free(tng_data_dest->molecules);
sizeof(int64_t) * tng_data_src->n_molecules);
if(!list_temp)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * tng_data_src->n_molecules,
__FILE__, __LINE__);
free(tng_data_dest->molecule_cnt_list);
}
}
molecule_temp->n_bonds = molecule->n_bonds;
- bond_temp = realloc(molecule_temp->bonds, sizeof(struct tng_bond) *
- molecule->n_bonds);
- if(!bond_temp)
- {
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
- sizeof(struct tng_bond) * molecule->n_bonds,
- __FILE__, __LINE__);
- free(molecule_temp->bonds);
- molecule_temp->n_bonds = 0;
- return(TNG_CRITICAL);
- }
- molecule_temp->bonds = bond_temp;
- for(j = 0; j < molecule->n_bonds; j++)
+ if(molecule->n_bonds > 0)
{
- molecule_temp->bonds[j] = molecule->bonds[j];
+ bond_temp = realloc(molecule_temp->bonds, sizeof(struct tng_bond) *
+ molecule->n_bonds);
+ if(!bond_temp)
+ {
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
+ sizeof(struct tng_bond) * molecule->n_bonds,
+ __FILE__, __LINE__);
+ free(molecule_temp->bonds);
+ molecule_temp->n_bonds = 0;
+ return(TNG_CRITICAL);
+ }
+ molecule_temp->bonds = bond_temp;
+ for(j = 0; j < molecule->n_bonds; j++)
+ {
+ molecule_temp->bonds[j] = molecule->bonds[j];
+ }
}
stat = tng_molecule_cnt_set(tng_data_dest, molecule_temp,
tng_data_src->molecule_cnt_list[i]);
if(!new_chains)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_chain) * (molecule->n_chains + 1),
__FILE__, __LINE__);
free(molecule->chains);
if(!new_bonds)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_bond) * (molecule->n_bonds + 1),
__FILE__, __LINE__);
*bond = 0;
if(!new_residues)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_residue) * (molecule->n_residues + 1),
__FILE__, __LINE__);
free(molecule->residues);
if(!new_atoms)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_atom) * (molecule->n_atoms + 1),
__FILE__, __LINE__);
free(molecule->atoms);
*molecule_p = malloc(sizeof(struct tng_molecule));
if(!*molecule_p)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_molecule), __FILE__, __LINE__);
return(TNG_CRITICAL);
}
*from_atoms = malloc(sizeof(int64_t) * (*n_bonds));
if(!*from_atoms)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (*n_bonds), __FILE__, __LINE__);
return(TNG_CRITICAL);
}
*to_atoms = malloc(sizeof(int64_t) * (*n_bonds));
if(!*to_atoms)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (*n_bonds), __FILE__, __LINE__);
free(*from_atoms);
*from_atoms = 0;
if(!mapping)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(struct tng_particle_mapping)*frame_set->n_mapping_blocks,
__FILE__, __LINE__);
free(frame_set->mappings);
frame_set->mappings[frame_set->n_mapping_blocks - 1].real_particle_numbers = malloc(sizeof(int64_t) * n_particles);
if(!frame_set->mappings[frame_set->n_mapping_blocks - 1].real_particle_numbers)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * n_particles, __FILE__, __LINE__);
return(TNG_CRITICAL);
}
*tng_data_p = malloc(sizeof(struct tng_trajectory));
if(!*tng_data_p)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_trajectory), __FILE__, __LINE__);
return(TNG_CRITICAL);
}
*dest_p = malloc(sizeof(struct tng_trajectory));
if(!*dest_p)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"TNG_PRIsize" bytes). %s: %d\n",
sizeof(struct tng_trajectory), __FILE__, __LINE__);
return(TNG_CRITICAL);
}
*values = malloc(sizeof(union data_values *) * n_frames);
if(!*values)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values **) * n_frames,
__FILE__, __LINE__);
return(TNG_CRITICAL);
n_values_per_frame);
if(!(*values)[i])
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values) * n_values_per_frame,
__FILE__, __LINE__);
free(values);
*values = malloc(sizeof(union data_values **) * n_frames);
if(!*values)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values **) * n_frames,
__FILE__, __LINE__);
return(TNG_CRITICAL);
n_particles);
if(!(*values)[i])
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values *) * n_particles,
__FILE__, __LINE__);
free(*values);
n_values_per_frame);
if(!(*values)[i][j])
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(union data_values *) * n_particles,
__FILE__, __LINE__);
tng_particle_data_values_free(tng_data, *values, n_frames,
temp = realloc(*data_block_ids_in_next_frame, size);
if(!temp)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (*n_data_blocks_in_next_frame),
__FILE__, __LINE__);
free(*data_block_ids_in_next_frame);
temp = realloc(*data_block_ids_in_next_frame, size);
if(!temp)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (*n_data_blocks_in_next_frame),
__FILE__, __LINE__);
free(*data_block_ids_in_next_frame);
temp = realloc(*data_block_ids_in_next_frame, size);
if(!temp)
{
- fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRIu64" bytes). %s: %d\n",
sizeof(int64_t) * (*n_data_blocks_in_next_frame),
__FILE__, __LINE__);
free(*data_block_ids_in_next_frame);