2018-03-04 16:59:20 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "format.h"
|
|
|
|
|
|
|
|
|
|
|
|
static struct cw_FormatDumpSettings CW_FORMAT_DUMP_SETTINGS = {
|
2018-03-09 07:44:17 +01:00
|
|
|
24, /* row_len */
|
2018-03-05 07:18:02 +01:00
|
|
|
8, /* marker_distance */
|
|
|
|
'|', /* marker_char */
|
|
|
|
1, /* ascii */
|
|
|
|
0, /* inv_len */
|
|
|
|
'*', /* inv_char */
|
|
|
|
"\n\t", /* dump_prefix */
|
2018-03-09 07:44:17 +01:00
|
|
|
"", /* dump_suffix */
|
2018-03-05 07:18:02 +01:00
|
|
|
" ", /* ascii_prefix */
|
|
|
|
"\n\t" /* newline */
|
2018-03-04 16:59:20 +01:00
|
|
|
};
|
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
void cw_format_get_dump_defaults(struct cw_FormatDumpSettings * settings)
|
|
|
|
{
|
|
|
|
memcpy(settings, &CW_FORMAT_DUMP_SETTINGS,
|
|
|
|
sizeof(struct cw_FormatDumpSettings));
|
|
|
|
}
|
2018-03-04 16:59:20 +01:00
|
|
|
|
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
static int cw_format_dump_row(char *dst, const uint8_t * data, int len,
|
|
|
|
struct cw_FormatDumpSettings * settings){
|
|
|
|
char *pdst, marker;
|
|
|
|
int i;
|
2018-03-04 16:59:20 +01:00
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
pdst = dst;
|
2018-03-04 16:59:20 +01:00
|
|
|
for (i = 0; i < len; i++) {
|
2018-03-05 07:18:02 +01:00
|
|
|
marker = ((i+1)% settings->marker_distance==0 && i<len-1)
|
|
|
|
? settings->marker_char
|
|
|
|
: ' ';
|
|
|
|
pdst += sprintf(pdst, "%02X%c", data[i] & 0xff, marker);
|
2018-03-04 16:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
if (settings->ascii){
|
|
|
|
for(i=len; i<settings->row_len; i++){
|
|
|
|
pdst += sprintf(pdst," ");
|
|
|
|
}
|
|
|
|
|
|
|
|
pdst+=sprintf(pdst,"%s",settings->ascii_prefix);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
int c = data[i] & 0xff;
|
|
|
|
if (c < 0x20 || c > 0x7f)
|
|
|
|
c = '.';
|
|
|
|
pdst+=sprintf(pdst,"%c",c);
|
|
|
|
}
|
2018-03-04 16:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return pdst-dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an ASCII hex dump of binary data
|
|
|
|
*
|
|
|
|
* @param data data to dump
|
|
|
|
* @param len number of bytes to dump (size of data)
|
|
|
|
* @return a character string with the created data ASCII
|
|
|
|
* dump (must be released with free).If memory cannot be allocated
|
|
|
|
* the return value is NULL.
|
|
|
|
*/
|
|
|
|
char *cw_format_dump(const uint8_t * data, int len,
|
|
|
|
struct cw_FormatDumpSettings *settings)
|
|
|
|
{
|
2018-03-05 07:18:02 +01:00
|
|
|
int row,rows,size;
|
|
|
|
char *dst, *pdst;
|
2018-03-04 16:59:20 +01:00
|
|
|
|
|
|
|
if (!settings)
|
|
|
|
settings = &CW_FORMAT_DUMP_SETTINGS;
|
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
rows = len / settings->row_len;
|
|
|
|
if (len % settings->row_len)
|
|
|
|
rows++;
|
2018-03-04 16:59:20 +01:00
|
|
|
|
2018-03-09 07:44:17 +01:00
|
|
|
size = strlen(settings->dump_prefix) + strlen(settings->dump_suffix) +
|
2018-03-05 07:18:02 +01:00
|
|
|
rows * strlen(settings->newline) +
|
|
|
|
(settings->ascii ? rows*settings->row_len*3 : len * 3);
|
2018-03-04 16:59:20 +01:00
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
if (settings->ascii){
|
|
|
|
size += len + rows*strlen(settings->ascii_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = malloc(size+1);
|
2018-03-04 16:59:20 +01:00
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pdst = dst;
|
2018-03-05 07:18:02 +01:00
|
|
|
pdst += sprintf(pdst, "%s",settings->dump_prefix);
|
2018-03-04 16:59:20 +01:00
|
|
|
|
2018-03-05 07:18:02 +01:00
|
|
|
for (row=0; row<rows; row++){
|
|
|
|
int rlen, pos;
|
|
|
|
pos = row * settings->row_len;
|
|
|
|
if (len - pos > settings->row_len){
|
|
|
|
rlen = settings->row_len;
|
2018-03-04 16:59:20 +01:00
|
|
|
}
|
2018-03-05 07:18:02 +01:00
|
|
|
else{
|
|
|
|
rlen = len-pos;
|
2018-03-04 16:59:20 +01:00
|
|
|
}
|
2018-03-05 07:18:02 +01:00
|
|
|
pdst += cw_format_dump_row(pdst,data+pos, rlen, settings);
|
2018-03-09 07:44:17 +01:00
|
|
|
if(row<rows-1)
|
2018-03-07 13:13:58 +01:00
|
|
|
pdst += sprintf(pdst,"%s",settings->newline);
|
|
|
|
else
|
|
|
|
pdst += sprintf(pdst,"%s",settings->dump_suffix);
|
2018-03-04 16:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
2018-03-05 07:18:02 +01:00
|
|
|
|
2018-03-04 16:59:20 +01:00
|
|
|
}
|
|
|
|
|