From a50ba4573c91cad2d95a2055ccd4133c17eeecb2 Mon Sep 17 00:00:00 2001 From: megazig Date: Wed, 25 Mar 2009 10:03:36 -0500 Subject: fixed a ton more brlyt.c --- brlyt.c | 1040 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 550 insertions(+), 490 deletions(-) (limited to 'brlyt.c') diff --git a/brlyt.c b/brlyt.c index 2c4fb39..7af7a04 100644 --- a/brlyt.c +++ b/brlyt.c @@ -118,7 +118,6 @@ unsigned int bit_extract(unsigned int num, unsigned int start, unsigned int end) } mask = firstMask = secondMask; int ret = (num & mask) >> (31 - end); - //printf("%08x, %08x, %08x, %08x\n", firstMask, secondMask, mask, ret); return ret; } @@ -126,18 +125,28 @@ char* getMaterial(int offset) { if (offset == 0) return materials; - char *foo = materials + strlen(materials) + 1; + char *mat = materials + strlen(materials) + 1; if (offset > 1) { int n = 1; - for (n; n%08x-%08x\n", data.flag1, data.flag2); printf(" %08x-%08x\n", data.alpha, data.alpha2); printf(" \n"); - printf(" %f\n", be32(data.x)); - printf(" %f\n", be32(data.y)); - printf(" %f\n", be32(data.z)); + printf(" %f\n", float_swap_bytes(data.x)); + printf(" %f\n", float_swap_bytes(data.y)); + printf(" %f\n", float_swap_bytes(data.z)); printf(" \n"); printf(" \n"); printf(" %f\n", float_swap_bytes(data.flip_x)); @@ -381,9 +389,9 @@ void PrintBRLYTEntry_wnd1(brlyt_entry entry, u8* brlyt_file) printf(" alpha: %08x\n", data.alpha); printf(" alpha2: %08x\n", data.alpha2); printf(" name: %s\n", data.name); - printf(" x: %f\n", be32(data.x)); - printf(" y: %f\n", be32(data.y)); - printf(" z: %f\n", be32(data.z)); + printf(" x: %f\n", float_swap_bytes(data.x)); + printf(" y: %f\n", float_swap_bytes(data.y)); + printf(" z: %f\n", float_swap_bytes(data.z)); printf(" flip_x: %f\n", float_swap_bytes(data.flip_x)); printf(" flip_y: %f\n", float_swap_bytes(data.flip_y)); printf(" angle: %f\n", float_swap_bytes(data.angle)); @@ -396,9 +404,9 @@ void PrintBRLYTEntry_wnd1(brlyt_entry entry, u8* brlyt_file) printf(" %08x-%08x\n", data.flag1, data.flag2); printf(" %08x-%08x\n", data.alpha, data.alpha2); printf(" \n"); - printf(" %f\n", be32(data.x)); - printf(" %f\n", be32(data.y)); - printf(" %f\n", be32(data.z)); + printf(" %f\n", float_swap_bytes(data.x)); + printf(" %f\n", float_swap_bytes(data.y)); + printf(" %f\n", float_swap_bytes(data.z)); printf(" \n"); printf(" \n"); printf(" %f\n", float_swap_bytes(data.flip_x)); @@ -433,9 +441,9 @@ void PrintBRLYTEntry_bnd1(brlyt_entry entry, u8* brlyt_file) printf(" alpha: %08x\n", data.alpha); printf(" alpha2: %08x\n", data.alpha2); printf(" name: %s\n", data.name); - printf(" x: %f\n", be32(data.x)); - printf(" y: %f\n", be32(data.y)); - printf(" z: %f\n", be32(data.z)); + printf(" x: %f\n", float_swap_bytes(data.x)); + printf(" y: %f\n", float_swap_bytes(data.y)); + printf(" z: %f\n", float_swap_bytes(data.z)); printf(" flip_x: %f\n", float_swap_bytes(data.flip_x)); printf(" flip_y: %f\n", float_swap_bytes(data.flip_y)); printf(" angle: %f\n", float_swap_bytes(data.angle)); @@ -448,9 +456,9 @@ void PrintBRLYTEntry_bnd1(brlyt_entry entry, u8* brlyt_file) printf(" %08x-%08x\n", data.flag1, data.flag2); printf(" %08x-%08x\n", data.alpha, data.alpha2); printf(" \n"); - printf(" %f\n", be32(data.x)); - printf(" %f\n", be32(data.y)); - printf(" %f\n", be32(data.z)); + printf(" %f\n", float_swap_bytes(data.x)); + printf(" %f\n", float_swap_bytes(data.y)); + printf(" %f\n", float_swap_bytes(data.z)); printf(" \n"); printf(" \n"); printf(" %f\n", float_swap_bytes(data.flip_x)); @@ -483,9 +491,9 @@ void PrintBRLYTEntry_pic1(brlyt_entry entry, u8* brlyt_file) printf(" alpha: %08x\n", data.alpha); printf(" alpha2: %08x\n", data.alpha2); printf(" name: %s\n", data.name); - printf(" x: %f\n", be32(data.x)); - printf(" y: %f\n", be32(data.y)); - printf(" z: %f\n", be32(data.z)); + printf(" x: %f\n", float_swap_bytes(data.x)); + printf(" y: %f\n", float_swap_bytes(data.y)); + printf(" z: %f\n", float_swap_bytes(data.z)); printf(" flip_x: %f\n", float_swap_bytes(data.flip_x)); printf(" flip_y: %f\n", float_swap_bytes(data.flip_y)); printf(" angle: %f\n", float_swap_bytes(data.angle)); @@ -498,9 +506,9 @@ void PrintBRLYTEntry_pic1(brlyt_entry entry, u8* brlyt_file) printf(" %08x-%08x\n", data.flag1, data.flag2); printf(" %08x-%08x\n", data.alpha, data.alpha2); printf(" \n"); - printf(" %f\n", be32(data.x)); - printf(" %f\n", be32(data.y)); - printf(" %f\n", be32(data.z)); + printf(" %f\n", float_swap_bytes(data.x)); + printf(" %f\n", float_swap_bytes(data.y)); + printf(" %f\n", float_swap_bytes(data.z)); printf(" \n"); printf(" \n"); printf(" %f\n", float_swap_bytes(data.flip_x)); @@ -522,6 +530,7 @@ void PrintBRLYTEntry_pic1(brlyt_entry entry, u8* brlyt_file) printf(" num_texcoords: %08x\n", data2.num_texcoords); printf(" padding: %08x\n", data2.padding); #else + printf(" \n", getMaterial(be16(data2.mat_off))); printf(" \n"); printf(" 0x%08X\n", be32(data2.vtx_colors[0])); printf(" 0x%08X\n", be32(data2.vtx_colors[1])); @@ -533,7 +542,7 @@ void PrintBRLYTEntry_pic1(brlyt_entry entry, u8* brlyt_file) int n = 0; for (n;n%08x-%08x\n", data.flag1, data.flag2); printf(" %08x-%08x\n", data.alpha, data.alpha2); printf(" \n"); - printf(" %f\n", be32(data.x)); - printf(" %f\n", be32(data.y)); - printf(" %f\n", be32(data.z)); + printf(" %f\n", float_swap_bytes(data.x)); + printf(" %f\n", float_swap_bytes(data.y)); + printf(" %f\n", float_swap_bytes(data.z)); printf(" \n"); printf(" \n"); printf(" %f\n", float_swap_bytes(data.flip_x)); @@ -637,7 +646,6 @@ void PrintBRLYTEntry_mat1(brlyt_entry entry, u8* brlyt_file) printf(" offs: %08x\n", be16(data.offs)); #else printf("type=\"%c%c%c%c\" count=\"%d\">\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3], be16(data.num)); -// printf(" \n"); printf(" %08x\n", data4.unk[0]); printf(" %08x\n", data4.unk[1]); printf(" %08x\n", data4.unk[2]); printf(" %08x\n", data4.unk[3]); + printf(" %08x\n", data4.unk[4]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 4*flags[20-23], followed by n = 0; for (n;n%08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# Changing ua3 things //# 1st --> disappears. @@ -751,7 +754,6 @@ void PrintBRLYTEntry_mat1(brlyt_entry entry, u8* brlyt_file) for (n;n%08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 4 * flags[4] n = 0; for (n;n%08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 4 * flags[19] n = 0; for (n;n%08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } n = 0; for (n;n%08x\n", be32(data4.e)); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 4 * flags[14-16] n = 0; for (n;n%08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 0x10 * flags[9-13] n = 0; for (n;n%08x\n", data4.unk[14]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 4 * flags[8], these are bytes btw n = 0; - for (n;n\n"); printf(" %08x\n", data4.unk[0]); printf(" %08x\n", data4.unk[1]); - printf(" %08x\n", data4.unk[2]); + printf(" %08x/\n", data4.unk[2]); printf(" %08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } //# 4 * flags[7] n = 0; - for (n;n%08x\n", data4.unk[3]); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE - //pos += item_size; } -// if n < vars['num'] - 1 -// { -// next_offset, = struct.unpack('>I', chunk[pos+4:pos+8]) -// if next_offset - 8 != mpos: -// mat['~_insane'] = next_offset - 8 - mpos //# Extra shit we di dn't parse :( -// } -// mat['unk_bit_5'] = bit_extract(flags, 5) -// mat['unk_bits_0_3'] = bit_extract(flags, 0, 3) //# Overwritten by stu ff -// vars['materials'].append(mat) -// pos += 4 #ifndef OLD_BRLYT_OUTSTYLE printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE BRLYT_fileoffset = tempDataLocation; } #ifndef OLD_BRLYT_OUTSTYLE -// printf(" \n"); printf(" \n"); #endif //OLD_BRLYT_OUTSTYLE } @@ -941,7 +917,6 @@ void PrintBRLYTEntry_gre1(brlyt_entry entry, u8* brlyt_file) #else printf("type=\"%c%c%c%c\" />\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3]); #endif //OLD_BRLYT_OUTSTYLE - //group end info } void PrintBRLYTEntry_grs1(brlyt_entry entry, u8* brlyt_file) @@ -951,7 +926,6 @@ void PrintBRLYTEntry_grs1(brlyt_entry entry, u8* brlyt_file) #else printf("type=\"%c%c%c%c\" />\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3]); #endif //OLD_BRLYT_OUTSTYLE - //group start info } void PrintBRLYTEntry_pae1(brlyt_entry entry, u8* brlyt_file) @@ -961,7 +935,6 @@ void PrintBRLYTEntry_pae1(brlyt_entry entry, u8* brlyt_file) #else printf("type=\"%c%c%c%c\" />\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3]); #endif //OLD_BRLYT_OUTSTYLE - //panel end info } void PrintBRLYTEntry_pas1(brlyt_entry entry, u8* brlyt_file) @@ -971,7 +944,6 @@ void PrintBRLYTEntry_pas1(brlyt_entry entry, u8* brlyt_file) #else printf("type=\"%c%c%c%c\" />\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3]); #endif //OLD_BRLYT_OUTSTYLE - //panel start info } void PrintBRLYTEntries(brlyt_entry *entries, int entrycnt, u8* brlyt_file) @@ -1085,7 +1057,7 @@ void parse_brlyt(char *filename) entries[i].data_location = BRLYT_fileoffset + sizeof(brlyt_entry_header); BRLYT_fileoffset += be32(tempentry.length); } -// int entrycnt = BRLYT_ReadEntries(brlyt_file, file_size, header, entries); + dbgprintf("%08x\n", entries); #ifdef OLD_BRLYT_OUTSTYLE printf("Parsed BRLYT! Information:\n"); @@ -1106,6 +1078,8 @@ void parse_brlyt(char *filename) printf("\n"); #endif //OLD_BRLYT_OUTSTYLE + free(materials); + } /* @@ -1269,10 +1243,9 @@ void create_tag_from_xml(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32 } */ -void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* blobsize, char temp[4]) +void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* blobsize, char temp[4], FILE* fp, unsigned int *fileOffset) { - - printf("temp holds: %c%c%c%c\t", temp[0], temp[1], temp[2], temp[3]); + unsigned int startOfChunk = *fileOffset; char lyt1[4] = {'l', 'y', 't', '1'}; // // char txl1[4] = {'t', 'x', 'l', '1'}; // // @@ -1292,18 +1265,14 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl if ( memcmp(temp, lyt1, sizeof(lyt1)) == 0) { brlyt_lytheader_chunk lytheader; - printf("found a lyt1\n"); + mxml_node_t *subnode = mxmlFindElement(node , node , "a", NULL, NULL, MXML_DESCEND); if (subnode != NULL) { - printf("an a value found\n"); char tempChar[4]; get_value(subnode, tempChar, 256); - //*(u32*)(&(data[x][i].part3)) = atoi(temp); - printf("temp holds: %s\n", tempChar); lytheader.a = atoi(tempChar); lytheader.pad[0]=0;lytheader.pad[1]=0;lytheader.pad[2]=0; - printf("a value: %08x\n", lytheader.a); } subnode = mxmlFindElement(node, node, "size", NULL, NULL, MXML_DESCEND); if (subnode != NULL) @@ -1315,75 +1284,131 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl get_value(valnode, tempChar, 256); float something; *(float*)(&(lytheader.width)) = atof(tempChar); - printf("width: %f\n", lytheader.width); } valnode = mxmlFindElement(subnode , subnode ,"height", NULL, NULL, MXML_DESCEND); if (valnode != NULL) { char tempChar[4]; get_value(valnode, tempChar, 256); - //*(f32*)(&(data[x][i].part3)) = atof(tempChar); - //float heightF = atof(tempChar); - //printf("height: %f\n", heightF); + *(float*)(&(lytheader.height)) = atof(tempChar); - printf("height: %f\n", lytheader.height); } } + lytheader.pad[0] = 0; lytheader.pad[1] = 0; lytheader.pad[2] = 0; + lytheader.width = float_swap_bytes(lytheader.width); + lytheader.height = float_swap_bytes(lytheader.height); + fwrite(&lytheader, sizeof(brlyt_header), 1, fp); + *fileOffset = *fileOffset + sizeof(lytheader); } if ( memcmp(temp, txl1, sizeof(txl1)) == 0) { - // create a numoffs for before txl names - // brlut_numoffs_chumk chunk; - // fwrite(chunk, f,f fp); - - //brlyt_group_chunk chunk; - //chunk.unk = 0; - printf("found a txl1\n"); - -// if(mxmlElementGetAttr(node, "name") != NULL) -// strcpy(temp, mxmlElementGetAttr(node, "name")); -// else{ -// printf("No name attribute found!\nQuitting!\n"); -// exit(1); -// } -// strcpy(chunk.name, temp); + int numoffsOffset = ftell(fp); + + brlyt_numoffs_chunk chunk; + chunk.num = 0; + chunk.offs = 0; + fwrite(&chunk, sizeof(chunk), 1, fp); + *fileOffset = *fileOffset + sizeof(chunk); + char *names; + names = malloc(sizeof(names)); + int lengthOfNames = 0; mxml_node_t *subnode = mxmlFindElement(node, node, "entries", NULL, NULL, MXML_DESCEND); if (subnode != NULL) { int numEntries = 0; - // do a for loop to get chunk.numsubs + + int oldNameLength; + int baseOffset = 0; + + u32 *offsunks; + + offsunks = malloc(sizeof(u32)); + brlyt_offsunk_chunk chunk2; mxml_node_t *valnode; for(valnode = mxmlFindElement(subnode, subnode, "name", NULL, NULL, MXML_DESCEND); valnode != NULL; valnode = mxmlFindElement(valnode, subnode, "name", NULL, NULL, MXML_DESCEND)) { if (valnode != NULL) { + char tempSub[256]; get_value(valnode, tempSub, 256); - - printf("name: %s\t", tempSub); + + oldNameLength = lengthOfNames; + lengthOfNames += strlen(tempSub); + //printf("name: %s\t", tempSub); + + offsunks = realloc(offsunks, (numEntries + 1)*(2 * sizeof(u32))); + if (offsunks == NULL) printf("NULLed by a realloc\n"); + + offsunks[(numEntries*2)+0] = baseOffset; + offsunks[(numEntries*2)+1] = 0; + + *fileOffset = *fileOffset + sizeof(chunk2); numEntries++; + + baseOffset += (strlen(tempSub) + 1); + + names = realloc(names, 1 + sizeof(char) * lengthOfNames); + strcpy(&names[oldNameLength], tempSub); + char nuller = '\0'; + memcpy(&names[lengthOfNames], &nuller, sizeof(char)); + lengthOfNames += 1; } } + chunk.num = be16(numEntries); + chunk.offs = 0; + fseek(fp, numoffsOffset, SEEK_SET); + fwrite(&chunk, sizeof(chunk), 1, fp); + + int i; + for(i=0;i 0) free(subs); } if ( memcmp(temp, grs1, sizeof(grs1)) == 0) { @@ -2558,7 +2661,8 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl { } - printf("\n"); + + *blobsize = *fileOffset - startOfChunk; } void WriteBRLYTHeader(brlyt_header rlythead, FILE* fp) @@ -2572,13 +2676,13 @@ void WriteBRLYTHeader(brlyt_header rlythead, FILE* fp) writehead.filesize = be32(rlythead.filesize); writehead.lyt_offset = be16(rlythead.lyt_offset); writehead.unk2 = be16(rlythead.unk2); + fwrite(&writehead, sizeof(brlyt_header), 1, fp); } void write_brlyt(char *infile, char *outfile) { - // set up tags list ?? - + unsigned int fileOffset = 0; FILE* fpx = fopen(infile, "r"); if(fpx == NULL) { printf("xmlyt couldn't be opened!\n"); @@ -2596,10 +2700,13 @@ void write_brlyt(char *infile, char *outfile) } mxml_node_t *node; FILE* fp = fopen(outfile, "wb+"); - if(fpx == NULL) { + if(fp == NULL) { printf("destination brlyt couldn't be opened!\n"); exit(1); } + + u32 fileError = ferror(fp); + u8* tagblob; u32 blobsize; u16 blobcount = 0; @@ -2615,11 +2722,10 @@ void write_brlyt(char *infile, char *outfile) rlythead.unk2 = 1; WriteBRLYTHeader(rlythead, fp); char temp[256]; -// big = (u8*)calloc(MAXIMUM_TAGS_SIZE, 1); -// MEMORY* tagsmem = mopen(tagchunksbig, MAXIMUM_TAGS_SIZE, 3); -// u32 totaltagsize = 0; -// printf("\x1b[33mTemp holds: \x1b[31m%s", temp); + fileOffset += sizeof(brlyt_header); + + u32 numberOfEntries; for(node = mxmlFindElement(tree, tree, "tag", NULL, NULL, MXML_DESCEND); node != NULL; node = mxmlFindElement(node, tree, "tag", NULL, NULL, MXML_DESCEND)) { @@ -2631,94 +2737,48 @@ void write_brlyt(char *infile, char *outfile) exit(1); } - WriteBRLYTEntry(tree, node, &tagblob, &blobsize, tempType); + unsigned int chunkHeaderOffset = fileOffset; -/* - char lyt1[4] = {'l', 'y', 't', '1'}; - if ( memcmp(temp, lyt1, sizeof(lyt1)) == 0) - { - brlyt_lytheader_chunk lytheader; - printf("found a lyt1\n"); - mxml_node_t *subnode = mxmlFindElement(node , node , "a", NULL, NULL, MXML_DESCEND); - if (subnode != NULL) - { - printf("an a value found\n"); - char tempChar[4]; - get_value(subnode, tempChar, 256); - //*(u32*)(&(data[x][i].part3)) = atoi(temp); - printf("temp holds: %s\n", tempChar); - lytheader.a = atoi(tempChar); - lytheader.pad[0]=0;lytheader.pad[1]=0;lytheader.pad[2]=0; - printf("a value: %08x\n", lytheader.a); - } - subnode = mxmlFindElement(node, node, "size", NULL, NULL, MXML_DESCEND); - if (subnode != NULL) - { - //mxml_node_t *valnode = mxmlfindElemt(subnode, subnode, "height", NULL, NULL, MXML_DESCEND); - mxml_node_t *valnode = mxmlFindElement(subnode , subnode , "width", NULL, NULL, MXML_DESCEND); - if (valnode != NULL) - { - char tempChar[4]; - get_value(valnode, tempChar, 256); - //*(f32*)(&(data[x][i].part3)) = atof(tempChar); - //float widthF = atof(tempChar); - float something; - //printf("width: %f\n", widthF); - *(float*)(&(lytheader.width)) = atof(tempChar); - printf("width: %f\n", lytheader.width); - } - valnode = mxmlFindElement(subnode , subnode ,"height", NULL, NULL, MXML_DESCEND); - if (valnode != NULL) - { - char tempChar[4]; - get_value(valnode, tempChar, 256); - //*(f32*)(&(data[x][i].part3)) = atof(tempChar); - //float heightF = atof(tempChar); - //printf("height: %f\n", heightF); - *(float*)(&(lytheader.height)) = atof(tempChar); - printf("height: %f\n", lytheader.height); - } - } - } -*/ + brlyt_entry_header chunk; + chunk.magic[0]=tempType[0]; + chunk.magic[1]=tempType[1]; + chunk.magic[2]=tempType[2]; + chunk.magic[3]=tempType[3]; + chunk.length = 0; + + fwrite(&chunk, sizeof(chunk), 1, fp); + + fileOffset += sizeof(chunk); + + WriteBRLYTEntry(tree, node, &tagblob, &blobsize, tempType, fp, &fileOffset); + + chunk.length = be32(blobsize + sizeof(chunk)); -// printf("name: %s\n", temp); + fseek(fp, chunkHeaderOffset, SEEK_SET); + fwrite(&chunk, sizeof(chunk), 1, fp); + fseek(fp, fileOffset, SEEK_SET); -// brlyt_entry_header entry; -// entry.magic[0] = temp[0]; -// entry.magic[1] = temp[1]; -// entry.magic[2] = temp[2]; -// entry.magic[3] = temp[3]; -// entry.length = 0; + numberOfEntries++; blobcount++; -// bloboffset = ftell(fp) + mtell(tagsmem) - (4 * (blobcount + 1)); -// bloboffset = be32(bloboffset); -// fwrite(&bloboffset, sizeof(u32), 1, fp); -// create_tag_from_xml(tree, node, &tagblob, &blobsize); -// mwrite(tagblob, blobsize, 1, tagsmem); -// totaltagsize += blobsize; + } -// tagchunksbig = (u8*)mclose(tagsmem); -// timgchunksbig = (u8*)mclose(timgmem); -// fwrite(timgchunksbig, totaltimgize, 1, fp); -// fwrite(tagchunksbig, totaltagsize, 1, fp); -// fseek(fp, 0, SEEK_END); -// lythead.size = ftell(fp) - rlythead.lyt_offset; -// rlythead.file_size = ftell(fp); -// fseek(fp, 0, SEEK_SET); -// WriteBRLYTHeader(rlythead, fp); - - printf("blob count: %08x", blobcount); - printf("\x1b[0m"); + + rlythead.filesize = fileOffset; + rlythead.unk2 = blobcount; + fseek(fp, 0, SEEK_SET); + WriteBRLYTHeader(rlythead, fp); + fclose(fpx); - fclose(fp); + fclose(fp); } void make_brlyt(char* infile, char* outfile) { printf("\x1b[33mParsing XMLYT @ \x1b[0m%s.\n", infile); write_brlyt(infile, outfile); + free(materials); + printf("\x1b[34mParsing XMLYT @ \x1b[0m%s complete.\n", infile); } -- cgit v1.3.1 From 9534b7c51f4f51ef01ae8bf8c9923efa7851123b Mon Sep 17 00:00:00 2001 From: megazig Date: Wed, 25 Mar 2009 16:56:13 -0500 Subject: fixed be16 overflow errors --- brlyt.c | 96 ++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 43 deletions(-) (limited to 'brlyt.c') diff --git a/brlyt.c b/brlyt.c index 7af7a04..c80d718 100644 --- a/brlyt.c +++ b/brlyt.c @@ -96,6 +96,16 @@ float float_swap_bytes(float float1) return *newFloat; } +u16 short_swap_bytes(u16 short1) +{ + unsigned char* short1c; short1c = (unsigned char*)&short1; + unsigned char charTemp = 0x00; + charTemp = short1c[0]; short1c[0] = short1c[1]; short1c[1] = charTemp; + + short *newShort; newShort = (short*)short1c; + return *newShort; +} + unsigned int bit_extract(unsigned int num, unsigned int start, unsigned int end) { if (end == 100) end = start; @@ -204,7 +214,7 @@ void PrintBRLYTEntry_grp1(brlyt_entry entry, u8* brlyt_file) int offset; offset = 20; int n = 0; - for (n;n\n", getMaterial(be16(data2.mat_off))); + printf(" \n", getMaterial(short_swap_bytes(data2.mat_off))); printf(" \n"); printf(" 0x%08X\n", be32(data2.vtx_colors[0])); printf(" 0x%08X\n", be32(data2.vtx_colors[1])); @@ -622,8 +632,8 @@ void PrintBRLYTEntry_txt1(brlyt_entry entry, u8* brlyt_file) printf(" char_space: %f\n", float_swap_bytes(data2.char_space)); printf(" line_space: %f\n", float_swap_bytes(data2.line_space)); #else - printf(" %04x-%04x\n", be16(data2.len1), be16(data2.len2)); - printf(" \n", be16(data2.font_idx)); + printf(" %04x-%04x\n", short_swap_bytes(data2.len1), short_swap_bytes(data2.len2)); + printf(" \n", short_swap_bytes(data2.font_idx)); printf(" %f\n", float_swap_bytes(data2.font_size_x)); printf(" %f\n", float_swap_bytes(data2.font_size_y)); printf(" %f\n", float_swap_bytes(data2.font_size_x)); @@ -645,10 +655,10 @@ void PrintBRLYTEntry_mat1(brlyt_entry entry, u8* brlyt_file) printf(" num: %08x\n", be16(data.num)); printf(" offs: %08x\n", be16(data.offs)); #else - printf("type=\"%c%c%c%c\" count=\"%d\">\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3], be16(data.num)); + printf("type=\"%c%c%c%c\" count=\"%d\">\n", entry.magic[0], entry.magic[1], entry.magic[2], entry.magic[3], short_swap_bytes(data.num)); #endif //OLD_BRLYT_OUTSTYLE int n = 0; - for (n;n\n", data3.name); printf(" \n"); - printf(" %#x\n", be16(data3.tev_color[0])); - printf(" %#x\n", be16(data3.tev_color[1])); - printf(" %#x\n", be16(data3.tev_color[2])); - printf(" %#x\n", be16(data3.tev_color[3])); - printf(" %#x\n", be16(data3.unk_color[0])); - printf(" %#x\n", be16(data3.unk_color[1])); - printf(" %#x\n", be16(data3.unk_color[2])); - printf(" %#x\n", be16(data3.unk_color[3])); - printf(" %#x\n", be16(data3.unk_color_2[0])); - printf(" %#x\n", be16(data3.unk_color_2[1])); - printf(" %#x\n", be16(data3.unk_color_2[2])); - printf(" %#x\n", be16(data3.unk_color_2[3])); - printf(" %#x\n", be16(data3.tev_kcolor[0])); - printf(" %#x\n", be16(data3.tev_kcolor[1])); - printf(" %#x\n", be16(data3.tev_kcolor[2])); - printf(" %#x\n", be16(data3.tev_kcolor[3])); + printf(" %#x\n", short_swap_bytes(data3.tev_color[0])); + printf(" %#x\n", short_swap_bytes(data3.tev_color[1])); + printf(" %#x\n", short_swap_bytes(data3.tev_color[2])); + printf(" %#x\n", short_swap_bytes(data3.tev_color[3])); + printf(" %#x\n", short_swap_bytes(data3.unk_color[0])); + printf(" %#x\n", short_swap_bytes(data3.unk_color[1])); + printf(" %#x\n", short_swap_bytes(data3.unk_color[2])); + printf(" %#x\n", short_swap_bytes(data3.unk_color[3])); + printf(" %#x\n", short_swap_bytes(data3.unk_color_2[0])); + printf(" %#x\n", short_swap_bytes(data3.unk_color_2[1])); + printf(" %#x\n", short_swap_bytes(data3.unk_color_2[2])); + printf(" %#x\n", short_swap_bytes(data3.unk_color_2[3])); + printf(" %#x\n", short_swap_bytes(data3.tev_kcolor[0])); + printf(" %#x\n", short_swap_bytes(data3.tev_kcolor[1])); + printf(" %#x\n", short_swap_bytes(data3.tev_kcolor[2])); + printf(" %#x\n", short_swap_bytes(data3.tev_kcolor[3])); printf(" \n"); printf(" %08x\n", be32(data3.flags)); #endif //OLD_BRLYT_OUTSTYLE @@ -694,7 +704,7 @@ void PrintBRLYTEntry_mat1(brlyt_entry entry, u8* brlyt_file) { brlyt_texref_chunk data4; BRLYT_ReadDataFromMemory(&data4, brlyt_file, sizeof(brlyt_texref_chunk)); - int tplOffset = be16(data4.tex_offs); + int tplOffset = short_swap_bytes(data4.tex_offs); #ifdef OLD_BRLYT_OUTSTYLE printf(" texoffs: %08x\n", be16(data4.tex_offs)); printf(" wrap_s: %08x\n", data4.wrap_s); @@ -1032,7 +1042,7 @@ void parse_brlyt(char *filename) BRLYT_ReadDataFromMemory(&header, brlyt_file, sizeof(brlyt_header)); BRLYT_CheckHeaderSanity(header, file_size); brlyt_entry *entries; - BRLYT_fileoffset = be16(header.lyt_offset); + BRLYT_fileoffset = short_swap_bytes(header.lyt_offset); brlyt_entry_header tempentry; int i; dbgprintf("curr %08x max %08x\n", BRLYT_fileoffset, file_size); @@ -1048,7 +1058,7 @@ void parse_brlyt(char *filename) printf("Couldn't allocate for entries!\n"); exit(1); } - BRLYT_fileoffset = be16(header.lyt_offset); + BRLYT_fileoffset = short_swap_bytes(header.lyt_offset); for(i = 0; i < entrycount; i++) { dbgprintf("&(entries[i]) = %08x\n", &(entries[i])); BRLYT_ReadDataFromMemoryX(&tempentry, brlyt_file, sizeof(brlyt_entry_header)); @@ -1066,8 +1076,8 @@ void parse_brlyt(char *filename) printf(" Unk1: %08x\n", be32(header.unk1)); printf(" Filesize: %lu\n", be32(header.filesize)); printf(" %s real file size!\n", be32(header.filesize) == file_size ? "Matches" : "Does not match"); - printf(" Offset to lyt1: %04x\n", be16(header.lyt_offset)); - printf(" Unk2: %04x\n", be16(header.unk2)); + printf(" Offset to lyt1: %04x\n", short_swap_bytes(header.lyt_offset)); + printf(" Unk2: %04x\n", short_swap_bytes(header.unk2)); printf("\nBRLYT Entries:"); #else printf("\n" \ @@ -1357,7 +1367,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl lengthOfNames += 1; } } - chunk.num = be16(numEntries); + chunk.num = short_swap_bytes(numEntries); chunk.offs = 0; fseek(fp, numoffsOffset, SEEK_SET); fwrite(&chunk, sizeof(chunk), 1, fp); @@ -1435,7 +1445,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl lengthOfNames += 1; } } - chunk.num = be16(numEntries); + chunk.num = short_swap_bytes(numEntries); chunk.offs = 0; fseek(fp, numoffsOffset, SEEK_SET); fwrite(&chunk, sizeof(chunk), 1, fp); @@ -1472,7 +1482,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl for(subnode=mxmlFindElement(node, node, "entries", NULL, NULL, MXML_DESCEND);subnode!=NULL;subnode=mxmlFindElement(subnode, node, "entries", NULL, NULL, MXML_DESCEND)) numberOfEntries++; - numchunk.num = be16(numberOfEntries); + numchunk.num = short_swap_bytes(numberOfEntries); numchunk.offs = 0; fwrite(&numchunk, sizeof(numchunk), 1, fp); *fileOffset = *fileOffset + sizeof(numchunk); @@ -1521,7 +1531,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl chunk.tev_color[colorNumber] = strtoul(tempCoord, NULL, 16); - chunk.tev_color[colorNumber] = be16(chunk.tev_color[colorNumber]); + chunk.tev_color[colorNumber] = short_swap_bytes(chunk.tev_color[colorNumber]); colorNumber+=1; } @@ -1533,7 +1543,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl chunk.unk_color[colorNumber] = strtoul(tempCoord, NULL, 16); - chunk.unk_color[colorNumber] = be16(chunk.unk_color[colorNumber]); + chunk.unk_color[colorNumber] = short_swap_bytes(chunk.unk_color[colorNumber]); colorNumber+=1; } @@ -1545,7 +1555,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl chunk.unk_color_2[colorNumber] = strtoul(tempCoord, NULL, 16); - chunk.unk_color_2[colorNumber] = be16(chunk.unk_color_2[colorNumber]); + chunk.unk_color_2[colorNumber] = short_swap_bytes(chunk.unk_color_2[colorNumber]); colorNumber+=1; } @@ -1584,7 +1594,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl printf("No name attribute found!\nQuitting!\n"); exit(1); } - chunkTexRef.tex_offs = be16(findOffset(temp)); + chunkTexRef.tex_offs = short_swap_bytes(findOffset(temp)); valnode=mxmlFindElement(setnode, setnode, "wrap_s", NULL, NULL, MXML_DESCEND); @@ -2354,7 +2364,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl exit(1); } chunk2.font_idx = atoi(temp); - chunk2.font_idx = be16(chunk2.font_idx); + chunk2.font_idx = short_swap_bytes(chunk2.font_idx); mxml_node_t *valnode; valnode = mxmlFindElement(subnode, subnode, "xsize", NULL, NULL, MXML_DESCEND); @@ -2393,9 +2403,9 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl char tempCoord[256]; get_value(subnode, tempCoord, 256); chunk2.len1 = strtoul(tempCoord, NULL, 16); - chunk2.len1 = be16(chunk2.len1); + chunk2.len1 = short_swap_bytes(chunk2.len1); chunk2.len2 = strtoul(tempCoord+5, NULL, 16); - chunk2.len2 = be16(chunk2.len2); + chunk2.len2 = short_swap_bytes(chunk2.len2); } subnode = mxmlFindElement(node, node, "color", NULL, NULL, MXML_DESCEND); if (subnode != NULL) @@ -2545,7 +2555,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl printf("No name attribute found!\nQuitting!\n"); exit(1); } - chunk2.mat_off = be16(findOffset(temp)); + chunk2.mat_off = short_swap_bytes(findOffset(temp)); } subnode = mxmlFindElement(node, node, "colors", NULL, NULL, MXML_DESCEND); if (subnode != NULL) @@ -2638,7 +2648,7 @@ void WriteBRLYTEntry(mxml_node_t *tree, mxml_node_t *node, u8** tagblob, u32* bl } } } - chunk.numsubs = be16(numSubs); + chunk.numsubs = short_swap_bytes(numSubs); fwrite(&chunk, sizeof(chunk), 1, fp); *fileOffset = *fileOffset + sizeof(chunk); fwrite(subs, sizeof(char) * subsLength, 1, fp); @@ -2674,8 +2684,8 @@ void WriteBRLYTHeader(brlyt_header rlythead, FILE* fp) writehead.magic[3] = rlythead.magic[3]; writehead.unk1 = be32(rlythead.unk1); writehead.filesize = be32(rlythead.filesize); - writehead.lyt_offset = be16(rlythead.lyt_offset); - writehead.unk2 = be16(rlythead.unk2); + writehead.lyt_offset = short_swap_bytes(rlythead.lyt_offset); + writehead.unk2 = short_swap_bytes(rlythead.unk2); fwrite(&writehead, sizeof(brlyt_header), 1, fp); } -- cgit v1.3.1