Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / pdisk / layout_dump.c
blob210be3b2f5de57d21ff94d6fa222d0b96e815ffa
1 /*
2 * layout_dump.c -
4 * Written by Eryk Vershen
5 */
7 /*
8 * Copyright 1996,1997 by Apple Computer, Inc.
9 * All Rights Reserved
11 * Permission to use, copy, modify, and distribute this software and
12 * its documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appears in all copies and
14 * that both the copyright notice and this permission notice appear in
15 * supporting documentation.
17 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE.
21 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
23 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
24 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
25 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 // for printf()
29 #include <stdio.h>
30 // for strlen()
31 #include <string.h>
32 #include "layout_dump.h"
36 * Defines
41 * Types
46 * Global Constants
48 unsigned char bitmasks[] = {
49 0x01, 0x03, 0x07, 0x0F,
50 0x1F, 0x3F, 0x7F, 0xFF
55 * Global Variables
60 * Forward declarations
65 * Routines
67 void
68 dump_using_layout(void *buffer, layout *desc)
70 layout *entry;
71 int byte_length;
72 long value;
73 int max_name;
74 int i;
76 max_name = 0;
77 for (entry = desc; entry->format != kEnd; entry++) {
78 value = strlen(entry->name);
79 if (value > max_name) {
80 max_name = value;
85 for (entry = desc; entry->format != kEnd; entry++) {
87 if (entry->format != kBit) {
88 printf("%*s: ", max_name, entry->name);
90 byte_length = entry->bit_length / 8;
92 if (entry->bit_offset != 0 || (entry->bit_length % 8) != 0) {
93 printf("entry %d, can't handle bitfields yet.\n", entry - desc);
94 continue;
97 value = 0;
98 for (i = entry->byte_offset; byte_length > 0;i++) {
99 value = value << 8;
100 value |= ((unsigned char *)buffer)[i];
101 byte_length--;
103 } else {
104 if (entry->bit_offset < 0 || entry->bit_offset > 8) {
105 printf("entry %d, bad bit offset (%d).\n", entry - desc, entry->bit_offset);
106 continue;
107 } else if (entry->bit_length <= 0
108 || entry->bit_length > (entry->bit_offset + 1)) {
109 printf("entry %d, bad bit length (%d,%d).\n", entry - desc,
110 entry->bit_offset, entry->bit_length);
111 continue;
113 value = (((unsigned char *)buffer)[entry->byte_offset]
114 & bitmasks[entry->bit_offset])
115 >> ((entry->bit_offset + 1) - entry->bit_length);
118 switch (entry->format) {
119 case kHex:
120 printf("0x%x\n", (unsigned int) value);
121 break;
122 case kDec:
123 byte_length = entry->bit_length / 8;
124 switch (byte_length) {
125 case 4: printf("%ld\n", (signed long)value); break;
126 case 2: printf("%d\n", (signed short)value); break;
127 case 1: printf("%d\n", (signed char)value); break;
129 break;
130 case kUns:
131 byte_length = entry->bit_length / 8;
132 switch (byte_length) {
133 case 4: printf("%lu\n", (unsigned long)value); break;
134 case 2: printf("%u\n", (unsigned short)value); break;
135 case 1: printf("%u\n", (unsigned char)value); break;
137 break;
138 case kBit:
139 if (value) {
140 printf("%*s %s\n", max_name, "", entry->name);
142 break;
143 default:
144 printf("entry %d, unknown format (%d).\n", entry - desc, entry->format);
145 break;
151 void
152 DumpRawBuffer(unsigned char *bufferPtr, int length )
154 register int i;
155 int lineStart;
156 int lineLength;
157 short c;
159 #define kLineSize 16
160 for (lineStart = 0; lineStart < length; lineStart += lineLength) {
161 lineLength = kLineSize;
162 if (lineStart + lineLength > length)
163 lineLength = length - lineStart;
164 printf("%03x %3d:", lineStart, lineStart);
165 for (i = 0; i < lineLength; i++)
166 printf(" %02x", bufferPtr[lineStart + i] & 0xFF);
167 for (; i < kLineSize; i++)
168 printf(" ");
169 printf(" ");
170 for (i = 0; i < lineLength; i++) {
171 c = bufferPtr[lineStart + i] & 0xFF;
172 if (c > ' ' && c < '~')
173 printf("%c", c);
174 else {
175 printf(".");
178 printf("\n");