Add packaging files (deb)
[tools/deltarpm.git] / util.c
1 /*
2  * Copyright (c) 2004,2005 Michael Schroeder (mls@suse.de)
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #include "util.h"
15
16 /****************************************************************
17  *
18  * utility functions
19  *
20  */
21
22 void *
23 xmalloc(size_t len)
24 {
25   void *r = malloc(len ? len : 1);
26   if (r)
27     return r;
28   fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
29   exit(1);
30 }
31
32 void *
33 xmalloc2(size_t num, size_t len)
34 {
35   if (len && (num * len) / len != num)
36     {
37       fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
38       exit(1);
39     }
40   return xmalloc(num * len);
41 }
42
43 void *
44 xrealloc(void *old, size_t len)
45 {
46   if (old == 0)
47     old = malloc(len ? len : 1);
48   else
49     old = realloc(old, len ? len : 1);
50   if (old)
51     return old;
52   fprintf(stderr, "Out of memory reallocating %zu bytes!\n", len);
53   exit(1);
54 }
55
56 void *
57 xrealloc2(void *old, size_t num, size_t len)
58 {
59   if (len && (num * len) / len != num)
60     {
61       fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
62       exit(1);
63     }
64   return xrealloc(old, num * len);
65 }
66
67 void *
68 xcalloc(size_t num, size_t len)
69 {
70   void *r = calloc(num, len);
71   if (r)
72     return r;
73   fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
74   exit(1);
75 }
76
77 void *
78 xfree(void *mem)
79 {
80   if (mem)
81     free(mem);
82   return 0;
83 }
84
85 ssize_t
86 xread(int fd, void *buf, size_t l)
87 {
88   size_t ol = l;
89   ssize_t r;
90
91   while (l)
92     {
93       r = read(fd, buf, l); 
94       if (r < 0) 
95         {
96           if (errno == EINTR)
97             continue;
98           return r;
99         }
100       if (r == 0)
101         return ol - l;
102       buf += r;
103       l -= r;
104     }
105   return ol;
106 }
107
108 int
109 parsehex(char *s, unsigned char *hex, int len)
110 {
111   int i, r = 0;
112
113   len *= 2;
114   for (i = 0; ; i++, s++)
115     {
116       if (*s == 0 && !(i & 1))
117         return i / 2;
118       if (i == len)
119         {
120           fprintf(stderr, "parsehex: string too long\n");
121           exit(1);
122         }
123       if (*s >= '0' && *s <= '9')
124         r = (r << 4) | (*s - '0');
125       else if (*s >= 'a' && *s <= 'f')
126         r = (r << 4) | (*s - ('a' - 10));
127       else if (*s >= 'A' && *s <= 'F')
128         r = (r << 4) | (*s - ('a' - 10));
129       else
130         {
131           fprintf(stderr, "parsehex: bad string\n");
132           exit(1);
133         }
134       if ((i & 1) != 0)
135         {
136           hex[i / 2] = r;
137           r = 0;
138         }
139     }
140 }
141
142 void
143 parsemd5(char *s, unsigned char *md5)
144 {
145   if (!*s)
146     {
147       memset(md5, 0, 16);
148       return;
149     }
150   if (parsehex(s, md5, 16) != 16)
151     {
152       fprintf(stderr, "parsemd5: bad md5\n");
153       exit(1);
154     }
155 }
156
157 void
158 parsesha256(char *s, unsigned char *sha256)
159 {
160   if (!*s)
161     {
162       memset(sha256, 0, 32);
163       return;
164     }
165   if (parsehex(s, sha256, 32) != 32)
166     {
167       fprintf(stderr, "parsesha256: bad sha256\n");
168       exit(1);
169     }
170 }
171