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