LCOV - code coverage report
Current view: top level - src/common - base64.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 61 65 93.8 %
Date: 2019-06-18 07:06:57 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * base64.c
       4             :  *    Encoding and decoding routines for base64 without whitespace.
       5             :  *
       6             :  * Copyright (c) 2001-2019, 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 into base64 the given string.  Returns the length of the encoded
      45             :  * string.
      46             :  */
      47             : int
      48         256 : pg_b64_encode(const char *src, int len, char *dst)
      49             : {
      50             :     char       *p;
      51             :     const char *s,
      52         256 :                *end = src + len;
      53         256 :     int         pos = 2;
      54         256 :     uint32      buf = 0;
      55             : 
      56         256 :     s = src;
      57         256 :     p = dst;
      58             : 
      59        7168 :     while (s < end)
      60             :     {
      61        6656 :         buf |= (unsigned char) *s << (pos << 3);
      62        6656 :         pos--;
      63        6656 :         s++;
      64             : 
      65             :         /* write it out */
      66        6656 :         if (pos < 0)
      67             :         {
      68        2106 :             *p++ = _base64[(buf >> 18) & 0x3f];
      69        2106 :             *p++ = _base64[(buf >> 12) & 0x3f];
      70        2106 :             *p++ = _base64[(buf >> 6) & 0x3f];
      71        2106 :             *p++ = _base64[buf & 0x3f];
      72             : 
      73        2106 :             pos = 2;
      74        2106 :             buf = 0;
      75             :         }
      76             :     }
      77         256 :     if (pos != 2)
      78             :     {
      79         192 :         *p++ = _base64[(buf >> 18) & 0x3f];
      80         192 :         *p++ = _base64[(buf >> 12) & 0x3f];
      81         192 :         *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
      82         192 :         *p++ = '=';
      83             :     }
      84             : 
      85         256 :     return p - dst;
      86             : }
      87             : 
      88             : /*
      89             :  * pg_b64_decode
      90             :  *
      91             :  * Decode the given base64 string.  Returns the length of the decoded
      92             :  * string on success, and -1 in the event of an error.
      93             :  */
      94             : int
      95         518 : pg_b64_decode(const char *src, int len, char *dst)
      96             : {
      97         518 :     const char *srcend = src + len,
      98         518 :                *s = src;
      99         518 :     char       *p = dst;
     100             :     char        c;
     101         518 :     int         b = 0;
     102         518 :     uint32      buf = 0;
     103         518 :     int         pos = 0,
     104         518 :                 end = 0;
     105             : 
     106       20932 :     while (s < srcend)
     107             :     {
     108       19896 :         c = *s++;
     109             : 
     110             :         /* Leave if a whitespace is found */
     111       19896 :         if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
     112           0 :             return -1;
     113             : 
     114       19896 :         if (c == '=')
     115             :         {
     116             :             /* end sequence */
     117         682 :             if (!end)
     118             :             {
     119         506 :                 if (pos == 2)
     120         176 :                     end = 1;
     121         330 :                 else if (pos == 3)
     122         330 :                     end = 2;
     123             :                 else
     124             :                 {
     125             :                     /*
     126             :                      * Unexpected "=" character found while decoding base64
     127             :                      * sequence.
     128             :                      */
     129           0 :                     return -1;
     130             :                 }
     131             :             }
     132         682 :             b = 0;
     133             :         }
     134             :         else
     135             :         {
     136       19214 :             b = -1;
     137       19214 :             if (c > 0 && c < 127)
     138       19214 :                 b = b64lookup[(unsigned char) c];
     139       19214 :             if (b < 0)
     140             :             {
     141             :                 /* invalid symbol found */
     142           0 :                 return -1;
     143             :             }
     144             :         }
     145             :         /* add it to buffer */
     146       19896 :         buf = (buf << 6) + b;
     147       19896 :         pos++;
     148       19896 :         if (pos == 4)
     149             :         {
     150        4974 :             *p++ = (buf >> 16) & 255;
     151        4974 :             if (end == 0 || end > 1)
     152        4798 :                 *p++ = (buf >> 8) & 255;
     153        4974 :             if (end == 0 || end > 2)
     154        4468 :                 *p++ = buf & 255;
     155        4974 :             buf = 0;
     156        4974 :             pos = 0;
     157             :         }
     158             :     }
     159             : 
     160         518 :     if (pos != 0)
     161             :     {
     162             :         /*
     163             :          * base64 end sequence is invalid.  Input data is missing padding, is
     164             :          * truncated or is otherwise corrupted.
     165             :          */
     166           0 :         return -1;
     167             :     }
     168             : 
     169         518 :     return p - dst;
     170             : }
     171             : 
     172             : /*
     173             :  * pg_b64_enc_len
     174             :  *
     175             :  * Returns to caller the length of the string if it were encoded with
     176             :  * base64 based on the length provided by caller.  This is useful to
     177             :  * estimate how large a buffer allocation needs to be done before doing
     178             :  * the actual encoding.
     179             :  */
     180             : int
     181         256 : pg_b64_enc_len(int srclen)
     182             : {
     183             :     /* 3 bytes will be converted to 4 */
     184         256 :     return (srclen + 2) * 4 / 3;
     185             : }
     186             : 
     187             : /*
     188             :  * pg_b64_dec_len
     189             :  *
     190             :  * Returns to caller the length of the string if it were to be decoded
     191             :  * with base64, based on the length given by caller.  This is useful to
     192             :  * estimate how large a buffer allocation needs to be done before doing
     193             :  * the actual decoding.
     194             :  */
     195             : int
     196         518 : pg_b64_dec_len(int srclen)
     197             : {
     198         518 :     return (srclen * 3) >> 2;
     199             : }

Generated by: LCOV version 1.13