From a527c645915d4e97d6afdf67f73fa525ee4b1371 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Sat, 18 Mar 2017 08:16:14 +0100 Subject: [PATCH] Fixed crash (was a memset). --- Makefile.vars | 2 +- include/libg1m/format/std.h | 6 +++++- include/libg1m/format/std/lang.h | 9 ++++++--- include/libg1m/format/std/picture.h | 14 ++++++++++---- src/decode/main.c | 5 +---- src/decode/std.c | 1 - src/decode/std/fkey.c | 8 +++++--- src/decode/std/lang.c | 8 +++++--- src/manage/handle.c | 1 + 9 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Makefile.vars b/Makefile.vars index 267df4f..e89ff5e 100755 --- a/Makefile.vars +++ b/Makefile.vars @@ -92,7 +92,7 @@ endif # - Specific linker flags LDFLAGS_Windows := -lws2_32 -Wl,--out-implib,lib$(NAME).dll.a LDFLAGS_Linux := $(if $(STATIC),,-Wl,-soname,$(SONAME) \ - -e __lib$(NAME)_version \ + -e __lib$(NAME)_version \ -Wl,-z,relro -Wl,-z,combreloc -Wl,-z,defs) # - Linker flags LDFLAGS := $(if $(STATIC),,-shared) \ diff --git a/include/libg1m/format/std.h b/include/libg1m/format/std.h index 71f810d..f25e7b8 100644 --- a/include/libg1m/format/std.h +++ b/include/libg1m/format/std.h @@ -92,6 +92,7 @@ struct standard_subheader { uint32_t checksum; /* file type: + * - 0x00: picture; * - 0x01: add-in; * - 0x02: function keys; * - 0x04: language files; */ @@ -103,7 +104,10 @@ struct standard_subheader { uint8_t platform; /* unknown */ - uint8_t _unknown0[8]; + uint8_t _unknown0[4]; + + /* size of the data + footer? */ + uint32_t df_size; /* control: * - for a G3A: filesize - 0x7000 - 4 diff --git a/include/libg1m/format/std/lang.h b/include/libg1m/format/std/lang.h index eac7970..8c5fdd6 100644 --- a/include/libg1m/format/std/lang.h +++ b/include/libg1m/format/std/lang.h @@ -57,11 +57,14 @@ struct g1l_subheader { * have this language header: */ struct g3l_lang_header { - /* sequence: '4C 59 37 35 35 00 00 00 02' (LY755 ) */ - uint8_t sequence[9]; + /* sequence: '4C 59 37 35 35 00 00 00' (LY755 ) */ + uint8_t sequence[8]; + + /* unknown: 0x02 */ + uint8_t _unknown; /* unused byte. */ - uint8_t unused; + uint8_t _unused; /* number of messages ("possibly 0 base indexed") */ uint32_t num; diff --git a/include/libg1m/format/std/picture.h b/include/libg1m/format/std/picture.h index 02f3ddf..136b304 100644 --- a/include/libg1m/format/std/picture.h +++ b/include/libg1m/format/std/picture.h @@ -37,12 +37,18 @@ enum g3p_colorsize { /* So, after the header, here is the G3P subheader: */ struct g3p_subheader { - /* some magic sequence: "CP0100Ly755" - * "CP" is check by a syscall */ - uint8_t magic[11]; + /* some magic sequence: "CP0100" + * "CP" is checked by a syscall */ + uint8_t type[6]; + + /* some magic sequence: '4C 59 37 35 35 00 00 00' (LY755 ) */ + uint8_t magic[8]; + + /* unknown: 0x02 */ + uint8_t _unknown; /* unused */ - uint8_t unused[5]; + uint8_t _unused; /* size of the file after standard header */ uint32_t g3p_size; diff --git a/src/decode/main.c b/src/decode/main.c index 7b1e0b3..4af3ce1 100644 --- a/src/decode/main.c +++ b/src/decode/main.c @@ -93,13 +93,10 @@ static int lookup_extension(const char *path, g1m_type_t types, int g1m_decode(g1m_t **handle, const char *path, g1m_buffer_t *buffer, g1m_type_t expected_types) { - /* initialize the handle */ - memset(handle, 0, sizeof(g1m_t)); - /* match using extension */ decode_func decode; int err = lookup_extension(path, expected_types, &decode); - if (err == g1m_error_wrong_type) return (g1m_error_wrong_type); + if (err == g1m_error_wrong_type) return (err); else if (!err) return ((*decode)(handle, buffer)); /* identify a CAS file */ diff --git a/src/decode/std.c b/src/decode/std.c index ce764cf..e118650 100644 --- a/src/decode/std.c +++ b/src/decode/std.c @@ -71,7 +71,6 @@ static int find_decode_function(g1m_platform_t platform, g1m_type_t type, { /* get the function */ struct corresp *c = parsing_functions - 1; - log_info("Type is %04X, platform is %04X", type, platform); while ((++c)->decode) { if (c->type != type) continue; diff --git a/src/decode/std/fkey.c b/src/decode/std/fkey.c index a85e935..1aef068 100644 --- a/src/decode/std/fkey.c +++ b/src/decode/std/fkey.c @@ -140,17 +140,17 @@ int g1m_decode_std_cg_fkey(g1m_t **h, g1m_buffer_t *buffer, struct standard_header *std, struct standard_subheader *sub, struct _prizm_subheader *prizm, uint32_t *check) { - int err; + int err = g1m_error_alloc; uint8_t *data = NULL; /* read the subheader */ DREAD(lhd, g3l_lang_header) *check = g1m_checksum32(&lhd, sizeof(struct g3l_lang_header), *check); /* read the data */ - size_t data_size = sub->filesize - sizeof(struct standard_header) + size_t data_size = be32toh(sub->filesize) - sizeof(struct standard_header) - sizeof(struct standard_subheader) - sizeof(struct _prizm_subheader) - sizeof(struct g3l_lang_header) - 4; - uint8_t data[data_size]; + if (!(data = malloc(data_size))) goto fail; READ(data, data_size) *check = g1m_checksum32(data, data_size, *check); @@ -183,9 +183,11 @@ int g1m_decode_std_cg_fkey(g1m_t **h, g1m_buffer_t *buffer, } /* done */ + free(data); return (0); fail: + free(data); g1m_free(*h); *h = NULL; return (err); } diff --git a/src/decode/std/lang.c b/src/decode/std/lang.c index 8391327..a8472dc 100644 --- a/src/decode/std/lang.c +++ b/src/decode/std/lang.c @@ -105,17 +105,17 @@ int g1m_decode_std_cg_lang(g1m_t **h, g1m_buffer_t *buffer, struct standard_header *std, struct standard_subheader *sub, struct _prizm_subheader *prizm, uint32_t *check) { - int err; + int err = g1m_error_alloc; uint8_t *data = NULL; /* read the subheader */ DREAD(lhd, g3l_lang_header) *check = g1m_checksum32(&lhd, sizeof(struct g3l_lang_header), *check); /* read the data */ - size_t data_size = sub->filesize - sizeof(struct standard_header) + size_t data_size = be32toh(sub->filesize) - sizeof(struct standard_header) - sizeof(struct standard_subheader) - sizeof(struct _prizm_subheader) - sizeof(struct g3l_lang_header) - 4; - uint8_t data[data_size]; + if (!(data = malloc(data_size))) goto fail; READ(data, data_size) *check = g1m_checksum32(data, data_size, *check); @@ -148,9 +148,11 @@ int g1m_decode_std_cg_lang(g1m_t **h, g1m_buffer_t *buffer, } /* done */ + free(data); return (0); fail: + free(data); g1m_free(*h); *h = NULL; return (err); } diff --git a/src/manage/handle.c b/src/manage/handle.c index b681192..8b3889f 100644 --- a/src/manage/handle.c +++ b/src/manage/handle.c @@ -194,6 +194,7 @@ int g1m_make_addin(g1m_t **h, g1m_platform_t platform, memset(handle, 0, sizeof(g1m_t)); /* set basic options */ + handle->type = g1m_type_addin; handle->platform = platform; handle->version = *version; handle->creation_date = *created;