ES-2024 reverter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <arpa/inet.h>
  7. #include "filedata.h"
  8. #include "romfile.h"
  9. struct romfile_header {
  10. uint16_t version;
  11. uint16_t size;
  12. uint16_t offset;
  13. char name[14];
  14. } __attribute__((packed));
  15. struct romfile * get_romfile(const char *filename, const char *romfile_name)
  16. {
  17. struct romfile *romfile = malloc(sizeof(struct romfile));
  18. if (romfile == NULL)
  19. return NULL;
  20. romfile->file = get_filedata(filename);
  21. if (romfile->file == NULL) {
  22. free(romfile);
  23. return NULL;
  24. }
  25. romfile->baseoffset = 0x2000;
  26. struct romfile_header header;
  27. while (1) {
  28. memcpy(&header, (void *)(romfile->file->data) + romfile->baseoffset, sizeof(header));
  29. if (strcmp(header.name, romfile_name) == 0) {
  30. romfile->data = (void *)(romfile->file->data) + 0x2000 + htons(header.offset);
  31. romfile->size = htons(header.size);
  32. return romfile;
  33. }
  34. if (header.name[0] == 0 || header.name[0] == -1) {
  35. free(romfile->file);
  36. free(romfile);
  37. return NULL;
  38. }
  39. romfile->baseoffset += sizeof(header);
  40. }
  41. }
  42. int put_romfile(const char *filename, struct romfile *romfile)
  43. {
  44. struct romfile_header header;
  45. memcpy(&header, (void *)(romfile->file->data) + romfile->baseoffset, sizeof(header));
  46. header.size = htons(romfile->size);
  47. memcpy((void *)(romfile->file->data) + romfile->baseoffset, &header, sizeof(header));
  48. return put_filedata(filename, romfile->file);
  49. }