12a988851Sagc /*-
22a988851Sagc  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
32a988851Sagc  * All rights reserved.
42a988851Sagc  *
52a988851Sagc  * Redistribution and use in source and binary forms, with or without
62a988851Sagc  * modification, are permitted provided that the following conditions
72a988851Sagc  * are met:
82a988851Sagc  * 1. Redistributions of source code must retain the above copyright
92a988851Sagc  *    notice, this list of conditions and the following disclaimer.
102a988851Sagc  * 2. Redistributions in binary form must reproduce the above copyright
112a988851Sagc  *    notice, this list of conditions and the following disclaimer in the
122a988851Sagc  *    documentation and/or other materials provided with the distribution.
132a988851Sagc  *
142a988851Sagc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
152a988851Sagc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
162a988851Sagc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
172a988851Sagc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
182a988851Sagc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
192a988851Sagc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
202a988851Sagc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
212a988851Sagc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
222a988851Sagc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
232a988851Sagc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
242a988851Sagc  */
252a988851Sagc #include <sys/types.h>
262a988851Sagc #include <sys/param.h>
272a988851Sagc 
282a988851Sagc #ifdef _KERNEL
292a988851Sagc # include <sys/kmem.h>
302a988851Sagc #else
312a988851Sagc # include <ctype.h>
322a988851Sagc # include <inttypes.h>
332a988851Sagc # include <stdarg.h>
342a988851Sagc # include <stdio.h>
352a988851Sagc # include <stdlib.h>
362a988851Sagc # include <string.h>
372a988851Sagc # include <time.h>
382a988851Sagc # include <unistd.h>
392a988851Sagc #endif
402a988851Sagc 
412a988851Sagc #include "misc.h"
422a988851Sagc 
432a988851Sagc #ifndef USE_ARG
442a988851Sagc #define USE_ARG(x)	/*LINTED*/(void)&(x)
452a988851Sagc #endif
462a988851Sagc 
472a988851Sagc void *
netpgp_allocate(size_t n,size_t nels)482a988851Sagc netpgp_allocate(size_t n, size_t nels)
492a988851Sagc {
502a988851Sagc #ifdef _KERNEL
512a988851Sagc 	return kmem_zalloc(n * nels, KM_SLEEP);
522a988851Sagc #else
532a988851Sagc 	return calloc(n, nels);
542a988851Sagc #endif
552a988851Sagc }
562a988851Sagc 
572a988851Sagc void
netpgp_deallocate(void * ptr,size_t size)582a988851Sagc netpgp_deallocate(void *ptr, size_t size)
592a988851Sagc {
602a988851Sagc #ifdef _KERNEL
612a988851Sagc 	kmem_free(ptr, size);
622a988851Sagc #else
632a988851Sagc 	USE_ARG(size);
642a988851Sagc 	free(ptr);
652a988851Sagc #endif
662a988851Sagc }
67*85d3c591Sagc 
68*85d3c591Sagc #define HEXDUMP_LINELEN	16
69*85d3c591Sagc 
70*85d3c591Sagc #ifndef PRIsize
71*85d3c591Sagc #define PRIsize	"z"
72*85d3c591Sagc #endif
73*85d3c591Sagc 
74*85d3c591Sagc /* show hexadecimal/ascii dump */
75*85d3c591Sagc ssize_t
netpgp_hexdump(const void * vin,const size_t len,void * outvp,size_t size)76*85d3c591Sagc netpgp_hexdump(const void *vin, const size_t len, void *outvp, size_t size)
77*85d3c591Sagc {
78*85d3c591Sagc 	const char	*in = (const char *)vin;
79*85d3c591Sagc 	size_t		 i;
80*85d3c591Sagc 	char		 line[HEXDUMP_LINELEN + 1];
81*85d3c591Sagc 	char		*out = (char *)outvp;
82*85d3c591Sagc 	int		 o;
83*85d3c591Sagc 
84*85d3c591Sagc 	for (i = 0, o = 0 ; i < len ; i++) {
85*85d3c591Sagc 		if (i % HEXDUMP_LINELEN == 0) {
86*85d3c591Sagc 			o += snprintf(&out[o], size - o,
87*85d3c591Sagc 					"%.5" PRIsize "u |  ", i);
88*85d3c591Sagc 		} else if (i % (HEXDUMP_LINELEN / 2) == 0) {
89*85d3c591Sagc 			o += snprintf(&out[o], size - o, " ");
90*85d3c591Sagc 		}
91*85d3c591Sagc 		o += snprintf(&out[o], size - o, "%.02x ", (uint8_t)in[i]);
92*85d3c591Sagc 		line[i % HEXDUMP_LINELEN] =
93*85d3c591Sagc 			(isprint((uint8_t)in[i])) ? in[i] : '.';
94*85d3c591Sagc 		if (i % HEXDUMP_LINELEN == HEXDUMP_LINELEN - 1) {
95*85d3c591Sagc 			line[HEXDUMP_LINELEN] = 0x0;
96*85d3c591Sagc 			o += snprintf(&out[o], size - o, " | %s\n", line);
97*85d3c591Sagc 		}
98*85d3c591Sagc 	}
99*85d3c591Sagc 	if (i % HEXDUMP_LINELEN != 0) {
100*85d3c591Sagc 		for ( ; i % HEXDUMP_LINELEN != 0 ; i++) {
101*85d3c591Sagc 			o += snprintf(&out[o], size - o, "   ");
102*85d3c591Sagc 			if (i % (HEXDUMP_LINELEN / 2) == 0) {
103*85d3c591Sagc 				o += snprintf(&out[o], size - o, " ");
104*85d3c591Sagc 			}
105*85d3c591Sagc 			line[i % HEXDUMP_LINELEN] = ' ';
106*85d3c591Sagc 		}
107*85d3c591Sagc 		line[HEXDUMP_LINELEN] = 0x0;
108*85d3c591Sagc 		o += snprintf(&out[o], size - o, " | %s\n", line);
109*85d3c591Sagc 	}
110*85d3c591Sagc 	return (ssize_t)o;
111*85d3c591Sagc }
112