]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/string-extras.c
switching to dedicated endian switching function
[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         return 0;
44 }
45
46 uint16_t firstword_length(const char* s){
47         uint16_t ret=0;
48         if(!s){
49                 return 0;
50         }
51         int c;
52         while(c=*s++, isgraph(c)){
53                 ret++;
54         }
55         return ret; 
56 }
57
58 char* strstrip(char* str){
59         if(!str)
60                 return str;
61         char* endptr;
62         while(*str && (*str==' ' || *str=='\t'))
63                 ++str;
64         endptr=str;
65         while(*endptr)
66                 ++endptr;
67         do{
68                 --endptr;
69         }while(*endptr==' ' || *endptr=='\t');
70         endptr[1]='\0';
71         return str;
72 }
73
74 void str_reverse(char* buffer){
75         char *i, *j;
76         char c;
77         i=buffer;
78         j=buffer + strlen(buffer)-1;
79         while(i<j){
80                 c = *i;
81                 *i = *j;
82                 *j = c;
83                 ++i;
84                 --j;
85         }
86 }
87
88 char* ultoa(unsigned long a, char* buffer, uint8_t radix){
89         if(radix<2 || radix>36){
90                 return NULL;
91         }
92         char* ptr=buffer;
93         div_t result;
94         if(a==0){
95                 ptr[0] = '0';
96                 ptr[1] = '\0';
97                 return buffer;
98         }
99         while(a){
100                 /* toolchain bug??
101                 result = div(a, radix);
102                 */
103                 result.quot = a/radix;
104                 result.rem = a%radix;
105                 *ptr = result.rem;
106                 if(result.rem<10){
107                         *ptr += '0';
108                 }else{
109                         *ptr += 'a'-10;
110                 }
111                 ++ptr;
112                 a = result.quot;
113         }
114         *ptr = '\0';
115         str_reverse(buffer);
116         return buffer;
117 }
118
119
120 char* ulltoa(unsigned long long a, char* buffer, uint8_t radix){
121         if(radix<2 || radix>36){
122                 return NULL;
123         }
124         char* ptr=buffer;
125         uint8_t rem;
126         unsigned long long quot;
127         if(a==0){
128                 ptr[0] = '0';
129                 ptr[1] = '\0';
130                 return buffer;
131         }
132         while(a){
133                 rem = a % radix;
134                 quot = a / radix;
135                 if(rem<10){
136                         rem += '0';
137                 }else{
138                         rem += 'a'-10;
139                 }
140                 *ptr++ =rem;
141                 a = quot;
142         }
143         *ptr = '\0';
144         str_reverse(buffer);
145         return buffer;
146 }
147
148 char* ustoa(unsigned short a, char* buffer, uint8_t radix){
149         return ultoa((unsigned long)a, buffer, radix);
150 }
151 /*
152 void strlwr(char* s){
153         while(*s){
154                 *s = tolower(*s);
155                 s++;
156         }
157 }
158 */
159
160 char* utoa(unsigned a, char* buffer, uint8_t radix){
161         return ultoa((unsigned)a, buffer, radix);
162 }
163
164 char* itoa(int a, char* buffer, uint8_t radix){
165         if(a<0){
166                 *buffer = '-';
167                 a = -a;
168         }
169         ultoa(a, buffer + 1, radix);
170         return buffer;
171 }