Skip to content

Commit

Permalink
[appvar] add comment string, fix compression
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Nov 14, 2024
1 parent 45ad92e commit 0b17414
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 54 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ It primarily is used for the TI-84+CE and related calculator series, however can
: 'false' leaves it unarchived.
: Default is 'false'.

comment: <string> : AppVar 8xv comment section string

--------------------------------------------------------------------------------

Credits:
Expand Down
30 changes: 3 additions & 27 deletions src/appvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ int appvar_write(struct appvar *a, const char *path)
static uint8_t output[APPVAR_MAX_FILE_SIZE];
uint32_t checksum;
FILE *fdv = NULL;
size_t name_size;
size_t file_size;
size_t data_size;
size_t varb_size;
Expand All @@ -76,40 +75,17 @@ int appvar_write(struct appvar *a, const char *path)

memset(output, 0, sizeof output);

if (a->compress != COMPRESS_NONE)
{
size_t size = a->size;

LOG_INFO(" - Size before compression: %u bytes\n", (unsigned int)a->size);

if (compress_array(a->data, &size, a->compress))
{
LOG_ERROR("Failed to compress data for AppVar \'%s\'.\n", a->name);
goto error;
}

a->size = size;

LOG_INFO(" - Size after compression: %u bytes\n", (unsigned int)a->size);
}

if (a->size > APPVAR_MAX_DATA_SIZE)
{
LOG_ERROR("Too much data for AppVar \'%s\'.\n", a->name);
goto error;
}

file_size = a->size + APPVAR_DATA_POS + APPVAR_CHECKSUM_LEN;
data_size = a->size + APPVAR_VAR_HEADER_LEN + APPVAR_VARB_SIZE_LEN;
var_size = a->size + APPVAR_VARB_SIZE_LEN;
varb_size = a->size;

name_size = strlen(a->name) > 8 ? 8 : strlen(a->name);

memcpy(output + APPVAR_FILE_HEADER_POS, file_header, sizeof file_header);
memcpy(output + APPVAR_NAME_POS, a->name, name_size);
memcpy(output + APPVAR_COMMENT_POS, a->comment, APPVAR_MAX_COMMENT_SIZE);
memcpy(output + APPVAR_NAME_POS, a->name, APPVAR_MAX_NAME_SIZE);
memcpy(output + APPVAR_DATA_POS, a->data, varb_size);


output[APPVAR_VAR_HEADER_POS] = APPVAR_MAGIC;
output[APPVAR_TYPE_POS] = APPVAR_TYPE_FLAG;
output[APPVAR_ARCHIVE_POS] = APPVAR_ARCHIVE_FLAG;
Expand Down
7 changes: 6 additions & 1 deletion src/appvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ extern "C" {
#define APPVAR_MAX_FILE_SIZE (64 * 1024 + 300)
#define APPVAR_MAX_DATA_SIZE (65505)
#define APPVAR_MAX_BEFORE_COMPRESSION_SIZE (65505*4)
#define APPVAR_MAX_COMMENT_SIZE 42
#define APPVAR_MAX_NAME_SIZE 8

#define APPVAR_TYPE_FLAG 21
#define APPVAR_ARCHIVE_FLAG 128
Expand All @@ -53,6 +55,7 @@ extern "C" {
#define APPVAR_FILE_HEADER_LEN 55

#define APPVAR_FILE_HEADER_POS 0x00
#define APPVAR_COMMENT_POS 0x0B
#define APPVAR_DATA_SIZE_POS 0x35
#define APPVAR_VAR_HEADER_POS 0x37
#define APPVAR_VAR_SIZE0_POS 0x39
Expand All @@ -75,18 +78,20 @@ typedef enum

struct appvar
{
char *name;
char name[APPVAR_MAX_NAME_SIZE + 1];
char *header;
uint8_t *data;
bool archived;
bool init;
bool lut;
uint32_t size;
uint32_t uncompressed_size;
uint32_t entry_size;
uint32_t nr_entries;
uint32_t total_entries;
uint32_t header_size;
uint32_t data_offset;
char comment[APPVAR_MAX_COMMENT_SIZE + 1];
appvar_source_t source;
compress_mode_t compress;
};
Expand Down
2 changes: 2 additions & 0 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ static void options_show(const char *prgm)
LOG_PRINT(" : \'false\' leaves it unarchived.\n");
LOG_PRINT(" : Default is \'false\'.\n");
LOG_PRINT("\n");
LOG_PRINT(" comment: <string> : AppVar 8xv comment section string\n");
LOG_PRINT("\n");
LOG_PRINT("--------------------------------------------------------------------------------\n");
LOG_PRINT("\n");
LOG_PRINT("Credits:\n");
Expand Down
61 changes: 49 additions & 12 deletions src/output-appvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,17 @@ void output_appvar_c_include_file(struct output *output, FILE *fdh)
fprintf(fdh, "#endif\n");
fprintf(fdh, "\n");

fprintf(fdh, "#define %s_appvar_size %u\n",
appvar->name,
(unsigned int)appvar->size);

if (appvar->compress != COMPRESS_NONE)
{
fprintf(fdh, "#define %s_appvar_uncompressed_size %u\n",
appvar->name,
(unsigned int)appvar->uncompressed_size);
}

if (output->order == OUTPUT_PALETTES_FIRST)
{
output_appvar_c_include_file_palettes(output, fdh, &index);
Expand Down Expand Up @@ -904,6 +915,17 @@ void output_appvar_asm_include_file(struct output *output, FILE *fdh)
uint32_t nr_entries = 0;
bool order = output->order;

fprintf(fdh, "%s_appvar_size := %u\n",
appvar->name,
(unsigned int)appvar->size);

if (appvar->compress != COMPRESS_NONE)
{
fprintf(fdh, "%s_appvar_uncompressed_size := %u\n",
appvar->name,
(unsigned int)appvar->uncompressed_size);
}

for (uint32_t o = 0; o < 2; ++o)
{
if (order == OUTPUT_PALETTES_FIRST)
Expand Down Expand Up @@ -1068,16 +1090,37 @@ int output_appvar_include(struct output *output)
goto error;
}

appvar->uncompressed_size = appvar->size;

if (appvar->compress != COMPRESS_NONE)
{
size_t size = appvar->size;
void *original_data = appvar->data;

LOG_INFO(" - Compressing AppVar \'%s\'\n", appvar->name);

appvar->data = compress_array(original_data, &size, appvar->compress);
free(original_data);

if (appvar->data == NULL)
{
LOG_ERROR("Failed to compress AppVar.\n");
goto error;
}

appvar->size = size;
}

if (appvar->size > APPVAR_MAX_DATA_SIZE)
{
LOG_ERROR("Too much data for AppVar \'%s\'.\n", appvar->name);
goto error;
}

switch (appvar->source)
{
case APPVAR_SOURCE_C:
{
if (output->include_file == NULL)
{
LOG_ERROR("Missing \"include-file\" parameter for AppVar.\n");
goto error;
}

LOG_INFO(" - Writing \'%s\'\n", output->include_file);

fdh = clean_fopen(output->include_file, "wt");
Expand Down Expand Up @@ -1108,12 +1151,6 @@ int output_appvar_include(struct output *output)

case APPVAR_SOURCE_ASM:
{
if (output->include_file == NULL)
{
LOG_ERROR("Missing \"include-file\" parameter for AppVar.\n");
goto error;
}

LOG_INFO(" - Writing \'%s\'\n", output->include_file);

fdh = clean_fopen(output->include_file, "wt");
Expand Down
7 changes: 3 additions & 4 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ struct output *output_alloc(void)
output->order = OUTPUT_PALETTES_FIRST;
output->format = OUTPUT_FORMAT_INVALID;
output->constant = "";
output->appvar.name = NULL;
output->appvar.archived = true;
output->appvar.init = true;
output->appvar.source = APPVAR_SOURCE_NONE;
Expand All @@ -103,6 +102,9 @@ struct output *output_alloc(void)
output->appvar.entry_size = 3;
output->appvar.data = NULL;

memset(output->appvar.comment, 0, APPVAR_MAX_COMMENT_SIZE + 1);
memset(output->appvar.name, 0, APPVAR_MAX_NAME_SIZE + 1);

output->directory = strings_dup("");
if (output->directory == NULL)
{
Expand Down Expand Up @@ -192,9 +194,6 @@ void output_free(struct output *output)
free(output->palette_names);
output->palette_names = NULL;

free(output->appvar.name);
output->appvar.name = NULL;

free(output->appvar.header);
output->appvar.header = NULL;

Expand Down
15 changes: 5 additions & 10 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1319,18 +1319,9 @@ static int parse_output(struct yaml *yaml, yaml_document_t *doc, yaml_node_t *ro
parser_show_mark_error(keyn->start_mark);
return -1;
}

if (parse_str_cmp("name", key))
{
if (output->appvar.name != NULL)
{
free(output->appvar.name);
}
output->appvar.name = strings_dup(value);
if (output->appvar.name == NULL)
{
return -1;
}
strncpy(output->appvar.name, value, APPVAR_MAX_NAME_SIZE);
}
else if (parse_str_cmp("archived", key))
{
Expand Down Expand Up @@ -1397,6 +1388,10 @@ static int parse_output(struct yaml *yaml, yaml_document_t *doc, yaml_node_t *ro
return -1;
}
}
else if (parse_str_cmp("comment", key))
{
strncpy(output->appvar.comment, value, APPVAR_MAX_COMMENT_SIZE);
}
else if (parse_str_cmp("header-string", key))
{
char *header;
Expand Down

0 comments on commit 0b17414

Please sign in to comment.