Pass malloc'd strings with mdb_col_to_string, et al

This commit is contained in:
whydoubt 2005-02-25 03:27:32 +00:00
parent 2002c19cee
commit 937e72c305
5 changed files with 128 additions and 108 deletions

View File

@ -1,3 +1,9 @@
Thu Feb 24 21:13:02 CST 2005 Jeff Smith <whydoubt@yahoo.com>
* src/libmdb/data.c:
* src/libmdb/money.c:
* src/libmdb/props.c:
* src/odbc/odbc.c: Pass malloc'd strings with mdb_col_to_string, et al
Fri Feb 11 10:41:55 EST 2005 Brian Bruns <brian@bruns.com> Fri Feb 11 10:41:55 EST 2005 Brian Bruns <brian@bruns.com>
* src/libmdb/index.c: Fix off by one bug in mdb_index_swap_n() * src/libmdb/index.c: Fix off by one bug in mdb_index_swap_n()
* src/libmdb/write.c: Fix bug in key hash * src/libmdb/write.c: Fix bug in key hash

View File

@ -27,10 +27,11 @@
#define OFFSET_MASK 0x1fff #define OFFSET_MASK 0x1fff
char *mdb_money_to_string(MdbHandle *mdb, int start, char *s); char *mdb_money_to_string(MdbHandle *mdb, int start);
static int _mdb_attempt_bind(MdbHandle *mdb, static int _mdb_attempt_bind(MdbHandle *mdb,
MdbColumn *col, unsigned char isnull, int offset, int len); MdbColumn *col, unsigned char isnull, int offset, int len);
static char *mdb_num_to_string(MdbHandle *mdb, int start, int datatype, int prec, int scale); static char *mdb_num_to_string(MdbHandle *mdb, int start, int datatype, int prec, int scale);
static char *mdb_date_to_string(MdbHandle *mdb, int start);
int mdb_copy_ole(MdbHandle *mdb, char *dest, int start, int size); int mdb_copy_ole(MdbHandle *mdb, char *dest, int start, int size);
static char date_fmt[64] = "%x %X"; static char date_fmt[64] = "%x %X";
@ -196,18 +197,19 @@ int ret;
if (col->bind_ptr) { if (col->bind_ptr) {
if (!len) { if (!len) {
strcpy(col->bind_ptr, ""); strcpy(col->bind_ptr, "");
} else if (col->col_type == MDB_NUMERIC) {
//fprintf(stdout,"len %d size %d\n",len, col->col_size);
char *str = mdb_num_to_string(mdb, start, col->col_type,
col->col_prec, col->col_scale);
strcpy(col->bind_ptr, str);
g_free(str);
} else { } else {
//fprintf(stdout,"len %d size %d\n",len, col->col_size); //fprintf(stdout,"len %d size %d\n",len, col->col_size);
char *str = mdb_col_to_string(mdb, mdb->pg_buf, start, char *str;
col->col_type, len); if (col->col_type == MDB_NUMERIC) {
str = mdb_num_to_string(mdb, start,
col->col_type, col->col_prec,
col->col_scale);
} else {
str = mdb_col_to_string(mdb, mdb->pg_buf, start,
col->col_type, len);
}
strcpy(col->bind_ptr, str); strcpy(col->bind_ptr, str);
g_free(str);
} }
ret = strlen(col->bind_ptr); ret = strlen(col->bind_ptr);
if (col->len_ptr) { if (col->len_ptr) {
@ -595,18 +597,19 @@ int mdb_copy_ole(MdbHandle *mdb, char *dest, int start, int size)
static char *mdb_memo_to_string(MdbHandle *mdb, int start, int size) static char *mdb_memo_to_string(MdbHandle *mdb, int start, int size)
{ {
guint16 memo_len; guint16 memo_len;
static char text[MDB_BIND_SIZE];
guint16 memo_flags; guint16 memo_flags;
guint32 row_start, pg_row; guint32 row_start, pg_row;
guint32 len; guint32 len;
char *buf; char *buf;
char *text = (char *) g_malloc(MDB_BIND_SIZE);
if (size<MDB_MEMO_OVERHEAD) { if (size<MDB_MEMO_OVERHEAD) {
return ""; strcpy(text, "");
return text;
} }
#if MDB_DEBUG #if MDB_DEBUG
buffer_dump(mdb->pg_buf, start, start + 12); buffer_dump(mdb->pg_buf, start, start + MDB_MEMO_OVERHEAD);
#endif #endif
/* The 16 bit integer at offset 0 is the length of the memo field. /* The 16 bit integer at offset 0 is the length of the memo field.
@ -626,7 +629,8 @@ static char *mdb_memo_to_string(MdbHandle *mdb, int start, int size)
printf("Reading LVAL page %06x\n", pg_row >> 8); printf("Reading LVAL page %06x\n", pg_row >> 8);
#endif #endif
if (mdb_find_pg_row(mdb, pg_row, &buf, &row_start, &len)) { if (mdb_find_pg_row(mdb, pg_row, &buf, &row_start, &len)) {
return ""; strcpy(text, "");
return text;
} }
#if MDB_DEBUG #if MDB_DEBUG
printf("row num %d start %d len %d\n", printf("row num %d start %d len %d\n",
@ -645,7 +649,8 @@ static char *mdb_memo_to_string(MdbHandle *mdb, int start, int size)
tmp[0] = '\0'; tmp[0] = '\0';
do { do {
if (mdb_find_pg_row(mdb,pg_row,&buf,&row_start,&len)) { if (mdb_find_pg_row(mdb,pg_row,&buf,&row_start,&len)) {
return ""; strcpy(text, "");
return text;
} }
#if MDB_DEBUG #if MDB_DEBUG
printf("row num %d start %d len %d\n", printf("row num %d start %d len %d\n",
@ -685,10 +690,12 @@ mdb_num_to_string(MdbHandle *mdb, int start, int datatype, int prec, int scale)
return text; return text;
} }
static int trim_trailing_zeros(char * buff, int n) static int trim_trailing_zeros(char * buff)
{ {
char * p = buff + n - 1; char *p;
int n = strlen(buff);
p = buff + n - 1;
while (p >= buff && *p == '0') while (p >= buff && *p == '0')
*p-- = '\0'; *p-- = '\0';
@ -697,15 +704,13 @@ static int trim_trailing_zeros(char * buff, int n)
return 0; return 0;
} }
char *mdb_col_to_string(MdbHandle *mdb, unsigned char *buf, int start, int datatype, int size)
{
/* FIX ME -- not thread safe */
static char text[MDB_BIND_SIZE];
int n;
float tf;
double td;
/* Date/Time is stored as a double, where the whole
part is the days from 12/30/1899 and the fractional
part is the fractional part of one day. */
static char *
mdb_date_to_string(MdbHandle *mdb, int start)
{
struct tm t; struct tm t;
long int day, time; long int day, time;
int yr, q; int yr, q;
@ -713,103 +718,110 @@ char *mdb_col_to_string(MdbHandle *mdb, unsigned char *buf, int start, int datat
int noleap_cal[] = {0,31,59,90,120,151,181,212,243,273,304,334,365}; int noleap_cal[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
int leap_cal[] = {0,31,60,91,121,152,182,213,244,274,305,335,366}; int leap_cal[] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
char *text = (char *) g_malloc(MDB_BIND_SIZE);
double td = mdb_get_double(mdb->pg_buf, start);
day = (long int)(td);
time = (long int)(fabs(td - day) * 86400.0 + 0.5);
t.tm_hour = time / 3600;
t.tm_min = (time / 60) % 60;
t.tm_sec = time % 60;
t.tm_year = 1 - 1900;
day += 693593; /* Days from 1/1/1 to 12/31/1899 */
t.tm_wday = (day+1) % 7;
q = day / 146097; /* 146097 days in 400 years */
t.tm_year += 400 * q;
day -= q * 146097;
q = day / 36524; /* 36524 days in 100 years */
if (q > 3) q = 3;
t.tm_year += 100 * q;
day -= q * 36524;
q = day / 1461; /* 1461 days in 4 years */
t.tm_year += 4 * q;
day -= q * 1461;
q = day / 365; /* 365 days in 1 year */
if (q > 3) q = 3;
t.tm_year += q;
day -= q * 365;
yr = t.tm_year + 1900;
cal = ((yr)%4==0 && ((yr)%100!=0 || (yr)%400==0)) ?
leap_cal : noleap_cal;
for (t.tm_mon=0; t.tm_mon<12; t.tm_mon++) {
if (day < cal[t.tm_mon+1]) break;
}
t.tm_mday = day - cal[t.tm_mon] + 1;
t.tm_yday = day;
t.tm_isdst = -1;
strftime(text, MDB_BIND_SIZE, date_fmt, &t);
return text;
}
char *mdb_col_to_string(MdbHandle *mdb, unsigned char *buf, int start, int datatype, int size)
{
char *text;
float tf;
double td;
switch (datatype) { switch (datatype) {
case MDB_BOOL: case MDB_BOOL:
/* shouldn't happen. bools are handled specially /* shouldn't happen. bools are handled specially
** by mdb_xfer_bound_bool() */ ** by mdb_xfer_bound_bool() */
break; break;
case MDB_BYTE: case MDB_BYTE:
sprintf(text,"%d",mdb_get_byte(buf, start)); text = g_strdup_printf("%d", mdb_get_byte(buf, start));
return text;
break; break;
case MDB_INT: case MDB_INT:
sprintf(text,"%ld",(long)mdb_get_int16(buf, start)); text = g_strdup_printf("%ld",
return text; (long)mdb_get_int16(buf, start));
break; break;
case MDB_LONGINT: case MDB_LONGINT:
sprintf(text,"%ld",mdb_get_int32(buf, start)); text = g_strdup_printf("%ld",
return text; mdb_get_int32(buf, start));
break; break;
case MDB_FLOAT: case MDB_FLOAT:
tf = mdb_get_single(mdb->pg_buf, start); tf = mdb_get_single(mdb->pg_buf, start);
n = sprintf(text,"%.*f",FLT_DIG - (int)ceil(log10(tf)), tf); text = g_strdup_printf("%.*f",
trim_trailing_zeros(text, n); FLT_DIG - (int)ceil(log10(tf)), tf);
return text; trim_trailing_zeros(text);
break; break;
case MDB_DOUBLE: case MDB_DOUBLE:
td = mdb_get_double(mdb->pg_buf, start); td = mdb_get_double(mdb->pg_buf, start);
n = sprintf(text,"%.*f",DBL_DIG - (int)ceil(log10(td)), td); text = g_strdup_printf("%.*f",
trim_trailing_zeros(text, n); DBL_DIG - (int)ceil(log10(td)), td);
return text; trim_trailing_zeros(text);
break; break;
case MDB_TEXT: case MDB_TEXT:
if (size<0) { if (size<0) {
return ""; text = g_strdup("");
} else {
text = (char *) g_malloc(MDB_BIND_SIZE);
mdb_unicode2ascii(mdb, mdb->pg_buf + start,
size, text, MDB_BIND_SIZE);
} }
mdb_unicode2ascii(mdb, mdb->pg_buf + start, size, text, MDB_BIND_SIZE);
return text;
break; break;
case MDB_SDATETIME: case MDB_SDATETIME:
/* Date/Time is stored as a double, where the whole text = mdb_date_to_string(mdb, start);
part is the days from 12/30/1899 and the fractional
part is the fractional part of one day. */
td = mdb_get_double(mdb->pg_buf, start);
day = (long int)(td);
time = (long int)(fabs(td - day) * 86400.0 + 0.5);
t.tm_hour = time / 3600;
t.tm_min = (time / 60) % 60;
t.tm_sec = time % 60;
t.tm_year = 1 - 1900;
day += 693593; /* Days from 1/1/1 to 12/31/1899 */
t.tm_wday = (day+1) % 7;
q = day / 146097; /* 146097 days in 400 years */
t.tm_year += 400 * q;
day -= q * 146097;
q = day / 36524; /* 36524 days in 100 years */
if (q > 3) q = 3;
t.tm_year += 100 * q;
day -= q * 36524;
q = day / 1461; /* 1461 days in 4 years */
t.tm_year += 4 * q;
day -= q * 1461;
q = day / 365; /* 365 days in 1 year */
if (q > 3) q = 3;
t.tm_year += q;
day -= q * 365;
yr = t.tm_year + 1900;
cal = ((yr)%4==0 && ((yr)%100!=0 || (yr)%400==0)) ?
leap_cal : noleap_cal;
for (t.tm_mon=0; t.tm_mon<12; t.tm_mon++) {
if (day < cal[t.tm_mon+1]) break;
}
t.tm_mday = day - cal[t.tm_mon] + 1;
t.tm_yday = day;
t.tm_isdst = -1;
strftime(text, MDB_BIND_SIZE, date_fmt, &t);
return text;
break; break;
case MDB_MEMO: case MDB_MEMO:
return mdb_memo_to_string(mdb, start, size); text = mdb_memo_to_string(mdb, start, size);
break; break;
case MDB_MONEY: case MDB_MONEY:
mdb_money_to_string(mdb, start, text); text = mdb_money_to_string(mdb, start);
return text;
case MDB_NUMERIC: case MDB_NUMERIC:
break; break;
default: default:
return ""; text = g_strdup("");
break; break;
} }
return NULL; return text;
} }
int mdb_col_disp_size(MdbColumn *col) int mdb_col_disp_size(MdbColumn *col)
{ {
@ -839,7 +851,7 @@ int mdb_col_disp_size(MdbColumn *col)
return 20; return 20;
break; break;
case MDB_MEMO: case MDB_MEMO:
return 255; return 64000;
break; break;
case MDB_MONEY: case MDB_MONEY:
return 21; return 21;

View File

@ -24,7 +24,7 @@
#include "dmalloc.h" #include "dmalloc.h"
#endif #endif
#define MAXPRECISION 20 #define MAXPRECISION 19
/* /*
** these routines are copied from the freetds project which does something ** these routines are copied from the freetds project which does something
** very similiar ** very similiar
@ -38,11 +38,10 @@ static char *array_to_string(unsigned char *array, int unsigned scale, char *s);
* mdb_money_to_string * mdb_money_to_string
* @mdb: Handle to open MDB database file * @mdb: Handle to open MDB database file
* @start: Offset of the field within the current page * @start: Offset of the field within the current page
* @s: String that will receieve the value
* *
* Returns: the string that has received the value. * Returns: the allocated string that has received the value.
*/ */
char *mdb_money_to_string(MdbHandle *mdb, int start, char *s) char *mdb_money_to_string(MdbHandle *mdb, int start)
{ {
int num_bytes = 8; int num_bytes = 8;
int i; int i;
@ -77,13 +76,7 @@ char *mdb_money_to_string(MdbHandle *mdb, int start, char *s)
memset(multiplier,0,MAXPRECISION); memset(multiplier,0,MAXPRECISION);
multiply_byte(multiplier, 256, temp); multiply_byte(multiplier, 256, temp);
} }
if (neg) { return array_to_string(product, 4, neg);
s[0]='-';
array_to_string(product, 4, &s[1]);
} else {
array_to_string(product, 4, s);
}
return s;
} }
static int multiply_byte(unsigned char *product, int num, unsigned char *multiplier) static int multiply_byte(unsigned char *product, int num, unsigned char *multiplier)
{ {
@ -119,17 +112,23 @@ static int do_carry(unsigned char *product)
} }
return 0; return 0;
} }
static char *array_to_string(unsigned char *array, unsigned int scale, char *s) static char *array_to_string(unsigned char *array, unsigned int scale, int neg)
{ {
char *s;
unsigned int top, i, j=0; unsigned int top, i, j=0;
for (top=MAXPRECISION;(top>0) && (top-1>scale) && !array[top-1];top--); for (top=MAXPRECISION;(top>0) && (top-1>scale) && !array[top-1];top--);
s = (char *) g_malloc(22);
if (neg)
s[j++] = '-';
if (top == 0) { if (top == 0) {
s[j++] = '0'; s[j++] = '0';
} else { } else {
for (i=top; i>0; i--) { for (i=top; i>0; i--) {
if (j == top-scale) s[j++]='.'; if (i == scale) s[j++]='.';
s[j++]=array[i-1]+'0'; s[j++]=array[i-1]+'0';
} }
} }

View File

@ -115,9 +115,11 @@ mdb_read_props(MdbHandle *mdb, GPtrArray *names, gchar *kkd, int len)
#endif #endif
if (dtype == MDB_MEMO) dtype = MDB_TEXT; if (dtype == MDB_MEMO) dtype = MDB_TEXT;
if (dtype == MDB_BOOL) { if (dtype == MDB_BOOL) {
g_hash_table_insert(props->hash, g_strdup(name), g_strdup(kkd[pos + 8] ? "yes" : "no")); g_hash_table_insert(props->hash, g_strdup(name),
g_strdup(kkd[pos + 8] ? "yes" : "no"));
} else { } else {
g_hash_table_insert(props->hash, g_strdup(name), g_strdup(mdb_col_to_string(mdb, kkd, pos + 8, dtype, dsize))); g_hash_table_insert(props->hash, g_strdup(name),
mdb_col_to_string(mdb, kkd, pos + 8, dtype, dsize));
} }
g_free(value); g_free(value);
pos += record_len; pos += record_len;

View File

@ -27,7 +27,7 @@
#include "connectparams.h" #include "connectparams.h"
static char software_version[] = "$Id: odbc.c,v 1.29 2005/01/15 05:02:09 calvinrsmith Exp $"; static char software_version[] = "$Id: odbc.c,v 1.30 2005/02/25 03:27:45 whydoubt Exp $";
static void *no_unused_var_warn[] = {software_version, static void *no_unused_var_warn[] = {software_version,
no_unused_var_warn}; no_unused_var_warn};
@ -1166,6 +1166,7 @@ SQLRETURN SQL_API SQLGetData(
char *str = mdb_col_to_string(mdb,mdb->pg_buf, char *str = mdb_col_to_string(mdb,mdb->pg_buf,
col->cur_value_start,col->col_type,col->cur_value_len); col->cur_value_start,col->col_type,col->cur_value_len);
strcpy(rgbValue, str); strcpy(rgbValue, str);
g_free(str);
if (pcbValue) if (pcbValue)
*pcbValue = col->cur_value_len; *pcbValue = col->cur_value_len;
} else { } else {