Merge pull request #37 from miksa/master
[platform/upstream/libsolv.git] / src / chksum.c
index 7f28d16..2c6fa3e 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * Copyright (c) 2008-2012, Novell Inc.
+ *
+ * This program is licensed under the BSD license, read LICENSE.BSD
+ * for further information
+ */
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -24,21 +31,21 @@ struct ctxhandle {
 };
 
 void *
-sat_chksum_create(Id type)
+solv_chksum_create(Id type)
 {
   struct ctxhandle *h;
-  h = sat_calloc(1, sizeof(*h));
+  h = solv_calloc(1, sizeof(*h));
   h->type = type;
   switch(type)
     {
     case REPOKEY_TYPE_MD5:
-      sat_MD5_Init(&h->c.md5);
+      solv_MD5_Init(&h->c.md5);
       return h;
     case REPOKEY_TYPE_SHA1:
-      sat_SHA1_Init(&h->c.sha1);
+      solv_SHA1_Init(&h->c.sha1);
       return h;
     case REPOKEY_TYPE_SHA256:
-      sat_SHA256_Init(&h->c.sha256);
+      solv_SHA256_Init(&h->c.sha256);
       return h;
     default:
       break;
@@ -47,14 +54,30 @@ sat_chksum_create(Id type)
   return 0;
 }
 
+int
+solv_chksum_len(Id type)
+{
+  switch (type)
+    {   
+    case REPOKEY_TYPE_MD5:
+      return 16; 
+    case REPOKEY_TYPE_SHA1:
+      return 20; 
+    case REPOKEY_TYPE_SHA256:
+      return 32; 
+    default:
+      return 0;
+    }   
+}
+
 void *
-sat_chksum_create_from_bin(Id type, const unsigned char *buf)
+solv_chksum_create_from_bin(Id type, const unsigned char *buf)
 {
   struct ctxhandle *h;
-  int l = sat_chksum_len(type);
+  int l = solv_chksum_len(type);
   if (buf == 0 || l == 0)
     return 0;
-  h = sat_calloc(1, sizeof(*h));
+  h = solv_calloc(1, sizeof(*h));
   h->type = type;
   h->done = 1;
   memcpy(h->result, buf, l);
@@ -62,7 +85,7 @@ sat_chksum_create_from_bin(Id type, const unsigned char *buf)
 }
 
 void
-sat_chksum_add(void *handle, const void *data, int len)
+solv_chksum_add(void *handle, const void *data, int len)
 {
   struct ctxhandle *h = handle;
   if (h->done)
@@ -70,13 +93,13 @@ sat_chksum_add(void *handle, const void *data, int len)
   switch(h->type)
     {
     case REPOKEY_TYPE_MD5:
-      sat_MD5_Update(&h->c.md5, (void *)data, len);
+      solv_MD5_Update(&h->c.md5, (void *)data, len);
       return;
     case REPOKEY_TYPE_SHA1:
-      sat_SHA1_Update(&h->c.sha1, data, len);
+      solv_SHA1_Update(&h->c.sha1, data, len);
       return;
     case REPOKEY_TYPE_SHA256:
-      sat_SHA256_Update(&h->c.sha256, data, len);
+      solv_SHA256_Update(&h->c.sha256, data, len);
       return;
     default:
       return;
@@ -84,31 +107,31 @@ sat_chksum_add(void *handle, const void *data, int len)
 }
 
 const unsigned char *
-sat_chksum_get(void *handle, int *lenp)
+solv_chksum_get(void *handle, int *lenp)
 {
   struct ctxhandle *h = handle;
   if (h->done)
     {
       if (lenp)
-        *lenp = sat_chksum_len(h->type);
+        *lenp = solv_chksum_len(h->type);
       return h->result;
     }
   switch(h->type)
     {
     case REPOKEY_TYPE_MD5:
-      sat_MD5_Final(h->result, &h->c.md5);
+      solv_MD5_Final(h->result, &h->c.md5);
       h->done = 1;
       if (lenp)
        *lenp = 16;
       return h->result;
     case REPOKEY_TYPE_SHA1:
-      sat_SHA1_Final(&h->c.sha1, h->result);
+      solv_SHA1_Final(&h->c.sha1, h->result);
       h->done = 1;
       if (lenp)
        *lenp = 20;
       return h->result;
     case REPOKEY_TYPE_SHA256:
-      sat_SHA256_Final(h->result, &h->c.sha256);
+      solv_SHA256_Final(h->result, &h->c.sha256);
       h->done = 1;
       if (lenp)
        *lenp = 32;
@@ -121,14 +144,21 @@ sat_chksum_get(void *handle, int *lenp)
 }
 
 Id
-sat_chksum_get_type(void *handle)
+solv_chksum_get_type(void *handle)
 {
   struct ctxhandle *h = handle;
   return h->type;
 }
 
+int
+solv_chksum_isfinished(void *handle)
+{
+  struct ctxhandle *h = handle;
+  return h->done != 0;
+}
+
 const char *
-sat_chksum_type2str(Id type)
+solv_chksum_type2str(Id type)
 {
   switch(type)
     {
@@ -144,7 +174,7 @@ sat_chksum_type2str(Id type)
 }
 
 Id
-sat_chksum_str2type(const char *str)
+solv_chksum_str2type(const char *str)
 {
   if (!strcasecmp(str, "md5"))
     return REPOKEY_TYPE_MD5;
@@ -156,17 +186,17 @@ sat_chksum_str2type(const char *str)
 }
 
 void *
-sat_chksum_free(void *handle, unsigned char *cp)
+solv_chksum_free(void *handle, unsigned char *cp)
 {
   if (cp)
     {
       const unsigned char *res;
       int l;
-      res = sat_chksum_get(handle, &l);
+      res = solv_chksum_get(handle, &l);
       if (l && res)
         memcpy(cp, res, l);
     }
-  sat_free(handle);
+  solv_free(handle);
   return 0;
 }