xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/libbn/misc.c (revision 640eb22bcb3eb4227a4cc2648891b207ab6a658c)
1*640eb22bSagc /*-
2*640eb22bSagc  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3*640eb22bSagc  * All rights reserved.
4*640eb22bSagc  *
5*640eb22bSagc  * Redistribution and use in source and binary forms, with or without
6*640eb22bSagc  * modification, are permitted provided that the following conditions
7*640eb22bSagc  * are met:
8*640eb22bSagc  * 1. Redistributions of source code must retain the above copyright
9*640eb22bSagc  *    notice, this list of conditions and the following disclaimer.
10*640eb22bSagc  * 2. Redistributions in binary form must reproduce the above copyright
11*640eb22bSagc  *    notice, this list of conditions and the following disclaimer in the
12*640eb22bSagc  *    documentation and/or other materials provided with the distribution.
13*640eb22bSagc  *
14*640eb22bSagc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*640eb22bSagc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*640eb22bSagc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*640eb22bSagc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*640eb22bSagc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*640eb22bSagc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*640eb22bSagc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*640eb22bSagc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*640eb22bSagc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*640eb22bSagc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*640eb22bSagc  */
25*640eb22bSagc #include <sys/types.h>
26*640eb22bSagc #include <sys/param.h>
27*640eb22bSagc #include <sys/syslog.h>
28*640eb22bSagc 
29*640eb22bSagc #ifdef _KERNEL
30*640eb22bSagc # include <sys/kmem.h>
31*640eb22bSagc #else
32*640eb22bSagc # include <ctype.h>
33*640eb22bSagc # include <inttypes.h>
34*640eb22bSagc # include <stdarg.h>
35*640eb22bSagc # include <stdio.h>
36*640eb22bSagc # include <stdlib.h>
37*640eb22bSagc # include <string.h>
38*640eb22bSagc # include <time.h>
39*640eb22bSagc # include <unistd.h>
40*640eb22bSagc #endif
41*640eb22bSagc 
42*640eb22bSagc #include "misc.h"
43*640eb22bSagc 
44*640eb22bSagc #ifndef USE_ARG
45*640eb22bSagc #define USE_ARG(x)	/*LINTED*/(void)&(x)
46*640eb22bSagc #endif
47*640eb22bSagc 
48*640eb22bSagc void *
netpgp_allocate(size_t n,size_t nels)49*640eb22bSagc netpgp_allocate(size_t n, size_t nels)
50*640eb22bSagc {
51*640eb22bSagc #ifdef _KERNEL
52*640eb22bSagc 	return kmem_zalloc(n * nels, KM_SLEEP);
53*640eb22bSagc #else
54*640eb22bSagc 	return calloc(n, nels);
55*640eb22bSagc #endif
56*640eb22bSagc }
57*640eb22bSagc 
58*640eb22bSagc void
netpgp_deallocate(void * ptr,size_t size)59*640eb22bSagc netpgp_deallocate(void *ptr, size_t size)
60*640eb22bSagc {
61*640eb22bSagc #ifdef _KERNEL
62*640eb22bSagc 	kmem_free(ptr, size);
63*640eb22bSagc #else
64*640eb22bSagc 	USE_ARG(size);
65*640eb22bSagc 	free(ptr);
66*640eb22bSagc #endif
67*640eb22bSagc }
68*640eb22bSagc 
69*640eb22bSagc #ifndef _KERNEL
70*640eb22bSagc void
logmessage(const int level,const char * fmt,...)71*640eb22bSagc logmessage(const int level, const char *fmt, ...)
72*640eb22bSagc {
73*640eb22bSagc 	va_list	args;
74*640eb22bSagc 
75*640eb22bSagc 	USE_ARG(level);
76*640eb22bSagc 	if (fmt != NULL) {
77*640eb22bSagc 		va_start(args, fmt);
78*640eb22bSagc 		vfprintf(stderr, fmt, args);
79*640eb22bSagc 		va_end(args);
80*640eb22bSagc 	}
81*640eb22bSagc }
82*640eb22bSagc #endif
83