2009-10-01 01:10:58 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* USB Loader GX Team
|
|
|
|
|
* openingbnr
|
|
|
|
|
*
|
|
|
|
|
* Extract opening.bnr/banner.bin/sound.bin/icon.bin
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2008 Magicus <magicus@gmail.com>
|
|
|
|
|
* Licensed under the terms of the GNU GPL, version 2
|
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
|
|
|
|
* Version 1.0 Initial release
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
|
|
#include <ogcsys.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
2009-11-15 22:30:44 +01:00
|
|
|
|
#include "libfat/fat.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
|
|
#include "MD5.h"
|
2009-11-10 00:03:13 +01:00
|
|
|
|
#include "openingbnr.h"
|
2010-09-25 08:54:27 +02:00
|
|
|
|
#include "FileOperations/fileops.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u16 be16(const u8 *p)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
return (p[0] << 8) | p[1];
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u32 be32(const u8 *p)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u64 be64(const u8 *p)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
return ((u64) be32(p) << 32) | be32(p + 4);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u64 be34(const u8 *p)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
return 4 * (u64) be32(p);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
void wbe16(u8 *p, u16 x)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
p[0] = x >> 8;
|
|
|
|
|
p[1] = x;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
void wbe32(u8 *p, u32 x)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
wbe16(p, x >> 16);
|
|
|
|
|
wbe16(p + 2, x);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
void wbe64(u8 *p, u64 x)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
wbe32(p, x >> 32);
|
|
|
|
|
wbe32(p + 4, x);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
void md5(u8 *data, u32 len, u8 *hash)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
MD5(hash, data, len);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
|
u8 zeroes[0x40];
|
|
|
|
|
u32 imet; // "IMET"
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u8 zero_six_zero_three[8]; // fixed, unknown purpose
|
2009-10-01 01:10:58 +02:00
|
|
|
|
u32 sizes[3];
|
|
|
|
|
u32 flag1;
|
|
|
|
|
u16 name_jp[0x2a]; // might be empty
|
|
|
|
|
u16 name_en[0x2a];
|
|
|
|
|
u16 name_de[0x2a];
|
|
|
|
|
u16 name_fr[0x2a];
|
|
|
|
|
u16 name_es[0x2a];
|
|
|
|
|
u16 name_it[0x2a];
|
|
|
|
|
u16 name_nl[0x2a];
|
|
|
|
|
u8 zeroes_2[0x348];
|
|
|
|
|
u8 crypto[0x10];
|
|
|
|
|
} imet_data_t;
|
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
u32 imd5_tag; // 0x494D4435 "IMD5";
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u32 size; // size of the rest of part B, starting from next field.
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u8 zeroes[8];
|
|
|
|
|
u8 md5[16];
|
|
|
|
|
u32 payload_tag; // 0x4C5A3737 "LZ77" if this is lz77
|
|
|
|
|
u32 payload_data;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
} imd5_header_t;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u16 type;
|
|
|
|
|
u16 name_offset;
|
|
|
|
|
u32 data_offset; // == absolut offset fr<66>n U.8- headerns b<>rjan
|
|
|
|
|
u32 size; // last included file num for directories
|
2009-10-01 01:10:58 +02:00
|
|
|
|
} U8_node;
|
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u32 tag; // 0x55AA382D "U.8-"
|
|
|
|
|
u32 rootnode_offset; // offset to root_node, always 0x20.
|
|
|
|
|
u32 header_size; // size of header from root_node to end of string table.
|
|
|
|
|
u32 data_offset; // offset to data -- this is rootnode_offset + header_size, aligned to 0x40.
|
|
|
|
|
u8 zeroes[16];
|
2009-10-01 01:10:58 +02:00
|
|
|
|
} U8_archive_header;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
static int write_file(void* data, size_t size, char* name)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
size_t written = 0;
|
|
|
|
|
FILE *out;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
out = fopen(name, "wb");
|
|
|
|
|
if (out)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
written = fwrite(data, 1, size, out);
|
|
|
|
|
fclose(out);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
return (written == size) ? 1 : -1;
|
2009-11-15 22:30:44 +01:00
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u8* decompress_lz77(u8 *data, size_t data_size, size_t* decompressed_size)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u8 *data_end;
|
|
|
|
|
u8 *decompressed_data;
|
|
|
|
|
size_t unpacked_size;
|
|
|
|
|
u8 *in_ptr;
|
|
|
|
|
u8 *out_ptr;
|
|
|
|
|
u8 *out_end;
|
|
|
|
|
|
|
|
|
|
in_ptr = data;
|
|
|
|
|
data_end = data + data_size;
|
|
|
|
|
|
|
|
|
|
// Assume this for now and grow when needed
|
|
|
|
|
unpacked_size = data_size;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
decompressed_data = malloc(unpacked_size);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
out_end = decompressed_data + unpacked_size;
|
|
|
|
|
|
|
|
|
|
out_ptr = decompressed_data;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
while (in_ptr < data_end)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
int bit;
|
|
|
|
|
u8 bitmask = *in_ptr;
|
|
|
|
|
|
|
|
|
|
in_ptr++;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
for (bit = 0x80; bit != 0; bit >>= 1)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (bitmask & bit)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
// Next section is compressed
|
|
|
|
|
u8 rep_length;
|
|
|
|
|
u16 rep_offset;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
rep_length = (*in_ptr >> 4) + 3;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
rep_offset = *in_ptr & 0x0f;
|
|
|
|
|
in_ptr++;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
rep_offset = *in_ptr | (rep_offset << 8);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
in_ptr++;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (out_ptr - decompressed_data < rep_offset)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
for (; rep_length > 0; rep_length--)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
*out_ptr = out_ptr[-rep_offset - 1];
|
2010-09-19 01:16:05 +02:00
|
|
|
|
out_ptr++;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (out_ptr >= out_end)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
// Need to grow buffer
|
2010-09-24 02:48:03 +02:00
|
|
|
|
decompressed_data = realloc(decompressed_data, unpacked_size * 2);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
out_ptr = decompressed_data + unpacked_size;
|
|
|
|
|
unpacked_size *= 2;
|
|
|
|
|
out_end = decompressed_data + unpacked_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Just copy byte
|
|
|
|
|
*out_ptr = *in_ptr;
|
|
|
|
|
out_ptr++;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (out_ptr >= out_end)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
// Need to grow buffer
|
2010-09-24 02:48:03 +02:00
|
|
|
|
decompressed_data = realloc(decompressed_data, unpacked_size * 2);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
out_ptr = decompressed_data + unpacked_size;
|
|
|
|
|
unpacked_size *= 2;
|
|
|
|
|
out_end = decompressed_data + unpacked_size;
|
|
|
|
|
}
|
|
|
|
|
in_ptr++;
|
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
*decompressed_size = (out_ptr - decompressed_data);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return decompressed_data;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
static int write_imd5_lz77(u8* data, size_t size, char* outname)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
imd5_header_t* header = (imd5_header_t*) data;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u32 tag;
|
|
|
|
|
u32 size_in_imd5;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
u8 md5_calc[16];
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u8 *decompressed_data;
|
|
|
|
|
size_t decompressed_size;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
tag = be32((u8*) &header->imd5_tag);
|
|
|
|
|
if (tag != 0x494D4435)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
return -4;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
md5(data + 32, size - 32, md5_calc);
|
|
|
|
|
if (memcmp(&header->md5, md5_calc, 0x10))
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
size_in_imd5 = be32((u8*) &header->size);
|
|
|
|
|
if (size_in_imd5 != size - 32)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
return -6;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
tag = be32((u8*) &header->payload_tag);
|
|
|
|
|
if (tag == 0x4C5A3737)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
// "LZ77" - uncompress
|
2010-09-24 02:48:03 +02:00
|
|
|
|
decompressed_data = decompress_lz77(data + sizeof(imd5_header_t), size - sizeof(imd5_header_t),
|
|
|
|
|
&decompressed_size);
|
|
|
|
|
if (decompressed_data == NULL) return -7;
|
|
|
|
|
write_file(decompressed_data, decompressed_size, outname);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
//printf(", uncompressed %d bytes, md5 ok", decompressed_size);
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
free(decompressed_data);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
write_file(&header->payload_tag, size - 32, outname);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
//printf(", md5 ok");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2010-02-09 07:33:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
static int do_U8_archive(FILE *fp)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
U8_archive_header header;
|
|
|
|
|
U8_node root_node;
|
|
|
|
|
u32 tag;
|
|
|
|
|
u32 num_nodes;
|
|
|
|
|
U8_node* nodes;
|
|
|
|
|
u8* string_table;
|
|
|
|
|
size_t rest_size;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
u32 data_offset;
|
|
|
|
|
u32 current_offset;
|
|
|
|
|
u16 dir_stack[16];
|
|
|
|
|
int dir_index = 0;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fread(&header, 1, sizeof header, fp);
|
|
|
|
|
tag = be32((u8*) &header.tag);
|
|
|
|
|
if (tag != 0x55AA382D)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fread(&root_node, 1, sizeof(root_node), fp);
|
|
|
|
|
num_nodes = be32((u8*) &root_node.size) - 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
//printf("Number of files: %d\n", num_nodes);
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
nodes = malloc(sizeof(U8_node) * (num_nodes));
|
|
|
|
|
fread(nodes, 1, num_nodes * sizeof(U8_node), fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
data_offset = be32((u8*) &header.data_offset);
|
|
|
|
|
rest_size = data_offset - sizeof(header) - (num_nodes + 1) * sizeof(U8_node);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
string_table = malloc(rest_size);
|
|
|
|
|
fread(string_table, 1, rest_size, fp);
|
2010-02-09 11:59:55 +01:00
|
|
|
|
current_offset = data_offset;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
for (i = 0; i < num_nodes; i++)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
U8_node* node = &nodes[i];
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u16 type = be16((u8*) &node->type);
|
|
|
|
|
u16 name_offset = be16((u8*) &node->name_offset);
|
|
|
|
|
u32 my_data_offset = be32((u8*) &node->data_offset);
|
|
|
|
|
u32 size = be32((u8*) &node->size);
|
|
|
|
|
char* name = (char*) &string_table[name_offset];
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u8* file_data;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (type == 0x0100)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
// Directory
|
2010-09-24 02:48:03 +02:00
|
|
|
|
mkdir(name, 0777);
|
|
|
|
|
chdir(name);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
dir_stack[++dir_index] = size;
|
|
|
|
|
//printf("%*s%s/\n", dir_index, "", name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Normal file
|
|
|
|
|
u8 padding[32];
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (type != 0x0000)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
free(string_table);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (current_offset < my_data_offset)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
int diff = my_data_offset - current_offset;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (diff > 32)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
free(string_table);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return -3;
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fread(padding, 1, diff, fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
current_offset += diff;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
file_data = malloc(size);
|
|
|
|
|
fread(file_data, 1, size, fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
//printf("%*s %s (%d bytes", dir_index, "", name, size);
|
|
|
|
|
int result;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
result = write_imd5_lz77(file_data, size, name);
|
|
|
|
|
if (result < 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
free(string_table);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
//printf(")\n");
|
|
|
|
|
current_offset += size;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
while (dir_stack[dir_index] == i + 2 && dir_index > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
chdir("..");
|
2010-09-19 01:16:05 +02:00
|
|
|
|
dir_index--;
|
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
free(string_table);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return 0;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
static void do_imet_header(FILE *fp)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
imet_data_t header;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fread(&header, 1, sizeof header, fp);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
write_file(&header, sizeof(header), "header.imet");
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
void do_U8_archivebanner(FILE *fp)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
|
U8_archive_header header;
|
|
|
|
|
U8_node root_node;
|
|
|
|
|
u32 tag;
|
|
|
|
|
u32 num_nodes;
|
|
|
|
|
U8_node* nodes;
|
|
|
|
|
u8* string_table;
|
|
|
|
|
size_t rest_size;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
u32 data_offset;
|
|
|
|
|
u16 dir_stack[16];
|
|
|
|
|
int dir_index = 0;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fread(&header, 1, sizeof header, fp);
|
|
|
|
|
tag = be32((u8*) &header.tag);
|
|
|
|
|
if (tag != 0x55AA382D)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
//printf("No U8 tag");
|
2010-09-24 02:48:03 +02:00
|
|
|
|
exit(0);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fread(&root_node, 1, sizeof(root_node), fp);
|
|
|
|
|
num_nodes = be32((u8*) &root_node.size) - 1;
|
|
|
|
|
printf("Number of files: %d\n", num_nodes);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
nodes = malloc(sizeof(U8_node) * (num_nodes));
|
|
|
|
|
fread(nodes, 1, num_nodes * sizeof(U8_node), fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
data_offset = be32((u8*) &header.data_offset);
|
|
|
|
|
rest_size = data_offset - sizeof(header) - (num_nodes + 1) * sizeof(U8_node);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
string_table = malloc(rest_size);
|
|
|
|
|
fread(string_table, 1, rest_size, fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
for (i = 0; i < num_nodes; i++)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
U8_node* node = &nodes[i];
|
2010-09-24 02:48:03 +02:00
|
|
|
|
u16 type = be16((u8*) &node->type);
|
|
|
|
|
u16 name_offset = be16((u8*) &node->name_offset);
|
|
|
|
|
u32 my_data_offset = be32((u8*) &node->data_offset);
|
|
|
|
|
u32 size = be32((u8*) &node->size);
|
|
|
|
|
char* name = (char*) &string_table[name_offset];
|
2010-09-19 01:16:05 +02:00
|
|
|
|
u8* file_data;
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (type == 0x0100)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
// Directory
|
2010-09-24 02:48:03 +02:00
|
|
|
|
mkdir(name, 0777);
|
|
|
|
|
chdir(name);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
dir_stack[++dir_index] = size;
|
|
|
|
|
//printf("%*s%s/\n", dir_index, "", name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Normal file
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (type != 0x0000)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
printf("Unknown type");
|
|
|
|
|
exit(0);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fseek(fp, my_data_offset, SEEK_SET);
|
|
|
|
|
file_data = malloc(size);
|
|
|
|
|
fread(file_data, 1, size, fp);
|
|
|
|
|
write_file(file_data, size, name);
|
|
|
|
|
free(file_data);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
//printf("%*s %s (%d bytes)\n", dir_index, "", name, size);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
while (dir_stack[dir_index] == i + 2 && dir_index > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
chdir("..");
|
2010-09-19 01:16:05 +02:00
|
|
|
|
dir_index--;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
free(string_table);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
int extractbnrfile(const char * filepath, const char * destpath)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2009-11-10 00:03:13 +01:00
|
|
|
|
int ret = -1;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
FILE *fp = fopen(filepath, "rb");
|
|
|
|
|
if (fp)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-25 08:54:27 +02:00
|
|
|
|
CreateSubfolder(destpath);
|
2010-09-24 02:48:03 +02:00
|
|
|
|
chdir(destpath);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
do_imet_header(fp);
|
|
|
|
|
ret = do_U8_archive(fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fclose(fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
int unpackBin(const char * filename, const char * outdir)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
FILE *fp = fopen(filename, "rb");
|
|
|
|
|
;
|
|
|
|
|
if (fp)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-25 08:54:27 +02:00
|
|
|
|
CreateSubfolder(outdir);
|
2010-09-24 02:48:03 +02:00
|
|
|
|
chdir(outdir);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
do_U8_archivebanner(fp);
|
|
|
|
|
fclose(fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2009-11-10 00:03:13 +01:00
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
|
#if 0
|
|
|
|
|
|
2009-11-10 00:03:13 +01:00
|
|
|
|
#define TMP_PATH(s) "BANNER:/dump"s
|
|
|
|
|
//#define TMP_PATH(s) "SD:/dump"s
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
int unpackBanner(const u8 *gameid, int what, const char *outdir)
|
2010-02-09 11:59:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
|
char path[256];
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (!ramdiskMount("BANNER", NULL)) return -1;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
2010-09-25 08:54:27 +02:00
|
|
|
|
CreateSubfolder(TMP_PATH( "/" ));
|
2010-09-24 02:48:03 +02:00
|
|
|
|
s32 ret = dump_banner(gameid, TMP_PATH( "/opening.bnr" ));
|
|
|
|
|
if (ret != 1)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error2;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
ret = extractbnrfile(TMP_PATH( "/opening.bnr" ), TMP_PATH( "/" ));
|
|
|
|
|
if (ret != 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error2;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (what & UNPACK_BANNER_BIN)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
snprintf(path, sizeof(path), "%sbanner/", outdir);
|
|
|
|
|
ret = unpackBin(TMP_PATH( "/meta/banner.bin" ), path);
|
|
|
|
|
if (ret != 1)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error2;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (what & UNPACK_ICON_BIN)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
snprintf(path, sizeof(path), "%sicon/", outdir);
|
|
|
|
|
ret = unpackBin(TMP_PATH( "/meta/icon.bin" ), path);
|
|
|
|
|
if (ret != 1)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error2;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (what & UNPACK_SOUND_BIN)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
|
snprintf(path, sizeof(path), "%ssound.bin", outdir);
|
|
|
|
|
FILE *fp = fopen(TMP_PATH( "/meta/sound.bin" ), "rb");
|
|
|
|
|
if (fp)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
size_t size;
|
|
|
|
|
u8 *data;
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
|
size = ftell(fp);
|
|
|
|
|
if (!size)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
data = (u8 *) malloc(size);
|
|
|
|
|
if (!data)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
if (fread(data, 1, size, fp) != size)
|
2010-09-19 01:16:05 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
ret = write_file(data, size, path);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
error: fclose(fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
|
ramdiskUnmount("BANNER");
|
|
|
|
|
error2: if (ret < 0) return ret;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
return 1;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
}
|
2010-10-01 17:01:19 +02:00
|
|
|
|
#endif
|