summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/buflib.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index dcef84d5c1..e7835c8a2e 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -65,7 +65,7 @@
65 * H - handle table entry pointer 65 * H - handle table entry pointer
66 * C - pointer to struct buflib_callbacks 66 * C - pointer to struct buflib_callbacks
67 * c - variable sized string identifier 67 * c - variable sized string identifier
68 * L2 - second length marker for string identifier 68 * L2 - length of the metadata
69 * crc - crc32 protecting buflib metadata integrity 69 * crc - crc32 protecting buflib metadata integrity
70 * X - actual payload 70 * X - actual payload
71 * Y - unallocated space 71 * Y - unallocated space
@@ -240,8 +240,8 @@ union buflib_data* handle_to_block(struct buflib_context* ctx, int handle)
240 * has already been allocated but not the data */ 240 * has already been allocated but not the data */
241 if (!data) 241 if (!data)
242 return NULL; 242 return NULL;
243 volatile size_t len = data[-2].val; 243
244 return data - (len + 4); 244 return data - data[-2].val;
245} 245}
246 246
247/* Shrink the handle table, returning true if its size was reduced, false if 247/* Shrink the handle table, returning true if its size was reduced, false if
@@ -627,15 +627,15 @@ buffer_alloc:
627 /* Set up the allocated block, by marking the size allocated, and storing 627 /* Set up the allocated block, by marking the size allocated, and storing
628 * a pointer to the handle. 628 * a pointer to the handle.
629 */ 629 */
630 union buflib_data *name_len_slot, *crc_slot; 630 union buflib_data *header_size_slot, *crc_slot;
631 block->val = size; 631 block->val = size;
632 block[1].handle = handle; 632 block[1].handle = handle;
633 block[2].ops = ops; 633 block[2].ops = ops;
634 if (name_len > 0) 634 if (name_len > 0)
635 strcpy(block[3].name, name); 635 strcpy(block[3].name, name);
636 name_len_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len); 636 header_size_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len);
637 name_len_slot->val = 1 + name_len/sizeof(union buflib_data); 637 header_size_slot->val = 5 + name_len/sizeof(union buflib_data);
638 crc_slot = (union buflib_data*)(name_len_slot + 1); 638 crc_slot = (union buflib_data*)(header_size_slot + 1);
639 crc_slot->crc = crc_32((void *)block, 639 crc_slot->crc = crc_32((void *)block,
640 (crc_slot - block)*sizeof(union buflib_data), 640 (crc_slot - block)*sizeof(union buflib_data),
641 0xffffffff); 641 0xffffffff);
@@ -742,7 +742,7 @@ static size_t
742free_space_at_end(struct buflib_context* ctx) 742free_space_at_end(struct buflib_context* ctx)
743{ 743{
744 /* subtract 5 elements for 744 /* subtract 5 elements for
745 * val, handle, name_len, ops and the handle table entry*/ 745 * val, handle, meta_len, ops and the handle table entry*/
746 ptrdiff_t diff = (ctx->last_handle - ctx->alloc_end - 5); 746 ptrdiff_t diff = (ctx->last_handle - ctx->alloc_end - 5);
747 diff -= 16; /* space for future handles */ 747 diff -= 16; /* space for future handles */
748 diff *= sizeof(union buflib_data); /* make it bytes */ 748 diff *= sizeof(union buflib_data); /* make it bytes */
@@ -933,9 +933,11 @@ const char* buflib_get_name(struct buflib_context *ctx, int handle)
933{ 933{
934 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data)); 934 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data));
935 size_t len = data[-2].val; 935 size_t len = data[-2].val;
936 if (len <= 1) 936 if (len <= 5)
937 return NULL; 937 return NULL;
938 return data[-len-1].name; 938
939 data -= len;
940 return data[3].name;
939} 941}
940 942
941#ifdef DEBUG 943#ifdef DEBUG