1153486Sphk/*- 2153486Sphk * Copyright (c) 2005 Poul-Henning Kamp 3153486Sphk * All rights reserved. 4153486Sphk * 5153486Sphk * Redistribution and use in source and binary forms, with or without 6153486Sphk * modification, are permitted provided that the following conditions 7153486Sphk * are met: 8153486Sphk * 1. Redistributions of source code must retain the above copyright 9153486Sphk * notice, this list of conditions and the following disclaimer. 10153486Sphk * 2. Redistributions in binary form must reproduce the above copyright 11153486Sphk * notice, this list of conditions and the following disclaimer in the 12153486Sphk * documentation and/or other materials provided with the distribution. 13153486Sphk * 14153486Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15153486Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16153486Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17153486Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18153486Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19153486Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20153486Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21153486Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22153486Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23153486Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24153486Sphk * SUCH DAMAGE. 25153486Sphk * 26153486Sphk * $FreeBSD$ 27153486Sphk */ 28153486Sphk 29153486Sphk#include <namespace.h> 30153486Sphk#include <stdio.h> 31153486Sphk#include <wchar.h> 32153486Sphk#include <stdint.h> 33153486Sphk#include <assert.h> 34153486Sphk#include <sys/time.h> 35153486Sphk#include "printf.h" 36153486Sphk 37153486Sphkint 38153486Sphk__printf_arginfo_hexdump(const struct printf_info *pi, size_t n, int *argt) 39153486Sphk{ 40153486Sphk 41153486Sphk assert(n >= 2); 42153486Sphk argt[0] = PA_POINTER; 43153486Sphk argt[1] = PA_INT; 44153486Sphk return (2); 45153486Sphk} 46153486Sphk 47153486Sphkint 48153486Sphk__printf_render_hexdump(struct __printf_io *io, const struct printf_info *pi, const void *const *arg) 49153486Sphk{ 50153486Sphk unsigned char *p; 51153486Sphk unsigned u, l, j, a; 52153486Sphk char buf[100], *q; 53153486Sphk int ret; 54153486Sphk 55153486Sphk if (pi->width > 0 && pi->width < 16) 56153486Sphk l = pi->width; 57153486Sphk else 58153486Sphk l = 16; 59153486Sphk p = *((unsigned char **)arg[0]); 60153486Sphk u = *((unsigned *)arg[1]); 61153486Sphk 62153486Sphk ret = 0; 63153486Sphk a = 0; 64153486Sphk while (u > 0) { 65153486Sphk q = buf; 66153486Sphk if (pi->showsign) 67153486Sphk q += sprintf(q, " %04x", a); 68153486Sphk for (j = 0; j < l && j < u; j++) 69153486Sphk q += sprintf(q, " %02x", p[j]); 70153486Sphk if (pi->alt) { 71153486Sphk for (; j < l; j++) 72153486Sphk q += sprintf(q, " "); 73153486Sphk q += sprintf(q, " |"); 74153486Sphk for (j = 0; j < l && j < u; j++) { 75153486Sphk if (p[j] < ' ' || p[j] > '~') 76153486Sphk *q++ = '.'; 77153486Sphk else 78153486Sphk *q++ = p[j]; 79153486Sphk } 80153486Sphk for (; j < l; j++) 81153486Sphk *q++ = ' '; 82153486Sphk *q++ = '|'; 83153486Sphk } 84153486Sphk if (l < u) 85153486Sphk j = l; 86153486Sphk else 87153486Sphk j = u; 88153486Sphk p += j; 89153486Sphk u -= j; 90153486Sphk a += j; 91153486Sphk if (u > 0) 92153486Sphk *q++ = '\n'; 93153486Sphk ret += __printf_puts(io, buf + 1, q - (buf + 1)); 94153486Sphk __printf_flush(io); 95153486Sphk } 96153486Sphk return (ret); 97153486Sphk} 98