2001-02-17 04:48:34 +00:00
|
|
|
/* MDB Tools - A library for reading MS Access database file
|
|
|
|
|
* Copyright (C) 1998-1999 Brian Bruns
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
2011-08-28 19:53:29 -04:00
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-02-17 04:48:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "mdbtools.h"
|
|
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
#define MAX_MONEY_PRECISION 20
|
|
|
|
|
#define MAX_NUMERIC_PRECISION 40
|
2001-02-17 04:48:34 +00:00
|
|
|
/*
|
|
|
|
|
** these routines are copied from the freetds project which does something
|
|
|
|
|
** very similiar
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
static int multiply_byte(unsigned char *product, int num, unsigned char *multiplier, size_t len);
|
|
|
|
|
static int do_carry(unsigned char *product, size_t len);
|
|
|
|
|
static char *array_to_string(unsigned char *array, size_t len, int unsigned scale, int neg);
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2004-06-22 04:27:42 +00:00
|
|
|
/**
|
|
|
|
|
* mdb_money_to_string
|
|
|
|
|
* @mdb: Handle to open MDB database file
|
|
|
|
|
* @start: Offset of the field within the current page
|
|
|
|
|
*
|
2005-02-25 03:27:32 +00:00
|
|
|
* Returns: the allocated string that has received the value.
|
2004-06-22 04:27:42 +00:00
|
|
|
*/
|
2005-02-25 03:27:32 +00:00
|
|
|
char *mdb_money_to_string(MdbHandle *mdb, int start)
|
2001-02-17 04:48:34 +00:00
|
|
|
{
|
2014-12-29 13:14:45 +01:00
|
|
|
const int num_bytes=8, scale=4;
|
2004-09-23 05:07:09 +00:00
|
|
|
int i;
|
|
|
|
|
int neg=0;
|
2020-08-06 12:34:09 -04:00
|
|
|
unsigned char multiplier[MAX_MONEY_PRECISION] = { 1 };
|
|
|
|
|
unsigned char temp[MAX_MONEY_PRECISION];
|
|
|
|
|
unsigned char product[MAX_MONEY_PRECISION] = { 0 };
|
|
|
|
|
unsigned char bytes[num_bytes];
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
memcpy(bytes, mdb->pg_buf + start, num_bytes);
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2004-09-23 05:07:09 +00:00
|
|
|
/* Perform two's complement for negative numbers */
|
2020-08-06 12:34:09 -04:00
|
|
|
if (bytes[num_bytes-1] & 0x80) {
|
2001-02-17 04:48:34 +00:00
|
|
|
neg = 1;
|
2002-03-16 02:24:53 +00:00
|
|
|
for (i=0;i<num_bytes;i++) {
|
2011-03-20 15:23:27 -04:00
|
|
|
bytes[i] = ~bytes[i];
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
2002-03-16 02:24:53 +00:00
|
|
|
for (i=0; i<num_bytes; i++) {
|
2011-03-20 15:23:27 -04:00
|
|
|
bytes[i] ++;
|
|
|
|
|
if (bytes[i]!=0) break;
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2002-03-16 02:24:53 +00:00
|
|
|
|
2004-09-23 05:07:09 +00:00
|
|
|
for (i=0;i<num_bytes;i++) {
|
|
|
|
|
/* product += multiplier * current byte */
|
2020-08-06 12:34:09 -04:00
|
|
|
multiply_byte(product, bytes[i], multiplier, sizeof(multiplier));
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2004-09-23 05:07:09 +00:00
|
|
|
/* multiplier = multiplier * 256 */
|
2020-08-06 12:34:09 -04:00
|
|
|
memcpy(temp, multiplier, sizeof(multiplier));
|
|
|
|
|
memset(multiplier, 0, sizeof(multiplier));
|
|
|
|
|
multiply_byte(multiplier, 256, temp, sizeof(multiplier));
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
2020-08-06 12:34:09 -04:00
|
|
|
return array_to_string(product, sizeof(product), scale, neg);
|
2011-03-20 15:23:27 -04:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 12:51:53 -05:00
|
|
|
char *mdb_numeric_to_string(MdbHandle *mdb, int start, int scale, int prec) {
|
2014-12-29 13:14:45 +01:00
|
|
|
const int num_bytes = 16;
|
2011-03-20 15:23:27 -04:00
|
|
|
int i;
|
|
|
|
|
int neg=0;
|
2020-08-06 12:34:09 -04:00
|
|
|
unsigned char multiplier[MAX_NUMERIC_PRECISION] = { 1 };
|
|
|
|
|
unsigned char temp[MAX_NUMERIC_PRECISION];
|
|
|
|
|
unsigned char product[MAX_NUMERIC_PRECISION] = { 0 };
|
2011-03-20 15:23:27 -04:00
|
|
|
unsigned char bytes[num_bytes];
|
|
|
|
|
|
|
|
|
|
memcpy(bytes, mdb->pg_buf + start + 1, num_bytes);
|
|
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
/* Negative bit is stored in first byte */
|
2011-03-20 15:23:27 -04:00
|
|
|
if (mdb->pg_buf[start] & 0x80) neg = 1;
|
|
|
|
|
for (i=0;i<num_bytes;i++) {
|
|
|
|
|
/* product += multiplier * current byte */
|
2020-08-06 12:34:09 -04:00
|
|
|
multiply_byte(product, bytes[12-4*(i/4)+i%4], multiplier, sizeof(multiplier));
|
2011-03-20 15:23:27 -04:00
|
|
|
|
|
|
|
|
/* multiplier = multiplier * 256 */
|
2020-08-06 12:34:09 -04:00
|
|
|
memcpy(temp, multiplier, sizeof(multiplier));
|
|
|
|
|
memset(multiplier, 0, sizeof(multiplier));
|
|
|
|
|
multiply_byte(multiplier, 256, temp, sizeof(multiplier));
|
2011-03-20 15:23:27 -04:00
|
|
|
}
|
2020-08-06 12:34:09 -04:00
|
|
|
return array_to_string(product, sizeof(product), prec, neg);
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
2011-03-20 15:23:27 -04:00
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
static int multiply_byte(unsigned char *product, int num, unsigned char *multiplier, size_t len)
|
2001-02-17 04:48:34 +00:00
|
|
|
{
|
2020-08-06 12:34:09 -04:00
|
|
|
unsigned char number[3] = { num % 10, (num/10) % 10, (num/100) % 10 };
|
2004-09-23 05:07:09 +00:00
|
|
|
unsigned int i, j;
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
for (i=0;i<len;i++) {
|
2004-09-23 05:07:09 +00:00
|
|
|
if (multiplier[i] == 0) continue;
|
2020-08-06 12:34:09 -04:00
|
|
|
for (j=0;j<3 && i+j<len;j++) {
|
2004-09-23 05:07:09 +00:00
|
|
|
if (number[j] == 0) continue;
|
|
|
|
|
product[i+j] += multiplier[i]*number[j];
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
2020-08-06 12:34:09 -04:00
|
|
|
do_carry(product, len);
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-08-06 12:34:09 -04:00
|
|
|
static int do_carry(unsigned char *product, size_t len)
|
2001-02-17 04:48:34 +00:00
|
|
|
{
|
2004-09-23 05:07:09 +00:00
|
|
|
unsigned int j;
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
for (j=0;j<len-1;j++) {
|
2001-02-17 04:48:34 +00:00
|
|
|
if (product[j]>9) {
|
|
|
|
|
product[j+1]+=product[j]/10;
|
2020-08-06 12:34:09 -04:00
|
|
|
product[j]%=10;
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-09-23 05:07:09 +00:00
|
|
|
if (product[j]>9) {
|
2020-08-06 12:34:09 -04:00
|
|
|
product[j]%=10;
|
2004-09-23 05:07:09 +00:00
|
|
|
}
|
2001-02-17 04:48:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-08-06 12:34:09 -04:00
|
|
|
static char *array_to_string(unsigned char *array, size_t len, unsigned int scale, int neg)
|
2001-02-17 04:48:34 +00:00
|
|
|
{
|
2005-02-25 03:27:32 +00:00
|
|
|
char *s;
|
2004-09-23 05:07:09 +00:00
|
|
|
unsigned int top, i, j=0;
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2020-08-06 12:34:09 -04:00
|
|
|
for (top=len;(top>0) && (top-1>scale) && !array[top-1];top--);
|
2001-02-17 04:48:34 +00:00
|
|
|
|
2011-03-20 15:23:27 -04:00
|
|
|
/* allocate enough space for all digits + minus sign + decimal point + trailing NULL byte */
|
2020-08-06 12:34:09 -04:00
|
|
|
s = (char *) g_malloc(len+3);
|
2005-02-25 03:27:32 +00:00
|
|
|
|
|
|
|
|
if (neg)
|
|
|
|
|
s[j++] = '-';
|
|
|
|
|
|
2004-09-23 05:07:09 +00:00
|
|
|
if (top == 0) {
|
|
|
|
|
s[j++] = '0';
|
|
|
|
|
} else {
|
|
|
|
|
for (i=top; i>0; i--) {
|
2005-02-25 03:27:32 +00:00
|
|
|
if (i == scale) s[j++]='.';
|
2004-09-23 05:07:09 +00:00
|
|
|
s[j++]=array[i-1]+'0';
|
|
|
|
|
}
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|
|
|
|
|
s[j]='\0';
|
2003-01-20 16:04:24 +00:00
|
|
|
|
|
|
|
|
return s;
|
2001-02-17 04:48:34 +00:00
|
|
|
}
|