From c101c21a7e6b1078cc5ea31bc6154fee71c42cd4 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Wed, 22 Mar 2017 18:26:42 +0100 Subject: [PATCH] Changed add-in handle creation and updated decoding. --- include/libg1m.h | 3 +++ src/decode/std/addin.c | 27 ++++++--------------------- src/manage/handle.c | 18 ++++++++++++++++++ src/utils/date.c | 36 ++++++++++++++++++++++++++---------- src/utils/version.c | 32 ++++++++++++++++++++++++-------- 5 files changed, 77 insertions(+), 39 deletions(-) diff --git a/include/libg1m.h b/include/libg1m.h index e0b2342..c0b4cf5 100644 --- a/include/libg1m.h +++ b/include/libg1m.h @@ -99,6 +99,7 @@ extern int g1m_make_lang(g1m_t **handle, g1m_platform_t platform, int count); extern int g1m_make_picture(g1m_t **handle, unsigned int width, unsigned int height); extern int g1m_make_addin(g1m_t **h, g1m_platform_t platform, size_t size, + const char *name, const char *internal_name, const g1m_version_t *version, const time_t *created); /* encode a handle, get the extension */ @@ -167,10 +168,12 @@ extern int g1m_encode_picture(const uint32_t **pixels, unsigned int width, unsigned int height); /* Version decoding/encoding */ +extern int g1m_check_version(const char *raw); extern int g1m_decode_version(const char *raw, g1m_version_t *version); extern int g1m_encode_version(const g1m_version_t *version, char *raw); /* Date decoding/encoding */ +extern int g1m_check_date(const char *raw); extern int g1m_decode_date(const char *raw, time_t *date); extern int g1m_encode_date(const time_t *date, char *raw); diff --git a/src/decode/std/addin.c b/src/decode/std/addin.c index a24c960..890262f 100644 --- a/src/decode/std/addin.c +++ b/src/decode/std/addin.c @@ -46,16 +46,11 @@ int g1m_decode_std_addin(g1m_t **h, g1m_buffer_t *buffer, size_t content_size = hd.filesize; /* already corrected */ content_size -= sizeof(struct standard_header) + sizeof(struct g1a_subheader); - err = g1m_make_addin(h, g1m_platform_fx, content_size, &version, &created); + err = g1m_make_addin(h, g1m_platform_fx, content_size, + (char*)hd.title, (char*)hd.internal_name, &version, &created); if (err) return (err); g1m_t *handle = *h; - /* set more data */ - strncpy(handle->title, (char*)hd.title, 8); - handle->title[8] = 0; - strncpy(handle->intname, (char*)hd.internal_name, 8); - handle->intname[8] = 0; - /* log info about the subheader */ log_info("title is '%s'", handle->title); log_info("internal name is '%s'", handle->intname); @@ -111,16 +106,11 @@ int g1m_decode_std_cp_addin(g1m_t **h, g1m_buffer_t *buffer, g1m_version_t version; g1m_decode_version((char*)sub->version, &version); time_t created; g1m_decode_date((char*)sub->timestamp, &created); size_t content_size = be32toh(sub->filesize) - 0x1000; - err = g1m_make_addin(h, g1m_platform_cp, content_size, &version, &created); + err = g1m_make_addin(h, g1m_platform_cp, content_size, + (char*)sub->title, (char*)sub->internal_name, &version, &created); if (err) return (err); g1m_t *handle = *h; - /* copy other basic information */ - strncpy(handle->intname, (char*)sub->internal_name, 8); - handle->intname[9] = 0; - strncpy(handle->title, (char*)sub->title, 16); - handle->title[16] = 0; - /* decode pictures */ g1m_decode_picture(handle->icon_unsel, g1m_pictureformat_1bit_packed, cphd.icon, handle->width, handle->height); @@ -181,16 +171,11 @@ int g1m_decode_std_cg_addin(g1m_t **h, g1m_buffer_t *buffer, + sizeof(struct _prizm_subheader) + sizeof(struct g3a_subheader) + sizeof(uint32_t); log_info("Content size is %" PRIuSIZE, content_size); - err = g1m_make_addin(h, g1m_platform_cg, content_size, &version, &created); + err = g1m_make_addin(h, g1m_platform_cg, content_size, + (char*)sub->internal_name, (char*)sub->title, &version, &created); if (err) return (err); g1m_t *handle = *h; - /* copy other basic information */ - strncpy(handle->intname, (char*)sub->internal_name, 8); - handle->intname[9] = 0; - strncpy(handle->title, (char*)sub->title, 16); - handle->title[16] = 0; - /* decode pictures */ g1m_decode_picture(handle->icon_unsel, g1m_pictureformat_16bit, cghd.unselected_icon_image, handle->width, handle->height); diff --git a/src/manage/handle.c b/src/manage/handle.c index fb249f9..a5d1e01 100644 --- a/src/manage/handle.c +++ b/src/manage/handle.c @@ -17,6 +17,7 @@ * along with libg1m; if not, see . * ************************************************************************** */ #include +#include /* ************************************************************************** */ /* Make a handle */ @@ -174,12 +175,15 @@ fail: * @arg h pointer to the handle to create. * @arg platform the platform for which to make the add-in. * @arg size the code size. + * @arg name the name. + * @arg internal the internal name. * @arg version the version of the add-in. * @arg created the creation date of the add-in. * @return the error code (0 if ok). */ int g1m_make_addin(g1m_t **h, g1m_platform_t platform, size_t size, + const char *name, const char *internal, const g1m_version_t *version, const time_t *created) { *h = NULL; @@ -190,6 +194,8 @@ int g1m_make_addin(g1m_t **h, g1m_platform_t platform, size_t size, return (g1m_error_op); if (platform == g1m_platform_fx && size > 512 * 1024) return (g1m_error_op); + if (!isupper(name[0]) || internal[0] != '@' || !isupper(internal[1])) + return (g1m_error_op); /* allocate the handle */ *h = malloc(sizeof(g1m_t)); @@ -210,6 +216,7 @@ int g1m_make_addin(g1m_t **h, g1m_platform_t platform, size_t size, /* check the platform */ unsigned int width, height; + int titlesize = 8; switch (platform) { case g1m_platform_fx: width = G1A_ICON_WIDTH; @@ -218,12 +225,23 @@ int g1m_make_addin(g1m_t **h, g1m_platform_t platform, size_t size, case g1m_platform_cp: width = C1A_ICON_WIDTH; height = C1A_ICON_HEIGHT; + titlesize = 16; break; default: /* case g1m_platform_cg: */ width = G3A_ICON_WIDTH; height = G3A_ICON_HEIGHT; + titlesize = 16; } + /* copy the name and internal name */ + int i; for (i = 0; isupper(name[i]) && i < titlesize; i++) + handle->title[i] = name[i]; + handle->title[i] = 0; + handle->intname[0] = '@'; + for (i = 1; isupper(internal[i]) && i < 8; i++) + handle->intname[i] = internal[i]; + handle->intname[i] = 0; + /* allocate pictures */ handle->icon_unsel = alloc_pixels(width, height); if (!handle->icon_unsel) goto fail; diff --git a/src/utils/date.c b/src/utils/date.c index 123006f..466a608 100644 --- a/src/utils/date.c +++ b/src/utils/date.c @@ -19,6 +19,32 @@ #include #include +/** + * g1m_check_date: + * Check if a date string is well formatted. + * + * @arg c the string to check. + * @return if there was an error. + */ + +int g1m_check_date(const char *c) +{ + /* check length */ + if (memchr(c, 0, 14)) return (g1m_error_op); + + /* check characters */ + if (!isdigit(c[0]) || !isdigit(c[1]) || !isdigit(c[2]) + || !isdigit(c[3]) || c[4] != '.' + || !isdigit(c[5]) || !isdigit(c[6]) || !isdigit(c[7]) + || !isdigit(c[8]) || c[9] != '.' + || !isdigit(c[10]) || !isdigit(c[11]) || !isdigit(c[12]) + || !isdigit(c[13])) + return (g1m_error_op); + + /* no error! */ + return (0); +} + /** * g1m_decode_date: * Decode a date from a string. @@ -30,16 +56,6 @@ int g1m_decode_date(const char *c, time_t *t) { - /* checks */ - if (memchr(c, 0, 14)) return (g1m_error_op); - if (!isdigit(c[0]) || !isdigit(c[1]) || !isdigit(c[2]) - || !isdigit(c[3]) || c[4] != '.' - || !isdigit(c[5]) || !isdigit(c[6]) || !isdigit(c[7]) - || !isdigit(c[8]) || c[9] != '.' - || !isdigit(c[10]) || !isdigit(c[11]) || !isdigit(c[12]) - || !isdigit(c[13])) - return (g1m_error_op); - /* helper values */ const int two = '0' + '0' * 10; const int four = two + '0' * 100 + '0' * 1000; diff --git a/src/utils/version.c b/src/utils/version.c index adc617d..e47f43e 100644 --- a/src/utils/version.c +++ b/src/utils/version.c @@ -19,6 +19,30 @@ #include #include +/** + * g1m_check_version: + * Check if a version string is well formatted. + * + * @arg raw the string to check. + * @return if there was an error. + */ + +int g1m_check_version(const char *raw) +{ + /* check length */ + if (memchr(raw, 0, 10)) return (g1m_error_op); + + /* check characters */ + if (!isdigit(raw[0]) || !isdigit(raw[1]) || raw[2] != '.' + || !isdigit(raw[3]) || !isdigit(raw[4]) || raw[5] != '.' + || !isdigit(raw[6]) || !isdigit(raw[7]) + || !isdigit(raw[8]) || !isdigit(raw[9])) + return (g1m_error_op); + + /* no error! */ + return (0); +} + /** * g1m_decode_version: * Decode a version from a 'MM.mm.ffff' formatted version string. @@ -30,14 +54,6 @@ int g1m_decode_version(const char *raw, g1m_version_t *version) { - /* checks */ - if (memchr(raw, 0, 10)) return (g1m_error_op); - if (!isdigit(raw[0]) || !isdigit(raw[1]) || raw[2] != '.' - || !isdigit(raw[3]) || !isdigit(raw[4]) || raw[5] != '.' - || !isdigit(raw[6]) || !isdigit(raw[7]) - || !isdigit(raw[8]) || !isdigit(raw[9])) - return (g1m_error_op); - /* helper values */ const int two = '0' + '0' * 10; const int four = two + '0' * 100 + '0' * 1000;