Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "tx_api.h"
#include "nx_api.h"
_nx_utility_base64_array
...
...
_nx_utility_string_length_check(CHAR *, UINT *, UINT)
...
...
_nx_utility_string_to_uint(CHAR *, UINT, UINT *)
...
...
_nx_utility_uint_to_string(UINT, UINT, CHAR *, UINT)
...
...
_nx_utility_base64_encode(UCHAR *, UINT, UCHAR *, UINT, UINT *)
...
_nx_utility_base64_decode(UCHAR *, UINT, UCHAR *, UINT, UINT *)
Files
loading...
SourceVuSTM32 Libraries and Samplesnetxduocommon/src/nx_utility.c
 
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Component */ /** */ /** Utility */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "tx_api.h" #include "nx_api.h" /* Define the base64 letters. */ static CHAR _nx_utility_base64_array[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_utility_string_length_check PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function traverses the string and returns the string length, */ /* if the string is invalid or the string length is bigger than max */ /* string length, returns error. */ /* */ /* INPUT */ /* */ /* input_string Pointer to input string */ /* string_length Pointer to string length */ /* max_string_length Max string length */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_utility_string_length_check(CHAR *input_string, UINT *string_length, UINT max_string_length) { UINT i; /* Check for invalid input pointers. */ if (input_string == NX_NULL) { return(NX_PTR_ERROR); }if (input_string == NX_NULL) { ... } /* Traverse the string. */ for (i = 0; input_string[i]; i++) { /* Check if the string length is bigger than the max string length. */ if (i >= max_string_length) { return(NX_SIZE_ERROR); }if (i >= max_string_length) { ... } }for (i = 0; input_string[i]; i++) { ... } /* Return the string length if string_length is not NULL. String_length being NULL indicates the caller needs to check for string length within the max_string_length. *//* ... */ if (string_length) { *string_length = i; }if (string_length) { ... } /* Return success. */ return(NX_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_utility_string_to_uint PORTABLE C */ /* 6.1.3 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function converts the string to unsigned integer. */ /* */ /* INPUT */ /* */ /* input_string Pointer to input string */ /* string_length Length of input string */ /* number Pointer to the number */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 12-31-2020 Yuxin Zhou Initial Version 6.1.3 */ /* */... /**************************************************************************/ UINT _nx_utility_string_to_uint(CHAR *input_string, UINT string_length, UINT *number) { UINT i; /* Check for invalid input pointers. */ if ((input_string == NX_NULL) || (number == NX_NULL)) { return(NX_PTR_ERROR); }if ((input_string == NX_NULL) || (number == NX_NULL)) { ... } /* Check string length. */ if (string_length == 0) { return(NX_SIZE_ERROR); }if (string_length == 0) { ... } /* Initialize. */ i = 0; *number = 0; /* Traverse the string. */ while (i < string_length) { /* Is a numeric character present? */ if ((input_string[i] >= '0') && (input_string[i] <= '9')) { /* Check overflow. Max Value: Hex:0xFFFFFFFF, Decimal: 4294967295. */ if (((*number == 429496729) && (input_string[i] > '5')) || (*number >= 429496730)) { return(NX_OVERFLOW); }if (((*number == 429496729) && (input_string[i] > '5')) || (*number >= 429496730)) { ... } /* Yes, numeric character is present. Update the number. */ *number = (*number * 10) + (UINT) (input_string[i] - '0'); }if ((input_string[i] >= '0') && (input_string[i] <= '9')) { ... } else { return(NX_INVALID_PARAMETERS); }else { ... } i++; }while (i < string_length) { ... } /* Return success. */ return(NX_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_utility_uint_to_string PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function converts the unsigned integer to string. */ /* */ /* INPUT */ /* */ /* number Input number */ /* base Base of the conversion */ /* 8 for OCT */ /* 10 for DEC */ /* 16 for HEX */ /* string_buffer Pointer to string buffer */ /* string_buffer_size Size of string buffer */ /* */ /* OUTPUT */ /* */ /* size The size of output string */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 08-02-2021 Yuxin Zhou Initial Version 6.1.8 */ /* 10-15-2021 Yuxin Zhou Modified comment(s), */ /* checked invalid input value,*/ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ UINT _nx_utility_uint_to_string(UINT number, UINT base, CHAR *string_buffer, UINT string_buffer_size) { UINT i; UINT digit; UINT size; /* Check for invalid input pointers. */ if ((string_buffer == NX_NULL) || (string_buffer_size == 0) || (base == 0)) { return(0); }if ((string_buffer == NX_NULL) || (string_buffer_size == 0) || (base == 0)) { ... } /* Initialize. */ i = 0; size = 0; /* Loop to convert the number to ASCII. Minus 1 to put NULL terminal. */ while (size < string_buffer_size - 1) { /* Shift the current digits over one. */ for (i = size; i != 0; i--) { /* Move each digit over one place. */ string_buffer[i] = string_buffer[i-1]; }for (i = size; i != 0; i--) { ... } /* Compute the next decimal digit. */ digit = number % base; /* Update the input number. */ number = number / base; /* Store the new digit in ASCII form. */ if (digit < 10) { string_buffer[0] = (CHAR) (digit + '0'); }if (digit < 10) { ... } else { string_buffer[0] = (CHAR) (digit + 'a' - 0xa); }else { ... } /* Increment the size. */ size++; /* Determine if the number is now zero. */ if (number == 0) break; }while (size < string_buffer_size - 1) { ... } /* Determine if there is an overflow error. */ if (number) { /* Error, return bad values to user. */ size = 0; }if (number) { ... } /* Make the string NULL terminated. */ string_buffer[size] = (CHAR) NX_NULL; /* Return size to caller. */ return(size); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_utility_base64_encode PORTABLE C */ /* 6.1.6 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function encodes the input string into a base64 */ /* representation. */ /* */ /* INPUT */ /* */ /* name Name string */ /* name_size Size of name */ /* base64name Encoded base64 name string */ /* base64name_size Size of encoded base64 name */ /* bytes_copied Number of bytes copied */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 04-02-2021 Yuxin Zhou Initial Version 6.1.6 */ /* */... /**************************************************************************/ UINT _nx_utility_base64_encode(UCHAR *name, UINT name_size, UCHAR *base64name, UINT base64name_size, UINT *bytes_copied) { UINT pad; UINT i, j; UINT step; /* Check for invalid input pointers. */ if ((name == NX_NULL) || (base64name == NX_NULL) || (bytes_copied == NX_NULL)) { return(NX_PTR_ERROR); }if ((name == NX_NULL) || (base64name == NX_NULL) || (bytes_copied == NX_NULL)) { ... } /* Check the size. */ if ((name_size == 0) || (base64name_size == 0)) { return(NX_SIZE_ERROR); }if ((name_size == 0) || (base64name_size == 0)) { ... } /* Adjust the length to represent the base64 name. */ name_size = ((name_size * 8) / 6); /* Default padding to none. */ pad = 0; /* Determine if an extra conversion is needed. */ if ((name_size * 6) % 24) { /* Some padding is needed. */ /* Calculate the number of pad characters. */ pad = (name_size * 6) % 24; pad = (24 - pad) / 6; pad = pad - 1; /* Adjust the length to pickup the character fraction. */ name_size++; }if ((name_size * 6) % 24) { ... } /* Check the buffer size. */ if (base64name_size <= (name_size + pad)) { return(NX_SIZE_ERROR); }if (base64name_size <= (name_size + pad)) { ... } /* Setup index into the base64name. */ j = 0; /* Compute the base64name. */ step = 0; i = 0; while (j < name_size) { /* Determine which step we are in. */ if (step == 0) { /* Use first 6 bits of name character for index. */ base64name[j++] = (UCHAR)_nx_utility_base64_array[((UCHAR)name[i]) >> 2]; step++; }if (step == 0) { ... } else if (step == 1) { /* Use last 2 bits of name character and first 4 bits of next name character for index. */ base64name[j++] = (UCHAR)_nx_utility_base64_array[((((UCHAR)name[i]) & 0x3) << 4) | (((UCHAR)name[i + 1]) >> 4)]; i++; step++; }else if (step == 1) { ... } else if (step == 2) { /* Use last 4 bits of name character and first 2 bits of next name character for index. */ base64name[j++] = (UCHAR)_nx_utility_base64_array[((((UCHAR)name[i]) & 0xF) << 2) | (((UCHAR)name[i + 1]) >> 6)]; i++; step++; }else if (step == 2) { ... } else /* Step 3 */ { /* Use last 6 bits of name character for index. */ base64name[j++] = (UCHAR)_nx_utility_base64_array[(((UCHAR)name[i]) & 0x3F)]; i++; step = 0; }else { ... } }while (j < name_size) { ... } /* Determine if the index needs to be advanced. */ if (step != 3) { i++; }if (step != 3) { ... } /* Now add the PAD characters. */ while (pad--) { /* Pad base64name with '=' characters. */ base64name[j++] = '='; }while (pad--) { ... } /* Put a NULL character in. */ base64name[j] = NX_NULL; *bytes_copied = j; return(NX_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_utility_base64_decode PORTABLE C */ /* 6.1.10 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function decodes the input base64 ASCII string and converts */ /* it into a standard ASCII representation. */ /* */ /* INPUT */ /* */ /* base64name Encoded base64 name string */ /* base64name_size Size of encoded base64 name */ /* name Name string */ /* name_size Size of name */ /* bytes_copied Number of bytes copied */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 04-02-2021 Yuxin Zhou Initial Version 6.1.6 */ /* 10-15-2021 Yuxin Zhou Modified comment(s), */ /* removed useless condition, */ /* resulting in version 6.1.9 */ /* 01-31-2022 Yuxin Zhou Modified comment(s), */ /* fixed the issue of reading */ /* overflow, */ /* resulting in version 6.1.10 */ /* */... /**************************************************************************/ UINT _nx_utility_base64_decode(UCHAR *base64name, UINT base64name_size, UCHAR *name, UINT name_size, UINT *bytes_copied) { UINT i, j; UINT value1, value2; UINT step; UINT source_size = base64name_size; /* Check for invalid input pointers. */ if ((base64name == NX_NULL) || (name == NX_NULL) || (bytes_copied == NX_NULL)) { return(NX_PTR_ERROR); }if ((base64name == NX_NULL) || (name == NX_NULL) || (bytes_copied == NX_NULL)) { ... } /* Check the size. */ if ((base64name_size == 0) || (name_size == 0)) { return(NX_SIZE_ERROR); }if ((base64name_size == 0) || (name_size == 0)) { ... } /* Adjust the length to represent the ASCII name. */ base64name_size = ((base64name_size * 6) / 8); if ((base64name_size) && (base64name[source_size - 1] == '=')) { base64name_size--; if ((base64name_size) && (base64name[source_size - 2] == '=')) { base64name_size--; }if ((base64name_size) && (base64name[source_size - 2] == '=')) { ... } }if ((base64name_size) && (base64name[source_size - 1] == '=')) { ... } /* Check the buffer size. */ if (name_size <= base64name_size) { return(NX_SIZE_ERROR); }if (name_size <= base64name_size) { ... } /* Setup index into the ASCII name. */ j = 0; /* Compute the ASCII name. */ step = 0; i = 0; while ((j < base64name_size) && (base64name[i]) && (base64name[i] != '=')) { /* Derive values of the Base64 name. */ if ((base64name[i] >= 'A') && (base64name[i] <= 'Z')) value1 = (UINT) (base64name[i] - 'A'); else if ((base64name[i] >= 'a') && (base64name[i] <= 'z')) value1 = (UINT) (base64name[i] - 'a') + 26; else if ((base64name[i] >= '0') && (base64name[i] <= '9')) value1 = (UINT) (base64name[i] - '0') + 52; else if ((base64name[i] == '+') || (base64name[i] == '-')) /* Base64 URL. */ value1 = 62; else if ((base64name[i] == '/') || (base64name[i] == '_')) /* Base64 URL. */ value1 = 63; else value1 = 0; /* Derive value for the next character. */ if ((base64name[i + 1] >= 'A') && (base64name[i + 1] <= 'Z')) value2 = (UINT) (base64name[i+1] - 'A'); else if ((base64name[i + 1] >= 'a') && (base64name[i + 1] <= 'z')) value2 = (UINT) (base64name[i+1] - 'a') + 26; else if ((base64name[i + 1] >= '0') && (base64name[i + 1] <= '9')) value2 = (UINT) (base64name[i+1] - '0') + 52; else if ((base64name[i + 1] == '+') || (base64name[i + 1] == '-')) /* Base64 URL. */ value2 = 62; else if ((base64name[i + 1] == '/') || (base64name[i + 1] == '_')) /* Base64 URL. */ value2 = 63; else value2 = 0; /* Determine which step we are in. */ if (step == 0) { /* Use first value and first 2 bits of second value. */ name[j++] = (UCHAR) (((value1 & 0x3f) << 2) | ((value2 >> 4) & 3)); i++; step++; }if (step == 0) { ... } else if (step == 1) { /* Use last 4 bits of first value and first 4 bits of next value. */ name[j++] = (UCHAR) (((value1 & 0xF) << 4) | (value2 >> 2)); i++; step++; }else if (step == 1) { ... } else { /* Use first 2 bits and following 6 bits of next value. */ name[j++] = (UCHAR) (((value1 & 3) << 6) | (value2 & 0x3f)); i++; i++; step = 0; }else { ... } }while ((j < base64name_size) && (base64name[i]) && (base64name[i] != '=')) { ... } /* Put a NULL character in. */ name[j] = NX_NULL; *bytes_copied = j; return(NX_SUCCESS); }{ ... }
Details