summaryrefslogtreecommitdiff
path: root/network/wlan/WDI/COMMON/GeneralFunc.c
blob: 3666a25e41896610967b5ef1920e83d60e752a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "Mp_Precomp.h"

#if WPP_SOFTWARE_TRACE
#include "GeneralFunc.tmh"
#endif

BOOLEAN 
eqNByte(
	pu1Byte	str1,
	pu1Byte	str2,
	u4Byte	num
	)
{
	if(num==0)
		return FALSE;
	while(num>0)
	{
		num--;
		if(str1[num]!=str2[num])
			return FALSE;
	}
	return TRUE;
}


//
//	Description:
//		Return TRUE if chTmp is represent for hex digit and 
//		FALSE otherwise.
//
//
BOOLEAN
IsHexDigit(
	IN		s1Byte		chTmp
)
{
	if( (chTmp >= '0' && chTmp <= '9') ||  
		(chTmp >= 'a' && chTmp <= 'f') ||
		(chTmp >= 'A' && chTmp <= 'F') )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


//
//	Description:
//		Translate a character to hex digit.
//
u4Byte
MapCharToHexDigit(
	IN		s1Byte		chTmp
)
{
	if(chTmp >= '0' && chTmp <= '9')
		return (chTmp - '0');
	else if(chTmp >= 'a' && chTmp <= 'f')
		return (10 + (chTmp - 'a'));
	else if(chTmp >= 'A' && chTmp <= 'F') 
		return (10 + (chTmp - 'A'));
	else
		return 0;	
}



//
//	Description:
//		Parse hex number from the string pucStr.
//
BOOLEAN 
GetHexValueFromString(
	IN		ps1Byte			szStr,
	IN OUT	pu4Byte			pu4bVal,
	IN OUT	pu4Byte			pu4bMove
)
{
	ps1Byte		szScan = szStr;

	// Check input parameter.
	if(szStr == NULL || pu4bVal == NULL || pu4bMove == NULL)
	{
		RT_TRACE(COMP_DBG, DBG_WARNING, 
			("GetHexValueFromString(): Invalid inpur argumetns! szStr: %p, pu4bVal: %p, pu4bMove: %p\n", szStr, pu4bVal, pu4bMove));
		return FALSE;
	}

	// Initialize output.
	*pu4bMove = 0;
	*pu4bVal = 0;

	// Skip leading space.
	while(	*szScan != '\0' && 
			(*szScan == ' ' || *szScan == '\t') )
	{
		szScan++;
		(*pu4bMove)++;
	}

	// Skip leading '0x' or '0X'.
	if(*szScan == '0' && (*(szScan+1) == 'x' || *(szScan+1) == 'X'))
	{
		szScan += 2;
		(*pu4bMove) += 2;
	}	

	// Check if szScan is now pointer to a character for hex digit, 
	// if not, it means this is not a valid hex number.
	if(!IsHexDigit(*szScan))
	{
		return FALSE;
	}

	// Parse each digit.
	do
	{
		(*pu4bVal) <<= 4;
		*pu4bVal += MapCharToHexDigit(*szScan);

		szScan++;
		(*pu4bMove)++;
	} while(IsHexDigit(*szScan));

	return TRUE;
}

BOOLEAN 
GetFractionValueFromString(
	IN		ps1Byte			szStr,
	IN OUT	pu1Byte			pInteger,
	IN OUT  pu1Byte			pFraction,
	IN OUT	pu4Byte			pu4bMove
)
{
	ps1Byte		szScan = szStr;

	// Initialize output.
	*pu4bMove = 0;
	*pInteger = 0;
	*pFraction = 0;

	// Skip leading space.
	while (	*szScan != '\0' && 	(*szScan == ' ' || *szScan == '\t') ) {
		++szScan;
		++(*pu4bMove);
	}

	// Parse each digit.
	do {
		(*pInteger) *= 10;
		*pInteger += ( *szScan - '0' );

		++szScan;
		++(*pu4bMove);

		if ( *szScan == '.' ) 
		{
			++szScan;
			++(*pu4bMove);
			
			if ( *szScan < '0' || *szScan > '9' )
				return FALSE;
			else {
				*pFraction = *szScan - '0';
				++szScan;
				++(*pu4bMove);
				return TRUE;
			}
		}
	} while(*szScan >= '0' && *szScan <= '9');

	return TRUE;
}


//
//	Description:
//		Return TRUE if szStr is comment out with leading "//".
//
BOOLEAN
IsCommentString(
	IN		ps1Byte			szStr
)
{
	if(*szStr == '/' && *(szStr+1) == '/')
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

// <20121004, Kordan> For example, 
// ParseQualifiedString(inString, 0, outString, '[', ']') gets "Kordan" from a string "Hello [Kordan]".
// If RightQualifier does not exist, it will hang on in the while loop
BOOLEAN 
ParseQualifiedString(
    IN      ps1Byte 		In, 
    IN OUT  pu4Byte 		Start, 
    OUT     ps1Byte 		Out,
    OUT     const u4Byte 	MaxOutLen,    
    IN      s1Byte  		LeftQualifier, 
    IN      s1Byte  		RightQualifier
    )
{
    u4Byte  i = 0, j = 0;
	u4Byte  origin = *Start;
    s1Byte  c = In[(*Start)++];
	BOOLEAN status = TRUE;

    if (c != LeftQualifier)
        status =  FALSE;

    i = (*Start);

	while ((c = In[(*Start)++]) != RightQualifier) 
		; // Keep going until the RightQualifier is found.

	j = (*Start) - 2;

	if (j - i + 1 >= MaxOutLen)
	{
		status = FALSE;
	}
	else
	{
		//strncpy((char *)Out, (const char*)(In+i), j-i+1);
		//Prefast warning C28719: Banned API Usage: strncpy is a Banned API as listed in dontuse.h for security purposes
		strncpy_s((char *)Out, j - i + 1, (const char*)(In + i), j - i + 1);
	}

	if (status == FALSE)
		*Start = origin; // Reset the position
	
    return status;
}

BOOLEAN
GetU1ByteIntegerFromStringInDecimal(
	IN		ps1Byte Str,
	IN OUT	pu1Byte pInt
	)
{
	u2Byte i = 0;
	*pInt = 0;

	while ( Str[i] != '\0' )
	{
		if ( Str[i] >= '0' && Str[i] <= '9' )
		{
			*pInt *= 10;
			*pInt += ( Str[i] - '0' );
		}
		else
		{
			return FALSE;
		}
		++i;
	}

	return TRUE;
}


BOOLEAN
GetS1ByteIntegerFromStringInDecimal(
	IN		ps1Byte Str,
	IN OUT	ps1Byte pInt
	)
{
	u2Byte i = 0;
	s1Byte Sign = 1; // Positive
	
	*pInt = 0;
	
	if( Str[i] == '-' )
	{
		Sign = -1; // Negative
		i++;
	}

	while ( Str[i] != '\0' )
	{
		if ( Str[i] >= '0' && Str[i] <= '9' )
		{
			*pInt *= 10;
			*pInt += ( Str[i] - '0' );
		}
		else
		{
			return FALSE;
		}
		++i;
	}
	*pInt *= Sign; // Apply +/- sign

	return TRUE;
}

BOOLEAN
isAllSpaceOrTab(
	pu1Byte data,
	u1Byte	size
	)
{
	u1Byte		cnt = 0, NumOfSpaceAndTab = 0;
	while( size > cnt )
	{
		if ( data[cnt] == ' ' || data[cnt] == '\t' || data[cnt] == '\0' )
			++NumOfSpaceAndTab;

		++cnt;
	}

	return size == NumOfSpaceAndTab;
}

//
// Description:
//	Generate a unique tag for debug
//
// For example:
//
//	If pFunName is "abcde", we will generate tag by function length, first character and last character.
//
//	genTag will return "50ea"
//
//	Windbg will show "ae05"
//
u4Byte
GenTag(
	IN	char	*pFunName
	)
{
	u2Byte	units, tens = 0;
	u4Byte	tag;
	
	units = (u2Byte)strlen(pFunName);
	tag = pFunName[0] | (pFunName[units-1] << 8);
	
	while(units >= 10)
	{
		units -= 10;
		tens ++;
	}
	tag |= ((tens+0x30) << 16) | ((units+0x30) << 24);

	return tag;
}