]> mj.ucw.cz Git - libucw.git/blobdiff - images/object.c
Released as 6.5.16.
[libucw.git] / images / object.c
index 5e0fa825fc4773a8a729c84ce30016355103543b..ae7b021910dec8a85832599d69cfbba3d4cf005e 100644 (file)
@@ -9,18 +9,18 @@
 #undef LOCAL_DEBUG
 
 #include "sherlock/sherlock.h"
-#include "lib/base224.h"
-#include "lib/mempool.h"
-#include "lib/fastbuf.h"
+#include <ucw/base224.h>
+#include <ucw/mempool.h>
+#include <ucw/fastbuf.h>
 #include "sherlock/object.h"
-#include "images/images.h"
-#include "images/object.h"
-#include "images/color.h"
-#include "images/signature.h"
+#include <images/images.h>
+#include <images/object.h>
+#include <images/color.h>
+#include <images/signature.h>
 #include <stdio.h>
 #include <string.h>
 
-uns
+uint
 get_image_obj_info(struct image_obj_info *ioi, struct odes *o)
 {
   byte *v = obj_find_aval(o, 'G');
@@ -29,8 +29,8 @@ get_image_obj_info(struct image_obj_info *ioi, struct odes *o)
       DBG("Missing image info attribute");
       return 0;
     }
-  byte color_space[MAX_ATTR_SIZE], thumb_format[MAX_ATTR_SIZE];
-  UNUSED uns cnt = sscanf(v, "%d%d%s%d%d%d%s", &ioi->cols, &ioi->rows, color_space,
+  byte color_space[16], thumb_format[16];
+  UNUSED uint cnt = sscanf(v, "%d%d%s%d%d%d%s", &ioi->cols, &ioi->rows, color_space,
       &ioi->colors, &ioi->thumb_cols, &ioi->thumb_rows, thumb_format);
   ASSERT(cnt == 7);
   ioi->thumb_format = (*thumb_format == 'p') ? IMAGE_FORMAT_PNG : IMAGE_FORMAT_JPEG;
@@ -38,7 +38,7 @@ get_image_obj_info(struct image_obj_info *ioi, struct odes *o)
   return 1;
 }
 
-uns
+uint
 get_image_obj_thumb(struct image_obj_info *ioi, struct odes *o, struct mempool *pool)
 {
   struct oattr *a = obj_find_attr(o, 'N');
@@ -47,10 +47,14 @@ get_image_obj_thumb(struct image_obj_info *ioi, struct odes *o, struct mempool *
       DBG("Missing image thumbnail attribute");
       return 0;
     }
-  uns count = 0;
+  uint count = 0;
+  uint max_len = 0;
   for (struct oattr *b = a; b; b = b->same)
-    count++;
-  byte buf[count * MAX_ATTR_SIZE], *b = buf;
+    {
+      count++;
+      max_len += strlen(b->val);
+    }
+  byte buf[max_len + 1], *b = buf;
   for (; a; a = a->same)
     b += base224_decode(b, a->val, strlen(a->val));
   ASSERT(b != buf);
@@ -84,40 +88,23 @@ error:
   return NULL;
 }
 
-uns
-encode_image_obj_signature(byte *buf, struct image_signature *sig)
-{
-  /* signatures should be short enough to fit one attribute */
-  byte tmp[sizeof(struct image_signature)];
-  uns size = put_image_signature(tmp, sig);
-  ASSERT(MAX_ATTR_SIZE > BASE224_ENC_LENGTH(size));
-  uns len = base224_encode(buf, tmp, size);
-  buf[len] = 0;
-  return len;
-}
-
-uns
-decode_image_obj_signature(byte *buf, struct image_signature *sig)
-{
-  if (!buf)
-    return 0;
-  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);
+  /* signatures should be short enough to in a single attribute */
+  uint size = image_signature_size(sig->len);
+  byte buf[BASE224_ENC_LENGTH(size) + 1];
+  buf[base224_encode(buf, (byte *)sig, size)] = 0;
   obj_set_attr(o, 'H', buf);
 }
 
-uns
-get_image_obj_signature(struct odes *o, struct image_signature *sig)
+uint
+get_image_obj_signature(struct image_signature *sig, struct odes *o)
 {
-  return decode_image_obj_signature(obj_find_aval(o, 'H'), sig);
+  byte *a = obj_find_aval(o, 'H');
+  if (!a)
+    return 0;
+  UNUSED uint size = base224_decode((byte *)sig, a, strlen(a));
+  ASSERT(size == image_signature_size(sig->len));
+  return 1;
 }