1/* 2 * Copyright (c) 2013 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25#include <stdio.h> 26#include <ctype.h> 27 28#define MAXBUF 16 29 30void 31hex_and_ascii_print(const char *prefix, const void *data, 32 size_t len, const char *suffix) 33{ 34 size_t i, j, k; 35 unsigned char *ptr = (unsigned char *)data; 36 unsigned char hexbuf[3 * MAXBUF + 1]; 37 unsigned char asciibuf[MAXBUF + 1]; 38 39 for (i = 0; i < len; i += MAXBUF) { 40 for (j = i, k = 0; j < i + MAXBUF && j < len; j++) { 41 unsigned char msnbl = ptr[j] >> 4; 42 unsigned char lsnbl = ptr[j] & 0x0f; 43 44 if (isprint(ptr[j])) 45 asciibuf[j % MAXBUF] = ptr[j]; 46 else 47 asciibuf[j % MAXBUF] = '.'; 48 asciibuf[(j % MAXBUF) + 1] = 0; 49 50 hexbuf[k++] = msnbl < 10 ? msnbl + '0' : msnbl + 'a' - 10; 51 hexbuf[k++] = lsnbl < 10 ? lsnbl + '0' : lsnbl + 'a' - 10; 52 if ((j % 2) == 1) 53 hexbuf[k++] = ' '; 54 } 55 for (; j < i + MAXBUF;j++) { 56 hexbuf[k++] = ' '; 57 hexbuf[k++] = ' '; 58 if ((j % 2) == 1) 59 hexbuf[k++] = ' '; 60 } 61 hexbuf[k] = 0; 62 printf("%s%s %s%s\n", prefix, hexbuf, asciibuf, suffix); 63 } 64} 65