Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * tgaCkd.c
- * by Reisyukaku
- *
- * Simple crude parser for CKD format used by Nintendo switch.
- */
- #include <stdio.h>
- #include <malloc.h>
- #include <stdint.h>
- #include <string.h>
- typedef uint32_t u32;
- typedef uint16_t u16;
- typedef uint8_t u8;
- //Format is similar to Gfx2
- typedef struct{
- u32 magic;
- char formatMagic[4];
- u32 hdrLength;
- u8 unk[0x20]; //*0x20 unk bytes*
- }CkdTexHead; //For some reason most values are big endian
- typedef struct{
- char magic[4];
- u32 hdrLength;
- u32 unk1;
- u32 unk2;
- } DfvnHead;
- typedef struct{
- char magic[4];
- u32 hdrLength;
- u32 dataLength;
- u32 unk1;
- u32 dataOffset;
- u32 unk2;
- u32 dataType; //maybe??? (2=image descriptor? 3=raw data?)
- u32 unk3;
- u32 unk4;
- } HbvnHead;
- //descriptor thingy:
- //u32 imageSize
- //u32 unk
- //u32 length
- //u32 width
- //...
- u32 swapEndian(u32 num){
- return ((num>>0x18)&0xff)|((num<<8)&0xff0000)|((num>>8)&0xff00)|((num<<0x18)&0xff000000);
- }
- int main(int argc, char **argv){
- if(argc != 2){
- printf("Usage: %s <file>\n", argv[0]);
- return 1;
- }
- //Open file
- FILE *fp;
- fp = fopen(argv[1], "rb");
- if(!fp){
- printf("File not found!\n");
- return 1;
- }
- fseek(fp, 0, SEEK_END);
- size_t fileSize = ftell(fp);
- rewind(fp);
- //Parse
- CkdTexHead *ckd = malloc(sizeof(CkdTexHead));
- DfvnHead *dfvn = malloc(sizeof(DfvnHead));
- fread(ckd, 1, sizeof(CkdTexHead), fp);
- if(swapEndian(ckd->magic) != 9){
- printf("Incorrect format!\n");
- return 1;
- }
- fseek(fp, swapEndian(ckd->hdrLength), SEEK_SET);
- fread(dfvn, 1, sizeof(DfvnHead), fp);
- u32 offset = swapEndian(ckd->hdrLength) + dfvn->hdrLength;
- printf(
- "CKD format parser\n"
- "----------------\n"
- "Format: %s\n\n",
- ckd->formatMagic
- );
- HbvnHead *hbvn;
- while(1){
- //Parse HBvN
- hbvn = malloc(sizeof(HbvnHead));
- fread(hbvn, 1, sizeof(HbvnHead), fp);
- char magic[5] = {0};
- memcpy(magic, hbvn->magic, 4);
- u32 magicOffset = offset;
- size_t chunkSize = hbvn->dataLength + hbvn->dataOffset;
- if()
- printf(
- "Magic: %s (@ 0x%X)\n"
- "Header length: 0x%X\n"
- "Data Length: 0x%X\n"
- "Data offset: 0x%X\n"
- "Total chunk size: 0x%X\n\n",
- magic, magicOffset,
- hbvn->hdrLength,
- hbvn->dataOffset,
- hbvn->dataLength,
- chunkSize
- );
- free(hbvn);
- //Break if all chunks have been read
- offset += chunkSize;
- if(offset >= fileSize) break;
- fseek(fp, offset, SEEK_SET);
- }
- //Cleanup
- fclose(fp);
- free(ckd);
- free(dfvn);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement