1e0f95098SPeter Avalos /*-
2e0f95098SPeter Avalos * Copyright (c) 2005 Poul-Henning Kamp
3e0f95098SPeter Avalos * All rights reserved.
4e0f95098SPeter Avalos *
5e0f95098SPeter Avalos * Redistribution and use in source and binary forms, with or without
6e0f95098SPeter Avalos * modification, are permitted provided that the following conditions
7e0f95098SPeter Avalos * are met:
8e0f95098SPeter Avalos * 1. Redistributions of source code must retain the above copyright
9e0f95098SPeter Avalos * notice, this list of conditions and the following disclaimer.
10e0f95098SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
11e0f95098SPeter Avalos * notice, this list of conditions and the following disclaimer in the
12e0f95098SPeter Avalos * documentation and/or other materials provided with the distribution.
13e0f95098SPeter Avalos *
14e0f95098SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e0f95098SPeter Avalos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e0f95098SPeter Avalos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e0f95098SPeter Avalos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e0f95098SPeter Avalos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e0f95098SPeter Avalos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e0f95098SPeter Avalos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e0f95098SPeter Avalos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e0f95098SPeter Avalos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e0f95098SPeter Avalos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e0f95098SPeter Avalos * SUCH DAMAGE.
25e0f95098SPeter Avalos *
26e0f95098SPeter Avalos * $FreeBSD: src/lib/libc/stdio/xprintf_hexdump.c,v 1.1 2005/12/16 18:56:38 phk Exp $
27e0f95098SPeter Avalos */
28e0f95098SPeter Avalos
29e0f95098SPeter Avalos #include "namespace.h"
30e0f95098SPeter Avalos #include <stdio.h>
31e0f95098SPeter Avalos #include <wchar.h>
32e0f95098SPeter Avalos #include <stdint.h>
33e0f95098SPeter Avalos #include <assert.h>
34e0f95098SPeter Avalos #include <sys/time.h>
35e0f95098SPeter Avalos #include "un-namespace.h"
36e0f95098SPeter Avalos #include "printf.h"
37e0f95098SPeter Avalos
38e0f95098SPeter Avalos int
__printf_arginfo_hexdump(const struct printf_info * pi __unused,size_t n,int * argt)39*6d7019e6SSascha Wildner __printf_arginfo_hexdump(const struct printf_info *pi __unused,
40*6d7019e6SSascha Wildner size_t n, int *argt)
41e0f95098SPeter Avalos {
42e0f95098SPeter Avalos
43e0f95098SPeter Avalos assert(n >= 2);
44e0f95098SPeter Avalos argt[0] = PA_POINTER;
45e0f95098SPeter Avalos argt[1] = PA_INT;
46e0f95098SPeter Avalos return (2);
47e0f95098SPeter Avalos }
48e0f95098SPeter Avalos
49e0f95098SPeter Avalos int
__printf_render_hexdump(struct __printf_io * io,const struct printf_info * pi,const void * const * arg)50e0f95098SPeter Avalos __printf_render_hexdump(struct __printf_io *io, const struct printf_info *pi,
51e0f95098SPeter Avalos const void *const *arg)
52e0f95098SPeter Avalos {
53e0f95098SPeter Avalos unsigned char *p;
54e0f95098SPeter Avalos unsigned u, l, j, a;
55e0f95098SPeter Avalos char buf[100], *q;
56e0f95098SPeter Avalos int ret;
57e0f95098SPeter Avalos
58e0f95098SPeter Avalos if (pi->width > 0 && pi->width < 16)
59e0f95098SPeter Avalos l = pi->width;
60e0f95098SPeter Avalos else
61e0f95098SPeter Avalos l = 16;
62e0f95098SPeter Avalos p = *((unsigned char **)arg[0]);
63e0f95098SPeter Avalos u = *((unsigned *)arg[1]);
64e0f95098SPeter Avalos
65e0f95098SPeter Avalos ret = 0;
66e0f95098SPeter Avalos a = 0;
67e0f95098SPeter Avalos while (u > 0) {
68e0f95098SPeter Avalos q = buf;
69e0f95098SPeter Avalos if (pi->showsign)
70e0f95098SPeter Avalos q += sprintf(q, " %04x", a);
71e0f95098SPeter Avalos for (j = 0; j < l && j < u; j++)
72e0f95098SPeter Avalos q += sprintf(q, " %02x", p[j]);
73e0f95098SPeter Avalos if (pi->alt) {
74e0f95098SPeter Avalos for (; j < l; j++)
75e0f95098SPeter Avalos q += sprintf(q, " ");
76e0f95098SPeter Avalos q += sprintf(q, " |");
77e0f95098SPeter Avalos for (j = 0; j < l && j < u; j++) {
78e0f95098SPeter Avalos if (p[j] < ' ' || p[j] > '~')
79e0f95098SPeter Avalos *q++ = '.';
80e0f95098SPeter Avalos else
81e0f95098SPeter Avalos *q++ = p[j];
82e0f95098SPeter Avalos }
83e0f95098SPeter Avalos for (; j < l; j++)
84e0f95098SPeter Avalos *q++ = ' ';
85e0f95098SPeter Avalos *q++ = '|';
86e0f95098SPeter Avalos }
87e0f95098SPeter Avalos if (l < u)
88e0f95098SPeter Avalos j = l;
89e0f95098SPeter Avalos else
90e0f95098SPeter Avalos j = u;
91e0f95098SPeter Avalos p += j;
92e0f95098SPeter Avalos u -= j;
93e0f95098SPeter Avalos a += j;
94e0f95098SPeter Avalos if (u > 0)
95e0f95098SPeter Avalos *q++ = '\n';
96e0f95098SPeter Avalos ret += __printf_puts(io, buf + 1, q - (buf + 1));
97e0f95098SPeter Avalos __printf_flush(io);
98e0f95098SPeter Avalos }
99e0f95098SPeter Avalos return (ret);
100e0f95098SPeter Avalos }
101