125f78d91Sagc /*-
225f78d91Sagc * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
325f78d91Sagc * All rights reserved.
425f78d91Sagc *
525f78d91Sagc * Redistribution and use in source and binary forms, with or without
625f78d91Sagc * modification, are permitted provided that the following conditions
725f78d91Sagc * are met:
825f78d91Sagc * 1. Redistributions of source code must retain the above copyright
925f78d91Sagc * notice, this list of conditions and the following disclaimer.
1025f78d91Sagc * 2. Redistributions in binary form must reproduce the above copyright
1125f78d91Sagc * notice, this list of conditions and the following disclaimer in the
1225f78d91Sagc * documentation and/or other materials provided with the distribution.
1325f78d91Sagc *
1425f78d91Sagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1525f78d91Sagc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1625f78d91Sagc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1725f78d91Sagc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1825f78d91Sagc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1925f78d91Sagc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2025f78d91Sagc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2125f78d91Sagc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2225f78d91Sagc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2325f78d91Sagc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2425f78d91Sagc */
2525f78d91Sagc #include <sys/types.h>
2625f78d91Sagc #include <sys/param.h>
2725f78d91Sagc
2825f78d91Sagc #ifdef _KERNEL
2925f78d91Sagc # include <sys/kmem.h>
3025f78d91Sagc #else
3125f78d91Sagc # include <ctype.h>
3225f78d91Sagc # include <inttypes.h>
3325f78d91Sagc # include <stdarg.h>
3425f78d91Sagc # include <stdio.h>
3525f78d91Sagc # include <stdlib.h>
3625f78d91Sagc # include <string.h>
3725f78d91Sagc # include <time.h>
3825f78d91Sagc # include <unistd.h>
3925f78d91Sagc #endif
4025f78d91Sagc
4125f78d91Sagc #include "misc.h"
4225f78d91Sagc
4325f78d91Sagc #ifndef USE_ARG
4425f78d91Sagc #define USE_ARG(x) /*LINTED*/(void)&(x)
4525f78d91Sagc #endif
4625f78d91Sagc
4725f78d91Sagc void *
netpgp_allocate(size_t n,size_t nels)4825f78d91Sagc netpgp_allocate(size_t n, size_t nels)
4925f78d91Sagc {
5025f78d91Sagc #ifdef _KERNEL
5125f78d91Sagc return kmem_zalloc(n * nels, KM_SLEEP);
5225f78d91Sagc #else
5325f78d91Sagc return calloc(n, nels);
5425f78d91Sagc #endif
5525f78d91Sagc }
5625f78d91Sagc
5725f78d91Sagc void
netpgp_deallocate(void * ptr,size_t size)5825f78d91Sagc netpgp_deallocate(void *ptr, size_t size)
5925f78d91Sagc {
6025f78d91Sagc #ifdef _KERNEL
6125f78d91Sagc kmem_free(ptr, size);
6225f78d91Sagc #else
6325f78d91Sagc USE_ARG(size);
6425f78d91Sagc free(ptr);
6525f78d91Sagc #endif
6625f78d91Sagc }
67*32b86961Sagc
68*32b86961Sagc #define HEXDUMP_LINELEN 16
69*32b86961Sagc
70*32b86961Sagc #ifndef PRIsize
71*32b86961Sagc #define PRIsize "z"
72*32b86961Sagc #endif
73*32b86961Sagc
74*32b86961Sagc /* show hexadecimal/ascii dump */
75*32b86961Sagc ssize_t
netpgp_hexdump(const void * vin,const size_t len,void * outvp,size_t size)76*32b86961Sagc netpgp_hexdump(const void *vin, const size_t len, void *outvp, size_t size)
77*32b86961Sagc {
78*32b86961Sagc const char *in = (const char *)vin;
79*32b86961Sagc size_t i;
80*32b86961Sagc char line[HEXDUMP_LINELEN + 1];
81*32b86961Sagc char *out = (char *)outvp;
82*32b86961Sagc int o;
83*32b86961Sagc
84*32b86961Sagc for (i = 0, o = 0 ; i < len ; i++) {
85*32b86961Sagc if (i % HEXDUMP_LINELEN == 0) {
86*32b86961Sagc o += snprintf(&out[o], size - o,
87*32b86961Sagc "%.5" PRIsize "u | ", i);
88*32b86961Sagc } else if (i % (HEXDUMP_LINELEN / 2) == 0) {
89*32b86961Sagc o += snprintf(&out[o], size - o, " ");
90*32b86961Sagc }
91*32b86961Sagc o += snprintf(&out[o], size - o, "%.02x ", (uint8_t)in[i]);
92*32b86961Sagc line[i % HEXDUMP_LINELEN] =
93*32b86961Sagc (isprint((uint8_t)in[i])) ? in[i] : '.';
94*32b86961Sagc if (i % HEXDUMP_LINELEN == HEXDUMP_LINELEN - 1) {
95*32b86961Sagc line[HEXDUMP_LINELEN] = 0x0;
96*32b86961Sagc o += snprintf(&out[o], size - o, " | %s\n", line);
97*32b86961Sagc }
98*32b86961Sagc }
99*32b86961Sagc if (i % HEXDUMP_LINELEN != 0) {
100*32b86961Sagc for ( ; i % HEXDUMP_LINELEN != 0 ; i++) {
101*32b86961Sagc o += snprintf(&out[o], size - o, " ");
102*32b86961Sagc if (i % (HEXDUMP_LINELEN / 2) == 0) {
103*32b86961Sagc o += snprintf(&out[o], size - o, " ");
104*32b86961Sagc }
105*32b86961Sagc line[i % HEXDUMP_LINELEN] = ' ';
106*32b86961Sagc }
107*32b86961Sagc line[HEXDUMP_LINELEN] = 0x0;
108*32b86961Sagc o += snprintf(&out[o], size - o, " | %s\n", line);
109*32b86961Sagc }
110*32b86961Sagc return (ssize_t)o;
111*32b86961Sagc }
112