Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Created August 28, 2023 21:56
Show Gist options
  • Save MohamedElashri/4c17a647d360ff0a5ac8dfce272ecb8a to your computer and use it in GitHub Desktop.
Save MohamedElashri/4c17a647d360ff0a5ac8dfce272ecb8a to your computer and use it in GitHub Desktop.
A C program to read and display key metadata of an HDF5 file, including its signature and superblock information
#include <stdio.h>
#include <string.h> // For strncmp
#define SIGNATURE_SIZE 8
#define MAX_OFFSET 2048
int main() {
FILE* file = fopen("data.h5", "rb");
if (!file) {
printf("Unable to open file!\n");
return 1;
}
char signature[SIGNATURE_SIZE];
int offset = 0;
unsigned char version, size_of_offsets, size_of_lengths;
unsigned short reserved, group_leaf_node_k, group_internal_node_k;
unsigned int file_consistency_flags;
unsigned long long base_address, freespace_info, end_of_file_address, driver_information_block_address;
// Search for the HDF5 signature
while (offset <= MAX_OFFSET) {
if (fseek(file, offset, SEEK_SET) != 0 || fread(signature, sizeof(char), SIGNATURE_SIZE, file) != SIGNATURE_SIZE) {
printf("File operation error.\n");
fclose(file);
return 1;
}
if (strncmp(signature, "\211HDF\r\n\032\n", SIGNATURE_SIZE) == 0) {
printf("Found HDF5 signature at offset %d\n", offset);
break;
}
offset *= 2;
if (offset == 0) offset = 512;
}
// Read the superblock
if (fread(&version, sizeof(unsigned char), 1, file) != 1 ||
fread(&size_of_offsets, sizeof(unsigned char), 1, file) != 1 ||
fread(&size_of_lengths, sizeof(unsigned char), 1, file) != 1 ||
fread(&reserved, sizeof(unsigned short), 1, file) != 1 ||
fread(&group_leaf_node_k, sizeof(unsigned short), 1, file) != 1 ||
fread(&group_internal_node_k, sizeof(unsigned short), 1, file) != 1 ||
fread(&file_consistency_flags, sizeof(unsigned int), 1, file) != 1 ||
fread(&base_address, sizeof(unsigned long long), 1, file) != 1 ||
fread(&freespace_info, sizeof(unsigned long long), 1, file) != 1 ||
fread(&end_of_file_address, sizeof(unsigned long long), 1, file) != 1 ||
fread(&driver_information_block_address, sizeof(unsigned long long), 1, file) != 1) {
printf("File operation error.\n");
fclose(file);
return 1;
}
// Output superblock info
printf("Superblock version: %d\n", version);
printf("Size of Offsets: %d\n", size_of_offsets);
printf("Size of Lengths: %d\n", size_of_lengths);
printf("Group Leaf Node K: %d\n", group_leaf_node_k);
printf("Group Internal Node K: %d\n", group_internal_node_k);
printf("File Consistency Flags: %d\n", file_consistency_flags);
printf("Base Address: %llu\n", base_address);
printf("Freespace Info: %llu\n", freespace_info);
printf("End of File Address: %llu\n", end_of_file_address);
printf("Driver Information Block Address: %llu\n", driver_information_block_address);
fclose(file);
return 0;
}
@MohamedElashri
Copy link
Author

Output example:

Found HDF5 signature at offset 0
Superblock version: 0
Size of Offsets: 0
Size of Lengths: 0
Group Leaf Node K: 2056
Group Internal Node K: 1024
File Consistency Flags: 4096
Base Address: 0
Freespace Info: 18446744073692774400
End of File Address: 302811971583
Driver Information Block Address: 18446744073692774400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment