Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdint>
- #include <cstdlib>
- #include <cstring>
- #include <sys/stat.h>
- const unsigned char audioSequence[] =
- {
- 0, 0, 1, 0xC0
- };
- const char fileName[] = "OPENING.SFD",
- audioName[] = "OPENING.ADX";
- uint32_t fileSize, audioOffset = 0;
- FILE *audioFp;
- /* Function to open a file in read mode.
- */
- FILE *getFile(const char *filename)
- {
- FILE *fp = fopen(filename, "r+");
- if (fp == nullptr) exit(0);
- return fp;
- }
- /* Function to get file size.
- */
- unsigned int getSize(FILE *fp)
- {
- struct stat st;
- fstat( fileno(fp), &st);
- return st.st_size;
- }
- /* Function that will replace an ADX block
- * with its equivalent from another file.
- */
- void replaceAudio(FILE *fp, uint32_t packsize)
- {
- uint8_t *data = new uint8_t[packsize];
- fseek(fp, 4 + 2 + 7 - 6, SEEK_CUR);
- fread(data, packsize, 1, audioFp);
- fwrite(data, packsize, 1, fp);
- delete[] data;
- }
- /* Simple function to locate an
- * arbitrary byte sequence in a
- * file.
- */
- void searchSequence(FILE *fp, int currentOffset)
- {
- unsigned char currentSequence[6] = { 0 };
- unsigned int packSize;
- fseek(fp, currentOffset, SEEK_SET);
- fread(¤tSequence, sizeof currentSequence, 1, fp);
- packSize = (currentSequence[4] << 8) | currentSequence[5];
- if( !memcmp(
- audioSequence,
- currentSequence,
- sizeof audioSequence
- ) && packSize <= 0x07E7)
- {
- replaceAudio(fp, packSize - 7);
- }
- }
- int main()
- {
- audioFp = getFile(audioName);
- FILE *fp = getFile(fileName);
- fileSize = getSize(fp);
- // ¯\_(ツ)_/¯
- for( unsigned int i = 0; i < fileSize - 4 - 1; i++)
- searchSequence(fp, i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement