#include "compression.h"
#include "common_header.h"
-void common_header_init_from_config(
+void common_header_from_config(
struct common_header *header,
const char magic[4],
uint8_t RESULT_VERSION,
header->content_size = content_size;
}
-bool common_header_init_from_file(struct common_header *header, FILE *f)
+void
+common_header_from_bytes(struct common_header *header, uint8_t *buffer)
{
- char buffer[COMMON_HEADER_SIZE];
- if (fread(buffer, 1, sizeof(buffer), f) != sizeof(buffer)) {
- return false;
- }
memcpy(header->magic, buffer, 4);
header->version = buffer[4];
header->compression_type = buffer[5];
header->compression_level = buffer[6];
header->content_size = UINT64_FROM_BYTES(buffer + 7);
- return true;
}
-bool common_header_write_to_file(const struct common_header *header, FILE *f)
+void
+common_header_to_bytes(const struct common_header *header, uint8_t *buffer)
{
- char buffer[COMMON_HEADER_SIZE];
memcpy(buffer, header->magic, 4);
buffer[4] = header->version;
buffer[5] = header->compression_type;
buffer[6] = header->compression_level;
BYTES_FROM_UINT64(buffer + 7, header->content_size);
- return fwrite(buffer, 1, sizeof(buffer), f) == sizeof(buffer);
}
bool common_header_verify(
+#ifndef COMMON_HEADER_H
+#define COMMON_HEADER_H
+
#define COMMON_HEADER_SIZE 15
struct common_header {
uint64_t content_size;
};
-void common_header_init_from_config(
+void common_header_from_config(
struct common_header *header,
const char magic[4],
uint8_t RESULT_VERSION,
uint64_t content_size);
-bool common_header_init_from_file(struct common_header *header, FILE *f);
-bool common_header_write_to_file(const struct common_header *header, FILE *f);
+void common_header_from_bytes(struct common_header *header, uint8_t *bytes);
+void common_header_to_bytes(
+ const struct common_header *header, uint8_t *bytes);
bool common_header_verify(
const struct common_header *header, int fd, const char *name, char **errmsg);
void common_header_dump(const struct common_header *header, FILE *f);
+
+#endif
goto out;
}
- if (!common_header_init_from_file(&mf->header, f)) {
+ uint8_t header_bytes[COMMON_HEADER_SIZE];
+ if (fread(header_bytes, sizeof(header_bytes), 1, f) != 1) {
*errmsg = format("Failed to read header from %s", path);
goto out;
}
+ common_header_from_bytes(&mf->header, header_bytes);
+
if (memcmp(mf->header.magic, MAGIC, sizeof(MAGIC)) != 0) {
*errmsg = format(
"Result file has bad magic value 0x%x%x%x%x",
}
struct common_header header;
- common_header_init_from_config(
- &header, MAGIC, MANIFEST_VERSION, content_size);
- if (!common_header_write_to_file(&header, f)) {
+ common_header_from_config(&header, MAGIC, MANIFEST_VERSION, content_size);
+ uint8_t header_bytes[COMMON_HEADER_SIZE];
+ common_header_to_bytes(&header, header_bytes);
+ if (fwrite(header_bytes, sizeof(header_bytes), 1, f) != 1) {
goto error;
}
goto out;
}
- struct common_header header;
- if (!common_header_init_from_file(&header, f)) {
+ uint8_t header_bytes[COMMON_HEADER_SIZE];
+ if (fread(header_bytes, sizeof(header_bytes), 1, f) != 1) {
*errmsg = format("Failed to read header from %s", path);
goto out;
}
+ struct common_header header;
+ common_header_from_bytes(&header, header_bytes);
+
if (memcmp(header.magic, MAGIC, sizeof(MAGIC)) != 0) {
*errmsg = format(
"Result file has bad magic value 0x%x%x%x%x",
}
struct common_header header;
- common_header_init_from_config(&header, MAGIC, RESULT_VERSION, content_size);
- if (!common_header_write_to_file(&header, f)) {
+ common_header_from_config(&header, MAGIC, RESULT_VERSION, content_size);
+
+ uint8_t header_bytes[COMMON_HEADER_SIZE];
+ common_header_to_bytes(&header, header_bytes);
+ if (fwrite(header_bytes, sizeof(header_bytes), 1, f) != 1) {
cc_log("Failed to write result file header to %s", tmp_file);
goto out;
}