diff options
author | Alejandro Mery <amery@geeks.cl> | 2012-05-07 11:19:50 +0400 |
---|---|---|
committer | Alejandro Mery <amery@geeks.cl> | 2012-05-07 11:39:02 +0400 |
commit | 049f0daf134cfca03104e0a43b1e284304270b7f (patch) | |
tree | 18ed891087200800188ae28c0e572e59a9bb5f8a /script_bin.c | |
parent | 4484aac3fdc8b29ac78c6799883073511b1b60f4 (diff) |
fex2bin: refactored generate_bin() as calculate_bin_size() into script_bin.c
Diffstat (limited to 'script_bin.c')
-rw-r--r-- | script_bin.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/script_bin.c b/script_bin.c new file mode 100644 index 0000000..5face09 --- /dev/null +++ b/script_bin.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2012 Alejandro Mery <amery@geeks.cl> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "sunxi-tools.h" + +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> + +#include "script.h" +#include "script_bin.h" + +#define pr_info(...) fprintf(stderr, "fex2bin: " __VA_ARGS__) +#define pr_err(...) pr_info("E: " __VA_ARGS__) + +#define WORDS(S) (((S)+(sizeof(uint32_t)-1))/(sizeof(uint32_t))) + +/** + */ +size_t calculate_bin_size(struct script *script, + size_t *sections, size_t *entries) +{ + size_t words = 0, bin_size = 0; + struct list_entry *ls, *le; + struct script_section *section; + struct script_entry *entry; + struct script_string_entry *string; + + *sections = *entries = 0; + + /* count */ + for (ls = list_first(&script->sections); ls; + ls = list_next(&script->sections, ls)) { + section = container_of(ls, struct script_section, sections); + size_t c = 0; + + for (le = list_first(§ion->entries); le; + le = list_next(§ion->entries, le)) { + size_t size = 0; + entry = container_of(le, struct script_entry, entries); + c++; + + switch(entry->type) { + case SCRIPT_VALUE_TYPE_NULL: + case SCRIPT_VALUE_TYPE_SINGLE_WORD: + size = sizeof(uint32_t); + break; + case SCRIPT_VALUE_TYPE_STRING: + string = container_of(entry, struct script_string_entry, + entry); + size = string->l; + break; + case SCRIPT_VALUE_TYPE_GPIO: + size = sizeof(struct script_bin_gpio_value); + break; + default: + abort(); + } + words += WORDS(size); + } + if (c>0) { + *sections += 1; + *entries += c; + } + } + + bin_size = sizeof(struct script_bin_head) + + (*sections)*sizeof(struct script_bin_section) + + (*entries)*sizeof(struct script_bin_entry) + + words*sizeof(uint32_t); + pr_info("sections:%zu entries:%zu data:%zu/%zu -> %zu\n", + *sections, *entries, words, words*sizeof(uint32_t), + bin_size); + return bin_size; +} +#undef WORDS |