LCOV - differential code coverage report
Current view: top level - contrib/pgcrypto - pgp-pgsql.c (source / functions) Coverage Total Hit UBC GNC CBC EUB ECB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 87.7 % 463 406 57 3 403 3
Current Date: 2026-03-14 14:10:32 -0400 Functions: 90.0 % 40 36 4 2 34
Baseline: lcov-20260315-024220-baseline Branches: 67.2 % 378 254 124 254 137 37
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 5 5 3 2
(360..) days: 87.6 % 458 401 57 401
Function coverage date bins:
(360..) days: 90.0 % 40 36 4 2 34
Branch coverage date bins:
(30,360] days: 0.0 % 24 0 18 6
(360..) days: 48.1 % 528 254 124 254 119 31

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * pgp-pgsql.c
                                  3                 :                :  *      PostgreSQL wrappers for pgp.
                                  4                 :                :  *
                                  5                 :                :  * Copyright (c) 2005 Marko Kreen
                                  6                 :                :  * All rights reserved.
                                  7                 :                :  *
                                  8                 :                :  * Redistribution and use in source and binary forms, with or without
                                  9                 :                :  * modification, are permitted provided that the following conditions
                                 10                 :                :  * are met:
                                 11                 :                :  * 1. Redistributions of source code must retain the above copyright
                                 12                 :                :  *    notice, this list of conditions and the following disclaimer.
                                 13                 :                :  * 2. Redistributions in binary form must reproduce the above copyright
                                 14                 :                :  *    notice, this list of conditions and the following disclaimer in the
                                 15                 :                :  *    documentation and/or other materials provided with the distribution.
                                 16                 :                :  *
                                 17                 :                :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                                 18                 :                :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                                 19                 :                :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                                 20                 :                :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                                 21                 :                :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                                 22                 :                :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                                 23                 :                :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                                 24                 :                :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                                 25                 :                :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                                 26                 :                :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                                 27                 :                :  * SUCH DAMAGE.
                                 28                 :                :  *
                                 29                 :                :  * contrib/pgcrypto/pgp-pgsql.c
                                 30                 :                :  */
                                 31                 :                : 
                                 32                 :                : #include "postgres.h"
                                 33                 :                : 
                                 34                 :                : #include "catalog/pg_type.h"
                                 35                 :                : #include "common/string.h"
                                 36                 :                : #include "funcapi.h"
                                 37                 :                : #include "lib/stringinfo.h"
                                 38                 :                : #include "mb/pg_wchar.h"
                                 39                 :                : #include "mbuf.h"
                                 40                 :                : #include "pgp.h"
                                 41                 :                : #include "px.h"
                                 42                 :                : #include "utils/array.h"
                                 43                 :                : #include "utils/builtins.h"
                                 44                 :                : 
                                 45                 :                : /*
                                 46                 :                :  * public functions
                                 47                 :                :  */
 7553 bruce@momjian.us           48                 :CBC           5 : PG_FUNCTION_INFO_V1(pgp_sym_encrypt_bytea);
                                 49                 :              7 : PG_FUNCTION_INFO_V1(pgp_sym_encrypt_text);
                                 50                 :              5 : PG_FUNCTION_INFO_V1(pgp_sym_decrypt_bytea);
                                 51                 :              8 : PG_FUNCTION_INFO_V1(pgp_sym_decrypt_text);
                                 52                 :                : 
                                 53                 :              3 : PG_FUNCTION_INFO_V1(pgp_pub_encrypt_bytea);
                                 54                 :              3 : PG_FUNCTION_INFO_V1(pgp_pub_encrypt_text);
                                 55                 :              5 : PG_FUNCTION_INFO_V1(pgp_pub_decrypt_bytea);
                                 56                 :              6 : PG_FUNCTION_INFO_V1(pgp_pub_decrypt_text);
                                 57                 :                : 
                                 58                 :              2 : PG_FUNCTION_INFO_V1(pgp_key_id_w);
                                 59                 :                : 
                                 60                 :              4 : PG_FUNCTION_INFO_V1(pg_armor);
                                 61                 :              7 : PG_FUNCTION_INFO_V1(pg_dearmor);
 4183 heikki.linnakangas@i       62                 :              2 : PG_FUNCTION_INFO_V1(pgp_armor_headers);
                                 63                 :                : 
                                 64                 :                : /*
                                 65                 :                :  * returns src in case of no conversion or error
                                 66                 :                :  */
                                 67                 :                : static text *
 7456 bruce@momjian.us           68                 :UBC           0 : convert_charset(text *src, int cset_from, int cset_to)
                                 69                 :                : {
 3290 noah@leadboat.com          70   [ #  #  #  #  :              0 :     int         src_len = VARSIZE_ANY_EXHDR(src);
                                     #  #  #  #  #  
                                                 # ]
                                 71                 :                :     unsigned char *dst;
                                 72         [ #  # ]:              0 :     unsigned char *csrc = (unsigned char *) VARDATA_ANY(src);
                                 73                 :                :     text       *res;
                                 74                 :                : 
 7553 bruce@momjian.us           75                 :              0 :     dst = pg_do_encoding_conversion(csrc, src_len, cset_from, cset_to);
                                 76         [ #  # ]:              0 :     if (dst == csrc)
                                 77                 :              0 :         return src;
                                 78                 :                : 
 6524 tgl@sss.pgh.pa.us          79                 :              0 :     res = cstring_to_text((char *) dst);
 7553 bruce@momjian.us           80                 :              0 :     pfree(dst);
                                 81                 :              0 :     return res;
                                 82                 :                : }
                                 83                 :                : 
                                 84                 :                : static text *
 7456                            85                 :              0 : convert_from_utf8(text *src)
                                 86                 :                : {
 7553                            87                 :              0 :     return convert_charset(src, PG_UTF8, GetDatabaseEncoding());
                                 88                 :                : }
                                 89                 :                : 
                                 90                 :                : static text *
 7456                            91                 :              0 : convert_to_utf8(text *src)
                                 92                 :                : {
 7553                            93                 :              0 :     return convert_charset(src, GetDatabaseEncoding(), PG_UTF8);
                                 94                 :                : }
                                 95                 :                : 
                                 96                 :                : static void
                                 97                 :              0 : clear_and_pfree(text *p)
                                 98                 :                : {
 3290 noah@leadboat.com          99   [ #  #  #  #  :              0 :     px_memset(p, 0, VARSIZE_ANY(p));
                                     #  #  #  #  #  
                                                 # ]
 7553 bruce@momjian.us          100                 :              0 :     pfree(p);
                                101                 :              0 : }
                                102                 :                : 
                                103                 :                : /*
                                104                 :                :  * expect-* arguments storage
                                105                 :                :  */
                                106                 :                : struct debug_expect
                                107                 :                : {
                                108                 :                :     int         debug;
                                109                 :                :     int         expect;
                                110                 :                :     int         cipher_algo;
                                111                 :                :     int         s2k_mode;
                                112                 :                :     int         s2k_count;
                                113                 :                :     int         s2k_cipher_algo;
                                114                 :                :     int         s2k_digest_algo;
                                115                 :                :     int         compress_algo;
                                116                 :                :     int         use_sess_key;
                                117                 :                :     int         disable_mdc;
                                118                 :                :     int         unicode_mode;
                                119                 :                : };
                                120                 :                : 
                                121                 :                : static void
 3189 tgl@sss.pgh.pa.us         122                 :CBC         117 : fill_expect(struct debug_expect *ex, int text_mode)
                                123                 :                : {
 7553 bruce@momjian.us          124                 :            117 :     ex->debug = 0;
                                125                 :            117 :     ex->expect = 0;
                                126                 :            117 :     ex->cipher_algo = -1;
                                127                 :            117 :     ex->s2k_mode = -1;
 3658 alvherre@alvh.no-ip.      128                 :            117 :     ex->s2k_count = -1;
 7553 bruce@momjian.us          129                 :            117 :     ex->s2k_cipher_algo = -1;
                                130                 :            117 :     ex->s2k_digest_algo = -1;
                                131                 :            117 :     ex->compress_algo = -1;
                                132                 :            117 :     ex->use_sess_key = -1;
                                133                 :            117 :     ex->disable_mdc = -1;
                                134                 :            117 :     ex->unicode_mode = -1;
                                135                 :            117 : }
                                136                 :                : 
                                137                 :                : #define EX_MSG(arg) \
                                138                 :                :     ereport(NOTICE, (errmsg( \
                                139                 :                :         "pgp_decrypt: unexpected %s: expected %d got %d", \
                                140                 :                :         CppAsString(arg), ex->arg, ctx->arg)))
                                141                 :                : 
                                142                 :                : #define EX_CHECK(arg) do { \
                                143                 :                :         if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \
                                144                 :                :     } while (0)
                                145                 :                : 
                                146                 :                : static void
 3189 tgl@sss.pgh.pa.us         147                 :             24 : check_expect(PGP_Context *ctx, struct debug_expect *ex)
                                148                 :                : {
 7553 bruce@momjian.us          149   [ +  +  +  +  :             24 :     EX_CHECK(cipher_algo);
                                              +  - ]
                                150   [ +  +  +  +  :             24 :     EX_CHECK(s2k_mode);
                                              +  - ]
 3658 alvherre@alvh.no-ip.      151   [ +  +  +  +  :             24 :     EX_CHECK(s2k_count);
                                              +  - ]
 7553 bruce@momjian.us          152   [ +  +  +  +  :             24 :     EX_CHECK(s2k_digest_algo);
                                              +  - ]
                                153   [ +  +  +  +  :             24 :     EX_CHECK(use_sess_key);
                                              +  - ]
                                154         [ +  + ]:             24 :     if (ctx->use_sess_key)
                                155   [ -  +  -  -  :              4 :         EX_CHECK(s2k_cipher_algo);
                                              -  - ]
                                156   [ +  +  +  +  :             24 :     EX_CHECK(disable_mdc);
                                              +  - ]
                                157   [ +  +  +  +  :             24 :     EX_CHECK(compress_algo);
                                              +  - ]
                                158   [ -  +  -  -  :             24 :     EX_CHECK(unicode_mode);
                                              -  - ]
                                159                 :             24 : }
                                160                 :                : 
                                161                 :                : static void
 7456                           162                 :              8 : show_debug(const char *msg)
                                163                 :                : {
 7553                           164         [ +  - ]:              8 :     ereport(NOTICE, (errmsg("dbg: %s", msg)));
                                165                 :              8 : }
                                166                 :                : 
                                167                 :                : static int
 6121                           168                 :             73 : set_arg(PGP_Context *ctx, char *key, char *val,
                                169                 :                :         struct debug_expect *ex)
                                170                 :                : {
 7456                           171                 :             73 :     int         res = 0;
                                172                 :                : 
 7553                           173         [ +  + ]:             73 :     if (strcmp(key, "cipher-algo") == 0)
                                174                 :              6 :         res = pgp_set_cipher_algo(ctx, val);
                                175         [ +  + ]:             67 :     else if (strcmp(key, "disable-mdc") == 0)
                                176                 :              1 :         res = pgp_disable_mdc(ctx, atoi(val));
                                177         [ +  + ]:             66 :     else if (strcmp(key, "sess-key") == 0)
                                178                 :              5 :         res = pgp_set_sess_key(ctx, atoi(val));
                                179         [ +  + ]:             61 :     else if (strcmp(key, "s2k-mode") == 0)
                                180                 :              3 :         res = pgp_set_s2k_mode(ctx, atoi(val));
 3658 alvherre@alvh.no-ip.      181         [ +  + ]:             58 :     else if (strcmp(key, "s2k-count") == 0)
                                182                 :              2 :         res = pgp_set_s2k_count(ctx, atoi(val));
 7553 bruce@momjian.us          183         [ +  + ]:             56 :     else if (strcmp(key, "s2k-digest-algo") == 0)
                                184                 :              2 :         res = pgp_set_s2k_digest_algo(ctx, val);
                                185         [ -  + ]:             54 :     else if (strcmp(key, "s2k-cipher-algo") == 0)
 7553 bruce@momjian.us          186                 :UBC           0 :         res = pgp_set_s2k_cipher_algo(ctx, val);
 7553 bruce@momjian.us          187         [ +  + ]:CBC          54 :     else if (strcmp(key, "compress-algo") == 0)
                                188                 :              5 :         res = pgp_set_compress_algo(ctx, atoi(val));
                                189         [ +  + ]:             49 :     else if (strcmp(key, "compress-level") == 0)
                                190                 :              2 :         res = pgp_set_compress_level(ctx, atoi(val));
                                191         [ +  + ]:             47 :     else if (strcmp(key, "convert-crlf") == 0)
                                192                 :              5 :         res = pgp_set_convert_crlf(ctx, atoi(val));
                                193         [ -  + ]:             42 :     else if (strcmp(key, "unicode-mode") == 0)
 7553 bruce@momjian.us          194                 :UBC           0 :         res = pgp_set_unicode_mode(ctx, atoi(val));
                                195                 :                : 
                                196                 :                :     /*
                                197                 :                :      * The remaining options are for debugging/testing and are therefore not
                                198                 :                :      * documented in the user-facing docs.
                                199                 :                :      */
 7553 bruce@momjian.us          200   [ +  -  +  + ]:CBC          42 :     else if (ex != NULL && strcmp(key, "debug") == 0)
                                201                 :              5 :         ex->debug = atoi(val);
                                202   [ +  -  +  + ]:             37 :     else if (ex != NULL && strcmp(key, "expect-cipher-algo") == 0)
                                203                 :                :     {
                                204                 :              8 :         ex->expect = 1;
                                205                 :              8 :         ex->cipher_algo = pgp_get_cipher_code(val);
                                206                 :                :     }
                                207   [ +  -  +  + ]:             29 :     else if (ex != NULL && strcmp(key, "expect-disable-mdc") == 0)
                                208                 :                :     {
                                209                 :              3 :         ex->expect = 1;
                                210                 :              3 :         ex->disable_mdc = atoi(val);
                                211                 :                :     }
                                212   [ +  -  +  + ]:             26 :     else if (ex != NULL && strcmp(key, "expect-sess-key") == 0)
                                213                 :                :     {
                                214                 :              7 :         ex->expect = 1;
                                215                 :              7 :         ex->use_sess_key = atoi(val);
                                216                 :                :     }
                                217   [ +  -  +  + ]:             19 :     else if (ex != NULL && strcmp(key, "expect-s2k-mode") == 0)
                                218                 :                :     {
                                219                 :              5 :         ex->expect = 1;
                                220                 :              5 :         ex->s2k_mode = atoi(val);
                                221                 :                :     }
 3658 alvherre@alvh.no-ip.      222   [ +  -  +  + ]:             14 :     else if (ex != NULL && strcmp(key, "expect-s2k-count") == 0)
                                223                 :                :     {
                                224                 :              2 :         ex->expect = 1;
                                225                 :              2 :         ex->s2k_count = atoi(val);
                                226                 :                :     }
 7553 bruce@momjian.us          227   [ +  -  +  + ]:             12 :     else if (ex != NULL && strcmp(key, "expect-s2k-digest-algo") == 0)
                                228                 :                :     {
                                229                 :              4 :         ex->expect = 1;
                                230                 :              4 :         ex->s2k_digest_algo = pgp_get_digest_code(val);
                                231                 :                :     }
                                232   [ +  -  -  + ]:              8 :     else if (ex != NULL && strcmp(key, "expect-s2k-cipher-algo") == 0)
                                233                 :                :     {
 7553 bruce@momjian.us          234                 :UBC           0 :         ex->expect = 1;
                                235                 :              0 :         ex->s2k_cipher_algo = pgp_get_cipher_code(val);
                                236                 :                :     }
 7553 bruce@momjian.us          237   [ +  -  +  - ]:CBC           8 :     else if (ex != NULL && strcmp(key, "expect-compress-algo") == 0)
                                238                 :                :     {
                                239                 :              8 :         ex->expect = 1;
                                240                 :              8 :         ex->compress_algo = atoi(val);
                                241                 :                :     }
 7553 bruce@momjian.us          242   [ #  #  #  # ]:UBC           0 :     else if (ex != NULL && strcmp(key, "expect-unicode-mode") == 0)
                                243                 :                :     {
                                244                 :              0 :         ex->expect = 1;
                                245                 :              0 :         ex->unicode_mode = atoi(val);
                                246                 :                :     }
                                247                 :                :     else
                                248                 :              0 :         res = PXE_ARGUMENT_ERROR;
                                249                 :                : 
 7553 bruce@momjian.us          250                 :CBC          73 :     return res;
                                251                 :                : }
                                252                 :                : 
                                253                 :                : /*
                                254                 :                :  * Find next word.  Handle ',' and '=' as words.  Skip whitespace.
                                255                 :                :  * Put word info into res_p, res_len.
                                256                 :                :  * Returns ptr to next word.
                                257                 :                :  */
                                258                 :                : static char *
 7456                           259                 :            146 : getword(char *p, char **res_p, int *res_len)
                                260                 :                : {
                                261                 :                :     /* whitespace at start */
 7553                           262   [ +  -  +  +  :            183 :     while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
                                        +  +  +  + ]
                                263                 :             37 :         p++;
                                264                 :                : 
                                265                 :                :     /* word data */
                                266                 :            146 :     *res_p = p;
                                267   [ +  -  -  + ]:            146 :     if (*p == '=' || *p == ',')
 7553 bruce@momjian.us          268                 :UBC           0 :         p++;
                                269                 :                :     else
 7553 bruce@momjian.us          270   [ +  +  +  -  :CBC        1326 :         while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'
                                        +  -  +  + ]
 7456                           271   [ +  +  +  + ]:           1271 :                        || *p == '=' || *p == ','))
 7553                           272                 :           1180 :             p++;
                                273                 :                : 
                                274                 :                :     /* word end */
                                275                 :            146 :     *res_len = p - *res_p;
                                276                 :                : 
                                277                 :                :     /* whitespace at end */
                                278   [ +  +  -  +  :            152 :     while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
                                        +  +  +  + ]
                                279                 :              6 :         p++;
                                280                 :                : 
                                281                 :            146 :     return p;
                                282                 :                : }
                                283                 :                : 
                                284                 :                : /*
                                285                 :                :  * Convert to lowercase asciiz string.
                                286                 :                :  */
                                287                 :                : static char *
 7456                           288                 :             55 : downcase_convert(const uint8 *s, int len)
                                289                 :                : {
                                290                 :                :     int         c,
                                291                 :                :                 i;
                                292                 :             55 :     char       *res = palloc(len + 1);
                                293                 :                : 
                                294         [ +  + ]:           1369 :     for (i = 0; i < len; i++)
                                295                 :                :     {
 7553                           296                 :           1314 :         c = s[i];
                                297   [ +  +  -  + ]:           1314 :         if (c >= 'A' && c <= 'Z')
 7553 bruce@momjian.us          298                 :UBC           0 :             c += 'a' - 'A';
 7553 bruce@momjian.us          299                 :CBC        1314 :         res[i] = c;
                                300                 :                :     }
                                301                 :             55 :     res[len] = 0;
                                302                 :             55 :     return res;
                                303                 :                : }
                                304                 :                : 
                                305                 :                : static int
 6121                           306                 :             55 : parse_args(PGP_Context *ctx, uint8 *args, int arg_len,
                                307                 :                :            struct debug_expect *ex)
                                308                 :                : {
 7456                           309                 :             55 :     char       *str = downcase_convert(args, arg_len);
                                310                 :                :     char       *key,
                                311                 :                :                *val;
                                312                 :                :     int         key_len,
                                313                 :                :                 val_len;
                                314                 :             55 :     int         res = 0;
                                315                 :             55 :     char       *p = str;
                                316                 :                : 
 7553                           317         [ +  + ]:            128 :     while (*p)
                                318                 :                :     {
                                319                 :             73 :         res = PXE_ARGUMENT_ERROR;
                                320                 :             73 :         p = getword(p, &key, &key_len);
                                321         [ -  + ]:             73 :         if (*p++ != '=')
 7553 bruce@momjian.us          322                 :UBC           0 :             break;
 7553 bruce@momjian.us          323                 :CBC          73 :         p = getword(p, &val, &val_len);
                                324         [ +  + ]:             73 :         if (*p == '\0')
                                325                 :                :             ;
                                326         [ -  + ]:             18 :         else if (*p++ != ',')
 7553 bruce@momjian.us          327                 :UBC           0 :             break;
                                328                 :                : 
 7553 bruce@momjian.us          329   [ +  -  +  -  :CBC          73 :         if (*key == 0 || *val == 0 || val_len == 0)
                                              +  - ]
                                330                 :                :             break;
                                331                 :                : 
                                332                 :             73 :         key[key_len] = 0;
                                333                 :             73 :         val[val_len] = 0;
                                334                 :                : 
                                335                 :             73 :         res = set_arg(ctx, key, val, ex);
                                336         [ -  + ]:             73 :         if (res < 0)
 7553 bruce@momjian.us          337                 :UBC           0 :             break;
                                338                 :                :     }
 7553 bruce@momjian.us          339                 :CBC          55 :     pfree(str);
                                340                 :             55 :     return res;
                                341                 :                : }
                                342                 :                : 
                                343                 :                : static MBuf *
                                344                 :             82 : create_mbuf_from_vardata(text *data)
                                345                 :                : {
 3290 noah@leadboat.com         346         [ -  + ]:             82 :     return mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
                                347   [ -  +  -  -  :             82 :                                  VARSIZE_ANY_EXHDR(data));
                                     -  -  -  -  -  
                                                 + ]
                                348                 :                : }
                                349                 :                : 
                                350                 :                : static void
 6121 bruce@momjian.us          351                 :            117 : init_work(PGP_Context **ctx_p, int is_text,
                                352                 :                :           text *args, struct debug_expect *ex)
                                353                 :                : {
 7456                           354                 :            117 :     int         err = pgp_init(ctx_p);
                                355                 :                : 
 7553                           356                 :            117 :     fill_expect(ex, is_text);
                                357                 :                : 
                                358   [ +  -  +  + ]:            117 :     if (err == 0 && args != NULL)
 3290 noah@leadboat.com         359         [ -  + ]:             55 :         err = parse_args(*ctx_p, (uint8 *) VARDATA_ANY(args),
                                360   [ -  +  -  -  :             55 :                          VARSIZE_ANY_EXHDR(args), ex);
                                     -  -  -  -  -  
                                                 + ]
                                361                 :                : 
 7553 bruce@momjian.us          362         [ -  + ]:            117 :     if (err)
 3387 heikki.linnakangas@i      363                 :UBC           0 :         px_THROW_ERROR(err);
                                364                 :                : 
 7553 bruce@momjian.us          365         [ +  + ]:CBC         117 :     if (ex->debug)
                                366                 :              5 :         px_set_debug_handler(show_debug);
                                367                 :                : 
                                368                 :            117 :     pgp_set_text_mode(*ctx_p, is_text);
                                369                 :            117 : }
                                370                 :                : 
                                371                 :                : static bytea *
                                372                 :             38 : encrypt_internal(int is_pubenc, int is_text,
                                373                 :                :                  text *data, text *key, text *args)
                                374                 :                : {
                                375                 :                :     MBuf       *src,
                                376                 :                :                *dst;
                                377                 :                :     uint8       tmp[VARHDRSZ];
                                378                 :                :     uint8      *restmp;
                                379                 :                :     bytea      *res;
                                380                 :                :     int         res_len;
                                381                 :                :     PGP_Context *ctx;
                                382                 :                :     int         err;
                                383                 :                :     struct debug_expect ex;
 7456                           384                 :             38 :     text       *tmp_data = NULL;
                                385                 :                : 
 7553                           386                 :             38 :     init_work(&ctx, is_text, args, &ex);
                                387                 :                : 
                                388   [ +  +  -  + ]:             38 :     if (is_text && pgp_get_unicode_mode(ctx))
                                389                 :                :     {
 7553 bruce@momjian.us          390                 :UBC           0 :         tmp_data = convert_to_utf8(data);
                                391         [ #  # ]:              0 :         if (tmp_data == data)
                                392                 :              0 :             tmp_data = NULL;
                                393                 :                :         else
                                394                 :              0 :             data = tmp_data;
                                395                 :                :     }
                                396                 :                : 
 7553 bruce@momjian.us          397                 :CBC          38 :     src = create_mbuf_from_vardata(data);
 3290 noah@leadboat.com         398   [ -  +  -  -  :             38 :     dst = mbuf_create(VARSIZE_ANY(data) + 128);
                                     -  -  -  -  -  
                                                 + ]
                                399                 :                : 
                                400                 :                :     /*
                                401                 :                :      * reserve room for header
                                402                 :                :      */
 7553 bruce@momjian.us          403                 :             38 :     mbuf_append(dst, tmp, VARHDRSZ);
                                404                 :                : 
                                405                 :                :     /*
                                406                 :                :      * set key
                                407                 :                :      */
                                408         [ +  + ]:             38 :     if (is_pubenc)
                                409                 :                :     {
 7456                           410                 :              8 :         MBuf       *kbuf = create_mbuf_from_vardata(key);
                                411                 :                : 
 7553                           412                 :              8 :         err = pgp_set_pubkey(ctx, kbuf,
                                413                 :                :                              NULL, 0, 0);
                                414                 :              8 :         mbuf_free(kbuf);
                                415                 :                :     }
                                416                 :                :     else
 3290 noah@leadboat.com         417         [ -  + ]:             30 :         err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
                                418   [ -  +  -  -  :             30 :                              VARSIZE_ANY_EXHDR(key));
                                     -  -  -  -  -  
                                                 + ]
                                419                 :                : 
                                420                 :                :     /*
                                421                 :                :      * encrypt
                                422                 :                :      */
 7553 bruce@momjian.us          423         [ +  + ]:             38 :     if (err >= 0)
                                424                 :             36 :         err = pgp_encrypt(ctx, src, dst);
                                425                 :                : 
                                426                 :                :     /*
                                427                 :                :      * check for error
                                428                 :                :      */
                                429         [ +  + ]:             38 :     if (err)
                                430                 :                :     {
                                431         [ -  + ]:              2 :         if (ex.debug)
 7553 bruce@momjian.us          432                 :UBC           0 :             px_set_debug_handler(NULL);
 7553 bruce@momjian.us          433         [ -  + ]:CBC           2 :         if (tmp_data)
 7553 bruce@momjian.us          434                 :UBC           0 :             clear_and_pfree(tmp_data);
 7553 bruce@momjian.us          435                 :CBC           2 :         pgp_free(ctx);
                                436                 :              2 :         mbuf_free(src);
                                437                 :              2 :         mbuf_free(dst);
 3387 heikki.linnakangas@i      438                 :              2 :         px_THROW_ERROR(err);
                                439                 :                :     }
                                440                 :                : 
                                441                 :                :     /* res_len includes VARHDRSZ */
 7553 bruce@momjian.us          442                 :             36 :     res_len = mbuf_steal_data(dst, &restmp);
                                443                 :             36 :     res = (bytea *) restmp;
 6956 tgl@sss.pgh.pa.us         444                 :             36 :     SET_VARSIZE(res, res_len);
                                445                 :                : 
 7553 bruce@momjian.us          446         [ -  + ]:             36 :     if (tmp_data)
 7553 bruce@momjian.us          447                 :UBC           0 :         clear_and_pfree(tmp_data);
 7553 bruce@momjian.us          448                 :CBC          36 :     pgp_free(ctx);
                                449                 :             36 :     mbuf_free(src);
                                450                 :             36 :     mbuf_free(dst);
                                451                 :                : 
                                452                 :             36 :     px_set_debug_handler(NULL);
                                453                 :                : 
                                454                 :             36 :     return res;
                                455                 :                : }
                                456                 :                : 
                                457                 :                : static bytea *
                                458                 :             79 : decrypt_internal(int is_pubenc, int need_text, text *data,
                                459                 :                :                  text *key, text *keypsw, text *args)
                                460                 :                : {
                                461                 :                :     int         err;
 7456                           462                 :             79 :     MBuf       *src = NULL,
                                463                 :             79 :                *dst = NULL;
                                464                 :                :     uint8       tmp[VARHDRSZ];
                                465                 :                :     uint8      *restmp;
                                466                 :                :     bytea      *res;
                                467                 :                :     int         res_len;
 7553                           468                 :             79 :     PGP_Context *ctx = NULL;
                                469                 :                :     struct debug_expect ex;
 7456                           470                 :             79 :     int         got_unicode = 0;
                                471                 :                : 
                                472                 :                : 
 7553                           473                 :             79 :     init_work(&ctx, need_text, args, &ex);
                                474                 :                : 
 3290 noah@leadboat.com         475         [ -  + ]:             79 :     src = mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
                                476   [ -  +  -  -  :             79 :                                 VARSIZE_ANY_EXHDR(data));
                                     -  -  -  -  -  
                                                 + ]
                                477   [ -  +  -  -  :             79 :     dst = mbuf_create(VARSIZE_ANY(data) + 2048);
                                     -  -  -  -  -  
                                                 + ]
                                478                 :                : 
                                479                 :                :     /*
                                480                 :                :      * reserve room for header
                                481                 :                :      */
 7553 bruce@momjian.us          482                 :             79 :     mbuf_append(dst, tmp, VARHDRSZ);
                                483                 :                : 
                                484                 :                :     /*
                                485                 :                :      * set key
                                486                 :                :      */
                                487         [ +  + ]:             79 :     if (is_pubenc)
                                488                 :                :     {
 7456                           489                 :             19 :         uint8      *psw = NULL;
                                490                 :             19 :         int         psw_len = 0;
                                491                 :                :         MBuf       *kbuf;
                                492                 :                : 
 7553                           493         [ +  + ]:             19 :         if (keypsw)
                                494                 :                :         {
 3290 noah@leadboat.com         495         [ -  + ]:              4 :             psw = (uint8 *) VARDATA_ANY(keypsw);
                                496   [ -  +  -  -  :              4 :             psw_len = VARSIZE_ANY_EXHDR(keypsw);
                                     -  -  -  -  -  
                                                 + ]
                                497                 :                :         }
 7553 bruce@momjian.us          498                 :             19 :         kbuf = create_mbuf_from_vardata(key);
                                499                 :             19 :         err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1);
                                500                 :             19 :         mbuf_free(kbuf);
                                501                 :                :     }
                                502                 :                :     else
 3290 noah@leadboat.com         503         [ -  + ]:             60 :         err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
                                504   [ -  +  -  -  :             60 :                              VARSIZE_ANY_EXHDR(key));
                                     -  -  -  -  -  
                                                 + ]
                                505                 :                : 
                                506                 :                :     /* decrypt */
 7553 bruce@momjian.us          507         [ +  + ]:             79 :     if (err >= 0)
                                508                 :                :     {
                                509                 :             75 :         err = pgp_decrypt(ctx, src, dst);
                                510                 :                : 
 4057 rhaas@postgresql.org      511         [ +  + ]:             75 :         if (ex.expect)
                                512                 :             24 :             check_expect(ctx, &ex);
                                513                 :                : 
                                514                 :                :         /* remember the setting */
                                515                 :             75 :         got_unicode = pgp_get_unicode_mode(ctx);
                                516                 :                :     }
                                517                 :                : 
                                518                 :             79 :     mbuf_free(src);
                                519                 :             79 :     pgp_free(ctx);
                                520                 :                : 
 7553 bruce@momjian.us          521         [ +  + ]:             79 :     if (err)
                                522                 :                :     {
                                523                 :             15 :         px_set_debug_handler(NULL);
 4057 rhaas@postgresql.org      524                 :             15 :         mbuf_free(dst);
 3387 heikki.linnakangas@i      525                 :             15 :         px_THROW_ERROR(err);
                                526                 :                :     }
                                527                 :                : 
 7553 bruce@momjian.us          528                 :             64 :     res_len = mbuf_steal_data(dst, &restmp);
                                529                 :             64 :     mbuf_free(dst);
                                530                 :                : 
                                531                 :                :     /* res_len includes VARHDRSZ */
                                532                 :             64 :     res = (bytea *) restmp;
 6956 tgl@sss.pgh.pa.us         533                 :             64 :     SET_VARSIZE(res, res_len);
                                534                 :                : 
 7553 bruce@momjian.us          535   [ +  +  -  + ]:             64 :     if (need_text && got_unicode)
                                536                 :                :     {
 7456 bruce@momjian.us          537                 :UBC           0 :         text       *utf = convert_from_utf8(res);
                                538                 :                : 
 7553                           539         [ #  # ]:              0 :         if (utf != res)
                                540                 :                :         {
                                541                 :              0 :             clear_and_pfree(res);
                                542                 :              0 :             res = utf;
                                543                 :                :         }
                                544                 :                :     }
 7553 bruce@momjian.us          545                 :CBC          64 :     px_set_debug_handler(NULL);
                                546                 :                : 
                                547                 :             64 :     return res;
                                548                 :                : }
                                549                 :                : 
                                550                 :                : /*
                                551                 :                :  * Wrappers for symmetric-key functions
                                552                 :                :  */
                                553                 :                : Datum
                                554                 :              3 : pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS)
                                555                 :                : {
                                556                 :                :     bytea      *data;
                                557                 :              3 :     text       *arg = NULL;
                                558                 :                :     text       *res,
                                559                 :                :                *key;
                                560                 :                : 
 3290 noah@leadboat.com         561                 :              3 :     data = PG_GETARG_BYTEA_PP(0);
  760 michael@paquier.xyz       562                 :              3 :     key = PG_GETARG_TEXT_PP(1);
 7553 bruce@momjian.us          563         [ +  + ]:              3 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       564                 :              1 :         arg = PG_GETARG_TEXT_PP(2);
                                565                 :                : 
 7553 bruce@momjian.us          566                 :              3 :     res = encrypt_internal(0, 0, data, key, arg);
                                567                 :                : 
                                568         [ -  + ]:              3 :     PG_FREE_IF_COPY(data, 0);
                                569         [ -  + ]:              3 :     PG_FREE_IF_COPY(key, 1);
                                570         [ +  + ]:              3 :     if (PG_NARGS() > 2)
                                571         [ -  + ]:              1 :         PG_FREE_IF_COPY(arg, 2);
                                572                 :              3 :     PG_RETURN_TEXT_P(res);
                                573                 :                : }
                                574                 :                : 
                                575                 :                : Datum
                                576                 :             27 : pgp_sym_encrypt_text(PG_FUNCTION_ARGS)
                                577                 :                : {
                                578                 :                :     text       *data,
                                579                 :                :                *key;
                                580                 :             27 :     text       *arg = NULL;
                                581                 :                :     text       *res;
                                582                 :                : 
  760 michael@paquier.xyz       583                 :             27 :     data = PG_GETARG_TEXT_PP(0);
                                584                 :             27 :     key = PG_GETARG_TEXT_PP(1);
 7553 bruce@momjian.us          585         [ +  + ]:             27 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       586                 :             22 :         arg = PG_GETARG_TEXT_PP(2);
                                587                 :                : 
 7553 bruce@momjian.us          588                 :             27 :     res = encrypt_internal(0, 1, data, key, arg);
                                589                 :                : 
                                590         [ -  + ]:             27 :     PG_FREE_IF_COPY(data, 0);
                                591         [ -  + ]:             27 :     PG_FREE_IF_COPY(key, 1);
                                592         [ +  + ]:             27 :     if (PG_NARGS() > 2)
                                593         [ -  + ]:             22 :         PG_FREE_IF_COPY(arg, 2);
                                594                 :             27 :     PG_RETURN_TEXT_P(res);
                                595                 :                : }
                                596                 :                : 
                                597                 :                : 
                                598                 :                : Datum
                                599                 :              4 : pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS)
                                600                 :                : {
                                601                 :                :     bytea      *data;
                                602                 :              4 :     text       *arg = NULL;
                                603                 :                :     text       *res,
                                604                 :                :                *key;
                                605                 :                : 
 3290 noah@leadboat.com         606                 :              4 :     data = PG_GETARG_BYTEA_PP(0);
  760 michael@paquier.xyz       607                 :              4 :     key = PG_GETARG_TEXT_PP(1);
 7553 bruce@momjian.us          608         [ +  + ]:              4 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       609                 :              1 :         arg = PG_GETARG_TEXT_PP(2);
                                610                 :                : 
 7553 bruce@momjian.us          611                 :              4 :     res = decrypt_internal(0, 0, data, key, NULL, arg);
                                612                 :                : 
                                613         [ -  + ]:              4 :     PG_FREE_IF_COPY(data, 0);
                                614         [ -  + ]:              4 :     PG_FREE_IF_COPY(key, 1);
                                615         [ +  + ]:              4 :     if (PG_NARGS() > 2)
                                616         [ -  + ]:              1 :         PG_FREE_IF_COPY(arg, 2);
                                617                 :              4 :     PG_RETURN_TEXT_P(res);
                                618                 :                : }
                                619                 :                : 
                                620                 :                : Datum
                                621                 :             56 : pgp_sym_decrypt_text(PG_FUNCTION_ARGS)
                                622                 :                : {
                                623                 :                :     bytea      *data;
                                624                 :             56 :     text       *arg = NULL;
                                625                 :                :     text       *res,
                                626                 :                :                *key;
                                627                 :                : 
 3290 noah@leadboat.com         628                 :             56 :     data = PG_GETARG_BYTEA_PP(0);
  760 michael@paquier.xyz       629                 :             56 :     key = PG_GETARG_TEXT_PP(1);
 7553 bruce@momjian.us          630         [ +  + ]:             56 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       631                 :             31 :         arg = PG_GETARG_TEXT_PP(2);
                                632                 :                : 
 7553 bruce@momjian.us          633                 :             56 :     res = decrypt_internal(0, 1, data, key, NULL, arg);
   34 noah@leadboat.com         634   [ -  +  -  -  :             50 :     pg_verifymbstr(VARDATA_ANY(res), VARSIZE_ANY_EXHDR(res), false);
                                     -  -  -  -  -  
                                           +  -  + ]
                                635                 :                : 
 7553 bruce@momjian.us          636         [ -  + ]:             49 :     PG_FREE_IF_COPY(data, 0);
                                637         [ -  + ]:             49 :     PG_FREE_IF_COPY(key, 1);
                                638         [ +  + ]:             49 :     if (PG_NARGS() > 2)
                                639         [ -  + ]:             26 :         PG_FREE_IF_COPY(arg, 2);
                                640                 :             49 :     PG_RETURN_TEXT_P(res);
                                641                 :                : }
                                642                 :                : 
                                643                 :                : /*
                                644                 :                :  * Wrappers for public-key functions
                                645                 :                :  */
                                646                 :                : 
                                647                 :                : Datum
                                648                 :              1 : pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS)
                                649                 :                : {
                                650                 :                :     bytea      *data,
                                651                 :                :                *key;
                                652                 :              1 :     text       *arg = NULL;
                                653                 :                :     text       *res;
                                654                 :                : 
 3290 noah@leadboat.com         655                 :              1 :     data = PG_GETARG_BYTEA_PP(0);
                                656                 :              1 :     key = PG_GETARG_BYTEA_PP(1);
 7553 bruce@momjian.us          657         [ -  + ]:              1 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       658                 :UBC           0 :         arg = PG_GETARG_TEXT_PP(2);
                                659                 :                : 
 7553 bruce@momjian.us          660                 :CBC           1 :     res = encrypt_internal(1, 0, data, key, arg);
                                661                 :                : 
                                662         [ -  + ]:              1 :     PG_FREE_IF_COPY(data, 0);
                                663         [ -  + ]:              1 :     PG_FREE_IF_COPY(key, 1);
                                664         [ -  + ]:              1 :     if (PG_NARGS() > 2)
 7553 bruce@momjian.us          665         [ #  # ]:UBC           0 :         PG_FREE_IF_COPY(arg, 2);
 7553 bruce@momjian.us          666                 :CBC           1 :     PG_RETURN_TEXT_P(res);
                                667                 :                : }
                                668                 :                : 
                                669                 :                : Datum
                                670                 :              7 : pgp_pub_encrypt_text(PG_FUNCTION_ARGS)
                                671                 :                : {
                                672                 :                :     bytea      *key;
                                673                 :              7 :     text       *arg = NULL;
                                674                 :                :     text       *res,
                                675                 :                :                *data;
                                676                 :                : 
  760 michael@paquier.xyz       677                 :              7 :     data = PG_GETARG_TEXT_PP(0);
 3290 noah@leadboat.com         678                 :              7 :     key = PG_GETARG_BYTEA_PP(1);
 7553 bruce@momjian.us          679         [ -  + ]:              7 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       680                 :UBC           0 :         arg = PG_GETARG_TEXT_PP(2);
                                681                 :                : 
 7553 bruce@momjian.us          682                 :CBC           7 :     res = encrypt_internal(1, 1, data, key, arg);
                                683                 :                : 
                                684         [ -  + ]:              5 :     PG_FREE_IF_COPY(data, 0);
                                685         [ -  + ]:              5 :     PG_FREE_IF_COPY(key, 1);
                                686         [ -  + ]:              5 :     if (PG_NARGS() > 2)
 7553 bruce@momjian.us          687         [ #  # ]:UBC           0 :         PG_FREE_IF_COPY(arg, 2);
 7553 bruce@momjian.us          688                 :CBC           5 :     PG_RETURN_TEXT_P(res);
                                689                 :                : }
                                690                 :                : 
                                691                 :                : 
                                692                 :                : Datum
                                693                 :              2 : pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS)
                                694                 :                : {
                                695                 :                :     bytea      *data,
                                696                 :                :                *key;
                                697                 :              2 :     text       *psw = NULL,
                                698                 :              2 :                *arg = NULL;
                                699                 :                :     text       *res;
                                700                 :                : 
 3290 noah@leadboat.com         701                 :              2 :     data = PG_GETARG_BYTEA_PP(0);
                                702                 :              2 :     key = PG_GETARG_BYTEA_PP(1);
 7553 bruce@momjian.us          703         [ -  + ]:              2 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       704                 :UBC           0 :         psw = PG_GETARG_TEXT_PP(2);
 7553 bruce@momjian.us          705         [ -  + ]:CBC           2 :     if (PG_NARGS() > 3)
  760 michael@paquier.xyz       706                 :UBC           0 :         arg = PG_GETARG_TEXT_PP(3);
                                707                 :                : 
 7553 bruce@momjian.us          708                 :CBC           2 :     res = decrypt_internal(1, 0, data, key, psw, arg);
                                709                 :                : 
                                710         [ -  + ]:              1 :     PG_FREE_IF_COPY(data, 0);
                                711         [ -  + ]:              1 :     PG_FREE_IF_COPY(key, 1);
                                712         [ -  + ]:              1 :     if (PG_NARGS() > 2)
 7553 bruce@momjian.us          713         [ #  # ]:UBC           0 :         PG_FREE_IF_COPY(psw, 2);
 7553 bruce@momjian.us          714         [ -  + ]:CBC           1 :     if (PG_NARGS() > 3)
 7553 bruce@momjian.us          715         [ #  # ]:UBC           0 :         PG_FREE_IF_COPY(arg, 3);
 7553 bruce@momjian.us          716                 :CBC           1 :     PG_RETURN_TEXT_P(res);
                                717                 :                : }
                                718                 :                : 
                                719                 :                : Datum
                                720                 :             17 : pgp_pub_decrypt_text(PG_FUNCTION_ARGS)
                                721                 :                : {
                                722                 :                :     bytea      *data,
                                723                 :                :                *key;
                                724                 :             17 :     text       *psw = NULL,
                                725                 :             17 :                *arg = NULL;
                                726                 :                :     text       *res;
                                727                 :                : 
 3290 noah@leadboat.com         728                 :             17 :     data = PG_GETARG_BYTEA_PP(0);
                                729                 :             17 :     key = PG_GETARG_BYTEA_PP(1);
 7553 bruce@momjian.us          730         [ +  + ]:             17 :     if (PG_NARGS() > 2)
  760 michael@paquier.xyz       731                 :              4 :         psw = PG_GETARG_TEXT_PP(2);
 7553 bruce@momjian.us          732         [ -  + ]:             17 :     if (PG_NARGS() > 3)
  760 michael@paquier.xyz       733                 :UBC           0 :         arg = PG_GETARG_TEXT_PP(3);
                                734                 :                : 
 7553 bruce@momjian.us          735                 :CBC          17 :     res = decrypt_internal(1, 1, data, key, psw, arg);
   34 noah@leadboat.com         736   [ -  +  -  -  :              9 :     pg_verifymbstr(VARDATA_ANY(res), VARSIZE_ANY_EXHDR(res), false);
                                     -  -  -  -  -  
                                           +  -  + ]
                                737                 :                : 
 7553 bruce@momjian.us          738         [ -  + ]:              9 :     PG_FREE_IF_COPY(data, 0);
                                739         [ -  + ]:              9 :     PG_FREE_IF_COPY(key, 1);
                                740         [ +  + ]:              9 :     if (PG_NARGS() > 2)
                                741         [ -  + ]:              2 :         PG_FREE_IF_COPY(psw, 2);
                                742         [ -  + ]:              9 :     if (PG_NARGS() > 3)
 7553 bruce@momjian.us          743         [ #  # ]:UBC           0 :         PG_FREE_IF_COPY(arg, 3);
 7553 bruce@momjian.us          744                 :CBC           9 :     PG_RETURN_TEXT_P(res);
                                745                 :                : }
                                746                 :                : 
                                747                 :                : 
                                748                 :                : /*
                                749                 :                :  * Wrappers for PGP ascii armor
                                750                 :                :  */
                                751                 :                : 
                                752                 :                : /*
                                753                 :                :  * Helper function for pg_armor. Converts arrays of keys and values into
                                754                 :                :  * plain C arrays, and checks that they don't contain invalid characters.
                                755                 :                :  */
                                756                 :                : static int
 4183 heikki.linnakangas@i      757                 :             14 : parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array,
                                758                 :                :                        char ***p_keys, char ***p_values)
                                759                 :                : {
 3949 bruce@momjian.us          760                 :             14 :     int         nkdims = ARR_NDIM(key_array);
                                761                 :             14 :     int         nvdims = ARR_NDIM(val_array);
                                762                 :                :     char      **keys,
                                763                 :                :               **values;
                                764                 :                :     Datum      *key_datums,
                                765                 :                :                *val_datums;
                                766                 :                :     bool       *key_nulls,
                                767                 :                :                *val_nulls;
                                768                 :                :     int         key_count,
                                769                 :                :                 val_count;
                                770                 :                :     int         i;
                                771                 :                : 
 4183 heikki.linnakangas@i      772   [ +  +  +  + ]:             14 :     if (nkdims > 1 || nkdims != nvdims)
                                773         [ +  - ]:              2 :         ereport(ERROR,
                                774                 :                :                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                                775                 :                :                  errmsg("wrong number of array subscripts")));
                                776         [ -  + ]:             12 :     if (nkdims == 0)
 4183 heikki.linnakangas@i      777                 :UBC           0 :         return 0;
                                778                 :                : 
 1353 peter@eisentraut.org      779                 :CBC          12 :     deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
                                780                 :             12 :     deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
                                781                 :                : 
 4183 heikki.linnakangas@i      782         [ +  + ]:             12 :     if (key_count != val_count)
                                783         [ +  - ]:              2 :         ereport(ERROR,
                                784                 :                :                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                                785                 :                :                  errmsg("mismatched array dimensions")));
                                786                 :                : 
  100 michael@paquier.xyz       787                 :GNC          10 :     keys = palloc_array(char *, key_count);
                                788                 :             10 :     values = palloc_array(char *, val_count);
                                789                 :                : 
 4183 heikki.linnakangas@i      790         [ +  + ]:CBC          17 :     for (i = 0; i < key_count; i++)
                                791                 :                :     {
                                792                 :                :         char       *v;
                                793                 :                : 
                                794                 :                :         /* Check that the key doesn't contain anything funny */
                                795         [ +  + ]:             12 :         if (key_nulls[i])
                                796         [ +  - ]:              1 :             ereport(ERROR,
                                797                 :                :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                798                 :                :                      errmsg("null value not allowed for header key")));
                                799                 :                : 
                                800                 :             11 :         v = TextDatumGetCString(key_datums[i]);
                                801                 :                : 
 1910 michael@paquier.xyz       802         [ -  + ]:             11 :         if (!pg_is_ascii(v))
 4183 heikki.linnakangas@i      803         [ #  # ]:UBC           0 :             ereport(ERROR,
                                804                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                805                 :                :                      errmsg("header key must not contain non-ASCII characters")));
 4183 heikki.linnakangas@i      806         [ +  + ]:CBC          11 :         if (strstr(v, ": "))
                                807         [ +  - ]:              1 :             ereport(ERROR,
                                808                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                809                 :                :                      errmsg("header key must not contain \": \"")));
                                810         [ +  + ]:             10 :         if (strchr(v, '\n'))
                                811         [ +  - ]:              1 :             ereport(ERROR,
                                812                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                813                 :                :                      errmsg("header key must not contain newlines")));
                                814                 :              9 :         keys[i] = v;
                                815                 :                : 
                                816                 :                :         /* And the same for the value */
                                817         [ +  + ]:              9 :         if (val_nulls[i])
                                818         [ +  - ]:              1 :             ereport(ERROR,
                                819                 :                :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                820                 :                :                      errmsg("null value not allowed for header value")));
                                821                 :                : 
                                822                 :              8 :         v = TextDatumGetCString(val_datums[i]);
                                823                 :                : 
 1910 michael@paquier.xyz       824         [ -  + ]:              8 :         if (!pg_is_ascii(v))
 4183 heikki.linnakangas@i      825         [ #  # ]:UBC           0 :             ereport(ERROR,
                                826                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                827                 :                :                      errmsg("header value must not contain non-ASCII characters")));
 4183 heikki.linnakangas@i      828         [ +  + ]:CBC           8 :         if (strchr(v, '\n'))
                                829         [ +  - ]:              1 :             ereport(ERROR,
                                830                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                831                 :                :                      errmsg("header value must not contain newlines")));
                                832                 :                : 
                                833                 :              7 :         values[i] = v;
                                834                 :                :     }
                                835                 :                : 
                                836                 :              5 :     *p_keys = keys;
                                837                 :              5 :     *p_values = values;
                                838                 :              5 :     return key_count;
                                839                 :                : }
                                840                 :                : 
                                841                 :                : Datum
 7553 bruce@momjian.us          842                 :             19 : pg_armor(PG_FUNCTION_ARGS)
                                843                 :                : {
                                844                 :                :     bytea      *data;
                                845                 :                :     text       *res;
                                846                 :                :     int         data_len;
                                847                 :                :     StringInfoData buf;
                                848                 :                :     int         num_headers;
 4183 heikki.linnakangas@i      849                 :             19 :     char      **keys = NULL,
                                850                 :             19 :               **values = NULL;
                                851                 :                : 
 3290 noah@leadboat.com         852                 :             19 :     data = PG_GETARG_BYTEA_PP(0);
                                853   [ -  +  -  -  :             19 :     data_len = VARSIZE_ANY_EXHDR(data);
                                     -  -  -  -  -  
                                                 + ]
 4183 heikki.linnakangas@i      854         [ +  + ]:             19 :     if (PG_NARGS() == 3)
                                855                 :                :     {
                                856                 :             14 :         num_headers = parse_key_value_arrays(PG_GETARG_ARRAYTYPE_P(1),
                                857                 :             14 :                                              PG_GETARG_ARRAYTYPE_P(2),
                                858                 :                :                                              &keys, &values);
                                859                 :                :     }
                                860         [ +  - ]:              5 :     else if (PG_NARGS() == 1)
                                861                 :              5 :         num_headers = 0;
                                862                 :                :     else
 4183 heikki.linnakangas@i      863         [ #  # ]:UBC           0 :         elog(ERROR, "unexpected number of arguments %d", PG_NARGS());
                                864                 :                : 
 4189 heikki.linnakangas@i      865                 :CBC          10 :     initStringInfo(&buf);
                                866                 :                : 
 3290 noah@leadboat.com         867         [ -  + ]:             10 :     pgp_armor_encode((uint8 *) VARDATA_ANY(data), data_len, &buf,
                                868                 :                :                      num_headers, keys, values);
                                869                 :                : 
 4189 heikki.linnakangas@i      870                 :             10 :     res = palloc(VARHDRSZ + buf.len);
                                871                 :             10 :     SET_VARSIZE(res, VARHDRSZ + buf.len);
                                872                 :             10 :     memcpy(VARDATA(res), buf.data, buf.len);
                                873                 :             10 :     pfree(buf.data);
                                874                 :                : 
 7553 bruce@momjian.us          875         [ -  + ]:             10 :     PG_FREE_IF_COPY(data, 0);
                                876                 :             10 :     PG_RETURN_TEXT_P(res);
                                877                 :                : }
                                878                 :                : 
                                879                 :                : Datum
                                880                 :             90 : pg_dearmor(PG_FUNCTION_ARGS)
                                881                 :                : {
                                882                 :                :     text       *data;
                                883                 :                :     bytea      *res;
                                884                 :                :     int         data_len;
                                885                 :                :     int         ret;
                                886                 :                :     StringInfoData buf;
                                887                 :                : 
 3290 noah@leadboat.com         888                 :             90 :     data = PG_GETARG_TEXT_PP(0);
                                889   [ -  +  -  -  :             90 :     data_len = VARSIZE_ANY_EXHDR(data);
                                     -  -  -  -  -  
                                                 + ]
                                890                 :                : 
 4189 heikki.linnakangas@i      891                 :             90 :     initStringInfo(&buf);
                                892                 :                : 
 3290 noah@leadboat.com         893         [ -  + ]:             90 :     ret = pgp_armor_decode((uint8 *) VARDATA_ANY(data), data_len, &buf);
 4189 heikki.linnakangas@i      894         [ +  + ]:             90 :     if (ret < 0)
 3387                           895                 :              1 :         px_THROW_ERROR(ret);
 4189                           896                 :             89 :     res = palloc(VARHDRSZ + buf.len);
                                897                 :             89 :     SET_VARSIZE(res, VARHDRSZ + buf.len);
                                898                 :             89 :     memcpy(VARDATA(res), buf.data, buf.len);
                                899                 :             89 :     pfree(buf.data);
                                900                 :                : 
 7553 bruce@momjian.us          901         [ +  + ]:             89 :     PG_FREE_IF_COPY(data, 0);
                                902                 :             89 :     PG_RETURN_TEXT_P(res);
                                903                 :                : }
                                904                 :                : 
                                905                 :                : /* cross-call state for pgp_armor_headers */
                                906                 :                : typedef struct
                                907                 :                : {
                                908                 :                :     int         nheaders;
                                909                 :                :     char      **keys;
                                910                 :                :     char      **values;
                                911                 :                : } pgp_armor_headers_state;
                                912                 :                : 
                                913                 :                : Datum
 4183 heikki.linnakangas@i      914                 :             37 : pgp_armor_headers(PG_FUNCTION_ARGS)
                                915                 :                : {
                                916                 :                :     FuncCallContext *funcctx;
                                917                 :                :     pgp_armor_headers_state *state;
                                918                 :                :     char       *utf8key;
                                919                 :                :     char       *utf8val;
                                920                 :                :     HeapTuple   tuple;
                                921                 :                :     TupleDesc   tupdesc;
                                922                 :                :     AttInMetadata *attinmeta;
                                923                 :                : 
                                924         [ +  + ]:             37 :     if (SRF_IS_FIRSTCALL())
                                925                 :                :     {
                                926                 :             14 :         text       *data = PG_GETARG_TEXT_PP(0);
                                927                 :                :         int         res;
                                928                 :                :         MemoryContext oldcontext;
                                929                 :                : 
                                930                 :             14 :         funcctx = SRF_FIRSTCALL_INIT();
                                931                 :                : 
                                932                 :                :         /* we need the state allocated in the multi call context */
                                933                 :             14 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                934                 :                : 
                                935                 :                :         /* Build a tuple descriptor for our result type */
                                936         [ -  + ]:             14 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 4183 heikki.linnakangas@i      937         [ #  # ]:UBC           0 :             elog(ERROR, "return type must be a row type");
                                938                 :                : 
 4183 heikki.linnakangas@i      939                 :CBC          14 :         attinmeta = TupleDescGetAttInMetadata(tupdesc);
                                940                 :             14 :         funcctx->attinmeta = attinmeta;
                                941                 :                : 
  100 michael@paquier.xyz       942                 :GNC          14 :         state = palloc_object(pgp_armor_headers_state);
                                943                 :                : 
 4183 heikki.linnakangas@i      944         [ -  + ]:CBC          14 :         res = pgp_extract_armor_headers((uint8 *) VARDATA_ANY(data),
                                945   [ -  +  -  -  :             14 :                                         VARSIZE_ANY_EXHDR(data),
                                     -  -  -  -  -  
                                                 + ]
                                946                 :                :                                         &state->nheaders, &state->keys,
                                947                 :                :                                         &state->values);
                                948         [ +  + ]:             14 :         if (res < 0)
 3387                           949                 :              2 :             px_THROW_ERROR(res);
                                950                 :                : 
 4183                           951                 :             12 :         MemoryContextSwitchTo(oldcontext);
                                952                 :             12 :         funcctx->user_fctx = state;
                                953                 :                :     }
                                954                 :                : 
                                955                 :             35 :     funcctx = SRF_PERCALL_SETUP();
                                956                 :             35 :     state = (pgp_armor_headers_state *) funcctx->user_fctx;
                                957                 :                : 
                                958         [ +  + ]:             35 :     if (funcctx->call_cntr >= state->nheaders)
                                959                 :             12 :         SRF_RETURN_DONE(funcctx);
                                960                 :                :     else
                                961                 :                :     {
                                962                 :                :         char       *values[2];
                                963                 :                : 
                                964                 :                :         /* we assume that the keys (and values) are in UTF-8. */
                                965                 :             23 :         utf8key = state->keys[funcctx->call_cntr];
                                966                 :             23 :         utf8val = state->values[funcctx->call_cntr];
                                967                 :                : 
                                968                 :             23 :         values[0] = pg_any_to_server(utf8key, strlen(utf8key), PG_UTF8);
                                969                 :             23 :         values[1] = pg_any_to_server(utf8val, strlen(utf8val), PG_UTF8);
                                970                 :                : 
                                971                 :                :         /* build a tuple */
                                972                 :             23 :         tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
                                973                 :             23 :         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
                                974                 :                :     }
                                975                 :                : }
                                976                 :                : 
                                977                 :                : 
                                978                 :                : 
                                979                 :                : /*
                                980                 :                :  * Wrappers for PGP key id
                                981                 :                :  */
                                982                 :                : 
                                983                 :                : Datum
 7553 bruce@momjian.us          984                 :             17 : pgp_key_id_w(PG_FUNCTION_ARGS)
                                985                 :                : {
                                986                 :                :     bytea      *data;
                                987                 :                :     text       *res;
                                988                 :                :     int         res_len;
                                989                 :                :     MBuf       *buf;
                                990                 :                : 
 3290 noah@leadboat.com         991                 :             17 :     data = PG_GETARG_BYTEA_PP(0);
 7553 bruce@momjian.us          992                 :             17 :     buf = create_mbuf_from_vardata(data);
                                993                 :             17 :     res = palloc(VARHDRSZ + 17);
                                994                 :                : 
                                995                 :             17 :     res_len = pgp_get_keyid(buf, VARDATA(res));
                                996                 :             17 :     mbuf_free(buf);
                                997         [ +  + ]:             17 :     if (res_len < 0)
 3387 heikki.linnakangas@i      998                 :              2 :         px_THROW_ERROR(res_len);
 6956 tgl@sss.pgh.pa.us         999                 :             15 :     SET_VARSIZE(res, VARHDRSZ + res_len);
                               1000                 :                : 
 7553 bruce@momjian.us         1001         [ -  + ]:             15 :     PG_FREE_IF_COPY(data, 0);
                               1002                 :             15 :     PG_RETURN_TEXT_P(res);
                               1003                 :                : }
        

Generated by: LCOV version 2.4-beta