Standard pgindent run for 8.1.

This commit is contained in:
Bruce Momjian 2005-10-15 02:49:52 +00:00
parent 790c01d280
commit 1dc3498251
770 changed files with 34334 additions and 32507 deletions

View File

@ -125,10 +125,10 @@ Datum
gbt_cash_penalty(PG_FUNCTION_ARGS)
{
cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
@ -138,8 +138,8 @@ Datum
gbt_cash_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -148,15 +148,15 @@ gbt_date_penalty(PG_FUNCTION_ARGS)
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(newentry->upper),
DateADTGetDatum(origentry->upper)));
DateADTGetDatum(newentry->upper),
DateADTGetDatum(origentry->upper)));
res = Max(diff, 0);
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(origentry->lower),
DateADTGetDatum(newentry->lower)));
DateADTGetDatum(origentry->lower),
DateADTGetDatum(newentry->lower)));
res += Max(diff, 0);
@ -166,8 +166,8 @@ gbt_date_penalty(PG_FUNCTION_ARGS)
{
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(origentry->upper),
DateADTGetDatum(origentry->lower)));
DateADTGetDatum(origentry->upper),
DateADTGetDatum(origentry->lower)));
*result += FLT_MIN;
*result += (float) (res / ((double) (res + diff)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
@ -181,8 +181,8 @@ Datum
gbt_date_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -127,7 +127,7 @@ gbt_float4_penalty(PG_FUNCTION_ARGS)
float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
@ -137,8 +137,8 @@ Datum
gbt_float4_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -129,7 +129,7 @@ gbt_float8_penalty(PG_FUNCTION_ARGS)
float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
@ -139,8 +139,8 @@ Datum
gbt_float8_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -194,8 +194,8 @@ gbt_inet_penalty(PG_FUNCTION_ARGS)
inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
}
@ -204,8 +204,8 @@ Datum
gbt_inet_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -128,10 +128,10 @@ Datum
gbt_int2_penalty(PG_FUNCTION_ARGS)
{
int16KEY *origentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int16KEY *newentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int16KEY *newentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
}
@ -140,8 +140,8 @@ Datum
gbt_int2_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -126,10 +126,10 @@ Datum
gbt_int4_penalty(PG_FUNCTION_ARGS)
{
int32KEY *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
}
@ -138,8 +138,8 @@ Datum
gbt_int4_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -125,11 +125,11 @@ Datum
gbt_int8_penalty(PG_FUNCTION_ARGS)
{
int64KEY *origentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int64KEY *newentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int64KEY *newentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
PG_RETURN_POINTER(result);
}
@ -137,8 +137,8 @@ Datum
gbt_int8_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -63,8 +63,8 @@ gbt_intvkey_cmp(const void *a, const void *b)
{
return DatumGetInt32(
DirectFunctionCall2(interval_cmp,
IntervalPGetDatum(((Nsrt *) a)->t),
IntervalPGetDatum(((Nsrt *) b)->t)
IntervalPGetDatum(((Nsrt *) a)->t),
IntervalPGetDatum(((Nsrt *) b)->t)
)
);
}
@ -78,7 +78,7 @@ intr2num(const Interval *i)
/*
* INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
* in pg_type. This might be less than sizeof(Interval) if the compiler
* in pg_type. This might be less than sizeof(Interval) if the compiler
* insists on adding alignment padding at the end of the struct.
*/
#define INTERVALSIZE 16
@ -202,7 +202,7 @@ gbt_intv_penalty(PG_FUNCTION_ARGS)
inew[0] = intr2num(&newentry->lower);
inew[1] = intr2num(&newentry->upper);
penalty_num(result,iorg[0],iorg[1],inew[0],inew[1]);
penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
PG_RETURN_POINTER(result);
@ -212,8 +212,8 @@ Datum
gbt_intv_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -63,8 +63,8 @@ gbt_macadkey_cmp(const void *a, const void *b)
return DatumGetInt32(
DirectFunctionCall2(
macaddr_cmp,
PointerGetDatum(&((Nsrt *) a)->t[0]),
PointerGetDatum(&((Nsrt *) b)->t[0])
PointerGetDatum(&((Nsrt *) a)->t[0]),
PointerGetDatum(&((Nsrt *) b)->t[0])
)
);
}
@ -157,7 +157,7 @@ gbt_macad_penalty(PG_FUNCTION_ARGS)
inew[0] = mac_2_uint64(&newentry->lower);
inew[1] = mac_2_uint64(&newentry->upper);
penalty_num(result,iorg[0],iorg[1],inew[0],inew[1]);
penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
PG_RETURN_POINTER(result);
@ -167,8 +167,8 @@ Datum
gbt_macad_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -129,7 +129,7 @@ gbt_oid_penalty(PG_FUNCTION_ARGS)
oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
penalty_num(result,origentry->lower,origentry->upper,newentry->lower,newentry->upper);
penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
}
@ -138,8 +138,8 @@ Datum
gbt_oid_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -87,7 +87,7 @@ gbt_text_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
if ( tinfo.eml == 0 )
if (tinfo.eml == 0)
{
tinfo.eml = pg_database_encoding_max_length();
}
@ -102,7 +102,7 @@ gbt_bpchar_compress(PG_FUNCTION_ARGS)
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
if ( tinfo.eml == 0 )
if (tinfo.eml == 0)
{
tinfo.eml = pg_database_encoding_max_length();
}
@ -111,7 +111,7 @@ gbt_bpchar_compress(PG_FUNCTION_ARGS)
{
Datum d = DirectFunctionCall1(rtrim1, entry->key);
GISTENTRY trim;
GISTENTRY trim;
gistentryinit(trim, d,
entry->rel, entry->page,
@ -136,7 +136,7 @@ gbt_text_consistent(PG_FUNCTION_ARGS)
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable(key);
if ( tinfo.eml == 0 )
if (tinfo.eml == 0)
{
tinfo.eml = pg_database_encoding_max_length();
}
@ -158,7 +158,7 @@ gbt_bpchar_consistent(PG_FUNCTION_ARGS)
bool retval;
GBT_VARKEY_R r = gbt_var_key_readable(key);
if ( tinfo.eml == 0 )
if (tinfo.eml == 0)
{
tinfo.eml = pg_database_encoding_max_length();
}

View File

@ -36,7 +36,7 @@ static bool
gbt_timegt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_gt, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(time_gt, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -44,7 +44,7 @@ static bool
gbt_timege(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_ge, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(time_ge, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -52,7 +52,7 @@ static bool
gbt_timeeq(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_eq, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(time_eq, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -60,7 +60,7 @@ static bool
gbt_timele(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_le, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(time_le, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -68,7 +68,7 @@ static bool
gbt_timelt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_lt, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(time_lt, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -212,15 +212,15 @@ gbt_time_penalty(PG_FUNCTION_ARGS)
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
P_TimeADTGetDatum(newentry->upper),
P_TimeADTGetDatum(origentry->upper)));
P_TimeADTGetDatum(newentry->upper),
P_TimeADTGetDatum(origentry->upper)));
res = INTERVAL_TO_SEC(intr);
res = Max(res, 0);
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
P_TimeADTGetDatum(origentry->lower),
P_TimeADTGetDatum(newentry->lower)));
P_TimeADTGetDatum(origentry->lower),
P_TimeADTGetDatum(newentry->lower)));
res2 = INTERVAL_TO_SEC(intr);
res2 = Max(res2, 0);
@ -232,8 +232,8 @@ gbt_time_penalty(PG_FUNCTION_ARGS)
{
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
P_TimeADTGetDatum(origentry->upper),
P_TimeADTGetDatum(origentry->lower)));
P_TimeADTGetDatum(origentry->upper),
P_TimeADTGetDatum(origentry->lower)));
*result += FLT_MIN;
*result += (float) (res / (res + INTERVAL_TO_SEC(intr)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
@ -247,8 +247,8 @@ Datum
gbt_time_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -32,13 +32,13 @@ Datum gbt_ts_penalty(PG_FUNCTION_ARGS);
Datum gbt_ts_same(PG_FUNCTION_ARGS);
#define P_TimestampGetDatum(x) PointerGetDatum( &(x) )
#define P_TimestampGetDatum(x) PointerGetDatum( &(x) )
static bool
gbt_tsgt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_gt, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(timestamp_gt, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -46,7 +46,7 @@ static bool
gbt_tsge(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_ge, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(timestamp_ge, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -54,7 +54,7 @@ static bool
gbt_tseq(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_eq, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(timestamp_eq, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -62,7 +62,7 @@ static bool
gbt_tsle(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_le, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(timestamp_le, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -70,7 +70,7 @@ static bool
gbt_tslt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_lt, PointerGetDatum(a), PointerGetDatum(b))
DirectFunctionCall2(timestamp_lt, PointerGetDatum(a), PointerGetDatum(b))
);
}
@ -114,7 +114,7 @@ tstz_to_ts_gmt(Timestamp *gmt, TimestampTz *ts)
*gmt = *ts;
DecodeSpecial(0, "gmt", &val);
if ( *ts < DT_NOEND && *ts > DT_NOBEGIN )
if (*ts < DT_NOEND && *ts > DT_NOBEGIN)
{
tz = val * 60;
@ -217,10 +217,10 @@ gbt_ts_union(PG_FUNCTION_ARGS)
#define penalty_check_max_float(val) do { \
if ( val > FLT_MAX ) \
val = FLT_MAX; \
if ( val < -FLT_MAX ) \
val = -FLT_MAX; \
if ( val > FLT_MAX ) \
val = FLT_MAX; \
if ( val < -FLT_MAX ) \
val = -FLT_MAX; \
} while(false);
@ -232,24 +232,24 @@ gbt_ts_penalty(PG_FUNCTION_ARGS)
tsKEY *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
double orgdbl[2],
newdbl[2];
double orgdbl[2],
newdbl[2];
/*
We are allways using "double" timestamps here.
Precision should be good enough.
*/
orgdbl[0] = ( (double) origentry->lower ) ;
orgdbl[1] = ( (double) origentry->upper ) ;
newdbl[0] = ( (double) newentry->lower ) ;
newdbl[1] = ( (double) newentry->upper ) ;
* We are allways using "double" timestamps here. Precision should be good
* enough.
*/
orgdbl[0] = ((double) origentry->lower);
orgdbl[1] = ((double) origentry->upper);
newdbl[0] = ((double) newentry->lower);
newdbl[1] = ((double) newentry->upper);
penalty_check_max_float( orgdbl[0] );
penalty_check_max_float( orgdbl[1] );
penalty_check_max_float( newdbl[0] );
penalty_check_max_float( newdbl[1] );
penalty_check_max_float(orgdbl[0]);
penalty_check_max_float(orgdbl[1]);
penalty_check_max_float(newdbl[0]);
penalty_check_max_float(newdbl[1]);
penalty_num(result,orgdbl[0],orgdbl[1],newdbl[0],newdbl[1]);
penalty_num(result, orgdbl[0], orgdbl[1], newdbl[0], newdbl[1]);
PG_RETURN_POINTER(result);
@ -260,8 +260,8 @@ Datum
gbt_ts_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}

View File

@ -48,17 +48,17 @@ typedef struct
* Note: The factor 0.49 in following macro avoids floating point overflows
*/
#define penalty_num(result,olower,oupper,nlower,nupper) do { \
double tmp = 0.0F; \
(*(result)) = 0.0F; \
double tmp = 0.0F; \
(*(result)) = 0.0F; \
if ( (nupper) > (oupper) ) \
tmp += ( ((double)nupper)*0.49F - ((double)oupper)*0.49F ); \
if ( (olower) > (nlower) ) \
tmp += ( ((double)olower)*0.49F - ((double)nlower)*0.49F ); \
if (tmp > 0.0F) \
{ \
(*(result)) += FLT_MIN; \
(*(result)) += (float) ( ((double)(tmp)) / ( (double)(tmp) + ( ((double)(oupper))*0.49F - ((double)(olower))*0.49F ) ) ); \
(*(result)) *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1)); \
(*(result)) += FLT_MIN; \
(*(result)) += (float) ( ((double)(tmp)) / ( (double)(tmp) + ( ((double)(oupper))*0.49F - ((double)(olower))*0.49F ) ) ); \
(*(result)) *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1)); \
} \
} while (0);

View File

@ -4,14 +4,14 @@
#include "utils/builtins.h"
PG_FUNCTION_INFO_V1(gbt_var_decompress);
Datum gbt_var_decompress(PG_FUNCTION_ARGS);
Datum gbt_var_decompress(PG_FUNCTION_ARGS);
Datum
gbt_var_decompress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
{
@ -92,45 +92,47 @@ static int32
gbt_var_node_cp_len(const GBT_VARKEY * node, const gbtree_vinfo * tinfo)
{
GBT_VARKEY_R r = gbt_var_key_readable(node);
int32 i = 0;
int32 l = 0;
int32 t1len = VARSIZE(r.lower) - VARHDRSZ ;
int32 t2len = VARSIZE(r.upper) - VARHDRSZ ;
int32 ml = Min(t1len, t2len);
GBT_VARKEY_R r = gbt_var_key_readable(node);
int32 i = 0;
int32 l = 0;
int32 t1len = VARSIZE(r.lower) - VARHDRSZ;
int32 t2len = VARSIZE(r.upper) - VARHDRSZ;
int32 ml = Min(t1len, t2len);
char *p1 = VARDATA(r.lower);
char *p2 = VARDATA(r.upper);
char *p1 = VARDATA(r.lower);
char *p2 = VARDATA(r.upper);
if ( ml == 0 )
return 0;
if (ml == 0)
return 0;
while ( i < ml )
while (i < ml)
{
if ( tinfo->eml > 1 && l == 0 )
{
if (tinfo->eml > 1 && l == 0)
{
if ( ( l = pg_mblen(p1) ) != pg_mblen(p2) )
{
return i;
}
}
if (*p1 != *p2)
{
if( tinfo->eml > 1 )
{
return (i-l+1);
} else {
return i;
}
}
if ((l = pg_mblen(p1)) != pg_mblen(p2))
{
return i;
}
}
if (*p1 != *p2)
{
if (tinfo->eml > 1)
{
return (i - l + 1);
}
else
{
return i;
}
}
p1++;
p2++;
l--;
i++;
p1++;
p2++;
l--;
i++;
}
return (ml); /* lower == upper */
return (ml); /* lower == upper */
}
@ -141,35 +143,37 @@ static bool
gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo * tinfo)
{
bool out = FALSE;
int32 k = 0;
int32 qlen = VARSIZE(query) - VARHDRSZ ;
int32 nlen = VARSIZE(pf) - VARHDRSZ ;
bool out = FALSE;
int32 k = 0;
int32 qlen = VARSIZE(query) - VARHDRSZ;
int32 nlen = VARSIZE(pf) - VARHDRSZ;
if (nlen <= qlen)
{
char *q = VARDATA(query);
char *n = VARDATA(pf);
char *q = VARDATA(query);
char *n = VARDATA(pf);
if ( tinfo->eml > 1 )
{
out = ( varstr_cmp(q, nlen, n, nlen) == 0 );
} else {
out = TRUE;
for (k = 0; k < nlen; k++)
{
if (*n != *q)
{
out = FALSE;
break;
}
if (k < (nlen - 1))
{
q++;
n++;
}
}
}
if (tinfo->eml > 1)
{
out = (varstr_cmp(q, nlen, n, nlen) == 0);
}
else
{
out = TRUE;
for (k = 0; k < nlen; k++)
{
if (*n != *q)
{
out = FALSE;
break;
}
if (k < (nlen - 1))
{
q++;
n++;
}
}
}
}
return out;
@ -184,10 +188,10 @@ static bool
gbt_var_node_pf_match(const GBT_VARKEY_R * node, const bytea *query, const gbtree_vinfo * tinfo)
{
return ( tinfo->trnc && (
gbt_bytea_pf_match(node->lower, query, tinfo) ||
gbt_bytea_pf_match(node->upper, query, tinfo)
) );
return (tinfo->trnc && (
gbt_bytea_pf_match(node->lower, query, tinfo) ||
gbt_bytea_pf_match(node->upper, query, tinfo)
));
}
@ -201,18 +205,18 @@ gbt_var_node_truncate(const GBT_VARKEY * node, int32 cpf_length, const gbtree_vi
{
GBT_VARKEY *out = NULL;
GBT_VARKEY_R r = gbt_var_key_readable(node);
int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
int32 si = 0;
int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
int32 si = 0;
len1 = Min(len1,(cpf_length + 1));
len2 = Min(len2,(cpf_length + 1));
len1 = Min(len1, (cpf_length + 1));
len2 = Min(len2, (cpf_length + 1));
si = 2 * VARHDRSZ + INTALIGN(VARHDRSZ + len1) + len2;
out = (GBT_VARKEY *) palloc(si);
out->vl_len = si;
memcpy((void *) &(((char *) out)[VARHDRSZ]), (void *) r.lower, len1 + VARHDRSZ );
memcpy((void *) &(((char *) out)[VARHDRSZ + INTALIGN(VARHDRSZ + len1)]), (void *) r.upper, len2 + VARHDRSZ );
memcpy((void *) &(((char *) out)[VARHDRSZ]), (void *) r.lower, len1 + VARHDRSZ);
memcpy((void *) &(((char *) out)[VARHDRSZ + INTALIGN(VARHDRSZ + len1)]), (void *) r.upper, len2 + VARHDRSZ);
*((int32 *) &(((char *) out)[VARHDRSZ])) = len1 + VARHDRSZ;
*((int32 *) &(((char *) out)[VARHDRSZ + INTALIGN(VARHDRSZ + len1)])) = len2 + VARHDRSZ;
@ -568,8 +572,8 @@ gbt_var_consistent(
else
retval = (
(
(*tinfo->f_cmp) (key->lower, (bytea *) query) <= 0 &&
(*tinfo->f_cmp) ((bytea *) query, (void *) key->upper) <= 0
(*tinfo->f_cmp) (key->lower, (bytea *) query) <= 0 &&
(*tinfo->f_cmp) ((bytea *) query, (void *) key->upper) <= 0
) || gbt_var_node_pf_match(key, query, tinfo)
);
break;

View File

@ -28,7 +28,8 @@ typedef struct
/* Attribs */
enum gbtree_type t; /* data type */
int32 eml; /* cached pg_database_encoding_max_length (0: undefined) */
int32 eml; /* cached pg_database_encoding_max_length (0:
* undefined) */
bool trnc; /* truncate (=compress) key */
/* Methods */

View File

@ -4,7 +4,7 @@
* darcy@druid.net
* http://www.druid.net/darcy/
*
* $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.13 2005/01/29 22:35:01 tgl Exp $
* $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.14 2005/10/15 02:49:04 momjian Exp $
* best viewed with tabs set to 4
*/
@ -90,8 +90,8 @@ chkpass_in(PG_FUNCTION_ARGS)
mysalt[0] = salt_chars[random() & 0x3f];
mysalt[1] = salt_chars[random() & 0x3f];
mysalt[2] = 0; /* technically the terminator is not
* necessary but I like to play safe */
mysalt[2] = 0; /* technically the terminator is not necessary
* but I like to play safe */
strcpy(result->password, crypt(str, mysalt));
PG_RETURN_POINTER(result);
}

View File

@ -115,7 +115,7 @@ NDBOX *
cube(text *str)
{
return cube_in(DatumGetCString(DirectFunctionCall1(textout,
PointerGetDatum(str))));
PointerGetDatum(str))));
}
char *
@ -219,7 +219,7 @@ g_cube_union(GistEntryVector *entryvec, int *sizep)
for (i = 1; i < entryvec->n; i++)
{
out = g_cube_binary_union(tmp, (NDBOX *)
DatumGetPointer(entryvec->vector[i].key),
DatumGetPointer(entryvec->vector[i].key),
sizep);
tmp = out;
}
@ -329,8 +329,7 @@ g_cube_picksplit(GistEntryVector *entryvec,
size_waste = size_union - size_inter;
/*
* are these a more promising split than what we've already
* seen?
* are these a more promising split than what we've already seen?
*/
if (size_waste > waste || firsttime)
@ -356,24 +355,24 @@ g_cube_picksplit(GistEntryVector *entryvec,
rt_cube_size(datum_r, &size_r);
/*
* Now split up the regions between the two seeds. An important
* property of this split algorithm is that the split vector v has the
* indices of items to be split in order in its left and right
* vectors. We exploit this property by doing a merge in the code
* that actually splits the page.
* Now split up the regions between the two seeds. An important property
* of this split algorithm is that the split vector v has the indices of
* items to be split in order in its left and right vectors. We exploit
* this property by doing a merge in the code that actually splits the
* page.
*
* For efficiency, we also place the new index tuple in this loop. This
* is handled at the very end, when we have placed all the existing
* tuples and i == maxoff + 1.
* For efficiency, we also place the new index tuple in this loop. This is
* handled at the very end, when we have placed all the existing tuples
* and i == maxoff + 1.
*/
maxoff = OffsetNumberNext(maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
/*
* If we've already decided where to place this item, just put it
* on the right list. Otherwise, we need to figure out which page
* needs the least enlargement in order to store the item.
* If we've already decided where to place this item, just put it on
* the right list. Otherwise, we need to figure out which page needs
* the least enlargement in order to store the item.
*/
if (i == seed_1)
@ -542,8 +541,8 @@ cube_union(NDBOX * a, NDBOX * b)
}
/*
* use the potentially smaller of the two boxes (b) to fill in the
* result, padding absent dimensions with zeroes
* use the potentially smaller of the two boxes (b) to fill in the result,
* padding absent dimensions with zeroes
*/
for (i = 0; i < b->dim; i++)
{
@ -562,7 +561,7 @@ cube_union(NDBOX * a, NDBOX * b)
result->x[i] =
Min(Min(a->x[i], a->x[i + a->dim]), result->x[i]);
result->x[i + a->dim] = Max(Max(a->x[i],
a->x[i + a->dim]), result->x[i + a->dim]);
a->x[i + a->dim]), result->x[i + a->dim]);
}
return (result);
@ -620,12 +619,11 @@ cube_inter(NDBOX * a, NDBOX * b)
result->x[i] =
Max(Min(a->x[i], a->x[i + a->dim]), result->x[i]);
result->x[i + a->dim] = Min(Max(a->x[i],
a->x[i + a->dim]), result->x[i + a->dim]);
a->x[i + a->dim]), result->x[i + a->dim]);
}
/*
* Is it OK to return a non-null intersection for non-overlapping
* boxes?
* Is it OK to return a non-null intersection for non-overlapping boxes?
*/
return (result);
}
@ -713,8 +711,8 @@ cube_cmp(NDBOX * a, NDBOX * b)
}
/*
* if all common dimensions are equal, the cube with more
* dimensions wins
* if all common dimensions are equal, the cube with more dimensions
* wins
*/
return 1;
}
@ -736,8 +734,8 @@ cube_cmp(NDBOX * a, NDBOX * b)
}
/*
* if all common dimensions are equal, the cube with more
* dimensions wins
* if all common dimensions are equal, the cube with more dimensions
* wins
*/
return -1;
}
@ -797,10 +795,9 @@ cube_contains(NDBOX * a, NDBOX * b)
if (a->dim < b->dim)
{
/*
* the further comparisons will make sense if the excess
* dimensions of (b) were zeroes Since both UL and UR coordinates
* must be zero, we can check them all without worrying about
* which is which.
* the further comparisons will make sense if the excess dimensions of
* (b) were zeroes Since both UL and UR coordinates must be zero, we
* can check them all without worrying about which is which.
*/
for (i = a->dim; i < b->dim; i++)
{

View File

@ -88,10 +88,10 @@ dbf_open(char *file, int flags)
dbh->db_nfields = (dbh->db_hlen - sizeof(dbf_header)) / sizeof(dbf_field);
/*
* dbh->db_hlen - sizeof(dbf_header) isn't the correct size, cos
* dbh->hlen is in fact a little more cos of the 0x0D (and possibly
* another byte, 0x4E, I have seen this somewhere). Because of
* rounding everything turns out right :)
* dbh->db_hlen - sizeof(dbf_header) isn't the correct size, cos dbh->hlen
* is in fact a little more cos of the 0x0D (and possibly another byte,
* 0x4E, I have seen this somewhere). Because of rounding everything turns
* out right :)
*/
if ((fields = (f_descr *) calloc(dbh->db_nfields, sizeof(f_descr)))
@ -155,8 +155,7 @@ dbf_write_head(dbhead * dbh)
now = time((time_t *) NULL);
dbf_time = localtime(&now);
head.dbh_year = dbf_time->tm_year;
head.dbh_month = dbf_time->tm_mon + 1; /* Months since January +
* 1 */
head.dbh_month = dbf_time->tm_mon + 1; /* Months since January + 1 */
head.dbh_day = dbf_time->tm_mday;
put_long(head.dbh_records, dbh->db_records);

View File

@ -9,7 +9,7 @@
#define _DBF_H
#ifdef _WIN32
#include <gmon.h> /* we need it to define u_char type */
#include <gmon.h> /* we need it to define u_char type */
#endif
#include <sys/types.h>
@ -64,8 +64,7 @@ typedef struct
u_char dbf_type; /* field-type */
u_char dbf_reserved[4]; /* some reserved stuff */
u_char dbf_flen; /* field-length */
u_char dbf_dec; /* number of decimal positions if type is
* 'N' */
u_char dbf_dec; /* number of decimal positions if type is 'N' */
u_char dbf_stub[14]; /* stuff we don't need */
} dbf_field;
@ -89,15 +88,14 @@ typedef struct
u_char db_year; /* last update as YYMMDD */
u_char db_month;
u_char db_day;
u_long db_hlen; /* length of the diskheader, for
* calculating the offsets */
u_long db_hlen; /* length of the diskheader, for calculating
* the offsets */
u_long db_records; /* number of records */
u_long db_currec; /* current record-number starting at 0 */
u_short db_rlen; /* length of the record */
u_char db_nfields; /* number of fields */
u_char *db_buff; /* record-buffer to save malloc()'s */
f_descr *db_fields; /* pointer to an array of field-
* descriptions */
f_descr *db_fields; /* pointer to an array of field- descriptions */
} dbhead;
/* structure that contains everything a user wants from a field, including

View File

@ -169,7 +169,7 @@ usage(void)
{
printf("dbf2pg\n"
"usage: dbf2pg [-u | -l] [-h hostname] [-W] [-U username]\n"
" [-B transaction_size] [-F charset_from [-T charset_to]]\n"
" [-B transaction_size] [-F charset_from [-T charset_to]]\n"
" [-s oldname=[newname][,oldname=[newname][...]]] [-d dbase]\n"
" [-t table] [-c | -D] [-f] [-v[v]] dbf-file\n");
}
@ -251,7 +251,7 @@ do_create(PGconn *conn, char *table, dbhead * dbh)
printf("Building CREATE-clause\n");
if (!(query = (char *) malloc(
(dbh->db_nfields * 40) + 29 + strlen(table))))
(dbh->db_nfields * 40) + 29 + strlen(table))))
{
fprintf(stderr, "Memory allocation error in function do_create\n");
PQfinish(conn);
@ -359,9 +359,8 @@ do_inserts(PGconn *conn, char *table, dbhead * dbh)
}
/*
* make sure we can build the COPY query, note that we don't need to
* just add this value, since the COPY query is a separate query (see
* below)
* make sure we can build the COPY query, note that we don't need to just
* add this value, since the COPY query is a separate query (see below)
*/
if (h < 17 + strlen(table))
h = 17 + strlen(table);
@ -370,7 +369,7 @@ do_inserts(PGconn *conn, char *table, dbhead * dbh)
{
PQfinish(conn);
fprintf(stderr,
"Memory allocation error in function do_inserts (query)\n");
"Memory allocation error in function do_inserts (query)\n");
close(dbh->db_fd);
free(dbh);
exit(1);
@ -386,12 +385,10 @@ do_inserts(PGconn *conn, char *table, dbhead * dbh)
exit(1);
}
if (end == 0) /* "end" is a user option, if not
* specified, */
if (end == 0) /* "end" is a user option, if not specified, */
end = dbh->db_records; /* then all records are processed. */
if (t_block == 0) /* user not specified transaction block
* size */
if (t_block == 0) /* user not specified transaction block size */
t_block = end - begin; /* then we set it to be the full data */
for (i = begin; i < end; i++)
@ -426,9 +423,8 @@ do_inserts(PGconn *conn, char *table, dbhead * dbh)
j = 0; /* counter for fields in the output */
for (h = 0; h < dbh->db_nfields; h++)
{
if (!strlen(fields[h].db_name)) /* When the new fieldname
* is empty, the field is
* skipped */
if (!strlen(fields[h].db_name)) /* When the new fieldname is
* empty, the field is skipped */
continue;
else
j++;
@ -639,8 +635,8 @@ main(int argc, char **argv)
usage();
/*
* FIXME: Ivan thinks this is bad: printf("unknown
* argument: %s\n", argv[0]);
* FIXME: Ivan thinks this is bad: printf("unknown argument:
* %s\n", argv[0]);
*/
exit(1);
break;

View File

@ -60,10 +60,9 @@
typedef struct remoteConn
{
PGconn *conn; /* Hold the remote connection */
int autoXactCursors; /* Indicates the number of open cursors,
* non-zero means we opened the xact
* ourselves */
PGconn *conn; /* Hold the remote connection */
int autoXactCursors;/* Indicates the number of open cursors,
* non-zero means we opened the xact ourselves */
} remoteConn;
/*
@ -71,7 +70,7 @@ typedef struct remoteConn
*/
static remoteConn *getConnectionByName(const char *name);
static HTAB *createConnHash(void);
static void createNewConnection(const char *name, remoteConn *rconn);
static void createNewConnection(const char *name, remoteConn * rconn);
static void deleteConnection(const char *name);
static char **get_pkey_attnames(Oid relid, int16 *numatts);
static char *get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals);
@ -224,9 +223,9 @@ dblink_connect(PG_FUNCTION_ARGS)
pfree(rconn);
ereport(ERROR,
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
errmsg("could not establish connection"),
errdetail("%s", msg)));
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
errmsg("could not establish connection"),
errdetail("%s", msg)));
}
if (connname)
@ -514,8 +513,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function
* calls
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@ -664,8 +662,7 @@ dblink_record(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function
* calls
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@ -730,8 +727,8 @@ dblink_record(PG_FUNCTION_ARGS)
TEXTOID, -1, 0);
/*
* and save a copy of the command status string to return as
* our result tuple
* and save a copy of the command status string to return as our
* result tuple
*/
sql_cmd_status = PQcmdStatus(res);
funcctx->max_calls = 1;
@ -766,8 +763,8 @@ dblink_record(PG_FUNCTION_ARGS)
/* failed to determine actual type of RECORD */
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
errmsg("function returning record called in context "
"that cannot accept type record")));
break;
default:
/* result type isn't composite */
@ -935,8 +932,8 @@ dblink_exec(PG_FUNCTION_ARGS)
{
PQclear(res);
ereport(ERROR,
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
errmsg("statement returning results not allowed")));
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
errmsg("statement returning results not allowed")));
}
/* if needed, close the connection to the database and cleanup */
@ -975,8 +972,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function
* calls
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@ -989,8 +985,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
GET_STR(PG_GETARG_TEXT_P(0)))));
/*
* need a tuple descriptor representing one INT and one TEXT
* column
* need a tuple descriptor representing one INT and one TEXT column
*/
tupdesc = CreateTemplateTupleDesc(2, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "position",
@ -999,8 +994,8 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
TEXTOID, -1, 0);
/*
* Generate attribute metadata needed later to produce tuples from
* raw C strings
* Generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
@ -1145,8 +1140,8 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
/*
* Source array is made up of key values that will be used to locate
* the tuple of interest from the local system.
* Source array is made up of key values that will be used to locate the
* tuple of interest from the local system.
*/
src_ndim = ARR_NDIM(src_pkattvals_arry);
src_dim = ARR_DIMS(src_pkattvals_arry);
@ -1158,8 +1153,8 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
if (src_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("source key array length must match number of key " \
"attributes")));
errmsg("source key array length must match number of key " \
"attributes")));
/*
* get array of pointers to c-strings from the input source array
@ -1178,8 +1173,8 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
}
/*
* Target array is made up of key values that will be used to build
* the SQL string for use on the remote system.
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
*/
tgt_ndim = ARR_NDIM(tgt_pkattvals_arry);
tgt_dim = ARR_DIMS(tgt_pkattvals_arry);
@ -1191,8 +1186,8 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
if (tgt_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("target key array length must match number of key " \
"attributes")));
errmsg("target key array length must match number of key " \
"attributes")));
/*
* get array of pointers to c-strings from the input target array
@ -1291,8 +1286,8 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
/*
* Target array is made up of key values that will be used to build
* the SQL string for use on the remote system.
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
*/
tgt_ndim = ARR_NDIM(tgt_pkattvals_arry);
tgt_dim = ARR_DIMS(tgt_pkattvals_arry);
@ -1304,8 +1299,8 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
if (tgt_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("target key array length must match number of key " \
"attributes")));
errmsg("target key array length must match number of key " \
"attributes")));
/*
* get array of pointers to c-strings from the input target array
@ -1414,8 +1409,8 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
/*
* Source array is made up of key values that will be used to locate
* the tuple of interest from the local system.
* Source array is made up of key values that will be used to locate the
* tuple of interest from the local system.
*/
src_ndim = ARR_NDIM(src_pkattvals_arry);
src_dim = ARR_DIMS(src_pkattvals_arry);
@ -1427,8 +1422,8 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
if (src_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("source key array length must match number of key " \
"attributes")));
errmsg("source key array length must match number of key " \
"attributes")));
/*
* get array of pointers to c-strings from the input source array
@ -1447,8 +1442,8 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
}
/*
* Target array is made up of key values that will be used to build
* the SQL string for use on the remote system.
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
*/
tgt_ndim = ARR_NDIM(tgt_pkattvals_arry);
tgt_dim = ARR_DIMS(tgt_pkattvals_arry);
@ -1460,8 +1455,8 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
if (tgt_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("target key array length must match number of key " \
"attributes")));
errmsg("target key array length must match number of key " \
"attributes")));
/*
* get array of pointers to c-strings from the input target array
@ -1610,7 +1605,7 @@ get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
appendStringInfo(str, ",");
appendStringInfo(str, "%s",
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
needComma = true;
}
@ -1688,7 +1683,7 @@ get_sql_delete(Oid relid, int2vector *pkattnums, int16 pknumatts, char **tgt_pka
appendStringInfo(str, " AND ");
appendStringInfo(str, "%s",
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
if (tgt_pkattvals != NULL)
val = pstrdup(tgt_pkattvals[i]);
@ -1756,7 +1751,7 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
appendStringInfo(str, ", ");
appendStringInfo(str, "%s = ",
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
if (tgt_pkattvals != NULL)
key = get_attnum_pk_pos(pkattnums, pknumatts, i + 1);
@ -1788,7 +1783,7 @@ get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pka
appendStringInfo(str, " AND ");
appendStringInfo(str, "%s",
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
if (tgt_pkattvals != NULL)
val = pstrdup(tgt_pkattvals[i]);
@ -1894,8 +1889,8 @@ get_tuple_of_interest(Oid relid, int2vector *pkattnums, int16 pknumatts, char **
elog(ERROR, "SPI connect failure - returned %d", ret);
/*
* Build sql statement to look up tuple of interest Use src_pkattvals
* as the criteria.
* Build sql statement to look up tuple of interest Use src_pkattvals as
* the criteria.
*/
appendStringInfo(str, "SELECT * FROM %s WHERE ", relname);
@ -1907,7 +1902,7 @@ get_tuple_of_interest(Oid relid, int2vector *pkattnums, int16 pknumatts, char **
appendStringInfo(str, " AND ");
appendStringInfo(str, "%s",
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
val = pstrdup(src_pkattvals[i]);
if (val != NULL)
@ -2045,7 +2040,7 @@ createConnHash(void)
}
static void
createNewConnection(const char *name, remoteConn *rconn)
createNewConnection(const char *name, remoteConn * rconn)
{
remoteConnHashEnt *hentry;
bool found;

View File

@ -1,7 +1,7 @@
/****************************************************************************
* pending.c
* $Id: pending.c,v 1.22 2005/10/02 23:50:05 tgl Exp $
* $PostgreSQL: pgsql/contrib/dbmirror/pending.c,v 1.22 2005/10/02 23:50:05 tgl Exp $
* $Id: pending.c,v 1.23 2005/10/15 02:49:04 momjian Exp $
* $PostgreSQL: pgsql/contrib/dbmirror/pending.c,v 1.23 2005/10/15 02:49:04 momjian Exp $
*
* This file contains a trigger for Postgresql-7.x to record changes to tables
* to a pending table for mirroring.
@ -115,7 +115,7 @@ recordchange(PG_FUNCTION_ARGS)
if (SPI_connect() < 0)
{
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE),
errmsg("dbmirror:recordchange could not connect to SPI")));
errmsg("dbmirror:recordchange could not connect to SPI")));
return -1;
}
trigdata = (TriggerData *) fcinfo->context;
@ -155,7 +155,7 @@ recordchange(PG_FUNCTION_ARGS)
else
{
ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("dbmirror:recordchange Unknown operation")));
errmsg("dbmirror:recordchange Unknown operation")));
}
@ -219,7 +219,7 @@ storePending(char *cpTableName, HeapTuple tBeforeTuple,
vpPlan = SPI_prepare(cpQueryBase, 3, taPlanArgTypes);
if (vpPlan == NULL)
ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("dbmirror:storePending error creating plan")));
errmsg("dbmirror:storePending error creating plan")));
saPlanData[0] = PointerGetDatum(cpTableName);
@ -662,7 +662,7 @@ saveSequenceUpdate(Oid relid, int64 nextValue, bool iscalled)
if (SPI_connect() < 0)
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:savesequenceupdate could not connect to SPI")));
errmsg("dbmirror:savesequenceupdate could not connect to SPI")));
insertPlan = SPI_prepare(insertQuery, 2, insertArgTypes);
insertDataPlan = SPI_prepare(insertDataQuery, 1, insertDataArgTypes);

View File

@ -67,7 +67,7 @@ geo_distance(Point *pt1, Point *pt2)
longdiff = TWO_PI - longdiff;
sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
if (sino > 1.)
sino = 1.;
*resultp = 2. * EARTH_RADIUS * asin(sino);

View File

@ -339,8 +339,8 @@ fti(PG_FUNCTION_ARGS)
ret = SPI_execp(*(plan->splan), values, NULL, 0);
if (ret != SPI_OK_INSERT)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("error executing insert")));
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("error executing insert")));
}
pfree(buff);
pfree(data);
@ -367,9 +367,8 @@ breakup(char *string, char *substring)
while (cur_pos > string) /* don't read before start of 'string' */
{
/*
* skip pieces at the end of a string that are not alfa-numeric
* (ie. 'string$%^&', last_start first points to '&', and after
* this to 'g'
* skip pieces at the end of a string that are not alfa-numeric (ie.
* 'string$%^&', last_start first points to '&', and after this to 'g'
*/
if (!isalnum((unsigned char) *last_start))
{
@ -379,8 +378,7 @@ breakup(char *string, char *substring)
cur_pos = last_start;
}
cur_pos--; /* substrings are at minimum 2 characters
* long */
cur_pos--; /* substrings are at minimum 2 characters long */
if (isalnum((unsigned char) *cur_pos))
{

View File

@ -48,8 +48,8 @@
/*
* $Revision: 1.5 $
* $Id: dmetaphone.c,v 1.5 2005/09/30 22:38:44 momjian Exp $
* $Revision: 1.6 $
* $Id: dmetaphone.c,v 1.6 2005/10/15 02:49:05 momjian Exp $
*/
@ -154,10 +154,10 @@ dmetaphone(PG_FUNCTION_ARGS)
alen = VARSIZE(arg) - VARHDRSZ;
/*
* Postgres' string values might not have trailing nuls. The VARSIZE
* will not include the nul in any case so we copy things out and add
* a trailing nul. When we copy back we ignore the nul (and we don't
* make space for it).
* Postgres' string values might not have trailing nuls. The VARSIZE will
* not include the nul in any case so we copy things out and add a
* trailing nul. When we copy back we ignore the nul (and we don't make
* space for it).
*/
aptr = palloc(alen + 1);
@ -236,7 +236,6 @@ dmetaphone_alt(PG_FUNCTION_ARGS)
*/
#define META_FREE(x) /* pfree((x)) */
#else /* not defined DMETAPHONE_MAIN */
/* use the standard malloc library when not running in PostgreSQL */
@ -512,8 +511,8 @@ DoubleMetaphone(char *str, char **codes)
&& StringAt(original, (current - 1), 3, "ACH", "")
&& ((GetAt(original, current + 2) != 'I')
&& ((GetAt(original, current + 2) != 'E')
|| StringAt(original, (current - 2), 6, "BACHER",
"MACHER", ""))))
|| StringAt(original, (current - 2), 6, "BACHER",
"MACHER", ""))))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
@ -582,9 +581,9 @@ DoubleMetaphone(char *str, char **codes)
/*
* e.g., 'wachtler', 'wechsler', but not 'tichner'
*/
&& StringAt(original, (current + 2), 1, "L", "R",
"N", "M", "B", "H", "F", "V", "W",
" ", "")))
&& StringAt(original, (current + 2), 1, "L", "R",
"N", "M", "B", "H", "F", "V", "W",
" ", "")))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
@ -639,14 +638,14 @@ DoubleMetaphone(char *str, char **codes)
{
/* 'bellocchio' but not 'bacchus' */
if (StringAt(original, (current + 2), 1, "I", "E", "H", "")
&& !StringAt(original, (current + 2), 2, "HU", ""))
&& !StringAt(original, (current + 2), 2, "HU", ""))
{
/* 'accident', 'accede' 'succeed' */
if (
((current == 1)
&& (GetAt(original, current - 1) == 'A'))
|| StringAt(original, (current - 1), 5, "UCCEE",
"UCCES", ""))
|| StringAt(original, (current - 1), 5, "UCCEE",
"UCCES", ""))
{
MetaphAdd(primary, "KS");
MetaphAdd(secondary, "KS");
@ -787,8 +786,8 @@ DoubleMetaphone(char *str, char **codes)
}
/*
* Parker's rule (with some further refinements) -
* e.g., 'hugh'
* Parker's rule (with some further refinements) - e.g.,
* 'hugh'
*/
if (
((current > 1)
@ -873,9 +872,9 @@ DoubleMetaphone(char *str, char **codes)
/* -ges-,-gep-,-gel-, -gie- at beginning */
if ((current == 0)
&& ((GetAt(original, current + 1) == 'Y')
|| StringAt(original, (current + 1), 2, "ES", "EP",
"EB", "EL", "EY", "IB", "IL", "IN", "IE",
"EI", "ER", "")))
|| StringAt(original, (current + 1), 2, "ES", "EP",
"EB", "EL", "EY", "IB", "IL", "IN", "IE",
"EI", "ER", "")))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "J");
@ -1002,7 +1001,7 @@ DoubleMetaphone(char *str, char **codes)
else
{
if (!StringAt(original, (current + 1), 1, "L", "T",
"K", "S", "N", "M", "B", "Z", "")
"K", "S", "N", "M", "B", "Z", "")
&& !StringAt(original, (current - 1), 1,
"S", "K", "L", ""))
{
@ -1035,10 +1034,10 @@ DoubleMetaphone(char *str, char **codes)
if (((current == (length - 3))
&& StringAt(original, (current - 1), 4, "ILLO",
"ILLA", "ALLE", ""))
|| ((StringAt(original, (last - 1), 2, "AS", "OS", "")
|| StringAt(original, last, 1, "A", "O", ""))
&& StringAt(original, (current - 1), 4,
"ALLE", "")))
|| ((StringAt(original, (last - 1), 2, "AS", "OS", "")
|| StringAt(original, last, 1, "A", "O", ""))
&& StringAt(original, (current - 1), 4,
"ALLE", "")))
{
MetaphAdd(primary, "L");
MetaphAdd(secondary, "");
@ -1056,7 +1055,7 @@ DoubleMetaphone(char *str, char **codes)
case 'M':
if ((StringAt(original, (current - 1), 3, "UMB", "")
&& (((current + 1) == last)
|| StringAt(original, (current + 2), 2, "ER", "")))
|| StringAt(original, (current + 2), 2, "ER", "")))
/* 'dumb','thumb' */
|| (GetAt(original, current + 1) == 'M'))
current += 2;
@ -1113,7 +1112,7 @@ DoubleMetaphone(char *str, char **codes)
if ((current == last)
&& !SlavoGermanic(original)
&& StringAt(original, (current - 2), 2, "IE", "")
&& !StringAt(original, (current - 4), 2, "ME", "MA", ""))
&& !StringAt(original, (current - 4), 2, "ME", "MA", ""))
{
MetaphAdd(primary, "");
MetaphAdd(secondary, "R");
@ -1152,8 +1151,8 @@ DoubleMetaphone(char *str, char **codes)
{
/* germanic */
if (StringAt
(original, (current + 1), 4, "HEIM", "HOEK", "HOLM",
"HOLZ", ""))
(original, (current + 1), 4, "HEIM", "HOEK", "HOLM",
"HOLZ", ""))
{
MetaphAdd(primary, "S");
MetaphAdd(secondary, "S");
@ -1187,8 +1186,8 @@ DoubleMetaphone(char *str, char **codes)
/*
* german & anglicisations, e.g. 'smith' match 'schmidt',
* 'snider' match 'schneider' also, -sz- in slavic
* language although in hungarian it is pronounced 's'
* 'snider' match 'schneider' also, -sz- in slavic language
* although in hungarian it is pronounced 's'
*/
if (((current == 0)
&& StringAt(original, (current + 1), 1,
@ -1264,7 +1263,7 @@ DoubleMetaphone(char *str, char **codes)
/* french e.g. 'resnais', 'artois' */
if ((current == last)
&& StringAt(original, (current - 2), 2, "AI", "OI", ""))
&& StringAt(original, (current - 2), 2, "AI", "OI", ""))
{
MetaphAdd(primary, "");
MetaphAdd(secondary, "S");
@ -1365,8 +1364,8 @@ DoubleMetaphone(char *str, char **codes)
/* Arnow should match Arnoff */
if (((current == last) && IsVowel(original, current - 1))
|| StringAt(original, (current - 1), 5, "EWSKI", "EWSKY",
"OWSKI", "OWSKY", "")
|| StringAt(original, (current - 1), 5, "EWSKI", "EWSKY",
"OWSKI", "OWSKY", "")
|| StringAt(original, 0, 3, "SCH", ""))
{
MetaphAdd(primary, "");
@ -1442,8 +1441,8 @@ DoubleMetaphone(char *str, char **codes)
}
/*
* printf("PRIMARY: %s\n", primary->str); printf("SECONDARY:
* %s\n", secondary->str);
* printf("PRIMARY: %s\n", primary->str); printf("SECONDARY: %s\n",
* secondary->str);
*/
}

View File

@ -65,10 +65,10 @@ levenshtein(PG_FUNCTION_ARGS)
int j;
/*
* Fetch the arguments. str_s is referred to as the "source" cols =
* length of source + 1 to allow for the initialization column str_t
* is referred to as the "target", rows = length of target + 1 rows =
* length of target + 1 to allow for the initialization row
* Fetch the arguments. str_s is referred to as the "source" cols = length
* of source + 1 to allow for the initialization column str_t is referred
* to as the "target", rows = length of target + 1 rows = length of target
* + 1 to allow for the initialization row
*/
str_s = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
str_t = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));
@ -78,10 +78,9 @@ levenshtein(PG_FUNCTION_ARGS)
/*
* Restrict the length of the strings being compared to something
* reasonable because we will have to perform rows * cols
* calculations. If longer strings need to be compared, increase
* MAX_LEVENSHTEIN_STRLEN to suit (but within your tolerance for speed
* and memory usage).
* reasonable because we will have to perform rows * cols calculations. If
* longer strings need to be compared, increase MAX_LEVENSHTEIN_STRLEN to
* suit (but within your tolerance for speed and memory usage).
*/
if ((cols > MAX_LEVENSHTEIN_STRLEN + 1) || (rows > MAX_LEVENSHTEIN_STRLEN + 1))
ereport(ERROR,
@ -90,9 +89,9 @@ levenshtein(PG_FUNCTION_ARGS)
MAX_LEVENSHTEIN_STRLEN)));
/*
* If either rows or cols is 0, the answer is the other value. This
* makes sense since it would take that many insertions the build a
* matching string
* If either rows or cols is 0, the answer is the other value. This makes
* sense since it would take that many insertions the build a matching
* string
*/
if (cols == 0)
@ -102,9 +101,8 @@ levenshtein(PG_FUNCTION_ARGS)
PG_RETURN_INT32(cols);
/*
* Allocate two vectors of integers. One will be used for the "upper"
* row, the other for the "lower" row. Initialize the "upper" row to
* 0..cols
* Allocate two vectors of integers. One will be used for the "upper" row,
* the other for the "lower" row. Initialize the "upper" row to 0..cols
*/
u_cells = palloc(sizeof(int) * cols);
for (i = 0; i < cols; i++)
@ -119,14 +117,13 @@ levenshtein(PG_FUNCTION_ARGS)
str_s0 = str_s;
/*
* Loop through the rows, starting at row 1. Row 0 is used for the
* initial "upper" row.
* Loop through the rows, starting at row 1. Row 0 is used for the initial
* "upper" row.
*/
for (j = 1; j < rows; j++)
{
/*
* We'll always start with col 1, and initialize lower row col 0
* to j
* We'll always start with col 1, and initialize lower row col 0 to j
*/
l_cells[0] = j;
@ -140,8 +137,7 @@ levenshtein(PG_FUNCTION_ARGS)
/*
* The "cost" value is 0 if the character at the current col
* position in the source string, matches the character at the
* current row position in the target string; cost is 1
* otherwise.
* current row position in the target string; cost is 1 otherwise.
*/
c = ((CHAREQ(str_s, str_t)) ? 0 : 1);
@ -172,8 +168,8 @@ levenshtein(PG_FUNCTION_ARGS)
}
/*
* Lower row now becomes the upper row, and the upper row gets
* reused as the new lower row.
* Lower row now becomes the upper row, and the upper row gets reused
* as the new lower row.
*/
tmp = u_cells;
u_cells = l_cells;
@ -301,8 +297,8 @@ Lookahead(char *word, int how_far)
for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
/* Edge forward in the string... */
letter_ahead = word[idx]; /* idx will be either == to how_far or at
* the end of the string */
letter_ahead = word[idx]; /* idx will be either == to how_far or at the
* end of the string */
return letter_ahead;
}
@ -453,11 +449,11 @@ _metaphone(
/*
* THOUGHT: It would be nice if, rather than having things
* like... well, SCI. For SCI you encode the S, then have to
* remember to skip the C. So the phonome SCI invades both S and
* C. It would be better, IMHO, to skip the C from the S part of
* the encoding. Hell, I'm trying it.
* THOUGHT: It would be nice if, rather than having things like...
* well, SCI. For SCI you encode the S, then have to remember to skip
* the C. So the phonome SCI invades both S and C. It would be
* better, IMHO, to skip the C from the S part of the encoding. Hell,
* I'm trying it.
*/
/* Ignore non-alphas */
@ -478,9 +474,9 @@ _metaphone(
break;
/*
* 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW
* is handled in S) S if -CI-, -CE- or -CY- dropped if
* -SCI-, SCE-, -SCY- (handed in S) else K
* 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW is
* handled in S) S if -CI-, -CE- or -CY- dropped if -SCI-,
* SCE-, -SCY- (handed in S) else K
*/
case 'C':
if (MAKESOFT(Next_Letter))
@ -534,8 +530,8 @@ _metaphone(
/*
* F if in -GH and not B--GH, D--GH, -H--GH, -H---GH else
* dropped if -GNED, -GN, else dropped if -DGE-, -DGI- or
* -DGY- (handled in D) else J if in -GE-, -GI, -GY and
* not GG else K
* -DGY- (handled in D) else J if in -GE-, -GI, -GY and not GG
* else K
*/
case 'G':
if (Next_Letter == 'H')
@ -761,14 +757,17 @@ PG_FUNCTION_INFO_V1(difference);
Datum
difference(PG_FUNCTION_ARGS)
{
char sndx1[SOUNDEX_LEN+1], sndx2[SOUNDEX_LEN+1];
int i, result;
char sndx1[SOUNDEX_LEN + 1],
sndx2[SOUNDEX_LEN + 1];
int i,
result;
_soundex(_textout(PG_GETARG_TEXT_P(0)), sndx1);
_soundex(_textout(PG_GETARG_TEXT_P(1)), sndx2);
result = 0;
for (i=0; i<SOUNDEX_LEN; i++) {
for (i = 0; i < SOUNDEX_LEN; i++)
{
if (sndx1[i] == sndx2[i])
result++;
}

View File

@ -59,8 +59,8 @@ typedef struct callContext
#define START_NUM 8 /* initial size of arrays */
#define PGARRAY_SIZE(n) (sizeof(PGARRAY) + (((n)-1)*sizeof(int4)))
static PGARRAY *GetPGArray(PGARRAY *p, AggState *aggstate, bool fAdd);
static PGARRAY *ShrinkPGArray(PGARRAY *p);
static PGARRAY *GetPGArray(PGARRAY * p, AggState *aggstate, bool fAdd);
static PGARRAY *ShrinkPGArray(PGARRAY * p);
Datum int_agg_state(PG_FUNCTION_ARGS);
Datum int_agg_final_array(PG_FUNCTION_ARGS);
@ -77,7 +77,7 @@ PG_FUNCTION_INFO_V1(int_enum);
* ie the Agg node's aggcontext.
*/
static PGARRAY *
GetPGArray(PGARRAY *p, AggState *aggstate, bool fAdd)
GetPGArray(PGARRAY * p, AggState *aggstate, bool fAdd)
{
if (!p)
{
@ -97,7 +97,7 @@ GetPGArray(PGARRAY *p, AggState *aggstate, bool fAdd)
/* Ensure array has space for another item */
if (p->items >= p->lower)
{
PGARRAY *pn;
PGARRAY *pn;
int n = p->lower * 2;
int cbNew = PGARRAY_SIZE(n);
@ -117,9 +117,10 @@ GetPGArray(PGARRAY *p, AggState *aggstate, bool fAdd)
* memory allocation context
*/
static PGARRAY *
ShrinkPGArray(PGARRAY *p)
ShrinkPGArray(PGARRAY * p)
{
PGARRAY *pnew;
/* get target size */
int cb = PGARRAY_SIZE(p->items);
@ -158,11 +159,11 @@ int_agg_state(PG_FUNCTION_ARGS)
if (!PG_ARGISNULL(1))
{
int4 value = PG_GETARG_INT32(1);
int4 value = PG_GETARG_INT32(1);
if (!p) /* internal error */
if (!p) /* internal error */
elog(ERROR, "no aggregate storage");
else if (p->items >= p->lower) /* internal error */
else if (p->items >= p->lower) /* internal error */
elog(ERROR, "aggregate storage too small");
else
p->array[p->items++] = value;
@ -212,7 +213,7 @@ int_enum(PG_FUNCTION_ARGS)
if (!rsi || !IsA(rsi, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("int_enum called in context that cannot accept a set")));
errmsg("int_enum called in context that cannot accept a set")));
if (!p)
{
@ -223,7 +224,7 @@ int_enum(PG_FUNCTION_ARGS)
if (!fcinfo->flinfo->fn_extra)
{
/* Allocate working state */
MemoryContext oldcontext;
MemoryContext oldcontext;
oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
@ -250,7 +251,8 @@ int_enum(PG_FUNCTION_ARGS)
fcinfo->flinfo->fn_extra = (void *) pc;
MemoryContextSwitchTo(oldcontext);
}
else /* use existing working state */
else
/* use existing working state */
pc = (CTX *) fcinfo->flinfo->fn_extra;
/* Are we done yet? */

View File

@ -38,8 +38,8 @@ g_int_consistent(PG_FUNCTION_ARGS)
if (strategy == BooleanSearchStrategy)
PG_RETURN_BOOL(execconsistent((QUERYTYPE *) query,
(ArrayType *) DatumGetPointer(entry->key),
ISLEAFKEY((ArrayType *) DatumGetPointer(entry->key))));
(ArrayType *) DatumGetPointer(entry->key),
ISLEAFKEY((ArrayType *) DatumGetPointer(entry->key))));
/* XXX are we sure it's safe to scribble on the query object here? */
/* XXX what about toasted input? */
@ -73,7 +73,7 @@ g_int_consistent(PG_FUNCTION_ARGS)
case RTContainedByStrategyNumber:
if (GIST_LEAF(entry))
retval = inner_int_contains(query,
(ArrayType *) DatumGetPointer(entry->key));
(ArrayType *) DatumGetPointer(entry->key));
else
retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
query);
@ -134,7 +134,7 @@ g_int_compress(PG_FUNCTION_ARGS)
r->flags |= LEAFKEY;
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
PG_RETURN_POINTER(retval);
}
@ -175,7 +175,7 @@ g_int_compress(PG_FUNCTION_ARGS)
r = resize_intArrayType(r, len);
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
PG_RETURN_POINTER(retval);
}
else
@ -211,7 +211,7 @@ g_int_decompress(PG_FUNCTION_ARGS)
{
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(in),
entry->rel, entry->page, entry->offset, VARSIZE(in), FALSE);
entry->rel, entry->page, entry->offset, VARSIZE(in), FALSE);
PG_RETURN_POINTER(retval);
}
@ -233,7 +233,7 @@ g_int_decompress(PG_FUNCTION_ARGS)
pfree(in);
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
PG_RETURN_POINTER(retval);
}
@ -378,8 +378,7 @@ g_int_picksplit(PG_FUNCTION_ARGS)
pfree(inter_d);
/*
* are these a more promising split that what we've already
* seen?
* are these a more promising split that what we've already seen?
*/
if (size_waste > waste || firsttime)
@ -430,15 +429,15 @@ g_int_picksplit(PG_FUNCTION_ARGS)
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
/*
* Now split up the regions between the two seeds. An important
* property of this split algorithm is that the split vector v has the
* indices of items to be split in order in its left and right
* vectors. We exploit this property by doing a merge in the code
* that actually splits the page.
* Now split up the regions between the two seeds. An important property
* of this split algorithm is that the split vector v has the indices of
* items to be split in order in its left and right vectors. We exploit
* this property by doing a merge in the code that actually splits the
* page.
*
* For efficiency, we also place the new index tuple in this loop. This
* is handled at the very end, when we have placed all the existing
* tuples and i == maxoff + 1.
* For efficiency, we also place the new index tuple in this loop. This is
* handled at the very end, when we have placed all the existing tuples
* and i == maxoff + 1.
*/
@ -447,9 +446,9 @@ g_int_picksplit(PG_FUNCTION_ARGS)
i = costvector[j].pos;
/*
* If we've already decided where to place this item, just put it
* on the right list. Otherwise, we need to figure out which page
* needs the least enlargement in order to store the item.
* If we've already decided where to place this item, just put it on
* the right list. Otherwise, we need to figure out which page needs
* the least enlargement in order to store the item.
*/
if (i == seed_1)

View File

@ -24,8 +24,8 @@ _int_contained(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(DatumGetBool(
DirectFunctionCall2(
_int_contains,
PointerGetDatum(PG_GETARG_POINTER(1)),
PointerGetDatum(PG_GETARG_POINTER(0))
PointerGetDatum(PG_GETARG_POINTER(1)),
PointerGetDatum(PG_GETARG_POINTER(0))
)
));
}
@ -54,8 +54,8 @@ _int_different(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(!DatumGetBool(
DirectFunctionCall2(
_int_same,
PointerGetDatum(PG_GETARG_POINTER(0)),
PointerGetDatum(PG_GETARG_POINTER(1))
PointerGetDatum(PG_GETARG_POINTER(0)),
PointerGetDatum(PG_GETARG_POINTER(1))
)
));
}

View File

@ -487,7 +487,7 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
if (strategy == BooleanSearchStrategy)
{
PG_RETURN_BOOL(signconsistent((QUERYTYPE *) query,
GETSIGN(DatumGetPointer(entry->key)),
GETSIGN(DatumGetPointer(entry->key)),
false));
}

View File

@ -53,7 +53,7 @@ array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree ** found)
while (num > 0)
{
if (DatumGetBool(DirectFunctionCall2(callback,
PointerGetDatum(item), PointerGetDatum(param))))
PointerGetDatum(item), PointerGetDatum(param))))
{
if (found)

View File

@ -101,7 +101,7 @@ checkLevel(lquery_level * curq, ltree_level * curt)
else if (
(
curvar->len == curt->len ||
(curt->len > curvar->len && (curvar->flag & LVAR_ANYEND))
(curt->len > curvar->len && (curvar->flag & LVAR_ANYEND))
) &&
(*cmpptr) (curvar->name, curt->name, curvar->len) == 0)
{
@ -332,7 +332,7 @@ lt_q_regex(PG_FUNCTION_ARGS)
while (num > 0)
{
if (DatumGetBool(DirectFunctionCall2(ltq_regex,
PointerGetDatum(tree), PointerGetDatum(query))))
PointerGetDatum(tree), PointerGetDatum(query))))
{
res = true;

View File

@ -157,7 +157,7 @@ bool ltree_execute(ITEM * curitem, void *checkval,
int ltree_compare(const ltree * a, const ltree * b);
bool inner_isparent(const ltree * c, const ltree * p);
bool compare_subnode(ltree_level * t, char *q, int len,
int (*cmpptr) (const char *, const char *, size_t), bool anyend);
int (*cmpptr) (const char *, const char *, size_t), bool anyend);
ltree *lca_inner(ltree ** a, int len);
#define PG_GETARG_LTREE(x) ((ltree*)DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(x))))

View File

@ -647,9 +647,9 @@ ltree_consistent(PG_FUNCTION_ARGS)
res = (ltree_compare((ltree *) query, LTG_NODE(key)) == 0);
else
res = (
ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0
ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0
&&
ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0
ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0
);
break;
case BTGreaterEqualStrategyNumber:
@ -677,8 +677,8 @@ ltree_consistent(PG_FUNCTION_ARGS)
case 13:
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(ltq_regex,
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((lquery *) query)
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((lquery *) query)
));
else
res = (gist_qe(key, (lquery *) query) && gist_between(key, (lquery *) query));
@ -687,8 +687,8 @@ ltree_consistent(PG_FUNCTION_ARGS)
case 15:
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(ltxtq_exec,
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((lquery *) query)
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((lquery *) query)
));
else
res = gist_qtxt(key, (ltxtquery *) query);
@ -697,8 +697,8 @@ ltree_consistent(PG_FUNCTION_ARGS)
case 17:
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(lt_q_regex,
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((ArrayType *) query)
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((ArrayType *) query)
));
else
res = arrq_cons(key, (ArrayType *) query);

View File

@ -82,7 +82,7 @@ ltree_in(PG_FUNCTION_ARGS)
errmsg("name of level is too long"),
errdetail("name length is %d, must " \
"be < 256, in position %d",
lptr->len, (int) (lptr->start - buf))));
lptr->len, (int) (lptr->start - buf))));
totallen += MAXALIGN(lptr->len + LEVEL_HDRSIZE);
lptr++;
@ -284,7 +284,7 @@ lquery_in(PG_FUNCTION_ARGS)
errmsg("name of level is too long"),
errdetail("name length is %d, must " \
"be < 256, in position %d",
lptr->len, (int) (lptr->start - buf))));
lptr->len, (int) (lptr->start - buf))));
state = LQPRS_WAITVAR;
}
@ -300,7 +300,7 @@ lquery_in(PG_FUNCTION_ARGS)
errmsg("name of level is too long"),
errdetail("name length is %d, must " \
"be < 256, in position %d",
lptr->len, (int) (lptr->start - buf))));
lptr->len, (int) (lptr->start - buf))));
state = LQPRS_WAITLEVEL;
curqlevel = NEXTLEV(curqlevel);

View File

@ -264,7 +264,7 @@ msqlListTables(int a)
char tbuf[BUFSIZ];
snprintf(tbuf, BUFSIZ,
"select relname from pg_class where relkind='r' and relowner=%d",
"select relname from pg_class where relkind='r' and relowner=%d",
geteuid());
if (msqlQuery(a, tbuf) > 0)
{
@ -288,7 +288,7 @@ msqlListIndex(int a, char *b, char *c)
char tbuf[BUFSIZ];
snprintf(tbuf, BUFSIZ,
"select relname from pg_class where relkind='i' and relowner=%d",
"select relname from pg_class where relkind='i' and relowner=%d",
geteuid());
if (msqlQuery(a, tbuf) > 0)
{

View File

@ -17,10 +17,10 @@
/* an extensible array to keep track of elements to show */
typedef struct
{
char **array;
int num;
int alloc;
} eary;
char **array;
int num;
int alloc;
} eary;
/* these are the opts structures for command line params */
struct options
@ -36,19 +36,19 @@ struct options
bool extended;
bool tablespaces;
char *dbname;
char *hostname;
char *port;
char *username;
char *password;
char *dbname;
char *hostname;
char *port;
char *username;
char *password;
};
/* function prototypes */
void get_opts(int, char **, struct options *);
void *myalloc(size_t size);
char *mystrdup(const char *str);
void add_one_elt(char *eltname, eary *eary);
char *get_comma_elts(eary *eary);
void add_one_elt(char *eltname, eary * eary);
char *get_comma_elts(eary * eary);
PGconn *sql_conn(struct options *);
int sql_exec(PGconn *, const char *sql, bool quiet);
void sql_exec_dumpalldbs(PGconn *, struct options *);
@ -95,7 +95,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
add_one_elt(optarg, my_opts->oids);
break;
/* specify one filenode to show*/
/* specify one filenode to show */
case 'f':
add_one_elt(optarg, my_opts->filenodes);
break;
@ -149,23 +149,23 @@ get_opts(int argc, char **argv, struct options * my_opts)
case '?':
case 'h':
fprintf(stderr,
"Usage: oid2name [-s|-d database] [-S][-i][-q][-x] [-t table|-o oid|-f file] ...\n"
" default action show all database Oids\n"
" -d database database to connect to\n"
" -s show all tablespaces\n"
" -S show system objects too\n"
" -i show indexes and sequences too\n"
" -x extended (show additional columns)\n"
" -q quiet (don't show headers)\n"
" -t <table> show info for table named <table>\n"
" -o <oid> show info for table with Oid <oid>\n"
" -f <filenode> show info for table with filenode <filenode>\n"
" -H host connect to remote host\n"
" -p port host port to connect to\n"
" -U username username to connect with\n"
" -P password password for username\n"
" (see also $PGPASSWORD and ~/.pgpass)\n"
);
"Usage: oid2name [-s|-d database] [-S][-i][-q][-x] [-t table|-o oid|-f file] ...\n"
" default action show all database Oids\n"
" -d database database to connect to\n"
" -s show all tablespaces\n"
" -S show system objects too\n"
" -i show indexes and sequences too\n"
" -x extended (show additional columns)\n"
" -q quiet (don't show headers)\n"
" -t <table> show info for table named <table>\n"
" -o <oid> show info for table with Oid <oid>\n"
" -f <filenode> show info for table with filenode <filenode>\n"
" -H host connect to remote host\n"
" -p port host port to connect to\n"
" -U username username to connect with\n"
" -P password password for username\n"
" (see also $PGPASSWORD and ~/.pgpass)\n"
);
exit(1);
break;
}
@ -175,7 +175,8 @@ get_opts(int argc, char **argv, struct options * my_opts)
void *
myalloc(size_t size)
{
void *ptr = malloc(size);
void *ptr = malloc(size);
if (!ptr)
{
fprintf(stderr, "out of memory");
@ -187,7 +188,8 @@ myalloc(size_t size)
char *
mystrdup(const char *str)
{
char *result = strdup(str);
char *result = strdup(str);
if (!result)
{
fprintf(stderr, "out of memory");
@ -202,7 +204,7 @@ mystrdup(const char *str)
* Add one element to a (possibly empty) eary struct.
*/
void
add_one_elt(char *eltname, eary *eary)
add_one_elt(char *eltname, eary * eary)
{
if (eary->alloc == 0)
{
@ -233,11 +235,12 @@ add_one_elt(char *eltname, eary *eary)
* SQL statement.
*/
char *
get_comma_elts(eary *eary)
get_comma_elts(eary * eary)
{
char *ret,
*ptr;
int i, length = 0;
char *ret,
*ptr;
int i,
length = 0;
if (eary->num == 0)
return mystrdup("");
@ -272,9 +275,9 @@ sql_conn(struct options * my_opts)
/* login */
conn = PQsetdbLogin(my_opts->hostname,
my_opts->port,
NULL, /* options */
NULL, /* tty */
my_opts->port,
NULL, /* options */
NULL, /* tty */
my_opts->dbname,
my_opts->username,
my_opts->password);
@ -303,7 +306,9 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
int nfields;
int nrows;
int i, j, l;
int i,
j,
l;
int *length;
char *pad;
@ -334,7 +339,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
{
for (j = 0; j < nfields; j++)
{
l = strlen(PQgetvalue(res, i, j));
l = strlen(PQgetvalue(res, i, j));
if (l > length[j])
length[j] = strlen(PQgetvalue(res, i, j));
}
@ -372,36 +377,36 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
}
/*
* Dump all databases. There are no system objects to worry about.
* Dump all databases. There are no system objects to worry about.
*/
void
sql_exec_dumpalldbs(PGconn *conn, struct options *opts)
sql_exec_dumpalldbs(PGconn *conn, struct options * opts)
{
char todo[1024];
/* get the oid and database name from the system pg_database table */
snprintf(todo, sizeof(todo),
"SELECT d.oid AS \"Oid\", datname AS \"Database Name\", "
"spcname AS \"Tablespace\" FROM pg_database d JOIN pg_tablespace t ON "
"spcname AS \"Tablespace\" FROM pg_database d JOIN pg_tablespace t ON "
"(dattablespace = t.oid) ORDER BY 2");
sql_exec(conn, todo, opts->quiet);
}
/*
/*
* Dump all tables, indexes and sequences in the current database.
*/
void
sql_exec_dumpalltables(PGconn *conn, struct options *opts)
sql_exec_dumpalltables(PGconn *conn, struct options * opts)
{
char todo[1024];
char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
snprintf(todo, sizeof(todo),
"SELECT relfilenode as \"Filenode\", relname as \"Table Name\" %s "
"SELECT relfilenode as \"Filenode\", relname as \"Table Name\" %s "
"FROM pg_class c "
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
" LEFT JOIN pg_catalog.pg_database d ON d.datname = current_database(),"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
" LEFT JOIN pg_catalog.pg_database d ON d.datname = current_database(),"
" pg_catalog.pg_tablespace t "
"WHERE relkind IN ('r'%s) AND "
" %s"
@ -412,7 +417,7 @@ sql_exec_dumpalltables(PGconn *conn, struct options *opts)
"ORDER BY relname",
opts->extended ? addfields : "",
opts->indexes ? ", 'i', 'S', 't'" : "",
opts->systables ? "" : "n.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema') AND");
opts->systables ? "" : "n.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema') AND");
sql_exec(conn, todo, opts->quiet);
}
@ -422,11 +427,14 @@ sql_exec_dumpalltables(PGconn *conn, struct options *opts)
* given objects in the current database.
*/
void
sql_exec_searchtables(PGconn *conn, struct options *opts)
sql_exec_searchtables(PGconn *conn, struct options * opts)
{
char *todo;
char *qualifiers, *ptr;
char *comma_oids, *comma_filenodes, *comma_tables;
char *qualifiers,
*ptr;
char *comma_oids,
*comma_filenodes,
*comma_tables;
bool written = false;
char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
@ -465,9 +473,9 @@ sql_exec_searchtables(PGconn *conn, struct options *opts)
/* now build the query */
todo = (char *) myalloc(650 + strlen(qualifiers));
snprintf(todo, 650 + strlen(qualifiers),
"SELECT relfilenode as \"Filenode\", relname as \"Table Name\" %s\n"
"SELECT relfilenode as \"Filenode\", relname as \"Table Name\" %s\n"
"FROM pg_class c \n"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"
" LEFT JOIN pg_catalog.pg_database d ON d.datname = current_database(),\n"
" pg_catalog.pg_tablespace t \n"
"WHERE relkind IN ('r', 'i', 'S', 't') AND \n"
@ -486,9 +494,9 @@ sql_exec_searchtables(PGconn *conn, struct options *opts)
}
void
sql_exec_dumpalltbspc(PGconn *conn, struct options *opts)
sql_exec_dumpalltbspc(PGconn *conn, struct options * opts)
{
char todo[1024];
char todo[1024];
snprintf(todo, sizeof(todo),
"SELECT oid AS \"Oid\", spcname as \"Tablespace Name\"\n"

View File

@ -1,9 +1,9 @@
/*-------------------------------------------------------------------------
*
* pg_buffercache_pages.c
* display some contents of the buffer cache
* display some contents of the buffer cache
*
* $PostgreSQL: pgsql/contrib/pg_buffercache/pg_buffercache_pages.c,v 1.5 2005/10/12 16:45:13 tgl Exp $
* $PostgreSQL: pgsql/contrib/pg_buffercache/pg_buffercache_pages.c,v 1.6 2005/10/15 02:49:05 momjian Exp $
*-------------------------------------------------------------------------
*/
#include "postgres.h"
@ -17,11 +17,11 @@
#define NUM_BUFFERCACHE_PAGES_ELEM 6
#if defined(WIN32) || defined(__CYGWIN__)
extern DLLIMPORT BufferDesc *BufferDescriptors;
extern DLLIMPORT volatile uint32 InterruptHoldoffCount;
extern DLLIMPORT BufferDesc *BufferDescriptors;
extern DLLIMPORT volatile uint32 InterruptHoldoffCount;
#endif
Datum pg_buffercache_pages(PG_FUNCTION_ARGS);
Datum pg_buffercache_pages(PG_FUNCTION_ARGS);
/*
@ -34,24 +34,24 @@ typedef struct
Oid relfilenode;
Oid reltablespace;
Oid reldatabase;
BlockNumber blocknum;
BlockNumber blocknum;
bool isvalid;
bool isdirty;
} BufferCachePagesRec;
} BufferCachePagesRec;
/*
* Function context for data persisting over repeated calls.
*/
typedef struct
typedef struct
{
AttInMetadata *attinmeta;
BufferCachePagesRec *record;
char *values[NUM_BUFFERCACHE_PAGES_ELEM];
AttInMetadata *attinmeta;
BufferCachePagesRec *record;
char *values[NUM_BUFFERCACHE_PAGES_ELEM];
} BufferCachePagesContext;
} BufferCachePagesContext;
/*
@ -63,44 +63,44 @@ Datum
pg_buffercache_pages(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
MemoryContext oldcontext;
BufferCachePagesContext *fctx; /* User function context. */
TupleDesc tupledesc;
HeapTuple tuple;
FuncCallContext *funcctx;
Datum result;
MemoryContext oldcontext;
BufferCachePagesContext *fctx; /* User function context. */
TupleDesc tupledesc;
HeapTuple tuple;
if (SRF_IS_FIRSTCALL())
{
uint32 i;
volatile BufferDesc *bufHdr;
volatile BufferDesc *bufHdr;
funcctx = SRF_FIRSTCALL_INIT();
/* Switch context when allocating stuff to be used in later calls */
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* Construct a tuple to return. */
tupledesc = CreateTemplateTupleDesc(NUM_BUFFERCACHE_PAGES_ELEM, false);
TupleDescInitEntry(tupledesc, (AttrNumber) 1, "bufferid",
INT4OID, -1, 0);
INT4OID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 2, "relfilenode",
OIDOID, -1, 0);
OIDOID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 3, "reltablespace",
OIDOID, -1, 0);
OIDOID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 4, "reldatabase",
OIDOID, -1, 0);
OIDOID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 5, "relblocknumber",
INT8OID, -1, 0);
INT8OID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 6, "isdirty",
BOOLOID, -1, 0);
BOOLOID, -1, 0);
/* Generate attribute metadata needed later to produce tuples */
funcctx->attinmeta = TupleDescGetAttInMetadata(tupledesc);
/*
* Create a function context for cross-call persistence
* and initialize the buffer counters.
/*
* Create a function context for cross-call persistence and initialize
* the buffer counters.
*/
fctx = (BufferCachePagesContext *) palloc(sizeof(BufferCachePagesContext));
funcctx->max_calls = NBuffers;
@ -118,12 +118,12 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
fctx->values[4] = (char *) palloc(3 * sizeof(uint32) + 1);
fctx->values[5] = (char *) palloc(2);
/* Return to original context when allocating transient memory */
MemoryContextSwitchTo(oldcontext);
/*
/*
* Lock Buffer map and scan though all the buffers, saving the
* relevant fields in the fctx->record structure.
*/
@ -140,7 +140,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
fctx->record[i].reldatabase = bufHdr->tag.rnode.dbNode;
fctx->record[i].blocknum = bufHdr->tag.blockNum;
if (bufHdr->flags & BM_DIRTY)
if (bufHdr->flags & BM_DIRTY)
fctx->record[i].isdirty = true;
else
fctx->record[i].isdirty = false;
@ -159,34 +159,34 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
}
funcctx = SRF_PERCALL_SETUP();
/* Get the saved state */
fctx = funcctx->user_fctx;
if (funcctx->call_cntr < funcctx->max_calls)
{
uint32 i = funcctx->call_cntr;
char *values[NUM_BUFFERCACHE_PAGES_ELEM];
uint32 i = funcctx->call_cntr;
char *values[NUM_BUFFERCACHE_PAGES_ELEM];
int j;
/*
* Use a temporary values array, initially pointing to
* fctx->values, so it can be reassigned w/o losing the storage
* for subsequent calls.
/*
* Use a temporary values array, initially pointing to fctx->values,
* so it can be reassigned w/o losing the storage for subsequent
* calls.
*/
for (j = 0; j < NUM_BUFFERCACHE_PAGES_ELEM; j++)
{
values[j] = fctx->values[j];
}
/*
* Set all fields except the bufferid to null if the buffer is
* unused or not valid.
* Set all fields except the bufferid to null if the buffer is unused
* or not valid.
*/
if (fctx->record[i].blocknum == InvalidBlockNumber ||
fctx->record[i].isvalid == false )
fctx->record[i].isvalid == false)
{
sprintf(values[0], "%u", fctx->record[i].bufferid);
@ -205,7 +205,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
sprintf(values[2], "%u", fctx->record[i].reltablespace);
sprintf(values[3], "%u", fctx->record[i].reldatabase);
sprintf(values[4], "%u", fctx->record[i].blocknum);
if (fctx->record[i].isdirty)
if (fctx->record[i].isdirty)
{
strcpy(values[5], "t");
}
@ -213,7 +213,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
{
strcpy(values[5], "f");
}
}
@ -228,4 +228,3 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
SRF_RETURN_DONE(funcctx);
}

View File

@ -300,8 +300,8 @@ similarity_op(PG_FUNCTION_ARGS)
{
float4 res = DatumGetFloat4(DirectFunctionCall2(
similarity,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)
));
PG_RETURN_BOOL(res >= trgm_limit);

View File

@ -1,5 +1,5 @@
/*
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.42 2005/10/07 15:34:17 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.43 2005/10/15 02:49:06 momjian Exp $
*
* pgbench: a simple benchmark program for PostgreSQL
* written by Tatsuo Ishii
@ -55,8 +55,7 @@ extern int optind;
#define MAXCLIENTS 1024 /* max number of clients allowed */
int nclients = 1; /* default number of simulated clients */
int nxacts = 10; /* default number of transactions per
* clients */
int nxacts = 10; /* default number of transactions per clients */
/*
* scaling factor. for example, tps = 10 will make 1000000 tuples of
@ -78,8 +77,7 @@ bool use_log; /* log transaction latencies to a file */
int remains; /* number of remaining clients */
int is_connect; /* establish connection for each
* transaction */
int is_connect; /* establish connection for each transaction */
char *pghost = "";
char *pgport = NULL;
@ -92,8 +90,8 @@ char *dbName;
/* variable definitions */
typedef struct
{
char *name; /* variable name */
char *value; /* its value */
char *name; /* variable name */
char *value; /* its value */
} Variable;
/*
@ -107,8 +105,8 @@ typedef struct
int state; /* state No. */
int cnt; /* xacts count */
int ecnt; /* error count */
int listen; /* 0 indicates that an async query has
* been sent */
int listen; /* 0 indicates that an async query has been
* sent */
Variable *variables; /* array of variable definitions */
int nvariables;
struct timeval txn_begin; /* used for measuring latencies */
@ -124,48 +122,48 @@ typedef struct
typedef struct
{
int type; /* command type (SQL_COMMAND or META_COMMAND) */
int argc; /* number of commands */
char *argv[MAX_ARGS]; /* command list */
int type; /* command type (SQL_COMMAND or META_COMMAND) */
int argc; /* number of commands */
char *argv[MAX_ARGS]; /* command list */
} Command;
#define MAX_FILES 128 /* max number of SQL script files allowed */
Command **sql_files[MAX_FILES]; /* SQL script files */
int num_files; /* its number */
Command **sql_files[MAX_FILES]; /* SQL script files */
int num_files; /* its number */
/* default scenario */
static char *tpc_b = {
"\\setrandom aid 1 100000\n"
"\\setrandom bid 1 1\n"
"\\setrandom tid 1 10\n"
"\\setrandom delta 1 10000\n"
"BEGIN;\n"
"UPDATE accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
"SELECT abalance FROM accounts WHERE aid = :aid;\n"
"UPDATE tellers SET tbalance = tbalance + :delta WHERE tid = :tid;\n"
"UPDATE branches SET bbalance = bbalance + :delta WHERE bid = :bid;\n"
"INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
"END;\n"
"\\setrandom aid 1 100000\n"
"\\setrandom bid 1 1\n"
"\\setrandom tid 1 10\n"
"\\setrandom delta 1 10000\n"
"BEGIN;\n"
"UPDATE accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
"SELECT abalance FROM accounts WHERE aid = :aid;\n"
"UPDATE tellers SET tbalance = tbalance + :delta WHERE tid = :tid;\n"
"UPDATE branches SET bbalance = bbalance + :delta WHERE bid = :bid;\n"
"INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
"END;\n"
};
/* -N case */
static char *simple_update = {
"\\setrandom aid 1 100000\n"
"\\setrandom bid 1 1\n"
"\\setrandom tid 1 10\n"
"\\setrandom delta 1 10000\n"
"BEGIN;\n"
"UPDATE accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
"SELECT abalance FROM accounts WHERE aid = :aid;\n"
"INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
"END;\n"
"\\setrandom aid 1 100000\n"
"\\setrandom bid 1 1\n"
"\\setrandom tid 1 10\n"
"\\setrandom delta 1 10000\n"
"BEGIN;\n"
"UPDATE accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
"SELECT abalance FROM accounts WHERE aid = :aid;\n"
"INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
"END;\n"
};
/* -S case */
static char *select_only = {
"\\setrandom aid 1 100000\n"
"SELECT abalance FROM accounts WHERE aid = :aid;\n"
"\\setrandom aid 1 100000\n"
"SELECT abalance FROM accounts WHERE aid = :aid;\n"
};
static void
@ -262,7 +260,7 @@ compareVariables(const void *v1, const void *v2)
static char *
getVariable(CState * st, char *name)
{
Variable key = { name }, *var;
Variable key = {name}, *var;
/* On some versions of Solaris, bsearch of zero items dumps core */
if (st->nvariables <= 0)
@ -282,7 +280,7 @@ getVariable(CState * st, char *name)
static int
putVariable(CState * st, char *name, char *value)
{
Variable key = { name }, *var;
Variable key = {name}, *var;
/* On some versions of Solaris, bsearch of zero items dumps core */
if (st->nvariables > 0)
@ -300,7 +298,7 @@ putVariable(CState * st, char *name, char *value)
if (st->variables)
newvars = (Variable *) realloc(st->variables,
(st->nvariables + 1) * sizeof(Variable));
(st->nvariables + 1) * sizeof(Variable));
else
newvars = (Variable *) malloc(sizeof(Variable));
@ -341,15 +339,19 @@ putVariable(CState * st, char *name, char *value)
static char *
assignVariables(CState * st, char *sql)
{
int i, j;
char *p, *name, *val;
int i,
j;
char *p,
*name,
*val;
void *tmp;
i = 0;
while ((p = strchr(&sql[i], ':')) != NULL)
{
i = j = p - sql;
do {
do
{
i++;
} while (isalnum((unsigned char) sql[i]) || sql[i] == '_');
if (i == j + 1)
@ -403,7 +405,7 @@ doCustom(CState * state, int n, int debug)
{
PGresult *res;
CState *st = &state[n];
Command **commands;
Command **commands;
commands = sql_files[st->use_file];
@ -414,20 +416,19 @@ doCustom(CState * state, int n, int debug)
if (debug)
fprintf(stderr, "client %d receiving\n", n);
if (!PQconsumeInput(st->con))
{ /* there's something wrong */
{ /* there's something wrong */
fprintf(stderr, "Client %d aborted in state %d. Probably the backend died while processing.\n", n, st->state);
remains--; /* I've aborted */
remains--; /* I've aborted */
PQfinish(st->con);
st->con = NULL;
return;
}
if (PQisBusy(st->con))
return; /* don't have the whole result yet */
return; /* don't have the whole result yet */
}
/*
* transaction finished: record the time it took in the
* log
* transaction finished: record the time it took in the log
*/
if (use_log && commands[st->state + 1] == NULL)
{
@ -468,7 +469,7 @@ doCustom(CState * state, int n, int debug)
if (++st->cnt >= nxacts)
{
remains--; /* I've done */
remains--; /* I've done */
if (st->con != NULL)
{
PQfinish(st->con);
@ -483,7 +484,7 @@ doCustom(CState * state, int n, int debug)
if (commands[st->state] == NULL)
{
st->state = 0;
st->use_file = getrand(0, num_files-1);
st->use_file = getrand(0, num_files - 1);
}
}
@ -525,13 +526,14 @@ doCustom(CState * state, int n, int debug)
}
else
{
st->listen = 1; /* flags that should be listened */
st->listen = 1; /* flags that should be listened */
}
free(sql);
}
else if (commands[st->state]->type == META_COMMAND)
{
int argc = commands[st->state]->argc, i;
int argc = commands[st->state]->argc,
i;
char **argv = commands[st->state]->argv;
if (debug)
@ -748,28 +750,29 @@ init(void)
PQfinish(con);
}
static Command*
static Command *
process_commands(char *buf)
{
const char delim[] = " \f\n\r\t\v";
Command *my_commands;
Command *my_commands;
int j;
char *p, *tok;
char *p,
*tok;
if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';
*p = '\0';
p = buf;
while (isspace((unsigned char) *p))
p++;
p++;
if (*p == '\0' || strncmp(p, "--", 2) == 0)
{
return NULL;
}
my_commands = (Command *)malloc(sizeof(Command));
my_commands = (Command *) malloc(sizeof(Command));
if (my_commands == NULL)
{
return NULL;
@ -794,10 +797,11 @@ process_commands(char *buf)
j++;
tok = strtok(NULL, delim);
}
if (strcasecmp(my_commands->argv[0], "setrandom") == 0)
{
int min, max;
int min,
max;
if (my_commands->argc < 4)
{
@ -806,8 +810,8 @@ process_commands(char *buf)
}
for (j = 4; j < my_commands->argc; j++)
fprintf(stderr, "%s: extra argument \"%s\" ignored\n",
my_commands->argv[0], my_commands->argv[j]);
fprintf(stderr, "%s: extra argument \"%s\" ignored\n",
my_commands->argv[0], my_commands->argv[j]);
if ((min = atoi(my_commands->argv[2])) < 0)
{
@ -825,7 +829,7 @@ process_commands(char *buf)
}
else
{
fprintf(stderr, "invalid command %s\n", my_commands->argv[0]);
fprintf(stderr, "invalid command %s\n", my_commands->argv[0]);
return NULL;
}
}
@ -847,11 +851,11 @@ process_file(char *filename)
{
#define COMMANDS_ALLOC_NUM 128
Command **my_commands;
Command **my_commands;
FILE *fd;
int lineno;
char buf[BUFSIZ];
int alloc_num;
int alloc_num;
if (num_files >= MAX_FILES)
{
@ -860,7 +864,7 @@ process_file(char *filename)
}
alloc_num = COMMANDS_ALLOC_NUM;
my_commands = (Command **)malloc(sizeof(Command **)*alloc_num);
my_commands = (Command **) malloc(sizeof(Command **) * alloc_num);
if (my_commands == NULL)
return false;
@ -876,7 +880,7 @@ process_file(char *filename)
while (fgets(buf, sizeof(buf), fd) != NULL)
{
Command *commands;
Command *commands;
commands = process_commands(buf);
if (commands == NULL)
@ -913,25 +917,25 @@ process_builtin(char *tb)
{
#define COMMANDS_ALLOC_NUM 128
Command **my_commands;
Command **my_commands;
int lineno;
char buf[BUFSIZ];
int alloc_num;
int alloc_num;
if (*tb == '\0')
return NULL;
alloc_num = COMMANDS_ALLOC_NUM;
my_commands = malloc(sizeof(Command **)*alloc_num);
my_commands = malloc(sizeof(Command **) * alloc_num);
if (my_commands == NULL)
return NULL;
lineno = 0;
for(;;)
for (;;)
{
char *p;
Command *commands;
char *p;
Command *commands;
p = buf;
while (*tb && *tb != '\n')
@ -1016,20 +1020,18 @@ main(int argc, char **argv)
{
int c;
int is_init_mode = 0; /* initialize mode? */
int is_no_vacuum = 0; /* no vacuum at all before
* testing? */
int is_no_vacuum = 0; /* no vacuum at all before testing? */
int is_full_vacuum = 0; /* do full vacuum before testing? */
int debug = 0; /* debug flag */
int ttype = 0; /* transaction type. 0: TPC-B, 1: SELECT
* only, 2: skip update of branches and
* tellers */
int ttype = 0; /* transaction type. 0: TPC-B, 1: SELECT only,
* 2: skip update of branches and tellers */
char *filename = NULL;
static CState *state; /* status of clients */
struct timeval tv1; /* start up time */
struct timeval tv2; /* after establishing all connections to
* the backend */
struct timeval tv2; /* after establishing all connections to the
* backend */
struct timeval tv3; /* end time */
int i;
@ -1105,7 +1107,8 @@ main(int argc, char **argv)
fprintf(stderr, "Use limit/ulimt to increase the limit before using pgbench.\n");
exit(1);
}
#endif /* #if !(defined(__CYGWIN__) || defined(__MINGW32__)) */
#endif /* #if !(defined(__CYGWIN__) ||
* defined(__MINGW32__)) */
break;
case 'C':
is_connect = 1;
@ -1305,35 +1308,35 @@ main(int argc, char **argv)
/* process bultin SQL scripts */
switch (ttype)
{
char buf[128];
char buf[128];
case 0:
sql_files[0] = process_builtin(tpc_b);
snprintf(buf, sizeof(buf), "%d", 100000*tps);
snprintf(buf, sizeof(buf), "%d", 100000 * tps);
sql_files[0][0]->argv[3] = strdup(buf);
snprintf(buf, sizeof(buf), "%d", 1*tps);
snprintf(buf, sizeof(buf), "%d", 1 * tps);
sql_files[0][1]->argv[3] = strdup(buf);
snprintf(buf, sizeof(buf), "%d", 10*tps);
snprintf(buf, sizeof(buf), "%d", 10 * tps);
sql_files[0][2]->argv[3] = strdup(buf);
snprintf(buf, sizeof(buf), "%d", 10000*tps);
snprintf(buf, sizeof(buf), "%d", 10000 * tps);
sql_files[0][3]->argv[3] = strdup(buf);
num_files = 1;
break;
case 1:
sql_files[0] = process_builtin(select_only);
snprintf(buf, sizeof(buf), "%d", 100000*tps);
snprintf(buf, sizeof(buf), "%d", 100000 * tps);
sql_files[0][0]->argv[3] = strdup(buf);
num_files = 1;
break;
case 2:
sql_files[0] = process_builtin(simple_update);
snprintf(buf, sizeof(buf), "%d", 100000*tps);
snprintf(buf, sizeof(buf), "%d", 100000 * tps);
sql_files[0][0]->argv[3] = strdup(buf);
snprintf(buf, sizeof(buf), "%d", 1*tps);
snprintf(buf, sizeof(buf), "%d", 1 * tps);
sql_files[0][1]->argv[3] = strdup(buf);
snprintf(buf, sizeof(buf), "%d", 10*tps);
snprintf(buf, sizeof(buf), "%d", 10 * tps);
sql_files[0][2]->argv[3] = strdup(buf);
snprintf(buf, sizeof(buf), "%d", 10000*tps);
snprintf(buf, sizeof(buf), "%d", 10000 * tps);
sql_files[0][3]->argv[3] = strdup(buf);
num_files = 1;
break;
@ -1344,7 +1347,7 @@ main(int argc, char **argv)
/* send start up queries in async manner */
for (i = 0; i < nclients; i++)
{
state[i].use_file = getrand(0, num_files-1);
state[i].use_file = getrand(0, num_files - 1);
doCustom(state, i, debug);
}
@ -1366,9 +1369,9 @@ main(int argc, char **argv)
maxsock = -1;
for (i = 0; i < nclients; i++)
{
Command **commands = sql_files[state[i].use_file];
Command **commands = sql_files[state[i].use_file];
if (state[i].con && commands[state[i].state]->type != META_COMMAND)
if (state[i].con && commands[state[i].state]->type != META_COMMAND)
{
int sock = PQsocket(state[i].con);
@ -1396,7 +1399,7 @@ main(int argc, char **argv)
exit(1);
}
else if (nsocks == 0)
{ /* timeout */
{ /* timeout */
fprintf(stderr, "select timeout\n");
for (i = 0; i < nclients; i++)
{
@ -1410,10 +1413,10 @@ main(int argc, char **argv)
/* ok, backend returns reply */
for (i = 0; i < nclients; i++)
{
Command **commands = sql_files[state[i].use_file];
Command **commands = sql_files[state[i].use_file];
if (state[i].con && (FD_ISSET(PQsocket(state[i].con), &input_mask)
|| commands[state[i].state]->type == META_COMMAND))
|| commands[state[i].state]->type == META_COMMAND))
{
doCustom(state, i, debug);
}

View File

@ -520,7 +520,6 @@ extern void _BF_body_r(BF_ctx * ctx);
#define BF_body() \
_BF_body_r(&data.ctx);
#else
#define BF_body() \
@ -712,7 +711,7 @@ _crypt_blowfish_rn(const char *key, const char *setting,
memcpy(output, setting, 7 + 22 - 1);
output[7 + 22 - 1] = BF_itoa64[(int)
BF_atoi64[(int) setting[7 + 22 - 1] - 0x20] & 0x30];
BF_atoi64[(int) setting[7 + 22 - 1] - 0x20] & 0x30];
/* This has to be bug-compatible with the original implementation, so
* only encode 23 of the 24 bytes. :-) */

View File

@ -246,8 +246,8 @@ des_init(void)
}
/*
* Convert the inverted S-boxes into 4 arrays of 8 bits. Each will
* handle 12 bits of the S-box input.
* Convert the inverted S-boxes into 4 arrays of 8 bits. Each will handle
* 12 bits of the S-box input.
*/
for (b = 0; b < 4; b++)
for (i = 0; i < 64; i++)
@ -267,8 +267,8 @@ des_init(void)
}
/*
* Invert the key permutation and initialise the inverted key
* compression permutation.
* Invert the key permutation and initialise the inverted key compression
* permutation.
*/
for (i = 0; i < 56; i++)
{
@ -284,8 +284,8 @@ des_init(void)
inv_comp_perm[comp_perm[i] - 1] = i;
/*
* Set up the OR-mask arrays for the initial and final permutations,
* and for the key initial and compression permutations.
* Set up the OR-mask arrays for the initial and final permutations, and
* for the key initial and compression permutations.
*/
for (k = 0; k < 8; k++)
{
@ -347,8 +347,8 @@ des_init(void)
}
/*
* Invert the P-box permutation, and convert into OR-masks for
* handling the output of the S-box arrays setup above.
* Invert the P-box permutation, and convert into OR-masks for handling
* the output of the S-box arrays setup above.
*/
for (i = 0; i < 32; i++)
un_pbox[pbox[i] - 1] = i;
@ -411,9 +411,9 @@ des_setkey(const char *key)
&& rawkey1 == old_rawkey1)
{
/*
* Already setup for this key. This optimisation fails on a zero
* key (which is weak and has bad parity anyway) in order to
* simplify the starting conditions.
* Already setup for this key. This optimisation fails on a zero key
* (which is weak and has bad parity anyway) in order to simplify the
* starting conditions.
*/
return (0);
}
@ -560,16 +560,16 @@ do_des(uint32 l_in, uint32 r_in, uint32 *l_out, uint32 *r_out, int count)
| ((r & 0x80000000) >> 31);
/*
* Do salting for crypt() and friends, and XOR with the
* permuted key.
* Do salting for crypt() and friends, and XOR with the permuted
* key.
*/
f = (r48l ^ r48r) & saltbits;
r48l ^= f ^ *kl++;
r48r ^= f ^ *kr++;
/*
* Do sbox lookups (which shrink it back to 32 bits) and do
* the pbox permutation at the same time.
* Do sbox lookups (which shrink it back to 32 bits) and do the
* pbox permutation at the same time.
*/
f = psbox[0][m_sbox[0][r48l >> 12]]
| psbox[1][m_sbox[1][r48l & 0xfff]]
@ -660,8 +660,8 @@ px_crypt_des(const char *key, const char *setting)
/*
* Copy the key, shifting each character up by one bit and padding
* with zeros.
* Copy the key, shifting each character up by one bit and padding with
* zeros.
*/
q = (uint8 *) keybuf;
while (q - (uint8 *) keybuf - 8)
@ -706,10 +706,10 @@ px_crypt_des(const char *key, const char *setting)
strncpy(output, setting, 9);
/*
* Double check that we weren't given a short setting. If we were,
* the above code will probably have created wierd values for
* count and salt, but we don't really care. Just make sure the
* output string doesn't have an extra NUL in it.
* Double check that we weren't given a short setting. If we were, the
* above code will probably have created wierd values for count and
* salt, but we don't really care. Just make sure the output string
* doesn't have an extra NUL in it.
*/
output[9] = '\0';
p = output + strlen(output);
@ -728,9 +728,9 @@ px_crypt_des(const char *key, const char *setting)
output[0] = setting[0];
/*
* If the encrypted password that the salt was extracted from is
* only 1 character long, the salt will be corrupted. We need to
* ensure that the output string doesn't have an extra NUL in it!
* If the encrypted password that the salt was extracted from is only
* 1 character long, the salt will be corrupted. We need to ensure
* that the output string doesn't have an extra NUL in it!
*/
output[1] = setting[1] ? setting[1] : output[0];

View File

@ -22,7 +22,7 @@ unsigned char _crypt_itoa64[64 + 1] =
char *
_crypt_gensalt_traditional_rn(unsigned long count,
const char *input, int size, char *output, int output_size)
const char *input, int size, char *output, int output_size)
{
if (size < 2 || output_size < 2 + 1 || (count && count != 25))
{
@ -40,7 +40,7 @@ _crypt_gensalt_traditional_rn(unsigned long count,
char *
_crypt_gensalt_extended_rn(unsigned long count,
const char *input, int size, char *output, int output_size)
const char *input, int size, char *output, int output_size)
{
unsigned long value;
@ -76,7 +76,7 @@ _crypt_gensalt_extended_rn(unsigned long count,
char *
_crypt_gensalt_md5_rn(unsigned long count,
const char *input, int size, char *output, int output_size)
const char *input, int size, char *output, int output_size)
{
unsigned long value;
@ -158,7 +158,7 @@ BF_encode(char *dst, const BF_word * src, int size)
char *
_crypt_gensalt_blowfish_rn(unsigned long count,
const char *input, int size, char *output, int output_size)
const char *input, int size, char *output, int output_size)
{
if (size < 16 || output_size < 7 + 22 + 1 ||
(count && (count < 4 || count > 31)))

View File

@ -8,7 +8,7 @@
*
* $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5 1999/12/17 20:21:45 peter Exp $
*
* $PostgreSQL: pgsql/contrib/pgcrypto/crypt-md5.c,v 1.5 2005/09/24 19:14:04 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/crypt-md5.c,v 1.6 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -24,9 +24,9 @@
char *
px_crypt_md5(const char *pw, const char *salt, char *passwd, unsigned dstlen)
{
static char *magic = "$1$"; /* This string is magic for this
* algorithm. Having it this way, we can
* get get better later on */
static char *magic = "$1$"; /* This string is magic for this algorithm.
* Having it this way, we can get get better
* later on */
static char *p;
static const char *sp,
*ep;

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/fortuna.c,v 1.4 2005/07/18 17:12:54 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/fortuna.c,v 1.5 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -43,16 +43,16 @@
* Why Fortuna-like: There does not seem to be any definitive reference
* on Fortuna in the net. Instead this implementation is based on
* following references:
*
*
* http://en.wikipedia.org/wiki/Fortuna_(PRNG)
* - Wikipedia article
* - Wikipedia article
* http://jlcooke.ca/random/
* - Jean-Luc Cooke Fortuna-based /dev/random driver for Linux.
* - Jean-Luc Cooke Fortuna-based /dev/random driver for Linux.
*/
/*
* There is some confusion about whether and how to carry forward
* the state of the pools. Seems like original Fortuna does not
* the state of the pools. Seems like original Fortuna does not
* do it, resetting hash after each request. I guess expecting
* feeding to happen more often that requesting. This is absolutely
* unsuitable for pgcrypto, as nothing asynchronous happens here.
@ -76,7 +76,7 @@
* How many pools.
*
* Original Fortuna uses 32 pools, that means 32'th pool is
* used not earlier than in 13th year. This is a waste in
* used not earlier than in 13th year. This is a waste in
* pgcrypto, as we have very low-frequancy seeding. Here
* is preferable to have all entropy usable in reasonable time.
*
@ -89,12 +89,12 @@
#define NUM_POOLS 23
/* in microseconds */
#define RESEED_INTERVAL 100000 /* 0.1 sec */
#define RESEED_INTERVAL 100000 /* 0.1 sec */
/* for one big request, reseed after this many bytes */
#define RESEED_BYTES (1024*1024)
/*
/*
* Skip reseed if pool 0 has less than this many
* bytes added since last reseed.
*/
@ -114,17 +114,18 @@
#define MD_CTX SHA256_CTX
#define CIPH_CTX rijndael_ctx
struct fortuna_state {
uint8 counter[CIPH_BLOCK];
uint8 result[CIPH_BLOCK];
uint8 key[BLOCK];
MD_CTX pool[NUM_POOLS];
CIPH_CTX ciph;
unsigned reseed_count;
struct timeval last_reseed_time;
unsigned pool0_bytes;
unsigned rnd_pos;
int counter_init;
struct fortuna_state
{
uint8 counter[CIPH_BLOCK];
uint8 result[CIPH_BLOCK];
uint8 key[BLOCK];
MD_CTX pool[NUM_POOLS];
CIPH_CTX ciph;
unsigned reseed_count;
struct timeval last_reseed_time;
unsigned pool0_bytes;
unsigned rnd_pos;
int counter_init;
};
typedef struct fortuna_state FState;
@ -137,29 +138,35 @@ typedef struct fortuna_state FState;
* - No memory allocations.
*/
static void ciph_init(CIPH_CTX *ctx, const uint8 *key, int klen)
static void
ciph_init(CIPH_CTX * ctx, const uint8 *key, int klen)
{
rijndael_set_key(ctx, (const uint32 *)key, klen, 1);
rijndael_set_key(ctx, (const uint32 *) key, klen, 1);
}
static void ciph_encrypt(CIPH_CTX *ctx, const uint8 *in, uint8 *out)
static void
ciph_encrypt(CIPH_CTX * ctx, const uint8 *in, uint8 *out)
{
rijndael_encrypt(ctx, (const uint32 *)in, (uint32 *)out);
rijndael_encrypt(ctx, (const uint32 *) in, (uint32 *) out);
}
static void md_init(MD_CTX *ctx)
static void
md_init(MD_CTX * ctx)
{
SHA256_Init(ctx);
}
static void md_update(MD_CTX *ctx, const uint8 *data, int len)
static void
md_update(MD_CTX * ctx, const uint8 *data, int len)
{
SHA256_Update(ctx, data, len);
}
static void md_result(MD_CTX *ctx, uint8 *dst)
static void
md_result(MD_CTX * ctx, uint8 *dst)
{
SHA256_CTX tmp;
SHA256_CTX tmp;
memcpy(&tmp, ctx, sizeof(*ctx));
SHA256_Final(dst, &tmp);
memset(&tmp, 0, sizeof(tmp));
@ -168,9 +175,11 @@ static void md_result(MD_CTX *ctx, uint8 *dst)
/*
* initialize state
*/
static void init_state(FState *st)
static void
init_state(FState * st)
{
int i;
int i;
memset(st, 0, sizeof(*st));
for (i = 0; i < NUM_POOLS; i++)
md_init(&st->pool[i]);
@ -180,9 +189,11 @@ static void init_state(FState *st)
* Endianess does not matter.
* It just needs to change without repeating.
*/
static void inc_counter(FState *st)
static void
inc_counter(FState * st)
{
uint32 *val = (uint32*)st->counter;
uint32 *val = (uint32 *) st->counter;
if (++val[0])
return;
if (++val[1])
@ -195,7 +206,8 @@ static void inc_counter(FState *st)
/*
* This is called 'cipher in counter mode'.
*/
static void encrypt_counter(FState *st, uint8 *dst)
static void
encrypt_counter(FState * st, uint8 *dst)
{
ciph_encrypt(&st->ciph, st->counter, dst);
inc_counter(st);
@ -206,12 +218,13 @@ static void encrypt_counter(FState *st, uint8 *dst)
* The time between reseed must be at least RESEED_INTERVAL
* microseconds.
*/
static int too_often(FState *st)
static int
too_often(FState * st)
{
int ok;
int ok;
struct timeval tv;
struct timeval *last = &st->last_reseed_time;
gettimeofday(&tv, NULL);
ok = 0;
@ -229,19 +242,19 @@ static int too_often(FState *st)
/*
* generate new key from all the pools
*/
static void reseed(FState *st)
static void
reseed(FState * st)
{
unsigned k;
unsigned n;
MD_CTX key_md;
uint8 buf[BLOCK];
unsigned k;
unsigned n;
MD_CTX key_md;
uint8 buf[BLOCK];
/* set pool as empty */
st->pool0_bytes = 0;
/*
* Both #0 and #1 reseed would use only pool 0.
* Just skip #0 then.
* Both #0 and #1 reseed would use only pool 0. Just skip #0 then.
*/
n = ++st->reseed_count;
@ -249,7 +262,8 @@ static void reseed(FState *st)
* The goal: use k-th pool only 1/(2^k) of the time.
*/
md_init(&key_md);
for (k = 0; k < NUM_POOLS; k++) {
for (k = 0; k < NUM_POOLS; k++)
{
md_result(&st->pool[k], buf);
md_update(&key_md, buf, BLOCK);
@ -272,11 +286,12 @@ static void reseed(FState *st)
}
/*
* Pick a random pool. This uses key bytes as random source.
* Pick a random pool. This uses key bytes as random source.
*/
static unsigned get_rand_pool(FState *st)
static unsigned
get_rand_pool(FState * st)
{
unsigned rnd;
unsigned rnd;
/*
* This slightly prefers lower pools - thats OK.
@ -293,11 +308,12 @@ static unsigned get_rand_pool(FState *st)
/*
* update pools
*/
static void add_entropy(FState *st, const uint8 *data, unsigned len)
static void
add_entropy(FState * st, const uint8 *data, unsigned len)
{
unsigned pos;
uint8 hash[BLOCK];
MD_CTX md;
unsigned pos;
uint8 hash[BLOCK];
MD_CTX md;
/* hash given data */
md_init(&md);
@ -305,14 +321,13 @@ static void add_entropy(FState *st, const uint8 *data, unsigned len)
md_result(&md, hash);
/*
* Make sure the pool 0 is initialized,
* then update randomly.
* Make sure the pool 0 is initialized, then update randomly.
*/
if (st->reseed_count == 0 && st->pool0_bytes < POOL0_FILL)
pos = 0;
else
pos = get_rand_pool(st);
md_update( &st->pool[pos], hash, BLOCK);
md_update(&st->pool[pos], hash, BLOCK);
if (pos == 0)
st->pool0_bytes += len;
@ -324,7 +339,8 @@ static void add_entropy(FState *st, const uint8 *data, unsigned len)
/*
* Just take 2 next blocks as new key
*/
static void rekey(FState *st)
static void
rekey(FState * st)
{
encrypt_counter(st, st->key);
encrypt_counter(st, st->key + CIPH_BLOCK);
@ -336,7 +352,8 @@ static void rekey(FState *st)
* In case it does not, slow down the attacker by initialising
* the couter to random value.
*/
static void init_counter(FState *st)
static void
init_counter(FState * st)
{
/* Use next block as counter. */
encrypt_counter(st, st->counter);
@ -348,10 +365,11 @@ static void init_counter(FState *st)
st->counter_init = 1;
}
static void extract_data(FState *st, unsigned count, uint8 *dst)
static void
extract_data(FState * st, unsigned count, uint8 *dst)
{
unsigned n;
unsigned block_nr = 0;
unsigned n;
unsigned block_nr = 0;
/* Can we reseed? */
if (st->pool0_bytes >= POOL0_FILL && !too_often(st))
@ -361,7 +379,8 @@ static void extract_data(FState *st, unsigned count, uint8 *dst)
if (!st->counter_init)
init_counter(st);
while (count > 0) {
while (count > 0)
{
/* produce bytes */
encrypt_counter(st, st->result);
@ -391,9 +410,10 @@ static void extract_data(FState *st, unsigned count, uint8 *dst)
*/
static FState main_state;
static int init_done = 0;
static int init_done = 0;
void fortuna_add_entropy(const uint8 *data, unsigned len)
void
fortuna_add_entropy(const uint8 *data, unsigned len)
{
if (!init_done)
{
@ -405,7 +425,8 @@ void fortuna_add_entropy(const uint8 *data, unsigned len)
add_entropy(&main_state, data, len);
}
void fortuna_get_bytes(unsigned len, uint8 *dst)
void
fortuna_get_bytes(unsigned len, uint8 *dst)
{
if (!init_done)
{
@ -416,4 +437,3 @@ void fortuna_get_bytes(unsigned len, uint8 *dst)
return;
extract_data(&main_state, len, dst);
}

View File

@ -26,14 +26,13 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/fortuna.h,v 1.2 2005/07/18 17:12:54 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/fortuna.h,v 1.3 2005/10/15 02:49:06 momjian Exp $
*/
#ifndef __FORTUNA_H
#define __FORTUNA_H
void fortuna_get_bytes(unsigned len, uint8 *dst);
void fortuna_add_entropy(const uint8 *data, unsigned len);
void fortuna_get_bytes(unsigned len, uint8 *dst);
void fortuna_add_entropy(const uint8 *data, unsigned len);
#endif

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.22 2005/07/18 17:12:54 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.23 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -52,7 +52,7 @@
/*
* The chance is x/256 that the reseed happens.
*/
#define SYSTEM_RESEED_CHANCE (4) /* 256/4 * 10min ~ 10h */
#define SYSTEM_RESEED_CHANCE (4) /* 256/4 * 10min ~ 10h */
/*
* If this much time has passed, force reseed.
@ -88,13 +88,13 @@ struct int_digest
};
static const struct int_digest
int_digest_list[] = {
{ "md5", init_md5 },
{ "sha1", init_sha1 },
{ "sha256", init_sha256 },
{ "sha384", init_sha384 },
{ "sha512", init_sha512 },
{ NULL, NULL }
int_digest_list[] = {
{"md5", init_md5},
{"sha1", init_sha1},
{"sha256", init_sha256},
{"sha384", init_sha384},
{"sha512", init_sha512},
{NULL, NULL}
};
/* MD5 */
@ -210,7 +210,7 @@ int_sha256_block_len(PX_MD * h)
static void
int_sha256_update(PX_MD * h, const uint8 *data, unsigned dlen)
{
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_Update(ctx, data, dlen);
}
@ -218,7 +218,7 @@ int_sha256_update(PX_MD * h, const uint8 *data, unsigned dlen)
static void
int_sha256_reset(PX_MD * h)
{
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_Init(ctx);
}
@ -226,7 +226,7 @@ int_sha256_reset(PX_MD * h)
static void
int_sha256_finish(PX_MD * h, uint8 *dst)
{
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_Final(dst, ctx);
}
@ -234,12 +234,13 @@ int_sha256_finish(PX_MD * h, uint8 *dst)
static void
int_sha256_free(PX_MD * h)
{
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
memset(ctx, 0, sizeof(*ctx));
px_free(ctx);
px_free(h);
}
/* SHA384 */
static unsigned
@ -257,7 +258,7 @@ int_sha384_block_len(PX_MD * h)
static void
int_sha384_update(PX_MD * h, const uint8 *data, unsigned dlen)
{
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_Update(ctx, data, dlen);
}
@ -265,7 +266,7 @@ int_sha384_update(PX_MD * h, const uint8 *data, unsigned dlen)
static void
int_sha384_reset(PX_MD * h)
{
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_Init(ctx);
}
@ -273,7 +274,7 @@ int_sha384_reset(PX_MD * h)
static void
int_sha384_finish(PX_MD * h, uint8 *dst)
{
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_Final(dst, ctx);
}
@ -281,7 +282,7 @@ int_sha384_finish(PX_MD * h, uint8 *dst)
static void
int_sha384_free(PX_MD * h)
{
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
memset(ctx, 0, sizeof(*ctx));
px_free(ctx);
@ -305,7 +306,7 @@ int_sha512_block_len(PX_MD * h)
static void
int_sha512_update(PX_MD * h, const uint8 *data, unsigned dlen)
{
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_Update(ctx, data, dlen);
}
@ -313,7 +314,7 @@ int_sha512_update(PX_MD * h, const uint8 *data, unsigned dlen)
static void
int_sha512_reset(PX_MD * h)
{
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_Init(ctx);
}
@ -321,7 +322,7 @@ int_sha512_reset(PX_MD * h)
static void
int_sha512_finish(PX_MD * h, uint8 *dst)
{
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_Final(dst, ctx);
}
@ -329,7 +330,7 @@ int_sha512_finish(PX_MD * h, uint8 *dst)
static void
int_sha512_free(PX_MD * h)
{
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
memset(ctx, 0, sizeof(*ctx));
px_free(ctx);
@ -381,7 +382,7 @@ init_sha1(PX_MD * md)
static void
init_sha256(PX_MD * md)
{
SHA256_CTX *ctx;
SHA256_CTX *ctx;
ctx = px_alloc(sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
@ -401,7 +402,7 @@ init_sha256(PX_MD * md)
static void
init_sha384(PX_MD * md)
{
SHA384_CTX *ctx;
SHA384_CTX *ctx;
ctx = px_alloc(sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
@ -421,7 +422,7 @@ init_sha384(PX_MD * md)
static void
init_sha512(PX_MD * md)
{
SHA512_CTX *ctx;
SHA512_CTX *ctx;
ctx = px_alloc(sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
@ -752,12 +753,12 @@ struct int_cipher
};
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 }
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[] = {
@ -828,7 +829,7 @@ px_find_cipher(const char *name, PX_Cipher ** res)
int
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
{
int i;
int i;
for (i = 0; i < count; i++)
*dst++ = random();
@ -838,12 +839,13 @@ px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
static time_t seed_time = 0;
static time_t check_time = 0;
static void system_reseed(void)
static void
system_reseed(void)
{
uint8 buf[1024];
int n;
time_t t;
int skip = 1;
uint8 buf[1024];
int n;
time_t t;
int skip = 1;
t = time(NULL);
@ -890,4 +892,3 @@ px_add_entropy(const uint8 *data, unsigned count)
fortuna_add_entropy(data, count);
return 0;
}

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/mbuf.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/mbuf.c,v 1.3 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -166,7 +166,8 @@ mbuf_grab(MBuf * mbuf, int len, uint8 **data_p)
return len;
}
int mbuf_rewind(MBuf *mbuf)
int
mbuf_rewind(MBuf * mbuf)
{
mbuf->read_pos = mbuf->data;
return 0;
@ -175,7 +176,7 @@ int mbuf_rewind(MBuf *mbuf)
int
mbuf_steal_data(MBuf * mbuf, uint8 **data_p)
{
int len = mbuf_size(mbuf);
int len = mbuf_size(mbuf);
mbuf->no_write = 1;
mbuf->own_data = 0;
@ -193,7 +194,7 @@ mbuf_steal_data(MBuf * mbuf, uint8 **data_p)
struct PullFilter
{
PullFilter *src;
PullFilter *src;
const PullFilterOps *op;
int buflen;
uint8 *buf;
@ -204,7 +205,7 @@ struct PullFilter
int
pullf_create(PullFilter ** pf_p, const PullFilterOps * op, void *init_arg, PullFilter * src)
{
PullFilter *pf;
PullFilter *pf;
void *priv;
int res;
@ -260,13 +261,14 @@ pullf_free(PullFilter * pf)
int
pullf_read(PullFilter * pf, int len, uint8 **data_p)
{
int res;
int res;
if (pf->op->pull)
{
if (pf->buflen && len > pf->buflen)
len = pf->buflen;
res = pf->op->pull(pf->priv, pf->src, len, data_p,
pf->buf, pf->buflen);
pf->buf, pf->buflen);
}
else
res = pullf_read(pf->src, len, data_p);
@ -276,8 +278,9 @@ pullf_read(PullFilter * pf, int len, uint8 **data_p)
int
pullf_read_max(PullFilter * pf, int len, uint8 **data_p, uint8 *tmpbuf)
{
int res, total;
uint8 *tmp;
int res,
total;
uint8 *tmp;
res = pullf_read(pf, len, data_p);
if (res <= 0 || res == len)
@ -288,8 +291,9 @@ pullf_read_max(PullFilter * pf, int len, uint8 **data_p, uint8 *tmpbuf)
*data_p = tmpbuf;
len -= res;
total = res;
while (len > 0) {
while (len > 0)
{
res = pullf_read(pf, len, &tmp);
if (res < 0)
{
@ -308,10 +312,12 @@ pullf_read_max(PullFilter * pf, int len, uint8 **data_p, uint8 *tmpbuf)
/*
* caller wants exatly len bytes and dont bother with references
*/
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
int
pullf_read_fixed(PullFilter * src, int len, uint8 *dst)
{
int res;
uint8 *p;
int res;
uint8 *p;
res = pullf_read_max(src, len, &p, dst);
if (res < 0)
return res;
@ -330,9 +336,10 @@ int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
*/
static int
pull_from_mbuf(void *arg, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen)
uint8 **data_p, uint8 *buf, int buflen)
{
MBuf *mbuf = arg;
return mbuf_grab(mbuf, len, data_p);
}
@ -364,7 +371,7 @@ struct PushFilter
int
pushf_create(PushFilter ** mp_p, const PushFilterOps * op, void *init_arg, PushFilter * next)
{
PushFilter *mp;
PushFilter *mp;
void *priv;
int res;
@ -419,7 +426,7 @@ pushf_free(PushFilter * mp)
void
pushf_free_all(PushFilter * mp)
{
PushFilter *tmp;
PushFilter *tmp;
while (mp)
{
@ -549,8 +556,8 @@ static const struct PushFilterOps mbuf_filter = {
NULL, push_into_mbuf, NULL, NULL
};
int pushf_create_mbuf_writer(PushFilter **res, MBuf *dst)
int
pushf_create_mbuf_writer(PushFilter ** res, MBuf * dst)
{
return pushf_create(res, &mbuf_filter, dst, 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/mbuf.h,v 1.1 2005/07/10 13:46:28 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/mbuf.h,v 1.2 2005/10/15 02:49:06 momjian Exp $
*/
#ifndef __PX_MBUF_H
@ -40,32 +40,36 @@ typedef struct PullFilterOps PullFilterOps;
struct PushFilterOps
{
/* should return needed buffer size, 0- no buffering, <0 on error
* if NULL, no buffering, and priv=init_arg
/*
* should return needed buffer size, 0- no buffering, <0 on error if NULL,
* no buffering, and priv=init_arg
*/
int (*init) (PushFilter * next, void *init_arg, void **priv_p);
/* send data to next. should consume all?
* if null, it will be simply copied (in-place)
* returns 0 on error
/*
* send data to next. should consume all? if null, it will be simply
* copied (in-place) returns 0 on error
*/
int (*push) (PushFilter * next, void *priv,
const uint8 *src, int len);
const uint8 *src, int len);
int (*flush) (PushFilter * next, void *priv);
void (*free) (void *priv);
};
struct PullFilterOps
{
/* should return needed buffer size, 0- no buffering, <0 on error
* if NULL, no buffering, and priv=init_arg
/*
* should return needed buffer size, 0- no buffering, <0 on error if NULL,
* no buffering, and priv=init_arg
*/
int (*init) (void **priv_p, void *init_arg, PullFilter * src);
/* request data from src, put result ptr to data_p
* can use ptr from src or use buf as work area
* if NULL in-place copy
/*
* request data from src, put result ptr to data_p can use ptr from src or
* use buf as work area if NULL in-place copy
*/
int (*pull) (void *priv, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen);
uint8 **data_p, uint8 *buf, int buflen);
void (*free) (void *priv);
};
@ -86,8 +90,8 @@ int mbuf_free(MBuf * mbuf);
/*
* Push filter
*/
int pushf_create(PushFilter ** res, const PushFilterOps * ops, void *init_arg,
PushFilter * next);
int pushf_create(PushFilter ** res, const PushFilterOps * ops, void *init_arg,
PushFilter * next);
int pushf_write(PushFilter * mp, const uint8 *data, int len);
void pushf_free_all(PushFilter * mp);
void pushf_free(PushFilter * mp);
@ -98,13 +102,13 @@ int pushf_create_mbuf_writer(PushFilter ** mp_p, MBuf * mbuf);
/*
* Pull filter
*/
int pullf_create(PullFilter ** res, const PullFilterOps * ops,
void *init_arg, PullFilter * src);
int pullf_create(PullFilter ** res, const PullFilterOps * ops,
void *init_arg, PullFilter * src);
int pullf_read(PullFilter * mp, int len, uint8 **data_p);
int pullf_read_max(PullFilter * mp, int len,
uint8 **data_p, uint8 *tmpbuf);
int pullf_read_max(PullFilter * mp, int len,
uint8 **data_p, uint8 *tmpbuf);
void pullf_free(PullFilter * mp);
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst);
int pullf_read_fixed(PullFilter * src, int len, uint8 *dst);
int pullf_create_mbuf_reader(PullFilter ** pf_p, MBuf * mbuf);
@ -118,4 +122,3 @@ int pullf_create_mbuf_reader(PullFilter ** pf_p, MBuf * mbuf);
} while (0)
#endif /* __PX_MBUF_H */

View File

@ -1,4 +1,4 @@
/* $PostgreSQL: pgsql/contrib/pgcrypto/md5.h,v 1.8 2003/11/29 22:39:28 pgsql Exp $ */
/* $PostgreSQL: pgsql/contrib/pgcrypto/md5.h,v 1.9 2005/10/15 02:49:06 momjian Exp $ */
/* $KAME: md5.h,v 1.3 2000/02/22 14:01:18 itojun Exp $ */
/*

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.25 2005/07/12 20:27:42 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.26 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -47,14 +47,13 @@
#define MAX_IV (128/8)
/*
* Does OpenSSL support AES?
* Does OpenSSL support AES?
*/
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
/* Yes, it does. */
#include <openssl/aes.h>
#else /* old OPENSSL */
#else /* old OPENSSL */
/*
* No, it does not. So use included rijndael code to emulate it.
@ -91,8 +90,7 @@
memcpy(iv, (src) + (len) - 16, 16); \
} \
} while (0)
#endif /* old OPENSSL */
#endif /* old OPENSSL */
/*
* Compatibility with older OpenSSL API for DES.
@ -157,8 +155,8 @@ digest_finish(PX_MD * h, uint8 *dst)
EVP_DigestFinal(ctx, dst, NULL);
/*
* Some builds of 0.9.7x clear all of ctx in EVP_DigestFinal.
* Fix it by reinitializing ctx.
* Some builds of 0.9.7x clear all of ctx in EVP_DigestFinal. Fix it by
* reinitializing ctx.
*/
EVP_DigestInit(ctx, md);
}
@ -246,7 +244,9 @@ typedef struct
} des;
struct
{
DES_key_schedule k1, k2, k3;
DES_key_schedule k1,
k2,
k3;
} des3;
CAST_KEY cast_key;
AES_KEY aes_key;
@ -597,12 +597,12 @@ ossl_aes_init(PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv)
ossldata *od = c->ptr;
unsigned bs = gen_ossl_block_size(c);
if (klen <= 128/8)
od->klen = 128/8;
else if (klen <= 192/8)
od->klen = 192/8;
else if (klen <= 256/8)
od->klen = 256/8;
if (klen <= 128 / 8)
od->klen = 128 / 8;
else if (klen <= 192 / 8)
od->klen = 192 / 8;
else if (klen <= 256 / 8)
od->klen = 256 / 8;
else
return PXE_KEY_TOO_BIG;
@ -825,7 +825,8 @@ static int openssl_random_init = 0;
* OpenSSL random should re-feeded occasionally. From /dev/urandom
* preferably.
*/
static void init_openssl_rand(void)
static void
init_openssl_rand(void)
{
if (RAND_get_rand_method() == NULL)
RAND_set_rand_method(RAND_SSLeay());
@ -871,4 +872,3 @@ px_add_entropy(const uint8 *data, unsigned count)
RAND_add(data, count, 0);
return 0;
}

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.19 2005/09/24 19:14:04 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.20 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -244,7 +244,7 @@ pg_gen_salt_rounds(PG_FUNCTION_ARGS)
if (len < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("gen_salt: %s", px_strerror(len))));
errmsg("gen_salt: %s", px_strerror(len))));
res = (text *) palloc(len + VARHDRSZ);
VARATT_SIZEP(res) = len + VARHDRSZ;

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/pgp-armor.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-armor.c,v 1.3 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -40,7 +40,7 @@
*/
static const unsigned char _base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int
b64_encode(const uint8 *src, unsigned len, uint8 *dst)
@ -96,13 +96,13 @@ static int
b64_decode(const uint8 *src, unsigned len, uint8 *dst)
{
const uint8 *srcend = src + len,
*s = src;
*s = src;
uint8 *p = dst;
char c;
unsigned b = 0;
unsigned long buf = 0;
int pos = 0,
end = 0;
end = 0;
while (s < srcend)
{
@ -242,11 +242,13 @@ static const uint8 *
find_str(const uint8 *data, const uint8 *data_end, const char *str, int strlen)
{
const uint8 *p = data;
if (!strlen)
return NULL;
if (data_end - data < strlen)
return NULL;
while (p < data_end) {
while (p < data_end)
{
p = memchr(p, str[0], data_end - p);
if (p == NULL)
return NULL;
@ -261,7 +263,7 @@ find_str(const uint8 *data, const uint8 *data_end, const char *str, int strlen)
static int
find_header(const uint8 *data, const uint8 *datend,
const uint8 **start_p, int is_end)
const uint8 **start_p, int is_end)
{
const uint8 *p = data;
static const char *start_sep = "-----BEGIN";
@ -285,7 +287,7 @@ find_header(const uint8 *data, const uint8 *datend,
/* check if header text ok */
for (; p < datend && *p != '-'; p++)
{
/* various junk can be there, but definitely not line-feed */
/* various junk can be there, but definitely not line-feed */
if (*p >= ' ')
continue;
return PXE_PGP_CORRUPT_ARMOR;
@ -313,7 +315,8 @@ pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst)
const uint8 *p = src;
const uint8 *data_end = src + len;
long crc;
const uint8 *base64_start, *armor_end;
const uint8 *base64_start,
*armor_end;
const uint8 *base64_end = NULL;
uint8 buf[4];
int hlen;
@ -329,7 +332,7 @@ pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst)
hlen = find_header(p, data_end, &armor_end, 1);
if (hlen <= 0)
goto out;
/* skip comments - find empty line */
while (p < armor_end && *p != '\n' && *p != '\r')
{
@ -341,7 +344,7 @@ pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst)
p++;
}
base64_start = p;
/* find crc pos */
for (p = armor_end; p >= base64_start; p--)
if (*p == '=')
@ -355,7 +358,7 @@ pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst)
/* decode crc */
if (b64_decode(p + 1, 4, buf) != 3)
goto out;
crc = (((long)buf[0]) << 16) + (((long)buf[1]) << 8) + (long)buf[2];
crc = (((long) buf[0]) << 16) + (((long) buf[1]) << 8) + (long) buf[2];
/* decode data */
res = b64_decode(base64_start, base64_end - base64_start, dst);
@ -378,4 +381,3 @@ pgp_armor_dec_len(unsigned len)
{
return b64_dec_len(len);
}

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/pgp-cfb.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-cfb.c,v 1.3 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -35,7 +35,7 @@
#include "px.h"
#include "pgp.h"
typedef int (*mix_data_t)(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
typedef int (*mix_data_t) (PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst);
struct PGP_CFB
{
@ -50,12 +50,12 @@ struct PGP_CFB
};
int
pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len,
int resync, uint8 *iv)
pgp_cfb_create(PGP_CFB ** ctx_p, int algo, const uint8 *key, int key_len,
int resync, uint8 *iv)
{
int res;
PX_Cipher *ciph;
PGP_CFB *ctx;
int res;
PX_Cipher *ciph;
PGP_CFB *ctx;
res = pgp_load_cipher(algo, &ciph);
if (res < 0)
@ -82,7 +82,7 @@ pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len,
}
void
pgp_cfb_free(PGP_CFB *ctx)
pgp_cfb_free(PGP_CFB * ctx)
{
px_cipher_free(ctx->ciph);
memset(ctx, 0, sizeof(*ctx));
@ -90,12 +90,13 @@ pgp_cfb_free(PGP_CFB *ctx)
}
/*
* Data processing for normal CFB. (PGP_PKT_SYMENCRYPTED_DATA_MDC)
* Data processing for normal CFB. (PGP_PKT_SYMENCRYPTED_DATA_MDC)
*/
static int
mix_encrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
mix_encrypt_normal(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
{
int i;
int i;
for (i = ctx->pos; i < ctx->pos + len; i++)
*dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
ctx->pos += len;
@ -103,9 +104,10 @@ mix_encrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
}
static int
mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
mix_decrypt_normal(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
{
int i;
int i;
for (i = ctx->pos; i < ctx->pos + len; i++)
{
ctx->encbuf[i] = *data++;
@ -122,9 +124,11 @@ mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
* thus its all concentrated here.
*/
static int
mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
mix_encrypt_resync(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
{
int i,n;
int i,
n;
/* block #2 is 2 bytes long */
if (ctx->block_no == 2)
{
@ -152,9 +156,11 @@ mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
}
static int
mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
mix_decrypt_resync(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
{
int i,n;
int i,
n;
/* block #2 is 2 bytes long */
if (ctx->block_no == 2)
{
@ -190,11 +196,11 @@ mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
* common code for both encrypt and decrypt.
*/
static int
cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst,
mix_data_t mix_data)
cfb_process(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst,
mix_data_t mix_data)
{
int n;
int res;
int n;
int res;
while (len > 0 && ctx->pos > 0)
{
@ -243,16 +249,17 @@ cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst,
*/
int
pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
pgp_cfb_encrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
{
mix_data_t mix = ctx->resync ? mix_encrypt_resync : mix_encrypt_normal;
mix_data_t mix = ctx->resync ? mix_encrypt_resync : mix_encrypt_normal;
return cfb_process(ctx, data, len, dst, mix);
}
int
pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
pgp_cfb_decrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
{
mix_data_t mix = ctx->resync ? mix_decrypt_resync : mix_decrypt_normal;
mix_data_t mix = ctx->resync ? mix_decrypt_resync : mix_decrypt_normal;
return cfb_process(ctx, data, len, dst, mix);
}

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/pgp-compress.c,v 1.4 2005/07/18 17:09:01 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-compress.c,v 1.5 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -180,12 +180,12 @@ compress_free(void *priv)
}
static const PushFilterOps
compress_filter = {
compress_filter = {
compress_init, compress_process, compress_flush, compress_free
};
int
pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
pgp_compress_filter(PushFilter ** res, PGP_Context * ctx, PushFilter * dst)
{
return pushf_create(res, &compress_filter, ctx, dst);
}
@ -195,8 +195,8 @@ pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
*/
struct DecomprData
{
int buf_len; /* = ZIP_OUT_BUF */
int buf_data; /* available data */
int buf_len; /* = ZIP_OUT_BUF */
int buf_data; /* available data */
uint8 *pos;
z_stream stream;
int eof;
@ -204,14 +204,14 @@ struct DecomprData
};
static int
decompress_init(void **priv_p, void *arg, PullFilter *src)
decompress_init(void **priv_p, void *arg, PullFilter * src)
{
PGP_Context *ctx = arg;
struct DecomprData *dec;
int res;
int res;
if (ctx->compress_algo != PGP_COMPR_ZLIB
&& ctx->compress_algo != PGP_COMPR_ZIP)
&& ctx->compress_algo != PGP_COMPR_ZIP)
return PXE_PGP_UNSUPPORTED_COMPR;
dec = px_alloc(sizeof(*dec));
@ -232,15 +232,16 @@ decompress_init(void **priv_p, void *arg, PullFilter *src)
px_debug("decompress_init: inflateInit error");
return PXE_PGP_COMPRESSION_ERROR;
}
return 0;
}
static int decompress_read(void *priv, PullFilter *src, int len,
uint8 **data_p, uint8 *buf, int buflen)
static int
decompress_read(void *priv, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen)
{
int res;
int flush;
int res;
int flush;
struct DecomprData *dec = priv;
restart:
@ -256,24 +257,26 @@ restart:
if (dec->eof)
return 0;
if (dec->stream.avail_in == 0) {
uint8 *tmp;
if (dec->stream.avail_in == 0)
{
uint8 *tmp;
res = pullf_read(src, 8192, &tmp);
if (res < 0)
return res;
dec->stream.next_in = tmp;
dec->stream.avail_in = res;
}
dec->stream.next_out = dec->buf;
dec->stream.avail_out = dec->buf_len;
dec->pos = dec->buf;
/*
* Z_SYNC_FLUSH is tell zlib to output as much as possible.
* It should do it anyway (Z_NO_FLUSH), but seems to reserve
* the right not to. So lets follow the API.
* Z_SYNC_FLUSH is tell zlib to output as much as possible. It should do
* it anyway (Z_NO_FLUSH), but seems to reserve the right not to. So lets
* follow the API.
*/
flush = dec->stream.avail_in ? Z_SYNC_FLUSH : Z_FINISH;
res = inflate(&dec->stream, flush);
@ -289,39 +292,38 @@ restart:
goto restart;
}
static void decompress_free(void *priv)
static void
decompress_free(void *priv)
{
struct DecomprData *dec = priv;
inflateEnd(&dec->stream);
memset(dec, 0, sizeof(*dec));
px_free(dec);
}
static const PullFilterOps
decompress_filter = {
decompress_filter = {
decompress_init, decompress_read, decompress_free
};
int
pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
pgp_decompress_filter(PullFilter ** res, PGP_Context * ctx, PullFilter * src)
{
return pullf_create(res, &decompress_filter, ctx, src);
}
#else /* DISABLE_ZLIB */
#else /* DISABLE_ZLIB */
int
pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
pgp_compress_filter(PushFilter ** res, PGP_Context * ctx, PushFilter * dst)
{
return PXE_PGP_UNSUPPORTED_COMPR;
}
int
pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
pgp_decompress_filter(PullFilter ** res, PGP_Context * ctx, PullFilter * src)
{
return PXE_PGP_UNSUPPORTED_COMPR;
}
#endif

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/pgp-decrypt.c,v 1.5 2005/09/24 19:14:04 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-decrypt.c,v 1.6 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -80,7 +80,7 @@ parse_new_len(PullFilter * src, int *len_p)
len = 1 << (b & 0x1F);
pkttype = PKT_STREAM;
}
if (len < 0 || len > MAX_CHUNK)
{
px_debug("parse_new_len: weird length");
@ -130,7 +130,7 @@ pgp_parse_pkt_hdr(PullFilter * src, uint8 *tag, int *len_p, int allow_ctx)
{
int lentype;
int res;
uint8 *p;
uint8 *p;
/* EOF is normal here, thus we dont use GETBYTE */
res = pullf_read(src, 1, &p);
@ -165,15 +165,17 @@ pgp_parse_pkt_hdr(PullFilter * src, uint8 *tag, int *len_p, int allow_ctx)
/*
* Packet reader
*/
struct PktData {
int type;
int len;
struct PktData
{
int type;
int len;
};
static int pktreader_pull(void *priv, PullFilter *src, int len,
uint8 **data_p, uint8 *buf, int buflen)
static int
pktreader_pull(void *priv, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen)
{
int res;
int res;
struct PktData *pkt = priv;
/* PKT_CONTEXT means: whatever there is */
@ -207,6 +209,7 @@ static void
pktreader_free(void *priv)
{
struct PktData *pkt = priv;
memset(pkt, 0, sizeof(*pkt));
px_free(pkt);
}
@ -217,11 +220,12 @@ static struct PullFilterOps pktreader_filter = {
/* needs helper function to pass several parameters */
int
pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
int pkttype, PGP_Context *ctx)
pgp_create_pkt_reader(PullFilter ** pf_p, PullFilter * src, int len,
int pkttype, PGP_Context * ctx)
{
int res;
int res;
struct PktData *pkt = px_alloc(sizeof(*pkt));
pkt->type = pkttype;
pkt->len = len;
res = pullf_create(pf_p, &pktreader_filter, pkt, src);
@ -234,13 +238,14 @@ pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
* Prefix check filter
*/
static int prefix_init(void **priv_p, void *arg, PullFilter *src)
static int
prefix_init(void **priv_p, void *arg, PullFilter * src)
{
PGP_Context *ctx = arg;
int len;
int res;
uint8 *buf;
uint8 tmpbuf[PGP_MAX_BLOCK + 2];
int len;
int res;
uint8 *buf;
uint8 tmpbuf[PGP_MAX_BLOCK + 2];
len = pgp_get_cipher_block_size(ctx->cipher_algo);
if (len > sizeof(tmpbuf))
@ -259,20 +264,19 @@ static int prefix_init(void **priv_p, void *arg, PullFilter *src)
if (buf[len - 2] != buf[len] || buf[len - 1] != buf[len + 1])
{
px_debug("prefix_init: corrupt prefix");
/*
* The original purpose of the 2-byte check was
* to show user a friendly "wrong key" message.
* This made following possible:
* The original purpose of the 2-byte check was to show user a
* friendly "wrong key" message. This made following possible:
*
* "An Attack on CFB Mode Encryption As Used By OpenPGP"
* by Serge Mister and Robert Zuccherato
* "An Attack on CFB Mode Encryption As Used By OpenPGP" by Serge Mister
* and Robert Zuccherato
*
* To avoid being 'oracle', we delay reporting, which
* basically means we prefer to run into corrupt packet
* header.
* To avoid being 'oracle', we delay reporting, which basically means we
* prefer to run into corrupt packet header.
*
* We _could_ throw PXE_PGP_CORRUPT_DATA here, but
* there is possibility of attack via timing, so we don't.
* We _could_ throw PXE_PGP_CORRUPT_DATA here, but there is possibility
* of attack via timing, so we don't.
*/
ctx->corrupt_prefix = 1;
}
@ -289,9 +293,10 @@ static struct PullFilterOps prefix_filter = {
* Decrypt filter
*/
static int decrypt_init(void **priv_p, void *arg, PullFilter *src)
static int
decrypt_init(void **priv_p, void *arg, PullFilter * src)
{
PGP_CFB *cfb = arg;
PGP_CFB *cfb = arg;
*priv_p = cfb;
@ -299,15 +304,17 @@ static int decrypt_init(void **priv_p, void *arg, PullFilter *src)
return 4096;
}
static int decrypt_read(void *priv, PullFilter *src, int len,
uint8 **data_p, uint8 *buf, int buflen)
static int
decrypt_read(void *priv, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen)
{
PGP_CFB *cfb = priv;
uint8 *tmp;
int res;
PGP_CFB *cfb = priv;
uint8 *tmp;
int res;
res = pullf_read(src, len, &tmp);
if (res > 0) {
if (res > 0)
{
pgp_cfb_decrypt(cfb, tmp, res, buf);
*data_p = buf;
}
@ -323,28 +330,33 @@ struct PullFilterOps pgp_decrypt_filter = {
* MDC hasher filter
*/
static int mdc_init(void **priv_p, void *arg, PullFilter *src)
static int
mdc_init(void **priv_p, void *arg, PullFilter * src)
{
PGP_Context *ctx = arg;
*priv_p = ctx;
return pgp_load_digest(PGP_DIGEST_SHA1, &ctx->mdc_ctx);
}
static void mdc_free(void *priv)
static void
mdc_free(void *priv)
{
PGP_Context *ctx = priv;
if (ctx->use_mdcbuf_filter)
return;
px_md_free(ctx->mdc_ctx);
ctx->mdc_ctx = NULL;
}
static int mdc_finish(PGP_Context *ctx, PullFilter *src,
int len, uint8 **data_p)
static int
mdc_finish(PGP_Context * ctx, PullFilter * src,
int len, uint8 **data_p)
{
int res;
uint8 hash[20];
uint8 tmpbuf[22];
int res;
uint8 hash[20];
uint8 tmpbuf[22];
if (len + 1 > sizeof(tmpbuf))
return PXE_BUG;
@ -362,7 +374,7 @@ static int mdc_finish(PGP_Context *ctx, PullFilter *src,
}
return 0;
}
/* safety check */
if (ctx->in_mdc_pkt > 1)
{
@ -370,14 +382,14 @@ static int mdc_finish(PGP_Context *ctx, PullFilter *src,
return PXE_PGP_CORRUPT_DATA;
}
ctx->in_mdc_pkt++;
/* is the packet sane? */
if (res != 20)
{
px_debug("mdc_finish: read failed, res=%d", res);
return PXE_PGP_CORRUPT_DATA;
}
/*
* ok, we got the hash, now check
*/
@ -394,10 +406,11 @@ static int mdc_finish(PGP_Context *ctx, PullFilter *src,
return len;
}
static int mdc_read(void *priv, PullFilter *src, int len,
uint8 **data_p, uint8 *buf, int buflen)
static int
mdc_read(void *priv, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen)
{
int res;
int res;
PGP_Context *ctx = priv;
/* skip this filter? */
@ -434,18 +447,20 @@ static struct PullFilterOps mdc_filter = {
* packet, which is silly.
*/
#define MDCBUF_LEN 8192
struct MDCBufData {
struct MDCBufData
{
PGP_Context *ctx;
int eof;
int buflen;
int avail;
uint8 *pos;
int mdc_avail;
uint8 mdc_buf[22];
uint8 buf[MDCBUF_LEN];
int eof;
int buflen;
int avail;
uint8 *pos;
int mdc_avail;
uint8 mdc_buf[22];
uint8 buf[MDCBUF_LEN];
};
static int mdcbuf_init(void **priv_p, void *arg, PullFilter *src)
static int
mdcbuf_init(void **priv_p, void *arg, PullFilter * src)
{
PGP_Context *ctx = arg;
struct MDCBufData *st;
@ -462,10 +477,11 @@ static int mdcbuf_init(void **priv_p, void *arg, PullFilter *src)
return 0;
}
static int mdcbuf_finish(struct MDCBufData *st)
static int
mdcbuf_finish(struct MDCBufData * st)
{
uint8 hash[20];
int res;
uint8 hash[20];
int res;
st->eof = 1;
@ -486,25 +502,29 @@ static int mdcbuf_finish(struct MDCBufData *st)
return res;
}
static void mdcbuf_load_data(struct MDCBufData *st, uint8 *src, int len)
static void
mdcbuf_load_data(struct MDCBufData * st, uint8 *src, int len)
{
uint8 *dst = st->pos + st->avail;
uint8 *dst = st->pos + st->avail;
memcpy(dst, src, len);
px_md_update(st->ctx->mdc_ctx, src, len);
st->avail += len;
}
static void mdcbuf_load_mdc(struct MDCBufData *st, uint8 *src, int len)
static void
mdcbuf_load_mdc(struct MDCBufData * st, uint8 *src, int len)
{
memmove(st->mdc_buf + st->mdc_avail, src, len);
st->mdc_avail += len;
}
static int mdcbuf_refill(struct MDCBufData *st, PullFilter *src)
static int
mdcbuf_refill(struct MDCBufData * st, PullFilter * src)
{
uint8 *data;
int res;
int need;
uint8 *data;
int res;
int need;
/* put avail data in start */
if (st->avail > 0 && st->pos != st->buf)
@ -530,7 +550,8 @@ static int mdcbuf_refill(struct MDCBufData *st, PullFilter *src)
}
else
{
int canmove = st->mdc_avail + res - 22;
int canmove = st->mdc_avail + res - 22;
if (canmove > 0)
{
mdcbuf_load_data(st, st->mdc_buf, canmove);
@ -542,11 +563,12 @@ static int mdcbuf_refill(struct MDCBufData *st, PullFilter *src)
return 0;
}
static int mdcbuf_read(void *priv, PullFilter *src, int len,
uint8 **data_p, uint8 *buf, int buflen)
static int
mdcbuf_read(void *priv, PullFilter * src, int len,
uint8 **data_p, uint8 *buf, int buflen)
{
struct MDCBufData *st = priv;
int res;
int res;
if (!st->eof && len > st->avail)
{
@ -568,6 +590,7 @@ static void
mdcbuf_free(void *priv)
{
struct MDCBufData *st = priv;
px_md_free(st->ctx->mdc_ctx);
st->ctx->mdc_ctx = NULL;
memset(st, 0, sizeof(*st));
@ -583,29 +606,30 @@ static struct PullFilterOps mdcbuf_filter = {
* Decrypt separate session key
*/
static int
decrypt_key(PGP_Context *ctx, const uint8 *src, int len)
decrypt_key(PGP_Context * ctx, const uint8 *src, int len)
{
int res;
uint8 algo;
PGP_CFB *cfb;
int res;
uint8 algo;
PGP_CFB *cfb;
res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
if (res < 0)
return res;
pgp_cfb_decrypt(cfb, src, 1, &algo);
src ++;
len --;
src++;
len--;
pgp_cfb_decrypt(cfb, src, len, ctx->sess_key);
pgp_cfb_free(cfb);
ctx->sess_key_len = len;
ctx->cipher_algo = algo;
if (pgp_get_cipher_key_size(algo) != len) {
if (pgp_get_cipher_key_size(algo) != len)
{
px_debug("sesskey bad len: algo=%d, expected=%d, got=%d",
algo, pgp_get_cipher_key_size(algo), len);
algo, pgp_get_cipher_key_size(algo), len);
return PXE_PGP_CORRUPT_DATA;
}
return 0;
@ -643,7 +667,7 @@ parse_symenc_sesskey(PGP_Context * ctx, PullFilter * src)
* generate key from password
*/
res = pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
ctx->sym_key, ctx->sym_key_len);
ctx->sym_key, ctx->sym_key_len);
if (res < 0)
return res;
@ -684,21 +708,23 @@ parse_symenc_sesskey(PGP_Context * ctx, PullFilter * src)
}
static int
copy_crlf(MBuf *dst, uint8 *data, int len, int *got_cr)
copy_crlf(MBuf * dst, uint8 *data, int len, int *got_cr)
{
uint8 *data_end = data + len;
uint8 tmpbuf[1024];
uint8 *tmp_end = tmpbuf + sizeof(tmpbuf);
uint8 *p;
int res;
uint8 *data_end = data + len;
uint8 tmpbuf[1024];
uint8 *tmp_end = tmpbuf + sizeof(tmpbuf);
uint8 *p;
int res;
p = tmpbuf;
if (*got_cr) {
if (*got_cr)
{
if (*data != '\n')
*p++ = '\r';
*got_cr = 0;
}
while (data < data_end) {
while (data < data_end)
{
if (*data == '\r')
{
if (data + 1 < data_end)
@ -779,8 +805,9 @@ parse_literal_data(PGP_Context * ctx, MBuf * dst, PullFilter * pkt)
ctx->unicode_mode = (type == 'u') ? 1 : 0;
/* read data */
while (1) {
res = pullf_read(pkt, 32*1024, &buf);
while (1)
{
res = pullf_read(pkt, 32 * 1024, &buf);
if (res <= 0)
break;
@ -797,8 +824,8 @@ parse_literal_data(PGP_Context * ctx, MBuf * dst, PullFilter * pkt)
}
/* process_data_packets and parse_compressed_data call each other */
static int process_data_packets(PGP_Context * ctx, MBuf * dst,
PullFilter * src, int allow_compr, int need_mdc);
static int process_data_packets(PGP_Context * ctx, MBuf * dst,
PullFilter * src, int allow_compr, int need_mdc);
static int
parse_compressed_data(PGP_Context * ctx, MBuf * dst, PullFilter * pkt)
@ -822,7 +849,7 @@ parse_compressed_data(PGP_Context * ctx, MBuf * dst, PullFilter * pkt)
if (res >= 0)
{
res = process_data_packets(ctx, dst, pf_decompr,
NO_COMPR, NO_MDC);
NO_COMPR, NO_MDC);
pullf_free(pf_decompr);
}
break;
@ -850,7 +877,7 @@ process_data_packets(PGP_Context * ctx, MBuf * dst, PullFilter * src,
int got_data = 0;
int got_mdc = 0;
PullFilter *pkt = NULL;
uint8 *tmp;
uint8 *tmp;
while (1)
{
@ -951,12 +978,12 @@ static int
parse_symenc_data(PGP_Context * ctx, PullFilter * pkt, MBuf * dst)
{
int res;
PGP_CFB *cfb = NULL;
PGP_CFB *cfb = NULL;
PullFilter *pf_decrypt = NULL;
PullFilter *pf_prefix = NULL;
res = pgp_cfb_create(&cfb, ctx->cipher_algo,
ctx->sess_key, ctx->sess_key_len, 1, NULL);
ctx->sess_key, ctx->sess_key_len, 1, NULL);
if (res < 0)
goto out;
@ -985,11 +1012,11 @@ static int
parse_symenc_mdc_data(PGP_Context * ctx, PullFilter * pkt, MBuf * dst)
{
int res;
PGP_CFB *cfb = NULL;
PGP_CFB *cfb = NULL;
PullFilter *pf_decrypt = NULL;
PullFilter *pf_prefix = NULL;
PullFilter *pf_mdc = NULL;
uint8 ver;
uint8 ver;
GETBYTE(pkt, ver);
if (ver != 1)
@ -999,7 +1026,7 @@ parse_symenc_mdc_data(PGP_Context * ctx, PullFilter * pkt, MBuf * dst)
}
res = pgp_cfb_create(&cfb, ctx->cipher_algo,
ctx->sess_key, ctx->sess_key_len, 0, NULL);
ctx->sess_key, ctx->sess_key_len, 0, NULL);
if (res < 0)
goto out;
@ -1034,49 +1061,52 @@ out:
* skip over packet contents
*/
int
pgp_skip_packet(PullFilter *pkt)
pgp_skip_packet(PullFilter * pkt)
{
int res = 1;
uint8 *tmp;
while (res > 0)
res = pullf_read(pkt, 32*1024, &tmp);
return res < 0 ? res : 0;
int res = 1;
uint8 *tmp;
while (res > 0)
res = pullf_read(pkt, 32 * 1024, &tmp);
return res < 0 ? res : 0;
}
/*
* expect to be at packet end, any data is error
*/
int
pgp_expect_packet_end(PullFilter *pkt)
pgp_expect_packet_end(PullFilter * pkt)
{
int res = 1;
uint8 *tmp;
while (res > 0)
int res = 1;
uint8 *tmp;
while (res > 0)
{
res = pullf_read(pkt, 32*1024, &tmp);
res = pullf_read(pkt, 32 * 1024, &tmp);
if (res > 0)
{
px_debug("pgp_expect_packet_end: got data");
return PXE_PGP_CORRUPT_DATA;
}
}
return res < 0 ? res : 0;
return res < 0 ? res : 0;
}
int
pgp_decrypt(PGP_Context * ctx, MBuf * msrc, MBuf * mdst)
{
int res;
int res;
PullFilter *src = NULL;
PullFilter *pkt = NULL;
uint8 tag;
int len;
int got_key = 0;
int got_data = 0;
uint8 tag;
int len;
int got_key = 0;
int got_data = 0;
res = pullf_create_mbuf_reader(&src, msrc);
while (res >= 0) {
while (res >= 0)
{
res = pgp_parse_pkt_hdr(src, &tag, &len, NO_CTX_SIZE);
if (res <= 0)
break;
@ -1086,7 +1116,8 @@ pgp_decrypt(PGP_Context * ctx, MBuf * msrc, MBuf * mdst)
break;
res = PXE_PGP_CORRUPT_DATA;
switch (tag) {
switch (tag)
{
case PGP_PKT_MARKER:
res = pgp_skip_packet(pkt);
break;
@ -1097,10 +1128,11 @@ pgp_decrypt(PGP_Context * ctx, MBuf * msrc, MBuf * mdst)
break;
case PGP_PKT_SYMENCRYPTED_SESSKEY:
if (got_key)
/* Theoretically, there could be several keys,
* both public and symmetric, all of which
* encrypt same session key. Decrypt should try
* with each one, before failing.
/*
* Theoretically, there could be several keys, both public
* and symmetric, all of which encrypt same session key.
* Decrypt should try with each one, before failing.
*/
px_debug("pgp_decrypt: using first of several keys");
else
@ -1154,4 +1186,3 @@ pgp_decrypt(PGP_Context * ctx, MBuf * msrc, MBuf * mdst)
return res;
}

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/pgp-encrypt.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-encrypt.c,v 1.3 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -65,9 +65,11 @@ render_newlen(uint8 *h, int len)
return h;
}
static int write_tag_only(PushFilter *dst, int tag)
static int
write_tag_only(PushFilter * dst, int tag)
{
uint8 hdr = 0xC0 | tag;
uint8 hdr = 0xC0 | tag;
return pushf_write(dst, &hdr, 1);
}
@ -121,7 +123,7 @@ mdc_flush(PushFilter * dst, void *priv)
* create mdc pkt
*/
pkt[0] = 0xD3;
pkt[1] = 0x14; /* MDC_DIGEST_LEN */
pkt[1] = 0x14; /* MDC_DIGEST_LEN */
px_md_update(md, pkt, 2);
px_md_finish(md, pkt + 2);
@ -150,7 +152,7 @@ static const PushFilterOps mdc_filter = {
struct EncStat
{
PGP_CFB *ciph;
uint8 buf[ENCBUF];
uint8 buf[ENCBUF];
};
static int
@ -158,28 +160,29 @@ encrypt_init(PushFilter * next, void *init_arg, void **priv_p)
{
struct EncStat *st;
PGP_Context *ctx = init_arg;
PGP_CFB *ciph;
int resync = 1;
int res;
PGP_CFB *ciph;
int resync = 1;
int res;
/* should we use newer packet format? */
if (ctx->disable_mdc == 0)
{
uint8 ver = 1;
uint8 ver = 1;
resync = 0;
res = pushf_write(next, &ver, 1);
if (res < 0)
return res;
}
res = pgp_cfb_create(&ciph, ctx->cipher_algo,
ctx->sess_key, ctx->sess_key_len, resync, NULL);
ctx->sess_key, ctx->sess_key_len, resync, NULL);
if (res < 0)
return res;
st = px_alloc(sizeof(*st));
memset(st, 0, sizeof(*st));
st->ciph = ciph;
*priv_p = st;
return ENCBUF;
}
@ -189,11 +192,12 @@ encrypt_process(PushFilter * next, void *priv, const uint8 *data, int len)
{
int res;
struct EncStat *st = priv;
int avail = len;
int avail = len;
while (avail > 0)
{
int tmplen = avail > ENCBUF ? ENCBUF : avail;
int tmplen = avail > ENCBUF ? ENCBUF : avail;
res = pgp_cfb_encrypt(st->ciph, data, tmplen, st->buf);
if (res < 0)
return res;
@ -303,9 +307,11 @@ static const PushFilterOps pkt_stream_filter = {
pkt_stream_init, pkt_stream_process, pkt_stream_flush, pkt_stream_free
};
int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p)
int
pgp_create_pkt_writer(PushFilter * dst, int tag, PushFilter ** res_p)
{
int res;
int res;
res = write_tag_only(dst, tag);
if (res < 0)
return res;
@ -320,17 +326,19 @@ int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p)
static int
crlf_process(PushFilter * dst, void *priv, const uint8 *data, int len)
{
const uint8 * data_end = data + len;
const uint8 * p2, * p1 = data;
int line_len;
static const uint8 crlf[] = { '\r', '\n' };
int res = 0;
const uint8 *data_end = data + len;
const uint8 *p2,
*p1 = data;
int line_len;
static const uint8 crlf[] = {'\r', '\n'};
int res = 0;
while (p1 < data_end)
{
p2 = memchr(p1, '\n', data_end - p1);
if (p2 == NULL)
p2 = data_end;
line_len = p2 - p1;
/* write data */
@ -363,13 +371,13 @@ static const PushFilterOps crlf_filter = {
* Initialize literal data packet
*/
static int
init_litdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
init_litdata_packet(PushFilter ** pf_res, PGP_Context * ctx, PushFilter * dst)
{
int res;
int hdrlen;
uint8 hdr[6];
uint32 t;
PushFilter *pkt;
PushFilter *pkt;
int type;
/*
@ -382,10 +390,10 @@ init_litdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
type = 'b';
/*
* Store the creation time into packet.
* The goal is to have as few known bytes as possible.
* Store the creation time into packet. The goal is to have as few known
* bytes as possible.
*/
t = (uint32)time(NULL);
t = (uint32) time(NULL);
hdr[0] = type;
hdr[1] = 0;
@ -418,10 +426,10 @@ init_litdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
* Initialize compression filter
*/
static int
init_compress(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
init_compress(PushFilter ** pf_res, PGP_Context * ctx, PushFilter * dst)
{
int res;
uint8 type = ctx->compress_algo;
int res;
uint8 type = ctx->compress_algo;
PushFilter *pkt;
res = write_tag_only(dst, PGP_PKT_COMPRESSED_DATA);
@ -446,7 +454,7 @@ init_compress(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
* Initialize encdata packet
*/
static int
init_encdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
init_encdata_packet(PushFilter ** pf_res, PGP_Context * ctx, PushFilter * dst)
{
int res;
int tag;
@ -467,7 +475,7 @@ init_encdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
* write prefix
*/
static int
write_prefix(PGP_Context *ctx, PushFilter * dst)
write_prefix(PGP_Context * ctx, PushFilter * dst)
{
uint8 prefix[PGP_MAX_BLOCK + 2];
int res,
@ -491,17 +499,17 @@ write_prefix(PGP_Context *ctx, PushFilter * dst)
*/
static int
symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)
symencrypt_sesskey(PGP_Context * ctx, uint8 *dst)
{
int res;
PGP_CFB *cfb;
uint8 algo = ctx->cipher_algo;
int res;
PGP_CFB *cfb;
uint8 algo = ctx->cipher_algo;
res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
if (res < 0)
return res;
pgp_cfb_encrypt(cfb, &algo, 1, dst);
pgp_cfb_encrypt(cfb, ctx->sess_key, ctx->sess_key_len, dst + 1);
@ -511,12 +519,12 @@ symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)
/* 5.3: Symmetric-Key Encrypted Session-Key */
static int
write_symenc_sesskey(PGP_Context *ctx, PushFilter *dst)
write_symenc_sesskey(PGP_Context * ctx, PushFilter * dst)
{
uint8 pkt[256];
int pktlen;
int res;
uint8 *p = pkt;
uint8 *p = pkt;
*p++ = 4; /* 5.3 - version number */
*p++ = ctx->s2k_cipher_algo;
@ -564,13 +572,14 @@ init_s2k_key(PGP_Context * ctx)
return res;
return pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
ctx->sym_key, ctx->sym_key_len);
ctx->sym_key, ctx->sym_key_len);
}
static int
init_sess_key(PGP_Context *ctx)
init_sess_key(PGP_Context * ctx)
{
int res;
int res;
if (ctx->use_sess_key || ctx->pub_key)
{
ctx->sess_key_len = pgp_get_cipher_key_size(ctx->cipher_algo);
@ -596,7 +605,8 @@ pgp_encrypt(PGP_Context * ctx, MBuf * src, MBuf * dst)
int res;
int len;
uint8 *buf;
PushFilter *pf, *pf_tmp;
PushFilter *pf,
*pf_tmp;
/*
* do we have any key
@ -618,7 +628,7 @@ pgp_encrypt(PGP_Context * ctx, MBuf * src, MBuf * dst)
if (res < 0)
goto out;
}
res = init_sess_key(ctx);
if (res < 0)
goto out;
@ -674,7 +684,7 @@ pgp_encrypt(PGP_Context * ctx, MBuf * src, MBuf * dst)
goto out;
pf = pf_tmp;
/* text conversion? */
if (ctx->text_mode && ctx->convert_crlf)
{
@ -696,4 +706,3 @@ out:
pushf_free_all(pf);
return res;
}

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/pgp-info.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-info.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -34,9 +34,10 @@
#include "mbuf.h"
#include "pgp.h"
static int read_pubkey_keyid(PullFilter *pkt, uint8 *keyid_buf)
static int
read_pubkey_keyid(PullFilter * pkt, uint8 *keyid_buf)
{
int res;
int res;
PGP_PubKey *pk = NULL;
res = _pgp_read_public_key(pkt, &pk);
@ -66,10 +67,11 @@ err:
return res;
}
static int read_pubenc_keyid(PullFilter *pkt, uint8 *keyid_buf)
static int
read_pubenc_keyid(PullFilter * pkt, uint8 *keyid_buf)
{
uint8 ver;
int res;
uint8 ver;
int res;
GETBYTE(pkt, ver);
if (ver != 3)
@ -87,45 +89,50 @@ static const char hextbl[] = "0123456789ABCDEF";
static int
print_key(uint8 *keyid, char *dst)
{
int i;
unsigned c;
for (i = 0; i < 8; i++) {
int i;
unsigned c;
for (i = 0; i < 8; i++)
{
c = keyid[i];
*dst++ = hextbl[(c >> 4) & 0x0F];
*dst++ = hextbl[c & 0x0F];
}
*dst = 0;
return 8*2;
return 8 * 2;
}
static const uint8 any_key[] =
{ 0, 0, 0, 0, 0, 0, 0, 0 };
static const uint8 any_key[] =
{0, 0, 0, 0, 0, 0, 0, 0};
/*
* dst should have room for 17 bytes
*/
int
pgp_get_keyid(MBuf *pgp_data, char *dst)
pgp_get_keyid(MBuf * pgp_data, char *dst)
{
int res;
int res;
PullFilter *src;
PullFilter *pkt = NULL;
int len;
uint8 tag;
int got_pub_key=0, got_symenc_key=0, got_pubenc_key=0;
int got_data=0;
uint8 keyid_buf[8];
int got_main_key=0;
int len;
uint8 tag;
int got_pub_key = 0,
got_symenc_key = 0,
got_pubenc_key = 0;
int got_data = 0;
uint8 keyid_buf[8];
int got_main_key = 0;
res = pullf_create_mbuf_reader(&src, pgp_data);
if (res < 0)
return res;
while (1) {
while (1)
{
res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
if (res <= 0)
break;
break;
res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
if (res < 0)
break;
@ -226,4 +233,3 @@ pgp_get_keyid(MBuf *pgp_data, char *dst)
return res;
}

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/pgp-mpi-internal.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-mpi-internal.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -35,27 +35,27 @@
#include "pgp.h"
int
pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m,
PGP_MPI **c1_p, PGP_MPI **c2_p)
pgp_elgamal_encrypt(PGP_PubKey * pk, PGP_MPI * _m,
PGP_MPI ** c1_p, PGP_MPI ** c2_p)
{
return PXE_PGP_NO_BIGNUM;
}
int
pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2,
PGP_MPI **msg_p)
pgp_elgamal_decrypt(PGP_PubKey * pk, PGP_MPI * _c1, PGP_MPI * _c2,
PGP_MPI ** msg_p)
{
return PXE_PGP_NO_BIGNUM;
}
int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *m, PGP_MPI **c)
int
pgp_rsa_encrypt(PGP_PubKey * pk, PGP_MPI * m, PGP_MPI ** c)
{
return PXE_PGP_NO_BIGNUM;
}
int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *c, PGP_MPI **m)
int
pgp_rsa_decrypt(PGP_PubKey * pk, PGP_MPI * c, PGP_MPI ** m)
{
return PXE_PGP_NO_BIGNUM;
}

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/pgp-mpi-openssl.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-mpi-openssl.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -37,15 +37,16 @@
#include "pgp.h"
static BIGNUM *
mpi_to_bn(PGP_MPI *n)
mpi_to_bn(PGP_MPI * n)
{
BIGNUM *bn = BN_bin2bn(n->data, n->bytes, NULL);
BIGNUM *bn = BN_bin2bn(n->data, n->bytes, NULL);
if (!bn)
return NULL;
if (BN_num_bits(bn) != n->bits)
{
px_debug("mpi_to_bn: bignum conversion failed: mpi=%d, bn=%d",
n->bits, BN_num_bits(bn));
n->bits, BN_num_bits(bn));
BN_clear_free(bn);
return NULL;
}
@ -55,8 +56,8 @@ mpi_to_bn(PGP_MPI *n)
static PGP_MPI *
bn_to_mpi(BIGNUM *bn)
{
int res;
PGP_MPI *n;
int res;
PGP_MPI *n;
res = pgp_mpi_alloc(BN_num_bits(bn), &n);
if (res < 0)
@ -65,7 +66,7 @@ bn_to_mpi(BIGNUM *bn)
if (BN_num_bytes(bn) != n->bytes)
{
px_debug("bn_to_mpi: bignum conversion failed: bn=%d, mpi=%d",
BN_num_bytes(bn), n->bytes);
BN_num_bytes(bn), n->bytes);
pgp_mpi_free(n);
return NULL;
}
@ -81,7 +82,7 @@ bn_to_mpi(BIGNUM *bn)
*
* Until I research it further, I just mimic gpg behaviour.
* It has a special mapping table, for values <= 5120,
* above that it uses 'arbitrary high number'. Following
* above that it uses 'arbitrary high number'. Following
* algorihm hovers 10-70 bits above gpg values. And for
* larger p, it uses gpg's algorihm.
*
@ -98,20 +99,20 @@ decide_k_bits(int p_bits)
}
int
pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m,
PGP_MPI **c1_p, PGP_MPI **c2_p)
pgp_elgamal_encrypt(PGP_PubKey * pk, PGP_MPI * _m,
PGP_MPI ** c1_p, PGP_MPI ** c2_p)
{
int res = PXE_PGP_MATH_FAILED;
int k_bits;
BIGNUM *m = mpi_to_bn(_m);
BIGNUM *p = mpi_to_bn(pk->pub.elg.p);
BIGNUM *g = mpi_to_bn(pk->pub.elg.g);
BIGNUM *y = mpi_to_bn(pk->pub.elg.y);
BIGNUM *k = BN_new();
BIGNUM *yk = BN_new();
BIGNUM *c1 = BN_new();
BIGNUM *c2 = BN_new();
BN_CTX *tmp = BN_CTX_new();
int res = PXE_PGP_MATH_FAILED;
int k_bits;
BIGNUM *m = mpi_to_bn(_m);
BIGNUM *p = mpi_to_bn(pk->pub.elg.p);
BIGNUM *g = mpi_to_bn(pk->pub.elg.g);
BIGNUM *y = mpi_to_bn(pk->pub.elg.y);
BIGNUM *k = BN_new();
BIGNUM *yk = BN_new();
BIGNUM *c1 = BN_new();
BIGNUM *c2 = BN_new();
BN_CTX *tmp = BN_CTX_new();
if (!m || !p || !g || !y || !k || !yk || !c1 || !c2 || !tmp)
goto err;
@ -124,8 +125,7 @@ pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m,
goto err;
/*
* c1 = g^k
* c2 = m * y^k
* c1 = g^k c2 = m * y^k
*/
if (!BN_mod_exp(c1, g, k, p, tmp))
goto err;
@ -140,35 +140,44 @@ pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m,
if (*c1_p && *c2_p)
res = 0;
err:
if (tmp) BN_CTX_free(tmp);
if (c2) BN_clear_free(c2);
if (c1) BN_clear_free(c1);
if (yk) BN_clear_free(yk);
if (k) BN_clear_free(k);
if (y) BN_clear_free(y);
if (g) BN_clear_free(g);
if (p) BN_clear_free(p);
if (m) BN_clear_free(m);
if (tmp)
BN_CTX_free(tmp);
if (c2)
BN_clear_free(c2);
if (c1)
BN_clear_free(c1);
if (yk)
BN_clear_free(yk);
if (k)
BN_clear_free(k);
if (y)
BN_clear_free(y);
if (g)
BN_clear_free(g);
if (p)
BN_clear_free(p);
if (m)
BN_clear_free(m);
return res;
}
int
pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2,
PGP_MPI **msg_p)
pgp_elgamal_decrypt(PGP_PubKey * pk, PGP_MPI * _c1, PGP_MPI * _c2,
PGP_MPI ** msg_p)
{
int res = PXE_PGP_MATH_FAILED;
BIGNUM *c1 = mpi_to_bn(_c1);
BIGNUM *c2 = mpi_to_bn(_c2);
BIGNUM *p = mpi_to_bn(pk->pub.elg.p);
BIGNUM *x = mpi_to_bn(pk->sec.elg.x);
BIGNUM *c1x = BN_new();
BIGNUM *div = BN_new();
BIGNUM *m = BN_new();
BN_CTX *tmp = BN_CTX_new();
int res = PXE_PGP_MATH_FAILED;
BIGNUM *c1 = mpi_to_bn(_c1);
BIGNUM *c2 = mpi_to_bn(_c2);
BIGNUM *p = mpi_to_bn(pk->pub.elg.p);
BIGNUM *x = mpi_to_bn(pk->sec.elg.x);
BIGNUM *c1x = BN_new();
BIGNUM *div = BN_new();
BIGNUM *m = BN_new();
BN_CTX *tmp = BN_CTX_new();
if (!c1 || !c2 || !p || !x || !c1x || !div || !m || !tmp)
goto err;
/*
* m = c2 / (c1^x)
*/
@ -184,26 +193,34 @@ pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2,
if (*msg_p)
res = 0;
err:
if (tmp) BN_CTX_free(tmp);
if (m) BN_clear_free(m);
if (div) BN_clear_free(div);
if (c1x) BN_clear_free(c1x);
if (x) BN_clear_free(x);
if (p) BN_clear_free(p);
if (c2) BN_clear_free(c2);
if (c1) BN_clear_free(c1);
if (tmp)
BN_CTX_free(tmp);
if (m)
BN_clear_free(m);
if (div)
BN_clear_free(div);
if (c1x)
BN_clear_free(c1x);
if (x)
BN_clear_free(x);
if (p)
BN_clear_free(p);
if (c2)
BN_clear_free(c2);
if (c1)
BN_clear_free(c1);
return res;
}
int
pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
pgp_rsa_encrypt(PGP_PubKey * pk, PGP_MPI * _m, PGP_MPI ** c_p)
{
int res = PXE_PGP_MATH_FAILED;
BIGNUM *m = mpi_to_bn(_m);
BIGNUM *e = mpi_to_bn(pk->pub.rsa.e);
BIGNUM *n = mpi_to_bn(pk->pub.rsa.n);
BIGNUM *c = BN_new();
BN_CTX *tmp = BN_CTX_new();
int res = PXE_PGP_MATH_FAILED;
BIGNUM *m = mpi_to_bn(_m);
BIGNUM *e = mpi_to_bn(pk->pub.rsa.e);
BIGNUM *n = mpi_to_bn(pk->pub.rsa.n);
BIGNUM *c = BN_new();
BN_CTX *tmp = BN_CTX_new();
if (!m || !e || !n || !c || !tmp)
goto err;
@ -218,23 +235,28 @@ pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
if (*c_p)
res = 0;
err:
if (tmp) BN_CTX_free(tmp);
if (c) BN_clear_free(c);
if (n) BN_clear_free(n);
if (e) BN_clear_free(e);
if (m) BN_clear_free(m);
if (tmp)
BN_CTX_free(tmp);
if (c)
BN_clear_free(c);
if (n)
BN_clear_free(n);
if (e)
BN_clear_free(e);
if (m)
BN_clear_free(m);
return res;
}
int
pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
pgp_rsa_decrypt(PGP_PubKey * pk, PGP_MPI * _c, PGP_MPI ** m_p)
{
int res = PXE_PGP_MATH_FAILED;
BIGNUM *c = mpi_to_bn(_c);
BIGNUM *d = mpi_to_bn(pk->sec.rsa.d);
BIGNUM *n = mpi_to_bn(pk->pub.rsa.n);
BIGNUM *m = BN_new();
BN_CTX *tmp = BN_CTX_new();
int res = PXE_PGP_MATH_FAILED;
BIGNUM *c = mpi_to_bn(_c);
BIGNUM *d = mpi_to_bn(pk->sec.rsa.d);
BIGNUM *n = mpi_to_bn(pk->pub.rsa.n);
BIGNUM *m = BN_new();
BN_CTX *tmp = BN_CTX_new();
if (!m || !d || !n || !c || !tmp)
goto err;
@ -249,11 +271,15 @@ pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
if (*m_p)
res = 0;
err:
if (tmp) BN_CTX_free(tmp);
if (m) BN_clear_free(m);
if (n) BN_clear_free(n);
if (d) BN_clear_free(d);
if (c) BN_clear_free(c);
if (tmp)
BN_CTX_free(tmp);
if (m)
BN_clear_free(m);
if (n)
BN_clear_free(n);
if (d)
BN_clear_free(d);
if (c)
BN_clear_free(c);
return res;
}

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/pgp-mpi.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-mpi.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -34,10 +34,12 @@
#include "mbuf.h"
#include "pgp.h"
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
int
pgp_mpi_alloc(int bits, PGP_MPI ** mpi)
{
PGP_MPI *n;
int len = (bits + 7) / 8;
PGP_MPI *n;
int len = (bits + 7) / 8;
if (bits < 0 || bits > 0xFFFF)
{
px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
@ -46,15 +48,16 @@ int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
n = px_alloc(sizeof(*n) + len);
n->bits = bits;
n->bytes = len;
n->data = (uint8*)(n) + sizeof(*n);
n->data = (uint8 *) (n) + sizeof(*n);
*mpi = n;
return 0;
}
int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
int
pgp_mpi_create(uint8 *data, int bits, PGP_MPI ** mpi)
{
int res;
PGP_MPI *n;
int res;
PGP_MPI *n;
res = pgp_mpi_alloc(bits, &n);
if (res < 0)
@ -64,7 +67,8 @@ int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
return 0;
}
int pgp_mpi_free(PGP_MPI *mpi)
int
pgp_mpi_free(PGP_MPI * mpi)
{
if (mpi == NULL)
return 0;
@ -73,17 +77,18 @@ int pgp_mpi_free(PGP_MPI *mpi)
return 0;
}
int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
int
pgp_mpi_read(PullFilter * src, PGP_MPI ** mpi)
{
int res;
uint8 hdr[2];
int bits;
PGP_MPI *n;
int res;
uint8 hdr[2];
int bits;
PGP_MPI *n;
res = pullf_read_fixed(src, 2, hdr);
if (res < 0)
return res;
bits = ((unsigned)hdr[0] << 8) + hdr[1];
bits = ((unsigned) hdr[0] << 8) + hdr[1];
res = pgp_mpi_alloc(bits, &n);
if (res < 0)
@ -97,10 +102,11 @@ int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
return res;
}
int pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
int
pgp_mpi_write(PushFilter * dst, PGP_MPI * n)
{
int res;
uint8 buf[2];
int res;
uint8 buf[2];
buf[0] = n->bits >> 8;
buf[1] = n->bits & 0xFF;
@ -110,9 +116,10 @@ int pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
return res;
}
int pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
int
pgp_mpi_hash(PX_MD * md, PGP_MPI * n)
{
uint8 buf[2];
uint8 buf[2];
buf[0] = n->bits >> 8;
buf[1] = n->bits & 0xFF;
@ -122,9 +129,10 @@ int pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
return 0;
}
unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
unsigned
pgp_mpi_cksum(unsigned cksum, PGP_MPI * n)
{
int i;
int i;
cksum += n->bits >> 8;
cksum += n->bits & 0xFF;
@ -133,4 +141,3 @@ unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
return cksum & 0xFFFF;
}

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/pgp-pgsql.c,v 1.5 2005/09/24 19:14:04 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pgsql.c,v 1.6 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -42,20 +42,20 @@
/*
* public functions
*/
Datum pgp_sym_encrypt_text(PG_FUNCTION_ARGS);
Datum pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_sym_decrypt_text(PG_FUNCTION_ARGS);
Datum pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_sym_encrypt_text(PG_FUNCTION_ARGS);
Datum pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_sym_decrypt_text(PG_FUNCTION_ARGS);
Datum pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_pub_encrypt_text(PG_FUNCTION_ARGS);
Datum pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_pub_decrypt_text(PG_FUNCTION_ARGS);
Datum pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_pub_encrypt_text(PG_FUNCTION_ARGS);
Datum pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_pub_decrypt_text(PG_FUNCTION_ARGS);
Datum pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS);
Datum pgp_key_id_w(PG_FUNCTION_ARGS);
Datum pgp_key_id_w(PG_FUNCTION_ARGS);
Datum pg_armor(PG_FUNCTION_ARGS);
Datum pg_dearmor(PG_FUNCTION_ARGS);
Datum pg_armor(PG_FUNCTION_ARGS);
Datum pg_dearmor(PG_FUNCTION_ARGS);
/* function headers */
@ -89,9 +89,10 @@ PG_FUNCTION_INFO_V1(pg_dearmor);
/*
* Mix a block of data into RNG.
*/
static void add_block_entropy(PX_MD *md, text *data)
static void
add_block_entropy(PX_MD * md, text *data)
{
uint8 sha1[20];
uint8 sha1[20];
px_md_reset(md);
px_md_update(md, (uint8 *) VARDATA(data), VARSIZE(data) - VARHDRSZ);
@ -103,13 +104,14 @@ static void add_block_entropy(PX_MD *md, text *data)
}
/*
* Mix user data into RNG. It is for user own interests to have
* Mix user data into RNG. It is for user own interests to have
* RNG state shuffled.
*/
static void add_entropy(text *data1, text *data2, text *data3)
static void
add_entropy(text *data1, text *data2, text *data3)
{
PX_MD *md;
uint8 rnd[3];
PX_MD *md;
uint8 rnd[3];
if (!data1 && !data2 && !data3)
return;
@ -122,9 +124,9 @@ static void add_entropy(text *data1, text *data2, text *data3)
/*
* Try to make the feeding unpredictable.
*
* Prefer data over keys, as it's rather likely
* that key is same in several calls.
*
* Prefer data over keys, as it's rather likely that key is same in several
* calls.
*/
/* chance: 7/8 */
@ -146,14 +148,15 @@ static void add_entropy(text *data1, text *data2, text *data3)
/*
* returns src in case of no conversion or error
*/
static text *convert_charset(text *src, int cset_from, int cset_to)
static text *
convert_charset(text *src, int cset_from, int cset_to)
{
int src_len = VARSIZE(src) - VARHDRSZ;
int dst_len;
int src_len = VARSIZE(src) - VARHDRSZ;
int dst_len;
unsigned char *dst;
unsigned char *csrc = (unsigned char *) VARDATA(src);
text *res;
text *res;
dst = pg_do_encoding_conversion(csrc, src_len, cset_from, cset_to);
if (dst == csrc)
return src;
@ -166,12 +169,14 @@ static text *convert_charset(text *src, int cset_from, int cset_to)
return res;
}
static text *convert_from_utf8(text *src)
static text *
convert_from_utf8(text *src)
{
return convert_charset(src, PG_UTF8, GetDatabaseEncoding());
}
static text *convert_to_utf8(text *src)
static text *
convert_to_utf8(text *src)
{
return convert_charset(src, GetDatabaseEncoding(), PG_UTF8);
}
@ -186,20 +191,22 @@ clear_and_pfree(text *p)
/*
* expect-* arguments storage
*/
struct debug_expect {
int debug;
int expect;
int cipher_algo;
int s2k_mode;
int s2k_cipher_algo;
int s2k_digest_algo;
int compress_algo;
int use_sess_key;
int disable_mdc;
int unicode_mode;
struct debug_expect
{
int debug;
int expect;
int cipher_algo;
int s2k_mode;
int s2k_cipher_algo;
int s2k_digest_algo;
int compress_algo;
int use_sess_key;
int disable_mdc;
int unicode_mode;
};
static void fill_expect(struct debug_expect *ex, int text_mode)
static void
fill_expect(struct debug_expect * ex, int text_mode)
{
ex->debug = 0;
ex->expect = 0;
@ -222,7 +229,8 @@ static void fill_expect(struct debug_expect *ex, int text_mode)
if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \
} while (0)
static void check_expect(PGP_Context *ctx, struct debug_expect *ex)
static void
check_expect(PGP_Context * ctx, struct debug_expect * ex)
{
EX_CHECK(cipher_algo);
EX_CHECK(s2k_mode);
@ -235,15 +243,18 @@ static void check_expect(PGP_Context *ctx, struct debug_expect *ex)
EX_CHECK(unicode_mode);
}
static void show_debug(const char *msg)
static void
show_debug(const char *msg)
{
ereport(NOTICE, (errmsg("dbg: %s", msg)));
}
static int set_arg(PGP_Context *ctx, char *key, char*val,
struct debug_expect *ex)
static int
set_arg(PGP_Context * ctx, char *key, char *val,
struct debug_expect * ex)
{
int res = 0;
int res = 0;
if (strcmp(key, "cipher-algo") == 0)
res = pgp_set_cipher_algo(ctx, val);
else if (strcmp(key, "disable-mdc") == 0)
@ -314,11 +325,12 @@ static int set_arg(PGP_Context *ctx, char *key, char*val,
}
/*
* Find next word. Handle ',' and '=' as words. Skip whitespace.
* Find next word. Handle ',' and '=' as words. Skip whitespace.
* Put word info into res_p, res_len.
* Returns ptr to next word.
*/
static char *getword(char *p, char **res_p, int *res_len)
static char *
getword(char *p, char **res_p, int *res_len)
{
/* whitespace at start */
while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
@ -330,12 +342,12 @@ static char *getword(char *p, char **res_p, int *res_len)
p++;
else
while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'
|| *p == '=' || *p == ','))
|| *p == '=' || *p == ','))
p++;
/* word end */
*res_len = p - *res_p;
/* whitespace at end */
while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
p++;
@ -346,11 +358,15 @@ static char *getword(char *p, char **res_p, int *res_len)
/*
* Convert to lowercase asciiz string.
*/
static char *downcase_convert(const uint8 *s, int len)
static char *
downcase_convert(const uint8 *s, int len)
{
int c, i;
char *res = palloc(len + 1);
for (i = 0; i < len; i++) {
int c,
i;
char *res = palloc(len + 1);
for (i = 0; i < len; i++)
{
c = s[i];
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A';
@ -360,14 +376,17 @@ static char *downcase_convert(const uint8 *s, int len)
return res;
}
static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len,
struct debug_expect *ex)
static int
parse_args(PGP_Context * ctx, uint8 *args, int arg_len,
struct debug_expect * ex)
{
char *str = downcase_convert(args, arg_len);
char *key, *val;
int key_len, val_len;
int res = 0;
char *p = str;
char *str = downcase_convert(args, arg_len);
char *key,
*val;
int key_len,
val_len;
int res = 0;
char *p = str;
while (*p)
{
@ -403,10 +422,10 @@ create_mbuf_from_vardata(text *data)
}
static void
init_work(PGP_Context **ctx_p, int is_text,
text *args, struct debug_expect *ex)
init_work(PGP_Context ** ctx_p, int is_text,
text *args, struct debug_expect * ex)
{
int err = pgp_init(ctx_p);
int err = pgp_init(ctx_p);
fill_expect(ex, is_text);
@ -429,17 +448,18 @@ init_work(PGP_Context **ctx_p, int is_text,
static bytea *
encrypt_internal(int is_pubenc, int is_text,
text *data, text *key, text *args)
text *data, text *key, text *args)
{
MBuf *src, *dst;
uint8 tmp[VARHDRSZ];
uint8 *restmp;
bytea *res;
int res_len;
MBuf *src,
*dst;
uint8 tmp[VARHDRSZ];
uint8 *restmp;
bytea *res;
int res_len;
PGP_Context *ctx;
int err;
int err;
struct debug_expect ex;
text *tmp_data = NULL;
text *tmp_data = NULL;
/*
* Add data and key info RNG.
@ -470,15 +490,16 @@ encrypt_internal(int is_pubenc, int is_text,
*/
if (is_pubenc)
{
MBuf *kbuf = create_mbuf_from_vardata(key);
MBuf *kbuf = create_mbuf_from_vardata(key);
err = pgp_set_pubkey(ctx, kbuf,
NULL, 0, 0);
NULL, 0, 0);
mbuf_free(kbuf);
}
else
err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key),
VARSIZE(key) - VARHDRSZ);
/*
* encrypt
*/
@ -520,17 +541,18 @@ encrypt_internal(int is_pubenc, int is_text,
static bytea *
decrypt_internal(int is_pubenc, int need_text, text *data,
text *key, text *keypsw, text *args)
text *key, text *keypsw, text *args)
{
int err;
MBuf *src = NULL, *dst = NULL;
uint8 tmp[VARHDRSZ];
uint8 *restmp;
bytea *res;
int res_len;
int err;
MBuf *src = NULL,
*dst = NULL;
uint8 tmp[VARHDRSZ];
uint8 *restmp;
bytea *res;
int res_len;
PGP_Context *ctx = NULL;
struct debug_expect ex;
int got_unicode = 0;
int got_unicode = 0;
init_work(&ctx, need_text, args, &ex);
@ -543,15 +565,16 @@ decrypt_internal(int is_pubenc, int need_text, text *data,
* reserve room for header
*/
mbuf_append(dst, tmp, VARHDRSZ);
/*
* set key
*/
if (is_pubenc)
{
uint8 *psw = NULL;
int psw_len = 0;
MBuf *kbuf;
uint8 *psw = NULL;
int psw_len = 0;
MBuf *kbuf;
if (keypsw)
{
psw = (uint8 *) VARDATA(keypsw);
@ -608,7 +631,8 @@ out:
if (need_text && got_unicode)
{
text *utf = convert_from_utf8(res);
text *utf = convert_from_utf8(res);
if (utf != res)
{
clear_and_pfree(res);
@ -927,4 +951,3 @@ pgp_key_id_w(PG_FUNCTION_ARGS)
PG_FREE_IF_COPY(data, 0);
PG_RETURN_TEXT_P(res);
}

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/pgp-pubdec.c,v 1.4 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pubdec.c,v 1.5 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -35,24 +35,25 @@
#include "pgp.h"
/*
* padded msg = 02 || PS || 00 || M
* padded msg = 02 || PS || 00 || M
* PS - pad bytes
* M - msg
*/
static uint8 *
check_eme_pkcs1_v15(uint8 *data, int len)
{
uint8 *data_end = data + len;
uint8 *p = data;
int rnd = 0;
uint8 *data_end = data + len;
uint8 *p = data;
int rnd = 0;
if (len < 1 + 8 + 1)
return NULL;
if (*p++ != 2)
return NULL;
while (p < data_end && *p) {
while (p < data_end && *p)
{
p++;
rnd++;
}
@ -73,8 +74,9 @@ check_eme_pkcs1_v15(uint8 *data, int len)
static int
control_cksum(uint8 *msg, int msglen)
{
int i;
unsigned my_cksum, got_cksum;
int i;
unsigned my_cksum,
got_cksum;
if (msglen < 3)
return PXE_PGP_WRONG_KEY;
@ -83,8 +85,9 @@ control_cksum(uint8 *msg, int msglen)
for (i = 1; i < msglen - 2; i++)
my_cksum += msg[i];
my_cksum &= 0xFFFF;
got_cksum = ((unsigned)(msg[msglen-2]) << 8) + msg[msglen-1];
if (my_cksum != got_cksum) {
got_cksum = ((unsigned) (msg[msglen - 2]) << 8) + msg[msglen - 1];
if (my_cksum != got_cksum)
{
px_debug("pubenc cksum failed");
return PXE_PGP_WRONG_KEY;
}
@ -92,11 +95,11 @@ control_cksum(uint8 *msg, int msglen)
}
static int
decrypt_elgamal(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
decrypt_elgamal(PGP_PubKey * pk, PullFilter * pkt, PGP_MPI ** m_p)
{
int res;
PGP_MPI *c1 = NULL;
PGP_MPI *c2 = NULL;
int res;
PGP_MPI *c1 = NULL;
PGP_MPI *c2 = NULL;
if (pk->algo != PGP_PUB_ELG_ENCRYPT)
return PXE_PGP_WRONG_KEY;
@ -119,13 +122,13 @@ out:
}
static int
decrypt_rsa(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
decrypt_rsa(PGP_PubKey * pk, PullFilter * pkt, PGP_MPI ** m_p)
{
int res;
PGP_MPI *c;
int res;
PGP_MPI *c;
if (pk->algo != PGP_PUB_RSA_ENCRYPT
&& pk->algo != PGP_PUB_RSA_ENCRYPT_SIGN)
&& pk->algo != PGP_PUB_RSA_ENCRYPT_SIGN)
return PXE_PGP_WRONG_KEY;
/* read rsa encrypted data */
@ -142,28 +145,30 @@ decrypt_rsa(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
/* key id is missing - user is expected to try all keys */
static const uint8
any_key[] = {0, 0, 0, 0, 0, 0, 0, 0};
any_key[] = {0, 0, 0, 0, 0, 0, 0, 0};
int
pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt)
pgp_parse_pubenc_sesskey(PGP_Context * ctx, PullFilter * pkt)
{
int ver;
int algo;
int res;
uint8 key_id[8];
int ver;
int algo;
int res;
uint8 key_id[8];
PGP_PubKey *pk;
uint8 *msg;
int msglen;
PGP_MPI *m;
uint8 *msg;
int msglen;
PGP_MPI *m;
pk = ctx->pub_key;
if (pk == NULL) {
if (pk == NULL)
{
px_debug("no pubkey?");
return PXE_BUG;
}
GETBYTE(pkt, ver);
if (ver != 3) {
if (ver != 3)
{
px_debug("unknown pubenc_sesskey pkt ver=%d", ver);
return PXE_PGP_CORRUPT_DATA;
}
@ -175,7 +180,7 @@ pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt)
if (res < 0)
return res;
if (memcmp(key_id, any_key, 8) != 0
&& memcmp(key_id, pk->key_id, 8) != 0)
&& memcmp(key_id, pk->key_id, 8) != 0)
{
px_debug("key_id's does not match");
return PXE_PGP_WRONG_KEY;
@ -204,7 +209,8 @@ pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt)
* extract message
*/
msg = check_eme_pkcs1_v15(m->data, m->bytes);
if (msg == NULL) {
if (msg == NULL)
{
px_debug("check_eme_pkcs1_v15 failed");
res = PXE_PGP_WRONG_KEY;
goto out;
@ -228,5 +234,3 @@ out:
return res;
return pgp_expect_packet_end(pkt);
}

View File

@ -1,6 +1,6 @@
/*
* pgp-pubenc.c
* Encrypt session key with public key.
* Encrypt session key with public key.
*
* Copyright (c) 2005 Marko Kreen
* All rights reserved.
@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pubenc.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pubenc.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -40,9 +40,10 @@
static int
pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
{
int res;
uint8 *buf, *p;
int pad_len = res_len - 2 - data_len;
int res;
uint8 *buf,
*p;
int pad_len = res_len - 2 - data_len;
if (pad_len < 8)
return PXE_BUG;
@ -76,7 +77,7 @@ pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
px_free(buf);
return res;
}
buf[pad_len + 1] = 0;
memcpy(buf + pad_len + 2, data, data_len);
*res_p = buf;
@ -85,19 +86,20 @@ pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
}
static int
create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
create_secmsg(PGP_Context * ctx, PGP_MPI ** msg_p, int full_bytes)
{
uint8 *secmsg;
int res, i;
unsigned cksum = 0;
int klen = ctx->sess_key_len;
uint8 *padded = NULL;
PGP_MPI *m = NULL;
uint8 *secmsg;
int res,
i;
unsigned cksum = 0;
int klen = ctx->sess_key_len;
uint8 *padded = NULL;
PGP_MPI *m = NULL;
/* calc checksum */
for (i = 0; i < klen; i++)
cksum += ctx->sess_key[i];
/*
* create "secret message"
*/
@ -114,7 +116,8 @@ create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
if (res >= 0)
{
/* first byte will be 0x02 */
int full_bits = full_bytes * 8 - 6;
int full_bits = full_bytes * 8 - 6;
res = pgp_mpi_create(padded, full_bits, &m);
}
@ -133,10 +136,12 @@ create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
}
static int
encrypt_and_write_elgamal(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
encrypt_and_write_elgamal(PGP_Context * ctx, PGP_PubKey * pk, PushFilter * pkt)
{
int res;
PGP_MPI *m = NULL, *c1 = NULL, *c2 = NULL;
int res;
PGP_MPI *m = NULL,
*c1 = NULL,
*c2 = NULL;
/* create padded msg */
res = create_secmsg(ctx, &m, pk->pub.elg.p->bytes - 1);
@ -162,10 +167,11 @@ err:
}
static int
encrypt_and_write_rsa(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
encrypt_and_write_rsa(PGP_Context * ctx, PGP_PubKey * pk, PushFilter * pkt)
{
int res;
PGP_MPI *m = NULL, *c = NULL;
int res;
PGP_MPI *m = NULL,
*c = NULL;
/* create padded msg */
res = create_secmsg(ctx, &m, pk->pub.rsa.n->bytes - 1);
@ -186,15 +192,17 @@ err:
return res;
}
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
int
pgp_write_pubenc_sesskey(PGP_Context * ctx, PushFilter * dst)
{
int res;
int res;
PGP_PubKey *pk = ctx->pub_key;
uint8 ver = 3;
uint8 ver = 3;
PushFilter *pkt = NULL;
uint8 algo = pk->algo;
uint8 algo = pk->algo;
if (pk == NULL) {
if (pk == NULL)
{
px_debug("no pubkey?\n");
return PXE_BUG;
}
@ -238,5 +246,3 @@ err:
return res;
}

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/pgp-pubkey.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pubkey.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -34,16 +34,19 @@
#include "mbuf.h"
#include "pgp.h"
int pgp_key_alloc(PGP_PubKey **pk_p)
int
pgp_key_alloc(PGP_PubKey ** pk_p)
{
PGP_PubKey *pk;
pk = px_alloc(sizeof(*pk));
memset(pk, 0, sizeof(*pk));
*pk_p = pk;
return 0;
}
void pgp_key_free(PGP_PubKey *pk)
void
pgp_key_free(PGP_PubKey * pk)
{
if (pk == NULL)
return;
@ -79,13 +82,13 @@ void pgp_key_free(PGP_PubKey *pk)
}
static int
calc_key_id(PGP_PubKey *pk)
calc_key_id(PGP_PubKey * pk)
{
int res;
PX_MD *md;
int len;
uint8 hdr[3];
uint8 hash[20];
int res;
PX_MD *md;
int len;
uint8 hdr[3];
uint8 hash[20];
res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
if (res < 0)
@ -121,7 +124,7 @@ calc_key_id(PGP_PubKey *pk)
px_md_update(md, &pk->ver, 1);
px_md_update(md, pk->time, 4);
px_md_update(md, &pk->algo, 1);
switch (pk->algo)
{
case PGP_PUB_ELG_ENCRYPT:
@ -152,9 +155,10 @@ calc_key_id(PGP_PubKey *pk)
return 0;
}
int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
int
_pgp_read_public_key(PullFilter * pkt, PGP_PubKey ** pk_p)
{
int res;
int res;
PGP_PubKey *pk;
res = pgp_key_alloc(&pk);
@ -163,11 +167,12 @@ int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
/* get version */
GETBYTE(pkt, pk->ver);
if (pk->ver != 4) {
if (pk->ver != 4)
{
res = PXE_PGP_NOT_V4_KEYPKT;
goto out;
}
/* read time */
res = pullf_read_fixed(pkt, 4, pk->time);
if (res < 0)
@ -176,16 +181,21 @@ int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
/* pubkey algorithm */
GETBYTE(pkt, pk->algo);
switch (pk->algo) {
switch (pk->algo)
{
case PGP_PUB_DSA_SIGN:
res = pgp_mpi_read(pkt, &pk->pub.dsa.p);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->pub.dsa.q);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->pub.dsa.g);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->pub.dsa.y);
if (res < 0) break;
if (res < 0)
break;
res = calc_key_id(pk);
break;
@ -194,9 +204,11 @@ int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
case PGP_PUB_RSA_ENCRYPT:
case PGP_PUB_RSA_ENCRYPT_SIGN:
res = pgp_mpi_read(pkt, &pk->pub.rsa.n);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->pub.rsa.e);
if (res < 0) break;
if (res < 0)
break;
res = calc_key_id(pk);
@ -206,11 +218,14 @@ int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
case PGP_PUB_ELG_ENCRYPT:
res = pgp_mpi_read(pkt, &pk->pub.elg.p);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->pub.elg.g);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->pub.elg.y);
if (res < 0) break;
if (res < 0)
break;
res = calc_key_id(pk);
@ -236,12 +251,12 @@ out:
#define HIDE_SHA1 254
static int
check_key_sha1(PullFilter *src, PGP_PubKey *pk)
check_key_sha1(PullFilter * src, PGP_PubKey * pk)
{
int res;
uint8 got_sha1[20];
uint8 my_sha1[20];
PX_MD *md;
int res;
uint8 got_sha1[20];
uint8 my_sha1[20];
PX_MD *md;
res = pullf_read_fixed(src, 20, got_sha1);
if (res < 0)
@ -282,17 +297,18 @@ err:
}
static int
check_key_cksum(PullFilter *src, PGP_PubKey *pk)
check_key_cksum(PullFilter * src, PGP_PubKey * pk)
{
int res;
unsigned got_cksum, my_cksum = 0;
uint8 buf[2];
int res;
unsigned got_cksum,
my_cksum = 0;
uint8 buf[2];
res = pullf_read_fixed(src, 2, buf);
if (res < 0)
return res;
got_cksum = ((unsigned)buf[0] << 8) + buf[1];
got_cksum = ((unsigned) buf[0] << 8) + buf[1];
switch (pk->algo)
{
case PGP_PUB_ELG_ENCRYPT:
@ -318,17 +334,19 @@ check_key_cksum(PullFilter *src, PGP_PubKey *pk)
return 0;
}
static int process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p,
const uint8 *key, int key_len)
static int
process_secret_key(PullFilter * pkt, PGP_PubKey ** pk_p,
const uint8 *key, int key_len)
{
int res;
int hide_type;
int cipher_algo;
int bs;
uint8 iv[512];
PullFilter *pf_decrypt = NULL, *pf_key;
PGP_CFB *cfb = NULL;
PGP_S2K s2k;
int res;
int hide_type;
int cipher_algo;
int bs;
uint8 iv[512];
PullFilter *pf_decrypt = NULL,
*pf_key;
PGP_CFB *cfb = NULL;
PGP_S2K s2k;
PGP_PubKey *pk;
/* first read public key part */
@ -340,7 +358,8 @@ static int process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p,
* is secret key encrypted?
*/
GETBYTE(pkt, hide_type);
if (hide_type == HIDE_SHA1 || hide_type == HIDE_CKSUM) {
if (hide_type == HIDE_SHA1 || hide_type == HIDE_CKSUM)
{
if (key == NULL)
return PXE_PGP_NEED_SECRET_PSW;
GETBYTE(pkt, cipher_algo);
@ -351,15 +370,17 @@ static int process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p,
res = pgp_s2k_process(&s2k, cipher_algo, key, key_len);
if (res < 0)
return res;
bs = pgp_get_cipher_block_size(cipher_algo);
if (bs == 0) {
if (bs == 0)
{
px_debug("unknown cipher algo=%d", cipher_algo);
return PXE_PGP_UNSUPPORTED_CIPHER;
}
res = pullf_read_fixed(pkt, bs, iv);
if (res < 0)
return res;
/*
* create decrypt filter
*/
@ -370,26 +391,35 @@ static int process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p,
if (res < 0)
return res;
pf_key = pf_decrypt;
} else if (hide_type == HIDE_CLEAR) {
}
else if (hide_type == HIDE_CLEAR)
{
pf_key = pkt;
} else {
}
else
{
px_debug("unknown hide type");
return PXE_PGP_KEYPKT_CORRUPT;
}
/* read secret key */
switch (pk->algo) {
switch (pk->algo)
{
case PGP_PUB_RSA_SIGN:
case PGP_PUB_RSA_ENCRYPT:
case PGP_PUB_RSA_ENCRYPT_SIGN:
res = pgp_mpi_read(pkt, &pk->sec.rsa.d);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->sec.rsa.p);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->sec.rsa.q);
if (res < 0) break;
if (res < 0)
break;
res = pgp_mpi_read(pkt, &pk->sec.rsa.u);
if (res < 0) break;
if (res < 0)
break;
break;
case PGP_PUB_ELG_ENCRYPT:
res = pgp_mpi_read(pf_key, &pk->sec.elg.x);
@ -426,31 +456,33 @@ static int process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p,
}
static int
internal_read_key(PullFilter *src, PGP_PubKey **pk_p,
const uint8 *psw, int psw_len, int pubtype)
internal_read_key(PullFilter * src, PGP_PubKey ** pk_p,
const uint8 *psw, int psw_len, int pubtype)
{
PullFilter *pkt = NULL;
int res;
uint8 tag;
int len;
int res;
uint8 tag;
int len;
PGP_PubKey *enc_key = NULL;
PGP_PubKey *pk = NULL;
int got_main_key = 0;
int got_main_key = 0;
/*
* Search for encryption key.
*
* Error out on anything fancy.
*/
while (1) {
while (1)
{
res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
if (res <= 0)
break;
res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
if (res < 0)
break;
switch (tag) {
switch (tag)
{
case PGP_PKT_PUBLIC_KEY:
case PGP_PKT_SECRET_KEY:
if (got_main_key)
@ -489,7 +521,7 @@ internal_read_key(PullFilter *src, PGP_PubKey **pk_p,
res = PXE_PGP_UNEXPECTED_PKT;
}
pullf_free(pkt);
pkt = NULL;
pkt = NULL;
if (pk != NULL)
{
@ -531,10 +563,10 @@ internal_read_key(PullFilter *src, PGP_PubKey **pk_p,
}
int
pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
const uint8 *key, int key_len, int pubtype)
pgp_set_pubkey(PGP_Context * ctx, MBuf * keypkt,
const uint8 *key, int key_len, int pubtype)
{
int res;
int res;
PullFilter *src;
PGP_PubKey *pk = NULL;
@ -550,4 +582,3 @@ pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
return res < 0 ? res : 0;
}

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/pgp-s2k.c,v 1.3 2005/07/18 17:12:54 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-s2k.c,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -36,7 +36,7 @@
#include "pgp.h"
static int
calc_s2k_simple(PGP_S2K * s2k, PX_MD *md, const uint8 *key,
calc_s2k_simple(PGP_S2K * s2k, PX_MD * md, const uint8 *key,
unsigned key_len)
{
unsigned md_bs,
@ -81,7 +81,7 @@ calc_s2k_simple(PGP_S2K * s2k, PX_MD *md, const uint8 *key,
}
static int
calc_s2k_salted(PGP_S2K * s2k, PX_MD *md, const uint8 *key, unsigned key_len)
calc_s2k_salted(PGP_S2K * s2k, PX_MD * md, const uint8 *key, unsigned key_len)
{
unsigned md_bs,
md_rlen;
@ -126,8 +126,8 @@ calc_s2k_salted(PGP_S2K * s2k, PX_MD *md, const uint8 *key, unsigned key_len)
}
static int
calc_s2k_iter_salted(PGP_S2K * s2k, PX_MD *md, const uint8 *key,
unsigned key_len)
calc_s2k_iter_salted(PGP_S2K * s2k, PX_MD * md, const uint8 *key,
unsigned key_len)
{
unsigned md_bs,
md_rlen;
@ -200,7 +200,7 @@ calc_s2k_iter_salted(PGP_S2K * s2k, PX_MD *md, const uint8 *key,
/*
* Decide S2K_ISALTED iteration count
*
*
* Too small: weak
* Too big: slow
* gpg defaults to 96 => 65536 iters
@ -213,15 +213,16 @@ decide_count(unsigned rand_byte)
}
int
pgp_s2k_fill(PGP_S2K *s2k, int mode,int digest_algo)
pgp_s2k_fill(PGP_S2K * s2k, int mode, int digest_algo)
{
int res = 0;
uint8 tmp;
int res = 0;
uint8 tmp;
s2k->mode = mode;
s2k->digest_algo = digest_algo;
switch (s2k->mode) {
switch (s2k->mode)
{
case 0:
break;
case 1:
@ -243,13 +244,14 @@ pgp_s2k_fill(PGP_S2K *s2k, int mode,int digest_algo)
}
int
pgp_s2k_read(PullFilter *src, PGP_S2K *s2k)
pgp_s2k_read(PullFilter * src, PGP_S2K * s2k)
{
int res = 0;
int res = 0;
GETBYTE(src, s2k->mode);
GETBYTE(src, s2k->digest_algo);
switch (s2k->mode) {
switch (s2k->mode)
{
case 0:
break;
case 1:
@ -267,10 +269,11 @@ pgp_s2k_read(PullFilter *src, PGP_S2K *s2k)
return res;
}
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
int
pgp_s2k_process(PGP_S2K * s2k, int cipher, const uint8 *key, int key_len)
{
int res;
PX_MD *md;
int res;
PX_MD *md;
s2k->key_len = pgp_get_cipher_key_size(cipher);
if (s2k->key_len <= 0)
@ -280,7 +283,8 @@ int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
if (res < 0)
return res;
switch (s2k->mode) {
switch (s2k->mode)
{
case 0:
res = calc_s2k_simple(s2k, md, key, key_len);
break;
@ -296,4 +300,3 @@ int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
px_md_free(md);
return res;
}

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/pgp.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp.c,v 1.3 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -62,8 +62,8 @@ struct cipher_info
const char *name;
int code;
const char *int_name;
int key_len;
int block_len;
int key_len;
int block_len;
};
static const struct digest_info digest_list[] = {
@ -78,15 +78,15 @@ static const struct digest_info digest_list[] = {
};
static const struct cipher_info cipher_list[] = {
{"3des", PGP_SYM_DES3, "3des-ecb", 192/8, 64/8},
{"cast5", PGP_SYM_CAST5, "cast5-ecb", 128/8, 64/8},
{"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128/8, 64/8},
{"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128/8, 64/8},
{"aes", PGP_SYM_AES_128, "aes-ecb", 128/8, 128/8},
{"aes128", PGP_SYM_AES_128, "aes-ecb", 128/8, 128/8},
{"aes192", PGP_SYM_AES_192, "aes-ecb", 192/8, 128/8},
{"aes256", PGP_SYM_AES_256, "aes-ecb", 256/8, 128/8},
{"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256/8, 128/8},
{"3des", PGP_SYM_DES3, "3des-ecb", 192 / 8, 64 / 8},
{"cast5", PGP_SYM_CAST5, "cast5-ecb", 128 / 8, 64 / 8},
{"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
{"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
{"aes", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
{"aes128", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
{"aes192", PGP_SYM_AES_192, "aes-ecb", 192 / 8, 128 / 8},
{"aes256", PGP_SYM_AES_256, "aes-ecb", 256 / 8, 128 / 8},
{"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256 / 8, 128 / 8},
{NULL, 0, NULL}
};
@ -94,6 +94,7 @@ static const struct cipher_info *
get_cipher_info(int code)
{
const struct cipher_info *i;
for (i = cipher_list; i->name; i++)
if (i->code == code)
return i;
@ -104,6 +105,7 @@ int
pgp_get_digest_code(const char *name)
{
const struct digest_info *i;
for (i = digest_list; i->name; i++)
if (pg_strcasecmp(i->name, name) == 0)
return i->code;
@ -114,6 +116,7 @@ int
pgp_get_cipher_code(const char *name)
{
const struct cipher_info *i;
for (i = cipher_list; i->name; i++)
if (pg_strcasecmp(i->name, name) == 0)
return i->code;
@ -124,6 +127,7 @@ const char *
pgp_get_digest_name(int code)
{
const struct digest_info *i;
for (i = digest_list; i->name; i++)
if (i->code == code)
return i->name;
@ -134,6 +138,7 @@ const char *
pgp_get_cipher_name(int code)
{
const struct cipher_info *i = get_cipher_info(code);
if (i != NULL)
return i->name;
return NULL;
@ -143,6 +148,7 @@ int
pgp_get_cipher_key_size(int code)
{
const struct cipher_info *i = get_cipher_info(code);
if (i != NULL)
return i->key_len;
return 0;
@ -152,6 +158,7 @@ int
pgp_get_cipher_block_size(int code)
{
const struct cipher_info *i = get_cipher_info(code);
if (i != NULL)
return i->block_len;
return 0;
@ -300,6 +307,7 @@ int
pgp_set_cipher_algo(PGP_Context * ctx, const char *name)
{
int code = pgp_get_cipher_code(name);
if (code < 0)
return code;
ctx->cipher_algo = code;
@ -310,6 +318,7 @@ int
pgp_set_s2k_cipher_algo(PGP_Context * ctx, const char *name)
{
int code = pgp_get_cipher_code(name);
if (code < 0)
return code;
ctx->s2k_cipher_algo = code;
@ -320,6 +329,7 @@ int
pgp_set_s2k_digest_algo(PGP_Context * ctx, const char *name)
{
int code = pgp_get_digest_code(name);
if (code < 0)
return code;
ctx->s2k_digest_algo = code;
@ -327,20 +337,20 @@ pgp_set_s2k_digest_algo(PGP_Context * ctx, const char *name)
}
int
pgp_get_unicode_mode(PGP_Context *ctx)
pgp_get_unicode_mode(PGP_Context * ctx)
{
return ctx->unicode_mode;
}
int
pgp_set_unicode_mode(PGP_Context *ctx, int mode)
pgp_set_unicode_mode(PGP_Context * ctx, int mode)
{
ctx->unicode_mode = mode ? 1 : 0;
return 0;
}
int
pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
pgp_set_symkey(PGP_Context * ctx, const uint8 *key, int len)
{
if (key == NULL || len < 1)
return PXE_ARGUMENT_ERROR;
@ -348,4 +358,3 @@ pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
ctx->sym_key_len = len;
return 0;
}

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/pgp.h,v 1.3 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp.h,v 1.4 2005/10/15 02:49:06 momjian Exp $
*/
enum
@ -55,7 +55,7 @@ enum
PGP_PKT_USER_ATTR = 17,
PGP_PKT_SYMENCRYPTED_DATA_MDC = 18,
PGP_PKT_MDC = 19,
PGP_PKT_PRIV_61 = 61 /* occurs in gpg secring */
PGP_PKT_PRIV_61 = 61 /* occurs in gpg secring */
} PGP_PKT_TYPE;
enum
@ -93,11 +93,11 @@ enum
enum
{
PGP_DIGEST_MD5 = 1, /* should, deprecated */
PGP_DIGEST_SHA1 = 2, /* must */
PGP_DIGEST_SHA1 = 2, /* must */
PGP_DIGEST_RIPEMD160 = 3,
PGP_DIGEST_XSHA = 4, /* obsolete */
PGP_DIGEST_XSHA = 4, /* obsolete */
PGP_DIGEST_MD2 = 5, /* obsolete */
PGP_DIGEST_TIGER192 = 6, /* obsolete */
PGP_DIGEST_TIGER192 = 6, /* obsolete */
PGP_DIGEST_HAVAL5_160 = 7, /* obsolete */
PGP_DIGEST_SHA256 = 8,
PGP_DIGEST_SHA384 = 9,
@ -114,14 +114,15 @@ typedef struct PGP_PubKey PGP_PubKey;
typedef struct PGP_Context PGP_Context;
typedef struct PGP_S2K PGP_S2K;
struct PGP_S2K {
uint8 mode;
uint8 digest_algo;
uint8 salt[8];
uint8 iter;
struct PGP_S2K
{
uint8 mode;
uint8 digest_algo;
uint8 salt[8];
uint8 iter;
/* calculated: */
uint8 key[PGP_MAX_KEY];
uint8 key_len;
uint8 key[PGP_MAX_KEY];
uint8 key_len;
};
@ -151,9 +152,9 @@ struct PGP_Context
int in_mdc_pkt;
int use_mdcbuf_filter;
PX_MD *mdc_ctx;
PGP_PubKey *pub_key; /* ctx owns it*/
const uint8 *sym_key; /* ctx does not own it */
PGP_PubKey *pub_key; /* ctx owns it */
const uint8 *sym_key; /* ctx does not own it */
int sym_key_len;
/*
@ -163,54 +164,64 @@ struct PGP_Context
unsigned sess_key_len;
};
struct PGP_MPI {
uint8 *data;
int bits;
int bytes;
struct PGP_MPI
{
uint8 *data;
int bits;
int bytes;
};
struct PGP_PubKey {
uint8 ver;
uint8 time[4];
uint8 algo;
struct PGP_PubKey
{
uint8 ver;
uint8 time[4];
uint8 algo;
/* public part */
union {
struct {
PGP_MPI *p;
PGP_MPI *g;
PGP_MPI *y;
} elg;
struct {
PGP_MPI *n;
PGP_MPI *e;
} rsa;
struct {
PGP_MPI *p;
PGP_MPI *q;
PGP_MPI *g;
PGP_MPI *y;
} dsa;
} pub;
union
{
struct
{
PGP_MPI *p;
PGP_MPI *g;
PGP_MPI *y;
} elg;
struct
{
PGP_MPI *n;
PGP_MPI *e;
} rsa;
struct
{
PGP_MPI *p;
PGP_MPI *q;
PGP_MPI *g;
PGP_MPI *y;
} dsa;
} pub;
/* secret part */
union {
struct {
PGP_MPI *x;
} elg;
struct {
PGP_MPI *d;
PGP_MPI *p;
PGP_MPI *q;
PGP_MPI *u;
} rsa;
struct {
PGP_MPI *x;
} dsa;
} sec;
union
{
struct
{
PGP_MPI *x;
} elg;
struct
{
PGP_MPI *d;
PGP_MPI *p;
PGP_MPI *q;
PGP_MPI *u;
} rsa;
struct
{
PGP_MPI *x;
} dsa;
} sec;
uint8 key_id[8];
int can_encrypt;
uint8 key_id[8];
int can_encrypt;
};
int pgp_init(PGP_Context ** ctx);
@ -236,11 +247,11 @@ int pgp_set_text_mode(PGP_Context * ctx, int mode);
int pgp_set_unicode_mode(PGP_Context * ctx, int mode);
int pgp_get_unicode_mode(PGP_Context * ctx);
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int klen);
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
const uint8 *key, int klen, int pubtype);
int pgp_set_symkey(PGP_Context * ctx, const uint8 *key, int klen);
int pgp_set_pubkey(PGP_Context * ctx, MBuf * keypkt,
const uint8 *key, int klen, int pubtype);
int pgp_get_keyid(MBuf *pgp_data, char *dst);
int pgp_get_keyid(MBuf * pgp_data, char *dst);
/* internal functions */
@ -249,55 +260,55 @@ int pgp_load_cipher(int c, PX_Cipher ** res);
int pgp_get_cipher_key_size(int c);
int pgp_get_cipher_block_size(int c);
int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo);
int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k);
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int klen);
int pgp_s2k_fill(PGP_S2K * s2k, int mode, int digest_algo);
int pgp_s2k_read(PullFilter * src, PGP_S2K * s2k);
int pgp_s2k_process(PGP_S2K * s2k, int cipher, const uint8 *key, int klen);
typedef struct PGP_CFB PGP_CFB;
int pgp_cfb_create(PGP_CFB **ctx_p, int algo,
const uint8 *key, int key_len, int recync, uint8 *iv);
void pgp_cfb_free(PGP_CFB *ctx);
int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
int pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
int
pgp_cfb_create(PGP_CFB ** ctx_p, int algo,
const uint8 *key, int key_len, int recync, uint8 *iv);
void pgp_cfb_free(PGP_CFB * ctx);
int pgp_cfb_encrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst);
int pgp_cfb_decrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst);
int pgp_armor_encode(const uint8 *src, unsigned len, uint8 *dst);
int pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst);
unsigned pgp_armor_enc_len(unsigned len);
unsigned pgp_armor_dec_len(unsigned len);
int pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst);
int pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src);
int pgp_compress_filter(PushFilter ** res, PGP_Context * ctx, PushFilter * dst);
int pgp_decompress_filter(PullFilter ** res, PGP_Context * ctx, PullFilter * src);
int pgp_key_alloc(PGP_PubKey **pk_p);
void pgp_key_free(PGP_PubKey *pk);
int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p);
int pgp_key_alloc(PGP_PubKey ** pk_p);
void pgp_key_free(PGP_PubKey * pk);
int _pgp_read_public_key(PullFilter * pkt, PGP_PubKey ** pk_p);
int pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt);
int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
int pkttype, PGP_Context *ctx);
int pgp_parse_pubenc_sesskey(PGP_Context * ctx, PullFilter * pkt);
int pgp_create_pkt_reader(PullFilter ** pf_p, PullFilter * src, int len,
int pkttype, PGP_Context * ctx);
int pgp_parse_pkt_hdr(PullFilter * src, uint8 *tag, int *len_p,
int allow_ctx);
int allow_ctx);
int pgp_skip_packet(PullFilter *pkt);
int pgp_expect_packet_end(PullFilter *pkt);
int pgp_skip_packet(PullFilter * pkt);
int pgp_expect_packet_end(PullFilter * pkt);
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst);
int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p);
int pgp_write_pubenc_sesskey(PGP_Context * ctx, PushFilter * dst);
int pgp_create_pkt_writer(PushFilter * dst, int tag, PushFilter ** res_p);
int pgp_mpi_alloc(int bits, PGP_MPI **mpi);
int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi);
int pgp_mpi_free(PGP_MPI *mpi);
int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi);
int pgp_mpi_write(PushFilter *dst, PGP_MPI *n);
int pgp_mpi_hash(PX_MD *md, PGP_MPI *n);
unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n);
int pgp_mpi_alloc(int bits, PGP_MPI ** mpi);
int pgp_mpi_create(uint8 *data, int bits, PGP_MPI ** mpi);
int pgp_mpi_free(PGP_MPI * mpi);
int pgp_mpi_read(PullFilter * src, PGP_MPI ** mpi);
int pgp_mpi_write(PushFilter * dst, PGP_MPI * n);
int pgp_mpi_hash(PX_MD * md, PGP_MPI * n);
unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI * n);
int pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *m,
PGP_MPI **c1, PGP_MPI **c2);
int pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *c1, PGP_MPI *c2,
PGP_MPI **m);
int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *m, PGP_MPI **c);
int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *c, PGP_MPI **m);
int pgp_elgamal_encrypt(PGP_PubKey * pk, PGP_MPI * m,
PGP_MPI ** c1, PGP_MPI ** c2);
int pgp_elgamal_decrypt(PGP_PubKey * pk, PGP_MPI * c1, PGP_MPI * c2,
PGP_MPI ** m);
int pgp_rsa_encrypt(PGP_PubKey * pk, PGP_MPI * m, PGP_MPI ** c);
int pgp_rsa_decrypt(PGP_PubKey * pk, PGP_MPI * c, PGP_MPI ** m);
extern struct PullFilterOps pgp_decrypt_filter;

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.14 2005/09/24 19:14:04 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.15 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -77,9 +77,9 @@ struct px_crypt_algo
};
static const struct px_crypt_algo
px_crypt_list[] = {
px_crypt_list[] = {
{"$2a$", 4, run_crypt_bf},
{"$2$", 3, NULL}, /* N/A */
{"$2$", 3, NULL}, /* N/A */
{"$1$", 3, run_crypt_md5},
{"_", 1, run_crypt_des},
{"", 0, run_crypt_des},
@ -164,4 +164,3 @@ px_gen_salt(const char *salt_type, char *buf, int rounds)
return strlen(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/px-crypt.h,v 1.8 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.h,v 1.9 2005/10/15 02:49:06 momjian Exp $
*/
#ifndef _PX_CRYPT_H
@ -65,13 +65,13 @@ extern char px_crypt_a64[];
/* crypt-gensalt.c */
char *_crypt_gensalt_traditional_rn(unsigned long count,
const char *input, int size, char *output, int output_size);
const char *input, int size, char *output, int output_size);
char *_crypt_gensalt_extended_rn(unsigned long count,
const char *input, int size, char *output, int output_size);
const char *input, int size, char *output, int output_size);
char *_crypt_gensalt_md5_rn(unsigned long count,
const char *input, int size, char *output, int output_size);
const char *input, int size, char *output, int output_size);
char *_crypt_gensalt_blowfish_rn(unsigned long count,
const char *input, int size, char *output, int output_size);
const char *input, int size, char *output, int output_size);
/* disable 'extended DES crypt' */
/* #define DISABLE_XDES */

View File

@ -26,15 +26,16 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/px.c,v 1.14 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px.c,v 1.15 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
#include "px.h"
struct error_desc {
int err;
struct error_desc
{
int err;
const char *desc;
};
@ -67,14 +68,14 @@ static const struct error_desc px_err_list[] = {
{PXE_PGP_UNEXPECTED_PKT, "Unexpected packet in key data"},
{PXE_PGP_NO_BIGNUM,
"public-key functions disabled - "
"pgcrypto needs OpenSSL for bignums"},
"pgcrypto needs OpenSSL for bignums"},
{PXE_PGP_MATH_FAILED, "Math operation failed"},
{PXE_PGP_SHORT_ELGAMAL_KEY, "Elgamal keys must be at least 1024 bits long"},
{PXE_PGP_RSA_UNSUPPORTED, "pgcrypto does not support RSA keys"},
{PXE_PGP_UNKNOWN_PUBALGO, "Unknown public-key encryption algorithm"},
{PXE_PGP_WRONG_KEY, "Wrong key"},
{PXE_PGP_MULTIPLE_KEYS,
"Several keys given - pgcrypto does not handle keyring"},
"Several keys given - pgcrypto does not handle keyring"},
{PXE_PGP_EXPECT_PUBLIC_KEY, "Refusing to encrypt with secret key"},
{PXE_PGP_EXPECT_SECRET_KEY, "Cannot decrypt with public key"},
{PXE_PGP_NOT_V4_KEYPKT, "Only V4 key packets are supported"},
@ -87,13 +88,15 @@ static const struct error_desc px_err_list[] = {
/* fake this as PXE_PGP_CORRUPT_DATA */
{PXE_MBUF_SHORT_READ, "Corrupt data"},
{0, NULL},
};
const char *px_strerror(int err)
const char *
px_strerror(int err)
{
const struct error_desc *e;
for (e = px_err_list; e->desc; e++)
if (e->err == err)
return e->desc;
@ -113,19 +116,24 @@ px_resolve_alias(const PX_Alias * list, const char *name)
return name;
}
static void (*debug_handler)(const char *) = NULL;
static void (*debug_handler) (const char *) = NULL;
void px_set_debug_handler(void (*handler)(const char *))
void
px_set_debug_handler(void (*handler) (const char *))
{
debug_handler = handler;
}
void px_debug(const char *fmt, ...)
void
px_debug(const char *fmt,...)
{
va_list ap;
va_list ap;
va_start(ap, fmt);
if (debug_handler) {
char buf[512];
if (debug_handler)
{
char buf[512];
vsnprintf(buf, sizeof(buf), fmt, ap);
debug_handler(buf);
}

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.15 2005/08/13 02:06:20 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.16 2005/10/15 02:49:06 momjian Exp $
*/
#ifndef __PX_H
@ -90,12 +90,12 @@ void px_free(void *p);
#define PXE_PGP_CORRUPT_DATA -100
#define PXE_PGP_CORRUPT_ARMOR -101
#define PXE_PGP_UNSUPPORTED_COMPR -102
#define PXE_PGP_UNSUPPORTED_CIPHER -103
#define PXE_PGP_UNSUPPORTED_HASH -104
#define PXE_PGP_COMPRESSION_ERROR -105
#define PXE_PGP_NOT_TEXT -106
#define PXE_PGP_UNEXPECTED_PKT -107
#define PXE_PGP_UNSUPPORTED_COMPR -102
#define PXE_PGP_UNSUPPORTED_CIPHER -103
#define PXE_PGP_UNSUPPORTED_HASH -104
#define PXE_PGP_COMPRESSION_ERROR -105
#define PXE_PGP_NOT_TEXT -106
#define PXE_PGP_UNEXPECTED_PKT -107
#define PXE_PGP_NO_BIGNUM -108
#define PXE_PGP_MATH_FAILED -109
#define PXE_PGP_SHORT_ELGAMAL_KEY -110
@ -110,7 +110,7 @@ void px_free(void *p);
#define PXE_PGP_NO_USABLE_KEY -119
#define PXE_PGP_NEED_SECRET_PSW -120
#define PXE_PGP_BAD_S2K_MODE -121
#define PXE_PGP_UNSUPPORTED_PUBALGO -122
#define PXE_PGP_UNSUPPORTED_PUBALGO -122
#define PXE_PGP_MULTIPLE_SUBKEYS -123
@ -132,7 +132,7 @@ struct px_digest
union
{
unsigned code;
void *ptr;
void *ptr;
} p;
};
@ -207,9 +207,10 @@ const char *px_strerror(int err);
const char *px_resolve_alias(const PX_Alias * aliases, const char *name);
void px_set_debug_handler(void (*handler)(const char *));
void px_set_debug_handler(void (*handler) (const char *));
#ifdef PX_DEBUG
void px_debug(const char *fmt, ...);
void px_debug(const char *fmt,...);
#else
#define px_debug(...)
#endif

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/random.c,v 1.15 2005/07/18 17:09:01 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/random.c,v 1.16 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -95,7 +95,6 @@ try_dev_random(uint8 *dst)
dst += res;
return dst;
}
#endif
/*
@ -111,22 +110,23 @@ try_dev_random(uint8 *dst)
/*
* this function is from libtomcrypt
*
*
* try to use Microsoft crypto API
*/
static uint8 * try_win32_genrand(uint8 *dst)
static uint8 *
try_win32_genrand(uint8 *dst)
{
int res;
HCRYPTPROV h = 0;
int res;
HCRYPTPROV h = 0;
res = CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
if (!res)
res = CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET);
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET);
if (!res)
return dst;
res = CryptGenRandom(h, RND_BYTES, dst);
if (res == TRUE)
dst += RND_BYTES;
@ -135,9 +135,10 @@ static uint8 * try_win32_genrand(uint8 *dst)
return dst;
}
static uint8 * try_win32_perfc(uint8 *dst)
static uint8 *
try_win32_perfc(uint8 *dst)
{
int res;
int res;
LARGE_INTEGER time;
res = QueryPerformanceCounter(&time);
@ -147,8 +148,7 @@ static uint8 * try_win32_perfc(uint8 *dst)
memcpy(dst, &time, sizeof(time));
return dst + sizeof(time);
}
#endif /* WIN32 */
#endif /* WIN32 */
/*
@ -174,33 +174,34 @@ static uint8 * try_win32_perfc(uint8 *dst)
static uint8 *
try_unix_std(uint8 *dst)
{
pid_t pid;
int x;
PX_MD *md;
pid_t pid;
int x;
PX_MD *md;
struct timeval tv;
int res;
int res;
/* process id */
pid = getpid();
memcpy(dst, (uint8*)&pid, sizeof(pid));
memcpy(dst, (uint8 *) &pid, sizeof(pid));
dst += sizeof(pid);
/* time */
gettimeofday(&tv, NULL);
memcpy(dst, (uint8*)&tv, sizeof(tv));
memcpy(dst, (uint8 *) &tv, sizeof(tv));
dst += sizeof(tv);
/* pointless, but should not hurt */
x = random();
memcpy(dst, (uint8*)&x, sizeof(x));
memcpy(dst, (uint8 *) &x, sizeof(x));
dst += sizeof(x);
/* let's be desperate */
res = px_find_digest("sha1", &md);
if (res >= 0) {
uint8 *ptr;
uint8 stack[8192];
int alloc = 32*1024;
if (res >= 0)
{
uint8 *ptr;
uint8 stack[8192];
int alloc = 32 * 1024;
px_md_update(md, stack, sizeof(stack));
ptr = px_alloc(alloc);
@ -215,7 +216,6 @@ try_unix_std(uint8 *dst)
return dst;
}
#endif
/*
@ -223,9 +223,11 @@ try_unix_std(uint8 *dst)
*
* dst should have room for 1024 bytes.
*/
unsigned px_acquire_system_randomness(uint8 *dst)
unsigned
px_acquire_system_randomness(uint8 *dst)
{
uint8 *p = dst;
uint8 *p = dst;
#ifdef TRY_DEV_RANDOM
p = try_dev_random(p);
#endif
@ -240,4 +242,3 @@ unsigned px_acquire_system_randomness(uint8 *dst)
#endif
return p - dst;
}

View File

@ -1,6 +1,6 @@
/* $OpenBSD: rijndael.c,v 1.6 2000/12/09 18:51:34 markus Exp $ */
/* $PostgreSQL: pgsql/contrib/pgcrypto/rijndael.c,v 1.11 2005/07/11 15:07:59 tgl Exp $ */
/* $PostgreSQL: pgsql/contrib/pgcrypto/rijndael.c,v 1.12 2005/10/15 02:49:06 momjian Exp $ */
/* This is an independent implementation of the encryption algorithm: */
/* */
@ -91,7 +91,6 @@ static void gen_tabs(void);
#include "rijndael.tbl"
#define tab_gen 1
#else /* !PRE_CALC_TABLES */
static u1byte pow_tab[256];
@ -143,7 +142,6 @@ static u4byte tab_gen = 0;
il_tab[1][byte((bi)[((n) + 3) & 3],1)] ^ \
il_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
il_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
#else
#define ls_box(x) \

View File

@ -3,7 +3,7 @@
/*
* FILE: sha2.c
* AUTHOR: Aaron D. Gifford <me@aarongifford.com>
*
*
* Copyright (c) 2000-2001, Aaron D. Gifford
* All rights reserved.
*
@ -11,18 +11,18 @@
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@ -33,7 +33,7 @@
*
* $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
*
* $PostgreSQL: pgsql/contrib/pgcrypto/sha2.c,v 1.4 2005/07/12 20:27:42 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/sha2.c,v 1.5 2005/10/15 02:49:06 momjian Exp $
*/
#include "postgres.h"
@ -48,11 +48,11 @@
* loop version for the hash transform rounds (defined using macros
* later in this file). Either define on the command line, for example:
*
* cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
* cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
*
* or define below:
*
* #define SHA2_UNROLL_TRANSFORM
* #define SHA2_UNROLL_TRANSFORM
*
*/
@ -69,16 +69,16 @@
* If your system does not define the above, then you can do so by
* hand like this:
*
* #define LITTLE_ENDIAN 1234
* #define BIG_ENDIAN 4321
* #define LITTLE_ENDIAN 1234
* #define BIG_ENDIAN 4321
*
* And for little-endian machines, add:
*
* #define BYTE_ORDER LITTLE_ENDIAN
* #define BYTE_ORDER LITTLE_ENDIAN
*
* Or for big-endian machines:
*
* #define BYTE_ORDER BIG_ENDIAN
* #define BYTE_ORDER BIG_ENDIAN
*
* The FreeBSD machine this was written on defines BYTE_ORDER
* appropriately by including <sys/types.h> (which in turn includes
@ -108,11 +108,11 @@
uint64 tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
((tmp & 0x00ff00ff00ff00ffULL) << 8); \
((tmp & 0x00ff00ff00ff00ffULL) << 8); \
(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
((tmp & 0x0000ffff0000ffffULL) << 16); \
((tmp & 0x0000ffff0000ffffULL) << 16); \
}
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
/*
* Macro for incrementally adding the unsigned 64-bit integer n to the
@ -130,13 +130,13 @@
/*
* Bit shifting and rotation (used by the six SHA-XYZ logical functions:
*
* NOTE: The naming of R and S appears backwards here (R is a SHIFT and
* S is a ROTATION) because the SHA-256/384/512 description document
* (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
* same "backwards" definition.
* NOTE: The naming of R and S appears backwards here (R is a SHIFT and
* S is a ROTATION) because the SHA-256/384/512 description document
* (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
* same "backwards" definition.
*/
/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
#define R(b,x) ((x) >> (b))
#define R(b,x) ((x) >> (b))
/* 32-bit Rotate-right (used in SHA-256): */
#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
@ -163,9 +163,9 @@
* library -- they are intended for private internal visibility/use
* only.
*/
void SHA512_Last(SHA512_CTX *);
void SHA256_Transform(SHA256_CTX *, const uint8 *);
void SHA512_Transform(SHA512_CTX *, const uint8 *);
void SHA512_Last(SHA512_CTX *);
void SHA256_Transform(SHA256_CTX *, const uint8 *);
void SHA512_Transform(SHA512_CTX *, const uint8 *);
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
@ -272,7 +272,7 @@ static const uint64 sha512_initial_hash_value[8] = {
/*** SHA-256: *********************************************************/
void
SHA256_Init(SHA256_CTX *context)
SHA256_Init(SHA256_CTX * context)
{
if (context == NULL)
return;
@ -285,36 +285,46 @@ SHA256_Init(SHA256_CTX *context)
/* Unrolled SHA-256 round macros: */
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \
W256[j] = (uint32)data[3] | ((uint32)data[2] << 8) | \
((uint32)data[1] << 16) | ((uint32)data[0] << 24); \
data += 4; \
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \
W256[j] = (uint32)data[3] | ((uint32)data[2] << 8) | \
((uint32)data[1] << 16) | ((uint32)data[0] << 24); \
data += 4; \
T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
(d) += T1; \
(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
j++; \
(d) += T1; \
(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
j++; \
} while(0)
#define ROUND256(a,b,c,d,e,f,g,h) do { \
s0 = W256[(j+1)&0x0f]; \
s0 = sigma0_256(s0); \
s1 = W256[(j+14)&0x0f]; \
s1 = sigma1_256(s1); \
T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
j++; \
#define ROUND256(a,b,c,d,e,f,g,h) do { \
s0 = W256[(j+1)&0x0f]; \
s0 = sigma0_256(s0); \
s1 = W256[(j+14)&0x0f]; \
s1 = sigma1_256(s1); \
T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
j++; \
} while(0)
void
SHA256_Transform(SHA256_CTX *context, const uint8 *data)
SHA256_Transform(SHA256_CTX * context, const uint8 *data)
{
uint32 a, b, c, d, e, f, g, h, s0, s1;
uint32 T1, *W256;
int j;
uint32 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint32 T1,
*W256;
int j;
W256 = (uint32 *)context->buffer;
W256 = (uint32 *) context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
@ -327,28 +337,30 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
h = context->state[7];
j = 0;
do {
do
{
/* Rounds 0 to 15 (unrolled): */
ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
ROUND256_0_TO_15(a, b, c, d, e, f, g, h);
ROUND256_0_TO_15(h, a, b, c, d, e, f, g);
ROUND256_0_TO_15(g, h, a, b, c, d, e, f);
ROUND256_0_TO_15(f, g, h, a, b, c, d, e);
ROUND256_0_TO_15(e, f, g, h, a, b, c, d);
ROUND256_0_TO_15(d, e, f, g, h, a, b, c);
ROUND256_0_TO_15(c, d, e, f, g, h, a, b);
ROUND256_0_TO_15(b, c, d, e, f, g, h, a);
} while (j < 16);
/* Now for the remaining rounds to 64: */
do {
ROUND256(a,b,c,d,e,f,g,h);
ROUND256(h,a,b,c,d,e,f,g);
ROUND256(g,h,a,b,c,d,e,f);
ROUND256(f,g,h,a,b,c,d,e);
ROUND256(e,f,g,h,a,b,c,d);
ROUND256(d,e,f,g,h,a,b,c);
ROUND256(c,d,e,f,g,h,a,b);
ROUND256(b,c,d,e,f,g,h,a);
do
{
ROUND256(a, b, c, d, e, f, g, h);
ROUND256(h, a, b, c, d, e, f, g);
ROUND256(g, h, a, b, c, d, e, f);
ROUND256(f, g, h, a, b, c, d, e);
ROUND256(e, f, g, h, a, b, c, d);
ROUND256(d, e, f, g, h, a, b, c);
ROUND256(c, d, e, f, g, h, a, b);
ROUND256(b, c, d, e, f, g, h, a);
} while (j < 64);
/* Compute the current intermediate hash value */
@ -364,17 +376,27 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
}
#else /* SHA2_UNROLL_TRANSFORM */
#else /* SHA2_UNROLL_TRANSFORM */
void
SHA256_Transform(SHA256_CTX *context, const uint8 *data)
SHA256_Transform(SHA256_CTX * context, const uint8 *data)
{
uint32 a, b, c, d, e, f, g, h, s0, s1;
uint32 T1, T2, *W256;
int j;
uint32 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint32 T1,
T2,
*W256;
int j;
W256 = (uint32 *)context->buffer;
W256 = (uint32 *) context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
@ -387,9 +409,10 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
h = context->state[7];
j = 0;
do {
W256[j] = (uint32)data[3] | ((uint32)data[2] << 8) |
((uint32)data[1] << 16) | ((uint32)data[0] << 24);
do
{
W256[j] = (uint32) data[3] | ((uint32) data[2] << 8) |
((uint32) data[1] << 16) | ((uint32) data[0] << 24);
data += 4;
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
@ -406,16 +429,17 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
j++;
} while (j < 16);
do {
do
{
/* Part of the message block expansion: */
s0 = W256[(j+1)&0x0f];
s0 = W256[(j + 1) & 0x0f];
s0 = sigma0_256(s0);
s1 = W256[(j+14)&0x0f];
s1 = W256[(j + 14) & 0x0f];
s1 = sigma1_256(s1);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
(W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
@ -442,31 +466,35 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
}
#endif /* SHA2_UNROLL_TRANSFORM */
#endif /* SHA2_UNROLL_TRANSFORM */
void
SHA256_Update(SHA256_CTX *context, const uint8 *data, size_t len)
SHA256_Update(SHA256_CTX * context, const uint8 *data, size_t len)
{
size_t freespace, usedspace;
size_t freespace,
usedspace;
/* Calling with no data is valid (we do nothing) */
if (len == 0)
return;
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
if (usedspace > 0) {
if (usedspace > 0)
{
/* Calculate how much free space is available in the buffer */
freespace = SHA256_BLOCK_LENGTH - usedspace;
if (len >= freespace) {
if (len >= freespace)
{
/* Fill the buffer completely and process it */
memcpy(&context->buffer[usedspace], data, freespace);
context->bitcount += freespace << 3;
len -= freespace;
data += freespace;
SHA256_Transform(context, context->buffer);
} else {
}
else
{
/* The buffer is not yet full */
memcpy(&context->buffer[usedspace], data, len);
context->bitcount += len << 3;
@ -475,14 +503,16 @@ SHA256_Update(SHA256_CTX *context, const uint8 *data, size_t len)
return;
}
}
while (len >= SHA256_BLOCK_LENGTH) {
while (len >= SHA256_BLOCK_LENGTH)
{
/* Process as many complete blocks as we can */
SHA256_Transform(context, data);
context->bitcount += SHA256_BLOCK_LENGTH << 3;
len -= SHA256_BLOCK_LENGTH;
data += SHA256_BLOCK_LENGTH;
}
if (len > 0) {
if (len > 0)
{
/* There's left-overs, so save 'em */
memcpy(context->buffer, data, len);
context->bitcount += len << 3;
@ -492,26 +522,32 @@ SHA256_Update(SHA256_CTX *context, const uint8 *data, size_t len)
}
void
SHA256_Final(uint8 digest[], SHA256_CTX *context)
SHA256_Final(uint8 digest[], SHA256_CTX * context)
{
unsigned int usedspace;
unsigned int usedspace;
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) {
if (digest != NULL)
{
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
#if BYTE_ORDER == LITTLE_ENDIAN
/* Convert FROM host byte order */
REVERSE64(context->bitcount,context->bitcount);
REVERSE64(context->bitcount, context->bitcount);
#endif
if (usedspace > 0) {
if (usedspace > 0)
{
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
if (usedspace <= SHA256_SHORT_BLOCK_LENGTH)
{
/* Set-up for the last transform: */
memset(&context->buffer[usedspace], 0, SHA256_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA256_BLOCK_LENGTH) {
}
else
{
if (usedspace < SHA256_BLOCK_LENGTH)
{
memset(&context->buffer[usedspace], 0, SHA256_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
@ -520,7 +556,9 @@ SHA256_Final(uint8 digest[], SHA256_CTX *context)
/* And set-up for the last transform: */
memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
}
} else {
}
else
{
/* Set-up for the last transform: */
memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
@ -528,7 +566,7 @@ SHA256_Final(uint8 digest[], SHA256_CTX *context)
*context->buffer = 0x80;
}
/* Set the bit count: */
*(uint64 *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
*(uint64 *) &context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
/* Final transform: */
SHA256_Transform(context, context->buffer);
@ -536,9 +574,11 @@ SHA256_Final(uint8 digest[], SHA256_CTX *context)
#if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE32(context->state[j],context->state[j]);
int j;
for (j = 0; j < 8; j++)
{
REVERSE32(context->state[j], context->state[j]);
}
}
#endif
@ -553,50 +593,60 @@ SHA256_Final(uint8 digest[], SHA256_CTX *context)
/*** SHA-512: *********************************************************/
void
SHA512_Init(SHA512_CTX *context)
SHA512_Init(SHA512_CTX * context)
{
if (context == NULL)
return;
memcpy(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
memset(context->buffer, 0, SHA512_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
context->bitcount[0] = context->bitcount[1] = 0;
}
#ifdef SHA2_UNROLL_TRANSFORM
/* Unrolled SHA-512 round macros: */
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do { \
W512[j] = (uint64)data[7] | ((uint64)data[6] << 8) | \
((uint64)data[5] << 16) | ((uint64)data[4] << 24) | \
((uint64)data[3] << 32) | ((uint64)data[2] << 40) | \
((uint64)data[1] << 48) | ((uint64)data[0] << 56); \
data += 8; \
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do { \
W512[j] = (uint64)data[7] | ((uint64)data[6] << 8) | \
((uint64)data[5] << 16) | ((uint64)data[4] << 24) | \
((uint64)data[3] << 32) | ((uint64)data[2] << 40) | \
((uint64)data[1] << 48) | ((uint64)data[0] << 56); \
data += 8; \
T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
(d) += T1; \
(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
j++; \
(d) += T1; \
(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
j++; \
} while(0)
#define ROUND512(a,b,c,d,e,f,g,h) do { \
s0 = W512[(j+1)&0x0f]; \
s0 = sigma0_512(s0); \
s1 = W512[(j+14)&0x0f]; \
s1 = sigma1_512(s1); \
T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + \
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
j++; \
#define ROUND512(a,b,c,d,e,f,g,h) do { \
s0 = W512[(j+1)&0x0f]; \
s0 = sigma0_512(s0); \
s1 = W512[(j+14)&0x0f]; \
s1 = sigma1_512(s1); \
T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + \
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
j++; \
} while(0)
void
SHA512_Transform(SHA512_CTX *context, const uint8 *data)
SHA512_Transform(SHA512_CTX * context, const uint8 *data)
{
uint64 a, b, c, d, e, f, g, h, s0, s1;
uint64 T1, *W512 = (uint64 *)context->buffer;
int j;
uint64 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint64 T1,
*W512 = (uint64 *) context->buffer;
int j;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
@ -609,27 +659,29 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
h = context->state[7];
j = 0;
do {
ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
do
{
ROUND512_0_TO_15(a, b, c, d, e, f, g, h);
ROUND512_0_TO_15(h, a, b, c, d, e, f, g);
ROUND512_0_TO_15(g, h, a, b, c, d, e, f);
ROUND512_0_TO_15(f, g, h, a, b, c, d, e);
ROUND512_0_TO_15(e, f, g, h, a, b, c, d);
ROUND512_0_TO_15(d, e, f, g, h, a, b, c);
ROUND512_0_TO_15(c, d, e, f, g, h, a, b);
ROUND512_0_TO_15(b, c, d, e, f, g, h, a);
} while (j < 16);
/* Now for the remaining rounds up to 79: */
do {
ROUND512(a,b,c,d,e,f,g,h);
ROUND512(h,a,b,c,d,e,f,g);
ROUND512(g,h,a,b,c,d,e,f);
ROUND512(f,g,h,a,b,c,d,e);
ROUND512(e,f,g,h,a,b,c,d);
ROUND512(d,e,f,g,h,a,b,c);
ROUND512(c,d,e,f,g,h,a,b);
ROUND512(b,c,d,e,f,g,h,a);
do
{
ROUND512(a, b, c, d, e, f, g, h);
ROUND512(h, a, b, c, d, e, f, g);
ROUND512(g, h, a, b, c, d, e, f);
ROUND512(f, g, h, a, b, c, d, e);
ROUND512(e, f, g, h, a, b, c, d);
ROUND512(d, e, f, g, h, a, b, c);
ROUND512(c, d, e, f, g, h, a, b);
ROUND512(b, c, d, e, f, g, h, a);
} while (j < 80);
/* Compute the current intermediate hash value */
@ -645,15 +697,25 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
}
#else /* SHA2_UNROLL_TRANSFORM */
#else /* SHA2_UNROLL_TRANSFORM */
void
SHA512_Transform(SHA512_CTX *context, const uint8 *data)
SHA512_Transform(SHA512_CTX * context, const uint8 *data)
{
uint64 a, b, c, d, e, f, g, h, s0, s1;
uint64 T1, T2, *W512 = (uint64 *)context->buffer;
int j;
uint64 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint64 T1,
T2,
*W512 = (uint64 *) context->buffer;
int j;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
@ -666,11 +728,12 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
h = context->state[7];
j = 0;
do {
W512[j] = (uint64)data[7] | ((uint64)data[6] << 8) |
((uint64)data[5] << 16) | ((uint64)data[4] << 24) |
((uint64)data[3] << 32) | ((uint64)data[2] << 40) |
((uint64)data[1] << 48) | ((uint64)data[0] << 56);
do
{
W512[j] = (uint64) data[7] | ((uint64) data[6] << 8) |
((uint64) data[5] << 16) | ((uint64) data[4] << 24) |
((uint64) data[3] << 32) | ((uint64) data[2] << 40) |
((uint64) data[1] << 48) | ((uint64) data[0] << 56);
data += 8;
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
@ -687,16 +750,17 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
j++;
} while (j < 16);
do {
do
{
/* Part of the message block expansion: */
s0 = W512[(j+1)&0x0f];
s0 = W512[(j + 1) & 0x0f];
s0 = sigma0_512(s0);
s1 = W512[(j+14)&0x0f];
s1 = sigma1_512(s1);
s1 = W512[(j + 14) & 0x0f];
s1 = sigma1_512(s1);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
(W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
@ -723,31 +787,35 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
}
#endif /* SHA2_UNROLL_TRANSFORM */
#endif /* SHA2_UNROLL_TRANSFORM */
void
SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
SHA512_Update(SHA512_CTX * context, const uint8 *data, size_t len)
{
size_t freespace, usedspace;
size_t freespace,
usedspace;
/* Calling with no data is valid (we do nothing) */
if (len == 0)
return;
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
if (usedspace > 0) {
if (usedspace > 0)
{
/* Calculate how much free space is available in the buffer */
freespace = SHA512_BLOCK_LENGTH - usedspace;
if (len >= freespace) {
if (len >= freespace)
{
/* Fill the buffer completely and process it */
memcpy(&context->buffer[usedspace], data, freespace);
ADDINC128(context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
SHA512_Transform(context, context->buffer);
} else {
}
else
{
/* The buffer is not yet full */
memcpy(&context->buffer[usedspace], data, len);
ADDINC128(context->bitcount, len << 3);
@ -756,14 +824,16 @@ SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
return;
}
}
while (len >= SHA512_BLOCK_LENGTH) {
while (len >= SHA512_BLOCK_LENGTH)
{
/* Process as many complete blocks as we can */
SHA512_Transform(context, data);
ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
len -= SHA512_BLOCK_LENGTH;
data += SHA512_BLOCK_LENGTH;
}
if (len > 0) {
if (len > 0)
{
/* There's left-overs, so save 'em */
memcpy(context->buffer, data, len);
ADDINC128(context->bitcount, len << 3);
@ -773,25 +843,30 @@ SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
}
void
SHA512_Last(SHA512_CTX *context)
SHA512_Last(SHA512_CTX * context)
{
unsigned int usedspace;
unsigned int usedspace;
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
#if BYTE_ORDER == LITTLE_ENDIAN
/* Convert FROM host byte order */
REVERSE64(context->bitcount[0],context->bitcount[0]);
REVERSE64(context->bitcount[1],context->bitcount[1]);
REVERSE64(context->bitcount[0], context->bitcount[0]);
REVERSE64(context->bitcount[1], context->bitcount[1]);
#endif
if (usedspace > 0) {
if (usedspace > 0)
{
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
if (usedspace <= SHA512_SHORT_BLOCK_LENGTH)
{
/* Set-up for the last transform: */
memset(&context->buffer[usedspace], 0, SHA512_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA512_BLOCK_LENGTH) {
}
else
{
if (usedspace < SHA512_BLOCK_LENGTH)
{
memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
@ -800,7 +875,9 @@ SHA512_Last(SHA512_CTX *context)
/* And set-up for the last transform: */
memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
}
} else {
}
else
{
/* Prepare for final transform: */
memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH);
@ -808,27 +885,30 @@ SHA512_Last(SHA512_CTX *context)
*context->buffer = 0x80;
}
/* Store the length of input data (in bits): */
*(uint64 *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
*(uint64 *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
*(uint64 *) &context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
*(uint64 *) &context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8] = context->bitcount[0];
/* Final transform: */
SHA512_Transform(context, context->buffer);
}
void
SHA512_Final(uint8 digest[], SHA512_CTX *context)
SHA512_Final(uint8 digest[], SHA512_CTX * context)
{
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) {
if (digest != NULL)
{
SHA512_Last(context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE64(context->state[j],context->state[j]);
int j;
for (j = 0; j < 8; j++)
{
REVERSE64(context->state[j], context->state[j]);
}
}
#endif
@ -842,7 +922,7 @@ SHA512_Final(uint8 digest[], SHA512_CTX *context)
/*** SHA-384: *********************************************************/
void
SHA384_Init(SHA384_CTX *context)
SHA384_Init(SHA384_CTX * context)
{
if (context == NULL)
return;
@ -852,25 +932,28 @@ SHA384_Init(SHA384_CTX *context)
}
void
SHA384_Update(SHA384_CTX *context, const uint8 *data, size_t len)
SHA384_Update(SHA384_CTX * context, const uint8 *data, size_t len)
{
SHA512_Update((SHA512_CTX *)context, data, len);
SHA512_Update((SHA512_CTX *) context, data, len);
}
void
SHA384_Final(uint8 digest[], SHA384_CTX *context)
SHA384_Final(uint8 digest[], SHA384_CTX * context)
{
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) {
SHA512_Last((SHA512_CTX *)context);
if (digest != NULL)
{
SHA512_Last((SHA512_CTX *) context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 6; j++) {
REVERSE64(context->state[j],context->state[j]);
int j;
for (j = 0; j < 6; j++)
{
REVERSE64(context->state[j], context->state[j]);
}
}
#endif

View File

@ -1,10 +1,10 @@
/* $PostgreSQL: pgsql/contrib/pgcrypto/sha2.h,v 1.1 2005/07/10 13:46:29 momjian Exp $ */
/* $PostgreSQL: pgsql/contrib/pgcrypto/sha2.h,v 1.2 2005/10/15 02:49:06 momjian Exp $ */
/* $OpenBSD: sha2.h,v 1.2 2004/04/28 23:11:57 millert Exp $ */
/*
* FILE: sha2.h
* AUTHOR: Aaron D. Gifford <me@aarongifford.com>
*
*
* Copyright (c) 2000-2001, Aaron D. Gifford
* All rights reserved.
*
@ -12,18 +12,18 @@
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@ -42,39 +42,41 @@
/*** SHA-256/384/512 Various Length Definitions ***********************/
#define SHA256_BLOCK_LENGTH 64
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA384_BLOCK_LENGTH 128
#define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_BLOCK_LENGTH 128
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
/*** SHA-256/384/512 Context Structures *******************************/
typedef struct _SHA256_CTX {
uint32 state[8];
uint64 bitcount;
uint8 buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX {
uint64 state[8];
uint64 bitcount[2];
uint8 buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;
typedef struct _SHA256_CTX
{
uint32 state[8];
uint64 bitcount;
uint8 buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX
{
uint64 state[8];
uint64 bitcount[2];
uint8 buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;
typedef SHA512_CTX SHA384_CTX;
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX *, const uint8 *, size_t);
void SHA256_Final(uint8[SHA256_DIGEST_LENGTH], SHA256_CTX *);
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX *, const uint8 *, size_t);
void SHA256_Final(uint8[SHA256_DIGEST_LENGTH], SHA256_CTX *);
void SHA384_Init(SHA384_CTX *);
void SHA384_Update(SHA384_CTX *, const uint8 *, size_t);
void SHA384_Final(uint8[SHA384_DIGEST_LENGTH], SHA384_CTX *);
void SHA384_Init(SHA384_CTX *);
void SHA384_Update(SHA384_CTX *, const uint8 *, size_t);
void SHA384_Final(uint8[SHA384_DIGEST_LENGTH], SHA384_CTX *);
void SHA512_Init(SHA512_CTX *);
void SHA512_Update(SHA512_CTX *, const uint8 *, size_t);
void SHA512_Final(uint8[SHA512_DIGEST_LENGTH], SHA512_CTX *);
void SHA512_Init(SHA512_CTX *);
void SHA512_Update(SHA512_CTX *, const uint8 *, size_t);
void SHA512_Final(uint8[SHA512_DIGEST_LENGTH], SHA512_CTX *);
#endif /* _SHA2_H */
#endif /* _SHA2_H */

View File

@ -1,5 +1,5 @@
/*
* $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.19 2005/05/30 23:09:06 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.20 2005/10/15 02:49:06 momjian Exp $
*
* Copyright (c) 2001,2002 Tatsuo Ishii
*
@ -123,8 +123,8 @@ pgstattuple_real(Relation rel, FunctionCallInfo fcinfo)
tupdesc = CreateTupleDescCopy(tupdesc);
/*
* Generate attribute metadata needed later to produce tuples from raw
* C strings
* Generate attribute metadata needed later to produce tuples from raw C
* strings
*/
attinmeta = TupleDescGetAttInMetadata(tupdesc);
@ -197,9 +197,9 @@ pgstattuple_real(Relation rel, FunctionCallInfo fcinfo)
}
/*
* Prepare a values array for constructing the tuple. This should be
* an array of C strings which will be processed later by the
* appropriate "in" functions.
* Prepare a values array for constructing the tuple. This should be an
* array of C strings which will be processed later by the appropriate
* "in" functions.
*/
values = (char **) palloc(NCOLUMNS * sizeof(char *));
for (i = 0; i < NCOLUMNS; i++)

View File

@ -124,8 +124,7 @@ seg_out(SEG * seg)
if (seg->lower == seg->upper && seg->l_ext == seg->u_ext)
{
/*
* indicates that this interval was built by seg_in off a single
* point
* indicates that this interval was built by seg_in off a single point
*/
p += restore(p, seg->lower, seg->l_sigd);
}
@ -349,8 +348,7 @@ gseg_picksplit(GistEntryVector *entryvec,
size_waste = size_union - size_inter;
/*
* are these a more promising split that what we've already
* seen?
* are these a more promising split that what we've already seen?
*/
if (size_waste > waste || firsttime)
{
@ -375,24 +373,24 @@ gseg_picksplit(GistEntryVector *entryvec,
rt_seg_size(datum_r, &size_r);
/*
* Now split up the regions between the two seeds. An important
* property of this split algorithm is that the split vector v has the
* indices of items to be split in order in its left and right
* vectors. We exploit this property by doing a merge in the code
* that actually splits the page.
* Now split up the regions between the two seeds. An important property
* of this split algorithm is that the split vector v has the indices of
* items to be split in order in its left and right vectors. We exploit
* this property by doing a merge in the code that actually splits the
* page.
*
* For efficiency, we also place the new index tuple in this loop. This
* is handled at the very end, when we have placed all the existing
* tuples and i == maxoff + 1.
* For efficiency, we also place the new index tuple in this loop. This is
* handled at the very end, when we have placed all the existing tuples
* and i == maxoff + 1.
*/
maxoff = OffsetNumberNext(maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
/*
* If we've already decided where to place this item, just put it
* on the right list. Otherwise, we need to figure out which page
* needs the least enlargement in order to store the item.
* If we've already decided where to place this item, just put it on
* the right list. Otherwise, we need to figure out which page needs
* the least enlargement in order to store the item.
*/
if (i == seed_1)
@ -742,8 +740,8 @@ seg_cmp(SEG * a, SEG * b)
* a->lower == b->lower, so consider type of boundary.
*
* A '-' lower bound is < any other kind (this could only be relevant if
* -HUGE_VAL is used as a regular data value). A '<' lower bound is <
* any other kind except '-'. A '>' lower bound is > any other kind.
* -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
* other kind except '-'. A '>' lower bound is > any other kind.
*/
if (a->l_ext != b->l_ext)
{
@ -764,8 +762,7 @@ seg_cmp(SEG * a, SEG * b)
/*
* For other boundary types, consider # of significant digits first.
*/
if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include
* (b) */
if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
return -1;
if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
* included in (b) */
@ -800,8 +797,8 @@ seg_cmp(SEG * a, SEG * b)
* a->upper == b->upper, so consider type of boundary.
*
* A '-' upper bound is > any other kind (this could only be relevant if
* HUGE_VAL is used as a regular data value). A '<' upper bound is <
* any other kind. A '>' upper bound is > any other kind except '-'.
* HUGE_VAL is used as a regular data value). A '<' upper bound is < any
* other kind. A '>' upper bound is > any other kind except '-'.
*/
if (a->u_ext != b->u_ext)
{
@ -820,11 +817,10 @@ seg_cmp(SEG * a, SEG * b)
}
/*
* For other boundary types, consider # of significant digits first.
* Note result here is converse of the lower-boundary case.
* For other boundary types, consider # of significant digits first. Note
* result here is converse of the lower-boundary case.
*/
if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include
* (b) */
if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
return 1;
if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
* included in (b) */
@ -908,17 +904,17 @@ restore(char *result, float val, int n)
sign;
/*
* put a cap on the number of siugnificant digits to avoid nonsense in
* the output
* put a cap on the number of siugnificant digits to avoid nonsense in the
* output
*/
n = Min(n, FLT_DIG);
/* remember the sign */
sign = (val < 0 ? 1 : 0);
efmt[5] = '0' + (n - 1) % 10; /* makes %-15.(n-1)e -- this
* format guarantees that the
* exponent is always present */
efmt[5] = '0' + (n - 1) % 10; /* makes %-15.(n-1)e -- this format
* guarantees that the exponent is
* always present */
sprintf(result, efmt, val);
@ -940,8 +936,8 @@ restore(char *result, float val, int n)
if (Abs(exp) <= 4)
{
/*
* remove the decimal point from the mantyssa and write the
* digits to the buf array
* remove the decimal point from the mantyssa and write the digits
* to the buf array
*/
for (p = result + sign, i = 10, dp = 0; *p != 'e'; p++, i++)
{
@ -960,10 +956,9 @@ restore(char *result, float val, int n)
if (dp - 10 + exp >= n)
{
/*
* the decimal point is behind the last significant
* digit; the digits in between must be converted to
* the exponent and the decimal point placed after the
* first digit
* the decimal point is behind the last significant digit;
* the digits in between must be converted to the exponent
* and the decimal point placed after the first digit
*/
exp = dp - 10 + exp - n;
buf[10 + n] = '\0';
@ -978,8 +973,8 @@ restore(char *result, float val, int n)
}
/*
* adjust the exponent by the number of digits after
* the decimal point
* adjust the exponent by the number of digits after the
* decimal point
*/
if (n > 1)
sprintf(&buf[11 + n], "e%d", exp + n - 1);

View File

@ -73,8 +73,8 @@ autoinc(PG_FUNCTION_ARGS)
if (SPI_gettypeid(tupdesc, attnum) != INT4OID)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("attribute \"%s\" of \"%s\" must be type INT4",
args[i], relname)));
errmsg("attribute \"%s\" of \"%s\" must be type INT4",
args[i], relname)));
val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull));

View File

@ -65,7 +65,7 @@ insert_username(PG_FUNCTION_ARGS)
if (attnum < 0)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("\"%s\" has no attribute \"%s\"", relname, args[0])));
errmsg("\"%s\" has no attribute \"%s\"", relname, args[0])));
if (SPI_gettypeid(tupdesc, attnum) != TEXTOID)
ereport(ERROR,
@ -75,7 +75,7 @@ insert_username(PG_FUNCTION_ARGS)
/* create fields containing name */
newval = DirectFunctionCall1(textin,
CStringGetDatum(GetUserNameFromId(GetUserId())));
CStringGetDatum(GetUserNameFromId(GetUserId())));
/* construct new tuple */
rettuple = SPI_modifytuple(rel, rettuple, 1, &attnum, &newval, NULL);

View File

@ -76,9 +76,8 @@ moddatetime(PG_FUNCTION_ARGS)
Int32GetDatum(-1));
/*
* This gets the position in the tuple of the field we want. args[0]
* being the name of the field to update, as passed in from the
* trigger.
* This gets the position in the tuple of the field we want. args[0] being
* the name of the field to update, as passed in from the trigger.
*/
attnum = SPI_fnumber(tupdesc, args[0]);
@ -100,8 +99,8 @@ moddatetime(PG_FUNCTION_ARGS)
if (SPI_gettypeid(tupdesc, attnum) != TIMESTAMPOID)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP",
args[0], relname)));
errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP",
args[0], relname)));
/* 1 is the number of items in the arrays attnum and newdt.
attnum is the positional number of the field to be updated.

View File

@ -114,8 +114,8 @@ check_primary_key(PG_FUNCTION_ARGS)
kvals = (Datum *) palloc(nkeys * sizeof(Datum));
/*
* Construct ident string as TriggerName $ TriggeredRelationId and try
* to find prepared execution plan.
* Construct ident string as TriggerName $ TriggeredRelationId and try to
* find prepared execution plan.
*/
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
plan = find_plan(ident, &PPlans, &nPPlans);
@ -134,16 +134,16 @@ check_primary_key(PG_FUNCTION_ARGS)
if (fnumber < 0)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("there is no attribute \"%s\" in relation \"%s\"",
args[i], SPI_getrelname(rel))));
errmsg("there is no attribute \"%s\" in relation \"%s\"",
args[i], SPI_getrelname(rel))));
/* Well, get binary (in internal format) value of column */
kvals[i] = SPI_getbinval(tuple, tupdesc, fnumber, &isnull);
/*
* If it's NULL then nothing to do! DON'T FORGET call SPI_finish
* ()! DON'T FORGET return tuple! Executor inserts tuple you're
* returning! If you return NULL then nothing will be inserted!
* If it's NULL then nothing to do! DON'T FORGET call SPI_finish ()!
* DON'T FORGET return tuple! Executor inserts tuple you're returning!
* If you return NULL then nothing will be inserted!
*/
if (isnull)
{
@ -164,14 +164,14 @@ check_primary_key(PG_FUNCTION_ARGS)
char sql[8192];
/*
* Construct query: SELECT 1 FROM _referenced_relation_ WHERE
* Pkey1 = $1 [AND Pkey2 = $2 [...]]
* Construct query: SELECT 1 FROM _referenced_relation_ WHERE Pkey1 =
* $1 [AND Pkey2 = $2 [...]]
*/
snprintf(sql, sizeof(sql), "select 1 from %s where ", relname);
for (i = 0; i < nkeys; i++)
{
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s = $%d %s",
args[i + nkeys + 1], i + 1, (i < nkeys - 1) ? "and " : "");
args[i + nkeys + 1], i + 1, (i < nkeys - 1) ? "and " : "");
}
/* Prepare plan for query */
@ -181,9 +181,8 @@ check_primary_key(PG_FUNCTION_ARGS)
elog(ERROR, "check_primary_key: SPI_prepare returned %d", SPI_result);
/*
* Remember that SPI_prepare places plan in current memory context
* - so, we have to save plan in Top memory context for latter
* use.
* Remember that SPI_prepare places plan in current memory context -
* so, we have to save plan in Top memory context for latter use.
*/
pplan = SPI_saveplan(pplan);
if (pplan == NULL)
@ -252,8 +251,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
EPlan *plan; /* prepared plan(s) */
Oid *argtypes = NULL; /* key types to prepare execution plan */
bool isnull; /* to know is some column NULL or not */
bool isequal = true; /* are keys in both tuples equal (in
* UPDATE) */
bool isequal = true; /* are keys in both tuples equal (in UPDATE) */
char ident[2 * NAMEDATALEN]; /* to identify myself */
int is_update = 0;
int ret;
@ -287,9 +285,8 @@ check_foreign_key(PG_FUNCTION_ARGS)
trigtuple = trigdata->tg_trigtuple;
/*
* But if this is UPDATE then we have to return tg_newtuple. Also, if
* key in tg_newtuple is the same as in tg_trigtuple then nothing to
* do.
* But if this is UPDATE then we have to return tg_newtuple. Also, if key
* in tg_newtuple is the same as in tg_trigtuple then nothing to do.
*/
is_update = 0;
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
@ -337,8 +334,8 @@ check_foreign_key(PG_FUNCTION_ARGS)
kvals = (Datum *) palloc(nkeys * sizeof(Datum));
/*
* Construct ident string as TriggerName $ TriggeredRelationId and try
* to find prepared execution plan(s).
* Construct ident string as TriggerName $ TriggeredRelationId and try to
* find prepared execution plan(s).
*/
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
plan = find_plan(ident, &FPlans, &nFPlans);
@ -365,16 +362,16 @@ check_foreign_key(PG_FUNCTION_ARGS)
if (fnumber < 0)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("there is no attribute \"%s\" in relation \"%s\"",
args[i], SPI_getrelname(rel))));
errmsg("there is no attribute \"%s\" in relation \"%s\"",
args[i], SPI_getrelname(rel))));
/* Well, get binary (in internal format) value of column */
kvals[i] = SPI_getbinval(trigtuple, tupdesc, fnumber, &isnull);
/*
* If it's NULL then nothing to do! DON'T FORGET call SPI_finish
* ()! DON'T FORGET return tuple! Executor inserts tuple you're
* returning! If you return NULL then nothing will be inserted!
* If it's NULL then nothing to do! DON'T FORGET call SPI_finish ()!
* DON'T FORGET return tuple! Executor inserts tuple you're returning!
* If you return NULL then nothing will be inserted!
*/
if (isnull)
{
@ -383,9 +380,9 @@ check_foreign_key(PG_FUNCTION_ARGS)
}
/*
* If UPDATE then get column value from new tuple being inserted
* and compare is this the same as old one. For the moment we use
* string presentation of values...
* If UPDATE then get column value from new tuple being inserted and
* compare is this the same as old one. For the moment we use string
* presentation of values...
*/
if (newtuple != NULL)
{
@ -473,7 +470,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
type = SPI_gettype(tupdesc, fn);
if ((strcmp(type, "text") && strcmp(type, "varchar") &&
strcmp(type, "char") && strcmp(type, "bpchar") &&
strcmp(type, "char") && strcmp(type, "bpchar") &&
strcmp(type, "date") && strcmp(type, "timestamp")) == 0)
is_char_type = 1;
#ifdef DEBUG_QUERY
@ -482,8 +479,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
#endif
/*
* is_char_type =1 i set ' ' for define a new
* value
* is_char_type =1 i set ' ' for define a new value
*/
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql),
" %s = %s%s%s %s ",
@ -503,8 +499,8 @@ check_foreign_key(PG_FUNCTION_ARGS)
/*
* For 'S'etnull action we construct UPDATE query - UPDATE
* _referencing_relation_ SET Fkey1 null [, Fkey2 null [...]]
* WHERE Fkey1 = $1 [AND Fkey2 = $2 [...]] - to set key
* columns in all referencing tuples to NULL.
* WHERE Fkey1 = $1 [AND Fkey2 = $2 [...]] - to set key columns in
* all referencing tuples to NULL.
*/
else if (action == 's')
{
@ -532,9 +528,9 @@ check_foreign_key(PG_FUNCTION_ARGS)
elog(ERROR, "check_foreign_key: SPI_prepare returned %d", SPI_result);
/*
* Remember that SPI_prepare places plan in current memory
* context - so, we have to save plan in Top memory context
* for latter use.
* Remember that SPI_prepare places plan in current memory context
* - so, we have to save plan in Top memory context for latter
* use.
*/
pplan = SPI_saveplan(pplan);
if (pplan == NULL)
@ -566,8 +562,8 @@ check_foreign_key(PG_FUNCTION_ARGS)
for (r = 0; r < nrefs; r++)
{
/*
* For 'R'estrict we may to execute plan for one tuple only, for
* other actions - for all tuples.
* For 'R'estrict we may to execute plan for one tuple only, for other
* actions - for all tuples.
*/
int tcount = (action == 'r') ? 1 : 0;

View File

@ -245,8 +245,8 @@ timetravel(PG_FUNCTION_ARGS)
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[a_time_off]);
/*
* If DELETE/UPDATE of tuple with stop_date neq INFINITY then say
* upper Executor to skip operation for this tuple
* If DELETE/UPDATE of tuple with stop_date neq INFINITY then say upper
* Executor to skip operation for this tuple
*/
if (newtuple != NULL)
{ /* UPDATE */
@ -263,8 +263,7 @@ timetravel(PG_FUNCTION_ARGS)
relname, args[a_time_on], args[a_time_off]);
}
if (oldtimeoff != NOEND_ABSTIME)
{ /* current record is a deleted/updated
* record */
{ /* current record is a deleted/updated record */
pfree(relname);
return PointerGetDatum(NULL);
}
@ -285,8 +284,7 @@ timetravel(PG_FUNCTION_ARGS)
}
/* change date column(s) */
cvals[attnum[a_time_off] - 1] = newtimeoff; /* stop_date eq current
* date */
cvals[attnum[a_time_off] - 1] = newtimeoff; /* stop_date eq current date */
cnulls[attnum[a_time_off] - 1] = ' ';
if (!newtuple)
@ -299,8 +297,8 @@ timetravel(PG_FUNCTION_ARGS)
}
/*
* Construct ident string as TriggerName $ TriggeredRelationId and try
* to find prepared execution plan.
* Construct ident string as TriggerName $ TriggeredRelationId and try to
* find prepared execution plan.
*/
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
plan = find_plan(ident, &Plans, &nPlans);
@ -339,9 +337,8 @@ timetravel(PG_FUNCTION_ARGS)
elog(ERROR, "timetravel (%s): SPI_prepare returned %d", relname, SPI_result);
/*
* Remember that SPI_prepare places plan in current memory context
* - so, we have to save plan in Top memory context for latter
* use.
* Remember that SPI_prepare places plan in current memory context -
* so, we have to save plan in Top memory context for latter use.
*/
pplan = SPI_saveplan(pplan);
if (pplan == NULL)
@ -398,8 +395,8 @@ timetravel(PG_FUNCTION_ARGS)
rettuple = SPI_modifytuple(rel, newtuple, chnattrs, chattrs, newvals, newnulls);
/*
* SPI_copytuple allocates tmptuple in upper executor context -
* have to free allocation using SPI_pfree
* SPI_copytuple allocates tmptuple in upper executor context - have
* to free allocation using SPI_pfree
*/
/* SPI_pfree(tmptuple); */
}

View File

@ -184,8 +184,7 @@ normal_rand(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function
* calls
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@ -196,10 +195,10 @@ normal_rand(PG_FUNCTION_ARGS)
fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
/*
* Use fctx to keep track of upper and lower bounds from call to
* call. It will also be used to carry over the spare value we get
* from the Box-Muller algorithm so that we only actually
* calculate a new value every other call.
* Use fctx to keep track of upper and lower bounds from call to call.
* It will also be used to carry over the spare value we get from the
* Box-Muller algorithm so that we only actually calculate a new value
* every other call.
*/
fctx->mean = PG_GETARG_FLOAT8(1);
fctx->stddev = PG_GETARG_FLOAT8(2);
@ -254,7 +253,7 @@ normal_rand(PG_FUNCTION_ARGS)
SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
}
else
/* do when there is no more left */
/* do when there is no more left */
SRF_RETURN_DONE(funcctx);
}
@ -331,8 +330,8 @@ get_normal_pair(float8 *x1, float8 *x2)
* 1. SQL result must be ordered by 1,2.
* 2. The number of values columns depends on the tuple description
* of the function's declared return type. The return type's columns
* must match the datatypes of the SQL query's result. The datatype
* of the category column can be anything, however.
* must match the datatypes of the SQL query's result. The datatype
* of the category column can be anything, however.
* 3. Missing values (i.e. not enough adjacent rows of same rowid to
* fill the number of result values columns) are filled in with nulls.
* 4. Extra values (i.e. too many adjacent rows of same rowid to fill
@ -368,8 +367,7 @@ crosstab(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function
* calls
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@ -394,7 +392,7 @@ crosstab(PG_FUNCTION_ARGS)
* 1. rowname
* the label or identifier for each row in the final result
* 2. category
* the label or identifier for each column in the final result
* the label or identifier for each column in the final result
* 3. values
* the value for each column in the final result
*----------
@ -404,7 +402,7 @@ crosstab(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid source data SQL statement"),
errdetail("The provided SQL must return 3 "
"columns: rowid, category, and values.")));
"columns: rowid, category, and values.")));
}
else
{
@ -439,8 +437,8 @@ crosstab(PG_FUNCTION_ARGS)
tupdesc = CreateTupleDescCopy(tupdesc);
/*
* Check that return tupdesc is compatible with the data we got
* from SPI, at least based on number and type of attributes
* Check that return tupdesc is compatible with the data we got from
* SPI, at least based on number and type of attributes
*/
if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
ereport(ERROR,
@ -449,8 +447,8 @@ crosstab(PG_FUNCTION_ARGS)
"incompatible")));
/*
* Generate attribute metadata needed later to produce tuples from
* raw C strings
* Generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
@ -530,11 +528,10 @@ crosstab(PG_FUNCTION_ARGS)
rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
/*
* If this is the first pass through the values for this
* rowid set it, otherwise make sure it hasn't changed on
* us. Also check to see if the rowid is the same as that
* of the last tuple sent -- if so, skip this tuple
* entirely
* If this is the first pass through the values for this rowid
* set it, otherwise make sure it hasn't changed on us. Also
* check to see if the rowid is the same as that of the last
* tuple sent -- if so, skip this tuple entirely
*/
if (i == 0)
values[0] = pstrdup(rowid);
@ -550,16 +547,15 @@ crosstab(PG_FUNCTION_ARGS)
* Get the next category item value, which is alway
* attribute number three.
*
* Be careful to sssign the value to the array index
* based on which category we are presently
* processing.
* Be careful to sssign the value to the array index based on
* which category we are presently processing.
*/
values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
/*
* increment the counter since we consume a row for
* each category, but not for last pass because the
* API will do that for us
* increment the counter since we consume a row for each
* category, but not for last pass because the API will do
* that for us
*/
if (i < (num_categories - 1))
call_cntr = ++funcctx->call_cntr;
@ -567,9 +563,9 @@ crosstab(PG_FUNCTION_ARGS)
else
{
/*
* We'll fill in NULLs for the missing values, but we
* need to decrement the counter since this sql result
* row doesn't belong to the current output tuple.
* We'll fill in NULLs for the missing values, but we need
* to decrement the counter since this sql result row
* doesn't belong to the current output tuple.
*/
call_cntr = --funcctx->call_cntr;
break;
@ -584,8 +580,8 @@ crosstab(PG_FUNCTION_ARGS)
if (values[0] != NULL)
{
/*
* switch to memory context appropriate for multiple
* function calls
* switch to memory context appropriate for multiple function
* calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@ -612,8 +608,8 @@ crosstab(PG_FUNCTION_ARGS)
else
{
/*
* Skipping this tuple entirely, but we need to advance
* the counter like the API would if we had returned one.
* Skipping this tuple entirely, but we need to advance the
* counter like the API would if we had returned one.
*/
call_cntr = ++funcctx->call_cntr;
@ -631,7 +627,7 @@ crosstab(PG_FUNCTION_ARGS)
}
}
else
/* do when there is no more left */
/* do when there is no more left */
{
/* release SPI related resources */
SPI_finish();
@ -730,10 +726,10 @@ crosstab_hash(PG_FUNCTION_ARGS)
/*
* SFRM_Materialize mode expects us to return a NULL Datum. The actual
* tuples are in our tuplestore and passed back through
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description
* that we actually used to build our tuples with, so the caller can
* verify we did what it was expecting.
* tuples are in our tuplestore and passed back through rsinfo->setResult.
* rsinfo->setDesc is set to the tuple description that we actually used
* to build our tuples with, so the caller can verify we did what it was
* expecting.
*/
rsinfo->setDesc = tupdesc;
MemoryContextSwitchTo(oldcontext);
@ -758,8 +754,8 @@ load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
ctl.entrysize = sizeof(crosstab_HashEnt);
/*
* use INIT_CATS, defined above as a guess of how many hash table
* entries to create, initially
* use INIT_CATS, defined above as a guess of how many hash table entries
* to create, initially
*/
crosstab_HashTable = hash_create("crosstab hash", INIT_CATS, &ctl, HASH_ELEM);
@ -780,8 +776,8 @@ load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
int i;
/*
* The provided categories SQL query must always return one
* column: category - the label or identifier for each column
* The provided categories SQL query must always return one column:
* category - the label or identifier for each column
*/
if (spi_tupdesc->natts != 1)
ereport(ERROR,
@ -872,26 +868,24 @@ get_crosstab_tuplestore(char *sql,
}
/*
* The provided SQL query must always return at least three
* columns:
* The provided SQL query must always return at least three columns:
*
* 1. rowname the label for each row - column 1 in the final result
* 2. category the label for each value-column in the final
* result 3. value the values used to populate the
* value-columns
* 1. rowname the label for each row - column 1 in the final result 2.
* category the label for each value-column in the final result 3.
* value the values used to populate the value-columns
*
* If there are more than three columns, the last two are taken as
* "category" and "values". The first column is taken as
* "rowname". Additional columns (2 thru N-2) are assumed the same
* for the same "rowname", and are copied into the result tuple
* from the first time we encounter a particular rowname.
* "category" and "values". The first column is taken as "rowname".
* Additional columns (2 thru N-2) are assumed the same for the same
* "rowname", and are copied into the result tuple from the first time
* we encounter a particular rowname.
*/
if (ncols < 3)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid source data SQL statement"),
errdetail("The provided SQL must return 3 " \
" columns; rowid, category, and values.")));
" columns; rowid, category, and values.")));
result_ncols = (ncols - 2) + num_categories;
@ -902,7 +896,7 @@ get_crosstab_tuplestore(char *sql,
errmsg("invalid return type"),
errdetail("query-specified return " \
"tuple has %d columns but crosstab " \
"returns %d", tupdesc->natts, result_ncols)));
"returns %d", tupdesc->natts, result_ncols)));
/* allocate space */
values = (char **) palloc(result_ncols * sizeof(char *));
@ -933,14 +927,13 @@ get_crosstab_tuplestore(char *sql,
if ((lastrowid == NULL) || (strcmp(rowid, lastrowid) != 0))
{
/*
* a new row means we need to flush the old one first,
* unless we're on the very first row
* a new row means we need to flush the old one first, unless
* we're on the very first row
*/
if (lastrowid != NULL)
{
/*
* switch to appropriate context while storing the
* tuple
* switch to appropriate context while storing the tuple
*/
SPIcontext = MemoryContextSwitchTo(per_query_ctx);
@ -1103,10 +1096,10 @@ connectby_text(PG_FUNCTION_ARGS)
/*
* SFRM_Materialize mode expects us to return a NULL Datum. The actual
* tuples are in our tuplestore and passed back through
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description
* that we actually used to build our tuples with, so the caller can
* verify we did what it was expecting.
* tuples are in our tuplestore and passed back through rsinfo->setResult.
* rsinfo->setDesc is set to the tuple description that we actually used
* to build our tuples with, so the caller can verify we did what it was
* expecting.
*/
return (Datum) 0;
}
@ -1182,10 +1175,10 @@ connectby_text_serial(PG_FUNCTION_ARGS)
/*
* SFRM_Materialize mode expects us to return a NULL Datum. The actual
* tuples are in our tuplestore and passed back through
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description
* that we actually used to build our tuples with, so the caller can
* verify we did what it was expecting.
* tuples are in our tuplestore and passed back through rsinfo->setResult.
* rsinfo->setDesc is set to the tuple description that we actually used
* to build our tuples with, so the caller can verify we did what it was
* expecting.
*/
return (Datum) 0;
}
@ -1382,16 +1375,16 @@ build_tuplestore_recursively(char *key_fld,
{
/*
* Check that return tupdesc is compatible with the one we got
* from the query, but only at level 0 -- no need to check
* more than once
* from the query, but only at level 0 -- no need to check more
* than once
*/
if (!compatConnectbyTupleDescs(tupdesc, spi_tupdesc))
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("invalid return type"),
errdetail("Return and SQL tuple descriptions are " \
"incompatible.")));
errdetail("Return and SQL tuple descriptions are " \
"incompatible.")));
}
for (i = 0; i < proc; i++)
@ -1576,7 +1569,7 @@ compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("invalid return type"),
errdetail("SQL parent key field datatype does " \
"not match return parent key field datatype.")));
"not match return parent key field datatype.")));
/* OK, the two tupdescs are compatible for our purposes */
return true;
@ -1605,9 +1598,9 @@ compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
"return rowid datatype.")));
/*
* - attribute [1] of the sql tuple is the category; no need to check
* it - attribute [2] of the sql tuple should match attributes [1] to
* [natts] of the return tuple
* - attribute [1] of the sql tuple is the category; no need to check it -
* attribute [2] of the sql tuple should match attributes [1] to [natts]
* of the return tuple
*/
sql_attr = sql_tupdesc->attrs[2];
for (i = 1; i < ret_tupdesc->natts; i++)

View File

@ -91,9 +91,9 @@ reset_dict(void)
static int
comparedict(const void *a, const void *b)
{
if ( ((DictInfo *) a)->dict_id == ((DictInfo *) b)->dict_id )
if (((DictInfo *) a)->dict_id == ((DictInfo *) b)->dict_id)
return 0;
return ( ((DictInfo *) a)->dict_id < ((DictInfo *) b)->dict_id ) ? -1 : 1;
return (((DictInfo *) a)->dict_id < ((DictInfo *) b)->dict_id) ? -1 : 1;
}
DictInfo *
@ -184,8 +184,8 @@ lexize(PG_FUNCTION_ARGS)
{
text *in = PG_GETARG_TEXT_P(1);
DictInfo *dict;
TSLexeme *res,
*ptr;
TSLexeme *res,
*ptr;
Datum *da;
ArrayType *a;
@ -193,11 +193,11 @@ lexize(PG_FUNCTION_ARGS)
dict = finddict(PG_GETARG_OID(0));
ptr = res = (TSLexeme *) DatumGetPointer(
FunctionCall3(&(dict->lexize_info),
PointerGetDatum(dict->dictionary),
PointerGetDatum(VARDATA(in)),
Int32GetDatum(VARSIZE(in) - VARHDRSZ)
)
FunctionCall3(&(dict->lexize_info),
PointerGetDatum(dict->dictionary),
PointerGetDatum(VARDATA(in)),
Int32GetDatum(VARSIZE(in) - VARHDRSZ)
)
);
PG_FREE_IF_COPY(in, 1);
if (!res)

View File

@ -39,26 +39,22 @@ typedef struct
void parse_cfgdict(text *in, Map ** m);
/* return struct for any lexize function */
typedef struct {
/* number of variant of split word , for example
Word 'fotballklubber' (norwegian) has two varian to split:
( fotball, klubb ) and ( fot, ball, klubb ). So, dictionary
should return:
nvariant lexeme
1 fotball
1 klubb
2 fot
2 ball
2 klubb
*/
uint16 nvariant;
typedef struct
{
/*
* number of variant of split word , for example Word 'fotballklubber'
* (norwegian) has two varian to split: ( fotball, klubb ) and ( fot,
* ball, klubb ). So, dictionary should return: nvariant lexeme 1
* fotball 1 klubb 2 fot 2 ball 2 klubb
*
*/
uint16 nvariant;
/* currently unused */
uint16 flags;
uint16 flags;
/* C-string */
char *lexeme;
} TSLexeme;
char *lexeme;
} TSLexeme;
#endif

View File

@ -52,7 +52,7 @@ dex_lexize(PG_FUNCTION_ARGS)
char *txt = pnstrdup(in, PG_GETARG_INT32(2));
TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
memset(res,0,sizeof(TSLexeme) * 2);
memset(res, 0, sizeof(TSLexeme) * 2);
if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
{

View File

@ -66,8 +66,8 @@ spell_init(PG_FUNCTION_ARGS)
{
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("dictionary already loaded")));
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("dictionary already loaded")));
}
if (NIImportDictionary(&(d->obj), pcfg->value))
{
@ -85,8 +85,8 @@ spell_init(PG_FUNCTION_ARGS)
{
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("affixes already loaded")));
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("affixes already loaded")));
}
if (NIImportAffixes(&(d->obj), pcfg->value))
{
@ -106,8 +106,8 @@ spell_init(PG_FUNCTION_ARGS)
{
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("stop words already loaded")));
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("stop words already loaded")));
}
readstoplist(tmp, &(d->stoplist));
sortstoplist(&(d->stoplist));
@ -157,9 +157,9 @@ spell_lexize(PG_FUNCTION_ARGS)
DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0);
char *in = (char *) PG_GETARG_POINTER(1);
char *txt;
TSLexeme *res;
TSLexeme *ptr,
*cptr;
TSLexeme *res;
TSLexeme *ptr,
*cptr;
if (!PG_GETARG_INT32(2))
PG_RETURN_POINTER(NULL);

View File

@ -102,7 +102,7 @@ snb_lexize(PG_FUNCTION_ARGS)
DictSnowball *d = (DictSnowball *) PG_GETARG_POINTER(0);
char *in = (char *) PG_GETARG_POINTER(1);
char *txt = pnstrdup(in, PG_GETARG_INT32(2));
TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
memset(res, 0, sizeof(TSLexeme) * 2);
if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))

View File

@ -159,7 +159,7 @@ syn_lexize(PG_FUNCTION_ARGS)
char *in = (char *) PG_GETARG_POINTER(1);
Syn key,
*found;
TSLexeme *res = NULL;
TSLexeme *res = NULL;
if (!PG_GETARG_INT32(2))
PG_RETURN_POINTER(NULL);
@ -174,7 +174,7 @@ syn_lexize(PG_FUNCTION_ARGS)
PG_RETURN_POINTER(NULL);
res = palloc(sizeof(TSLexeme) * 2);
memset(res,0,sizeof(TSLexeme) * 2);
memset(res, 0, sizeof(TSLexeme) * 2);
res[0].lexeme = pstrdup(found->out);
PG_RETURN_POINTER(res);

View File

@ -65,30 +65,32 @@ gtsvector_in(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(0);
}
#define SINGOUTSTR "%d true bits, %d false bits"
#define ARROUTSTR "%d unique words"
#define SINGOUTSTR "%d true bits, %d false bits"
#define ARROUTSTR "%d unique words"
#define EXTRALEN ( 2*13 )
static int outbuf_maxlen = 0;
static int outbuf_maxlen = 0;
Datum
gtsvector_out(PG_FUNCTION_ARGS)
{
GISTTYPE *key = (GISTTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_POINTER(0)));
char *outbuf;
char *outbuf;
if ( outbuf_maxlen==0 )
outbuf_maxlen = 2*EXTRALEN + Max( strlen(SINGOUTSTR), strlen(ARROUTSTR) ) + 1;
outbuf = palloc( outbuf_maxlen );
if (outbuf_maxlen == 0)
outbuf_maxlen = 2 * EXTRALEN + Max(strlen(SINGOUTSTR), strlen(ARROUTSTR)) + 1;
outbuf = palloc(outbuf_maxlen);
if ( ISARRKEY(key) )
sprintf( outbuf, ARROUTSTR, (int) ARRNELEM(key) );
else {
int cnttrue = ( ISALLTRUE(key) ) ? SIGLENBIT : sizebitvec(GETSIGN(key));
sprintf( outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT - cnttrue );
}
if (ISARRKEY(key))
sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
else
{
int cnttrue = (ISALLTRUE(key)) ? SIGLENBIT : sizebitvec(GETSIGN(key));
PG_FREE_IF_COPY(key,0);
sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT - cnttrue);
}
PG_FREE_IF_COPY(key, 0);
PG_RETURN_POINTER(outbuf);
}
@ -165,8 +167,8 @@ gtsvector_compress(PG_FUNCTION_ARGS)
if (len != val->size)
{
/*
* there is a collision of hash-function; len is always less
* than val->size
* there is a collision of hash-function; len is always less than
* val->size
*/
len = CALCGTSIZE(ARRKEY, len);
res = (GISTTYPE *) repalloc((void *) res, len);
@ -280,7 +282,7 @@ gtsvector_consistent(PG_FUNCTION_ARGS)
{
QUERYTYPE *query = (QUERYTYPE *) PG_GETARG_POINTER(1);
GISTTYPE *key = (GISTTYPE *) DatumGetPointer(
((GISTENTRY *) PG_GETARG_POINTER(0))->key
((GISTENTRY *) PG_GETARG_POINTER(0))->key
);
if (!query->size)

View File

@ -10,8 +10,8 @@
*/
#define BITBYTE 8
#define SIGLENINT 63 /* >121 => key will toast, so it will not
* work !!! */
#define SIGLENINT 63 /* >121 => key will toast, so it will not work
* !!! */
#define SIGLEN ( sizeof(int4) * SIGLENINT )
#define SIGLENBIT (SIGLEN * BITBYTE)
@ -55,7 +55,7 @@ typedef struct
#define ISSIGNKEY(x) ( ((GISTTYPE*)(x))->flag & SIGNKEY )
#define ISALLTRUE(x) ( ((GISTTYPE*)(x))->flag & ALLISTRUE )
#define GTHDRSIZE ( sizeof(int4) * 2 )
#define GTHDRSIZE ( sizeof(int4) * 2 )
#define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int4)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
#define GETSIGN(x) ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )

View File

@ -153,7 +153,7 @@ NIImportDictionary(IspellDict * Conf, const char *filename)
return (1);
while (fgets(str, sizeof(str), dict))
{
char *s;
char *s;
const char *flag;
flag = NULL;
@ -266,7 +266,7 @@ NIAddAffix(IspellDict * Conf, int flag, char flagflags, const char *mask, const
{
Conf->Affix[Conf->naffixes].issimple = 0;
Conf->Affix[Conf->naffixes].isregis = 0;
Conf->Affix[Conf->naffixes].mask = (char*)malloc( strlen(mask) + 2 );
Conf->Affix[Conf->naffixes].mask = (char *) malloc(strlen(mask) + 2);
if (type == FF_SUFFIX)
sprintf(Conf->Affix[Conf->naffixes].mask, "%s$", mask);
else
@ -489,8 +489,8 @@ mkSPNode(IspellDict * Conf, int low, int high, int level)
if (data->isword && data->affix != Conf->Spell[i].p.d.affix)
{
/*
* fprintf(stderr,"Word already exists: %s (affixes:
* '%s' and '%s')\n", Conf->Spell[i].word,
* fprintf(stderr,"Word already exists: %s (affixes: '%s'
* and '%s')\n", Conf->Spell[i].word,
* Conf->AffixData[data->affix],
* Conf->AffixData[Conf->Spell[i].p.d.affix] );
*/
@ -806,7 +806,8 @@ CheckAffix(const char *word, size_t len, AFFIX * Affix, char flagflags, char *ne
pfree(mask);
if (err)
{
char regerrstr[ERRSTRSIZE];
char regerrstr[ERRSTRSIZE];
pg_regerror(err, &(Affix->reg.regex), regerrstr, ERRSTRSIZE);
elog(ERROR, "Regex error in '%s': %s", Affix->mask, regerrstr);
}
@ -1037,8 +1038,7 @@ SplitToVariants(IspellDict * Conf, SPNode * snode, SplitVar * orig, char *word,
while (level > startpos && (lenaff = CheckCompoundAffixes(&caff, word + level, wordlen - level)) > 0)
{
/*
* there is one of compound suffixes, so check word for
* existings
* there is one of compound suffixes, so check word for existings
*/
char buf[MAXNORMLEN];
char **subres;
@ -1128,20 +1128,24 @@ TSLexeme *
NINormalizeWord(IspellDict * Conf, char *word)
{
char **res = NormalizeSubWord(Conf, word, 0);
TSLexeme *lcur=NULL, *lres=NULL;
uint16 NVariant=1;
TSLexeme *lcur = NULL,
*lres = NULL;
uint16 NVariant = 1;
if (res) {
char **ptr = res;
lcur = lres = (TSLexeme*)palloc( MAX_NORM * sizeof(TSLexeme) );
while(*ptr) {
lcur->lexeme=*ptr;
lcur->flags=0;
if (res)
{
char **ptr = res;
lcur = lres = (TSLexeme *) palloc(MAX_NORM * sizeof(TSLexeme));
while (*ptr)
{
lcur->lexeme = *ptr;
lcur->flags = 0;
lcur->nvariant = NVariant++;
lcur++;
ptr++;
}
lcur->lexeme=NULL;
lcur->lexeme = NULL;
pfree(res);
}
@ -1162,29 +1166,31 @@ NINormalizeWord(IspellDict * Conf, char *word)
{
char **subptr = subres;
if ( !lcur )
lcur = lres = (TSLexeme*)palloc( MAX_NORM * sizeof(TSLexeme) );
while(*subptr) {
for(i=0;i<var->nstem-1;i++) {
lcur->lexeme=(subptr==subres) ? var->stem[ i ] : pstrdup(var->stem[ i ]);
lcur->flags=0;
if (!lcur)
lcur = lres = (TSLexeme *) palloc(MAX_NORM * sizeof(TSLexeme));
while (*subptr)
{
for (i = 0; i < var->nstem - 1; i++)
{
lcur->lexeme = (subptr == subres) ? var->stem[i] : pstrdup(var->stem[i]);
lcur->flags = 0;
lcur->nvariant = NVariant;
lcur++;
}
lcur->lexeme=*subptr;
lcur->flags=0;
lcur->lexeme = *subptr;
lcur->flags = 0;
lcur->nvariant = NVariant;
lcur++;
subptr++;
NVariant++;
}
}
lcur->lexeme=NULL;
lcur->lexeme = NULL;
pfree(subres);
var->stem[0] = NULL;
pfree( var->stem[ var->nstem-1 ] );
pfree(var->stem[var->nstem - 1]);
}
}

View File

@ -7,7 +7,7 @@
#include "regis.h"
#include "dict.h"
struct SPNode;
@ -55,9 +55,9 @@ typedef struct aff_struct
isregis:1,
unused:1,
replen:16;
char *mask;
char *find;
char *repl;
char *mask;
char *find;
char *repl;
union
{
regex_t regex;
@ -117,7 +117,7 @@ typedef struct
} IspellDict;
TSLexeme *NINormalizeWord(IspellDict * Conf, char *word);
TSLexeme *NINormalizeWord(IspellDict * Conf, char *word);
int NIImportAffixes(IspellDict * Conf, const char *filename);
int NIImportDictionary(IspellDict * Conf, const char *filename);

View File

@ -73,8 +73,8 @@ parse_cfgdict(text *in, Map ** m)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("syntax error"),
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
}
else if (state == CS_INKEY)
{
@ -92,8 +92,8 @@ parse_cfgdict(text *in, Map ** m)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("syntax error"),
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
}
else if (state == CS_WAITEQ)
{
@ -103,8 +103,8 @@ parse_cfgdict(text *in, Map ** m)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("syntax error"),
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
}
else if (state == CS_WAITVALUE)
{
@ -149,8 +149,8 @@ parse_cfgdict(text *in, Map ** m)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("syntax error"),
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
errdetail("Syntax error in position %d near \"%c\"",
(int) (ptr - VARDATA(in)), *ptr)));
}
else if (state == CS_INESC)
state = CS_INVALUE;

View File

@ -265,7 +265,11 @@ pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval, int2 we
{
int4 count = 0;
PRSTEXT prs;
uint32 variant, pos, cntvar=0, cntpos=0, cnt=0;
uint32 variant,
pos,
cntvar = 0,
cntpos = 0,
cnt = 0;
prs.lenwords = 32;
prs.curwords = 0;
@ -274,39 +278,44 @@ pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval, int2 we
parsetext_v2(findcfg(state->cfg_id), &prs, strval, lenval);
if ( prs.curwords>0 ) {
if (prs.curwords > 0)
{
while (count < prs.curwords) {
while (count < prs.curwords)
{
pos = prs.words[count].pos.pos;
cntvar=0;
while(count < prs.curwords && pos==prs.words[count].pos.pos) {
cntvar = 0;
while (count < prs.curwords && pos == prs.words[count].pos.pos)
{
variant = prs.words[count].nvariant;
cnt=0;
while(count < prs.curwords && pos==prs.words[count].pos.pos && variant==prs.words[count].nvariant) {
cnt = 0;
while (count < prs.curwords && pos == prs.words[count].pos.pos && variant == prs.words[count].nvariant)
{
pushval_asis(state, VAL, prs.words[count].word, prs.words[count].len, weight);
pfree(prs.words[count].word);
if ( cnt )
if (cnt)
pushquery(state, OPR, (int4) '&', 0, 0, 0);
cnt++;
count++;
}
if ( cntvar )
if (cntvar)
pushquery(state, OPR, (int4) '|', 0, 0, 0);
cntvar++;
}
if (cntpos)
if (cntpos)
pushquery(state, OPR, (int4) '&', 0, 0, 0);
cntpos++;
}
pfree(prs.words);
} else
}
else
pushval_asis(state, VALSTOP, NULL, 0, 0);
}

View File

@ -45,6 +45,7 @@ static float weights[] = {0.1, 0.2, 0.4, 1.0};
static float calc_rank_or(float *w, tsvector * t, QUERYTYPE * q);
static float calc_rank_and(float *w, tsvector * t, QUERYTYPE * q);
/*
* Returns a weight of a word collocation
*/
@ -115,44 +116,52 @@ find_wordentry(tsvector * t, QUERYTYPE * q, ITEM * item)
}
static char * SortAndUniqOperand=NULL;
static char *SortAndUniqOperand = NULL;
static int
compareITEM( const void * a, const void * b ) {
if ( (*(ITEM**)a)->length == (*(ITEM**)b)->length )
return strncmp( SortAndUniqOperand + (*(ITEM**)a)->distance,
SortAndUniqOperand + (*(ITEM**)b)->distance,
(*(ITEM**)b)->length );
compareITEM(const void *a, const void *b)
{
if ((*(ITEM **) a)->length == (*(ITEM **) b)->length)
return strncmp(SortAndUniqOperand + (*(ITEM **) a)->distance,
SortAndUniqOperand + (*(ITEM **) b)->distance,
(*(ITEM **) b)->length);
return ((*(ITEM**)a)->length > (*(ITEM**)b)->length) ? 1 : -1;
return ((*(ITEM **) a)->length > (*(ITEM **) b)->length) ? 1 : -1;
}
static ITEM**
SortAndUniqItems( char *operand, ITEM *item, int *size ) {
ITEM **res, **ptr, **prevptr;
ptr = res = (ITEM**) palloc( sizeof(ITEM*) * *size );
static ITEM **
SortAndUniqItems(char *operand, ITEM * item, int *size)
{
ITEM **res,
**ptr,
**prevptr;
while( (*size)-- ) {
if ( item->type == VAL ) {
ptr = res = (ITEM **) palloc(sizeof(ITEM *) * *size);
while ((*size)--)
{
if (item->type == VAL)
{
*ptr = item;
ptr++;
}
}
item++;
}
*size = ptr-res;
if ( *size < 2 )
*size = ptr - res;
if (*size < 2)
return res;
SortAndUniqOperand=operand;
qsort( res, *size, sizeof(ITEM**), compareITEM );
SortAndUniqOperand = operand;
qsort(res, *size, sizeof(ITEM **), compareITEM);
ptr = res + 1;
prevptr = res;
while( ptr - res < *size ) {
if ( compareITEM( (void*) ptr, (void*) prevptr ) != 0 ) {
while (ptr - res < *size)
{
if (compareITEM((void *) ptr, (void *) prevptr) != 0)
{
prevptr++;
*prevptr = *ptr;
}
@ -183,18 +192,19 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
lenct,
dist;
float res = -1.0;
ITEM **item;
int size = q->size;
ITEM **item;
int size = q->size;
item = SortAndUniqItems( GETOPERAND(q), GETQUERY(q), &size);
if ( size < 2 ) {
item = SortAndUniqItems(GETOPERAND(q), GETQUERY(q), &size);
if (size < 2)
{
pfree(item);
return calc_rank_or(w, t, q);
}
}
pos = (uint16 **) palloc(sizeof(uint16 *) * q->size);
memset(pos, 0, sizeof(uint16 *) * q->size);
*(uint16 *) POSNULL = lengthof(POSNULL) - 1;
WEP_SETPOS(POSNULL[1], MAXENTRYPOS-1);
WEP_SETPOS(POSNULL[1], MAXENTRYPOS - 1);
for (i = 0; i < size; i++)
{
@ -220,7 +230,7 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
{
for (p = 0; p < lenct; p++)
{
dist = Abs((int)WEP_GETPOS(post[l]) - (int)WEP_GETPOS(ct[p]));
dist = Abs((int) WEP_GETPOS(post[l]) - (int) WEP_GETPOS(ct[p]));
if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL)))
{
float curw;
@ -248,11 +258,11 @@ calc_rank_or(float *w, tsvector * t, QUERYTYPE * q)
j,
i;
float res = -1.0;
ITEM **item;
int size = q->size;
ITEM **item;
int size = q->size;
*(uint16 *) POSNULL = lengthof(POSNULL) - 1;
item = SortAndUniqItems( GETOPERAND(q), GETQUERY(q), &size);
item = SortAndUniqItems(GETOPERAND(q), GETQUERY(q), &size);
for (i = 0; i < size; i++)
{
@ -279,7 +289,7 @@ calc_rank_or(float *w, tsvector * t, QUERYTYPE * q)
res = 1.0 - (1.0 - res) * (1.0 - wpos(post[j]));
}
}
pfree( item );
pfree(item);
return res;
}
@ -288,7 +298,7 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method)
{
ITEM *item = GETQUERY(q);
float res = 0.0;
int len;
int len;
if (!t->size || !q->size)
return 0.0;
@ -304,11 +314,12 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method)
case 0:
break;
case 1:
res /= log( (float)(cnt_length(t)+1) ) / log(2.0);
res /= log((float) (cnt_length(t) + 1)) / log(2.0);
break;
case 2:
len = cnt_length(t);
if ( len > 0 ) res /= (float)len;
if (len > 0)
res /= (float) len;
break;
default:
/* internal error */
@ -406,7 +417,7 @@ checkcondition_DR(void *checkval, ITEM * val)
while (ptr - ((ChkDocR *) checkval)->doc < ((ChkDocR *) checkval)->len)
{
if ( val == ptr->item || compareITEM( &val, &(ptr->item) ) == 0 )
if (val == ptr->item || compareITEM(&val, &(ptr->item)) == 0)
return true;
ptr++;
}
@ -496,12 +507,11 @@ Cover(DocRepresentation * doc, int len, QUERYTYPE * query, int *pos, int *p, int
ch.doc = f;
ch.len = (doc + lastpos) - f + 1;
*pos = f - doc + 1;
SortAndUniqOperand = GETOPERAND(query);
SortAndUniqOperand = GETOPERAND(query);
if (TS_execute(GETQUERY(query), &ch, false, checkcondition_DR))
{
/*
* elog(NOTICE,"OP:%d NP:%d P:%d Q:%d", *pos, lastpos, *p,
* *q);
* elog(NOTICE,"OP:%d NP:%d P:%d Q:%d", *pos, lastpos, *p, *q);
*/
return true;
}
@ -611,11 +621,12 @@ rank_cd(PG_FUNCTION_ARGS)
case 0:
break;
case 1:
res /= log( (float)(cnt_length(txt)+1) );
res /= log((float) (cnt_length(txt) + 1));
break;
case 2:
len = cnt_length(txt);
if ( len > 0 ) res /= (float)len;
if (len > 0)
res /= (float) len;
break;
default:
/* internal error */
@ -638,7 +649,7 @@ rank_cd_def(PG_FUNCTION_ARGS)
Int32GetDatum(-1),
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1),
(PG_NARGS() == 3) ? PG_GETARG_DATUM(2) : Int32GetDatum(DEF_NORM_METHOD)
(PG_NARGS() == 3) ? PG_GETARG_DATUM(2) : Int32GetDatum(DEF_NORM_METHOD)
));
}

View File

@ -1,69 +1,85 @@
#include <stdlib.h> /* for calloc, free */
#include <stdlib.h> /* for calloc, free */
#include "header.h"
extern struct SN_env * SN_create_env(int S_size, int I_size, int B_size)
extern struct SN_env *
SN_create_env(int S_size, int I_size, int B_size)
{
struct SN_env * z = (struct SN_env *) calloc(1, sizeof(struct SN_env));
if (z == NULL) return NULL;
z->p = create_s();
if (z->p == NULL) goto error;
if (S_size)
{
int i;
z->S = (symbol * *) calloc(S_size, sizeof(symbol *));
if (z->S == NULL) goto error;
struct SN_env *z = (struct SN_env *) calloc(1, sizeof(struct SN_env));
for (i = 0; i < S_size; i++)
{
z->S[i] = create_s();
if (z->S[i] == NULL) goto error;
}
z->S_size = S_size;
}
if (z == NULL)
return NULL;
z->p = create_s();
if (z->p == NULL)
goto error;
if (S_size)
{
int i;
if (I_size)
{
z->I = (int *) calloc(I_size, sizeof(int));
if (z->I == NULL) goto error;
z->I_size = I_size;
}
z->S = (symbol * *) calloc(S_size, sizeof(symbol *));
if (z->S == NULL)
goto error;
if (B_size)
{
z->B = (symbol *) calloc(B_size, sizeof(symbol));
if (z->B == NULL) goto error;
z->B_size = B_size;
}
for (i = 0; i < S_size; i++)
{
z->S[i] = create_s();
if (z->S[i] == NULL)
goto error;
}
z->S_size = S_size;
}
return z;
if (I_size)
{
z->I = (int *) calloc(I_size, sizeof(int));
if (z->I == NULL)
goto error;
z->I_size = I_size;
}
if (B_size)
{
z->B = (symbol *) calloc(B_size, sizeof(symbol));
if (z->B == NULL)
goto error;
z->B_size = B_size;
}
return z;
error:
SN_close_env(z);
return NULL;
SN_close_env(z);
return NULL;
}
extern void SN_close_env(struct SN_env * z)
extern void
SN_close_env(struct SN_env * z)
{
if (z == NULL) return;
if (z->S_size)
{
int i;
for (i = 0; i < z->S_size; i++)
{
lose_s(z->S[i]);
}
free(z->S);
}
if (z->I_size) free(z->I);
if (z->B_size) free(z->B);
if (z->p) lose_s(z->p);
free(z);
if (z == NULL)
return;
if (z->S_size)
{
int i;
for (i = 0; i < z->S_size; i++)
{
lose_s(z->S[i]);
}
free(z->S);
}
if (z->I_size)
free(z->I);
if (z->B_size)
free(z->B);
if (z->p)
lose_s(z->p);
free(z);
}
extern int SN_set_current(struct SN_env * z, int size, const symbol * s)
extern int
SN_set_current(struct SN_env * z, int size, const symbol * s)
{
int err = replace_s(z, 0, z->l, size, s, NULL);
z->c = 0;
return err;
}
int err = replace_s(z, 0, z->l, size, s, NULL);
z->c = 0;
return err;
}

Some files were not shown because too many files have changed in this diff Show More