xref: /minix3/crypto/external/bsd/netpgp/dist/src/netpgpverify/misc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
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 #include <sys/syslog.h>
28 
29 #ifdef _KERNEL
30 # include <sys/kmem.h>
31 #else
32 # include <ctype.h>
33 # include <inttypes.h>
34 # include <stdarg.h>
35 # include <stdio.h>
36 # include <stdlib.h>
37 # include <string.h>
38 # include <time.h>
39 # include <unistd.h>
40 #endif
41 
42 #include "misc.h"
43 
44 #ifndef USE_ARG
45 #define USE_ARG(x)	/*LINTED*/(void)&(x)
46 #endif
47 
48 void *
netpgp_allocate(size_t n,size_t nels)49 netpgp_allocate(size_t n, size_t nels)
50 {
51 #ifdef _KERNEL
52 	return kmem_zalloc(n * nels, KM_SLEEP);
53 #else
54 	return calloc(n, nels);
55 #endif
56 }
57 
58 void
netpgp_deallocate(void * ptr,size_t size)59 netpgp_deallocate(void *ptr, size_t size)
60 {
61 #ifdef _KERNEL
62 	kmem_free(ptr, size);
63 #else
64 	USE_ARG(size);
65 	free(ptr);
66 #endif
67 }
68 
69 #define HEXDUMP_LINELEN	16
70 
71 #ifndef PRIsize
72 #define PRIsize	"z"
73 #endif
74 
75 /* show hexadecimal/ascii dump */
76 ssize_t
netpgp_hexdump(const void * vin,const size_t len,void * outvp,size_t size)77 netpgp_hexdump(const void *vin, const size_t len, void *outvp, size_t size)
78 {
79 	const char	*in = (const char *)vin;
80 	size_t		 i;
81 	char		 line[HEXDUMP_LINELEN + 1];
82 	char		*out = (char *)outvp;
83 	int		 o;
84 
85 	for (i = 0, o = 0 ; i < len ; i++) {
86 		if (i % HEXDUMP_LINELEN == 0) {
87 			o += snprintf(&out[o], size - o,
88 					"%.5" PRIsize "u |  ", i);
89 		} else if (i % (HEXDUMP_LINELEN / 2) == 0) {
90 			o += snprintf(&out[o], size - o, " ");
91 		}
92 		o += snprintf(&out[o], size - o, "%.02x ", (uint8_t)in[i]);
93 		line[i % HEXDUMP_LINELEN] =
94 			(isprint((uint8_t)in[i])) ? in[i] : '.';
95 		if (i % HEXDUMP_LINELEN == HEXDUMP_LINELEN - 1) {
96 			line[HEXDUMP_LINELEN] = 0x0;
97 			o += snprintf(&out[o], size - o, " | %s\n", line);
98 		}
99 	}
100 	if (i % HEXDUMP_LINELEN != 0) {
101 		for ( ; i % HEXDUMP_LINELEN != 0 ; i++) {
102 			o += snprintf(&out[o], size - o, "   ");
103 			if (i % (HEXDUMP_LINELEN / 2) == 0) {
104 				o += snprintf(&out[o], size - o, " ");
105 			}
106 			line[i % HEXDUMP_LINELEN] = ' ';
107 		}
108 		line[HEXDUMP_LINELEN] = 0x0;
109 		o += snprintf(&out[o], size - o, " | %s\n", line);
110 	}
111 	return (ssize_t)o;
112 }
113