[patch 2/6] various cleanups - Mailing list pgsql-patches

From Marko Kreen
Subject [patch 2/6] various cleanups
Date
Msg-id 20050319234647.000674000@grue
Whole thread Raw
In response to [patch 0/6] pgcrypto update  (Marko Kreen <marko@l-t.ee>)
List pgsql-patches
* construct "struct {} list [] = {}" confuses pgindent - split those.
  It was a bad style to begin with, and now several loops can be clearer.
* pgcrypto.c: Fix function comments
* crypt-gensalt.c, crypt-blowfish.c: stop messing with errno
* openssl.c: use px_free instead pfree
* px.h: make redefining px_alloc/px_realloc/px_free easier


Index: pgsql/contrib/pgcrypto/openssl.c
===================================================================
*** pgsql.orig/contrib/pgcrypto/openssl.c
--- pgsql/contrib/pgcrypto/openssl.c
*************** gen_ossl_free(PX_Cipher * c)
*** 208,215 ****
      ossldata   *od = (ossldata *) c->ptr;

      memset(od, 0, sizeof(*od));
!     pfree(od);
!     pfree(c);
  }

  /* Blowfish */
--- 208,215 ----
      ossldata   *od = (ossldata *) c->ptr;

      memset(od, 0, sizeof(*od));
!     px_free(od);
!     px_free(c);
  }

  /* Blowfish */
*************** static const struct ossl_cipher ossl_cas
*** 473,509 ****
  /*
   * Special handlers
   */
! static const struct
  {
      const char *name;
      const struct ossl_cipher *ciph;
! }    ossl_cipher_types[] =

! {
!     {
!         "bf-cbc", &ossl_bf_cbc
!     },
!     {
!         "bf-ecb", &ossl_bf_ecb
!     },
!     {
!         "bf-cfb", &ossl_bf_cfb
!     },
!     {
!         "des-ecb", &ossl_des_ecb
!     },
!     {
!         "des-cbc", &ossl_des_cbc
!     },
!     {
!         "cast5-ecb", &ossl_cast_ecb
!     },
!     {
!         "cast5-cbc", &ossl_cast_cbc
!     },
!     {
!         NULL
!     }
  };

  /* PUBLIC functions */
--- 473,493 ----
  /*
   * Special handlers
   */
! struct ossl_cipher_lookup
  {
      const char *name;
      const struct ossl_cipher *ciph;
! };

! static const struct ossl_cipher_lookup ossl_cipher_types[] = {
!     {"bf-cbc", &ossl_bf_cbc},
!     {"bf-ecb", &ossl_bf_ecb},
!     {"bf-cfb", &ossl_bf_cfb},
!     {"des-ecb", &ossl_des_ecb},
!     {"des-cbc", &ossl_des_cbc},
!     {"cast5-ecb", &ossl_cast_ecb},
!     {"cast5-cbc", &ossl_cast_cbc},
!     {NULL}
  };

  /* PUBLIC functions */
*************** static const struct
*** 511,548 ****
  int
  px_find_cipher(const char *name, PX_Cipher ** res)
  {
!     unsigned    i;
!     PX_Cipher  *c = NULL,
!                *csrc;
      ossldata   *od;
-     const struct ossl_cipher *ossl_ciph = NULL;

      name = px_resolve_alias(ossl_aliases, name);
!     for (i = 0; ossl_cipher_types[i].name; i++)
!     {
!         if (!strcmp(ossl_cipher_types[i].name, name))
!         {
!             ossl_ciph = ossl_cipher_types[i].ciph;
              break;
!         }
!     }
!     if (ossl_ciph == NULL)
          return -1;

      od = px_alloc(sizeof(*od));
      memset(od, 0, sizeof(*od));
!     od->ciph = ossl_ciph;
!
!     csrc = NULL;

      c = px_alloc(sizeof(*c));
      c->block_size = gen_ossl_block_size;
      c->key_size = gen_ossl_key_size;
      c->iv_size = gen_ossl_iv_size;
      c->free = gen_ossl_free;
!     c->init = ossl_ciph->init;
!     c->encrypt = ossl_ciph->encrypt;
!     c->decrypt = ossl_ciph->decrypt;
      c->ptr = od;

      *res = c;
--- 495,523 ----
  int
  px_find_cipher(const char *name, PX_Cipher ** res)
  {
!     const struct ossl_cipher_lookup *i;
!     PX_Cipher  *c = NULL;
      ossldata   *od;

      name = px_resolve_alias(ossl_aliases, name);
!     for (i = ossl_cipher_types; i->name; i++)
!         if (!strcmp(i->name, name))
              break;
!     if (i->name == NULL)
          return -1;

      od = px_alloc(sizeof(*od));
      memset(od, 0, sizeof(*od));
!     od->ciph = i->ciph;

      c = px_alloc(sizeof(*c));
      c->block_size = gen_ossl_block_size;
      c->key_size = gen_ossl_key_size;
      c->iv_size = gen_ossl_iv_size;
      c->free = gen_ossl_free;
!     c->init = od->ciph->init;
!     c->encrypt = od->ciph->encrypt;
!     c->decrypt = od->ciph->decrypt;
      c->ptr = od;

      *res = c;
Index: pgsql/contrib/pgcrypto/px-crypt.c
===================================================================
*** pgsql.orig/contrib/pgcrypto/px-crypt.c
--- pgsql/contrib/pgcrypto/px-crypt.c
*************** run_crypt_bf(const char *psw, const char
*** 69,120 ****
      return res;
  }

! static struct
  {
      char       *id;
      unsigned    id_len;
      char       *(*crypt) (const char *psw, const char *salt,
                                        char *buf, unsigned len);
! }    px_crypt_list[] =

! {
!     {
!         "$2a$", 4, run_crypt_bf
!     },
!     {
!         "$2$", 3, NULL
!     },                            /* N/A */
!     {
!         "$1$", 3, run_crypt_md5
!     },
!     {
!         "_", 1, run_crypt_des
!     },
!     {
!         "", 0, run_crypt_des
!     },
!     {
!         NULL, 0, NULL
!     }
  };

  char *
  px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
  {
!     int            i;

!     for (i = 0; px_crypt_list[i].id; i++)
      {
!         if (!px_crypt_list[i].id_len)
              break;
!         if (!strncmp(salt, px_crypt_list[i].id, px_crypt_list[i].id_len))
              break;
      }

!     if (px_crypt_list[i].crypt == NULL)
          return NULL;

!     return px_crypt_list[i].crypt(psw, salt, buf, len);
  }

  #else                            /* PX_SYSTEM_CRYPT */
--- 69,109 ----
      return res;
  }

! struct px_crypt_algo
  {
      char       *id;
      unsigned    id_len;
      char       *(*crypt) (const char *psw, const char *salt,
                                        char *buf, unsigned len);
! };

! static const struct px_crypt_algo
! px_crypt_list[] = {
!     {"$2a$", 4, run_crypt_bf},
!     {"$2$", 3, NULL},                            /* N/A */
!     {"$1$", 3, run_crypt_md5},
!     {"_", 1, run_crypt_des},
!     {"", 0, run_crypt_des},
!     {NULL, 0, NULL}
  };

  char *
  px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
  {
!     const struct px_crypt_algo *c;

!     for (c = px_crypt_list; c->id; c++)
      {
!         if (!c->id_len)
              break;
!         if (!strncmp(salt, c->id, c->id_len))
              break;
      }

!     if (c->crypt == NULL)
          return NULL;

!     return c->crypt(psw, salt, buf, len);
  }

  #else                            /* PX_SYSTEM_CRYPT */
*************** static struct generator gen_list[] = {
*** 155,161 ****
      {"md5", _crypt_gensalt_md5_rn, 6, 0, 0, 0},
      {"xdes", _crypt_gensalt_extended_rn, 3, PX_XDES_ROUNDS, 1, 0xFFFFFF},
      {"bf", _crypt_gensalt_blowfish_rn, 16, PX_BF_ROUNDS, 4, 31},
!     {NULL, NULL, 0, 0, 0}
  };

  unsigned
--- 144,150 ----
      {"md5", _crypt_gensalt_md5_rn, 6, 0, 0, 0},
      {"xdes", _crypt_gensalt_extended_rn, 3, PX_XDES_ROUNDS, 1, 0xFFFFFF},
      {"bf", _crypt_gensalt_blowfish_rn, 16, PX_BF_ROUNDS, 4, 31},
!     {NULL, NULL, 0, 0, 0, 0}
  };

  unsigned
Index: pgsql/contrib/pgcrypto/internal.c
===================================================================
*** pgsql.orig/contrib/pgcrypto/internal.c
--- pgsql/contrib/pgcrypto/internal.c
***************
*** 57,78 ****
  static void init_md5(PX_MD * h);
  static void init_sha1(PX_MD * h);

! static struct int_digest
  {
      char       *name;
      void        (*init) (PX_MD * h);
! }    int_digest_list[] =

! {
!     {
!         "md5", init_md5
!     },
!     {
!         "sha1", init_sha1
!     },
!     {
!         NULL, NULL
!     }
  };

  /* MD5 */
--- 57,73 ----
  static void init_md5(PX_MD * h);
  static void init_sha1(PX_MD * h);

! struct int_digest
  {
      char       *name;
      void        (*init) (PX_MD * h);
! };

! static const struct int_digest
! int_digest_list[] = {
!     { "md5", init_md5 },
!     { "sha1", init_sha1 },
!     { NULL, NULL }
  };

  /* MD5 */
*************** bf_cbc_load(void)
*** 516,546 ****
      return bf_load(MODE_CBC);
  }

! static struct
  {
      char       *name;
      PX_Cipher  *(*load) (void);
! }    int_ciphers[] =

! {
!     {
!         "bf-cbc", bf_cbc_load
!     },
!     {
!         "bf-ecb", bf_ecb_load
!     },
!     {
!         "aes-128-cbc", rj_128_cbc
!     },
!     {
!         "aes-128-ecb", rj_128_ecb
!     },
!     {
!         NULL, NULL
!     }
  };

! static PX_Alias int_aliases[] = {
      {"bf", "bf-cbc"},
      {"blowfish", "bf-cbc"},
      {"aes", "aes-128-cbc"},
--- 511,532 ----
      return bf_load(MODE_CBC);
  }

! struct int_cipher
  {
      char       *name;
      PX_Cipher  *(*load) (void);
! };

! static const struct int_cipher
! int_ciphers[] = {
!     { "bf-cbc", bf_cbc_load },
!     { "bf-ecb", bf_ecb_load },
!     { "aes-128-cbc", rj_128_cbc },
!     { "aes-128-ecb", rj_128_ecb },
!     { NULL, NULL }
  };

! static const PX_Alias int_aliases[] = {
      {"bf", "bf-cbc"},
      {"blowfish", "bf-cbc"},
      {"aes", "aes-128-cbc"},
*************** static PX_Alias int_aliases[] = {
*** 557,563 ****
  int
  px_find_digest(const char *name, PX_MD ** res)
  {
!     struct int_digest *p;
      PX_MD       *h;

      for (p = int_digest_list; p->name; p++)
--- 543,549 ----
  int
  px_find_digest(const char *name, PX_MD ** res)
  {
!     const struct int_digest *p;
      PX_MD       *h;

      for (p = int_digest_list; p->name; p++)
Index: pgsql/contrib/pgcrypto/pgcrypto.c
===================================================================
*** pgsql.orig/contrib/pgcrypto/pgcrypto.c
--- pgsql/contrib/pgcrypto/pgcrypto.c
*************** typedef int (*PFN) (const char *name, vo
*** 46,52 ****
  static void *
              find_provider(text *name, PFN pf, char *desc, int silent);

! /* SQL function: hash(text, text) returns text */
  PG_FUNCTION_INFO_V1(pg_digest);

  Datum
--- 46,52 ----
  static void *
              find_provider(text *name, PFN pf, char *desc, int silent);

! /* SQL function: hash(bytea, text) returns bytea */
  PG_FUNCTION_INFO_V1(pg_digest);

  Datum
*************** pg_digest_exists(PG_FUNCTION_ARGS)
*** 111,117 ****
      PG_RETURN_BOOL(true);
  }

! /* SQL function: hmac(data:text, key:text, type:text) */
  PG_FUNCTION_INFO_V1(pg_hmac);

  Datum
--- 111,117 ----
      PG_RETURN_BOOL(true);
  }

! /* SQL function: hmac(data:bytea, key:bytea, type:text) returns bytea */
  PG_FUNCTION_INFO_V1(pg_hmac);

  Datum
*************** pg_crypt(PG_FUNCTION_ARGS)
*** 316,322 ****
      PG_RETURN_TEXT_P(res);
  }

! /* SQL function: pg_encrypt(text, text, text) returns text */
  PG_FUNCTION_INFO_V1(pg_encrypt);

  Datum
--- 316,322 ----
      PG_RETURN_TEXT_P(res);
  }

! /* SQL function: pg_encrypt(bytea, bytea, text) returns bytea */
  PG_FUNCTION_INFO_V1(pg_encrypt);

  Datum
*************** pg_encrypt(PG_FUNCTION_ARGS)
*** 367,373 ****
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_decrypt(text, text, text) returns text */
  PG_FUNCTION_INFO_V1(pg_decrypt);

  Datum
--- 367,373 ----
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_decrypt(bytea, bytea, text) returns bytea */
  PG_FUNCTION_INFO_V1(pg_decrypt);

  Datum
*************** pg_decrypt(PG_FUNCTION_ARGS)
*** 417,423 ****
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_encrypt(text, text, text) returns text */
  PG_FUNCTION_INFO_V1(pg_encrypt_iv);

  Datum
--- 417,423 ----
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_encrypt_iv(bytea, bytea, bytea, text) returns bytea */
  PG_FUNCTION_INFO_V1(pg_encrypt_iv);

  Datum
*************** pg_encrypt_iv(PG_FUNCTION_ARGS)
*** 473,479 ****
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_decrypt_iv(text, text, text) returns text */
  PG_FUNCTION_INFO_V1(pg_decrypt_iv);

  Datum
--- 473,479 ----
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_decrypt_iv(bytea, bytea, bytea, text) returns bytea */
  PG_FUNCTION_INFO_V1(pg_decrypt_iv);

  Datum
*************** pg_decrypt_iv(PG_FUNCTION_ARGS)
*** 529,535 ****
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_decrypt(text, text, text) returns text */
  PG_FUNCTION_INFO_V1(pg_cipher_exists);

  Datum
--- 529,535 ----
      PG_RETURN_BYTEA_P(res);
  }

! /* SQL function: pg_cipher_exists(text) returns bool */
  PG_FUNCTION_INFO_V1(pg_cipher_exists);

  Datum
*************** pg_cipher_exists(PG_FUNCTION_ARGS)
*** 550,556 ****
      PG_RETURN_BOOL((c != NULL) ? true : false);
  }

-
  static void *
  find_provider(text *name,
                PFN provider_lookup,
--- 550,555 ----
Index: pgsql/contrib/pgcrypto/crypt-blowfish.c
===================================================================
*** pgsql.orig/contrib/pgcrypto/crypt-blowfish.c
--- pgsql/contrib/pgcrypto/crypt-blowfish.c
***************
*** 35,46 ****
  #include "px.h"
  #include "px-crypt.h"

- #define __set_errno(v)
-
- #ifndef __set_errno
- #define __set_errno(val) errno = (val)
- #endif
-
  #ifdef __i386__
  #define BF_ASM                0    /* 1 */
  #define BF_SCALE            1
--- 35,40 ----
*************** _crypt_blowfish_rn(const char *key, cons
*** 600,609 ****
      int            i;

      if (size < 7 + 22 + 31 + 1)
-     {
-         __set_errno(ERANGE);
          return NULL;
-     }

      if (setting[0] != '$' ||
          setting[1] != '2' ||
--- 594,600 ----
*************** _crypt_blowfish_rn(const char *key, cons
*** 613,619 ****
          setting[5] < '0' || setting[5] > '9' ||
          setting[6] != '$')
      {
-         __set_errno(EINVAL);
          return NULL;
      }

--- 604,609 ----
*************** _crypt_blowfish_rn(const char *key, cons
*** 621,627 ****
      if (count < 16 || BF_decode(data.binary.salt, &setting[7], 16))
      {
          memset(data.binary.salt, 0, sizeof(data.binary.salt));
-         __set_errno(EINVAL);
          return NULL;
      }
      BF_swap(data.binary.salt, 4);
--- 611,616 ----
Index: pgsql/contrib/pgcrypto/crypt-gensalt.c
===================================================================
*** pgsql.orig/contrib/pgcrypto/crypt-gensalt.c
--- pgsql/contrib/pgcrypto/crypt-gensalt.c
***************
*** 15,25 ****
  #include "px.h"
  #include "px-crypt.h"

- #include <errno.h>
- #ifndef __set_errno
- #define __set_errno(val) (errno = (val))
- #endif
-
  typedef unsigned int BF_word;

  unsigned char _crypt_itoa64[64 + 1] =
--- 15,20 ----
*************** _crypt_gensalt_traditional_rn(unsigned l
*** 33,39 ****
      {
          if (output_size > 0)
              output[0] = '\0';
-         __set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
          return NULL;
      }

--- 28,33 ----
*************** _crypt_gensalt_extended_rn(unsigned long
*** 57,63 ****
      {
          if (output_size > 0)
              output[0] = '\0';
-         __set_errno((output_size < 1 + 4 + 4 + 1) ? ERANGE : EINVAL);
          return NULL;
      }

--- 51,56 ----
*************** _crypt_gensalt_md5_rn(unsigned long coun
*** 91,97 ****
      {
          if (output_size > 0)
              output[0] = '\0';
-         __set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
          return NULL;
      }

--- 84,89 ----
*************** _crypt_gensalt_blowfish_rn(unsigned long
*** 173,179 ****
      {
          if (output_size > 0)
              output[0] = '\0';
-         __set_errno((output_size < 7 + 22 + 1) ? ERANGE : EINVAL);
          return NULL;
      }

--- 165,170 ----
Index: pgsql/contrib/pgcrypto/px.h
===================================================================
*** pgsql.orig/contrib/pgcrypto/px.h
--- pgsql/contrib/pgcrypto/px.h
***************
*** 43,63 ****
  #endif


! #if 1

  #define px_alloc(s) palloc(s)
! #define px_realloc(p, s) prealloc(p, s)
  #define px_free(p)    pfree(p)

  #else

! void       *xalloc(size_t s);
! void       *xrealloc(void *p, size_t s);
! void        xfree(void *p);
!
! #define px_alloc(s) xalloc(s)
! #define px_realloc(p, s) xrealloc(p, s)
! #define px_free(p)    xfree(p)
  #endif

  /* max len of 'type' parms */
--- 43,60 ----
  #endif


! #ifndef PX_OWN_ALLOC

  #define px_alloc(s) palloc(s)
! #define px_realloc(p, s) repalloc(p, s)
  #define px_free(p)    pfree(p)

  #else

! void       *px_alloc(size_t s);
! void       *px_realloc(void *p, size_t s);
! void        px_free(void *p);
!
  #endif

  /* max len of 'type' parms */

--

pgsql-patches by date:

Previous
From: Marko Kreen
Date:
Subject: [patch 3/6] better error handling
Next
From: Marko Kreen
Date:
Subject: [patch 1/6] remove support for mhash/mcrypt