Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:18

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup libmisc_dumpbuf Print Memory Buffer
0007  *
0008  * @brief Dump Buffer
0009  */
0010 
0011 /*
0012  *  COPYRIGHT (c) 1997-2015.
0013  *  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include <stdio.h>
0042 #include <string.h>
0043 #include <ctype.h>
0044 #include <rtems/dumpbuf.h>
0045 #include <rtems/bspIo.h>
0046 
0047 #define HEX_FMT_LENGTH 3   /* Length of the formatted hex string. */
0048 #define ASCII_FMT_LENGTH 1 /* Length of the formatted ASCII string. */
0049 #define BYTES_PER_ROW 16    /* Amount of bytes from buffer shown in each row. */
0050 #define BARS 2             /* Amount of bars in each row. */
0051 /* Max length of each row string. */
0052 #define ROW_LENGTH (BYTES_PER_ROW * (HEX_FMT_LENGTH + ASCII_FMT_LENGTH) + BARS)
0053 
0054 /*
0055  *  Put the body below rtems_print_buffer so it won't get inlined.
0056  */
0057 
0058 static void Dump_Line(const unsigned char *buffer, const unsigned int length);
0059 
0060 /**
0061  * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
0062  * Printing will be done in rows, each showing BYTES_PER_ROW bytes.
0063  * @details Non-printable chars will appear as dots.
0064  *
0065  * @param buffer The buffer we'll print.
0066  * @param length Amount of bytes from \p buffer we'll print. This can't be
0067  * unsigned because we don't have a way to check if we're erroneously getting
0068  * a negative \p length.
0069  */
0070 void rtems_print_buffer(const unsigned char *buffer, const int length)
0071 {
0072   unsigned int i, mod, max;
0073 
0074   if (length > 0) {
0075     mod = length % BYTES_PER_ROW;
0076 
0077     max = length - mod;
0078 
0079     /* Print length / BYTES_PER_ROW rows. */
0080     for (i = 0; i < max; i += BYTES_PER_ROW) {
0081       Dump_Line(&buffer[i], BYTES_PER_ROW);
0082     }
0083 
0084     /* Print another row with the remaining bytes. */
0085     if (mod > 0) {
0086       Dump_Line(&buffer[max], mod);
0087     }
0088   } else {
0089     printk("Error: length must be greater than zero.");
0090   }
0091 }
0092 
0093 static char const hexlist[] = "0123456789abcdef";
0094 
0095 /**
0096  * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
0097  * @details Non-printable chars will appear as dots.
0098  *
0099  * @param buffer The buffer we'll print.
0100  * @param length Amount of bytes from \p buffer we'll print.
0101  */
0102 static void Dump_Line(const unsigned char *buffer, const unsigned int length)
0103 {
0104   unsigned int i;
0105 
0106   /* Output the hex value of each byte. */
0107   for (i = 0; i < length; ++i) {
0108     unsigned char c = buffer[i];
0109 
0110     rtems_putc(hexlist[(c >> 4) & 0xf]);
0111     rtems_putc(hexlist[c & 0xf]);
0112     rtems_putc(' ');
0113   }
0114 
0115   /* Fill the remaining space with whitespace (if necessary). */
0116   for (; i < BYTES_PER_ROW; ++i) {
0117     rtems_putc(' ');
0118     rtems_putc(' ');
0119     rtems_putc(' ');
0120   }
0121 
0122   /* Append a bar. */
0123   rtems_putc('|');
0124 
0125   /* Now output the ASCII glyphs of printable chars. */
0126   for (i = 0; i < length; ++i) {
0127     unsigned char c = buffer[i];
0128 
0129     rtems_putc(isprint(c) ? c : '.');
0130   }
0131 
0132   /* Fill the remaining space with whitespace (if necessary). */
0133   for(; i < BYTES_PER_ROW; i++) {
0134     rtems_putc(' ');
0135   }
0136 
0137   /* Append another bar and print the resulting string. */
0138   rtems_putc('|');
0139   rtems_putc('\n');
0140 }