aboutsummaryrefslogtreecommitdiffstats
path: root/util.c
blob: a8c3729380613178b14f433adaa3566497e0e343 (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
/* util.c  -- Various error checking wrappers and utilities.
 *
 *  Copyright (C) 2016 Tomasz Kramkowski <tk@the-tk.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Some contents of this file (indicated below) are based on work covered by
 * the following copyright and permission notice:
 *
 *  Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
 *
 *  Permission to use, copy, modify, and distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "log.h"
#include "util.h"

/* szadd: checked size_t addition */
size_t szadd(size_t a, size_t b)
{
	if (SIZE_MAX - a < b)
		error(0, EMSG_SZOP);

	return a + b;
}

/* szmul: checked size_t multiplication
 * License information can be found at the top of this file */
size_t szmul(size_t a, size_t b)
{
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof (size_t) * 4))
	if ((a >= MUL_NO_OVERFLOW || b >= MUL_NO_OVERFLOW) &&
		a > 0 && SIZE_MAX / a < b)
		error(0, EMSG_SZOP);
#undef MUL_NO_OVERFLOW

	return a * b;
}

/* xstrmalloc: allocate for a string (+1) or exit with error */
void *xstrmalloc(size_t size)
{
	size_t nsize;

	nsize = size + 1;
	if (nsize < size)
		error(0, EMSG_ALLOC);

	return xmalloc(nsize);
}

/* xstrdup: duplicae string or exit with error */
char *xstrdup(const char *s)
{
	char *ret;

	assert(s);

	ret = xstrmalloc(strlen(s));
	strcpy(ret, s);

	return ret;
}

/* xrealloc: reallocate or exit with error */
void *xrealloc(void *p, size_t size)
{
	p = realloc(p, size);
	if (!p)
		error(errno, EMSG_ALLOC);

	return p;
}

/* xmalloc: allocate or exit with error */
void *xmalloc(size_t size)
{
	return xrealloc(NULL, size);
}

/* xstrcut: cut out a bit of a string into a new string */
char *xstrcut(const char *s, size_t offt, size_t len)
{
	char *ret;

	ret = xstrmalloc(len);
	strncpy(ret, s + offt, len + 1);

	return ret;
}

/* xstrcasecmp: case insensitive strcmp */
int xstrcasecmp(const char *s1, const char *s2)
{
	while (toupper(*s1++) == toupper(*s2++))
		if (*s1 == '\0' || *s2 == '\0')
			break;
	if (*s1 == *s2)
		return 0;
	if (*s1 < *s2)
		return -1;
	return 1;
}