2010-09-18 02:51:12 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-04-09 17:50:58 +02:00
|
|
|
* Copyright (c) 2010-2021, Tomasz Sowa
|
2010-09-18 02:51:12 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-09-18 02:51:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2012-01-12 03:24:08 +01:00
|
|
|
#include "utf8/utf8.h"
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PGresult * DbBase::AssertQuery(const DbTextStream & query)
|
|
|
|
{
|
|
|
|
return AssertQuery(query.CStr());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-12 15:52:52 +01:00
|
|
|
|
|
|
|
PGresult * DbBase::AssertQuery(const char * q, ExecStatusType t)
|
|
|
|
{
|
|
|
|
PGresult * r = AssertQuery(q);
|
|
|
|
AssertResult(r, t);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PGresult * DbBase::AssertQuery(const DbTextStream & query, ExecStatusType t)
|
|
|
|
{
|
|
|
|
return AssertQuery(query.CStr(), t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
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);
|
2012-05-27 20:11:34 +02:00
|
|
|
static std::wstring temp_wide_value; // !! IMPROVE ME add as a class field (nonstatic)
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2021-05-21 00:41:27 +02:00
|
|
|
pt::utf8_to_wide(res, temp_wide_value);
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
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);
|
2021-05-21 00:41:27 +02:00
|
|
|
pt::utf8_to_wide(res, result);
|
2010-11-21 01:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::Date DbBase::AssertValueDate(PGresult * r, int row, int col)
|
2012-05-27 01:04:49 +02:00
|
|
|
{
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::Date date = AssertValue(r, row, col);
|
2012-05-27 01:04:49 +02:00
|
|
|
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
bool DbBase::AssertValueSpace(PGresult * r, int row, int col, pt::Space & space)
|
2012-01-08 04:59:51 +01:00
|
|
|
{
|
|
|
|
const char * res = AssertValue(r, row, col);
|
|
|
|
|
2021-05-21 04:51:58 +02:00
|
|
|
pt::SpaceParser::Status status = conf_parser.parse_space(res, space);
|
2012-01-08 04:59:51 +01:00
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
if( status != pt::SpaceParser::ok )
|
2012-01-08 04:59:51 +01:00
|
|
|
{
|
2012-01-12 03:24:08 +01:00
|
|
|
log << log1 << "Db: a problem with parsing a PT::Space";
|
2012-01-08 04:59:51 +01:00
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
if( status == pt::SpaceParser::syntax_error )
|
2021-04-09 17:50:58 +02:00
|
|
|
log << ", syntax error at line: " << conf_parser.get_last_parsed_line();
|
2012-01-08 04:59:51 +01:00
|
|
|
|
|
|
|
log << logend;
|
|
|
|
|
2021-04-09 17:50:58 +02:00
|
|
|
space.clear();
|
2012-01-08 04:59:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-08-15 21:33:20 +02:00
|
|
|
bool DbBase::EndTrans(bool everything_ok)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
if( everything_ok )
|
|
|
|
{
|
|
|
|
result = (CommitTrans() == WINIX_ERR_OK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RollbackTrans();
|
|
|
|
// we return the old err code
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2012-09-11 07:19:45 +02:00
|
|
|
the old way (escape format)
|
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
|
|
|
*/
|
2012-09-11 07:19:45 +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
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 07:19:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
converting from a bytea
|
|
|
|
the new way (hex format)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
char DbBase::UnescapeBinHexToDigit(char hex)
|
|
|
|
{
|
|
|
|
if( hex>='0' && hex<='9' )
|
|
|
|
return hex - '0';
|
|
|
|
|
|
|
|
if( hex>='a' && hex<='z' )
|
|
|
|
return hex - 'a' + 10;
|
|
|
|
|
|
|
|
if( hex>='A' && hex<='Z' )
|
|
|
|
return hex - 'A' + 10;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbBase::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out)
|
|
|
|
{
|
|
|
|
if( clear_out )
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
if( len < 2 || str[0]!='\\' || str[1]!='x' )
|
|
|
|
{
|
|
|
|
log << log1 << "Db: unsupported binary format (skipping)" << logend;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i=2 ; i + 1 < len ; i+=2 )
|
|
|
|
{
|
|
|
|
int c1 = UnescapeBinHexToDigit(str[i]);
|
|
|
|
int c2 = UnescapeBinHexToDigit(str[i+1]);
|
|
|
|
|
|
|
|
out += ((c1 << 4) | c2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
end of converting from bytea
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
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
|
|
|
|