]> mj.ucw.cz Git - libucw.git/blob - images/sig-fb.c
safe signatures aligning in the indexer
[libucw.git] / images / sig-fb.c
1 /*
2  *      Image Library -- Computation of image signatures
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "sherlock/sherlock.h"
13 #include "lib/fastbuf.h"
14 #include "images/images.h"
15 #include "images/signature.h"
16
17 uns
18 image_signature_read(struct fastbuf *fb, struct image_signature *sig)
19 {
20   uns size = image_signature_size(bpeekc(sig));
21   breadb(fb, sig, size);
22 #ifndef CPU_ALLOW_UNALIGNED
23 #define FIX_U16(x) PUT_U16(&(x), x)
24   FIX_U16(sig->dh);
25   struct image_region *reg = sig->reg;
26   for (uns i = 0; i < sig->len; i++, reg++)
27     {
28       for (uns j = 0; j < IMAGE_REG_H; j++)
29         FIX_U16(reg->h[j]);
30       FIX_U16(reg->wa);
31       FIX_U16(reg->wb);
32     }
33 #undef FIX_U16  
34 #endif
35   return size;
36 }
37
38 uns
39 image_signature_write(struct fastbuf *fb, struct image_signature *sig)
40 {
41   uns size = image_signature_size(sig->len);
42 #ifdef CPU_ALLOW_UNALIGNED
43   bwrite(fb, sig, size);
44 #else
45   struct image_signature tmp;
46   memcpy(tmp, sig, size);
47 #define FIX_U16(x) do { x = GET_U16(&(x)); } while(0)
48   FIX_U16(tmp.dh);
49   struct image_region *reg = tmp.reg;
50   for (uns i = 0; i < tmp.len; i++, reg++)
51     {
52       for (uns j = 0; j < IMAGE_REG_H; j++)
53         FIX_U16(reg->h[j]);
54       FIX_U16(reg->wa);
55       FIX_U16(reg->wb);
56     }
57   bwrite(fb, &tmp, size);
58 #undef FIX_U16
59 #endif
60   return size;
61 }