ao-tools/lib: Add ao_hex_image_cat function
authorKeith Packard <keithp@keithp.com>
Fri, 18 Mar 2016 17:52:03 +0000 (10:52 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 18 Mar 2016 17:52:03 +0000 (10:52 -0700)
This takes two images and constructs a third containing the union of
the contents along with 0xff bytes for any area between them.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/lib/ao-hex.c
ao-tools/lib/ao-hex.h

index 5cfc63c1347e999e70b7dd2da2e4c497b33925f1..2ceed7ab4f2ba9e1791c3283e23b735e93a2aaea 100644 (file)
@@ -294,7 +294,7 @@ load_symbols(struct ao_hex_file *hex,
        struct ao_sym           *symbol;
        int                     num_symbols = 0;
        int                     size_symbols = 0;
-       
+
        extended_addr = 0;
        for (i = 0; i < hex->nrecord; i++) {
                record = hex->records[i];
@@ -451,6 +451,31 @@ ao_hex_image_free(struct ao_hex_image *image)
        free(image);
 }
 
+uint32_t min(uint32_t a, uint32_t b) { return a < b ? a : b; }
+uint32_t max(uint32_t a, uint32_t b) { return a > b ? a : b; }
+
+struct ao_hex_image *
+ao_hex_image_cat(struct ao_hex_image *a, struct ao_hex_image *b)
+{
+       struct ao_hex_image     *n;
+       uint32_t                base, bound;
+       uint32_t                length;
+
+       base = min(a->address, b->address);
+       bound = max(a->address + a->length, b->address + b->length);
+       length = bound - base;
+
+       n = calloc (sizeof (struct ao_hex_image) + length, 1);
+       if (!n)
+               return NULL;
+       n->address = base;
+       n->length = length;
+       memset(n->data, 0xff, length);
+       memcpy(n->data + a->address - n->address, a->data, a->length);
+       memcpy(n->data + b->address - n->address, b->data, b->length);
+       return n;
+}
+
 int
 ao_hex_image_equal(struct ao_hex_image *a, struct ao_hex_image *b)
 {
@@ -471,7 +496,7 @@ ao_hex_load(char *filename, struct ao_sym **symbols, int *num_symbolsp)
        file = fopen (filename, "r");
        if (!file)
                return NULL;
-       
+
        hex_file = ao_hex_file_read(file, filename);
        fclose(file);
        if (!hex_file)
@@ -549,7 +574,7 @@ ao_hex_file_create(struct ao_hex_image *image, struct ao_sym *symbols, int num_s
        ao_hex_record_set_checksum(record);
 
        hex_file->records[nrecord++] = record;
-       
+
        /* Add the symbols
         */
 
@@ -616,7 +641,7 @@ ao_hex_save(FILE *file, struct ao_hex_image *image,
                        goto write_failed;
        }
        ret = true;
-       
+
        if (fflush(file) != 0)
                ret = false;
 write_failed:
index eb510ba2a18321dc2614f8ac17a3adcb3f14e7b4..a1ab490ceab707c30630241f91fa04cea6b69225 100644 (file)
@@ -71,6 +71,9 @@ ao_hex_image_free(struct ao_hex_image *image);
 struct ao_hex_image *
 ao_hex_load(char *filename, struct ao_sym **symbols, int *num_symbols);
 
+struct ao_hex_image *
+ao_hex_image_cat(struct ao_hex_image *a, struct ao_hex_image *b);
+
 int
 ao_hex_image_equal(struct ao_hex_image *a, struct ao_hex_image *b);