chksum.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <unistd.h>
00006 
00007 #include "pool.h"
00008 #include "util.h"
00009 #include "chksum.h"
00010 
00011 #include "md5.h"
00012 #include "sha1.h"
00013 #include "sha2.h"
00014 
00015 struct ctxhandle {
00016   Id type;
00017   int done;
00018   unsigned char result[64];
00019   union {
00020     MD5_CTX md5;
00021     SHA1_CTX sha1;
00022     SHA256_CTX sha256;
00023   } c;
00024 };
00025 
00026 void *
00027 sat_chksum_create(Id type)
00028 {
00029   struct ctxhandle *h;
00030   h = sat_calloc(1, sizeof(*h));
00031   h->type = type;
00032   switch(type)
00033     {
00034     case REPOKEY_TYPE_MD5:
00035       sat_MD5_Init(&h->c.md5);
00036       return h;
00037     case REPOKEY_TYPE_SHA1:
00038       sat_SHA1_Init(&h->c.sha1);
00039       return h;
00040     case REPOKEY_TYPE_SHA256:
00041       sat_SHA256_Init(&h->c.sha256);
00042       return h;
00043     default:
00044       break;
00045     }
00046   free(h);
00047   return 0;
00048 }
00049 
00050 void *
00051 sat_chksum_create_from_bin(Id type, const unsigned char *buf)
00052 {
00053   struct ctxhandle *h;
00054   int l = sat_chksum_len(type);
00055   if (buf == 0 || l == 0)
00056     return 0;
00057   h = sat_calloc(1, sizeof(*h));
00058   h->type = type;
00059   h->done = 1;
00060   memcpy(h->result, buf, l);
00061   return h;
00062 }
00063 
00064 void
00065 sat_chksum_add(void *handle, const void *data, int len)
00066 {
00067   struct ctxhandle *h = handle;
00068   if (h->done)
00069     return;
00070   switch(h->type)
00071     {
00072     case REPOKEY_TYPE_MD5:
00073       sat_MD5_Update(&h->c.md5, (void *)data, len);
00074       return;
00075     case REPOKEY_TYPE_SHA1:
00076       sat_SHA1_Update(&h->c.sha1, data, len);
00077       return;
00078     case REPOKEY_TYPE_SHA256:
00079       sat_SHA256_Update(&h->c.sha256, data, len);
00080       return;
00081     default:
00082       return;
00083     }
00084 }
00085 
00086 const unsigned char *
00087 sat_chksum_get(void *handle, int *lenp)
00088 {
00089   struct ctxhandle *h = handle;
00090   if (h->done)
00091     {
00092       if (lenp)
00093         *lenp = sat_chksum_len(h->type);
00094       return h->result;
00095     }
00096   switch(h->type)
00097     {
00098     case REPOKEY_TYPE_MD5:
00099       sat_MD5_Final(h->result, &h->c.md5);
00100       h->done = 1;
00101       if (lenp)
00102         *lenp = 16;
00103       return h->result;
00104     case REPOKEY_TYPE_SHA1:
00105       sat_SHA1_Final(&h->c.sha1, h->result);
00106       h->done = 1;
00107       if (lenp)
00108         *lenp = 20;
00109       return h->result;
00110     case REPOKEY_TYPE_SHA256:
00111       sat_SHA256_Final(h->result, &h->c.sha256);
00112       h->done = 1;
00113       if (lenp)
00114         *lenp = 32;
00115       return h->result;
00116     default:
00117       if (lenp)
00118         *lenp = 0;
00119       return 0;
00120     }
00121 }
00122 
00123 Id
00124 sat_chksum_get_type(void *handle)
00125 {
00126   struct ctxhandle *h = handle;
00127   return h->type;
00128 }
00129 
00130 int
00131 sat_chksum_isfinished(void *handle)
00132 {
00133   struct ctxhandle *h = handle;
00134   return h->done != 0;
00135 }
00136 
00137 const char *
00138 sat_chksum_type2str(Id type)
00139 {
00140   switch(type)
00141     {
00142     case REPOKEY_TYPE_MD5:
00143       return "md5";
00144     case REPOKEY_TYPE_SHA1:
00145       return "sha1";
00146     case REPOKEY_TYPE_SHA256:
00147       return "sha256";
00148     default:
00149       return 0;
00150     }
00151 }
00152 
00153 Id
00154 sat_chksum_str2type(const char *str)
00155 {
00156   if (!strcasecmp(str, "md5"))
00157     return REPOKEY_TYPE_MD5;
00158   if (!strcasecmp(str, "sha") || !strcasecmp(str, "sha1"))
00159     return REPOKEY_TYPE_SHA1;
00160   if (!strcasecmp(str, "sha256"))
00161     return REPOKEY_TYPE_SHA256;
00162   return 0;
00163 }
00164 
00165 void *
00166 sat_chksum_free(void *handle, unsigned char *cp)
00167 {
00168   if (cp)
00169     {
00170       const unsigned char *res;
00171       int l;
00172       res = sat_chksum_get(handle, &l);
00173       if (l && res)
00174         memcpy(cp, res, l);
00175     }
00176   sat_free(handle);
00177   return 0;
00178 }
00179 

Generated on Mon Dec 15 17:56:24 2014 for satsolver by  doxygen 1.5.6