From c08e5b6cf16ac7fdf810a142bf2f1f48951ba3fc Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Tue, 12 Sep 2006 11:54:49 +0200 Subject: [PATCH] safe signatures aligning --- images/Makefile | 2 +- images/object.c | 40 ++++++++++++++------- images/object.h | 4 ++- images/sig-dump.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++ images/sig-fb.c | 61 ------------------------------- images/signature.h | 15 ++++---- 6 files changed, 130 insertions(+), 82 deletions(-) delete mode 100644 images/sig-fb.c diff --git a/images/Makefile b/images/Makefile index de7ea49b..8b7efbdd 100644 --- a/images/Makefile +++ b/images/Makefile @@ -15,7 +15,7 @@ PROGS+=$(o)/images/image-sim-test LIBIMAGES_MODS+=sig-cmp endif ifneq ($(CONFIG_IMAGES_DUP)$(CONFIG_IMAGES_SIM),) -LIBIMAGES_MODS+=sig-dump sig-fb sig-init sig-seg sig-txt +LIBIMAGES_MODS+=sig-dump sig-init sig-seg sig-txt endif LIBIMAGES_LIBS=-lm -lpthread diff --git a/images/object.c b/images/object.c index d817e1c9..5e0fa825 100644 --- a/images/object.c +++ b/images/object.c @@ -81,27 +81,43 @@ read_image_obj_thumb(struct image_obj_info *ioi, struct fastbuf *fb, struct imag return img; error: DBG("Failed to decompress thumbnail: %s", io->thread->err_msg); - return NULL; + return NULL; } -void -put_image_obj_signature(struct odes *o, struct image_signature *sig) +uns +encode_image_obj_signature(byte *buf, struct image_signature *sig) { /* signatures should be short enough to fit one attribute */ - byte buf[MAX_ATTR_SIZE]; - uns size = image_signature_size(sig->len); + byte tmp[sizeof(struct image_signature)]; + uns size = put_image_signature(tmp, sig); ASSERT(MAX_ATTR_SIZE > BASE224_ENC_LENGTH(size)); - buf[base224_encode(buf, (byte *)sig, size)] = 0; - obj_set_attr(o, 'H', buf); + uns len = base224_encode(buf, tmp, size); + buf[len] = 0; + return len; } uns -get_image_obj_signature(struct image_signature *sig, struct odes *o) +decode_image_obj_signature(byte *buf, struct image_signature *sig) { - byte *a = obj_find_aval(o, 'H'); - if (!a) + if (!buf) return 0; - UNUSED uns size = base224_decode((byte *)sig, a, strlen(a)); - ASSERT(size == image_signature_size(sig->len)); + byte tmp[sizeof(struct image_signature)]; + UNUSED uns size = base224_decode(tmp, buf, strlen(buf)); + ASSERT(size == image_signature_size(*tmp)); + get_image_signature(tmp, sig); return 1; } + +void +put_image_obj_signature(struct odes *o, struct image_signature *sig) +{ + byte buf[MAX_ATTR_SIZE]; + encode_image_obj_signature(buf, sig); + obj_set_attr(o, 'H', buf); +} + +uns +get_image_obj_signature(struct odes *o, struct image_signature *sig) +{ + return decode_image_obj_signature(obj_find_aval(o, 'H'), sig); +} diff --git a/images/object.h b/images/object.h index 189b3c0b..2f2bc0b1 100644 --- a/images/object.h +++ b/images/object.h @@ -21,7 +21,9 @@ struct image_signature; uns get_image_obj_info(struct image_obj_info *ioi, struct odes *o); uns get_image_obj_thumb(struct image_obj_info *ioi, struct odes *o, struct mempool *pool); struct image *read_image_obj_thumb(struct image_obj_info *ioi, struct fastbuf *fb, struct image_io *io, struct mempool *pool); +uns encode_image_obj_signature(byte *buf, struct image_signature *sig); +uns decode_image_obj_signature(byte *buf, struct image_signature *sig); void put_image_obj_signature(struct odes *o, struct image_signature *sig); -uns get_image_obj_signature(struct image_signature *sig, struct odes *o); +uns get_image_obj_signature(struct odes *o, struct image_signature *sig); #endif diff --git a/images/sig-dump.c b/images/sig-dump.c index a150f612..5c35eefc 100644 --- a/images/sig-dump.c +++ b/images/sig-dump.c @@ -8,6 +8,8 @@ */ #include "lib/lib.h" +#include "lib/fastbuf.h" +#include "lib/unaligned.h" #include "images/images.h" #include "images/signature.h" #include @@ -50,3 +52,91 @@ image_region_dump(byte *buf, struct image_region *reg) *p = 0; return buf; } + +uns +get_image_signature(byte *buf, struct image_signature *sig) +{ + uns size = image_signature_size(*buf); + memcpy(sig, buf, size); +#ifndef CPU_ALLOW_UNALIGNED +#define FIX_U16(x) PUT_U16(&(x), x) + FIX_U16(sig->dh); + struct image_region *reg = sig->reg; + for (uns i = 0; i < sig->len; i++, reg++) + { + for (uns j = 0; j < IMAGE_REG_H; j++) + FIX_U16(reg->h[j]); + FIX_U16(reg->wa); + FIX_U16(reg->wb); + } +#undef FIX_U16 +#endif + return size; +} + +uns +put_image_signature(byte *buf, struct image_signature *sig) +{ + uns size = image_signature_size(sig->len); + memcpy(buf, sig, size); +#ifndef CPU_ALLOW_UNALIGNED +#define FIX_U16(x) do { x = GET_U16(&(x)); } while(0) + struct image_signature *tmp = (struct image_signature *)buf; + FIX_U16(tmp->dh); + struct image_region *reg = tmp->reg; + for (uns i = 0; i < tmp->len; i++, reg++) + { + for (uns j = 0; j < IMAGE_REG_H; j++) + FIX_U16(reg->h[j]); + FIX_U16(reg->wa); + FIX_U16(reg->wb); + } +#undef FIX_U16 +#endif + return size; +} +uns +bget_image_signature(struct fastbuf *fb, struct image_signature *sig) +{ + uns size = image_signature_size(bpeekc(sig)); + breadb(fb, sig, size); +#ifndef CPU_ALLOW_UNALIGNED +#define FIX_U16(x) PUT_U16(&(x), x) + FIX_U16(sig->dh); + struct image_region *reg = sig->reg; + for (uns i = 0; i < sig->len; i++, reg++) + { + for (uns j = 0; j < IMAGE_REG_H; j++) + FIX_U16(reg->h[j]); + FIX_U16(reg->wa); + FIX_U16(reg->wb); + } +#undef FIX_U16 +#endif + return size; +} + +uns +bput_image_signature(struct fastbuf *fb, struct image_signature *sig) +{ + uns size = image_signature_size(sig->len); +#ifdef CPU_ALLOW_UNALIGNED + bwrite(fb, sig, size); +#else + struct image_signature tmp; + memcpy(tmp, sig, size); +#define FIX_U16(x) do { x = GET_U16(&(x)); } while(0) + FIX_U16(tmp.dh); + struct image_region *reg = tmp.reg; + for (uns i = 0; i < tmp.len; i++, reg++) + { + for (uns j = 0; j < IMAGE_REG_H; j++) + FIX_U16(reg->h[j]); + FIX_U16(reg->wa); + FIX_U16(reg->wb); + } + bwrite(fb, &tmp, size); +#undef FIX_U16 +#endif + return size; +} diff --git a/images/sig-fb.c b/images/sig-fb.c deleted file mode 100644 index 75dcdeb8..00000000 --- a/images/sig-fb.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Image Library -- Computation of image signatures - * - * (c) 2006 Pavel Charvat - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#undef LOCAL_DEBUG - -#include "sherlock/sherlock.h" -#include "lib/fastbuf.h" -#include "images/images.h" -#include "images/signature.h" - -uns -image_signature_read(struct fastbuf *fb, struct image_signature *sig) -{ - uns size = image_signature_size(bpeekc(sig)); - breadb(fb, sig, size); -#ifndef CPU_ALLOW_UNALIGNED -#define FIX_U16(x) PUT_U16(&(x), x) - FIX_U16(sig->dh); - struct image_region *reg = sig->reg; - for (uns i = 0; i < sig->len; i++, reg++) - { - for (uns j = 0; j < IMAGE_REG_H; j++) - FIX_U16(reg->h[j]); - FIX_U16(reg->wa); - FIX_U16(reg->wb); - } -#undef FIX_U16 -#endif - return size; -} - -uns -image_signature_write(struct fastbuf *fb, struct image_signature *sig) -{ - uns size = image_signature_size(sig->len); -#ifdef CPU_ALLOW_UNALIGNED - bwrite(fb, sig, size); -#else - struct image_signature tmp; - memcpy(tmp, sig, size); -#define FIX_U16(x) do { x = GET_U16(&(x)); } while(0) - FIX_U16(tmp.dh); - struct image_region *reg = tmp.reg; - for (uns i = 0; i < tmp.len; i++, reg++) - { - for (uns j = 0; j < IMAGE_REG_H; j++) - FIX_U16(reg->h[j]); - FIX_U16(reg->wa); - FIX_U16(reg->wb); - } - bwrite(fb, &tmp, size); -#undef FIX_U16 -#endif - return size; -} diff --git a/images/signature.h b/images/signature.h index 94a0813d..868f4d1c 100644 --- a/images/signature.h +++ b/images/signature.h @@ -100,32 +100,33 @@ struct image_sig_data { u32 f[IMAGE_VEC_F]; }; -/* sig-fb.c */ - #define IMAGE_VECTOR_SIZE (sizeof(struct image_vector)) static inline uns -image_vector_read(struct fastbuf *fb, struct image_vector *vec) +bget_image_vector(struct fastbuf *fb, struct image_vector *vec) { breadb(fb, vec, sizeof(*vec)); return IMAGE_VECTOR_SIZE; } static inline uns -image_vector_write(struct fastbuf *fb, struct image_vector *vec) +bput_image_vector(struct fastbuf *fb, struct image_vector *vec) { bwrite(fb, vec, sizeof(*vec)); return IMAGE_VECTOR_SIZE; } static inline uns -image_signature_peek_size(struct fastbuf *fb) +bpeek_image_signature(struct fastbuf *fb) { return image_signature_size(bpeekc(fb)); } -uns image_signature_read(struct fastbuf *fb, struct image_signature *sig); -uns image_signature_write(struct fastbuf *fb, struct image_signature *sig); +uns get_image_signature(byte *buf, struct image_signature *sig); +uns put_image_signature(byte *buf, struct image_signature *sig); + +uns bget_image_signature(struct fastbuf *fb, struct image_signature *sig); +uns bput_image_signature(struct fastbuf *fb, struct image_signature *sig); /* sig-init.c */ -- 2.39.2