]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/dump.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / test_src / dump.c
1 /* dump.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file     dump.c
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2009-02-04
24  * \license  GPLv3 or later
25  * 
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include <stdlib.h>                                     
31 #include <ctype.h>
32 #include "cli.h" 
33 #include "string-extras.h" 
34  
35 #define DUMP_WIDTH 16
36
37 static
38 void dump_chars(uint8_t* buffer, uint8_t len){
39         uint8_t i;
40         cli_putc('|');
41         for(i=0; i<len; ++i){
42                 if(isprint(buffer[i])){
43                         cli_putc(buffer[i]);
44                 }else{
45                         cli_putc('.');
46                 }
47         }
48         for(;len<DUMP_WIDTH; ++len){
49                 cli_putc(' ');
50         }
51         cli_putc('|');
52 }
53
54 static 
55 void print_aligned(unsigned long value, uint8_t align){
56         char str[10];
57         uint8_t i;
58         ultoa(value, str, 16);
59         for(i=strlen(str);i<align;++i)
60                 cli_putc(' ');
61         cli_putstr(str);
62 }
63
64
65 void dump(char* s){
66         uint8_t readlen;
67         uint32_t addr=0;
68         uint32_t size=32;
69         uint8_t buffer[DUMP_WIDTH];
70         char tstr[9];
71         if(s){
72                 s=strstrip(s);
73                 if(isalpha((uint8_t)(*s))){
74                         while(isalpha((uint8_t)(*s)))
75                                 ++s;
76                 }
77                 char* eptr;
78                 if(*s)
79                         addr = strtoul(s, &eptr, 0);
80                 if(eptr)
81                         size = strtoul(eptr, NULL, 0);
82                 if(!size)
83                         size = 32;
84         }
85         cli_putstr("\r\ndumping ");
86         ultoa(size, tstr, 10);
87         cli_putstr(tstr);
88         cli_putstr(" bytes, beginning at 0x");
89         ultoa(addr, tstr, 16);
90         cli_putstr(tstr);
91         cli_putstr(":\r\n");
92         uint8_t t;
93         while(size){
94                 readlen = (size>DUMP_WIDTH)?DUMP_WIDTH:size;
95                 memcpy(buffer, (void*)addr, readlen);
96                 print_aligned(addr, 6);
97                 cli_putstr(": ");
98                 cli_hexdump2(buffer, readlen);
99                 t=(DUMP_WIDTH-readlen)*3;
100                 while(t--){
101                         cli_putc(' ');
102                 }
103                 cli_putc('\t');
104                 dump_chars(buffer,readlen);
105                 addr+=readlen;
106                 size-=readlen;
107                 cli_putstr("\r\n");
108         }
109 }
110