source: rtems/cpukit/libmisc/dumpbuf/dumpbuf.c @ cc93c83

Last change on this file since cc93c83 was cc93c83, checked in by Martin Galvan <martin.galvan@…>, on Sep 2, 2015 at 9:54:24 PM

cpukit/libmisc/dumpbuf/dumpbuf.c: Fix undefined behavior for sprintf()

I also used the 'n' versions of the string functions, #define'd magic numbers
and added a few comments.

Updates #2405.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief Dump Buffer
5 * @ingroup libmisc_dumpbuf Print Memory Buffer
6 */
7
8/*
9 *  COPYRIGHT (c) 1997-2015.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may in
13 *  the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <stdio.h>
22#include <string.h>
23#include <ctype.h>
24#include <rtems/dumpbuf.h>
25#include <rtems/bspIo.h>
26
27#define HEX_FMT_LENGTH 3   /* Length of the formatted hex string. */
28#define ASCII_FMT_LENGTH 1 /* Length of the formatted ASCII string. */
29#define BYTES_PER_ROW 16    /* Amount of bytes from buffer shown in each row. */
30#define BARS 2             /* Amount of bars in each row. */
31/* Max length of each row string. */
32#define ROW_LENGTH (BYTES_PER_ROW * (HEX_FMT_LENGTH + ASCII_FMT_LENGTH) + BARS)
33
34/*
35 *  Put the body below rtems_print_buffer so it won't get inlined.
36 */
37
38static void Dump_Line(const unsigned char *buffer, const unsigned int length);
39
40/**
41 * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
42 * Printing will be done in rows, each showing BYTES_PER_ROW bytes.
43 * @details Non-printable chars will appear as dots.
44 *
45 * @param buffer The buffer we'll print.
46 * @param length Amount of bytes from \p buffer we'll print. This can't be
47 * unsigned because we don't have a way to check if we're erroneously getting
48 * a negative \p length.
49 */
50void rtems_print_buffer(const unsigned char *buffer, const int length)
51{
52  unsigned int i, mod, max;
53
54  if (length > 0) {
55    mod = length % BYTES_PER_ROW;
56
57    max = length - mod;
58
59    /* Print length / BYTES_PER_ROW rows. */
60    for (i = 0; i < max; i += BYTES_PER_ROW) {
61      Dump_Line(&buffer[i], BYTES_PER_ROW);
62    }
63
64    /* Print another row with the remaining bytes. */
65    if (mod > 0) {
66      Dump_Line(&buffer[max], mod);
67    }
68  } else {
69    printk("Error: length must be greater than zero.");
70  }
71}
72
73/**
74 * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
75 * @details Non-printable chars will appear as dots.
76 *
77 * @param buffer The buffer we'll print.
78 * @param length Amount of bytes from \p buffer we'll print.
79 */
80static void Dump_Line(const unsigned char *buffer, const unsigned int length)
81{
82  unsigned int i;
83  static unsigned char line_buffer[ROW_LENGTH] = "";
84  size_t tmp_len;
85
86  /* Output the hex value of each byte. */
87  for (i = 0; i < length; ++i) {
88    snprintf(&line_buffer[i * HEX_FMT_LENGTH], HEX_FMT_LENGTH + 1,
89             "%02x ", buffer[i]);
90  }
91
92  /* Fill the remaining space with whitespace (if necessary). */
93  for (; i < BYTES_PER_ROW; ++i) {
94    strncat(line_buffer, "   ", HEX_FMT_LENGTH);
95  }
96
97  /* Append a bar. */
98  strncat(line_buffer, "|", 1);
99  tmp_len = strnlen(line_buffer, ROW_LENGTH);
100
101  /* Now output the ASCII glyphs of printable chars. */
102  for (i = 0; i < length; ++i) {
103    snprintf(&line_buffer[tmp_len + i], ASCII_FMT_LENGTH + 1,
104             "%c", isprint(buffer[i]) ? buffer[i] : '.');
105  }
106
107  /* Fill the remaining space with whitespace (if necessary). */
108  for(; i < BYTES_PER_ROW; i++) {
109    strncat(line_buffer, " ", ASCII_FMT_LENGTH);
110  }
111
112  /* Append another bar and print the resulting string. */
113  printk("%s|\n", line_buffer);
114}
Note: See TracBrowser for help on using the repository browser.