- set pool_id2color, fix comments
[platform/upstream/libsolv.git] / src / chksum.c
1 /*
2  * Copyright (c) 2008-2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "pool.h"
15 #include "util.h"
16 #include "chksum.h"
17
18 #include "md5.h"
19 #include "sha1.h"
20 #include "sha2.h"
21
22 struct ctxhandle {
23   Id type;
24   int done;
25   unsigned char result[64];
26   union {
27     MD5_CTX md5;
28     SHA1_CTX sha1;
29     SHA256_CTX sha256;
30   } c;
31 };
32
33 void *
34 solv_chksum_create(Id type)
35 {
36   struct ctxhandle *h;
37   h = solv_calloc(1, sizeof(*h));
38   h->type = type;
39   switch(type)
40     {
41     case REPOKEY_TYPE_MD5:
42       solv_MD5_Init(&h->c.md5);
43       return h;
44     case REPOKEY_TYPE_SHA1:
45       solv_SHA1_Init(&h->c.sha1);
46       return h;
47     case REPOKEY_TYPE_SHA256:
48       solv_SHA256_Init(&h->c.sha256);
49       return h;
50     default:
51       break;
52     }
53   free(h);
54   return 0;
55 }
56
57 void *
58 solv_chksum_create_from_bin(Id type, const unsigned char *buf)
59 {
60   struct ctxhandle *h;
61   int l = solv_chksum_len(type);
62   if (buf == 0 || l == 0)
63     return 0;
64   h = solv_calloc(1, sizeof(*h));
65   h->type = type;
66   h->done = 1;
67   memcpy(h->result, buf, l);
68   return h;
69 }
70
71 void
72 solv_chksum_add(void *handle, const void *data, int len)
73 {
74   struct ctxhandle *h = handle;
75   if (h->done)
76     return;
77   switch(h->type)
78     {
79     case REPOKEY_TYPE_MD5:
80       solv_MD5_Update(&h->c.md5, (void *)data, len);
81       return;
82     case REPOKEY_TYPE_SHA1:
83       solv_SHA1_Update(&h->c.sha1, data, len);
84       return;
85     case REPOKEY_TYPE_SHA256:
86       solv_SHA256_Update(&h->c.sha256, data, len);
87       return;
88     default:
89       return;
90     }
91 }
92
93 const unsigned char *
94 solv_chksum_get(void *handle, int *lenp)
95 {
96   struct ctxhandle *h = handle;
97   if (h->done)
98     {
99       if (lenp)
100         *lenp = solv_chksum_len(h->type);
101       return h->result;
102     }
103   switch(h->type)
104     {
105     case REPOKEY_TYPE_MD5:
106       solv_MD5_Final(h->result, &h->c.md5);
107       h->done = 1;
108       if (lenp)
109         *lenp = 16;
110       return h->result;
111     case REPOKEY_TYPE_SHA1:
112       solv_SHA1_Final(&h->c.sha1, h->result);
113       h->done = 1;
114       if (lenp)
115         *lenp = 20;
116       return h->result;
117     case REPOKEY_TYPE_SHA256:
118       solv_SHA256_Final(h->result, &h->c.sha256);
119       h->done = 1;
120       if (lenp)
121         *lenp = 32;
122       return h->result;
123     default:
124       if (lenp)
125         *lenp = 0;
126       return 0;
127     }
128 }
129
130 Id
131 solv_chksum_get_type(void *handle)
132 {
133   struct ctxhandle *h = handle;
134   return h->type;
135 }
136
137 int
138 solv_chksum_isfinished(void *handle)
139 {
140   struct ctxhandle *h = handle;
141   return h->done != 0;
142 }
143
144 const char *
145 solv_chksum_type2str(Id type)
146 {
147   switch(type)
148     {
149     case REPOKEY_TYPE_MD5:
150       return "md5";
151     case REPOKEY_TYPE_SHA1:
152       return "sha1";
153     case REPOKEY_TYPE_SHA256:
154       return "sha256";
155     default:
156       return 0;
157     }
158 }
159
160 Id
161 solv_chksum_str2type(const char *str)
162 {
163   if (!strcasecmp(str, "md5"))
164     return REPOKEY_TYPE_MD5;
165   if (!strcasecmp(str, "sha") || !strcasecmp(str, "sha1"))
166     return REPOKEY_TYPE_SHA1;
167   if (!strcasecmp(str, "sha256"))
168     return REPOKEY_TYPE_SHA256;
169   return 0;
170 }
171
172 void *
173 solv_chksum_free(void *handle, unsigned char *cp)
174 {
175   if (cp)
176     {
177       const unsigned char *res;
178       int l;
179       res = solv_chksum_get(handle, &l);
180       if (l && res)
181         memcpy(cp, res, l);
182     }
183   solv_free(handle);
184   return 0;
185 }
186