2010-09-18 02:51:12 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)
pointing to /dev/null
added: DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
it reads binary (bytea) data
added: DbTextStream can handle 'bool' types now
(is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
with RSA
currently we have following hashes:
md4, md5, sha1, sha224, sha256, sha384, sha512
we are using openssl to manage them
(look at config options for more info)
changed: winix version to 0.4.7
added: class Run - you can run any program from os and send a buffer to its standard input
and read what the program put on its standard output
added: class Crypt (in System) - calculating hashes, and crypting/decrypting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
2011-06-09 23:22:08 +02:00
|
|
|
* Copyright (c) 2010-2011, Tomasz Sowa
|
2010-09-18 02:51:12 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <limits>
|
2010-11-21 01:19:17 +01:00
|
|
|
#include "dbbase.h"
|
|
|
|
#include "core/log.h"
|
|
|
|
#include "core/error.h"
|
2011-01-20 19:37:51 +01:00
|
|
|
#include "core/misc.h"
|
2010-11-23 22:52:25 +01:00
|
|
|
#include "utf8.h"
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
|
2011-01-20 19:37:51 +01:00
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
DbBase::DbBase()
|
|
|
|
{
|
|
|
|
db_conn = 0;
|
|
|
|
log_queries = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbBase::SetConn(DbConn * conn)
|
|
|
|
{
|
|
|
|
db_conn = conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbBase::SetConn(DbConn & conn)
|
|
|
|
{
|
|
|
|
db_conn = &conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DbConn * DbBase::GetConn()
|
|
|
|
{
|
|
|
|
return db_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbBase::LogQueries(bool log_q)
|
|
|
|
{
|
|
|
|
log_queries = log_q;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PGresult * DbBase::AssertQuery(const char * q)
|
|
|
|
{
|
|
|
|
if( log_queries )
|
|
|
|
log << log1 << "Db: executing query: " << q << logend;
|
|
|
|
|
|
|
|
bool bad_query = false;
|
|
|
|
PGresult * r = PQexec(db_conn->GetPgConn(), q);
|
|
|
|
|
|
|
|
if( !r )
|
|
|
|
{
|
|
|
|
bad_query = true;
|
|
|
|
|
|
|
|
if( PQstatus(db_conn->GetPgConn()) != CONNECTION_OK )
|
|
|
|
{
|
|
|
|
db_conn->AssertConnection();
|
|
|
|
r = PQexec(db_conn->GetPgConn(), q);
|
|
|
|
|
|
|
|
if( r )
|
|
|
|
bad_query = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( bad_query )
|
|
|
|
{
|
|
|
|
log << log1 << "Db: Problem with this query: \"" << q << '\"' << logend;
|
|
|
|
log << log1 << "Db: " << PQerrorMessage(db_conn->GetPgConn()) << logend;
|
|
|
|
|
|
|
|
throw Error(WINIX_ERR_DB_INCORRECT_QUERY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
PGresult * DbBase::AssertQuery(const std::wstring & q)
|
2010-09-18 02:51:12 +02:00
|
|
|
{
|
|
|
|
return AssertQuery(q.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PGresult * DbBase::AssertQuery(const DbTextStream & query)
|
|
|
|
{
|
|
|
|
return AssertQuery(query.CStr());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbBase::AssertResult(PGresult * r, ExecStatusType t)
|
|
|
|
{
|
|
|
|
if( PQresultStatus(r) != t )
|
|
|
|
{
|
2011-10-16 23:33:45 +02:00
|
|
|
log << log1 << "Db: Incorrect result status: " << PQerrorMessage(db_conn->GetPgConn()) << logend;
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
throw Error(WINIX_ERR_DB_INCORRENT_RESULT_STATUS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int DbBase::AssertColumn(PGresult * r, const char * column_name)
|
|
|
|
{
|
|
|
|
int c = PQfnumber(r, column_name);
|
|
|
|
|
|
|
|
if( c == -1 )
|
|
|
|
{
|
|
|
|
log << log1 << "Db: there is no column: " << column_name << logend;
|
|
|
|
throw Error(WINIX_ERR_DB_NO_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * DbBase::AssertValue(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
const char * res = PQgetvalue(r, row, col);
|
|
|
|
|
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
log << log1 << "Db: there is no such an item in the result, row:" << row << ", col:" << col << logend;
|
|
|
|
throw Error(WINIX_ERR_NO_ITEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
const std::wstring & DbBase::AssertValueWide(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
const char * res = AssertValue(r, row, col);
|
|
|
|
static std::wstring temp_wide_value;
|
|
|
|
|
|
|
|
Ezc::UTF8ToWide(res, temp_wide_value);
|
|
|
|
|
|
|
|
return temp_wide_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)
pointing to /dev/null
added: DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
it reads binary (bytea) data
added: DbTextStream can handle 'bool' types now
(is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
with RSA
currently we have following hashes:
md4, md5, sha1, sha224, sha256, sha384, sha512
we are using openssl to manage them
(look at config options for more info)
changed: winix version to 0.4.7
added: class Run - you can run any program from os and send a buffer to its standard input
and read what the program put on its standard output
added: class Crypt (in System) - calculating hashes, and crypting/decrypting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
2011-06-09 23:22:08 +02:00
|
|
|
|
|
|
|
void DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
|
|
|
|
const char * res = AssertValue(r, row, col);
|
|
|
|
int len = PQgetlength(r, row, col);
|
|
|
|
|
|
|
|
if( len <= 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
UnescapeBin(res, len, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void DbBase::AssertValueWide(PGresult * r, int row, int col, std::wstring & result)
|
|
|
|
{
|
|
|
|
const char * res = AssertValue(r, row, col);
|
|
|
|
Ezc::UTF8ToWide(res, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
long DbBase::AssertValueLong(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
return strtol( AssertValue(r, row, col), 0, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DbBase::AssertValueInt(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
return (int)strtol( AssertValue(r, row, col), 0, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
bool DbBase::AssertValueBool(PGresult * r, int row, int col)
|
|
|
|
{
|
changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)
pointing to /dev/null
added: DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
it reads binary (bytea) data
added: DbTextStream can handle 'bool' types now
(is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
with RSA
currently we have following hashes:
md4, md5, sha1, sha224, sha256, sha384, sha512
we are using openssl to manage them
(look at config options for more info)
changed: winix version to 0.4.7
added: class Run - you can run any program from os and send a buffer to its standard input
and read what the program put on its standard output
added: class Crypt (in System) - calculating hashes, and crypting/decrypting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
2011-06-09 23:22:08 +02:00
|
|
|
const char * s = AssertValue(r, row, col);
|
|
|
|
return (s[0]=='t' || s[0]=='y' || s[0]=='1');
|
2010-12-11 23:55:48 +01:00
|
|
|
}
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
unsigned long DbBase::AssertValueULong(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
return strtoul( AssertValue(r, row, col), 0, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int DbBase::AssertValueUInt(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
return (unsigned int)strtoul( AssertValue(r, row, col), 0, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-09 22:27:45 +02:00
|
|
|
tm DbBase::AssertValueTm(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
return ConvertTime(AssertValue(r, row, col));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
void DbBase::ClearResult(PGresult * r)
|
|
|
|
{
|
|
|
|
if( r )
|
|
|
|
PQclear(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DbBase::IsNull(PGresult * r, int row, int col)
|
|
|
|
{
|
|
|
|
return PQgetisnull(r, row, col) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DbBase::Rows(PGresult * r)
|
|
|
|
{
|
|
|
|
// PQntuples - Returns the number of rows (tuples) in the query result. Because it returns
|
|
|
|
// an integer result, large result sets might overflow the return value on 32-bit operating systems.
|
|
|
|
return PQntuples(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DbBase::Cols(PGresult * r)
|
|
|
|
{
|
|
|
|
// PQnfields - Returns the number of columns (fields) in each row of the query result.
|
|
|
|
return PQnfields(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long DbBase::AffectedRows(PGresult * r)
|
|
|
|
{
|
|
|
|
// PQcmdTuples - This function returns a string containing the number of rows affected by the SQL
|
|
|
|
// statement that generated the PGresult. This function can only be used following the execution
|
|
|
|
// of an INSERT, UPDATE, DELETE, MOVE, FETCH, or COPY statement, or [...]
|
|
|
|
char * rows_str = PQcmdTuples(r); // can be an empty string
|
|
|
|
long rows = 0;
|
|
|
|
|
|
|
|
if( rows_str )
|
|
|
|
{
|
|
|
|
rows = strtol(rows_str, 0, 10);
|
|
|
|
// strtol - If an overflow or underflow occurs, errno is set to ERANGE
|
|
|
|
// and the function return value is clamped according to the following table:
|
|
|
|
// Function underflow overflow
|
|
|
|
// strtol() LONG_MIN LONG_MAX
|
|
|
|
|
|
|
|
if( rows < 0 )
|
|
|
|
rows = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long DbBase::AssertCurrval(const char * table)
|
|
|
|
{
|
|
|
|
PGresult * r;
|
|
|
|
|
|
|
|
bquery.Clear();
|
|
|
|
bquery << R("select currval(")
|
|
|
|
<< table
|
|
|
|
<< R(");");
|
|
|
|
|
|
|
|
r = AssertQuery(bquery);
|
|
|
|
AssertResult(r, PGRES_TUPLES_OK);
|
|
|
|
|
|
|
|
if( Rows(r) != 1 )
|
|
|
|
{
|
|
|
|
log << log1 << "Db: error (currval) for table: " << table << ", " << PQerrorMessage(db_conn->GetPgConn()) << logend;
|
|
|
|
throw Error(WINIX_ERR_DB_ERR_CURRVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AssertValueLong(r, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tm DbBase::ConvertTime(const char * str)
|
|
|
|
{
|
|
|
|
tm t;
|
|
|
|
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
|
|
|
|
|
|
if( !str )
|
|
|
|
return t;
|
|
|
|
|
|
|
|
size_t len = strlen(str);
|
|
|
|
|
|
|
|
if( len != 19 )
|
|
|
|
{
|
|
|
|
// the format must be like this: 2008-12-31 22:30:00
|
2011-01-20 19:37:51 +01:00
|
|
|
log << log1 << "DbBase: ConvertTime: unknown time format: \"" << str << "\"" << logend;
|
2010-09-18 02:51:12 +02:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
t.tm_year = Toi(str + 0) - 1900; /* year - 1900 */
|
|
|
|
t.tm_mon = Toi(str + 5) - 1; /* month of year (0 - 11) */
|
|
|
|
t.tm_mday = Toi(str + 8); /* day of month (1 - 31) */
|
|
|
|
t.tm_hour = Toi(str + 11); /* hours (0 - 23) */
|
|
|
|
t.tm_min = Toi(str + 14); /* minutes (0 - 59) */
|
|
|
|
t.tm_sec = Toi(str + 17); /* seconds (0 - 60) */
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2011-12-14 21:35:19 +01:00
|
|
|
t = Time(Time(t));
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
// t.tm_wday = 0; /* day of week (Sunday = 0) */
|
|
|
|
// t.tm_yday = 0; /* day of year (0 - 365) */
|
|
|
|
// t.tm_isdst = 0; /* is summer time in effect? */
|
|
|
|
// t.tm_zone = 0; // const_cast<char*>(""); /* abbreviation of timezone name */
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * DbBase::ConvertTime(const tm & t)
|
|
|
|
{
|
|
|
|
// not thread safe
|
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
t.tm_year + 1900,
|
|
|
|
t.tm_mon + 1,
|
|
|
|
t.tm_mday,
|
|
|
|
t.tm_hour,
|
|
|
|
t.tm_min,
|
|
|
|
t.tm_sec);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void DbBase::CreateIdList(const std::vector<long> & id_tab, std::wstring & list, bool add_parentheses)
|
2010-10-19 02:31:20 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
wchar_t buffer[50];
|
|
|
|
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
2010-10-19 02:31:20 +02:00
|
|
|
|
|
|
|
list.clear();
|
|
|
|
|
|
|
|
if( add_parentheses )
|
|
|
|
list += '(';
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<id_tab.size() ; ++i)
|
|
|
|
{
|
2011-01-20 19:37:51 +01:00
|
|
|
Toa((unsigned long)id_tab[i], buffer, buffer_len);
|
2010-10-19 02:31:20 +02:00
|
|
|
list += buffer;
|
|
|
|
|
|
|
|
if( i+1 < id_tab.size() )
|
|
|
|
list += ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
if( add_parentheses )
|
|
|
|
list += ')';
|
|
|
|
}
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
|
|
|
|
Error DbBase::DoCommand(const DbTextStream & command)
|
|
|
|
{
|
|
|
|
PGresult * r = 0;
|
|
|
|
Error status = WINIX_ERR_OK;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
r = AssertQuery(command);
|
|
|
|
AssertResult(r, PGRES_COMMAND_OK);
|
|
|
|
}
|
|
|
|
catch(const Error & e)
|
|
|
|
{
|
|
|
|
status = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
Error DbBase::DoCommand(const char * command)
|
|
|
|
{
|
|
|
|
PGresult * r = 0;
|
|
|
|
Error status = WINIX_ERR_OK;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-01-05 22:24:11 +01:00
|
|
|
r = AssertQuery(command);
|
2010-10-19 02:31:20 +02:00
|
|
|
AssertResult(r, PGRES_COMMAND_OK);
|
|
|
|
}
|
|
|
|
catch(const Error & e)
|
|
|
|
{
|
|
|
|
status = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearResult(r);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error DbBase::BeginTrans()
|
|
|
|
{
|
|
|
|
return DoCommand("BEGIN;");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error DbBase::RollbackTrans()
|
|
|
|
{
|
|
|
|
return DoCommand("ROLLBACK;");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error DbBase::CommitTrans()
|
|
|
|
{
|
|
|
|
return DoCommand("COMMIT;");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
Error DbBase::EndTrans(Error err)
|
|
|
|
{
|
|
|
|
if( err == WINIX_ERR_OK )
|
|
|
|
{
|
|
|
|
err = CommitTrans();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-24 00:23:24 +01:00
|
|
|
// we return the old err code
|
2011-01-05 22:24:11 +01:00
|
|
|
RollbackTrans();
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2010-09-18 02:51:12 +02:00
|
|
|
|
changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)
pointing to /dev/null
added: DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
it reads binary (bytea) data
added: DbTextStream can handle 'bool' types now
(is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
with RSA
currently we have following hashes:
md4, md5, sha1, sha224, sha256, sha384, sha512
we are using openssl to manage them
(look at config options for more info)
changed: winix version to 0.4.7
added: class Run - you can run any program from os and send a buffer to its standard input
and read what the program put on its standard output
added: class Crypt (in System) - calculating hashes, and crypting/decrypting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
2011-06-09 23:22:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
converting from a bytea
|
|
|
|
*/
|
|
|
|
|
|
|
|
int DbBase::CharToInt(char c)
|
|
|
|
{
|
|
|
|
return (int)(unsigned char)(c-'0');
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DbBase::IsCorrectOctalDigit(char c)
|
|
|
|
{
|
|
|
|
return c>='0' && c<='7';
|
|
|
|
}
|
|
|
|
|
|
|
|
// moves 'i' at least once
|
|
|
|
// return -1 if there is en error
|
|
|
|
int DbBase::UnescapeBin(const char * str, size_t & i, size_t len)
|
|
|
|
{
|
|
|
|
if( str[i] != '\\' )
|
|
|
|
return str[i++];
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
if( i >= len )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( str[i] == '\\' )
|
|
|
|
return str[i++];
|
|
|
|
|
|
|
|
if( i+2 >= len )
|
|
|
|
{
|
|
|
|
i = len;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !IsCorrectOctalDigit(str[i]) ||
|
|
|
|
!IsCorrectOctalDigit(str[i+1]) ||
|
|
|
|
!IsCorrectOctalDigit(str[i+2]) )
|
|
|
|
{
|
|
|
|
i += 3;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int c = 8*8*CharToInt(str[i]) + 8*CharToInt(str[i+1]) + CharToInt(str[i+2]);
|
|
|
|
|
|
|
|
i += 3;
|
|
|
|
|
|
|
|
if( c<0 || c>255 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbBase::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if( clear_out )
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
while( i < len )
|
|
|
|
{
|
|
|
|
c = UnescapeBin(str, i, len);
|
|
|
|
|
|
|
|
if( c != -1 )
|
|
|
|
out += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
end of converting from bytea
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|