]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: base64 - Use unsigned size type for base64_get_full_encoded_size().
authorStephan Bosch <stephan.bosch@open-xchange.com>
Wed, 4 Sep 2019 20:23:25 +0000 (22:23 +0200)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Tue, 10 Sep 2019 07:02:40 +0000 (10:02 +0300)
Makes more sense.

src/lib/base64.c
src/lib/base64.h

index f0baf6578bab4cee64d55d1bd362f2a05636efba..a66446f4f0c2f40cf07d9b449712c200955b8158 100644 (file)
@@ -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;
 }
index 082b76e45bc441549decf073a7d1001eaee9eed0..2d0712ed6260f1ba02dc7882418f917782ac28f3 100644 (file)
@@ -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. */