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