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,11 @@ 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 );
158
+ src += srclength ;
159
159
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 );
160
+ output [1 ] = (uint8_t ) ((( input [0 ] & 0x03 ) << 4 ) + (input [1 ] >> 4 ) );
161
+ output [2 ] = (uint8_t ) ((( input [1 ] & 0x0f ) << 2 ) + (input [2 ] >> 6 ) );
162
162
Assert (output [0 ] < 64 );
163
163
Assert (output [1 ] < 64 );
164
164
Assert (output [2 ] < 64 );
@@ -277,27 +277,24 @@ static const uint8_t mongoc_b64rmap_invalid = 0xff;
277
277
278
278
static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap )
279
279
{
280
- int i ;
281
- unsigned char ch ;
282
-
283
280
/* Null: end of string, stop parsing */
284
281
mongoc_b64rmap [0 ] = mongoc_b64rmap_end ;
285
282
286
- for (i = 1 ; i < 256 ; ++ i ) {
287
- ch = (unsigned char ) i ;
283
+ mlib_foreach_urange (i , 1 , 256 ) {
284
+ const uint8_t ch = (uint8_t ) i ;
288
285
/* Whitespaces */
289
286
if (bson_isspace (ch ))
290
- mongoc_b64rmap [i ] = mongoc_b64rmap_space ;
287
+ mongoc_b64rmap [ch ] = mongoc_b64rmap_space ;
291
288
/* Padding: stop parsing */
292
289
else if (ch == Pad64 )
293
- mongoc_b64rmap [i ] = mongoc_b64rmap_end ;
290
+ mongoc_b64rmap [ch ] = mongoc_b64rmap_end ;
294
291
/* Non-base64 char */
295
292
else
296
- mongoc_b64rmap [i ] = mongoc_b64rmap_invalid ;
293
+ mongoc_b64rmap [ch ] = mongoc_b64rmap_invalid ;
297
294
}
298
295
299
296
/* Fill reverse mapping for base64 chars */
300
- for (i = 0 ; Base64 [i ] != '\0' ; ++ i )
297
+ for (uint8_t i = 0 ; Base64 [i ] != '\0' ; ++ i )
301
298
mongoc_b64rmap [(uint8_t ) Base64 [i ]] = i ;
302
299
303
300
MONGOC_COMMON_ONCE_RETURN ;
@@ -313,7 +310,7 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
313
310
tarindex = 0 ;
314
311
315
312
while (1 ) {
316
- ch = * src ++ ;
313
+ ch = ( uint8_t ) * src ++ ;
317
314
ofs = mongoc_b64rmap [ch ];
318
315
319
316
if (ofs >= mongoc_b64rmap_special ) {
@@ -367,22 +364,22 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
367
364
* on a byte boundary, and/or with erroneous trailing characters.
368
365
*/
369
366
370
- if (ch == Pad64 ) { /* We got a pad char. */
371
- ch = * src ++ ; /* Skip it, get next. */
367
+ if (ch == Pad64 ) { /* We got a pad char. */
368
+ ch = ( uint8_t ) * src ++ ; /* Skip it, get next. */
372
369
switch (state ) {
373
370
case 0 : /* Invalid = in first position */
374
371
case 1 : /* Invalid = in second position */
375
372
return (-1 );
376
373
377
374
case 2 : /* Valid, means one byte of info */
378
375
/* Skip any number of spaces. */
379
- for ((void ) NULL ; ch != '\0' ; ch = * src ++ )
376
+ for ((void ) NULL ; ch != '\0' ; ch = ( uint8_t ) * src ++ )
380
377
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
381
378
break ;
382
379
/* Make sure there is another trailing = sign. */
383
380
if (ch != Pad64 )
384
381
return (-1 );
385
- ch = * src ++ ; /* Skip the = */
382
+ ch = ( uint8_t ) * src ++ ; /* Skip the = */
386
383
/* Fall through to "single trailing =" case. */
387
384
/* FALLTHROUGH */
388
385
@@ -391,7 +388,7 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
391
388
* We know this char is an =. Is there anything but
392
389
* whitespace after it?
393
390
*/
394
- for ((void ) NULL ; ch != '\0' ; ch = * src ++ )
391
+ for ((void ) NULL ; ch != '\0' ; ch = ( uint8_t ) * src ++ )
395
392
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
396
393
return (-1 );
397
394
@@ -422,14 +419,13 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
422
419
static int
423
420
mongoc_b64_pton_len (char const * src )
424
421
{
425
- int tarindex , state ;
426
- uint8_t ch , ofs ;
427
-
428
- state = 0 ;
429
- tarindex = 0 ;
422
+ uint8_t ch = 0 ;
423
+ uint8_t ofs = 0 ;
424
+ int state = 0 ;
425
+ int tarindex = 0 ;
430
426
431
427
while (1 ) {
432
- ch = * src ++ ;
428
+ ch = ( uint8_t ) * src ++ ;
433
429
ofs = mongoc_b64rmap [ch ];
434
430
435
431
if (ofs >= mongoc_b64rmap_special ) {
@@ -469,22 +465,22 @@ mongoc_b64_pton_len (char const *src)
469
465
* on a byte boundary, and/or with erroneous trailing characters.
470
466
*/
471
467
472
- if (ch == Pad64 ) { /* We got a pad char. */
473
- ch = * src ++ ; /* Skip it, get next. */
468
+ if (ch == Pad64 ) { /* We got a pad char. */
469
+ ch = ( uint8_t ) * src ++ ; /* Skip it, get next. */
474
470
switch (state ) {
475
471
case 0 : /* Invalid = in first position */
476
472
case 1 : /* Invalid = in second position */
477
473
return (-1 );
478
474
479
475
case 2 : /* Valid, means one byte of info */
480
476
/* Skip any number of spaces. */
481
- for ((void ) NULL ; ch != '\0' ; ch = * src ++ )
477
+ for ((void ) NULL ; ch != '\0' ; ch = ( uint8_t ) * src ++ )
482
478
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
483
479
break ;
484
480
/* Make sure there is another trailing = sign. */
485
481
if (ch != Pad64 )
486
482
return (-1 );
487
- ch = * src ++ ; /* Skip the = */
483
+ ch = ( uint8_t ) * src ++ ; /* Skip the = */
488
484
/* Fall through to "single trailing =" case. */
489
485
/* FALLTHROUGH */
490
486
@@ -493,7 +489,7 @@ mongoc_b64_pton_len (char const *src)
493
489
* We know this char is an =. Is there anything but
494
490
* whitespace after it?
495
491
*/
496
- for (( void ) NULL ; ch != '\0' ; ch = * src ++ )
492
+ for (; ch != '\0' ; ch = ( uint8_t ) * src ++ )
497
493
if (mongoc_b64rmap [ch ] != mongoc_b64rmap_space )
498
494
return (-1 );
499
495
0 commit comments