Advertisement
Guest User

FQRb - MPEG1 Overwrite

a guest
Jan 19th, 2021
265
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <cstdint>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <sys/stat.h>
  6.  
  7. const unsigned char audioSequence[] =
  8. {
  9.     0, 0, 1, 0xC0
  10. };
  11.  
  12. const char  fileName[]  = "OPENING.SFD",
  13.             audioName[] = "OPENING.ADX";
  14. uint32_t    fileSize, audioOffset   = 0;
  15. FILE        *audioFp;
  16.  
  17. /* Function to open a file in read mode.
  18.  */
  19. FILE *getFile(const char *filename)
  20. {
  21.     FILE    *fp = fopen(filename, "r+");
  22.     if  (fp == nullptr) exit(0);
  23.     return  fp;
  24. }
  25.  
  26. /* Function to get file size.
  27.  */
  28. unsigned int getSize(FILE *fp)
  29. {
  30.     struct  stat st;
  31.     fstat(  fileno(fp), &st);
  32.     return  st.st_size;
  33. }
  34.  
  35. /* Function that will replace an ADX block
  36.  * with its equivalent from another file.
  37.  */
  38. void replaceAudio(FILE *fp, uint32_t packsize)
  39. {
  40.     uint8_t *data = new uint8_t[packsize];
  41.     fseek(fp, 4 + 2 + 7 - 6, SEEK_CUR);
  42.     fread(data, packsize, 1, audioFp);
  43.     fwrite(data, packsize, 1, fp);
  44.     delete[] data;
  45. }
  46.  
  47. /* Simple function to locate an
  48.  * arbitrary byte sequence in a
  49.  * file.
  50.  */
  51. void searchSequence(FILE *fp, int currentOffset)
  52. {
  53.     unsigned char currentSequence[6] = { 0 };
  54.     unsigned int  packSize;
  55.  
  56.     fseek(fp, currentOffset, SEEK_SET);
  57.     fread(&currentSequence, sizeof currentSequence, 1, fp);
  58.     packSize = (currentSequence[4] << 8) | currentSequence[5];
  59.  
  60.     if( !memcmp(
  61.     audioSequence,
  62.     currentSequence,
  63.     sizeof audioSequence
  64.     ) && packSize <= 0x07E7)
  65.     {
  66.         replaceAudio(fp, packSize - 7);
  67.     }
  68. }
  69.  
  70. int main()
  71. {
  72.     audioFp  = getFile(audioName);
  73.     FILE *fp = getFile(fileName);
  74.     fileSize = getSize(fp);
  75.  
  76.     // ¯\_(ツ)_/¯
  77.     for( unsigned int i = 0; i < fileSize - 4 - 1; i++)
  78.         searchSequence(fp, i);
  79.  
  80.     return 0;
  81. }
Advertisement
RAW Paste Data Copied
Advertisement