1 /******************************************************************************
2 *
3 * Copyright(c) 2019 Realtek Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 *****************************************************************************/
15
16 #include "hal_str_proc.h"
17
18
hal_is_comment_string(char * szStr)19 bool hal_is_comment_string(
20 char *szStr
21 )
22 {
23 if (*szStr == '/' && *(szStr + 1) == '/')
24 return true;
25 else
26 return false;
27 }
28
hal_get_fractionvalue_fromstring(char * szStr,u8 * pInteger,u8 * pFraction,u32 * pu4bMove)29 bool hal_get_fractionvalue_fromstring(
30 char *szStr,
31 u8 *pInteger,
32 u8 *pFraction,
33 u32 *pu4bMove
34 )
35 {
36 char *szScan = szStr;
37
38 /* Initialize output. */
39 *pu4bMove = 0;
40 *pInteger = 0;
41 *pFraction = 0;
42
43 /* Skip leading space. */
44 while (*szScan != '\0' && (*szScan == ' ' || *szScan == '\t')) {
45 ++szScan;
46 ++(*pu4bMove);
47 }
48
49 if (*szScan < '0' || *szScan > '9')
50 return false;
51
52 /* Parse each digit. */
53 do {
54 (*pInteger) *= 10;
55 *pInteger += (*szScan - '0');
56
57 ++szScan;
58 ++(*pu4bMove);
59
60 if (*szScan == '.') {
61 ++szScan;
62 ++(*pu4bMove);
63
64 if (*szScan < '0' || *szScan > '9')
65 return false;
66
67 *pFraction += (*szScan - '0') * 10;
68 ++szScan;
69 ++(*pu4bMove);
70
71 if (*szScan >= '0' && *szScan <= '9') {
72 *pFraction += *szScan - '0';
73 ++szScan;
74 ++(*pu4bMove);
75 }
76 return true;
77 }
78 } while (*szScan >= '0' && *szScan <= '9');
79
80 return true;
81 }
82
hal_is_alpha(char ch_tmp)83 bool hal_is_alpha(char ch_tmp)
84 {
85 if ((ch_tmp >= 'a' && ch_tmp <= 'z') ||
86 (ch_tmp >= 'A' && ch_tmp <= 'Z'))
87 return true;
88 else
89 return false;
90 }
91
hal_ishexdigit(char ch_tmp)92 bool hal_ishexdigit(char ch_tmp)
93 {
94 if ((ch_tmp >= '0' && ch_tmp <= '9') ||
95 (ch_tmp >= 'a' && ch_tmp <= 'f') ||
96 (ch_tmp >= 'A' && ch_tmp <= 'F'))
97 return true;
98 else
99 return false;
100 }
101
hal_get_hexvalue_fromstring(char * szStr,u32 * pu4bVal,u32 * pu4bMove)102 bool hal_get_hexvalue_fromstring(
103 char *szStr,
104 u32 *pu4bVal,
105 u32 *pu4bMove
106 )
107 {
108 char *szScan = szStr;
109
110 /* Check input parameter. */
111 if (szStr == NULL || pu4bVal == NULL || pu4bMove == NULL) {
112 PHL_INFO("GetHexValueFromString(): Invalid inpur argumetns! szStr: %p, pu4bVal: %p, pu4bMove: %p\n", szStr, pu4bVal, pu4bMove);
113 return false;
114 }
115
116 /* Initialize output. */
117 *pu4bMove = 0;
118 *pu4bVal = 0;
119
120 /* Skip leading space. */
121 while (*szScan != '\0' &&
122 (*szScan == ' ' || *szScan == '\t')) {
123 szScan++;
124 (*pu4bMove)++;
125 }
126
127 /* Skip leading '0x' or '0X'. */
128 if (*szScan == '0' && (*(szScan + 1) == 'x' || *(szScan + 1) == 'X')) {
129 szScan += 2;
130 (*pu4bMove) += 2;
131 }
132
133 /* Check if szScan is now pointer to a character for hex digit, */
134 /* if not, it means this is not a valid hex number. */
135 if (!hal_ishexdigit(*szScan))
136 return false;
137
138 /* Parse each digit. */
139 do {
140 (*pu4bVal) <<= 4;
141 *pu4bVal += hal_mapchar_tohexdigit(*szScan);
142
143 szScan++;
144 (*pu4bMove)++;
145 } while (hal_ishexdigit(*szScan));
146
147 return true;
148 }
149
hal_is_allspace_tab(char * data,u8 size)150 bool hal_is_allspace_tab(
151 char *data,
152 u8 size
153 )
154 {
155 u8 cnt = 0, NumOfSpaceAndTab = 0;
156
157 while (size > cnt) {
158 if (data[cnt] == ' ' || data[cnt] == '\t' || data[cnt] == '\0')
159 ++NumOfSpaceAndTab;
160
161 ++cnt;
162 }
163
164 return size == NumOfSpaceAndTab;
165 }
166
hal_mapchar_tohexdigit(char chTmp)167 u32 hal_mapchar_tohexdigit(
168 char chTmp
169 )
170 {
171 if (chTmp >= '0' && chTmp <= '9')
172 return chTmp - '0';
173 else if (chTmp >= 'a' && chTmp <= 'f')
174 return 10 + (chTmp - 'a');
175 else if (chTmp >= 'A' && chTmp <= 'F')
176 return 10 + (chTmp - 'A');
177 else
178 return 0;
179 }
180
181
hal_parse_fiedstring(char * in_str,u32 * start,char * out_str,char lqualifier,char rqualifier)182 bool hal_parse_fiedstring(char *in_str, u32 *start, char *out_str, char lqualifier, char rqualifier)
183 {
184 u32 i = 0, j = 0;
185 char c = in_str[(*start)++];
186
187 if (c != lqualifier)
188 return false;
189
190 i = (*start);
191 c = in_str[(*start)++];
192
193 while (c != rqualifier && c != '\0')
194 c = in_str[(*start)++];
195
196 if (c == '\0')
197 return false;
198
199 j = (*start) - 2;
200 _os_strncpy((char *)out_str, (const char *)(in_str + i), j - i + 1);
201
202 return true;
203 }
204
205
hal_get_u1bint_fromstr_indec(char * str,u8 * pint)206 bool hal_get_u1bint_fromstr_indec(char *str, u8 *pint)
207 {
208 u16 i = 0;
209 *pint = 0;
210
211 while (str[i] != '\0') {
212 if (str[i] >= '0' && str[i] <= '9') {
213 *pint *= 10;
214 *pint += (str[i] - '0');
215 } else
216 return false;
217 ++i;
218 }
219
220 return true;
221 }
222
223
hal_get_s1bint_fromstr_indec(char * str,s8 * val)224 bool hal_get_s1bint_fromstr_indec(char *str, s8 *val)
225 {
226 u8 negative = 0;
227 u16 i = 0;
228
229 *val = 0;
230
231 while (str[i] != '\0') {
232 if (i == 0 && (str[i] == '+' || str[i] == '-')) {
233 if (str[i] == '-')
234 negative = 1;
235 } else if (str[i] >= '0' && str[i] <= '9') {
236 *val *= 10;
237 *val += (str[i] - '0');
238 } else
239 return false;
240 ++i;
241 }
242
243 if (negative)
244 *val = -*val;
245
246 return true;
247 }
248
249