LCOV - code coverage report
Current view: top level - src/common - base64.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 86.0 % 93 80
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 69.0 % 58 40

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * base64.c
       4                 :             :  *        Encoding and decoding routines for base64 without whitespace.
       5                 :             :  *
       6                 :             :  * Copyright (c) 2001-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/common/base64.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : 
      15                 :             : #ifndef FRONTEND
      16                 :             : #include "postgres.h"
      17                 :             : #else
      18                 :             : #include "postgres_fe.h"
      19                 :             : #endif
      20                 :             : 
      21                 :             : #include "common/base64.h"
      22                 :             : 
      23                 :             : /*
      24                 :             :  * BASE64
      25                 :             :  */
      26                 :             : 
      27                 :             : static const char _base64[] =
      28                 :             : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      29                 :             : 
      30                 :             : static const int8 b64lookup[128] = {
      31                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      32                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      33                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
      34                 :             :         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
      35                 :             :         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
      36                 :             :         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
      37                 :             :         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
      38                 :             :         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
      39                 :             : };
      40                 :             : 
      41                 :             : /*
      42                 :             :  * pg_b64_encode
      43                 :             :  *
      44                 :             :  * Encode the 'src' byte array into base64.  Returns the length of the encoded
      45                 :             :  * string, and -1 in the event of an error with the result buffer zeroed for
      46                 :             :  * safety.
      47                 :             :  */
      48                 :             : int
      49                 :          27 : pg_b64_encode(const uint8 *src, int len, char *dst, int dstlen)
      50                 :             : {
      51                 :          27 :         char       *p;
      52                 :          27 :         const uint8 *s,
      53                 :          27 :                            *end = src + len;
      54                 :          27 :         int                     pos = 2;
      55                 :          27 :         uint32          buf = 0;
      56                 :             : 
      57                 :          27 :         s = src;
      58                 :          27 :         p = dst;
      59                 :             : 
      60         [ +  + ]:         747 :         while (s < end)
      61                 :             :         {
      62                 :         720 :                 buf |= *s << (pos << 3);
      63                 :         720 :                 pos--;
      64                 :         720 :                 s++;
      65                 :             : 
      66                 :             :                 /* write it out */
      67         [ +  + ]:         720 :                 if (pos < 0)
      68                 :             :                 {
      69                 :             :                         /*
      70                 :             :                          * Leave if there is an overflow in the area allocated for the
      71                 :             :                          * encoded string.
      72                 :             :                          */
      73         [ -  + ]:         225 :                         if ((p - dst + 4) > dstlen)
      74                 :           0 :                                 goto error;
      75                 :             : 
      76                 :         225 :                         *p++ = _base64[(buf >> 18) & 0x3f];
      77                 :         225 :                         *p++ = _base64[(buf >> 12) & 0x3f];
      78                 :         225 :                         *p++ = _base64[(buf >> 6) & 0x3f];
      79                 :         225 :                         *p++ = _base64[buf & 0x3f];
      80                 :             : 
      81                 :         225 :                         pos = 2;
      82                 :         225 :                         buf = 0;
      83                 :         225 :                 }
      84                 :             :         }
      85         [ -  + ]:          27 :         if (pos != 2)
      86                 :             :         {
      87                 :             :                 /*
      88                 :             :                  * Leave if there is an overflow in the area allocated for the encoded
      89                 :             :                  * string.
      90                 :             :                  */
      91         [ -  + ]:          27 :                 if ((p - dst + 4) > dstlen)
      92                 :           0 :                         goto error;
      93                 :             : 
      94                 :          27 :                 *p++ = _base64[(buf >> 18) & 0x3f];
      95                 :          27 :                 *p++ = _base64[(buf >> 12) & 0x3f];
      96         [ +  + ]:          27 :                 *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
      97                 :          27 :                 *p++ = '=';
      98                 :          27 :         }
      99                 :             : 
     100         [ +  - ]:          27 :         Assert((p - dst) <= dstlen);
     101                 :          27 :         return p - dst;
     102                 :             : 
     103                 :             : error:
     104                 :           0 :         memset(dst, 0, dstlen);
     105                 :           0 :         return -1;
     106                 :          27 : }
     107                 :             : 
     108                 :             : /*
     109                 :             :  * pg_b64_decode
     110                 :             :  *
     111                 :             :  * Decode the given base64 string.  Returns the length of the decoded
     112                 :             :  * string on success, and -1 in the event of an error with the result
     113                 :             :  * buffer zeroed for safety.
     114                 :             :  */
     115                 :             : int
     116                 :          90 : pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen)
     117                 :             : {
     118                 :          90 :         const char *srcend = src + len,
     119                 :          90 :                            *s = src;
     120                 :          90 :         uint8      *p = dst;
     121                 :          90 :         char            c;
     122                 :          90 :         int                     b = 0;
     123                 :          90 :         uint32          buf = 0;
     124                 :          90 :         int                     pos = 0,
     125                 :          90 :                                 end = 0;
     126                 :             : 
     127         [ +  + ]:        3578 :         while (s < srcend)
     128                 :             :         {
     129                 :        3488 :                 c = *s++;
     130                 :             : 
     131                 :             :                 /* Leave if a whitespace is found */
     132   [ +  -  +  -  :        3488 :                 if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
             +  -  -  + ]
     133                 :           0 :                         goto error;
     134                 :             : 
     135         [ +  + ]:        3488 :                 if (c == '=')
     136                 :             :                 {
     137                 :             :                         /* end sequence */
     138         [ +  + ]:         118 :                         if (!end)
     139                 :             :                         {
     140         [ +  + ]:          87 :                                 if (pos == 2)
     141                 :          31 :                                         end = 1;
     142         [ +  - ]:          56 :                                 else if (pos == 3)
     143                 :          56 :                                         end = 2;
     144                 :             :                                 else
     145                 :             :                                 {
     146                 :             :                                         /*
     147                 :             :                                          * Unexpected "=" character found while decoding base64
     148                 :             :                                          * sequence.
     149                 :             :                                          */
     150                 :           0 :                                         goto error;
     151                 :             :                                 }
     152                 :          87 :                         }
     153                 :         118 :                         b = 0;
     154                 :         118 :                 }
     155                 :             :                 else
     156                 :             :                 {
     157                 :        3370 :                         b = -1;
     158   [ +  -  -  + ]:        3370 :                         if (c > 0 && c < 127)
     159                 :        3370 :                                 b = b64lookup[(unsigned char) c];
     160         [ +  - ]:        3370 :                         if (b < 0)
     161                 :             :                         {
     162                 :             :                                 /* invalid symbol found */
     163                 :           0 :                                 goto error;
     164                 :             :                         }
     165                 :             :                 }
     166                 :             :                 /* add it to buffer */
     167                 :        3488 :                 buf = (buf << 6) + b;
     168                 :        3488 :                 pos++;
     169         [ +  + ]:        3488 :                 if (pos == 4)
     170                 :             :                 {
     171                 :             :                         /*
     172                 :             :                          * Leave if there is an overflow in the area allocated for the
     173                 :             :                          * decoded string.
     174                 :             :                          */
     175         [ -  + ]:         872 :                         if ((p - dst + 1) > dstlen)
     176                 :           0 :                                 goto error;
     177                 :         872 :                         *p++ = (buf >> 16) & 255;
     178                 :             : 
     179   [ +  +  +  + ]:         872 :                         if (end == 0 || end > 1)
     180                 :             :                         {
     181                 :             :                                 /* overflow check */
     182         [ +  - ]:         841 :                                 if ((p - dst + 1) > dstlen)
     183                 :           0 :                                         goto error;
     184                 :         841 :                                 *p++ = (buf >> 8) & 255;
     185                 :         841 :                         }
     186   [ +  +  -  + ]:         872 :                         if (end == 0 || end > 2)
     187                 :             :                         {
     188                 :             :                                 /* overflow check */
     189         [ -  + ]:         785 :                                 if ((p - dst + 1) > dstlen)
     190                 :           0 :                                         goto error;
     191                 :         785 :                                 *p++ = buf & 255;
     192                 :         785 :                         }
     193                 :         872 :                         buf = 0;
     194                 :         872 :                         pos = 0;
     195                 :         872 :                 }
     196                 :             :         }
     197                 :             : 
     198         [ -  + ]:          90 :         if (pos != 0)
     199                 :             :         {
     200                 :             :                 /*
     201                 :             :                  * base64 end sequence is invalid.  Input data is missing padding, is
     202                 :             :                  * truncated or is otherwise corrupted.
     203                 :             :                  */
     204                 :           0 :                 goto error;
     205                 :             :         }
     206                 :             : 
     207         [ +  - ]:          90 :         Assert((p - dst) <= dstlen);
     208                 :          90 :         return p - dst;
     209                 :             : 
     210                 :             : error:
     211                 :           0 :         memset(dst, 0, dstlen);
     212                 :           0 :         return -1;
     213                 :          90 : }
     214                 :             : 
     215                 :             : /*
     216                 :             :  * pg_b64_enc_len
     217                 :             :  *
     218                 :             :  * Returns to caller the length of the string if it were encoded with
     219                 :             :  * base64 based on the length provided by caller.  This is useful to
     220                 :             :  * estimate how large a buffer allocation needs to be done before doing
     221                 :             :  * the actual encoding.
     222                 :             :  */
     223                 :             : int
     224                 :          27 : pg_b64_enc_len(int srclen)
     225                 :             : {
     226                 :             :         /* 3 bytes will be converted to 4 */
     227                 :          27 :         return (srclen + 2) / 3 * 4;
     228                 :             : }
     229                 :             : 
     230                 :             : /*
     231                 :             :  * pg_b64_dec_len
     232                 :             :  *
     233                 :             :  * Returns to caller the length of the string if it were to be decoded
     234                 :             :  * with base64, based on the length given by caller.  This is useful to
     235                 :             :  * estimate how large a buffer allocation needs to be done before doing
     236                 :             :  * the actual decoding.
     237                 :             :  */
     238                 :             : int
     239                 :          90 : pg_b64_dec_len(int srclen)
     240                 :             : {
     241                 :          90 :         return (srclen * 3) >> 2;
     242                 :             : }
        

Generated by: LCOV version 2.3.2-1