xref: /minix3/crypto/external/bsd/netpgp/dist/src/libmj/defs.h (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /* $NetBSD: defs.h,v 1.1 2010/08/07 04:13:57 agc Exp $ */
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*-
4*ebfedea0SLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5*ebfedea0SLionel Sambuc  * All rights reserved.
6*ebfedea0SLionel Sambuc  *
7*ebfedea0SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*ebfedea0SLionel Sambuc  * by Alistair Crooks (agc@NetBSD.org)
9*ebfedea0SLionel Sambuc  *
10*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12*ebfedea0SLionel Sambuc  * are met:
13*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*ebfedea0SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*ebfedea0SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*ebfedea0SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*ebfedea0SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*ebfedea0SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*ebfedea0SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*ebfedea0SLionel Sambuc  */
31*ebfedea0SLionel Sambuc #ifndef DEFS_H_
32*ebfedea0SLionel Sambuc #define DEFS_H_
33*ebfedea0SLionel Sambuc 
34*ebfedea0SLionel Sambuc #include <sys/types.h>
35*ebfedea0SLionel Sambuc #include <sys/param.h>
36*ebfedea0SLionel Sambuc 
37*ebfedea0SLionel Sambuc #ifdef HAVE_INTTYPES_H
38*ebfedea0SLionel Sambuc #include <inttypes.h>
39*ebfedea0SLionel Sambuc #endif
40*ebfedea0SLionel Sambuc 
41*ebfedea0SLionel Sambuc #ifdef HAVE_STDINT_H
42*ebfedea0SLionel Sambuc #include <stdint.h>
43*ebfedea0SLionel Sambuc #endif
44*ebfedea0SLionel Sambuc 
45*ebfedea0SLionel Sambuc #include <stdio.h>
46*ebfedea0SLionel Sambuc #include <stdlib.h>
47*ebfedea0SLionel Sambuc #include <string.h>
48*ebfedea0SLionel Sambuc 
49*ebfedea0SLionel Sambuc #define NEWARRAY(type,ptr,size,where,action) do {			\
50*ebfedea0SLionel Sambuc 	if ((ptr = calloc(sizeof(type), (unsigned)(size))) == NULL) {	\
51*ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \
52*ebfedea0SLionel Sambuc 			where, (unsigned long)(size * sizeof(type)));	\
53*ebfedea0SLionel Sambuc 		action;							\
54*ebfedea0SLionel Sambuc 	}								\
55*ebfedea0SLionel Sambuc } while( /* CONSTCOND */ 0)
56*ebfedea0SLionel Sambuc 
57*ebfedea0SLionel Sambuc #define RENEW(type, _ptr, _size, _newsize, where, action) do {		\
58*ebfedea0SLionel Sambuc 	type *_newptr;							\
59*ebfedea0SLionel Sambuc 	_newptr = realloc(_ptr, (size_t)(_newsize) * sizeof(type));	\
60*ebfedea0SLionel Sambuc 	if (_newptr == NULL) {						\
61*ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "%s: can't realloc %lu bytes\n",	\
62*ebfedea0SLionel Sambuc 			where, (unsigned long)((_newsize) * sizeof(type))); \
63*ebfedea0SLionel Sambuc 		action;							\
64*ebfedea0SLionel Sambuc 	} else {							\
65*ebfedea0SLionel Sambuc 		(void) memset(&_newptr[_size], 0x0,			\
66*ebfedea0SLionel Sambuc 			(_newsize - _size) * sizeof(type));		\
67*ebfedea0SLionel Sambuc 		_ptr = _newptr;						\
68*ebfedea0SLionel Sambuc 		_size = _newsize;					\
69*ebfedea0SLionel Sambuc 	}								\
70*ebfedea0SLionel Sambuc } while( /* CONSTCOND */ 0)
71*ebfedea0SLionel Sambuc 
72*ebfedea0SLionel Sambuc #define NEW(type, ptr, where, action)	NEWARRAY(type, ptr, 1, where, action)
73*ebfedea0SLionel Sambuc 
74*ebfedea0SLionel Sambuc #define FREE(ptr)	free(ptr)
75*ebfedea0SLionel Sambuc 
76*ebfedea0SLionel Sambuc #define ALLOC(type, v, size, c, init, incr, where, action) do {		\
77*ebfedea0SLionel Sambuc 	uint32_t	_newsize = size;				\
78*ebfedea0SLionel Sambuc 	if (size == 0) {						\
79*ebfedea0SLionel Sambuc 		_newsize = init;					\
80*ebfedea0SLionel Sambuc 		NEWARRAY(type, v, _newsize, where ": new", action);	\
81*ebfedea0SLionel Sambuc 	} else if (c == size) {						\
82*ebfedea0SLionel Sambuc 		_newsize = size + incr;					\
83*ebfedea0SLionel Sambuc 		RENEW(type, v, size, _newsize, where ": renew", action); \
84*ebfedea0SLionel Sambuc 	}								\
85*ebfedea0SLionel Sambuc 	size = _newsize;						\
86*ebfedea0SLionel Sambuc } while( /* CONSTCOND */ 0)
87*ebfedea0SLionel Sambuc 
88*ebfedea0SLionel Sambuc #define DEFINE_ARRAY(name, type)					\
89*ebfedea0SLionel Sambuc typedef struct name {							\
90*ebfedea0SLionel Sambuc 	uint32_t	c;						\
91*ebfedea0SLionel Sambuc 	uint32_t	size;						\
92*ebfedea0SLionel Sambuc 	type	       *v;						\
93*ebfedea0SLionel Sambuc } name
94*ebfedea0SLionel Sambuc 
95*ebfedea0SLionel Sambuc #endif /* !DEFS_H_ */
96