summaryrefslogtreecommitdiff
path: root/util.c
blob: 8f9405d7343253a6002910c1738f145cbcd9f789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2004,2005 Michael Schroeder (mls@suse.de)
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "util.h"

/****************************************************************
 *
 * utility functions
 *
 */

void *
xmalloc(size_t len)
{
  void *r = malloc(len ? len : 1);
  if (r)
    return r;
  fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
  exit(1);
}

void *
xmalloc2(size_t num, size_t len)
{
  if (len && (num * len) / len != num)
    {
      fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
      exit(1);
    }
  return xmalloc(num * len);
}

void *
xrealloc(void *old, size_t len)
{
  if (old == 0)
    old = malloc(len ? len : 1);
  else
    old = realloc(old, len ? len : 1);
  if (old)
    return old;
  fprintf(stderr, "Out of memory reallocating %zu bytes!\n", len);
  exit(1);
}

void *
xrealloc2(void *old, size_t num, size_t len)
{
  if (len && (num * len) / len != num)
    {
      fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
      exit(1);
    }
  return xrealloc(old, num * len);
}

void *
xcalloc(size_t num, size_t len)
{
  void *r = calloc(num, len);
  if (r)
    return r;
  fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
  exit(1);
}

void *
xfree(void *mem)
{
  if (mem)
    free(mem);
  return 0;
}

ssize_t
xread(int fd, void *buf, size_t l)
{
  size_t ol = l;
  ssize_t r;

  while (l)
    {
      r = read(fd, buf, l); 
      if (r < 0) 
	{
	  if (errno == EINTR)
	    continue;
          return r;
	}
      if (r == 0)
        return ol - l;
      buf += r;
      l -= r;
    }
  return ol;
}

int
parsehex(char *s, unsigned char *hex, int len)
{
  int i, r = 0;

  len *= 2;
  for (i = 0; ; i++, s++)
    {
      if (*s == 0 && !(i & 1))
	return i / 2;
      if (i == len)
	{
	  fprintf(stderr, "parsehex: string too long\n");
	  exit(1);
	}
      if (*s >= '0' && *s <= '9')
	r = (r << 4) | (*s - '0');
      else if (*s >= 'a' && *s <= 'f')
	r = (r << 4) | (*s - ('a' - 10));
      else if (*s >= 'A' && *s <= 'F')
	r = (r << 4) | (*s - ('a' - 10));
      else
	{
	  fprintf(stderr, "parsehex: bad string\n");
	  exit(1);
	}
      if ((i & 1) != 0)
	{
	  hex[i / 2] = r;
	  r = 0;
	}
    }
}

void
parsemd5(char *s, unsigned char *md5)
{
  if (!*s)
    {
      memset(md5, 0, 16);
      return;
    }
  if (parsehex(s, md5, 16) != 16)
    {
      fprintf(stderr, "parsemd5: bad md5\n");
      exit(1);
    }
}

void
parsesha256(char *s, unsigned char *sha256)
{
  if (!*s)
    {
      memset(sha256, 0, 32);
      return;
    }
  if (parsehex(s, sha256, 32) != 32)
    {
      fprintf(stderr, "parsesha256: bad sha256\n");
      exit(1);
    }
}