source: rtems/cpukit/libmisc/dumpbuf/dumpbuf.c @ 1050b629

Last change on this file since 1050b629 was 2a7f7767, checked in by Joel Sherrill <joel@…>, on 04/02/22 at 19:16:42

cpukit/libmisc/dumpbuf/dumpbuf.c: Manually change license to BSD-2

There was a typo in the license text and this file was missed by
the script.

Updates #3053

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup libmisc_dumpbuf Print Memory Buffer
7 *
8 * @brief Dump Buffer
9 */
10
11/*
12 *  COPYRIGHT (c) 1997-2015.
13 *  On-Line Applications Research Corporation (OAR).
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#include <stdio.h>
42#include <string.h>
43#include <ctype.h>
44#include <rtems/dumpbuf.h>
45#include <rtems/bspIo.h>
46
47#define HEX_FMT_LENGTH 3   /* Length of the formatted hex string. */
48#define ASCII_FMT_LENGTH 1 /* Length of the formatted ASCII string. */
49#define BYTES_PER_ROW 16    /* Amount of bytes from buffer shown in each row. */
50#define BARS 2             /* Amount of bars in each row. */
51/* Max length of each row string. */
52#define ROW_LENGTH (BYTES_PER_ROW * (HEX_FMT_LENGTH + ASCII_FMT_LENGTH) + BARS)
53
54/*
55 *  Put the body below rtems_print_buffer so it won't get inlined.
56 */
57
58static void Dump_Line(const unsigned char *buffer, const unsigned int length);
59
60/**
61 * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
62 * Printing will be done in rows, each showing BYTES_PER_ROW bytes.
63 * @details Non-printable chars will appear as dots.
64 *
65 * @param buffer The buffer we'll print.
66 * @param length Amount of bytes from \p buffer we'll print. This can't be
67 * unsigned because we don't have a way to check if we're erroneously getting
68 * a negative \p length.
69 */
70void rtems_print_buffer(const unsigned char *buffer, const int length)
71{
72  unsigned int i, mod, max;
73
74  if (length > 0) {
75    mod = length % BYTES_PER_ROW;
76
77    max = length - mod;
78
79    /* Print length / BYTES_PER_ROW rows. */
80    for (i = 0; i < max; i += BYTES_PER_ROW) {
81      Dump_Line(&buffer[i], BYTES_PER_ROW);
82    }
83
84    /* Print another row with the remaining bytes. */
85    if (mod > 0) {
86      Dump_Line(&buffer[max], mod);
87    }
88  } else {
89    printk("Error: length must be greater than zero.");
90  }
91}
92
93static char const hexlist[] = "0123456789abcdef";
94
95/**
96 * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
97 * @details Non-printable chars will appear as dots.
98 *
99 * @param buffer The buffer we'll print.
100 * @param length Amount of bytes from \p buffer we'll print.
101 */
102static void Dump_Line(const unsigned char *buffer, const unsigned int length)
103{
104  unsigned int i;
105
106  /* Output the hex value of each byte. */
107  for (i = 0; i < length; ++i) {
108    unsigned char c = buffer[i];
109
110    rtems_putc(hexlist[(c >> 4) & 0xf]);
111    rtems_putc(hexlist[c & 0xf]);
112    rtems_putc(' ');
113  }
114
115  /* Fill the remaining space with whitespace (if necessary). */
116  for (; i < BYTES_PER_ROW; ++i) {
117    rtems_putc(' ');
118    rtems_putc(' ');
119    rtems_putc(' ');
120  }
121
122  /* Append a bar. */
123  rtems_putc('|');
124
125  /* Now output the ASCII glyphs of printable chars. */
126  for (i = 0; i < length; ++i) {
127    unsigned char c = buffer[i];
128
129    rtems_putc(isprint(c) ? c : '.');
130  }
131
132  /* Fill the remaining space with whitespace (if necessary). */
133  for(; i < BYTES_PER_ROW; i++) {
134    rtems_putc(' ');
135  }
136
137  /* Append another bar and print the resulting string. */
138  rtems_putc('|');
139  rtems_putc('\n');
140}
Note: See TracBrowser for help on using the repository browser.