Fix base64 decode

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-04-23 13:48:09 -07:00
parent 29f96792af
commit c8d45b85e2

View File

@@ -223,20 +223,24 @@ RETURNS bytea AS $$
DECLARE
padded_text text;
mod_length integer;
normalized_text text;
BEGIN
-- Replace URL-safe characters with standard base64 characters
normalized_text := translate(input_text, '-_', '+/');
-- Calculate how many padding characters we need to add
mod_length := length(input_text) % 4;
mod_length := length(normalized_text) % 4;
-- Add the required padding
IF mod_length = 0 THEN
padded_text := input_text;
padded_text := normalized_text;
ELSIF mod_length = 1 THEN
-- Invalid base64 - length mod 4 can't be 1
RAISE EXCEPTION 'Invalid base64 length';
ELSIF mod_length = 2 THEN
padded_text := input_text || '==';
padded_text := normalized_text || '==';
ELSIF mod_length = 3 THEN
padded_text := input_text || '=';
padded_text := normalized_text || '=';
END IF;
-- Decode the padded base64