]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/string-extras.c
'hardening' infrastucture against toolchain bugs
[arm-crypto-lib.git] / test_src / string-extras.c
1 /* string_extras.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2008  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        string_extras.c
21  * \author  Daniel Otte 
22  * \date    2006-05-16
23  * \license     GPLv3 or later
24  * 
25  */
26
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31
32 #include "cli.h"
33
34 uint32_t stridentcnt(const char* a, const char* b){
35         uint16_t i=0;
36         for(;;){
37                 if(*a != *b || *b=='\0')
38                         return i;
39                 i++;
40                 a++;
41                 b++;
42         }
43 }
44
45 uint16_t firstword_length(const char* s){
46         uint16_t ret=0;
47         if(!s){
48                 return 0;
49         }
50         int c;
51         while(c=*s++, isgraph(c)){
52                 ret++;
53         }
54         return ret; 
55 }
56
57 char* strstrip(char* str){
58         if(!str)
59                 return str;
60         char* endptr;
61         while(*str && (*str==' ' || *str=='\t'))
62                 ++str;
63         endptr=str;
64         while(*endptr)
65                 ++endptr;
66         do{
67                 --endptr;
68         }while(*endptr==' ' || *endptr=='\t');
69         endptr[1]='\0';
70         return str;
71 }
72
73 void str_reverse(char* buffer){
74         char *i, *j;
75         char c;
76         i=buffer;
77         j=buffer + strlen(buffer)-1;
78         while(i<j){
79                 c = *i;
80                 *i = *j;
81                 *j = c;
82                 ++i;
83                 --j;
84         }
85 }
86
87 char* ultoa(unsigned long a, char* buffer, uint8_t radix){
88         if(radix<2 || radix>36){
89                 return NULL;
90         }
91         char* ptr=buffer;
92         div_t result;
93         if(a==0){
94                 ptr[0] = '0';
95                 ptr[1] = '\0';
96                 return buffer;
97         }
98         while(a){
99                 /* toolchain bug??
100                 result = div(a, radix);
101                 */
102                 result.quot = a/radix;
103                 result.rem = a%radix;
104                 *ptr = result.rem;
105                 if(result.rem<10){
106                         *ptr += '0';
107                 }else{
108                         *ptr += 'a'-10;
109                 }
110                 ++ptr;
111                 a = result.quot;
112         }
113         *ptr = '\0';
114         str_reverse(buffer);
115         return buffer;
116 }
117
118
119 char* ulltoa(unsigned long long a, char* buffer, uint8_t radix){
120         if(radix<2 || radix>36){
121                 return NULL;
122         }
123         char* ptr=buffer;
124         uint8_t rem;
125         unsigned long long quot;
126         if(a==0){
127                 ptr[0] = '0';
128                 ptr[1] = '\0';
129                 return buffer;
130         }
131         while(a){
132                 rem = a % radix;
133                 quot = a / radix;
134                 if(rem<10){
135                         rem += '0';
136                 }else{
137                         rem += 'a'-10;
138                 }
139                 *ptr++ =rem;
140                 a = quot;
141         }
142         *ptr = '\0';
143         str_reverse(buffer);
144         return buffer;
145 }
146
147 char* ustoa(unsigned short a, char* buffer, uint8_t radix){
148         return ultoa((unsigned long)a, buffer, radix);
149 }
150 /*
151 void strlwr(char* s){
152         while(*s){
153                 *s = tolower(*s);
154                 s++;
155         }
156 }
157 */