old-bannertool/source/3ds/cbmd.cpp

81 lines
1.8 KiB
C++
Raw Normal View History

#include "cbmd.h"
#include "lz11.h"
2015-09-20 22:39:39 +00:00
#include <stdlib.h>
#include <string.h>
typedef struct {
char magic[4] = {'C', 'B', 'M', 'D'};
u32 zero = 0;
u32 cgfxOffsets[14] = {0};
u8 padding[0x44] = {0};
u32 cwavOffset = 0;
} CBMDHeader;
2015-01-26 03:04:27 +00:00
u8* cbmd_build_data(CBMD cbmd, u32* size, bool bnr) {
u32 headerSize = sizeof(CBMDHeader);
CBMDHeader header;
2015-01-26 03:04:27 +00:00
u8* compressedCGFXs[14] = {NULL};
u32 compressedCGFXSizes[14] = {0};
u32 offset = headerSize;
for(int i = 0; i < 14; i++) {
if(cbmd.cgfxs[i] != NULL) {
compressedCGFXs[i] = lz11_compress(cbmd.cgfxs[i], cbmd.cgfxSizes[i], &compressedCGFXSizes[i]);
header.cgfxOffsets[i] = offset;
offset += compressedCGFXSizes[i];
}
}
u32 pad = 0;
if(bnr) {
2015-01-25 18:32:01 +00:00
pad = 0x10 - (offset % 0x10);
offset += pad;
}
if(cbmd.cwav != NULL) {
header.cwavOffset = offset;
offset += cbmd.cwavSize;
}
u8* output = (u8*) malloc(offset);
u32 pos = 0;
memcpy(output + pos, &header, headerSize);
pos += headerSize;
for(int i = 0; i < 14; i++) {
if(compressedCGFXs[i] != NULL) {
memcpy(output + pos, compressedCGFXs[i], compressedCGFXSizes[i]);
free(compressedCGFXs[i]);
compressedCGFXs[i] = NULL;
pos += compressedCGFXSizes[i];
}
}
if(bnr) {
memset(output + pos, 0, pad);
pos += pad;
}
if(cbmd.cwav != NULL) {
memcpy(output + pos, cbmd.cwav, cbmd.cwavSize);
pos += cbmd.cwavSize;
}
if(size != NULL) {
*size = offset;
}
return output;
}
2015-01-26 03:04:27 +00:00
u8* cbmd_build(CBMD cbmd, u32* size) {
return cbmd_build_data(cbmd, size, false);
}
2015-01-26 03:04:27 +00:00
u8* bnr_build(CBMD cbmd, u32* size) {
return cbmd_build_data(cbmd, size, true);
}