42
42
43
43
#include <bson/bson.h>
44
44
#include <common-b64-private.h>
45
+ #include <mlib/loop.h>
45
46
46
47
#define Assert (Cond ) \
47
48
if (!(Cond)) \
@@ -118,21 +119,21 @@ mcommon_b64_ntop (uint8_t const *src, size_t srclength, char *target, size_t tar
118
119
size_t datalength = 0 ;
119
120
uint8_t input [3 ];
120
121
uint8_t output [4 ];
121
- size_t i ;
122
122
123
123
if (!target ) {
124
124
return -1 ;
125
125
}
126
126
127
- while (2 < srclength ) {
127
+ // While we have at least three chars to read:
128
+ while (srclength > 2 ) {
128
129
input [0 ] = * src ++ ;
129
130
input [1 ] = * src ++ ;
130
131
input [2 ] = * src ++ ;
131
132
srclength -= 3 ;
132
133
133
134
output [0 ] = input [0 ] >> 2 ;
134
- output [1 ] = (( input [0 ] & 0x03 ) << 4 ) + (input [1 ] >> 4 );
135
- output [2 ] = (( input [1 ] & 0x0f ) << 2 ) + (input [2 ] >> 6 );
135
+ output [1 ] = (uint8_t ) ((( input [0 ] & 0x03 ) << 4 ) + (input [1 ] >> 4 ) );
136
+ output [2 ] = (uint8_t ) ((( input [1 ] & 0x0f ) << 2 ) + (input [2 ] >> 6 ) );
136
137
output [3 ] = input [2 ] & 0x3f ;
137
138
Assert (output [0 ] < 64 );
138
139
Assert (output [1 ] < 64 );
@@ -153,12 +154,10 @@ mcommon_b64_ntop (uint8_t const *src, size_t srclength, char *target, size_t tar
153
154
/* Get what's left. */
154
155
input [0 ] = input [1 ] = input [2 ] = '\0' ;
155
156
156
- for (i = 0 ; i < srclength ; i ++ ) {
157
- input [i ] = * src ++ ;
158
- }
157
+ memcpy (input , src , srclength );
159
158
output [0 ] = input [0 ] >> 2 ;
160
- output [1 ] = (( input [0 ] & 0x03 ) << 4 ) + (input [1 ] >> 4 );
161
- output [2 ] = (( input [1 ] & 0x0f ) << 2 ) + (input [2 ] >> 6 );
159
+ output [1 ] = (uint8_t ) ((( input [0 ] & 0x03 ) << 4 ) + (input [1 ] >> 4 ) );
160
+ output [2 ] = (uint8_t ) ((( input [1 ] & 0x0f ) << 2 ) + (input [2 ] >> 6 ) );
162
161
Assert (output [0 ] < 64 );
163
162
Assert (output [1 ] < 64 );
164
163
Assert (output [2 ] < 64 );
@@ -277,27 +276,24 @@ static const uint8_t mongoc_b64rmap_invalid = 0xff;
277
276
278
277
static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap )
279
278
{
280
- int i ;
281
- unsigned char ch ;
282
-
283
279
/* Null: end of string, stop parsing */
284
280
mongoc_b64rmap [0 ] = mongoc_b64rmap_end ;
285
281
286
- for (i = 1 ; i < 256 ; ++ i ) {
287
- ch = (unsigned char ) i ;
282
+ mlib_foreach_urange (i , 1 , 256 ) {
283
+ const uint8_t ch = (uint8_t ) i ;
288
284
/* Whitespaces */
289
285
if (bson_isspace (ch ))
290
- mongoc_b64rmap [i ] = mongoc_b64rmap_space ;
286
+ mongoc_b64rmap [ch ] = mongoc_b64rmap_space ;
291
287
/* Padding: stop parsing */
292
288
else if (ch == Pad64 )
293
- mongoc_b64rmap [i ] = mongoc_b64rmap_end ;
289
+ mongoc_b64rmap [ch ] = mongoc_b64rmap_end ;
294
290
/* Non-base64 char */
295
291
else
296
- mongoc_b64rmap [i ] = mongoc_b64rmap_invalid ;
292
+ mongoc_b64rmap [ch ] = mongoc_b64rmap_invalid ;
297
293
}
298
294
299
295
/* Fill reverse mapping for base64 chars */
300
- for (i = 0 ; Base64 [i ] != '\0' ; ++ i )
296
+ for (uint8_t i = 0 ; Base64 [i ] != '\0' ; ++ i )
301
297
mongoc_b64rmap [(uint8_t ) Base64 [i ]] = i ;
302
298
303
299
MONGOC_COMMON_ONCE_RETURN ;
@@ -313,7 +309,7 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
313
309
tarindex = 0 ;
314
310
315
311
while (1 ) {
316
- ch = * src ++ ;
312
+ ch = ( uint8_t ) * src ++ ;
317
313
ofs = mongoc_b64rmap [ch ];
318
314
319
315
if (ofs >= mongoc_b64rmap_special ) {
@@ -367,22 +363,22 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
367
363
* on a byte boundary, and/or with erroneous trailing characters.
368
364
*/
369
365
370
- if (ch == Pad64 ) { /* We got a pad char. */
371
- ch = * src ++ ; /* Skip it, get next. */
366
+ if (ch == Pad64 ) { /* We got a pad char. */
367
+ ch = ( uint8_t ) * src ++ ; /* Skip it, get next. */
372
368
switch (state ) {
373
369
case 0 : /* Invalid = in first position */
374
370
case 1 : /* Invalid = in second position */
375
371
return (-1 );
376
372
377
373
case 2 : /* Valid, means one byte of info */
378
374
/* Skip any number of spaces. */
379
- for ((void ) NULL ; ch != '\0' ; ch = * src ++ )
375
+ for ((void ) NULL ; ch != '\0' ; ch = ( uint8_t ) * src ++ )
380
376
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
381
377
break ;
382
378
/* Make sure there is another trailing = sign. */
383
379
if (ch != Pad64 )
384
380
return (-1 );
385
- ch = * src ++ ; /* Skip the = */
381
+ ch = ( uint8_t ) * src ++ ; /* Skip the = */
386
382
/* Fall through to "single trailing =" case. */
387
383
/* FALLTHROUGH */
388
384
@@ -391,7 +387,7 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
391
387
* We know this char is an =. Is there anything but
392
388
* whitespace after it?
393
389
*/
394
- for ((void ) NULL ; ch != '\0' ; ch = * src ++ )
390
+ for ((void ) NULL ; ch != '\0' ; ch = ( uint8_t ) * src ++ )
395
391
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
396
392
return (-1 );
397
393
@@ -422,14 +418,13 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
422
418
static int
423
419
mongoc_b64_pton_len (char const * src )
424
420
{
425
- int tarindex , state ;
426
- uint8_t ch , ofs ;
427
-
428
- state = 0 ;
429
- tarindex = 0 ;
421
+ uint8_t ch = 0 ;
422
+ uint8_t ofs = 0 ;
423
+ int state = 0 ;
424
+ int tarindex = 0 ;
430
425
431
426
while (1 ) {
432
- ch = * src ++ ;
427
+ ch = ( uint8_t ) * src ++ ;
433
428
ofs = mongoc_b64rmap [ch ];
434
429
435
430
if (ofs >= mongoc_b64rmap_special ) {
@@ -469,22 +464,22 @@ mongoc_b64_pton_len (char const *src)
469
464
* on a byte boundary, and/or with erroneous trailing characters.
470
465
*/
471
466
472
- if (ch == Pad64 ) { /* We got a pad char. */
473
- ch = * src ++ ; /* Skip it, get next. */
467
+ if (ch == Pad64 ) { /* We got a pad char. */
468
+ ch = ( uint8_t ) * src ++ ; /* Skip it, get next. */
474
469
switch (state ) {
475
470
case 0 : /* Invalid = in first position */
476
471
case 1 : /* Invalid = in second position */
477
472
return (-1 );
478
473
479
474
case 2 : /* Valid, means one byte of info */
480
475
/* Skip any number of spaces. */
481
- for ((void ) NULL ; ch != '\0' ; ch = * src ++ )
476
+ for ((void ) NULL ; ch != '\0' ; ch = ( uint8_t ) * src ++ )
482
477
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
483
478
break ;
484
479
/* Make sure there is another trailing = sign. */
485
480
if (ch != Pad64 )
486
481
return (-1 );
487
- ch = * src ++ ; /* Skip the = */
482
+ ch = ( uint8_t ) * src ++ ; /* Skip the = */
488
483
/* Fall through to "single trailing =" case. */
489
484
/* FALLTHROUGH */
490
485
@@ -493,7 +488,7 @@ mongoc_b64_pton_len (char const *src)
493
488
* We know this char is an =. Is there anything but
494
489
* whitespace after it?
495
490
*/
496
- for (( void ) NULL ; ch != '\0' ; ch = * src ++ )
491
+ for (; ch != '\0' ; ch = ( uint8_t ) * src ++ )
497
492
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
498
493
return (-1 );
499
494
0 commit comments