Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * UnpackAC.c
- * by Reisyukaku
- *
- * Unpacks files from AC/AS container (quick and dirty).
- */
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- #if defined(_WIN32)
- #include <direct.h>
- #else
- #include <sys/stat.h>
- #endif
- typedef uint8_t u8;
- typedef uint16_t u16;
- typedef uint32_t u32;
- typedef struct{
- u32 startAddr;
- } Entry;
- typedef struct{
- u16 magic;
- u16 entryCnt;
- } Header;
- void makeDir(const char* name) {
- #if defined(_WIN32)
- _mkdir(name);
- #else
- mkdir(name, 777);
- #endif
- }
- char *magicToExt(u32 magic){
- if(magic == 0x4D43) return ".cm";
- return ".bin";
- }
- int main(int argc, char **argv){
- u32 currPos = 0;
- char currFolder[8];
- char fullPath[0x20];
- u8 *fileBuffer;
- if(argc != 2){
- printf("Usage: %s <AS Package>\n", argv[0]);
- return -1;
- }
- //Open
- FILE *fpIn, *fpOut;
- fpIn = fopen(argv[1], "rb");
- if(!fpIn){printf("File not found!\n"); return -1;}
- //Read AC header
- Header acHead;
- fread(&acHead, sizeof(Header), 1, fpIn);
- Entry entries[acHead.entryCnt];
- fread(&entries, sizeof(Entry) * (acHead.entryCnt+1), 1, fpIn);
- if(acHead.magic != 0x4341 && acHead.magic != 0x5341){printf("Not and AC or AS container!\n"); goto final;}
- printf("Entries: %d\n\n", acHead.entryCnt);
- u32 i; for(i = 0; i < acHead.entryCnt; i++){
- currPos = entries[i].startAddr;
- fseek(fpIn, currPos, SEEK_SET);
- printf("Start: %08X\n\n", currPos);
- //Read CP headers
- Header cp;
- fread(&cp, sizeof(Header), 1, fpIn);
- Entry cpEntries[cp.entryCnt];
- fread(cpEntries, sizeof(Entry) * (cp.entryCnt+1), 1, fpIn);
- if(cp.magic != 0x5043 && cp.magic != 0x4D53){printf("Not CP or SM header!\n"); goto final;}
- snprintf(currFolder, 8, "%08X", currPos);
- makeDir(currFolder);
- printf("\tEntries: %d\n\n", cp.entryCnt);
- u32 j; for(j = 0; j < cp.entryCnt; j++){
- currPos = entries[i].startAddr + cpEntries[j].startAddr;
- u32 endPos = entries[i].startAddr + cpEntries[j+1].startAddr;
- fseek(fpIn, currPos, SEEK_SET);
- printf("\tStart: %08X\n", currPos);
- //Obtain info about file
- size_t size = endPos - currPos;
- fileBuffer = malloc(size);
- u32 fileMagic = 0;
- fread(&fileMagic, 2, 1, fpIn);
- fseek(fpIn, currPos, SEEK_SET);
- memset(fullPath, 0, 22);
- snprintf(fullPath, 0x20, "%s/%08X%s", currFolder, cpEntries[j].startAddr, magicToExt(fileMagic));
- printf("\tSize: %08X\n\tPath: %s\n\n", size, fullPath);
- //Write out file
- fpOut = fopen(fullPath, "wb");
- fread(fileBuffer, size, 1, fpIn);
- fwrite(fileBuffer, size, 1, fpOut);
- fclose(fpOut);
- free(fileBuffer);
- }
- }
- final:;
- fclose(fpIn);
- return 0;
- }
Add Comment
Please, Sign In to add comment