2
0

Fix some build warnings.

This commit is contained in:
Maarten Billemont 2017-08-06 18:56:37 -04:00
parent 68d1ab58b7
commit 18daef7808
5 changed files with 72 additions and 92 deletions

View File

@ -57,7 +57,7 @@
#include "base64.h" #include "base64.h"
/* aaaack but it's fast and const should make it shared text page. */ /* aaaack but it's fast and const should make it shared text page. */
static const unsigned char pr2six[256] = static const unsigned char b64ToBits[256] =
{ {
/* ASCII table */ /* ASCII table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
@ -80,58 +80,39 @@ static const unsigned char pr2six[256] =
size_t mpw_base64_decode_max(const char *b64Text) { size_t mpw_base64_decode_max(const char *b64Text) {
register const uint8_t *bufin; register const uint8_t *b64Cursor = (uint8_t *)b64Text;
register int nprbytes; while (b64ToBits[*(b64Cursor++)] <= 63);
int b64Size = (int)(b64Cursor - (uint8_t *)b64Text) - 1;
bufin = (uint8_t *)b64Text; // Every 4 b64 chars yield 3 plain bytes => len = 3 * ceil(b64Size / 4)
while (pr2six[*(bufin++)] <= 63); return (size_t)(3 /*bytes*/ * ((b64Size + 4 /*chars*/ - 1) / 4 /*chars*/));
nprbytes = (int)(bufin - (uint8_t *)b64Text) - 1;
return (size_t)(((nprbytes + 3) / 4) * 3);
} }
int mpw_base64_decode(uint8_t *plainBuf, size_t plainMax, const char *b64Text) { int mpw_base64_decode(uint8_t *plainBuf, const char *b64Text) {
register const uint8_t *bufin; register const uint8_t *b64Cursor = (uint8_t *)b64Text;
register uint8_t *bufout; while (b64ToBits[*(b64Cursor++)] <= 63);
register int nprbytes; int b64Remaining = (int)(b64Cursor - (uint8_t *)b64Text) - 1;
bufin = (uint8_t *)b64Text; b64Cursor = (uint8_t *)b64Text;
while (pr2six[*(bufin++)] <= 63); register uint8_t *plainCursor = plainBuf;
nprbytes = (int)(bufin - (uint8_t *)b64Text) - 1; while (b64Remaining > 4) {
*(plainCursor++) = (b64ToBits[b64Cursor[0]] << 2 | b64ToBits[b64Cursor[1]] >> 4);
bufout = plainBuf; *(plainCursor++) = (b64ToBits[b64Cursor[1]] << 4 | b64ToBits[b64Cursor[2]] >> 2);
bufin = (uint8_t *)b64Text; *(plainCursor++) = (b64ToBits[b64Cursor[2]] << 6 | b64ToBits[b64Cursor[3]]);
b64Cursor += 4;
while (nprbytes > 4) { b64Remaining -= 4;
if (bufout + 2 >= plainBuf + plainMax)
return -1;
*(bufout++) = (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
*(bufout++) = (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
*(bufout++) = (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
bufin += 4;
nprbytes -= 4;
} }
/* Note: (nprbytes == 1) would be an error, so just ingore that case */ /* Note: (b64Size == 1) would be an error, so just ingore that case */
if (nprbytes > 1) { if (b64Remaining > 1)
if (bufout >= plainBuf + plainMax) *(plainCursor++) = (b64ToBits[b64Cursor[0]] << 2 | b64ToBits[b64Cursor[1]] >> 4);
return -1; if (b64Remaining > 2)
*(bufout++) = (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); *(plainCursor++) = (b64ToBits[b64Cursor[1]] << 4 | b64ToBits[b64Cursor[2]] >> 2);
} if (b64Remaining > 3)
if (nprbytes > 2) { *(plainCursor++) = (b64ToBits[b64Cursor[2]] << 6 | b64ToBits[b64Cursor[3]]);
if (bufout >= plainBuf + plainMax)
return -1;
*(bufout++) = (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
}
if (nprbytes > 3) {
if (bufout >= plainBuf + plainMax)
return -1;
*(bufout++) = (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
}
return (int)(bufout - plainBuf); return (int)(plainCursor - plainBuf);
} }
static const char basis_64[] = static const char basis_64[] =
@ -143,40 +124,32 @@ size_t mpw_base64_encode_max(size_t plainSize) {
return 4 /*chars*/ * (plainSize + 3 /*bytes*/ - 1) / 3 /*bytes*/; return 4 /*chars*/ * (plainSize + 3 /*bytes*/ - 1) / 3 /*bytes*/;
} }
int mpw_base64_encode(char *b64Text, size_t b64Max, const uint8_t *plainBuf, size_t plainSize) { int mpw_base64_encode(char *b64Text, const uint8_t *plainBuf, size_t plainSize) {
int i; int plainCursor = 0;
char *p; char *b64Cursor = b64Text;
for (; plainCursor < plainSize - 2; plainCursor += 3) {
p = b64Text; *b64Cursor++ = basis_64[((plainBuf[plainCursor] >> 2)) & 0x3F];
for (i = 0; i < plainSize - 2; i += 3) { *b64Cursor++ = basis_64[((plainBuf[plainCursor] & 0x3) << 4) |
if (p >= b64Text + b64Max) ((plainBuf[plainCursor + 1] & 0xF0) >> 4)];
return -1; *b64Cursor++ = basis_64[((plainBuf[plainCursor + 1] & 0xF) << 2) |
((plainBuf[plainCursor + 2] & 0xC0) >> 6)];
*p++ = basis_64[(plainBuf[i] >> 2) & 0x3F]; *b64Cursor++ = basis_64[plainBuf[plainCursor + 2] & 0x3F];
*p++ = basis_64[((plainBuf[i] & 0x3) << 4) |
((plainBuf[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((plainBuf[i + 1] & 0xF) << 2) |
((plainBuf[i + 2] & 0xC0) >> 6)];
*p++ = basis_64[plainBuf[i + 2] & 0x3F];
} }
if (i < plainSize) { if (plainCursor < plainSize) {
if (p + 3 >= b64Text + b64Max) *b64Cursor++ = basis_64[(plainBuf[plainCursor] >> 2) & 0x3F];
return -1; if (plainCursor == (plainSize - 1)) {
*b64Cursor++ = basis_64[((plainBuf[plainCursor] & 0x3) << 4)];
*p++ = basis_64[(plainBuf[i] >> 2) & 0x3F]; *b64Cursor++ = '=';
if (i == (plainSize - 1)) {
*p++ = basis_64[((plainBuf[i] & 0x3) << 4)];
*p++ = '=';
} }
else { else {
*p++ = basis_64[((plainBuf[i] & 0x3) << 4) | *b64Cursor++ = basis_64[((plainBuf[plainCursor] & 0x3) << 4) |
((plainBuf[i + 1] & 0xF0) >> 4)]; ((plainBuf[plainCursor + 1] & 0xF0) >> 4)];
*p++ = basis_64[((plainBuf[i + 1] & 0xF) << 2)]; *b64Cursor++ = basis_64[((plainBuf[plainCursor + 1] & 0xF) << 2)];
} }
*p++ = '='; *b64Cursor++ = '=';
} }
*p = '\0'; *b64Cursor = '\0';
return (int)(p - b64Text); return (int)(b64Cursor - b64Text);
} }

View File

@ -62,18 +62,17 @@
*/ */
size_t mpw_base64_decode_max(const char *b64Text); size_t mpw_base64_decode_max(const char *b64Text);
/** Decodes a base-64 encoded string into a plain byte buffer. /** Decodes a base-64 encoded string into a plain byte buffer.
* @param plainMax the maximum amount of bytes to write to plainBuf. * @param plainBuf a byte buffer, size should be at least mpw_base64_decode_max(b64Text)
* @return The amount of bytes that were written to plainBuf or -1 if this amount would have exceeded plainMax. * @return The amount of bytes that were written to plainBuf.
*/ */
int mpw_base64_decode(uint8_t *plainBuf, size_t plainMax, const char *b64Text); int mpw_base64_decode(uint8_t *plainBuf, const char *b64Text);
/** /**
* @return The amount of characters needed to encode a plainBuf of the given size as base-64 (excluding the terminating NUL). * @return The amount of characters needed to encode a plainBuf of the given size as base-64 (excluding the terminating NUL).
*/ */
size_t mpw_base64_encode_max(size_t plainSize); size_t mpw_base64_encode_max(size_t plainSize);
/** Encodes a plain byte buffer into a base-64 encoded string. /** Encodes a plain byte buffer into a base-64 encoded string.
* @param b64Max the maximum amount of characters to write to b64Text, excluding the terminating NUL. * @param b64Text a character buffer, size should be at least mpw_base64_encode_max(plainSize) + 1
* @return The amount of characters that were written to b64Text, excluding the terminating NUL * @return The amount of characters that were written to b64Text, excluding the terminating NUL.
* or -1 if this amount would have exceeded b64Max.
*/ */
int mpw_base64_encode(char *b64Text, size_t b64Max, const uint8_t *plainBuf, size_t plainSize); int mpw_base64_encode(char *b64Text, const uint8_t *plainBuf, size_t plainSize);

View File

@ -163,7 +163,7 @@ const char *mpw_encrypt_v0(
// Base64-encode // Base64-encode
size_t b64Max = mpw_base64_encode_max( bufSize ); size_t b64Max = mpw_base64_encode_max( bufSize );
char *cipherText = calloc( 1, b64Max + 1 ); char *cipherText = calloc( 1, b64Max + 1 );
if (mpw_base64_encode( cipherText, b64Max, cipherBuf, bufSize ) < 0) { if (mpw_base64_encode( cipherText, cipherBuf, bufSize ) < 0) {
err( "Base64 encoding error." ); err( "Base64 encoding error." );
mpw_free_string( cipherText ); mpw_free_string( cipherText );
cipherText = NULL; cipherText = NULL;
@ -178,14 +178,14 @@ const char *mpw_decrypt_v0(
MPMasterKey masterKey, const char *cipherText) { MPMasterKey masterKey, const char *cipherText) {
// Base64-decode // Base64-decode
size_t bufSize = mpw_base64_decode_max( cipherText ); uint8_t *cipherBuf = calloc( 1, mpw_base64_decode_max( cipherText ) );
uint8_t *cipherBuf = calloc( 1, bufSize ); size_t bufSize = (size_t)mpw_base64_decode( cipherBuf, cipherText );
if ((bufSize = (size_t)mpw_base64_decode( cipherBuf, bufSize, cipherText )) < 0) { if ((int)bufSize < 0) {
err( "Base64 decoding error." ); err( "Base64 decoding error." );
mpw_free( cipherBuf, mpw_base64_decode_max( cipherText ) ); mpw_free( cipherBuf, mpw_base64_decode_max( cipherText ) );
return NULL; return NULL;
} }
trc( "b64 decoded: %lu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) ); trc( "b64 decoded: %zu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Decrypt // Decrypt
const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, bufSize ); const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, bufSize );

View File

@ -91,7 +91,7 @@ bool mpw_push_int(uint8_t **const buffer, size_t *const bufferSize, const uint32
return mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) ); return mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) );
} }
bool mpw_realloc(void **buffer, size_t *bufferSize, const size_t deltaSize) { bool __mpw_realloc(void **buffer, size_t *bufferSize, const size_t deltaSize) {
if (!buffer) if (!buffer)
return false; return false;
@ -188,6 +188,10 @@ static uint8_t const *mpw_aes(bool encrypt, const uint8_t *key, const size_t key
uint8_t nonce[crypto_stream_NONCEBYTES]; uint8_t nonce[crypto_stream_NONCEBYTES];
bzero( (void *)nonce, sizeof( nonce ) ); bzero( (void *)nonce, sizeof( nonce ) );
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (encrypt) { if (encrypt) {
uint8_t *const cipherBuf = malloc( bufSize ); uint8_t *const cipherBuf = malloc( bufSize );
if (crypto_stream_aes128ctr_xor( cipherBuf, buf, bufSize, nonce, key ) != 0) { if (crypto_stream_aes128ctr_xor( cipherBuf, buf, bufSize, nonce, key ) != 0) {
@ -195,7 +199,8 @@ static uint8_t const *mpw_aes(bool encrypt, const uint8_t *key, const size_t key
return NULL; return NULL;
} }
return cipherBuf; return cipherBuf;
} else { }
else {
uint8_t *const plainBuf = malloc( bufSize ); uint8_t *const plainBuf = malloc( bufSize );
if (crypto_stream_aes128ctr( plainBuf, bufSize, nonce, key ) != 0) { if (crypto_stream_aes128ctr( plainBuf, bufSize, nonce, key ) != 0) {
mpw_free( plainBuf, bufSize ); mpw_free( plainBuf, bufSize );
@ -205,6 +210,8 @@ static uint8_t const *mpw_aes(bool encrypt, const uint8_t *key, const size_t key
plainBuf[c] = buf[c] ^ plainBuf[c]; plainBuf[c] = buf[c] ^ plainBuf[c];
return plainBuf; return plainBuf;
} }
#pragma clang diagnostic pop
#pragma GCC diagnostic pop
#else #else
#error No crypto support for mpw_aes. #error No crypto support for mpw_aes.
#endif #endif
@ -274,8 +281,8 @@ const char *mpw_hex(const void *buf, size_t length) {
mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10; mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10;
if (mpw_realloc( &mpw_hex_buf[mpw_hex_buf_i], NULL, length * 2 + 1 )) if (mpw_realloc( &mpw_hex_buf[mpw_hex_buf_i], NULL, length * 2 + 1 ))
for (size_t kH = 0; kH < length; kH++) for (size_t kH = 0; kH < length; kH++)
sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] ); sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] );
return mpw_hex_buf[mpw_hex_buf_i]; return mpw_hex_buf[mpw_hex_buf_i];
} }

View File

@ -121,8 +121,9 @@ bool mpw_push_int(
* @param deltaSize The amount to increase the buffer's size by. * @param deltaSize The amount to increase the buffer's size by.
* @return true if successful, false if reallocation failed. * @return true if successful, false if reallocation failed.
*/ */
bool mpw_realloc( #define mpw_realloc(buffer, bufferSize, deltaSize) \
void **buffer, size_t *bufferSize, const size_t deltaSize); __mpw_realloc( (void **)buffer, bufferSize, deltaSize )
bool __mpw_realloc(void **buffer, size_t *bufferSize, const size_t deltaSize);
/** Free a buffer after zero'ing its contents. */ /** Free a buffer after zero'ing its contents. */
bool mpw_free( bool mpw_free(
const void *buffer, const size_t bufferSize); const void *buffer, const size_t bufferSize);