home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 14454 / rpe-extractor.7z / rpe-extractor.c next >
Encoding:
C/C++ Source or Header  |  2018-06-09  |  2.6 KB  |  95 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5.  
  6. #define ERR_OK 0
  7. #define ERR_FAIL -1
  8. #define ERR_INVALID_ARGS -2
  9. #define ERR_INVALID_OPTS -3
  10.  
  11. #define RPE_BUFF_LEN 32
  12. #define RPE_ID_LEN 32
  13.  
  14. typedef union {
  15.     struct {
  16.         uint32_t id;
  17.         uint32_t count;
  18.     };
  19.     char raw[RPE_BUFF_LEN];
  20. } rpe_header_t;
  21.  
  22. typedef struct {
  23.     char id[RPE_ID_LEN];
  24.     uint32_t offset;
  25.     uint32_t size;
  26.     float unknown;    // seems to be a floating point value of some sort, maybe duration as it is quite small
  27. } rpe_file_header_t;
  28.  
  29. char* readWholeFile(char* fpath) {
  30.     FILE* rpe = fopen(fpath, "r");
  31.     fseek(rpe, 0, SEEK_END);
  32.     int32_t rpe_len = ftell(rpe);
  33.     char* rpe_data = (char*)malloc(rpe_len);
  34.     fseek(rpe, 0, SEEK_SET);
  35.     fread(rpe_data, 1, rpe_len, rpe);
  36.     fclose(rpe);
  37.     return rpe_data;
  38. }
  39.  
  40. void extractFile(rpe_file_header_t* finfo, char* rpe_stream) {
  41.     char fpath[RPE_ID_LEN + 4];
  42.     strncpy(fpath, finfo->id, RPE_ID_LEN);
  43.     int32_t flen = strlen(fpath);
  44.     strncpy(&fpath[flen], ".wav", 5);
  45.  
  46.     FILE* out = fopen(fpath, "w");
  47.     fwrite(rpe_stream + finfo->offset, 1, finfo->size, out);
  48.     fclose(out);
  49. }
  50.  
  51. int32_t extractRPE(char* rpe_path) {
  52.     char* rpe_data = readWholeFile(rpe_path);
  53.     rpe_header_t* rpe_header = (rpe_header_t*)rpe_data;
  54.     rpe_file_header_t* rpe_file_header_table = (rpe_file_header_t*)(rpe_data + sizeof(rpe_header_t));
  55.     for (int32_t i = 0; i < rpe_header->count; ++i) {
  56.         extractFile(&rpe_file_header_table[i], rpe_data);
  57.     }
  58.     return ERR_OK;
  59. }
  60.  
  61. int32_t createRPE(char* rpe_path) {
  62.     printf("NOT YET IMPLEMENTED!\n");
  63.     return ERR_FAIL;
  64. }
  65.  
  66. void printUsage() {
  67.     printf("    Usage:\n");
  68.     printf("    rpe-extractor <options> <rpe_path>\n");
  69.     printf("\n");
  70.     printf("    Options:\n");
  71.     printf("    -x => extract data from .rpe container to folder with same name\n");
  72.     printf("    -c => create new .rpe container from data in folder with same name without .rpe extension\n");
  73.     printf("\n");
  74. }
  75.  
  76. int32_t main(int32_t argc, char** argv) {
  77.     if ((argc < 2) || (argc > 3)) {
  78.         printf("Invalid number of arguments!\n");
  79.         printUsage();
  80.         return ERR_INVALID_ARGS;
  81.     }
  82.  
  83.     switch (argv[1][1]) {
  84.         case 'x':
  85.             return extractRPE(argv[2]);
  86.         case 'c':
  87.             return createRPE(argv[2]);
  88.         default:
  89.             printf("Invalid option \"-%c\"!\n", argv[1][1]);
  90.             printUsage();
  91.             return ERR_INVALID_ARGS;
  92.     }
  93.  
  94.     return ERR_OK;
  95. }