* 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

Marko Kreen
This commit is contained in:
Neil Conway 2005-03-21 05:18:46 +00:00
parent 3cc8661232
commit fa332a06ec
7 changed files with 75 additions and 149 deletions

View File

@ -35,12 +35,6 @@
#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
@ -600,10 +594,7 @@ _crypt_blowfish_rn(const char *key, const char *setting,
int i;
if (size < 7 + 22 + 31 + 1)
{
__set_errno(ERANGE);
return NULL;
}
if (setting[0] != '$' ||
setting[1] != '2' ||
@ -613,7 +604,6 @@ _crypt_blowfish_rn(const char *key, const char *setting,
setting[5] < '0' || setting[5] > '9' ||
setting[6] != '$')
{
__set_errno(EINVAL);
return NULL;
}
@ -621,7 +611,6 @@ _crypt_blowfish_rn(const char *key, const char *setting,
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);

View File

@ -15,11 +15,6 @@
#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] =
@ -33,7 +28,6 @@ _crypt_gensalt_traditional_rn(unsigned long count,
{
if (output_size > 0)
output[0] = '\0';
__set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
return NULL;
}
@ -57,7 +51,6 @@ _crypt_gensalt_extended_rn(unsigned long count,
{
if (output_size > 0)
output[0] = '\0';
__set_errno((output_size < 1 + 4 + 4 + 1) ? ERANGE : EINVAL);
return NULL;
}
@ -91,7 +84,6 @@ _crypt_gensalt_md5_rn(unsigned long count,
{
if (output_size > 0)
output[0] = '\0';
__set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
return NULL;
}
@ -173,7 +165,6 @@ _crypt_gensalt_blowfish_rn(unsigned long count,
{
if (output_size > 0)
output[0] = '\0';
__set_errno((output_size < 7 + 22 + 1) ? ERANGE : EINVAL);
return NULL;
}

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.14 2004/10/25 02:15:02 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.15 2005/03/21 05:18:45 neilc Exp $
*/
@ -57,22 +57,17 @@
static void init_md5(PX_MD * h);
static void init_sha1(PX_MD * h);
static struct int_digest
struct int_digest
{
char *name;
void (*init) (PX_MD * h);
} int_digest_list[] =
};
{
{
"md5", init_md5
},
{
"sha1", init_sha1
},
{
NULL, NULL
}
static const struct int_digest
int_digest_list[] = {
{ "md5", init_md5 },
{ "sha1", init_sha1 },
{ NULL, NULL }
};
/* MD5 */
@ -516,31 +511,22 @@ bf_cbc_load(void)
return bf_load(MODE_CBC);
}
static struct
struct int_cipher
{
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[] = {
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"},
@ -557,7 +543,7 @@ static PX_Alias int_aliases[] = {
int
px_find_digest(const char *name, PX_MD ** res)
{
struct int_digest *p;
const struct int_digest *p;
PX_MD *h;
for (p = int_digest_list; p->name; p++)

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.14 2005/03/12 06:53:54 neilc Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.15 2005/03/21 05:18:45 neilc Exp $
*/
#include <postgres.h>
@ -208,8 +208,8 @@ gen_ossl_free(PX_Cipher * c)
ossldata *od = (ossldata *) c->ptr;
memset(od, 0, sizeof(*od));
pfree(od);
pfree(c);
px_free(od);
px_free(c);
}
/* Blowfish */
@ -473,37 +473,21 @@ static const struct ossl_cipher ossl_cast_cbc = {
/*
* Special handlers
*/
static const struct
struct ossl_cipher_lookup
{
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
}
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 */
@ -511,38 +495,29 @@ static const struct
int
px_find_cipher(const char *name, PX_Cipher ** res)
{
unsigned i;
PX_Cipher *c = NULL,
*csrc;
const struct ossl_cipher_lookup *i;
PX_Cipher *c = NULL;
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;
for (i = ossl_cipher_types; i->name; i++)
if (!strcmp(i->name, name))
break;
}
}
if (ossl_ciph == NULL)
if (i->name == NULL)
return -1;
od = px_alloc(sizeof(*od));
memset(od, 0, sizeof(*od));
od->ciph = ossl_ciph;
csrc = NULL;
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 = ossl_ciph->init;
c->encrypt = ossl_ciph->encrypt;
c->decrypt = ossl_ciph->decrypt;
c->init = od->ciph->init;
c->encrypt = od->ciph->encrypt;
c->decrypt = od->ciph->decrypt;
c->ptr = od;
*res = c;

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.16 2004/05/07 00:24:57 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.17 2005/03/21 05:18:45 neilc Exp $
*/
#include "postgres.h"
@ -46,7 +46,7 @@ typedef int (*PFN) (const char *name, void **res);
static void *
find_provider(text *name, PFN pf, char *desc, int silent);
/* SQL function: hash(text, text) returns text */
/* SQL function: hash(bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_digest);
Datum
@ -111,7 +111,7 @@ pg_digest_exists(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(true);
}
/* SQL function: hmac(data:text, key:text, type:text) */
/* SQL function: hmac(data:bytea, key:bytea, type:text) returns bytea */
PG_FUNCTION_INFO_V1(pg_hmac);
Datum
@ -316,7 +316,7 @@ pg_crypt(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(res);
}
/* SQL function: pg_encrypt(text, text, text) returns text */
/* SQL function: pg_encrypt(bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_encrypt);
Datum
@ -367,7 +367,7 @@ pg_encrypt(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(res);
}
/* SQL function: pg_decrypt(text, text, text) returns text */
/* SQL function: pg_decrypt(bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_decrypt);
Datum
@ -417,7 +417,7 @@ pg_decrypt(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(res);
}
/* SQL function: pg_encrypt(text, text, text) returns text */
/* SQL function: pg_encrypt_iv(bytea, bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_encrypt_iv);
Datum
@ -473,7 +473,7 @@ pg_encrypt_iv(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(res);
}
/* SQL function: pg_decrypt_iv(text, text, text) returns text */
/* SQL function: pg_decrypt_iv(bytea, bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_decrypt_iv);
Datum
@ -529,7 +529,7 @@ pg_decrypt_iv(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(res);
}
/* SQL function: pg_decrypt(text, text, text) returns text */
/* SQL function: pg_cipher_exists(text) returns bool */
PG_FUNCTION_INFO_V1(pg_cipher_exists);
Datum
@ -550,7 +550,6 @@ pg_cipher_exists(PG_FUNCTION_ARGS)
PG_RETURN_BOOL((c != NULL) ? true : false);
}
static void *
find_provider(text *name,
PFN provider_lookup,

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.8 2004/05/07 00:24:57 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.9 2005/03/21 05:18:45 neilc Exp $
*/
#include <postgres.h>
@ -69,52 +69,41 @@ run_crypt_bf(const char *psw, const char *salt,
return res;
}
static struct
struct px_crypt_algo
{
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
}
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)
{
int i;
const struct px_crypt_algo *c;
for (i = 0; px_crypt_list[i].id; i++)
for (c = px_crypt_list; c->id; c++)
{
if (!px_crypt_list[i].id_len)
if (!c->id_len)
break;
if (!strncmp(salt, px_crypt_list[i].id, px_crypt_list[i].id_len))
if (!strncmp(salt, c->id, c->id_len))
break;
}
if (px_crypt_list[i].crypt == NULL)
if (c->crypt == NULL)
return NULL;
return px_crypt_list[i].crypt(psw, salt, buf, len);
return c->crypt(psw, salt, buf, len);
}
#else /* PX_SYSTEM_CRYPT */
@ -155,7 +144,7 @@ static struct generator gen_list[] = {
{"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}
{NULL, NULL, 0, 0, 0, 0}
};
unsigned

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.9 2003/11/29 22:39:28 pgsql Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.10 2005/03/21 05:18:46 neilc Exp $
*/
#ifndef __PX_H
@ -43,21 +43,18 @@
#endif
#if 1
#ifndef PX_OWN_ALLOC
#define px_alloc(s) palloc(s)
#define px_realloc(p, s) prealloc(p, s)
#define px_realloc(p, s) repalloc(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);
void *px_alloc(size_t s);
void *px_realloc(void *p, size_t s);
void px_free(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 */