19428323dSchristos /*
29428323dSchristos * layout_dump.c -
39428323dSchristos *
49428323dSchristos * Written by Eryk Vershen
59428323dSchristos */
69428323dSchristos
79428323dSchristos /*
89428323dSchristos * Copyright 1996,1997 by Apple Computer, Inc.
99428323dSchristos * All Rights Reserved
109428323dSchristos *
119428323dSchristos * Permission to use, copy, modify, and distribute this software and
129428323dSchristos * its documentation for any purpose and without fee is hereby granted,
139428323dSchristos * provided that the above copyright notice appears in all copies and
149428323dSchristos * that both the copyright notice and this permission notice appear in
159428323dSchristos * supporting documentation.
169428323dSchristos *
179428323dSchristos * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
189428323dSchristos * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
199428323dSchristos * FOR A PARTICULAR PURPOSE.
209428323dSchristos *
219428323dSchristos * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
229428323dSchristos * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
239428323dSchristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
249428323dSchristos * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
259428323dSchristos * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
269428323dSchristos */
279428323dSchristos
289428323dSchristos // for printf()
299428323dSchristos #include <stdio.h>
309428323dSchristos // for strlen()
319428323dSchristos #include <string.h>
32*48a628aeSchristos #include <inttypes.h>
339428323dSchristos #include "layout_dump.h"
349428323dSchristos
359428323dSchristos
369428323dSchristos /*
379428323dSchristos * Defines
389428323dSchristos */
399428323dSchristos
409428323dSchristos
419428323dSchristos /*
429428323dSchristos * Types
439428323dSchristos */
449428323dSchristos
459428323dSchristos
469428323dSchristos /*
479428323dSchristos * Global Constants
489428323dSchristos */
49*48a628aeSchristos uint8_t bitmasks[] = {
509428323dSchristos 0x01, 0x03, 0x07, 0x0F,
519428323dSchristos 0x1F, 0x3F, 0x7F, 0xFF
529428323dSchristos };
539428323dSchristos
549428323dSchristos
559428323dSchristos /*
569428323dSchristos * Global Variables
579428323dSchristos */
589428323dSchristos
599428323dSchristos
609428323dSchristos /*
619428323dSchristos * Forward declarations
629428323dSchristos */
639428323dSchristos
649428323dSchristos
659428323dSchristos /*
669428323dSchristos * Routines
679428323dSchristos */
689428323dSchristos void
dump_using_layout(void * buffer,layout * desc)699428323dSchristos dump_using_layout(void *buffer, layout *desc)
709428323dSchristos {
719428323dSchristos layout *entry;
729428323dSchristos int byte_length;
739428323dSchristos long value;
749428323dSchristos int max_name;
759428323dSchristos int i;
769428323dSchristos
779428323dSchristos max_name = 0;
789428323dSchristos for (entry = desc; entry->format != kEnd; entry++) {
799428323dSchristos value = strlen(entry->name);
809428323dSchristos if (value > max_name) {
819428323dSchristos max_name = value;
829428323dSchristos }
839428323dSchristos }
849428323dSchristos
859428323dSchristos
869428323dSchristos for (entry = desc; entry->format != kEnd; entry++) {
879428323dSchristos
889428323dSchristos if (entry->format != kBit) {
899428323dSchristos printf("%*s: ", max_name, entry->name);
909428323dSchristos
919428323dSchristos byte_length = entry->bit_length / 8;
929428323dSchristos
939428323dSchristos if (entry->bit_offset != 0 || (entry->bit_length % 8) != 0) {
94*48a628aeSchristos printf("entry %d, can't handle bitfields yet.\n", (int)(entry - desc));
959428323dSchristos continue;
969428323dSchristos }
979428323dSchristos
989428323dSchristos value = 0;
999428323dSchristos for (i = entry->byte_offset; byte_length > 0;i++) {
1009428323dSchristos value = value << 8;
101*48a628aeSchristos value |= ((uint8_t *)buffer)[i];
1029428323dSchristos byte_length--;
1039428323dSchristos }
1049428323dSchristos } else {
1059428323dSchristos if (entry->bit_offset < 0 || entry->bit_offset > 8) {
106*48a628aeSchristos printf("entry %d, bad bit offset (%d).\n", (int)(entry - desc), entry->bit_offset);
1079428323dSchristos continue;
1089428323dSchristos } else if (entry->bit_length <= 0
1099428323dSchristos || entry->bit_length > (entry->bit_offset + 1)) {
110*48a628aeSchristos printf("entry %d, bad bit length (%d,%d).\n", (int)(entry - desc),
1119428323dSchristos entry->bit_offset, entry->bit_length);
1129428323dSchristos continue;
1139428323dSchristos }
114*48a628aeSchristos value = (((uint8_t *)buffer)[entry->byte_offset]
1159428323dSchristos & bitmasks[entry->bit_offset])
1169428323dSchristos >> ((entry->bit_offset + 1) - entry->bit_length);
1179428323dSchristos }
1189428323dSchristos
1199428323dSchristos switch (entry->format) {
1209428323dSchristos case kHex:
121*48a628aeSchristos printf("0x%x\n", (uint32_t) value);
1229428323dSchristos break;
1239428323dSchristos case kDec:
1249428323dSchristos byte_length = entry->bit_length / 8;
1259428323dSchristos switch (byte_length) {
126*48a628aeSchristos case 4: printf("%"PRId32"\n", (int32_t)value); break;
127*48a628aeSchristos case 2: printf("%"PRId16"\n", (int16_t)value); break;
128*48a628aeSchristos case 1: printf("%"PRId8"\n", (int8_t)value); break;
1299428323dSchristos }
1309428323dSchristos break;
1319428323dSchristos case kUns:
1329428323dSchristos byte_length = entry->bit_length / 8;
1339428323dSchristos switch (byte_length) {
134*48a628aeSchristos case 4: printf("%"PRIu32"\n", (uint32_t)value); break;
135*48a628aeSchristos case 2: printf("%"PRIu16"\n", (uint16_t)value); break;
136*48a628aeSchristos case 1: printf("%"PRIu8"\n", (uint8_t)value); break;
1379428323dSchristos }
1389428323dSchristos break;
1399428323dSchristos case kBit:
1409428323dSchristos if (value) {
1419428323dSchristos printf("%*s %s\n", max_name, "", entry->name);
1429428323dSchristos }
1439428323dSchristos break;
1449428323dSchristos default:
145*48a628aeSchristos printf("entry %d, unknown format (%d).\n", (int)(entry - desc), entry->format);
1469428323dSchristos break;
1479428323dSchristos }
1489428323dSchristos }
1499428323dSchristos }
1509428323dSchristos
1519428323dSchristos
1529428323dSchristos void
DumpRawBuffer(uint8_t * bufferPtr,int length)153*48a628aeSchristos DumpRawBuffer(uint8_t *bufferPtr, int length )
1549428323dSchristos {
1559428323dSchristos register int i;
1569428323dSchristos int lineStart;
1579428323dSchristos int lineLength;
1589428323dSchristos short c;
1599428323dSchristos
1609428323dSchristos #define kLineSize 16
1619428323dSchristos for (lineStart = 0; lineStart < length; lineStart += lineLength) {
1629428323dSchristos lineLength = kLineSize;
1639428323dSchristos if (lineStart + lineLength > length)
1649428323dSchristos lineLength = length - lineStart;
1659428323dSchristos printf("%03x %3d:", lineStart, lineStart);
1669428323dSchristos for (i = 0; i < lineLength; i++)
1679428323dSchristos printf(" %02x", bufferPtr[lineStart + i] & 0xFF);
1689428323dSchristos for (; i < kLineSize; i++)
1699428323dSchristos printf(" ");
1709428323dSchristos printf(" ");
1719428323dSchristos for (i = 0; i < lineLength; i++) {
1729428323dSchristos c = bufferPtr[lineStart + i] & 0xFF;
1739428323dSchristos if (c > ' ' && c < '~')
1749428323dSchristos printf("%c", c);
1759428323dSchristos else {
1769428323dSchristos printf(".");
1779428323dSchristos }
1789428323dSchristos }
1799428323dSchristos printf("\n");
1809428323dSchristos }
1819428323dSchristos }
182