Use typedef
's for custom types to make code less cluttered
This commit is contained in:
parent
e97feb1e3d
commit
82f8fdc736
4 changed files with 91 additions and 92 deletions
|
@ -38,11 +38,11 @@
|
||||||
|
|
||||||
#include "haggis.h"
|
#include "haggis.h"
|
||||||
|
|
||||||
int load_u16(FILE *stream, union u16 num);
|
int load_u16(FILE *stream, u16 num);
|
||||||
int store_u16(FILE *stream, union u16 num);
|
int store_u16(FILE *stream, u16 num);
|
||||||
int load_u32(FILE *stream, union u32 num);
|
int load_u32(FILE *stream, u32 num);
|
||||||
int store_u32(FILE *stream, union u32 num);
|
int store_u32(FILE *stream, u32 num);
|
||||||
int load_u64(FILE *stream, union u64 num);
|
int load_u64(FILE *stream, u64 num);
|
||||||
int store_u64(FILE *stream, union u64 num);
|
int store_u64(FILE *stream, u64 num);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -38,56 +38,59 @@
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
|
|
||||||
union u16 {
|
union _u16 {
|
||||||
uint16_t val;
|
uint16_t val;
|
||||||
u8 bytes[2];
|
u8 bytes[2];
|
||||||
};
|
};
|
||||||
|
typedef union _u16 u16;
|
||||||
|
|
||||||
union u32 {
|
union _u32 {
|
||||||
uint32_t val;
|
uint32_t val;
|
||||||
u8 bytes[4];
|
u8 bytes[4];
|
||||||
};
|
};
|
||||||
|
typedef union _u32 u32;
|
||||||
|
|
||||||
union u64 {
|
union _u64 {
|
||||||
uint64_t val;
|
uint64_t val;
|
||||||
u8 bytes[8];
|
u8 bytes[8];
|
||||||
};
|
};
|
||||||
|
typedef union _u64 u64;
|
||||||
|
|
||||||
struct haggis_device {
|
typedef struct {
|
||||||
union u32 major;
|
u32 major;
|
||||||
union u32 minor;
|
u32 minor;
|
||||||
};
|
} haggis_device;
|
||||||
|
|
||||||
enum haggis_algorithm {
|
typedef enum {
|
||||||
md5,
|
md5,
|
||||||
sha1,
|
sha1,
|
||||||
sha256,
|
sha256,
|
||||||
skip,
|
skip,
|
||||||
};
|
} haggis_algorithm;
|
||||||
|
|
||||||
union haggis_sum {
|
typedef union {
|
||||||
u8 md5[16];
|
u8 md5[16];
|
||||||
u8 sha1[20];
|
u8 sha1[20];
|
||||||
u8 sha256[32];
|
u8 sha256[32];
|
||||||
};
|
} haggis_sum;
|
||||||
|
|
||||||
struct haggis_checksum {
|
typedef struct {
|
||||||
enum haggis_algorithm tag;
|
haggis_algorithm tag;
|
||||||
union haggis_sum *sum;
|
haggis_sum *sum;
|
||||||
};
|
} haggis_checksum;
|
||||||
|
|
||||||
struct haggis_file {
|
typedef struct {
|
||||||
union u64 len;
|
u64 len;
|
||||||
struct haggis_checksum *cksum;
|
haggis_checksum *cksum;
|
||||||
u8 *data;
|
u8 *data;
|
||||||
};
|
} haggis_file;
|
||||||
|
|
||||||
struct haggis_filename {
|
typedef struct {
|
||||||
union u16 len;
|
u16 len;
|
||||||
char *name;
|
char *name;
|
||||||
};
|
} haggis_filename;
|
||||||
|
|
||||||
enum haggis_typeflag {
|
typedef enum {
|
||||||
normal,
|
normal,
|
||||||
hardlink,
|
hardlink,
|
||||||
softlink,
|
softlink,
|
||||||
|
@ -96,31 +99,31 @@ enum haggis_typeflag {
|
||||||
block,
|
block,
|
||||||
fifo,
|
fifo,
|
||||||
eof,
|
eof,
|
||||||
};
|
} haggis_typeflag;
|
||||||
|
|
||||||
union haggis_ft {
|
typedef union {
|
||||||
struct haggis_file *file;
|
haggis_file *file;
|
||||||
struct haggis_filename *target;
|
haggis_filename *target;
|
||||||
struct haggis_device *dev;
|
haggis_device *dev;
|
||||||
};
|
} haggis_ft;
|
||||||
|
|
||||||
struct haggis_filetype {
|
typedef struct {
|
||||||
enum haggis_typeflag tag;
|
haggis_typeflag tag;
|
||||||
union haggis_ft *f_type;
|
haggis_ft *f_type;
|
||||||
};
|
} haggis_filetype;
|
||||||
|
|
||||||
struct haggis_node {
|
typedef struct {
|
||||||
struct haggis_filename *name;
|
haggis_filename *name;
|
||||||
union u32 uid;
|
u32 uid;
|
||||||
union u32 gid;
|
u32 gid;
|
||||||
union u64 mtime;
|
u64 mtime;
|
||||||
union u16 mode;
|
u16 mode;
|
||||||
struct haggis_filetype *filetype;
|
haggis_filetype *filetype;
|
||||||
};
|
} haggis_node;
|
||||||
|
|
||||||
struct haggis_node* haggis_create_node(char *file);
|
haggis_node* haggis_create_node(char *file);
|
||||||
int haggis_extract_node(FILE *stram, struct haggis_node *node);
|
int haggis_extract_node(FILE *stram, haggis_node *node);
|
||||||
int haggis_load_node(FILE *stream, struct haggis_node *node);
|
int haggis_load_node(FILE *stream, haggis_node *node);
|
||||||
int haggis_store_node(FILE *stream, struct haggis_node *node);
|
int haggis_store_node(FILE *stream, haggis_node *node);
|
||||||
|
|
||||||
#endif // !HAGGIS_H
|
#endif // !HAGGIS_H
|
||||||
|
|
24
src/bytes.c
24
src/bytes.c
|
@ -38,31 +38,31 @@
|
||||||
#include "haggis.h"
|
#include "haggis.h"
|
||||||
|
|
||||||
#if __BYTE_ORDER__ == __LITTLE_ENDIAN
|
#if __BYTE_ORDER__ == __LITTLE_ENDIAN
|
||||||
int load_u16(FILE *stream, union u16 num) {
|
int load_u16(FILE *stream, u16 num) {
|
||||||
return fread(num.bytes, 1, 2, stream);
|
return fread(num.bytes, 1, 2, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int store_u16(FILE *stream, union u16 num) {
|
int store_u16(FILE *stream, u16 num) {
|
||||||
return fwrite(num.bytes, 1, 2, stream);
|
return fwrite(num.bytes, 1, 2, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_u32(FILE *stream, union u32 num) {
|
int load_u32(FILE *stream, u32 num) {
|
||||||
return fread(num.bytes, 1, 4, stream);
|
return fread(num.bytes, 1, 4, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int store_u32(FILE *stream, union u32 num) {
|
int store_u32(FILE *stream, u32 num) {
|
||||||
return fwrite(num.bytes, 1, 4, stream);
|
return fwrite(num.bytes, 1, 4, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_u64(FILE *stream, union u64 num) {
|
int load_u64(FILE *stream, u64 num) {
|
||||||
return fread(num.bytes, 1, 8, stream);
|
return fread(num.bytes, 1, 8, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int store_u64(FILE *stream, union u64 num) {
|
int store_u64(FILE *stream, u64 num) {
|
||||||
return fwrite(num.bytes, 1, 8, stream);
|
return fwrite(num.bytes, 1, 8, stream);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int load_u16(FILE *stream, union u16 num) {
|
int load_u16(FILE *stream, u16 num) {
|
||||||
u8 buf[2];
|
u8 buf[2];
|
||||||
int res = fread(buf, 1, 2, stream);
|
int res = fread(buf, 1, 2, stream);
|
||||||
if (res != 2)
|
if (res != 2)
|
||||||
|
@ -72,14 +72,14 @@ int load_u16(FILE *stream, union u16 num) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int store_u16(FILE * stream, union u16 num) {
|
int store_u16(FILE * stream, u16 num) {
|
||||||
u8 buf[2];
|
u8 buf[2];
|
||||||
buf[0] = num.bytes[1];
|
buf[0] = num.bytes[1];
|
||||||
buf[1] = num.bytes[0];
|
buf[1] = num.bytes[0];
|
||||||
return fwrite(buf, 1, 2, stream);
|
return fwrite(buf, 1, 2, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_u32(FILE *stream, union u32 num) {
|
int load_u32(FILE *stream, u32 num) {
|
||||||
u8 buf[4];
|
u8 buf[4];
|
||||||
int res = fread(buf, 1, 4, stream);
|
int res = fread(buf, 1, 4, stream);
|
||||||
if (res != 4)
|
if (res != 4)
|
||||||
|
@ -90,7 +90,7 @@ int load_u32(FILE *stream, union u32 num) {
|
||||||
num.bytes[3] = buf[0];
|
num.bytes[3] = buf[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
int store_u32(FILE *stream, union u32 num) {
|
int store_u32(FILE *stream, u32 num) {
|
||||||
u8 buf[4];
|
u8 buf[4];
|
||||||
buf[0] = num.bytes[3];
|
buf[0] = num.bytes[3];
|
||||||
buf[1] = num.bytes[2];
|
buf[1] = num.bytes[2];
|
||||||
|
@ -99,7 +99,7 @@ int store_u32(FILE *stream, union u32 num) {
|
||||||
return fwrite(buf, 1, 2, stream);
|
return fwrite(buf, 1, 2, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_u64(FILE *stream, union u64 num) {
|
int load_u64(FILE *stream, u64 num) {
|
||||||
u8 buf[8];
|
u8 buf[8];
|
||||||
int res = fread(buf, 1, 8, stream);
|
int res = fread(buf, 1, 8, stream);
|
||||||
if (res != 8)
|
if (res != 8)
|
||||||
|
@ -114,7 +114,7 @@ int load_u64(FILE *stream, union u64 num) {
|
||||||
num.bytes[7] = buf[0];
|
num.bytes[7] = buf[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
int store_u64(FILE *stream, union u64 num) {
|
int store_u64(FILE *stream, u64 num) {
|
||||||
u8 buf[8];
|
u8 buf[8];
|
||||||
buf[0] = num.bytes[7];
|
buf[0] = num.bytes[7];
|
||||||
buf[1] = num.bytes[6];
|
buf[1] = num.bytes[6];
|
||||||
|
|
54
src/haggis.c
54
src/haggis.c
|
@ -70,7 +70,7 @@ int haggis_check_header(FILE *stream) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_store_device(FILE *stream, struct haggis_device *dev) {
|
int haggis_store_device(FILE *stream, haggis_device *dev) {
|
||||||
if (fwrite(dev->major.bytes, 1, 4, stream) != 4)
|
if (fwrite(dev->major.bytes, 1, 4, stream) != 4)
|
||||||
return 1;
|
return 1;
|
||||||
if (fwrite(dev->minor.bytes, 1, 4, stream) != 4)
|
if (fwrite(dev->minor.bytes, 1, 4, stream) != 4)
|
||||||
|
@ -78,7 +78,7 @@ int haggis_store_device(FILE *stream, struct haggis_device *dev) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_load_device(FILE *stream, union haggis_ft *ft) {
|
int haggis_load_device(FILE *stream, haggis_ft *ft) {
|
||||||
if (fread(ft->dev->major.bytes, 1, 4, stream) != 4)
|
if (fread(ft->dev->major.bytes, 1, 4, stream) != 4)
|
||||||
return 1;
|
return 1;
|
||||||
if (fread(ft->dev->minor.bytes, 1, 4, stream) != 4)
|
if (fread(ft->dev->minor.bytes, 1, 4, stream) != 4)
|
||||||
|
@ -86,7 +86,7 @@ int haggis_load_device(FILE *stream, union haggis_ft *ft) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_store_cksum(FILE *stream, struct haggis_checksum *cksum) {
|
int haggis_store_cksum(FILE *stream, haggis_checksum *cksum) {
|
||||||
u8 flag;
|
u8 flag;
|
||||||
|
|
||||||
switch (cksum->tag) {
|
switch (cksum->tag) {
|
||||||
|
@ -120,7 +120,7 @@ int haggis_store_cksum(FILE *stream, struct haggis_checksum *cksum) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_load_cksum(FILE *stream, struct haggis_checksum *cksum) {
|
int haggis_load_cksum(FILE *stream, haggis_checksum *cksum) {
|
||||||
u8 flag;
|
u8 flag;
|
||||||
|
|
||||||
if (fread(&flag, 1, 1, stream) != 1)
|
if (fread(&flag, 1, 1, stream) != 1)
|
||||||
|
@ -148,7 +148,7 @@ int haggis_load_cksum(FILE *stream, struct haggis_checksum *cksum) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int validate_md5(struct haggis_file *file) {
|
int validate_md5(haggis_file *file) {
|
||||||
MD5_CTX ctx;
|
MD5_CTX ctx;
|
||||||
u8 digest[16];
|
u8 digest[16];
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ int validate_md5(struct haggis_file *file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
||||||
int validate_sha1(struct haggis_file *file) {
|
int validate_sha1(haggis_file *file) {
|
||||||
SHA1_CTX ctx;
|
SHA1_CTX ctx;
|
||||||
u8 digest[20];
|
u8 digest[20];
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ int validate_sha1(struct haggis_file *file) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
||||||
int validate_sha1(struct haggis_file *file) {
|
int validate_sha1(haggis_file *file) {
|
||||||
SHA1_CTX ctx;
|
SHA1_CTX ctx;
|
||||||
u8 digest[20];
|
u8 digest[20];
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ int validate_sha1(struct haggis_file *file) {
|
||||||
#endif /* if defined (__FreeBSD__) */
|
#endif /* if defined (__FreeBSD__) */
|
||||||
|
|
||||||
#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__)
|
#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__)
|
||||||
int validate_sha256(struct haggis_file *file) {
|
int validate_sha256(haggis_file *file) {
|
||||||
SHA256_CTX ctx;
|
SHA256_CTX ctx;
|
||||||
u8 digest[32];
|
u8 digest[32];
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ int validate_sha256(struct haggis_file *file) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#elif defined(__linux__) || defined(__OpenBSD__)
|
#elif defined(__linux__) || defined(__OpenBSD__)
|
||||||
int validate_sha256(struct haggis_file *file) {
|
int validate_sha256(haggis_file *file) {
|
||||||
SHA2_CTX ctx;
|
SHA2_CTX ctx;
|
||||||
u8 digest[32];
|
u8 digest[32];
|
||||||
|
|
||||||
|
@ -212,7 +212,7 @@ int validate_sha256(struct haggis_file *file) {
|
||||||
}
|
}
|
||||||
#endif /* if defined (__FreeBSD__) */
|
#endif /* if defined (__FreeBSD__) */
|
||||||
|
|
||||||
int haggis_validate_cksum(struct haggis_file *file) {
|
int haggis_validate_cksum(haggis_file *file) {
|
||||||
switch (file->cksum->tag) {
|
switch (file->cksum->tag) {
|
||||||
case md5:
|
case md5:
|
||||||
return validate_md5(file);
|
return validate_md5(file);
|
||||||
|
@ -226,7 +226,7 @@ int haggis_validate_cksum(struct haggis_file *file) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_store_file(FILE *stream, struct haggis_file *file) {
|
int haggis_store_file(FILE *stream, haggis_file *file) {
|
||||||
if (store_u64(stream, file->len) != 8)
|
if (store_u64(stream, file->len) != 8)
|
||||||
return 1;
|
return 1;
|
||||||
if (haggis_store_cksum(stream, file->cksum) != 0)
|
if (haggis_store_cksum(stream, file->cksum) != 0)
|
||||||
|
@ -237,8 +237,8 @@ int haggis_store_file(FILE *stream, struct haggis_file *file) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_load_file(FILE *stream, union haggis_ft *ft) {
|
int haggis_load_file(FILE *stream, haggis_ft *ft) {
|
||||||
union u64 len;
|
u64 len;
|
||||||
len.val = 0;
|
len.val = 0;
|
||||||
|
|
||||||
if (load_u64(stream, len) != 8)
|
if (load_u64(stream, len) != 8)
|
||||||
|
@ -260,7 +260,7 @@ int haggis_load_file(FILE *stream, union haggis_ft *ft) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_store_filetype(FILE *stream, struct haggis_filetype *filetype) {
|
int haggis_store_filetype(FILE *stream, haggis_filetype *filetype) {
|
||||||
size_t len;
|
size_t len;
|
||||||
u8 flag;
|
u8 flag;
|
||||||
int res;
|
int res;
|
||||||
|
@ -324,8 +324,8 @@ int haggis_store_filetype(FILE *stream, struct haggis_filetype *filetype) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_load_filename(FILE *stream, struct haggis_filename *n) {
|
int haggis_load_filename(FILE *stream, haggis_filename *n) {
|
||||||
union u16 len;
|
u16 len;
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
len.val = 0;
|
len.val = 0;
|
||||||
|
@ -341,11 +341,7 @@ int haggis_load_filename(FILE *stream, struct haggis_filename *n) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_load_filetype(
|
int haggis_load_filetype(FILE *stream, haggis_typeflag tag, haggis_filetype *file) {
|
||||||
FILE *stream,
|
|
||||||
enum haggis_typeflag tag,
|
|
||||||
struct haggis_filetype *file
|
|
||||||
) {
|
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
case normal:
|
case normal:
|
||||||
file->tag = 0;
|
file->tag = 0;
|
||||||
|
@ -375,27 +371,27 @@ int haggis_load_filetype(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum haggis_typeflag haggis_filetype_from_mode(union u16 mode) {
|
haggis_typeflag haggis_filetype_from_mode(u16 mode) {
|
||||||
u8 mask = 07 << 5;
|
u8 mask = 07 << 5;
|
||||||
int filetype = (int)((mode.bytes[0] & mask) >> 5);
|
int filetype = (int)((mode.bytes[0] & mask) >> 5);
|
||||||
return filetype;
|
return filetype;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct haggis_node* haggis_create_node(char *file) {
|
haggis_node* haggis_create_node(char *file) {
|
||||||
struct haggis_node *node = malloc(sizeof(struct haggis_node));
|
haggis_node *node = malloc(sizeof(haggis_node));
|
||||||
// todo
|
// todo
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_extract_node(FILE *stram, struct haggis_node *node) {
|
int haggis_extract_node(FILE *stram, haggis_node *node) {
|
||||||
// todo
|
// todo
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_load_node(FILE *stream, struct haggis_node *node) {
|
int haggis_load_node(FILE *stream, haggis_node *node) {
|
||||||
int res;
|
int res;
|
||||||
union u16 mode;
|
u16 mode;
|
||||||
enum haggis_typeflag tag;
|
haggis_typeflag tag;
|
||||||
|
|
||||||
mode.val = 0;
|
mode.val = 0;
|
||||||
res = haggis_load_filename(stream, node->name);
|
res = haggis_load_filename(stream, node->name);
|
||||||
|
@ -422,7 +418,7 @@ int haggis_load_node(FILE *stream, struct haggis_node *node) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int haggis_store_node(FILE *stream, struct haggis_node *node) {
|
int haggis_store_node(FILE *stream, haggis_node *node) {
|
||||||
// todo
|
// todo
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue