From: Stephan Bosch Date: Wed, 4 Sep 2019 20:23:25 +0000 (+0200) Subject: lib: base64 - Use unsigned size type for base64_get_full_encoded_size(). X-Git-Tag: 2.3.8~98 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=11ad5161cbebcf9fd6667b52ae22247c1550b590;p=thirdparty%2Fdovecot%2Fcore.git lib: base64 - Use unsigned size type for base64_get_full_encoded_size(). Makes more sense. --- diff --git a/src/lib/base64.c b/src/lib/base64.c index f0baf6578b..a66446f4f0 100644 --- a/src/lib/base64.c +++ b/src/lib/base64.c @@ -8,13 +8,13 @@ * Low-level Base64 encoder */ -off_t base64_get_full_encoded_size(struct base64_encoder *enc, off_t src_size) +uoff_t base64_get_full_encoded_size(struct base64_encoder *enc, uoff_t src_size) { bool crlf = HAS_ALL_BITS(enc->flags, BASE64_ENCODE_FLAG_CRLF); bool no_padding = HAS_ALL_BITS(enc->flags, BASE64_ENCODE_FLAG_NO_PADDING); - off_t out_size; - off_t newlines; + uoff_t out_size; + uoff_t newlines; if (src_size == 0) return 0; @@ -26,23 +26,27 @@ off_t base64_get_full_encoded_size(struct base64_encoder *enc, off_t src_size) case 0: break; case 1: + i_assert(out_size > 2); out_size -= 2; break; case 2: + i_assert(out_size > 1); out_size -= 1; break; } } - /* newline between each full line */ - newlines = (out_size / enc->max_line_len) - 1; - /* an extra newline to separate the partial last line from the previous - full line */ - if ((out_size % enc->max_line_len) != 0) - newlines++; + if (out_size > enc->max_line_len) { + /* newline between each full line */ + newlines = (out_size / enc->max_line_len) - 1; + /* an extra newline to separate the partial last line from the + previous full line */ + if ((out_size % enc->max_line_len) != 0) + newlines++; - /* update size with added newlines */ - out_size += newlines * (crlf ? 2 : 1); + /* update size with added newlines */ + out_size += newlines * (crlf ? 2 : 1); + } return out_size; } diff --git a/src/lib/base64.h b/src/lib/base64.h index 082b76e45b..2d0712ed62 100644 --- a/src/lib/base64.h +++ b/src/lib/base64.h @@ -80,7 +80,8 @@ base64_encode_reset(struct base64_encoder *enc) /* Translate the size of the full encoder input to the size of the encoder output. */ -off_t base64_get_full_encoded_size(struct base64_encoder *enc, off_t src_size); +uoff_t base64_get_full_encoded_size(struct base64_encoder *enc, + uoff_t src_size); /* Translate the size of the next input to the size of the output once encoded. This yields the amount of data appended to the dest buffer by base64_encode_more() with the indicated src_size. */