2 * Copyright (c) 2004,2005 Michael Schroeder (mls@suse.de)
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
16 /****************************************************************
25 void *r = malloc(len ? len : 1);
28 fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
33 xmalloc2(size_t num, size_t len)
35 if (len && (num * len) / len != num)
37 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
40 return xmalloc(num * len);
44 xrealloc(void *old, size_t len)
47 old = malloc(len ? len : 1);
49 old = realloc(old, len ? len : 1);
52 fprintf(stderr, "Out of memory reallocating %zu bytes!\n", len);
57 xrealloc2(void *old, size_t num, size_t len)
59 if (len && (num * len) / len != num)
61 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
64 return xrealloc(old, num * len);
68 xcalloc(size_t num, size_t len)
70 void *r = calloc(num, len);
73 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
86 xread(int fd, void *buf, size_t l)
109 parsehex(char *s, unsigned char *hex, int len)
114 for (i = 0; ; i++, s++)
116 if (*s == 0 && !(i & 1))
120 fprintf(stderr, "parsehex: string too long\n");
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));
131 fprintf(stderr, "parsehex: bad string\n");
143 parsemd5(char *s, unsigned char *md5)
150 if (parsehex(s, md5, 16) != 16)
152 fprintf(stderr, "parsemd5: bad md5\n");
158 parsesha256(char *s, unsigned char *sha256)
162 memset(sha256, 0, 32);
165 if (parsehex(s, sha256, 32) != 32)
167 fprintf(stderr, "parsesha256: bad sha256\n");