Change log level at g_log_remove_handler
[platform/upstream/glib.git] / glib / gbase64.c
index 651840e..3c427f8 100644 (file)
@@ -3,6 +3,8 @@
  *  Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
  *  Copyright (C) 2000-2003 Ximian Inc.
  *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
@@ -76,10 +78,10 @@ static const char base64_alphabet[] =
  * be written to it. Due to the way base64 encodes you will need
  * at least: (@len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of
  * non-zero state). If you enable line-breaking you will need at least:
- * ((@len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra space.
+ * ((@len / 3 + 1) * 4 + 4) / 76 + 1 bytes of extra space.
  *
  * @break_lines is typically used when putting base64-encoded data in emails.
- * It breaks the lines at 72 columns instead of putting all of the text on
+ * It breaks the lines at 76 columns instead of putting all of the text on
  * the same line. This avoids problems with long lines in the email system.
  * Note however that it breaks the lines with `LF` characters, not
  * `CR LF` sequences, so the result cannot be passed directly to SMTP
@@ -100,12 +102,12 @@ g_base64_encode_step (const guchar *in,
   char *outptr;
   const guchar *inptr;
 
-  g_return_val_if_fail (in != NULL, 0);
+  g_return_val_if_fail (in != NULL || len == 0, 0);
   g_return_val_if_fail (out != NULL, 0);
   g_return_val_if_fail (state != NULL, 0);
   g_return_val_if_fail (save != NULL, 0);
 
-  if (len <= 0)
+  if (len == 0)
     return 0;
 
   inptr = in;
@@ -160,7 +162,8 @@ g_base64_encode_step (const guchar *in,
       *state = already;
     }
 
-  if (len>0)
+  g_assert (len == 0 || len == 1 || len == 2);
+
     {
       char *saveout;
 
@@ -170,8 +173,11 @@ g_base64_encode_step (const guchar *in,
       /* len can only be 0 1 or 2 */
       switch(len)
         {
-        case 2: *saveout++ = *inptr++;
-        case 1: *saveout++ = *inptr++;
+        case 2:
+          *saveout++ = *inptr++;
+          G_GNUC_FALLTHROUGH;
+        case 1:
+          *saveout++ = *inptr++;
         }
       ((char *)save)[0] += len;
     }
@@ -241,7 +247,7 @@ g_base64_encode_close (gboolean  break_lines,
 
 /**
  * g_base64_encode:
- * @data: (array length=len) (element-type guint8): the binary data to encode
+ * @data: (array length=len) (element-type guint8) (nullable): the binary data to encode
  * @len: the length of @data
  *
  * Encode a sequence of binary data into its Base-64 stringified
@@ -265,9 +271,7 @@ g_base64_encode (const guchar *data,
 
   /* We can use a smaller limit here, since we know the saved state is 0,
      +1 is needed for trailing \0, also check for unlikely integer overflow */
-  if (len >= ((G_MAXSIZE - 1) / 4 - 1) * 3)
-    g_error("%s: input too large for Base64 encoding (%"G_GSIZE_FORMAT" chars)",
-        G_STRLOC, len);
+  g_return_val_if_fail (len < ((G_MAXSIZE - 1) / 4 - 1) * 3, NULL);
 
   out = g_malloc ((len / 3 + 1) * 4 + 1);
 
@@ -298,10 +302,10 @@ static const unsigned char mime_base64_rank[256] = {
 };
 
 /**
- * g_base64_decode_step:
+ * g_base64_decode_step: (skip)
  * @in: (array length=len) (element-type guint8): binary input data
  * @len: max length of @in data to decode
- * @out: (out) (array) (element-type guint8): output buffer
+ * @out: (out caller-allocates) (array) (element-type guint8): output buffer
  * @state: (inout): Saved state between steps, initialize to 0
  * @save: (inout): Saved state between steps, initialize to 0
  *
@@ -333,12 +337,12 @@ g_base64_decode_step (const gchar  *in,
   unsigned int v;
   int i;
 
-  g_return_val_if_fail (in != NULL, 0);
+  g_return_val_if_fail (in != NULL || len == 0, 0);
   g_return_val_if_fail (out != NULL, 0);
   g_return_val_if_fail (state != NULL, 0);
   g_return_val_if_fail (save != NULL, 0);
 
-  if (len <= 0)
+  if (len == 0)
     return 0;
 
   inend = (const guchar *)in+len;
@@ -389,7 +393,7 @@ g_base64_decode_step (const gchar  *in,
 
 /**
  * g_base64_decode:
- * @text: zero-terminated string with base64 text to decode
+ * @text: (not nullable): zero-terminated string with base64 text to decode
  * @out_len: (out): The length of the decoded data is written here
  *
  * Decode a sequence of Base-64 encoded text into binary data.  Note