From 5be04ec2ceb1df77afbca4fcbf9e92a712612d6f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:27:12 +0100 Subject: Sync with the latest Gnulib code (d4ec02b3cc) --- gl/base64.c | 50 +++++++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) (limited to 'gl/base64.c') diff --git a/gl/base64.c b/gl/base64.c index 95b669aa..c8b3b76b 100644 --- a/gl/base64.c +++ b/gl/base64.c @@ -1,5 +1,5 @@ /* base64.c -- Encode binary data using printable characters. - Copyright (C) 1999-2001, 2004-2006, 2009-2023 Free Software Foundation, Inc. + Copyright (C) 1999-2001, 2004-2006, 2009-2024 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -42,6 +42,7 @@ #include /* Get prototype. */ +#define BASE64_INLINE _GL_EXTERN_INLINE #include "base64.h" /* Get imalloc. */ @@ -49,9 +50,6 @@ #include -/* Get UCHAR_MAX. */ -#include - #include /* Convert 'char' to 'unsigned char' without casting. */ @@ -242,7 +240,7 @@ base64_encode_alloc (const char *in, idx_t inlen, char **out) : (_) == '/' ? 63 \ : -1) -static const signed char b64[0x100] = { +signed char const base64_to_int[256] = { B64 (0), B64 (1), B64 (2), B64 (3), B64 (4), B64 (5), B64 (6), B64 (7), B64 (8), B64 (9), B64 (10), B64 (11), @@ -309,28 +307,6 @@ static const signed char b64[0x100] = { B64 (252), B64 (253), B64 (254), B64 (255) }; -#if UCHAR_MAX == 255 -# define uchar_in_range(c) true -#else -# define uchar_in_range(c) ((c) <= 255) -#endif - -/* Return true if CH is a character from the Base64 alphabet, and - false otherwise. Note that '=' is padding and not considered to be - part of the alphabet. */ -bool -isbase64 (char ch) -{ - return uchar_in_range (to_uchar (ch)) && 0 <= b64[to_uchar (ch)]; -} - -/* Initialize decode-context buffer, CTX. */ -void -base64_decode_ctx_init (struct base64_decode_context *ctx) -{ - ctx->i = 0; -} - /* If CTX->i is 0 or 4, there are four or more bytes in [*IN..IN_END), and none of those four is a newline, then return *IN. Otherwise, copy up to 4 - CTX->i non-newline bytes from that range into CTX->buf, starting at @@ -405,8 +381,8 @@ decode_4 (char const *restrict in, idx_t inlen, if (*outleft) { - *out++ = ((b64[to_uchar (in[0])] << 2) - | (b64[to_uchar (in[1])] >> 4)); + *out++ = ((base64_to_int[to_uchar (in[0])] << 2) + | (base64_to_int[to_uchar (in[1])] >> 4)); --*outleft; } @@ -420,6 +396,10 @@ decode_4 (char const *restrict in, idx_t inlen, if (in[3] != '=') return_false; + + /* Reject non-canonical encodings. */ + if (base64_to_int[to_uchar (in[1])] & 0x0f) + return_false; } else { @@ -428,8 +408,8 @@ decode_4 (char const *restrict in, idx_t inlen, if (*outleft) { - *out++ = (((b64[to_uchar (in[1])] << 4) & 0xf0) - | (b64[to_uchar (in[2])] >> 2)); + *out++ = (((base64_to_int[to_uchar (in[1])] << 4) & 0xf0) + | (base64_to_int[to_uchar (in[2])] >> 2)); --*outleft; } @@ -440,6 +420,10 @@ decode_4 (char const *restrict in, idx_t inlen, { if (inlen != 4) return_false; + + /* Reject non-canonical encodings. */ + if (base64_to_int[to_uchar (in[2])] & 0x03) + return_false; } else { @@ -448,8 +432,8 @@ decode_4 (char const *restrict in, idx_t inlen, if (*outleft) { - *out++ = (((b64[to_uchar (in[2])] << 6) & 0xc0) - | b64[to_uchar (in[3])]); + *out++ = (((base64_to_int[to_uchar (in[2])] << 6) & 0xc0) + | base64_to_int[to_uchar (in[3])]); --*outleft; } } -- cgit v1.2.3-74-g34f1