xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/netpgpverify/misc.c (revision dd98b26d9b747061a6a9c2243c42b44a36f58989)
1 /*-
2  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include <sys/types.h>
26 #include <sys/param.h>
27 
28 #ifdef _KERNEL
29 # include <sys/kmem.h>
30 #else
31 # include <ctype.h>
32 # include <inttypes.h>
33 # include <stdarg.h>
34 # include <stdio.h>
35 # include <stdlib.h>
36 # include <string.h>
37 # include <time.h>
38 # include <unistd.h>
39 #endif
40 
41 #include "misc.h"
42 
43 #ifndef USE_ARG
44 #define USE_ARG(x)	/*LINTED*/(void)&(x)
45 #endif
46 
47 void *
netpgp_allocate(size_t n,size_t nels)48 netpgp_allocate(size_t n, size_t nels)
49 {
50 #ifdef _KERNEL
51 	return kmem_zalloc(n * nels, KM_SLEEP);
52 #else
53 	return calloc(n, nels);
54 #endif
55 }
56 
57 void
netpgp_deallocate(void * ptr,size_t size)58 netpgp_deallocate(void *ptr, size_t size)
59 {
60 #ifdef _KERNEL
61 	kmem_free(ptr, size);
62 #else
63 	USE_ARG(size);
64 	free(ptr);
65 #endif
66 }
67 
68 #define HEXDUMP_LINELEN	16
69 
70 #ifndef PRIsize
71 #define PRIsize	"z"
72 #endif
73 
74 /* show hexadecimal/ascii dump */
75 ssize_t
netpgp_hexdump(const void * vin,const size_t len,void * outvp,size_t size)76 netpgp_hexdump(const void *vin, const size_t len, void *outvp, size_t size)
77 {
78 	const char	*in = (const char *)vin;
79 	size_t		 i;
80 	char		 line[HEXDUMP_LINELEN + 1];
81 	char		*out = (char *)outvp;
82 	int		 o;
83 
84 	for (i = 0, o = 0 ; i < len ; i++) {
85 		if (i % HEXDUMP_LINELEN == 0) {
86 			o += snprintf(&out[o], size - o,
87 					"%.5" PRIsize "u |  ", i);
88 		} else if (i % (HEXDUMP_LINELEN / 2) == 0) {
89 			o += snprintf(&out[o], size - o, " ");
90 		}
91 		o += snprintf(&out[o], size - o, "%.02x ", (uint8_t)in[i]);
92 		line[i % HEXDUMP_LINELEN] =
93 			(isprint((uint8_t)in[i])) ? in[i] : '.';
94 		if (i % HEXDUMP_LINELEN == HEXDUMP_LINELEN - 1) {
95 			line[HEXDUMP_LINELEN] = 0x0;
96 			o += snprintf(&out[o], size - o, " | %s\n", line);
97 		}
98 	}
99 	if (i % HEXDUMP_LINELEN != 0) {
100 		for ( ; i % HEXDUMP_LINELEN != 0 ; i++) {
101 			o += snprintf(&out[o], size - o, "   ");
102 			if (i % (HEXDUMP_LINELEN / 2) == 0) {
103 				o += snprintf(&out[o], size - o, " ");
104 			}
105 			line[i % HEXDUMP_LINELEN] = ' ';
106 		}
107 		line[HEXDUMP_LINELEN] = 0x0;
108 		o += snprintf(&out[o], size - o, " | %s\n", line);
109 	}
110 	return (ssize_t)o;
111 }
112