print: add function to output data
This commit is contained in:
parent
f18d5ea850
commit
e7f93bfeab
38
lib/print.c
38
lib/print.c
|
@ -15,7 +15,7 @@
|
|||
/** printing utilities to replace the large printf from the standard library (code)
|
||||
* @file print.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @date 2017-2019
|
||||
*/
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
|
@ -25,6 +25,7 @@
|
|||
#include <math.h> // mathematics utilities to handle floating points
|
||||
|
||||
/* own libraries */
|
||||
#include "global.h" // some macro definitions
|
||||
#include "print.h" // printing utilities
|
||||
|
||||
uint8_t print_error;
|
||||
|
@ -464,3 +465,38 @@ size_t snprintf(char* str, size_t size, const char* format, ...)
|
|||
va_end(arglist);
|
||||
return length;
|
||||
}
|
||||
|
||||
size_t print_data(uint32_t offset, const uint8_t* data, size_t length)
|
||||
{
|
||||
size_t to_return = 0; // total number of characters printed
|
||||
uint32_t address = (offset / 16) * 16; // address of the data to print
|
||||
|
||||
if (offset > SIZE_MAX - length) { // prevent integer overflow on address
|
||||
return 0;
|
||||
}
|
||||
while (address < offset + length) { // print data lines until the end
|
||||
ADDU32_SAFE(to_return, printf("%08x: ", address)); // print address
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
if (address < offset || address >= offset + length) {
|
||||
ADDU32_SAFE(to_return, printf(" "));
|
||||
} else {
|
||||
ADDU32_SAFE(to_return, printf("%02x ", data[address - offset]));
|
||||
}
|
||||
address++;
|
||||
}
|
||||
address -= 16;
|
||||
ADDU32_SAFE(to_return, putc(' '));
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
if (address < offset || address >= offset + length) {
|
||||
ADDU32_SAFE(to_return, putc(' '));
|
||||
} else if (data[address - offset] < ' ' || data[address - offset] > '~') {
|
||||
ADDU32_SAFE(to_return, putc('.'));
|
||||
} else {
|
||||
ADDU32_SAFE(to_return, putc(data[address - offset]));
|
||||
}
|
||||
address++;
|
||||
}
|
||||
ADDU32_SAFE(to_return, putc('\n'));
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
* - B for uint64_t bits
|
||||
* @file print.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @date 2017-2019
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
@ -62,3 +62,10 @@ size_t printf(const char* format, ...);
|
|||
* @return number of characters printed (a return value of size or more means that the output was truncated)
|
||||
**/
|
||||
size_t snprintf(char* str, size_t size, const char* format, ...);
|
||||
/** print data in xxd style
|
||||
* @param[in] offset base address of the data to print
|
||||
* @param[in] data data to print
|
||||
* @param[in] length size of data to print
|
||||
* @return number of characters printed (a return value of size or more means that the output was truncated)
|
||||
*/
|
||||
size_t print_data(uint32_t offset, const uint8_t* data, size_t length);
|
||||
|
|
Loading…
Reference in New Issue