Re: encode/decode support for base64url

From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: Aleksander Alekseev <aleksander(at)tigerdata(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Cary Huang <cary(dot)huang(at)highgo(dot)ca>, Daniel Gustafsson <daniel(at)yesql(dot)se>, Przemysław Sztoch <przemyslaw(at)sztoch(dot)pl>
Subject: Re: encode/decode support for base64url
Date: 2025-07-10 20:38:43
Message-ID: 3B36664B-4AB5-477B-A64E-0CB2F0181899@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On 10 Jul 2025, at 10:07 PM, David E. Wheeler <david(at)justatheory(dot)com> wrote:
>
> Hi Florents,
>
> On Jul 9, 2025, at 23:25, Florents Tselai <florents(dot)tselai(at)gmail(dot)com> wrote:
>
>>> I reviewed and tested v4. To me it looks as good as it will get.
>>> Personally I would change a few minor things here and there and
>>> probably merge all three patches into a single commit. This however is
>>> up to the committer to decide.
>>
>> Attaching a single-file patch
>
> Somehow missed this thread previously. Had a quick look and had the same question Aleksander asked up-thread:
>
>> Although it is a possible implementation, wouldn't it be better to
>> parametrize pg_base64_encode instead of traversing the string twice?
>> Same for pg_base64_decode. You can refactor pg_base64_encode and make
>> it a wrapper for pg_base64_encode_impl if needed.
>
> It looks as though there could be complements to _base64 and b64urllookup:
>
> ```patch
> diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
> @@ -273,6 +273,9 @@ hex_dec_len(const char *src, size_t srclen)
> static const char _base64[] =
> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
>
> +static const char _base64url[] =
> +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
> +
> static const int8 b64lookup[128] = {
> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> @@ -284,6 +287,18 @@ static const int8 b64lookup[128] = {
> 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
> };
>
> +static const int8 b64urllookup[128] = {
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
> + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
> + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
> + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 62,
> + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
> + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
> +};
> +
> +
> static uint64
> pg_base64_encode(const char *src, size_t len, char *dst)
> {
> ```
>
> And then add the implementation functions that take argument with the proper lookup tables.
>
> Best,
>
> David
>

Why isn’t this sufficient?

static uint64
pg_base64_encode_internal(const char *src, size_t len, char *dst, bool url)
{
const char *alphabet = url ? _base64url : _base64;
There’s already a a bool url param and the alphabet is toggled based on that

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-07-10 20:44:03 18beta2 next week
Previous Message Dmitry Mityugov 2025-07-10 20:00:00 patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings