1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-30 19:23:07 +03:00

Initial commit for Encode/Decode.

This commit is contained in:
benthompson15
2020-11-06 15:23:24 -06:00
parent 84fb821c47
commit 3e9b7b3401
9 changed files with 320 additions and 4 deletions

View File

@ -30,8 +30,10 @@ set(funcexp_LIB_SRCS
func_dayname.cpp
func_dayofweek.cpp
func_dayofyear.cpp
func_decode.cpp
func_div.cpp
func_elt.cpp
func_encode.cpp
func_exp.cpp
func_extract.cpp
func_find_in_set.cpp
@ -106,7 +108,8 @@ set(funcexp_LIB_SRCS
func_week.cpp
func_weekday.cpp
func_year.cpp
func_yearweek.cpp)
func_yearweek.cpp
sql_crypt.cpp)
add_library(funcexp SHARED ${funcexp_LIB_SRCS})

View File

@ -0,0 +1,71 @@
#include <cstdlib>
#include <string>
using namespace std;
#include "functor_str.h"
#include "funchelpers.h"
#include "functioncolumn.h"
using namespace execplan;
namespace funcexp
{
void Func_decode::hash_password(ulong *result, const char *password, uint password_len)
{
ulong nr=1345345333L, add=7, nr2=0x12345671L;
ulong tmp;
const char *password_end= password + password_len;
for (; password < password_end; password++)
{
if (*password == ' ' || *password == '\t')
continue;
tmp= (ulong) (unsigned char) *password;
nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
nr2+=(nr2 << 8) ^ nr;
add+=tmp;
}
result[0]=nr & (((ulong) 1L << 31) -1L);
result[1]=nr2 & (((ulong) 1L << 31) -1L);
}
CalpontSystemCatalog::ColType Func_decode::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
{
return resultType;
}
string Func_decode::getStrVal(rowgroup::Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType&)
{
const string& str = parm[0]->data()->getStrVal(row, isNull);
if (isNull)
{
return "";
}
const string& password = parm[1]->data()->getStrVal(row, isNull);
if (isNull)
{
return "";
}
int nStrLen = str.length();
int nPassLen = password.length();
char res[nStrLen+1];
memset(res,0,nStrLen+1);
if (!fSeeded)
{
hash_password(fSeeds, password.c_str(), nPassLen);
sql_crypt.init(fSeeds);
fSeeded = true;
}
memcpy(res,str.c_str(),nStrLen);
sql_crypt.decode(res,nStrLen);
sql_crypt.reinit();
return res;
}
}

View File

@ -0,0 +1,73 @@
#include <cstdlib>
#include <string>
using namespace std;
#include "functor_str.h"
#include "funchelpers.h"
#include "functioncolumn.h"
using namespace execplan;
namespace funcexp
{
void Func_encode::hash_password(ulong *result, const char *password, uint password_len)
{
ulong nr=1345345333L, add=7, nr2=0x12345671L;
ulong tmp;
const char *password_end= password + password_len;
for (; password < password_end; password++)
{
if (*password == ' ' || *password == '\t')
continue;
tmp= (ulong) (unsigned char) *password;
nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
nr2+=(nr2 << 8) ^ nr;
add+=tmp;
}
result[0]=nr & (((ulong) 1L << 31) -1L);
result[1]=nr2 & (((ulong) 1L << 31) -1L);
}
CalpontSystemCatalog::ColType Func_encode::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
{
return resultType;
}
string Func_encode::getStrVal(rowgroup::Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType&)
{
const string& str = parm[0]->data()->getStrVal(row, isNull);
if (isNull)
{
return "";
}
const string& password = parm[1]->data()->getStrVal(row, isNull);
if (isNull)
{
return "";
}
int nStrLen = str.length();
int nPassLen = password.length();
char res[nStrLen+1];
memset(res,0,nStrLen+1);
if (!fSeeded)
{
hash_password(fSeeds, password.c_str(), nPassLen);
sql_crypt.init(fSeeds);
fSeeded = true;
}
memcpy(res,str.c_str(),nStrLen);
sql_crypt.encode(res,nStrLen);
sql_crypt.reinit();
return res;
}
}

View File

@ -111,9 +111,11 @@ FuncExp::FuncExp()
fFuncMap["dayofmonth"] = new Func_day(); //dlh
fFuncMap["dayofweek"] = new Func_dayofweek(); //dlh
fFuncMap["dayofyear"] = new Func_dayofyear(); //dlh
fFuncMap["decode"] = new Func_decode(); // BT
fFuncMap["degrees"] = new Func_degrees();
fFuncMap["DIV"] = new Func_div(); // MySQL use upper case for this function name
fFuncMap["elt"] = new Func_elt();
fFuncMap["encode"] = new Func_encode(); // BT
fFuncMap["exp"] = new Func_exp();
fFuncMap["extract"] = new Func_extract(); //dlh
fFuncMap["find_in_set"] = new Func_find_in_set();

View File

@ -24,6 +24,7 @@
#define FUNCTOR_STR_H
#include "functor.h"
#include "sql_crypt.h"
namespace funcexp
{
@ -882,6 +883,64 @@ public:
execplan::CalpontSystemCatalog::ColType& op_ct);
};
/** @brief Func_encode class
*/
class Func_encode : public Func_Str
{
public:
Func_encode() : Func_Str("encode") , fSeeded(false) , fSeeds{0,0} {}
virtual ~Func_encode() {}
execplan::CalpontSystemCatalog::ColType operationType(FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType);
std::string getStrVal(rowgroup::Row& row,
FunctionParm& fp,
bool& isNull,
execplan::CalpontSystemCatalog::ColType& op_ct);
void resetSeed()
{
fSeeded = false;
fSeeds[0] = 0;
fSeeds[1] = 0;
}
private:
void hash_password(ulong *result, const char *password, uint password_len);
bool fSeeded;
SQL_CRYPT sql_crypt;
ulong fSeeds[2];
};
/** @brief Func_encode class
*/
class Func_decode : public Func_Str
{
public:
Func_decode() : Func_Str("decode") , fSeeded(false) , fSeeds{0,0} {}
virtual ~Func_decode() {}
execplan::CalpontSystemCatalog::ColType operationType(FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType);
std::string getStrVal(rowgroup::Row& row,
FunctionParm& fp,
bool& isNull,
execplan::CalpontSystemCatalog::ColType& op_ct);
void resetSeed()
{
fSeeded = false;
fSeeds[0] = 0;
fSeeds[1] = 0;
}
private:
void hash_password(ulong *result, const char *password, uint password_len);
bool fSeeded;
SQL_CRYPT sql_crypt;
ulong fSeeds[2];
};
}
#endif

View File

@ -0,0 +1,56 @@
#include <cstdlib>
#include <string>
using namespace std;
#include "sql_crypt.h"
namespace funcexp
{
void SQL_CRYPT::init(ulong *rand_nr)
{
uint i;
my_rnd_init(&rand,rand_nr[0],rand_nr[1]);
for (i=0 ; i<=255; i++)
decode_buff[i]= (char) i;
for (i=0 ; i<= 255 ; i++)
{
int idx= (uint) (my_rnd(&rand)*255.0);
char a= decode_buff[idx];
decode_buff[idx]= decode_buff[i];
decode_buff[+i]=a;
}
for (i=0 ; i <= 255 ; i++)
encode_buff[(unsigned char) decode_buff[i]]=i;
org_rand=rand;
shift=0;
}
void SQL_CRYPT::encode(char *str,uint length)
{
for (uint i=0; i < length; i++)
{
shift^=(uint) (my_rnd(&rand)*255.0);
uint idx= (uint) (unsigned char) str[0];
*str++ = (char) ((unsigned char) encode_buff[idx] ^ shift);
shift^= idx;
}
}
void SQL_CRYPT::decode(char *str,uint length)
{
for (uint i=0; i < length; i++)
{
shift^=(uint) (my_rnd(&rand)*255.0);
uint idx= (uint) ((unsigned char) str[0] ^ shift);
*str = decode_buff[idx];
shift^= (uint) (unsigned char) *str++;
}
}
}

40
utils/funcexp/sql_crypt.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef _SQL_CRYPT_H_
#define _SQL_CRYPT_H_
//#include "my_global.h"
/* Macros to make switching between C and C++ mode easier */
#ifdef __cplusplus
#define C_MODE_START extern "C" {
#define C_MODE_END }
#else
#define C_MODE_START
#define C_MODE_END
#endif
#include "my_rnd.h"
namespace funcexp
{
class SQL_CRYPT
{
struct my_rnd_struct rand,org_rand;
char decode_buff[256],encode_buff[256];
uint shift;
public:
SQL_CRYPT() {}
SQL_CRYPT(ulong *seed)
{
init(seed);
}
~SQL_CRYPT() {}
void init(ulong *seed);
void reinit() { shift=0; rand=org_rand; }
void encode(char *str, uint length);
void decode(char *str, uint length);
};
}
#endif /* _SQL_CRYPT_H_ */