Advertisement
Guest User

Lowercase for chitin.key

a guest
Dec 13th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <inttypes.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <ctype.h>
  8.  
  9. struct key_header {
  10.     uint8_t sig[4];        /* 0x00: "KEY " */
  11.     uint8_t ver[4];        /* 0x04: "V1  " */
  12.     uint32_t bifcnt;       /* 0x08: Count of BIF entries */
  13.     uint32_t rescnt;       /* 0x0c: Count of resource entries */
  14.     uint32_t bifoff;       /* 0x10: Offest to BIF entries */
  15.     uint32_t resoff;       /* 0x14: Offest to resource entries */
  16. } __attribute__((packed));
  17.  
  18. struct bif_header {
  19.     uint32_t biflen;       /* 0x00: Length of BIF file */
  20.     uint32_t fileoff;      /* 0x04: Offset to ASCIIZ BIF filename */
  21.     uint16_t filelen;      /* 0x08: Length of ASCIIZ BIF filename */
  22.     uint16_t location;     /* 0x0a: Location of the file */
  23. } __attribute__((packed));
  24.  
  25. struct res_header {
  26.     uint8_t resname[8];    /* 0x00: Resource name */
  27.     uint16_t restype;      /* 0x08: Resource type */
  28.     uint32_t locator;      /* 0x0a: Resource locator */
  29. } __attribute__((packed));
  30.  
  31. struct chitin {
  32.     struct key_header header;
  33.     struct bif_header *bif;
  34.     struct res_header *res;
  35.     char *name;
  36.     size_t size;
  37. };
  38.  
  39.  
  40. struct chitin *init_chitin(void)
  41. {
  42.  
  43.     struct chitin *chitin;
  44.  
  45.     chitin = calloc(1, sizeof *chitin);
  46.     if (chitin == NULL) {
  47.         errno = -ENOMEM;
  48.         return NULL;
  49.     }
  50.  
  51.     chitin->bif = NULL;
  52.     chitin->res = NULL;
  53.     chitin->name = NULL;
  54.  
  55.     return chitin;
  56. }
  57.  
  58. void free_chitin(struct chitin *chitin)
  59. {
  60.     free(chitin->name);
  61.     free(chitin->bif);
  62.     free(chitin->res);
  63.     free(chitin);
  64. }
  65.  
  66. int read_key(const char *filename, struct chitin *chitin)
  67. {
  68.     FILE *f;
  69.     struct stat st;
  70.     int r;
  71.     int i, j;
  72.     int len;
  73.     int off;
  74.     char buf[512];
  75.  
  76.     errno = 0;
  77.  
  78.     if ((filename == NULL) || (chitin == NULL)) {
  79.         errno = -EFAULT;
  80.         goto out;
  81.     }
  82.     f = fopen(filename, "r+b");
  83.     if (f == NULL) {
  84.         errno = -ENOENT;
  85.         goto out;
  86.     }
  87.     r = fstat(fileno(f), &st);
  88.     if (r < 0) {
  89.         errno = -ENOENT;
  90.         goto fc;
  91.     }
  92.  
  93.     r = fread(&chitin->header, sizeof chitin->header, 1, f);
  94.     if (r < 1) {
  95.         errno = -ENODATA;
  96.         goto fc;
  97.     }
  98.  
  99.     r = memcmp(chitin->header.sig, "KEY ", 4);
  100.     if (r != 0) {
  101.         errno = -EINVAL;
  102.         goto fc;
  103.     }
  104.     r = memcmp(chitin->header.ver, "V1  ", 4);
  105.     if (r != 0) {
  106.         errno = -EINVAL;
  107.         goto fc;
  108.     }
  109.  
  110.     chitin->bif = calloc(chitin->header.bifcnt, sizeof *chitin->bif);
  111.     if (chitin->bif == NULL) {
  112.         errno = -ENOMEM;
  113.         goto fc;
  114.     }
  115.     fseek(f, chitin->header.bifoff, SEEK_SET);
  116.     r = fread(chitin->bif, sizeof *chitin->bif, chitin->header.bifcnt, f);
  117.     if (r < chitin->header.bifcnt) {
  118.         errno = -ENODATA;
  119.         goto fc;
  120.     }
  121.  
  122.     for (i = 0; i < chitin->header.bifcnt; i++) {
  123.         off = chitin->bif[i].fileoff;
  124.         len = chitin->bif[i].filelen;
  125.         fseek(f, off, SEEK_SET);
  126.         fread(buf, 1, len, f);
  127.         for (j = 0; j < len - 1; j++) {
  128.             if isupper(buf[j]) {
  129.                 buf[j] = tolower(buf[j]);
  130.             }
  131.         }
  132.         fseek(f, off, SEEK_SET);
  133.         fwrite(buf, 1, len, f);
  134.     }
  135.  
  136.     fseek(f, chitin->header.resoff, SEEK_SET);
  137.     chitin->res = calloc(chitin->header.rescnt, sizeof *chitin->res);
  138.     if (chitin->res == NULL) {
  139.         errno = -ENOMEM;
  140.         goto fc;
  141.     }
  142.     r = fread(chitin->res, sizeof *chitin->res, chitin->header.rescnt, f);
  143.     if (r < chitin->header.rescnt) {
  144.         errno = -ENODATA;
  145.         goto fc;
  146.     }
  147.  
  148.     for (i = 0; i < chitin->header.rescnt; i++) {
  149.         for (j = 0; j < 8; j++) {
  150.             if (isupper(chitin->res[i].resname[j])) {
  151.                 chitin->res[i].resname[j] = tolower(chitin->res[i].resname[j]);
  152.             }
  153.         }
  154.     }
  155.     fseek(f, chitin->header.resoff, SEEK_SET);
  156.     r = fwrite(chitin->res, sizeof *chitin->res, chitin->header.rescnt, f);
  157.  
  158.     chitin->name = strdup(filename);
  159.  
  160. fc:
  161.     fclose(f);
  162. out:
  163.     return errno;
  164. }
  165.  
  166. int main(int argc, char *argv[])
  167. {
  168.     int i, j;
  169.     int r;
  170.     struct chitin *chitin;
  171.    
  172.     if (argc != 2)
  173.         return -1;
  174.  
  175.     chitin = init_chitin();
  176.  
  177.     read_key(argv[1], chitin);
  178.  
  179.     if (chitin) {
  180.         printf("Processing chitin %s - %s\n", argv[1], chitin->name);
  181.         printf("Found %d BIF entries\n", chitin->header.bifcnt);
  182.         printf("Found %d resource entries\n", chitin->header.rescnt);
  183.     }
  184.  
  185.     free_chitin(chitin);
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement