Compare commits

...

2 Commits

Author SHA1 Message Date
DaniTheSkunk bf7b89c05a skip file format 2023-02-09 18:36:39 +00:00
DaniTheSkunk 732acde1e6 added fwrite helpers 2023-02-09 15:25:38 +00:00
4 changed files with 258 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#define GUARD_5B2A3676BBB85E5075C01FF213126C89
#include "types.h"
#include <stdio.h>
struct sw_filebuffer {
u8 *_data;
@ -9,6 +10,7 @@ struct sw_filebuffer {
u32 size;
};
/* TODO: use skunkworks overlay paths */
u8 *sw_file_load(char *path, u32 *out_size);
struct sw_filebuffer *sw_filebuffer_create(u32 size);
@ -17,4 +19,24 @@ void sw_filebuffer_destroy(struct sw_filebuffer *buf);
void sw_filebuffer_read(struct sw_filebuffer *buf, void *dst, u32 count);
/* fwrite helpers */
void sw_fwrite_u8(FILE *fil, u8 val);
void sw_fwrite_i8(FILE *fil, i8 val);
void sw_fwrite_u16(FILE *fil, u16 val);
void sw_fwrite_i16(FILE *fil, u16 val);
void sw_fwrite_u32(FILE *fil, u32 val);
void sw_fwrite_i32(FILE *fil, u32 val);
void sw_fwrite_u64(FILE *fil, u64 val);
void sw_fwrite_i64(FILE *fil, u64 val);
void sw_fwrite_f32(FILE *fil, f32 val);
void sw_fwrite_f64(FILE *fil, f64 val);
void sw_fwrite_bool(FILE *fil, bool val);
void sw_fwrite_str_nozero(FILE *fil, char *str);
void sw_fwrite_str_zero(FILE *fil, char *str);
void sw_fwrite_str_prefix8(FILE *fil, char *str);
void sw_fwrite_str_prefix16(FILE *fil, char *str);
void sw_fwrite_str_prefix32(FILE *fil, char *str);
#endif /* GUARD_5B2A3676BBB85E5075C01FF213126C89 */

57
include/skip.h Normal file
View File

@ -0,0 +1,57 @@
/******************************************************************************/
/* Skip File Format, skunkworks zip */
/* */
/* little-endian */
/* */
/* Main Header: */
/* */
/* bytes | what */
/* ------+------------------------------------------------------------------- */
/* 4 | 'SWZP' */
/* 1 | version, currently 0x00 */
/* 1 | compression format, currently only 0 supported, uncompressed */
/* 4 | size of uncompressed data chunk */
/* 4 | size of compressed data chunk (same as compressed if format 0) */
/* 2 | file count */
/* */
/* File Header (appears 'file count' times in a row): */
/* */
/* bytes | what */
/* ------+------------------------------------------------------------------- */
/* 4 | offset from beginning of data chunk */
/* 4 | file size (uncompressed) */
/* 1 | length of filename */
/* * | filename (not zero terminated) */
/* */
/* Rest of File: */
/* */
/* bytes | what */
/* ------+------------------------------------------------------------------- */
/* * | data chunk */
/* 4 | length of file total (to easily find header if file is attached to */
/* an executable) */
/******************************************************************************/
#ifndef GUARD_62F584E45B48B8F6069643FC44702ED5
#define GUARD_62F584E45B48B8F6069643FC44702ED5
#include "types.h"
struct sw_skip {
u32 data_size;
u8 *data;
u16 file_count;
struct {
u32 offset;
u32 size;
char *name;
} *files;
};
struct sw_skip *sw_skip_create();
struct sw_skip *sw_skip_load(char *filename);
void sw_skip_destroy(struct sw_skip *skip);
void sw_skip_save(struct sw_skip *skip, char *filename);
#endif /* GUARD_62F584E45B48B8F6069643FC44702ED5 */

View File

@ -61,3 +61,78 @@ void sw_filebuffer_read(struct sw_filebuffer *buf, void *dst, u32 count) {
memcpy(dst, buf->_data + buf->pos, count);
buf->pos += count;
}
/* fwrite helpers */
void sw_fwrite_u8(FILE *fil, u8 val) {
fwrite(&val, 1, 1, fil);
}
void sw_fwrite_i8(FILE *fil, i8 val) {
fwrite(&val, 1, 1, fil);
}
void sw_fwrite_u16(FILE *fil, u16 val) {
fwrite(&val, 1, 2, fil);
}
void sw_fwrite_i16(FILE *fil, u16 val) {
fwrite(&val, 1, 2, fil);
}
void sw_fwrite_u32(FILE *fil, u32 val) {
fwrite(&val, 1, 4, fil);
}
void sw_fwrite_i32(FILE *fil, u32 val) {
fwrite(&val, 1, 4, fil);
}
void sw_fwrite_u64(FILE *fil, u64 val) {
fwrite(&val, 1, 8, fil);
}
void sw_fwrite_i64(FILE *fil, u64 val) {
fwrite(&val, 1, 8, fil);
}
void sw_fwrite_f32(FILE *fil, f32 val) {
fwrite(&val, 1, 4, fil);
}
void sw_fwrite_f64(FILE *fil, f64 val) {
fwrite(&val, 1, 8, fil);
}
void sw_fwrite_bool(FILE *fil, bool val) {
fwrite(&val, 1, 1, fil);
}
void sw_fwrite_str_nozero(FILE *fil, char *str) {
fwrite(str, strlen(str), 1, fil);
}
void sw_fwrite_str_zero(FILE *fil, char *str) {
fwrite(str, strlen(str), 1, fil);
sw_fwrite_u8(fil, 0);
}
/* TODO: implement checks that string not too long? */
void sw_fwrite_str_prefix8(FILE *fil, char *str) {
u8 size = strlen(str);
sw_fwrite_u8(fil, size);
fwrite(str, size, 1, fil);
}
void sw_fwrite_str_prefix16(FILE *fil, char *str) {
u16 size = strlen(str);
sw_fwrite_u16(fil, size);
fwrite(str, size, 1, fil);
}
void sw_fwrite_str_prefix32(FILE *fil, char *str) {
u32 size = strlen(str);
sw_fwrite_u32(fil, size);
fwrite(str, size, 1, fil);
}

104
src/skip.c Normal file
View File

@ -0,0 +1,104 @@
#include "skip.h"
#include "error.h"
#include "file.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sw_skip *sw_skip_create() {
struct sw_skip *skip;
skip = calloc(1, sizeof(struct sw_skip));
return skip;
}
struct sw_skip *sw_skip_load(char *filename) {
FILE *fil;
struct sw_skip *skip;
char magic[5] = {0};
u8 version, compression;
u16 i;
char str[257];
u8 str_len;
skip = sw_skip_create();
fil = fopen(filename, "rb");
fread(magic, 1, 4, fil);
if(strcmp("SWZP", magic) != 0) {
sw_error("not a .skip file");
}
fread(&version, 1, 1, fil);
if(version > 0) {
sw_error("version %i of .skip not supported", version);
}
fread(&compression, 1, 1, fil);
if(compression > 0) {
sw_error("unsupported .skip compression method");
}
fread(&skip->data_size, 1, 4, fil);
fseek(fil, 4, SEEK_CUR); /* ignore compressed size */
fread(&skip->file_count, 1, 2, fil);
skip->files = malloc(sizeof(skip->files[0]) * skip->file_count);
for(i = 0; i < skip->file_count; ++i) {
memset(str, 0, 257);
fread(&skip->files[i].offset, 1, 4, fil);
fread(&skip->files[i].size, 1, 4, fil);
fread(&str_len, 1, 1, fil);
fread(str, str_len, 1, fil);
str[str_len] = 0;
skip->files[i].name = malloc(str_len + 1);
strcpy(skip->files[i].name, str);
}
fclose(fil);
return skip;
}
void sw_skip_destroy(struct sw_skip *skip) {
i32 i;
for(i = 0; i < skip->file_count; ++i) {
free(skip->files[i].name);
}
free(skip->files);
free(skip->data);
free(skip);
}
void sw_skip_save(struct sw_skip *skip, char *filename) {
FILE *fil;
u32 size;
u16 i;
size = 0;
for(i = 0; i < skip->file_count; ++i) {
size += skip->files[i].size;
}
fil = fopen(filename, "wb");
sw_fwrite_str_nozero(fil, "SWZP");
sw_fwrite_u8(fil, 0); /* version */
sw_fwrite_u8(fil, 0); /* compression */
sw_fwrite_u32(fil, size); /* uncompressed size */
sw_fwrite_u32(fil, size); /* compressed size */
sw_fwrite_u16(fil, skip->file_count); /* file count */
for(i = 0; i < skip->file_count; ++i) {
sw_fwrite_u32(fil, skip->files[i].offset); /* offset */
sw_fwrite_u32(fil, skip->files[i].size); /* file size */
sw_fwrite_str_prefix8(fil, skip->files[i].name); /* file name */
}
sw_fwrite_u32(fil, ftell(fil) + 4); /* file length */
fclose(fil);
}