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