1/*
2 * Copyright (c) 2005 Apple Computer, 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#include <stdio.h>
24#include <stdarg.h>
25#include "fsck_debug.h"
26#include "fsck_hfs.h"
27
28/* Current debug level of fsck_hfs for printing messages via DPRINTF */
29unsigned long cur_debug_level;
30
31/* Function: DPRINTF
32 *
33 * Description: Debug function similar to printf except the first parameter
34 * which indicates the type of message to be printed by DPRINTF. Based on
35 * current debug level and the type of message, the function decides
36 * whether to print the message or not.
37 *
38 * Each unique message type has a bit assigned to it.  The message type
39 * passed to DPRINTF can be one or combination (OR-ed value) of pre-defined
40 * debug message types.  Only the messages whose type have one or more similar
41 * bits set in comparison with current global debug level are printed.
42 *
43 * For example, if cur_debug_level = 0x11 (d_info|d_xattr)
44 * ----------------------------------------
45 *	message type	- 	printed/not printed
46 * ----------------------------------------
47 *	d_info			-	printed
48 *	d_error|d_xattr	-	printed
49 *	d_error			- 	not printed
50 *	d_overlap		- 	not printed
51 *
52 * Input:
53 *	message_type - type of message, to determine when to print the message
54 *	variable arguments - similar to printfs
55 *
56 * Output:
57 *	Nothing
58 */
59void DPRINTF (unsigned long type, char *fmt, ...)
60{
61	if (cur_debug_level & type) {
62		va_list ap;
63
64		plog ("\t");
65		va_start(ap, fmt);
66		vplog(fmt, ap);
67		va_end(ap);
68	}
69}
70
71void HexDump(const void *p_arg, unsigned length, int showOffsets)
72{
73	const u_int8_t *p = p_arg;
74	unsigned i;
75	char ascii[17];
76	u_int8_t byte;
77
78	ascii[16] = '\0';
79
80	for (i=0; i<length; ++i)
81	{
82		if (showOffsets && (i & 0xF) == 0)
83			plog("%08X: ", i);
84
85		byte = p[i];
86		plog("%02X ", byte);
87		if (byte < 32 || byte > 126)
88			ascii[i & 0xF] = '.';
89		else
90			ascii[i & 0xF] = byte;
91
92		if ((i & 0xF) == 0xF)
93		{
94			plog("  %s\n", ascii);
95		}
96	}
97
98	if (i & 0xF)
99	{
100		unsigned j;
101		for (j = i & 0xF; j < 16; ++j)
102			plog("   ");
103		ascii[i & 0xF] = 0;
104		plog("  %s\n", ascii);
105	}
106}
107