Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <inttypes.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <ctype.h>
- struct key_header {
- uint8_t sig[4]; /* 0x00: "KEY " */
- uint8_t ver[4]; /* 0x04: "V1 " */
- uint32_t bifcnt; /* 0x08: Count of BIF entries */
- uint32_t rescnt; /* 0x0c: Count of resource entries */
- uint32_t bifoff; /* 0x10: Offest to BIF entries */
- uint32_t resoff; /* 0x14: Offest to resource entries */
- } __attribute__((packed));
- struct bif_header {
- uint32_t biflen; /* 0x00: Length of BIF file */
- uint32_t fileoff; /* 0x04: Offset to ASCIIZ BIF filename */
- uint16_t filelen; /* 0x08: Length of ASCIIZ BIF filename */
- uint16_t location; /* 0x0a: Location of the file */
- } __attribute__((packed));
- struct res_header {
- uint8_t resname[8]; /* 0x00: Resource name */
- uint16_t restype; /* 0x08: Resource type */
- uint32_t locator; /* 0x0a: Resource locator */
- } __attribute__((packed));
- struct chitin {
- struct key_header header;
- struct bif_header *bif;
- struct res_header *res;
- char *name;
- size_t size;
- };
- struct chitin *init_chitin(void)
- {
- struct chitin *chitin;
- chitin = calloc(1, sizeof *chitin);
- if (chitin == NULL) {
- errno = -ENOMEM;
- return NULL;
- }
- chitin->bif = NULL;
- chitin->res = NULL;
- chitin->name = NULL;
- return chitin;
- }
- void free_chitin(struct chitin *chitin)
- {
- free(chitin->name);
- free(chitin->bif);
- free(chitin->res);
- free(chitin);
- }
- int read_key(const char *filename, struct chitin *chitin)
- {
- FILE *f;
- struct stat st;
- int r;
- int i, j;
- int len;
- int off;
- char buf[512];
- errno = 0;
- if ((filename == NULL) || (chitin == NULL)) {
- errno = -EFAULT;
- goto out;
- }
- f = fopen(filename, "r+b");
- if (f == NULL) {
- errno = -ENOENT;
- goto out;
- }
- r = fstat(fileno(f), &st);
- if (r < 0) {
- errno = -ENOENT;
- goto fc;
- }
- r = fread(&chitin->header, sizeof chitin->header, 1, f);
- if (r < 1) {
- errno = -ENODATA;
- goto fc;
- }
- r = memcmp(chitin->header.sig, "KEY ", 4);
- if (r != 0) {
- errno = -EINVAL;
- goto fc;
- }
- r = memcmp(chitin->header.ver, "V1 ", 4);
- if (r != 0) {
- errno = -EINVAL;
- goto fc;
- }
- chitin->bif = calloc(chitin->header.bifcnt, sizeof *chitin->bif);
- if (chitin->bif == NULL) {
- errno = -ENOMEM;
- goto fc;
- }
- fseek(f, chitin->header.bifoff, SEEK_SET);
- r = fread(chitin->bif, sizeof *chitin->bif, chitin->header.bifcnt, f);
- if (r < chitin->header.bifcnt) {
- errno = -ENODATA;
- goto fc;
- }
- for (i = 0; i < chitin->header.bifcnt; i++) {
- off = chitin->bif[i].fileoff;
- len = chitin->bif[i].filelen;
- fseek(f, off, SEEK_SET);
- fread(buf, 1, len, f);
- for (j = 0; j < len - 1; j++) {
- if isupper(buf[j]) {
- buf[j] = tolower(buf[j]);
- }
- }
- fseek(f, off, SEEK_SET);
- fwrite(buf, 1, len, f);
- }
- fseek(f, chitin->header.resoff, SEEK_SET);
- chitin->res = calloc(chitin->header.rescnt, sizeof *chitin->res);
- if (chitin->res == NULL) {
- errno = -ENOMEM;
- goto fc;
- }
- r = fread(chitin->res, sizeof *chitin->res, chitin->header.rescnt, f);
- if (r < chitin->header.rescnt) {
- errno = -ENODATA;
- goto fc;
- }
- for (i = 0; i < chitin->header.rescnt; i++) {
- for (j = 0; j < 8; j++) {
- if (isupper(chitin->res[i].resname[j])) {
- chitin->res[i].resname[j] = tolower(chitin->res[i].resname[j]);
- }
- }
- }
- fseek(f, chitin->header.resoff, SEEK_SET);
- r = fwrite(chitin->res, sizeof *chitin->res, chitin->header.rescnt, f);
- chitin->name = strdup(filename);
- fc:
- fclose(f);
- out:
- return errno;
- }
- int main(int argc, char *argv[])
- {
- int i, j;
- int r;
- struct chitin *chitin;
- if (argc != 2)
- return -1;
- chitin = init_chitin();
- read_key(argv[1], chitin);
- if (chitin) {
- printf("Processing chitin %s - %s\n", argv[1], chitin->name);
- printf("Found %d BIF entries\n", chitin->header.bifcnt);
- printf("Found %d resource entries\n", chitin->header.rescnt);
- }
- free_chitin(chitin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement