Merge branch 'master' of http://www.hyzgame.com:3000/hyzboy/CMUtil
This commit is contained in:
commit
464272b3c1
26
inc/hgl/util/Crypt.h
Normal file
26
inc/hgl/util/Crypt.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef HGL_ALGORITHM_CRYPT_INCLUDE
|
||||
#define HGL_ALGORITHM_CRYPT_INCLUDE
|
||||
|
||||
#include<hgl/type/DataType.h>
|
||||
namespace hgl //校验/加密算法
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
class OutputStream;
|
||||
}//namespace io
|
||||
|
||||
namespace crypt
|
||||
{
|
||||
void OverflowEncrypt(void *, void *, int, void *, int); ///<溢出加密(轻度加密)
|
||||
void OverflowDecrypt(void *, void *, int, void *, int); ///<溢出解密
|
||||
|
||||
void AesEncrypt(uint8 data[16], uint8 *key, int keysize); ///<AES加密
|
||||
void AesDecrypt(uint8 data[16], uint8 *key, int keysize); ///<AES解密
|
||||
|
||||
void RC4Encrypt(uint8 *data, int datasize, uint8 *key, int keysize); ///<RC4加密
|
||||
|
||||
bool base64_encode(io::OutputStream *os,const uchar *input,size_t len);
|
||||
bool base64_decode(io::OutputStream *os,const uchar *input,size_t len);
|
||||
}//namespace crypt
|
||||
}//namespace hgl
|
||||
#endif//HGL_ALGORITHM_CRYPT_INCLUDE
|
88
inc/hgl/util/csv/CSVFieldSplite.h
Normal file
88
inc/hgl/util/csv/CSVFieldSplite.h
Normal file
@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/StrChar.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
/**
|
||||
* CSV字段拆分工具<br>
|
||||
* 支持逗号分隔与tab分隔以及使用引号包裹的字符串
|
||||
*/
|
||||
template<typename T> class CSVFieldSplite
|
||||
{
|
||||
const T *str;
|
||||
int str_length;
|
||||
|
||||
const T *sp;
|
||||
const T *end;
|
||||
|
||||
public:
|
||||
|
||||
CSVFieldSplite()
|
||||
{
|
||||
str=nullptr;
|
||||
str_length=0;
|
||||
sp=nullptr;
|
||||
end=nullptr;
|
||||
}
|
||||
|
||||
CSVFieldSplite(const T *s,const int length){Start(s,length);}
|
||||
~CSVFieldSplite()=default;
|
||||
|
||||
void Start(const T *s,const int length)
|
||||
{
|
||||
str=s;
|
||||
str_length=length;
|
||||
sp=str;
|
||||
end=str+str_length;
|
||||
}
|
||||
|
||||
const T *next_field(int *len)
|
||||
{
|
||||
if(!len)return(nullptr);
|
||||
if(sp>=end)return(nullptr);
|
||||
|
||||
if(*sp==','||*sp=='\t')
|
||||
{
|
||||
*len=0;
|
||||
++sp;
|
||||
return sp;
|
||||
}
|
||||
|
||||
const T *result;
|
||||
|
||||
if(*sp=='"')
|
||||
{
|
||||
++sp;
|
||||
|
||||
const T *ep=hgl::strchr(sp,T('"'));
|
||||
|
||||
if(!ep)
|
||||
return nullptr;
|
||||
|
||||
result=sp;
|
||||
|
||||
*len=ep-sp;
|
||||
sp=ep+2;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result=sp;
|
||||
|
||||
const T *ep=sp+1;
|
||||
|
||||
while(*ep!=','&&*ep!='\t'&&ep<end)
|
||||
++ep;
|
||||
|
||||
*len=ep-sp;
|
||||
|
||||
sp=ep+1;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
};//class CSVFieldSplite
|
||||
}//namespace util
|
||||
}//namespace hgl
|
126
inc/hgl/util/csv/CSVOutput.h
Normal file
126
inc/hgl/util/csv/CSVOutput.h
Normal file
@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
#include<initializer_list>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
/**
|
||||
* CSV输出控制
|
||||
*/
|
||||
template<typename T> class CSVOutput
|
||||
{
|
||||
io::TextOutputStream *tos;
|
||||
|
||||
uint FieldCount;
|
||||
|
||||
T FieldsTerminatedChar;
|
||||
T EnclosedChar;
|
||||
|
||||
T NullStringField[2];
|
||||
|
||||
protected:
|
||||
|
||||
uint write_count;
|
||||
|
||||
void NextField()
|
||||
{
|
||||
--write_count;
|
||||
|
||||
if(!write_count)
|
||||
{
|
||||
write_count=FieldCount;
|
||||
tos->WriteLineEnd();
|
||||
}
|
||||
else
|
||||
tos->WriteChars(&FieldsTerminatedChar,1);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
CSVOutput( io::TextOutputStream *os, //文本输出流
|
||||
const uint field_count, //字段数量
|
||||
const T fields_terminated_char=T(','), //字段分隔符
|
||||
const T enclosed_char=T('"')) //字符串包裹字符
|
||||
{
|
||||
tos=os;
|
||||
|
||||
FieldCount =field_count;
|
||||
|
||||
FieldsTerminatedChar=fields_terminated_char;
|
||||
|
||||
EnclosedChar =enclosed_char;
|
||||
|
||||
NullStringField[0] =enclosed_char;
|
||||
NullStringField[1] =enclosed_char;
|
||||
|
||||
write_count =FieldCount;
|
||||
}
|
||||
|
||||
~CSVOutput()=default;
|
||||
|
||||
void WriteString(const T *str,const int len)
|
||||
{
|
||||
if(!str||!*str||len<=0)
|
||||
{
|
||||
tos->WriteChars(NullStringField,2);
|
||||
}
|
||||
else
|
||||
{
|
||||
tos->WriteChars(&EnclosedChar,1);
|
||||
tos->WriteChars(str,len);
|
||||
tos->WriteChars(&EnclosedChar,1);
|
||||
}
|
||||
NextField();
|
||||
}
|
||||
|
||||
void WriteString(const String<T> &str)
|
||||
{
|
||||
WriteString(str.c_str(),str.Length());
|
||||
}
|
||||
|
||||
void WriteStringList(const StringList<T> &sl)
|
||||
{
|
||||
for(const String<T> &str:sl)
|
||||
WriteString(str);
|
||||
}
|
||||
|
||||
void WriteStringList(const std::initializer_list<String<T>> &sl)
|
||||
{
|
||||
for(const String<T> &str:sl)
|
||||
WriteString(str);
|
||||
}
|
||||
|
||||
void WriteStringList(const std::initializer_list<const T *> &sl)
|
||||
{
|
||||
for(const T *str:sl)
|
||||
WriteString(str);
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
void WriteInteger(const I &value)
|
||||
{
|
||||
tos->WriteString(String<T>::numberOf(value));
|
||||
|
||||
NextField();
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void WriteFloat(const F &value,const uint frac)
|
||||
{
|
||||
tos->WriteString(String<T>::floatOf(value,frac));
|
||||
|
||||
NextField();
|
||||
}
|
||||
};//class CSVOutput
|
||||
|
||||
using UTF8CSVOutput=CSVOutput<u8char>;
|
||||
using UTF16CSVOutput=CSVOutput<u16char>;
|
||||
using UTF32CSVOutput=CSVOutput<u32char>;
|
||||
|
||||
inline CSVOutput<u8char> * CreateUTF8CSVOutput (io::TextOutputStream * tos,const uint fc,const u8char ftc=U8_TEXT (','),const u8char ec=U8_TEXT ('"')){return(new UTF8CSVOutput (tos,fc,ftc,ec));}
|
||||
inline CSVOutput<u16char> * CreateUTF16CSVOutput (io::TextOutputStream * tos,const uint fc,const u16char ftc=U16_TEXT(','),const u16char ec=U16_TEXT('"')){return(new UTF16CSVOutput(tos,fc,ftc,ec));}
|
||||
inline CSVOutput<u16char> * CreateUTF32CSVOutput (io::TextOutputStream * tos,const uint fc,const u32char ftc=U32_TEXT(','),const u32char ec=U32_TEXT('"')){return(new UTF16CSVOutput(tos,fc,ftc,ec));}
|
||||
}//namespace util
|
||||
}//namespace hgl
|
63
inc/hgl/util/csv/CSVOutputStream.h
Normal file
63
inc/hgl/util/csv/CSVOutputStream.h
Normal file
@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/csv/CSVOutput.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
class OutputStream;
|
||||
class TextOutputStream;
|
||||
}//namespace io
|
||||
|
||||
namespace util
|
||||
{
|
||||
template<typename T> class CSVOutputStream
|
||||
{
|
||||
io::OutputStream *os;
|
||||
|
||||
io::TextOutputStream *tos;
|
||||
|
||||
CSVOutput<T> *csv;
|
||||
|
||||
public:
|
||||
|
||||
CSVOutputStream(io::OutputStream *_os,io::TextOutputStream *_tos,CSVOutput<T> *_csv)
|
||||
{
|
||||
os=_os;
|
||||
tos=_tos;
|
||||
csv=_csv;
|
||||
}
|
||||
|
||||
~CSVOutputStream()
|
||||
{
|
||||
delete csv;
|
||||
delete tos;
|
||||
delete os;
|
||||
}
|
||||
|
||||
void WriteString(const T *str,const int len){csv->WriteString(str,len);}
|
||||
void WriteString(const String<T> &str){csv->WriteString(str);}
|
||||
|
||||
void WriteStringList(const StringList<T> &sl){return csv->WriteStringList(sl);}
|
||||
void WriteStringList(const std::initializer_list<String<T>> &sl){return csv->WriteStringList(sl);}
|
||||
void WriteStringList(const std::initializer_list<const T *> &sl){return csv->WriteStringList(sl);}
|
||||
|
||||
template<typename I>
|
||||
void WriteInteger(const I &value){csv->WriteInteger(value);}
|
||||
|
||||
template<typename F>
|
||||
void WriteFloat(const F &value,const uint frac){csv->WriteFloat(value,frac);}
|
||||
};//template<typename T> class CSVOutputStream
|
||||
|
||||
template<typename T> inline CSVOutputStream<T> *CreateCSVOutputToStream(io::OutputStream *os,io::TextOutputStream *tos,const uint field_count,const T fields_terminated_char=T(','),const T enclosed_char=T('"'))
|
||||
{
|
||||
CSVOutput<T> *csv=new CSVOutput<T>(tos,field_count,fields_terminated_char,enclosed_char);
|
||||
|
||||
return(new CSVOutputStream<T>(os,tos,csv));
|
||||
}
|
||||
|
||||
CSVOutputStream<u8char> *CreateCSVOutputToUTF8File(const OSString &filename,const uint field_count,const u8char fields_terminated_char=U8_TEXT(','),const u8char enclosed_char=U8_TEXT('"'));
|
||||
CSVOutputStream<u16char> *CreateCSVOutputToUTF16LEFile(const OSString &filename,const uint field_count,const u16char fields_terminated_char=U16_TEXT(','),const u16char enclosed_char=U16_TEXT('"'));
|
||||
}//namespace util
|
||||
}//namespace hgl
|
62
inc/hgl/util/csv/CSVParse.h
Normal file
62
inc/hgl/util/csv/CSVParse.h
Normal file
@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
#include<hgl/io/FileInputStream.h>
|
||||
#include<hgl/io/TextInputStream.h>
|
||||
#include<hgl/util/csv/CSVFieldSplite.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
template<typename T> class CSVParseCallback
|
||||
{
|
||||
public:
|
||||
|
||||
virtual bool OnLine(util::CSVFieldSplite<T> &csv)=0;
|
||||
};
|
||||
|
||||
template<typename T> class CSVTextParse:public io::TextInputStream::ParseCallback<T>
|
||||
{
|
||||
util::CSVFieldSplite<T> splite;
|
||||
|
||||
CSVParseCallback<T> *callback;
|
||||
|
||||
public:
|
||||
|
||||
CSVTextParse(CSVParseCallback<T> *pcb)
|
||||
{
|
||||
callback=pcb;
|
||||
}
|
||||
|
||||
bool OnLine(const T *text,const int length) override
|
||||
{
|
||||
if(!text||!*text||length<=0)
|
||||
return(true);
|
||||
|
||||
splite.Start(text,length);
|
||||
|
||||
return callback->OnLine(splite);
|
||||
}
|
||||
};//class CSVTextParse
|
||||
|
||||
template<typename T> inline bool ParseCSV(io::InputStream *is,CSVParseCallback<T> *pcb)
|
||||
{
|
||||
io::TextInputStream tis(is);
|
||||
|
||||
CSVTextParse<T> parse(pcb);
|
||||
|
||||
tis.SetParseCallback<T>(&parse);
|
||||
|
||||
return tis.Run()>0;
|
||||
}
|
||||
|
||||
template<typename T> inline bool ParseCSVFile(const OSString &filename,CSVParseCallback<T> *pcb)
|
||||
{
|
||||
io::OpenFileInputStream fis(filename);
|
||||
|
||||
if(!fis)
|
||||
return false;
|
||||
|
||||
return ParseCSV<T>(fis,pcb);
|
||||
}
|
||||
}//namespace util
|
||||
}//namespace hgl
|
@ -6,6 +6,13 @@ namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
constexpr const uint TGA_IMAGE_TYPE_COLOR_MAP =1;
|
||||
constexpr const uint TGA_IMAGE_TYPE_TRUE_COLOR =2;
|
||||
constexpr const uint TGA_IMAGE_TYPE_GRAYSCALE =3;
|
||||
|
||||
constexpr const uint TGA_DIRECTION_LOWER_LEFT =0;
|
||||
constexpr const uint TGA_DIRECTION_UPPER_LEFT =1;
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct TGAHeader
|
||||
{
|
||||
|
30
inc/hgl/util/plist/LoadPAttrib.h
Normal file
30
inc/hgl/util/plist/LoadPAttrib.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/plist/PAttrib.h>
|
||||
#include<hgl/io/LoadStringList.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
using namespace io;
|
||||
|
||||
/**
|
||||
* 从文本文件中加载PList
|
||||
* @param filename 文件名
|
||||
* @param pa_map 属性列表
|
||||
* @return 是否加载成功
|
||||
*/
|
||||
template<typename C> static int LoadFromTextFile(const OSString &filename,PAttribMap<C> &pa_map)
|
||||
{
|
||||
StringList<C> sl;
|
||||
|
||||
if(LoadStringListFromTextFile(sl,filename)<=0)
|
||||
return(false);
|
||||
|
||||
int n=sl.GetCount();
|
||||
|
||||
while(n--)
|
||||
Add(pa_map,sl[n]);
|
||||
|
||||
return(true);
|
||||
}
|
||||
}//namespace hgl
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/BaseString.h>
|
||||
#include<hgl/type/StringList.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/io/LoadStringList.h>
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
@ -17,21 +17,21 @@ namespace hgl
|
||||
PAttribBase()=default;
|
||||
virtual ~PAttribBase()=default;
|
||||
|
||||
virtual const bool ParseFromString(const BaseString<C> &str)=0;
|
||||
virtual BaseString<C> MakeToString()const=0;
|
||||
virtual const bool ParseFromString(const String<C> &str)=0;
|
||||
virtual String<C> MakeToString()const=0;
|
||||
};
|
||||
|
||||
template<typename C,typename T> class PAttrib:public PAttribBase<C>
|
||||
{
|
||||
protected:
|
||||
|
||||
BaseString<C> name;
|
||||
String<C> name;
|
||||
T value;
|
||||
T default_value;
|
||||
|
||||
public:
|
||||
|
||||
PAttrib(const BaseString<C> &n,const T &v)
|
||||
PAttrib(const String<C> &n,const T &v)
|
||||
{
|
||||
name=n;
|
||||
value=v;
|
||||
@ -40,8 +40,8 @@ namespace hgl
|
||||
|
||||
virtual ~PAttrib()=default;
|
||||
|
||||
virtual const bool ParseFromString(const BaseString<C> &str)=0;
|
||||
virtual BaseString<C> MakeToString()const=0;
|
||||
virtual const bool ParseFromString(const String<C> &str)=0;
|
||||
virtual String<C> MakeToString()const=0;
|
||||
|
||||
virtual const T &Get(){return value;}
|
||||
virtual void Set(const T &v){value=v;}
|
||||
@ -55,7 +55,7 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
PNumberAttrib(const BaseString<C> &n,const T &dv,const T &min_v,const T &max_v):PAttrib<C,T>(n,dv)
|
||||
PNumberAttrib(const String<C> &n,const T &dv,const T &min_v,const T &max_v):PAttrib<C,T>(n,dv)
|
||||
{
|
||||
min_value=min_v;
|
||||
max_value=max_v;
|
||||
@ -63,7 +63,7 @@ namespace hgl
|
||||
|
||||
virtual ~PNumberAttrib()=default;
|
||||
|
||||
const bool ParseFromString(const BaseString<C> &str) override
|
||||
const bool ParseFromString(const String<C> &str) override
|
||||
{
|
||||
if(ToNumber(str,this->value))
|
||||
{
|
||||
@ -75,16 +75,57 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
BaseString<C> MakeToString() const override
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return BaseString<C>(this->value);
|
||||
return String<C>::numberOf(this->value);
|
||||
}
|
||||
};//class PNumberAttrib:public PAttrib<C,uint>
|
||||
|
||||
template<typename C> using PIntAttrib =PNumberAttrib<C,int >;
|
||||
template<typename C> using PUintAttrib =PNumberAttrib<C,uint >;
|
||||
template<typename C> using PFloatAttrib =PNumberAttrib<C,float >;
|
||||
template<typename C> using PDoubleAttrib=PNumberAttrib<C,double >;
|
||||
|
||||
template<typename C,typename T> class PFloatNumberAttrib:public PAttrib<C,T>
|
||||
{
|
||||
protected:
|
||||
|
||||
T min_value,max_value;
|
||||
|
||||
uint frac;
|
||||
|
||||
public:
|
||||
|
||||
PFloatNumberAttrib(const String<C> &n,const T &dv,const T &min_v,const T &max_v,const int f):PAttrib<C,T>(n,dv)
|
||||
{
|
||||
min_value=min_v;
|
||||
max_value=max_v;
|
||||
|
||||
frac=f;
|
||||
}
|
||||
|
||||
virtual ~PFloatNumberAttrib()=default;
|
||||
|
||||
const bool ParseFromString(const String<C> &str) override
|
||||
{
|
||||
if(ToNumber(str,this->value))
|
||||
{
|
||||
if(this->value>=min_value&&this->value<=max_value)
|
||||
return(true);
|
||||
}
|
||||
|
||||
this->value=this->default_value;
|
||||
return(false);
|
||||
}
|
||||
|
||||
void SetFrac(uint f){frac=f;}
|
||||
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return String<C>::floatOf(this->value,frac);
|
||||
}
|
||||
};//class PFloatNumberAttrib:public PAttrib<C,uint>
|
||||
|
||||
template<typename C> using PFloatAttrib =PFloatNumberAttrib<C,float >;
|
||||
template<typename C> using PDoubleAttrib=PFloatNumberAttrib<C,double >;
|
||||
|
||||
template<typename C> class PBoolAttrib:public PAttrib<C,bool>
|
||||
{
|
||||
@ -92,7 +133,7 @@ namespace hgl
|
||||
|
||||
using PAttrib<C,bool>::PAttrib;
|
||||
|
||||
const bool ParseFromString(const BaseString<C> &str)
|
||||
const bool ParseFromString(const String<C> &str)
|
||||
{
|
||||
if(str.ToBool(this->value))
|
||||
return(true);
|
||||
@ -101,36 +142,87 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
BaseString<C> MakeToString() const override
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return(this->value?"true":"false");
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C> class PStringAttrib:public PAttrib<C,BaseString<C>>
|
||||
template<typename C> class PStringAttrib:public PAttrib<C,String<C>>
|
||||
{
|
||||
public:
|
||||
|
||||
using PAttrib<C,BaseString<C>>::PAttrib;
|
||||
using PAttrib<C,String<C>>::PAttrib;
|
||||
|
||||
const bool ParseFromString(const BaseString<C> &str) override
|
||||
const bool ParseFromString(const String<C> &str) override
|
||||
{
|
||||
this->value=str;
|
||||
return(true);
|
||||
}
|
||||
|
||||
BaseString<C> MakeToString() const override
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return this->value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C> using PAttribMap=Map<String<C>,PAttribBase<C> *>;
|
||||
|
||||
/**
|
||||
* 向属性列表中写入一个属性
|
||||
*/
|
||||
template<typename C> static bool Add(PAttribMap<C> &pa_map,const String<C> &str)
|
||||
{
|
||||
String<C> name;
|
||||
C *value;
|
||||
int off;
|
||||
|
||||
if(str.Length()<2)return(false);
|
||||
|
||||
if(((off=str.FindChar(C('\t')))==-1)
|
||||
&&((off=str.FindChar(C(' '))) ==-1)
|
||||
&&((off=str.FindChar(C('='))) ==-1)
|
||||
&&((off=str.FindChar(C(':'))) ==-1))
|
||||
return(false);
|
||||
|
||||
name.Strcpy(str,off);
|
||||
off++;
|
||||
|
||||
value=str.c_str()+off;
|
||||
|
||||
while(true)
|
||||
{
|
||||
if(*value == C('\t')
|
||||
||*value == C('=')
|
||||
||*value == C(' ')
|
||||
||*value == C(':'))
|
||||
{
|
||||
value++;
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
PAttribBase<C> *attr=GetListObject(pa_map,name);
|
||||
|
||||
if(attr)
|
||||
attr->ParseFromString(value);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename C> class PAttribSet
|
||||
{
|
||||
using PString=BaseString<C>;
|
||||
using PString=String<C>;
|
||||
using PStringList=StringList<PString>;
|
||||
|
||||
Map<BaseString<C>,PAttribBase<C> *> pa_map;
|
||||
PAttribMap<C> pa_map;
|
||||
|
||||
public:
|
||||
|
||||
operator PAttribMap<C> &() {return pa_map;}
|
||||
operator const PAttribMap<C> &()const {return pa_map;}
|
||||
|
||||
public:
|
||||
|
||||
@ -144,6 +236,25 @@ namespace hgl
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个浮点数属性
|
||||
* @param name 属性名称
|
||||
* @param dv 默认值
|
||||
* @param min_v 最小值
|
||||
* @param max_v 最大值
|
||||
* @param frac 小数点后位数
|
||||
* @return 属性对象
|
||||
*/
|
||||
template<typename T>
|
||||
PFloatNumberAttrib<C,T> *CreateFloatAttrib(const PString &name,const T &dv,const T &min_v,const T &max_v,const int frac)
|
||||
{
|
||||
PFloatNumberAttrib<C,T> *obj=new PFloatNumberAttrib<C,T>(name,dv,min_v,max_v,frac);
|
||||
|
||||
pa_map.Add(name,obj);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
PBoolAttrib<C> *CreateBoolAttrib(const PString &name,const bool &dv)
|
||||
{
|
||||
PBoolAttrib<C> *obj=new PBoolAttrib<C>(name,dv);
|
||||
@ -169,110 +280,22 @@ namespace hgl
|
||||
return pa_map.Add(name,attr);
|
||||
}
|
||||
|
||||
PAttribBase<C> *Get(const PString &name){return GetObject(pa_map,name);}
|
||||
bool Add(const PString &str)
|
||||
{
|
||||
return Add(pa_name,str);
|
||||
}
|
||||
|
||||
PAttribBase<C> *Get(const PString &name){return GetListObject(pa_map,name);}
|
||||
|
||||
void Delete(const PString &name){pa_map.DeleteByKey(name);}
|
||||
|
||||
void Clear(){pa_map.Clear();}
|
||||
void ClearData(){pa_map.ClearData();}
|
||||
|
||||
void Enum(void (*enum_func)(const BaseString<C> &key,PAttribBase<C> *value))
|
||||
void Enum(void (*enum_func)(const String<C> &key,PAttribBase<C> *value))
|
||||
{
|
||||
pa_map.Enum(enum_func);
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 保存到文本文件中
|
||||
*/
|
||||
template<ByteOrderMask BOM>
|
||||
bool SaveToTextFile(const OSString &filename,const PString &gap_ch=PString("\t")) ///<保存列表到文件
|
||||
{
|
||||
FileOutputStream fos;
|
||||
EndianTextOutputStream<BOM> tos(&fos);
|
||||
|
||||
if(!fos.CreateTrunc(filename))return(false);
|
||||
|
||||
tos.WriteBOM();
|
||||
|
||||
const int count=pa_map.GetCount();
|
||||
auto **pa_obj=pa_map.GetDataList();
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
tos.WriteString((*pa_obj)->left);
|
||||
tos.WriteString(gap_ch);
|
||||
tos.WriteString((*pa_obj)->right->MakeToString());
|
||||
tos.WriteLineEnd();
|
||||
|
||||
++pa_obj;
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
bool Add(const PString &str) ///<向列表中增加一项
|
||||
{
|
||||
PString name;
|
||||
C *value;
|
||||
int off;
|
||||
|
||||
if(str.Length()<2)return(false);
|
||||
|
||||
if(((off=str.FindChar(C('\t')))==-1)
|
||||
&&((off=str.FindChar(C(' '))) ==-1)
|
||||
&&((off=str.FindChar(C('='))) ==-1)
|
||||
&&((off=str.FindChar(C(':'))) ==-1))
|
||||
return(false);
|
||||
|
||||
name.Strcpy(str,off);
|
||||
off++;
|
||||
|
||||
value=str.c_str()+off;
|
||||
|
||||
while(true)
|
||||
{
|
||||
if(*value == C('\t')
|
||||
||*value == C('=')
|
||||
||*value == C(' ')
|
||||
||*value == C(':'))
|
||||
{
|
||||
value++;
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
PAttribBase<C> *attr=Get(name);
|
||||
|
||||
if(attr)
|
||||
attr->ParseFromString(value);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 从文本文件中加载
|
||||
*/
|
||||
virtual bool LoadFromTextFile(const OSString &filename) ///<从文件中加载列表
|
||||
{
|
||||
PStringList sl;
|
||||
|
||||
if(LoadStringListFromTextFile(sl,filename)<=0)
|
||||
return(false);
|
||||
|
||||
int n=sl.GetCount();
|
||||
|
||||
while(n--)
|
||||
Add(sl[n]);
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//template<typename C> class PAttribSet
|
||||
|
||||
using UTF8PAttribSet =PAttribSet<char>;
|
||||
|
@ -1,128 +0,0 @@
|
||||
#ifndef HGL_PLIST_INCLUDE
|
||||
#define HGL_PLIST_INCLUDE
|
||||
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/type/LoadStringList.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
namespace hgl
|
||||
{
|
||||
using namespace io;
|
||||
|
||||
/**
|
||||
* 属性列表,类似INI的管理类
|
||||
*/
|
||||
template<typename C> class PList:public Map<String<C>,String<C>> ///属性列表
|
||||
{
|
||||
public:
|
||||
|
||||
using PString=String<C>;
|
||||
using PStringList=StringList<C>;
|
||||
using PMap=Map<PString,PString>;
|
||||
|
||||
protected:
|
||||
|
||||
void ReadData(const PStringList &sl)
|
||||
{
|
||||
int n=sl.GetCount();
|
||||
|
||||
while(n--)
|
||||
Add(sl[n]);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual ~PList()=default;
|
||||
|
||||
virtual bool Add(const PString &key) ///<向列表中增加一项
|
||||
{
|
||||
PString name;
|
||||
C *value;
|
||||
int off;
|
||||
|
||||
if(key.Length()<2)return(false);
|
||||
|
||||
if(((off=key.FindChar(C('\t')))==-1)
|
||||
&&((off=key.FindChar(C(' ' )))==-1)
|
||||
&&((off=key.FindChar(C('=' )))==-1)
|
||||
&&((off=key.FindChar(C(':' )))==-1))
|
||||
return(false);
|
||||
|
||||
name.Strcpy(key,off);
|
||||
off++;
|
||||
|
||||
value=key.c_str()+off;
|
||||
|
||||
while(true)
|
||||
{
|
||||
if(*value == C('\t')
|
||||
||*value == C('=')
|
||||
||*value == C(' ')
|
||||
||*value == C(':'))
|
||||
value++;
|
||||
else
|
||||
{
|
||||
PString str=value;
|
||||
|
||||
PMap::Add(name,str);
|
||||
|
||||
return(true);
|
||||
}
|
||||
}
|
||||
}//bool PList::Add
|
||||
|
||||
virtual bool Add(const PString &key,const PString &value) ///<向列表中增加一项
|
||||
{
|
||||
return PMap::Add(key,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文本文件中加载
|
||||
*/
|
||||
virtual bool LoadFromTextFile(const OSString &filename,const CharSet &cs=OSCharSet) ///<从文件中加载列表
|
||||
{
|
||||
PStringList sl;
|
||||
|
||||
if(LoadStringListFromTextFile(sl,filename,cs)<=0)
|
||||
return(false);
|
||||
|
||||
ReadData(sl);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存到文本文件中
|
||||
*/
|
||||
template<ByteOrderMask BOM>
|
||||
bool SaveToTextFile(const OSString &filename,const C &gap_ch='\t') ///<保存列表到文件
|
||||
{
|
||||
FileOutputStream fos;
|
||||
EndianTextOutputStream<BOM> tos(&fos);
|
||||
|
||||
if(!fos.CreateTrunc(filename))return(false);
|
||||
|
||||
PString gap_str=PString::charOf(gap_ch);
|
||||
|
||||
int n=this->data_list.GetCount();
|
||||
|
||||
tos.WriteBOM();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
PString f,s;
|
||||
|
||||
if(GetBySerial(n,f,s))
|
||||
tos.WriteLine(f+gap_str+s);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class PList
|
||||
|
||||
using UTF8PList =PList<char >;
|
||||
using UTF16PList=PList<u16char>;
|
||||
using WidePList =PList<wchar_t>;
|
||||
using OSPList =PList<os_char>;
|
||||
}//namespace hgl
|
||||
#endif//HGL_PLIST_INCLUDE
|
42
inc/hgl/util/plist/SavePAttrib.h
Normal file
42
inc/hgl/util/plist/SavePAttrib.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/plist/PAttrib.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
using namespace io;
|
||||
|
||||
/**
|
||||
* 保存PList到文本文件中
|
||||
* @param filename 文件名
|
||||
* @param pa_map 属性列表
|
||||
* @param gap_ch 分隔符
|
||||
* @return 保存的属性数量
|
||||
*/
|
||||
template<typename C,ByteOrderMask BOM> static int SaveToTextFile(const OSString &filename,const PAttribMap<C> &pa_map,const String<C> &gap_ch=String<C>("\t"))
|
||||
{
|
||||
FileOutputStream fos;
|
||||
EndianTextOutputStream<BOM> tos(&fos);
|
||||
|
||||
if(!fos.CreateTrunc(filename))return(-1);
|
||||
|
||||
tos.WriteBOM();
|
||||
|
||||
const int count=pa_map.GetCount();
|
||||
auto **pa_obj=pa_map.GetDataList();
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
tos.WriteString((*pa_obj)->key);
|
||||
tos.WriteString(gap_ch);
|
||||
tos.WriteString((*pa_obj)->value->MakeToString());
|
||||
tos.WriteLineEnd();
|
||||
|
||||
++pa_obj;
|
||||
}
|
||||
|
||||
return(count);
|
||||
}
|
||||
}//namespace hgl
|
@ -1,7 +1,6 @@
|
||||
#ifndef HGL_ALGORITHM_SORT_INCLUDE
|
||||
#define HGL_ALGORITHM_SORT_INCLUDE
|
||||
|
||||
#include<hgl/TypeFunc.h>
|
||||
#include<hgl/CompOperator.h>
|
||||
#include<hgl/type/List.h>
|
||||
#include<string.h>
|
||||
@ -70,103 +69,102 @@ public:
|
||||
virtual bool sort()=0; //排序
|
||||
};//struct SortBase
|
||||
|
||||
//堆排序
|
||||
template<typename T> class HeapSort:public SortBase<T>
|
||||
//堆排序
|
||||
template<typename T> class HeapSort:public SortBase<T>
|
||||
{
|
||||
void isift(int i,int n)
|
||||
{
|
||||
void isift(int i,int n)
|
||||
int j;
|
||||
T temp;
|
||||
|
||||
SortBase<T>::cpy(&temp,SortBase<T>::buffer+i);
|
||||
|
||||
j=2*(i+1)-1;
|
||||
|
||||
while(j<=n)
|
||||
{
|
||||
int j;
|
||||
T temp;
|
||||
if((j<n)&&(SortBase<T>::compare_by_index(j,j+1)<0))j++;
|
||||
|
||||
SortBase<T>::cpy(&temp,SortBase<T>::buffer+i);
|
||||
|
||||
j=2*(i+1)-1;
|
||||
|
||||
while(j<=n)
|
||||
if(SortBase<T>::compare(temp,SortBase<T>::buffer[j])<0)
|
||||
{
|
||||
if((j<n)&&(SortBase<T>::compare_by_index(j,j+1)<0))j++;
|
||||
|
||||
if(SortBase<T>::compare(temp,SortBase<T>::buffer[j])<0)
|
||||
{
|
||||
SortBase<T>::cpy_by_index(i,j);
|
||||
i=j;
|
||||
j=2*(i+1)-1;
|
||||
}
|
||||
else j=n+1;
|
||||
SortBase<T>::cpy_by_index(i,j);
|
||||
i=j;
|
||||
j=2*(i+1)-1;
|
||||
}
|
||||
|
||||
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
|
||||
else j=n+1;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 本类构造函数
|
||||
* @param buf 数据缓冲区
|
||||
* @param n 数据个数
|
||||
* @param c 数据大小比较类
|
||||
*/
|
||||
HeapSort(T *buf,int n,Comparator<T> *c=new Comparator<T>()):SortBase<T>(buf,n,c)
|
||||
{
|
||||
}
|
||||
|
||||
bool sort()
|
||||
{
|
||||
if(!SortBase<T>::buffer||SortBase<T>::number<2||!SortBase<T>::comp)
|
||||
return(false);
|
||||
|
||||
int i;
|
||||
int mm=SortBase<T>::number>>1;
|
||||
|
||||
for(i=mm-1;i>=0;i--)
|
||||
isift(i,SortBase<T>::number-1);
|
||||
|
||||
for(i=SortBase<T>::number-1;i>=1;i--)
|
||||
{
|
||||
SortBase<T>::exchane_by_index(0,i);
|
||||
|
||||
isift(0,i-1);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class HeapSort:public SortBase<T>
|
||||
|
||||
template<typename T>
|
||||
bool Sort(T *data,int count,Comparator<T> *comp=new Comparator<T>())
|
||||
{
|
||||
HeapSort<T> hs(data,count,comp);
|
||||
|
||||
return hs.sort();
|
||||
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Sort(hgl::List<T> &list,Comparator<T> *comp=Comparator<T>())
|
||||
public:
|
||||
|
||||
/**
|
||||
* 本类构造函数
|
||||
* @param buf 数据缓冲区
|
||||
* @param n 数据个数
|
||||
* @param c 数据大小比较类
|
||||
*/
|
||||
HeapSort(T *buf,int n,Comparator<T> *c=new Comparator<T>()):SortBase<T>(buf,n,c)
|
||||
{
|
||||
return Sort(list.GetData(),
|
||||
list.GetCount(),
|
||||
comp);
|
||||
}
|
||||
|
||||
bool sort()
|
||||
{
|
||||
if(!SortBase<T>::buffer||SortBase<T>::number<2||!SortBase<T>::comp)
|
||||
return(false);
|
||||
|
||||
int i;
|
||||
int mm=SortBase<T>::number>>1;
|
||||
|
||||
for(i=mm-1;i>=0;i--)
|
||||
isift(i,SortBase<T>::number-1);
|
||||
|
||||
for(i=SortBase<T>::number-1;i>=1;i--)
|
||||
{
|
||||
SortBase<T>::exchane_by_index(0,i);
|
||||
|
||||
isift(0,i-1);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class HeapSort:public SortBase<T>
|
||||
|
||||
template<typename T>
|
||||
bool Sort(T *data,int count,Comparator<T> *comp=new Comparator<T>())
|
||||
{
|
||||
HeapSort<T> hs(data,count,comp);
|
||||
|
||||
return hs.sort();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Sort(hgl::DataArray<T> &list,Comparator<T> *comp=Comparator<T>())
|
||||
{
|
||||
return Sort(list.GetData(),
|
||||
list.GetCount(),
|
||||
comp);
|
||||
}
|
||||
/*
|
||||
//仅实现模拟虚拟成员函数即可,无需整个类重载
|
||||
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
|
||||
{
|
||||
int r=it1.GetItemID()-it2.GetItemID();
|
||||
//仅实现模拟虚拟成员函数即可,无需整个类重载
|
||||
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
|
||||
{
|
||||
int r=it1.GetItemID()-it2.GetItemID();
|
||||
|
||||
if(r!=0)
|
||||
return r;
|
||||
if(r!=0)
|
||||
return r;
|
||||
|
||||
return it1.count-it2.count;
|
||||
}
|
||||
return it1.count-it2.count;
|
||||
}
|
||||
|
||||
void BagManage::Sort()
|
||||
{
|
||||
Comparator<BagCell> comp_baginfo;
|
||||
void BagManage::Sort()
|
||||
{
|
||||
Comparator<BagCell> comp_baginfo;
|
||||
|
||||
BagCell cell_list[BAG_SLOT_COUNT];
|
||||
BagCell cell_list[BAG_SLOT_COUNT];
|
||||
|
||||
hgl::Sort<BagCell>(cell_list,BAG_SLOT_COUNT,&comp_baginfo);
|
||||
}
|
||||
*/
|
||||
hgl::Sort<BagCell>(cell_list,BAG_SLOT_COUNT,&comp_baginfo);
|
||||
}
|
||||
*/
|
||||
#endif//HGL_ALGORITHM_SORT_INCLUDE
|
||||
|
@ -1,3 +1,4 @@
|
||||
#################################################################################################### XML
|
||||
option(CM_UTIL_SUPPORT_XML "Build XML Parse module." ON)
|
||||
|
||||
if(CM_UTIL_SUPPORT_XML)
|
||||
@ -31,6 +32,8 @@ if(CM_UTIL_SUPPORT_XML)
|
||||
|
||||
endif(CM_UTIL_SUPPORT_XML)
|
||||
|
||||
#################################################################################################### JSON
|
||||
|
||||
option(CM_UTIL_SUPPORT_JSON "Build JSON Parse/make module." ON)
|
||||
|
||||
IF(CM_UTIL_SUPPORT_JSON)
|
||||
@ -42,6 +45,8 @@ IF(CM_UTIL_SUPPORT_JSON)
|
||||
SOURCE_GROUP("JSON" FILES ${JSON_TOOL_SOURCE})
|
||||
ENDIF(CM_UTIL_SUPPORT_JSON)
|
||||
|
||||
#################################################################################################### HASH
|
||||
|
||||
option(CM_UTIL_SUPPORT_HASH "Build HASH module" ON)
|
||||
|
||||
IF(CM_UTIL_SUPPORT_HASH)
|
||||
@ -61,8 +66,28 @@ IF(CM_UTIL_SUPPORT_HASH)
|
||||
SOURCE_GROUP("HASH" FILES ${HASH_HEADER_FILES} ${HASH_SOURCE_FILES})
|
||||
ENDIF(CM_UTIL_SUPPORT_HASH)
|
||||
|
||||
#################################################################################################### CRYPT
|
||||
|
||||
SET(CRYPT_HEADER_FILES ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/Crypt.h)
|
||||
|
||||
SET(CRYPT_SOURCE_FILES crypt/aes.cpp
|
||||
crypt/base64.cpp
|
||||
crypt/Overflow.cpp
|
||||
crypt/rc4.cpp
|
||||
)
|
||||
|
||||
SOURCE_GROUP("Crypt" FILES ${CRYPT_HEADER_FILES} ${CRYPT_SOURCE_FILES})
|
||||
|
||||
####################################################################################################
|
||||
|
||||
SET(CSV_SOURCE ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVFieldSplite.h
|
||||
${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVParse.h
|
||||
${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutput.h
|
||||
${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutputStream.h
|
||||
csv/CSVOutputStream.cpp)
|
||||
|
||||
SOURCE_GROUP("CSV" FILES ${CSV_SOURCE})
|
||||
|
||||
SET(CMD_SOURCE ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/cmd/CmdParse.h
|
||||
cmd/CmdParse.cpp)
|
||||
|
||||
@ -90,9 +115,13 @@ add_cm_library(CMUtil "CM" ${CMD_SOURCE}
|
||||
|
||||
${XML_PARSE_SOURCE}
|
||||
${JSON_TOOL_SOURCE}
|
||||
${CSV_SOURCE}
|
||||
|
||||
${HASH_HEADER_FILES}
|
||||
${HASH_SOURCE_FILES}
|
||||
|
||||
${CRYPT_HEADER_FILES}
|
||||
${CRYPT_SOURCE_FILES}
|
||||
)
|
||||
|
||||
if(CM_UTIL_SUPPORT_XML)
|
||||
|
93
src/crypt/Overflow.cpp
Normal file
93
src/crypt/Overflow.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#include<hgl/type/DataType.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace crypt
|
||||
{
|
||||
/**
|
||||
* 溢出加密
|
||||
* @param target 加密后的数据存放区
|
||||
* @param source 加密前的数据存放区
|
||||
* @param size 数据长度
|
||||
* @param key 密码
|
||||
* @param key_size 密码长度
|
||||
*/
|
||||
void OverflowEncrypt(void *target, void *source, int size, void *key, int key_size)
|
||||
{
|
||||
int n;
|
||||
uint8 *key_p;
|
||||
uint8 *tp, *sp;
|
||||
|
||||
n = key_size;
|
||||
key_p = (uint8 *)key;
|
||||
|
||||
tp = (uint8 *)target;
|
||||
sp = (uint8 *)source;
|
||||
|
||||
while (size--)
|
||||
{
|
||||
uint tmp = (*sp) + (*key_p);
|
||||
|
||||
if (tmp > 0xFF)
|
||||
*tp = tmp - 0x100;
|
||||
else
|
||||
*tp = tmp;
|
||||
|
||||
*key_p ^= *tp;
|
||||
|
||||
tp++;
|
||||
sp++;
|
||||
|
||||
if (--n == 0)
|
||||
{
|
||||
n = key_size;
|
||||
key_p = (uint8 *)key;
|
||||
}
|
||||
else
|
||||
key_p++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 溢出解密
|
||||
* @param target 解密后的数据存放区
|
||||
* @param source 解密前的数据存放区
|
||||
* @param size 数据长度
|
||||
* @param key 密码
|
||||
* @param keysize 密码长度
|
||||
*/
|
||||
void OverflowDecrypt(void *target, void *source, int size, void *key, int key_size)
|
||||
{
|
||||
int n;
|
||||
uint8 *key_p;
|
||||
uint8 *tp, *sp;
|
||||
|
||||
n = key_size;
|
||||
key_p = (uint8 *)key;
|
||||
|
||||
tp = (uint8 *)target;
|
||||
sp = (uint8 *)source;
|
||||
|
||||
while (size--)
|
||||
{
|
||||
if (*key_p > *sp)
|
||||
*tp = *sp + 0x100 - (*key_p);
|
||||
else
|
||||
*tp = *sp - (*key_p);
|
||||
|
||||
*key_p ^= *sp;
|
||||
|
||||
tp++;
|
||||
sp++;
|
||||
|
||||
if (--n == 0)
|
||||
{
|
||||
n = key_size;
|
||||
key_p = (uint8 *)key;
|
||||
}
|
||||
else
|
||||
key_p++;
|
||||
}
|
||||
}
|
||||
}//namespace crypt
|
||||
}//namespace hgl
|
613
src/crypt/aes.cpp
Normal file
613
src/crypt/aes.cpp
Normal file
@ -0,0 +1,613 @@
|
||||
#include<hgl/type/DataType.h>
|
||||
|
||||
using namespace hgl;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct aes_context
|
||||
{
|
||||
int nr; /* number of rounds */
|
||||
uint32 erk[64]; /* encryption round keys */
|
||||
uint32 drk[64]; /* decryption round keys */
|
||||
};
|
||||
/* forward S-box */
|
||||
const uint32 FSb[256] =
|
||||
{
|
||||
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
|
||||
0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
|
||||
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
|
||||
0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
|
||||
0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC,
|
||||
0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
|
||||
0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A,
|
||||
0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
|
||||
0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
|
||||
0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
|
||||
0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B,
|
||||
0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
|
||||
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85,
|
||||
0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
|
||||
0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
|
||||
0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
|
||||
0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17,
|
||||
0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
|
||||
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88,
|
||||
0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
|
||||
0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
|
||||
0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
|
||||
0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9,
|
||||
0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
|
||||
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6,
|
||||
0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
|
||||
0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
|
||||
0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
|
||||
0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94,
|
||||
0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
|
||||
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68,
|
||||
0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
|
||||
};
|
||||
/* forward table */
|
||||
#define FT \
|
||||
\
|
||||
V(C6,63,63,A5), V(F8,7C,7C,84), V(EE,77,77,99), V(F6,7B,7B,8D), \
|
||||
V(FF,F2,F2,0D), V(D6,6B,6B,BD), V(DE,6F,6F,B1), V(91,C5,C5,54), \
|
||||
V(60,30,30,50), V(02,01,01,03), V(CE,67,67,A9), V(56,2B,2B,7D), \
|
||||
V(E7,FE,FE,19), V(B5,D7,D7,62), V(4D,AB,AB,E6), V(EC,76,76,9A), \
|
||||
V(8F,CA,CA,45), V(1F,82,82,9D), V(89,C9,C9,40), V(FA,7D,7D,87), \
|
||||
V(EF,FA,FA,15), V(B2,59,59,EB), V(8E,47,47,C9), V(FB,F0,F0,0B), \
|
||||
V(41,AD,AD,EC), V(B3,D4,D4,67), V(5F,A2,A2,FD), V(45,AF,AF,EA), \
|
||||
V(23,9C,9C,BF), V(53,A4,A4,F7), V(E4,72,72,96), V(9B,C0,C0,5B), \
|
||||
V(75,B7,B7,C2), V(E1,FD,FD,1C), V(3D,93,93,AE), V(4C,26,26,6A), \
|
||||
V(6C,36,36,5A), V(7E,3F,3F,41), V(F5,F7,F7,02), V(83,CC,CC,4F), \
|
||||
V(68,34,34,5C), V(51,A5,A5,F4), V(D1,E5,E5,34), V(F9,F1,F1,08), \
|
||||
V(E2,71,71,93), V(AB,D8,D8,73), V(62,31,31,53), V(2A,15,15,3F), \
|
||||
V(08,04,04,0C), V(95,C7,C7,52), V(46,23,23,65), V(9D,C3,C3,5E), \
|
||||
V(30,18,18,28), V(37,96,96,A1), V(0A,05,05,0F), V(2F,9A,9A,B5), \
|
||||
V(0E,07,07,09), V(24,12,12,36), V(1B,80,80,9B), V(DF,E2,E2,3D), \
|
||||
V(CD,EB,EB,26), V(4E,27,27,69), V(7F,B2,B2,CD), V(EA,75,75,9F), \
|
||||
V(12,09,09,1B), V(1D,83,83,9E), V(58,2C,2C,74), V(34,1A,1A,2E), \
|
||||
V(36,1B,1B,2D), V(DC,6E,6E,B2), V(B4,5A,5A,EE), V(5B,A0,A0,FB), \
|
||||
V(A4,52,52,F6), V(76,3B,3B,4D), V(B7,D6,D6,61), V(7D,B3,B3,CE), \
|
||||
V(52,29,29,7B), V(DD,E3,E3,3E), V(5E,2F,2F,71), V(13,84,84,97), \
|
||||
V(A6,53,53,F5), V(B9,D1,D1,68), V(00,00,00,00), V(C1,ED,ED,2C), \
|
||||
V(40,20,20,60), V(E3,FC,FC,1F), V(79,B1,B1,C8), V(B6,5B,5B,ED), \
|
||||
V(D4,6A,6A,BE), V(8D,CB,CB,46), V(67,BE,BE,D9), V(72,39,39,4B), \
|
||||
V(94,4A,4A,DE), V(98,4C,4C,D4), V(B0,58,58,E8), V(85,CF,CF,4A), \
|
||||
V(BB,D0,D0,6B), V(C5,EF,EF,2A), V(4F,AA,AA,E5), V(ED,FB,FB,16), \
|
||||
V(86,43,43,C5), V(9A,4D,4D,D7), V(66,33,33,55), V(11,85,85,94), \
|
||||
V(8A,45,45,CF), V(E9,F9,F9,10), V(04,02,02,06), V(FE,7F,7F,81), \
|
||||
V(A0,50,50,F0), V(78,3C,3C,44), V(25,9F,9F,BA), V(4B,A8,A8,E3), \
|
||||
V(A2,51,51,F3), V(5D,A3,A3,FE), V(80,40,40,C0), V(05,8F,8F,8A), \
|
||||
V(3F,92,92,AD), V(21,9D,9D,BC), V(70,38,38,48), V(F1,F5,F5,04), \
|
||||
V(63,BC,BC,DF), V(77,B6,B6,C1), V(AF,DA,DA,75), V(42,21,21,63), \
|
||||
V(20,10,10,30), V(E5,FF,FF,1A), V(FD,F3,F3,0E), V(BF,D2,D2,6D), \
|
||||
V(81,CD,CD,4C), V(18,0C,0C,14), V(26,13,13,35), V(C3,EC,EC,2F), \
|
||||
V(BE,5F,5F,E1), V(35,97,97,A2), V(88,44,44,CC), V(2E,17,17,39), \
|
||||
V(93,C4,C4,57), V(55,A7,A7,F2), V(FC,7E,7E,82), V(7A,3D,3D,47), \
|
||||
V(C8,64,64,AC), V(BA,5D,5D,E7), V(32,19,19,2B), V(E6,73,73,95), \
|
||||
V(C0,60,60,A0), V(19,81,81,98), V(9E,4F,4F,D1), V(A3,DC,DC,7F), \
|
||||
V(44,22,22,66), V(54,2A,2A,7E), V(3B,90,90,AB), V(0B,88,88,83), \
|
||||
V(8C,46,46,CA), V(C7,EE,EE,29), V(6B,B8,B8,D3), V(28,14,14,3C), \
|
||||
V(A7,DE,DE,79), V(BC,5E,5E,E2), V(16,0B,0B,1D), V(AD,DB,DB,76), \
|
||||
V(DB,E0,E0,3B), V(64,32,32,56), V(74,3A,3A,4E), V(14,0A,0A,1E), \
|
||||
V(92,49,49,DB), V(0C,06,06,0A), V(48,24,24,6C), V(B8,5C,5C,E4), \
|
||||
V(9F,C2,C2,5D), V(BD,D3,D3,6E), V(43,AC,AC,EF), V(C4,62,62,A6), \
|
||||
V(39,91,91,A8), V(31,95,95,A4), V(D3,E4,E4,37), V(F2,79,79,8B), \
|
||||
V(D5,E7,E7,32), V(8B,C8,C8,43), V(6E,37,37,59), V(DA,6D,6D,B7), \
|
||||
V(01,8D,8D,8C), V(B1,D5,D5,64), V(9C,4E,4E,D2), V(49,A9,A9,E0), \
|
||||
V(D8,6C,6C,B4), V(AC,56,56,FA), V(F3,F4,F4,07), V(CF,EA,EA,25), \
|
||||
V(CA,65,65,AF), V(F4,7A,7A,8E), V(47,AE,AE,E9), V(10,08,08,18), \
|
||||
V(6F,BA,BA,D5), V(F0,78,78,88), V(4A,25,25,6F), V(5C,2E,2E,72), \
|
||||
V(38,1C,1C,24), V(57,A6,A6,F1), V(73,B4,B4,C7), V(97,C6,C6,51), \
|
||||
V(CB,E8,E8,23), V(A1,DD,DD,7C), V(E8,74,74,9C), V(3E,1F,1F,21), \
|
||||
V(96,4B,4B,DD), V(61,BD,BD,DC), V(0D,8B,8B,86), V(0F,8A,8A,85), \
|
||||
V(E0,70,70,90), V(7C,3E,3E,42), V(71,B5,B5,C4), V(CC,66,66,AA), \
|
||||
V(90,48,48,D8), V(06,03,03,05), V(F7,F6,F6,01), V(1C,0E,0E,12), \
|
||||
V(C2,61,61,A3), V(6A,35,35,5F), V(AE,57,57,F9), V(69,B9,B9,D0), \
|
||||
V(17,86,86,91), V(99,C1,C1,58), V(3A,1D,1D,27), V(27,9E,9E,B9), \
|
||||
V(D9,E1,E1,38), V(EB,F8,F8,13), V(2B,98,98,B3), V(22,11,11,33), \
|
||||
V(D2,69,69,BB), V(A9,D9,D9,70), V(07,8E,8E,89), V(33,94,94,A7), \
|
||||
V(2D,9B,9B,B6), V(3C,1E,1E,22), V(15,87,87,92), V(C9,E9,E9,20), \
|
||||
V(87,CE,CE,49), V(AA,55,55,FF), V(50,28,28,78), V(A5,DF,DF,7A), \
|
||||
V(03,8C,8C,8F), V(59,A1,A1,F8), V(09,89,89,80), V(1A,0D,0D,17), \
|
||||
V(65,BF,BF,DA), V(D7,E6,E6,31), V(84,42,42,C6), V(D0,68,68,B8), \
|
||||
V(82,41,41,C3), V(29,99,99,B0), V(5A,2D,2D,77), V(1E,0F,0F,11), \
|
||||
V(7B,B0,B0,CB), V(A8,54,54,FC), V(6D,BB,BB,D6), V(2C,16,16,3A)
|
||||
#define V(a,b,c,d) 0x##a##b##c##d
|
||||
const uint32 FT0[256] = { FT };
|
||||
#undef V
|
||||
#define V(a,b,c,d) 0x##d##a##b##c
|
||||
const uint32 FT1[256] = { FT };
|
||||
#undef V
|
||||
#define V(a,b,c,d) 0x##c##d##a##b
|
||||
const uint32 FT2[256] = { FT };
|
||||
#undef V
|
||||
#define V(a,b,c,d) 0x##b##c##d##a
|
||||
const uint32 FT3[256] = { FT };
|
||||
#undef V
|
||||
/* reverse S-box */
|
||||
const uint32 RSb[256] =
|
||||
{
|
||||
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38,
|
||||
0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
|
||||
0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
|
||||
0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
|
||||
0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D,
|
||||
0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
|
||||
0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2,
|
||||
0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
|
||||
0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
|
||||
0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
|
||||
0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA,
|
||||
0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
|
||||
0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A,
|
||||
0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
|
||||
0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
|
||||
0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
|
||||
0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA,
|
||||
0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
|
||||
0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85,
|
||||
0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
|
||||
0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
|
||||
0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
|
||||
0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20,
|
||||
0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
|
||||
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31,
|
||||
0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
|
||||
0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
|
||||
0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
|
||||
0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0,
|
||||
0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26,
|
||||
0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
|
||||
};
|
||||
/* reverse table */
|
||||
#define RT \
|
||||
\
|
||||
V(51,F4,A7,50), V(7E,41,65,53), V(1A,17,A4,C3), V(3A,27,5E,96), \
|
||||
V(3B,AB,6B,CB), V(1F,9D,45,F1), V(AC,FA,58,AB), V(4B,E3,03,93), \
|
||||
V(20,30,FA,55), V(AD,76,6D,F6), V(88,CC,76,91), V(F5,02,4C,25), \
|
||||
V(4F,E5,D7,FC), V(C5,2A,CB,D7), V(26,35,44,80), V(B5,62,A3,8F), \
|
||||
V(DE,B1,5A,49), V(25,BA,1B,67), V(45,EA,0E,98), V(5D,FE,C0,E1), \
|
||||
V(C3,2F,75,02), V(81,4C,F0,12), V(8D,46,97,A3), V(6B,D3,F9,C6), \
|
||||
V(03,8F,5F,E7), V(15,92,9C,95), V(BF,6D,7A,EB), V(95,52,59,DA), \
|
||||
V(D4,BE,83,2D), V(58,74,21,D3), V(49,E0,69,29), V(8E,C9,C8,44), \
|
||||
V(75,C2,89,6A), V(F4,8E,79,78), V(99,58,3E,6B), V(27,B9,71,DD), \
|
||||
V(BE,E1,4F,B6), V(F0,88,AD,17), V(C9,20,AC,66), V(7D,CE,3A,B4), \
|
||||
V(63,DF,4A,18), V(E5,1A,31,82), V(97,51,33,60), V(62,53,7F,45), \
|
||||
V(B1,64,77,E0), V(BB,6B,AE,84), V(FE,81,A0,1C), V(F9,08,2B,94), \
|
||||
V(70,48,68,58), V(8F,45,FD,19), V(94,DE,6C,87), V(52,7B,F8,B7), \
|
||||
V(AB,73,D3,23), V(72,4B,02,E2), V(E3,1F,8F,57), V(66,55,AB,2A), \
|
||||
V(B2,EB,28,07), V(2F,B5,C2,03), V(86,C5,7B,9A), V(D3,37,08,A5), \
|
||||
V(30,28,87,F2), V(23,BF,A5,B2), V(02,03,6A,BA), V(ED,16,82,5C), \
|
||||
V(8A,CF,1C,2B), V(A7,79,B4,92), V(F3,07,F2,F0), V(4E,69,E2,A1), \
|
||||
V(65,DA,F4,CD), V(06,05,BE,D5), V(D1,34,62,1F), V(C4,A6,FE,8A), \
|
||||
V(34,2E,53,9D), V(A2,F3,55,A0), V(05,8A,E1,32), V(A4,F6,EB,75), \
|
||||
V(0B,83,EC,39), V(40,60,EF,AA), V(5E,71,9F,06), V(BD,6E,10,51), \
|
||||
V(3E,21,8A,F9), V(96,DD,06,3D), V(DD,3E,05,AE), V(4D,E6,BD,46), \
|
||||
V(91,54,8D,B5), V(71,C4,5D,05), V(04,06,D4,6F), V(60,50,15,FF), \
|
||||
V(19,98,FB,24), V(D6,BD,E9,97), V(89,40,43,CC), V(67,D9,9E,77), \
|
||||
V(B0,E8,42,BD), V(07,89,8B,88), V(E7,19,5B,38), V(79,C8,EE,DB), \
|
||||
V(A1,7C,0A,47), V(7C,42,0F,E9), V(F8,84,1E,C9), V(00,00,00,00), \
|
||||
V(09,80,86,83), V(32,2B,ED,48), V(1E,11,70,AC), V(6C,5A,72,4E), \
|
||||
V(FD,0E,FF,FB), V(0F,85,38,56), V(3D,AE,D5,1E), V(36,2D,39,27), \
|
||||
V(0A,0F,D9,64), V(68,5C,A6,21), V(9B,5B,54,D1), V(24,36,2E,3A), \
|
||||
V(0C,0A,67,B1), V(93,57,E7,0F), V(B4,EE,96,D2), V(1B,9B,91,9E), \
|
||||
V(80,C0,C5,4F), V(61,DC,20,A2), V(5A,77,4B,69), V(1C,12,1A,16), \
|
||||
V(E2,93,BA,0A), V(C0,A0,2A,E5), V(3C,22,E0,43), V(12,1B,17,1D), \
|
||||
V(0E,09,0D,0B), V(F2,8B,C7,AD), V(2D,B6,A8,B9), V(14,1E,A9,C8), \
|
||||
V(57,F1,19,85), V(AF,75,07,4C), V(EE,99,DD,BB), V(A3,7F,60,FD), \
|
||||
V(F7,01,26,9F), V(5C,72,F5,BC), V(44,66,3B,C5), V(5B,FB,7E,34), \
|
||||
V(8B,43,29,76), V(CB,23,C6,DC), V(B6,ED,FC,68), V(B8,E4,F1,63), \
|
||||
V(D7,31,DC,CA), V(42,63,85,10), V(13,97,22,40), V(84,C6,11,20), \
|
||||
V(85,4A,24,7D), V(D2,BB,3D,F8), V(AE,F9,32,11), V(C7,29,A1,6D), \
|
||||
V(1D,9E,2F,4B), V(DC,B2,30,F3), V(0D,86,52,EC), V(77,C1,E3,D0), \
|
||||
V(2B,B3,16,6C), V(A9,70,B9,99), V(11,94,48,FA), V(47,E9,64,22), \
|
||||
V(A8,FC,8C,C4), V(A0,F0,3F,1A), V(56,7D,2C,D8), V(22,33,90,EF), \
|
||||
V(87,49,4E,C7), V(D9,38,D1,C1), V(8C,CA,A2,FE), V(98,D4,0B,36), \
|
||||
V(A6,F5,81,CF), V(A5,7A,DE,28), V(DA,B7,8E,26), V(3F,AD,BF,A4), \
|
||||
V(2C,3A,9D,E4), V(50,78,92,0D), V(6A,5F,CC,9B), V(54,7E,46,62), \
|
||||
V(F6,8D,13,C2), V(90,D8,B8,E8), V(2E,39,F7,5E), V(82,C3,AF,F5), \
|
||||
V(9F,5D,80,BE), V(69,D0,93,7C), V(6F,D5,2D,A9), V(CF,25,12,B3), \
|
||||
V(C8,AC,99,3B), V(10,18,7D,A7), V(E8,9C,63,6E), V(DB,3B,BB,7B), \
|
||||
V(CD,26,78,09), V(6E,59,18,F4), V(EC,9A,B7,01), V(83,4F,9A,A8), \
|
||||
V(E6,95,6E,65), V(AA,FF,E6,7E), V(21,BC,CF,08), V(EF,15,E8,E6), \
|
||||
V(BA,E7,9B,D9), V(4A,6F,36,CE), V(EA,9F,09,D4), V(29,B0,7C,D6), \
|
||||
V(31,A4,B2,AF), V(2A,3F,23,31), V(C6,A5,94,30), V(35,A2,66,C0), \
|
||||
V(74,4E,BC,37), V(FC,82,CA,A6), V(E0,90,D0,B0), V(33,A7,D8,15), \
|
||||
V(F1,04,98,4A), V(41,EC,DA,F7), V(7F,CD,50,0E), V(17,91,F6,2F), \
|
||||
V(76,4D,D6,8D), V(43,EF,B0,4D), V(CC,AA,4D,54), V(E4,96,04,DF), \
|
||||
V(9E,D1,B5,E3), V(4C,6A,88,1B), V(C1,2C,1F,B8), V(46,65,51,7F), \
|
||||
V(9D,5E,EA,04), V(01,8C,35,5D), V(FA,87,74,73), V(FB,0B,41,2E), \
|
||||
V(B3,67,1D,5A), V(92,DB,D2,52), V(E9,10,56,33), V(6D,D6,47,13), \
|
||||
V(9A,D7,61,8C), V(37,A1,0C,7A), V(59,F8,14,8E), V(EB,13,3C,89), \
|
||||
V(CE,A9,27,EE), V(B7,61,C9,35), V(E1,1C,E5,ED), V(7A,47,B1,3C), \
|
||||
V(9C,D2,DF,59), V(55,F2,73,3F), V(18,14,CE,79), V(73,C7,37,BF), \
|
||||
V(53,F7,CD,EA), V(5F,FD,AA,5B), V(DF,3D,6F,14), V(78,44,DB,86), \
|
||||
V(CA,AF,F3,81), V(B9,68,C4,3E), V(38,24,34,2C), V(C2,A3,40,5F), \
|
||||
V(16,1D,C3,72), V(BC,E2,25,0C), V(28,3C,49,8B), V(FF,0D,95,41), \
|
||||
V(39,A8,01,71), V(08,0C,B3,DE), V(D8,B4,E4,9C), V(64,56,C1,90), \
|
||||
V(7B,CB,84,61), V(D5,32,B6,70), V(48,6C,5C,74), V(D0,B8,57,42)
|
||||
#define V(a,b,c,d) 0x##a##b##c##d
|
||||
const uint32 RT0[256] = { RT };
|
||||
#undef V
|
||||
#define V(a,b,c,d) 0x##d##a##b##c
|
||||
const uint32 RT1[256] = { RT };
|
||||
#undef V
|
||||
#define V(a,b,c,d) 0x##c##d##a##b
|
||||
const uint32 RT2[256] = { RT };
|
||||
#undef V
|
||||
#define V(a,b,c,d) 0x##b##c##d##a
|
||||
const uint32 RT3[256] = { RT };
|
||||
#undef V
|
||||
/* round constants */
|
||||
const uint32 RCON[10] =
|
||||
{
|
||||
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
||||
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
||||
0x1B000000, 0x36000000
|
||||
};
|
||||
/* platform-independant 32-bit integer manipulation macros */
|
||||
#define GET_UINT32(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint32) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32) (b)[(i) + 3] ); \
|
||||
}
|
||||
#define PUT_UINT32(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (uint8) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (uint8) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (uint8) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (uint8) ( (n) ); \
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// key scheduling routine
|
||||
int aes_set_key( struct aes_context *ctx, unsigned char *key, int nbits )
|
||||
{
|
||||
int i;
|
||||
uint32 *RK;
|
||||
switch( nbits )
|
||||
{
|
||||
case 128: ctx->nr = 10; break;
|
||||
case 192: ctx->nr = 12; break;
|
||||
case 256: ctx->nr = 14; break;
|
||||
default : return( 1 );
|
||||
}
|
||||
RK = ctx->erk;
|
||||
for( i = 0; i < (nbits >> 5); i++ )
|
||||
{
|
||||
GET_UINT32( RK[i], key, i * 4 );
|
||||
}
|
||||
/* setup encryption round keys */
|
||||
switch( nbits )
|
||||
{
|
||||
case 128:
|
||||
for( i = 0; i < 10; i++, RK += 4 )
|
||||
{
|
||||
RK[4] = RK[0] ^ RCON[i] ^
|
||||
( FSb[ (unsigned char) ( RK[3] >> 16 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( RK[3] >> 8 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( RK[3] ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( RK[3] >> 24 ) ] );
|
||||
RK[5] = RK[1] ^ RK[4];
|
||||
RK[6] = RK[2] ^ RK[5];
|
||||
RK[7] = RK[3] ^ RK[6];
|
||||
}
|
||||
break;
|
||||
case 192:
|
||||
for( i = 0; i < 8; i++, RK += 6 )
|
||||
{
|
||||
RK[6] = RK[0] ^ RCON[i] ^
|
||||
( FSb[ (unsigned char) ( RK[5] >> 16 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( RK[5] >> 8 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( RK[5] ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( RK[5] >> 24 ) ] );
|
||||
RK[7] = RK[1] ^ RK[6];
|
||||
RK[8] = RK[2] ^ RK[7];
|
||||
RK[9] = RK[3] ^ RK[8];
|
||||
RK[10] = RK[4] ^ RK[9];
|
||||
RK[11] = RK[5] ^ RK[10];
|
||||
}
|
||||
break;
|
||||
case 256:
|
||||
for( i = 0; i < 7; i++, RK += 8 )
|
||||
{
|
||||
RK[8] = RK[0] ^ RCON[i] ^
|
||||
( FSb[ (unsigned char) ( RK[7] >> 16 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( RK[7] >> 8 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( RK[7] ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( RK[7] >> 24 ) ] );
|
||||
RK[9] = RK[1] ^ RK[8];
|
||||
RK[10] = RK[2] ^ RK[9];
|
||||
RK[11] = RK[3] ^ RK[10];
|
||||
RK[12] = RK[4] ^
|
||||
( FSb[ (unsigned char) ( RK[11] >> 24 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( RK[11] >> 16 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( RK[11] >> 8 ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( RK[11] ) ] );
|
||||
RK[13] = RK[5] ^ RK[12];
|
||||
RK[14] = RK[6] ^ RK[13];
|
||||
RK[15] = RK[7] ^ RK[14];
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* setup decryption round keys */
|
||||
for( i = 0; i <= ctx->nr; i++ )
|
||||
{
|
||||
ctx->drk[i * 4 ] = ctx->erk[( ctx->nr - i ) * 4 ];
|
||||
ctx->drk[i * 4 + 1] = ctx->erk[( ctx->nr - i ) * 4 + 1];
|
||||
ctx->drk[i * 4 + 2] = ctx->erk[( ctx->nr - i ) * 4 + 2];
|
||||
ctx->drk[i * 4 + 3] = ctx->erk[( ctx->nr - i ) * 4 + 3];
|
||||
}
|
||||
for( i = 1, RK = ctx->drk + 4; i < ctx->nr; i++, RK += 4 )
|
||||
{
|
||||
RK[0] = RT0[ FSb[ (unsigned char) ( RK[0] >> 24 ) ] ] ^
|
||||
RT1[ FSb[ (unsigned char) ( RK[0] >> 16 ) ] ] ^
|
||||
RT2[ FSb[ (unsigned char) ( RK[0] >> 8 ) ] ] ^
|
||||
RT3[ FSb[ (unsigned char) ( RK[0] ) ] ];
|
||||
RK[1] = RT0[ FSb[ (unsigned char) ( RK[1] >> 24 ) ] ] ^
|
||||
RT1[ FSb[ (unsigned char) ( RK[1] >> 16 ) ] ] ^
|
||||
RT2[ FSb[ (unsigned char) ( RK[1] >> 8 ) ] ] ^
|
||||
RT3[ FSb[ (unsigned char) ( RK[1] ) ] ];
|
||||
RK[2] = RT0[ FSb[ (unsigned char) ( RK[2] >> 24 ) ] ] ^
|
||||
RT1[ FSb[ (unsigned char) ( RK[2] >> 16 ) ] ] ^
|
||||
RT2[ FSb[ (unsigned char) ( RK[2] >> 8 ) ] ] ^
|
||||
RT3[ FSb[ (unsigned char) ( RK[2] ) ] ];
|
||||
RK[3] = RT0[ FSb[ (unsigned char) ( RK[3] >> 24 ) ] ] ^
|
||||
RT1[ FSb[ (unsigned char) ( RK[3] >> 16 ) ] ] ^
|
||||
RT2[ FSb[ (unsigned char) ( RK[3] >> 8 ) ] ] ^
|
||||
RT3[ FSb[ (unsigned char) ( RK[3] ) ] ];
|
||||
}
|
||||
return( 0 );
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// 128-bit block encryption routine
|
||||
void aes_encrypt( struct aes_context *ctx, unsigned char data[16] )
|
||||
{
|
||||
uint32 *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
|
||||
RK = ctx->erk;
|
||||
GET_UINT32( X0, data, 0 ); X0 ^= RK[0];
|
||||
GET_UINT32( X1, data, 4 ); X1 ^= RK[1];
|
||||
GET_UINT32( X2, data, 8 ); X2 ^= RK[2];
|
||||
GET_UINT32( X3, data, 12 ); X3 ^= RK[3];
|
||||
#define FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
|
||||
{ \
|
||||
RK += 4; \
|
||||
\
|
||||
X0 = RK[0] ^ FT0[ (uint8) ( Y0 >> 24 ) ] ^ \
|
||||
FT1[ (uint8) ( Y1 >> 16 ) ] ^ \
|
||||
FT2[ (uint8) ( Y2 >> 8 ) ] ^ \
|
||||
FT3[ (uint8) ( Y3 ) ]; \
|
||||
\
|
||||
X1 = RK[1] ^ FT0[ (uint8) ( Y1 >> 24 ) ] ^ \
|
||||
FT1[ (uint8) ( Y2 >> 16 ) ] ^ \
|
||||
FT2[ (uint8) ( Y3 >> 8 ) ] ^ \
|
||||
FT3[ (uint8) ( Y0 ) ]; \
|
||||
\
|
||||
X2 = RK[2] ^ FT0[ (uint8) ( Y2 >> 24 ) ] ^ \
|
||||
FT1[ (uint8) ( Y3 >> 16 ) ] ^ \
|
||||
FT2[ (uint8) ( Y0 >> 8 ) ] ^ \
|
||||
FT3[ (uint8) ( Y1 ) ]; \
|
||||
\
|
||||
X3 = RK[3] ^ FT0[ (uint8) ( Y3 >> 24 ) ] ^ \
|
||||
FT1[ (uint8) ( Y0 >> 16 ) ] ^ \
|
||||
FT2[ (uint8) ( Y1 >> 8 ) ] ^ \
|
||||
FT3[ (uint8) ( Y2 ) ]; \
|
||||
}
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 1 */
|
||||
FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 2 */
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 3 */
|
||||
FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 4 */
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 5 */
|
||||
FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 6 */
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 7 */
|
||||
FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 8 */
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 9 */
|
||||
if( ctx->nr > 10 )
|
||||
{
|
||||
FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 10 */
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 11 */
|
||||
}
|
||||
if( ctx->nr > 12 )
|
||||
{
|
||||
FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 12 */
|
||||
FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 13 */
|
||||
}
|
||||
/* last round */
|
||||
RK += 4;
|
||||
X0 = RK[0] ^ ( FSb[ (unsigned char) ( Y0 >> 24 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( Y1 >> 16 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( Y2 >> 8 ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( Y3 ) ] );
|
||||
X1 = RK[1] ^ ( FSb[ (unsigned char) ( Y1 >> 24 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( Y2 >> 16 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( Y3 >> 8 ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( Y0 ) ] );
|
||||
X2 = RK[2] ^ ( FSb[ (unsigned char) ( Y2 >> 24 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( Y3 >> 16 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( Y0 >> 8 ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( Y1 ) ] );
|
||||
X3 = RK[3] ^ ( FSb[ (unsigned char) ( Y3 >> 24 ) ] << 24 ) ^
|
||||
( FSb[ (unsigned char) ( Y0 >> 16 ) ] << 16 ) ^
|
||||
( FSb[ (unsigned char) ( Y1 >> 8 ) ] << 8 ) ^
|
||||
( FSb[ (unsigned char) ( Y2 ) ] );
|
||||
PUT_UINT32( X0, data, 0 );
|
||||
PUT_UINT32( X1, data, 4 );
|
||||
PUT_UINT32( X2, data, 8 );
|
||||
PUT_UINT32( X3, data, 12 );
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// 128-bit block decryption routine
|
||||
void aes_decrypt( struct aes_context *ctx, unsigned char data[16] )
|
||||
{
|
||||
uint32 *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
|
||||
RK = ctx->drk;
|
||||
GET_UINT32( X0, data, 0 ); X0 ^= RK[0];
|
||||
GET_UINT32( X1, data, 4 ); X1 ^= RK[1];
|
||||
GET_UINT32( X2, data, 8 ); X2 ^= RK[2];
|
||||
GET_UINT32( X3, data, 12 ); X3 ^= RK[3];
|
||||
#define RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
|
||||
{ \
|
||||
RK += 4; \
|
||||
\
|
||||
X0 = RK[0] ^ RT0[ (uint8) ( Y0 >> 24 ) ] ^ \
|
||||
RT1[ (uint8) ( Y3 >> 16 ) ] ^ \
|
||||
RT2[ (uint8) ( Y2 >> 8 ) ] ^ \
|
||||
RT3[ (uint8) ( Y1 ) ]; \
|
||||
\
|
||||
X1 = RK[1] ^ RT0[ (uint8) ( Y1 >> 24 ) ] ^ \
|
||||
RT1[ (uint8) ( Y0 >> 16 ) ] ^ \
|
||||
RT2[ (uint8) ( Y3 >> 8 ) ] ^ \
|
||||
RT3[ (uint8) ( Y2 ) ]; \
|
||||
\
|
||||
X2 = RK[2] ^ RT0[ (uint8) ( Y2 >> 24 ) ] ^ \
|
||||
RT1[ (uint8) ( Y1 >> 16 ) ] ^ \
|
||||
RT2[ (uint8) ( Y0 >> 8 ) ] ^ \
|
||||
RT3[ (uint8) ( Y3 ) ]; \
|
||||
\
|
||||
X3 = RK[3] ^ RT0[ (uint8) ( Y3 >> 24 ) ] ^ \
|
||||
RT1[ (uint8) ( Y2 >> 16 ) ] ^ \
|
||||
RT2[ (uint8) ( Y1 >> 8 ) ] ^ \
|
||||
RT3[ (uint8) ( Y0 ) ]; \
|
||||
}
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 1 */
|
||||
RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 2 */
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 3 */
|
||||
RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 4 */
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 5 */
|
||||
RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 6 */
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 7 */
|
||||
RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 8 */
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 9 */
|
||||
if( ctx->nr > 10 )
|
||||
{
|
||||
RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 10 */
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 11 */
|
||||
}
|
||||
if( ctx->nr > 12 )
|
||||
{
|
||||
RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 12 */
|
||||
RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 13 */
|
||||
}
|
||||
/* last round */
|
||||
RK += 4;
|
||||
X0 = RK[0] ^ ( RSb[ (unsigned char) ( Y0 >> 24 ) ] << 24 ) ^
|
||||
( RSb[ (unsigned char) ( Y3 >> 16 ) ] << 16 ) ^
|
||||
( RSb[ (unsigned char) ( Y2 >> 8 ) ] << 8 ) ^
|
||||
( RSb[ (unsigned char) ( Y1 ) ] );
|
||||
X1 = RK[1] ^ ( RSb[ (unsigned char) ( Y1 >> 24 ) ] << 24 ) ^
|
||||
( RSb[ (unsigned char) ( Y0 >> 16 ) ] << 16 ) ^
|
||||
( RSb[ (unsigned char) ( Y3 >> 8 ) ] << 8 ) ^
|
||||
( RSb[ (unsigned char) ( Y2 ) ] );
|
||||
X2 = RK[2] ^ ( RSb[ (unsigned char) ( Y2 >> 24 ) ] << 24 ) ^
|
||||
( RSb[ (unsigned char) ( Y1 >> 16 ) ] << 16 ) ^
|
||||
( RSb[ (unsigned char) ( Y0 >> 8 ) ] << 8 ) ^
|
||||
( RSb[ (unsigned char) ( Y3 ) ] );
|
||||
X3 = RK[3] ^ ( RSb[ (unsigned char) ( Y3 >> 24 ) ] << 24 ) ^
|
||||
( RSb[ (unsigned char) ( Y2 >> 16 ) ] << 16 ) ^
|
||||
( RSb[ (unsigned char) ( Y1 >> 8 ) ] << 8 ) ^
|
||||
( RSb[ (unsigned char) ( Y0 ) ] );
|
||||
PUT_UINT32( X0, data, 0 );
|
||||
PUT_UINT32( X1, data, 4 );
|
||||
PUT_UINT32( X2, data, 8 );
|
||||
PUT_UINT32( X3, data, 12 );
|
||||
}
|
||||
}//namespace
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace crypt
|
||||
{
|
||||
/**
|
||||
* AES算法加密函数
|
||||
* @param data 要加密的数据
|
||||
* @param key 密码
|
||||
* @param keysize 密码位数(仅为128,192,256)
|
||||
*/
|
||||
void AesEncrypt(uint8 data[16], uint8 *key, int keysize)
|
||||
{
|
||||
aes_context ac;
|
||||
aes_set_key(&ac, key, keysize);
|
||||
aes_encrypt(&ac, data);
|
||||
}
|
||||
/**
|
||||
* AES算法解密函数
|
||||
* @param data 要加密的数据
|
||||
* @param key 密码
|
||||
* @param keysize 密码位数(仅为128,192,256)
|
||||
*/
|
||||
void AesDecrypt(uint8 data[16], uint8 *key, int keysize)
|
||||
{
|
||||
aes_context ac;
|
||||
aes_set_key(&ac, key, keysize);
|
||||
aes_decrypt(&ac, data);
|
||||
}
|
||||
}//namespace crypt
|
||||
}//namespace hgl
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/*
|
||||
#ifdef TEST
|
||||
#include <BaseString.h>
|
||||
#include <stdio.h>
|
||||
// those are the standard FIPS 197 test vectors
|
||||
static unsigned char KEYs[3][32] =
|
||||
{
|
||||
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
|
||||
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" \
|
||||
"\x10\x11\x12\x13\x14\x15\x16\x17",
|
||||
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" \
|
||||
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
|
||||
};
|
||||
static unsigned char PT[16] =
|
||||
"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF";
|
||||
static unsigned char CTs[3][16] =
|
||||
{
|
||||
"\x69\xC4\xE0\xD8\x6A\x7B\x04\x30\xD8\xCD\xB7\x80\x70\xB4\xC5\x5A",
|
||||
"\xDD\xA9\x7C\xA4\x86\x4C\xDF\xE0\x6E\xAF\x70\xA0\xEC\x0D\x71\x91",
|
||||
"\x8E\xA2\xB7\xCA\x51\x67\x45\xBF\xEA\xFC\x49\x90\x4B\x49\x60\x89"
|
||||
};
|
||||
int aes_set_key( struct aes_context *ctx, unsigned char *key, int nbits );
|
||||
void aes_encrypt( struct aes_context *ctx, unsigned char data[16] );
|
||||
void aes_decrypt( struct aes_context *ctx, unsigned char data[16] );
|
||||
int main( void )
|
||||
{
|
||||
int i;
|
||||
struct aes_context ctx;
|
||||
unsigned char data[16];
|
||||
for( i = 0; i < 3; i++ )
|
||||
{
|
||||
memcpy( data, PT, 16 );
|
||||
aes_set_key( &ctx, KEYs[i], 128 + i * 64);
|
||||
aes_encrypt( &ctx, data );
|
||||
printf( "encryption test %d ", i + 1 );
|
||||
if( ! memcmp( data, CTs[i], 16 ) )
|
||||
{
|
||||
printf( "passed\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
printf( "failed\n" );
|
||||
return( 1 );
|
||||
}
|
||||
}
|
||||
for( i = 0; i < 3; i++ )
|
||||
{
|
||||
memcpy( data, CTs[i], 16 );
|
||||
aes_set_key( &ctx, KEYs[i], 128 + i * 64);
|
||||
aes_decrypt( &ctx, data );
|
||||
printf( "decryption test %d ", i + 1 );
|
||||
if( ! memcmp( data, PT, 16 ) )
|
||||
{
|
||||
printf( "passed\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
printf( "failed\n" );
|
||||
return( 1 );
|
||||
}
|
||||
}
|
||||
return( 0 );
|
||||
}
|
||||
#endif*/
|
130
src/crypt/base64.cpp
Normal file
130
src/crypt/base64.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/type/StrChar.h>
|
||||
#include<hgl/io/OutputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace crypt
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr uchar base64_chars[]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
const uchar base64_char_find(const uchar ch)
|
||||
{
|
||||
for(uchar i=0;i<sizeof(base64_chars);i++)
|
||||
if(base64_chars[i]==ch)
|
||||
return i;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}//namespace
|
||||
|
||||
bool base64_encode(io::OutputStream *os,const uchar *input, size_t len)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
uchar char_array_3[3];
|
||||
uchar char_array_4[4];
|
||||
uchar out[4];
|
||||
|
||||
while (len--)
|
||||
{
|
||||
char_array_3[i++] = *(input++);
|
||||
|
||||
if (i != 3)
|
||||
continue;
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
|
||||
((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
|
||||
((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for(i = 0; i <4 ; i++)
|
||||
out[i]=base64_chars[char_array_4[i]];
|
||||
|
||||
if(os->Write(out,4)!=4)
|
||||
return(false);
|
||||
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (i)
|
||||
{
|
||||
for(j = i; j < 3; j++)
|
||||
char_array_3[j] = '\0';
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
|
||||
((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
|
||||
((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for (j = 0; j < i + 1; j++)
|
||||
out[j]=base64_chars[char_array_4[j]];
|
||||
|
||||
if(os->Write(out,i+1)!=i+1)
|
||||
return(false);
|
||||
|
||||
for(j=0;j<3-i;j++)
|
||||
out[j]='=';
|
||||
|
||||
if(os->Write(out,3-i)!=3-i)
|
||||
return(false);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool base64_decode(io::OutputStream *os,const uchar *input,size_t in_len)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int in_ = 0;
|
||||
uchar char_array_4[4], char_array_3[3];
|
||||
|
||||
while (in_len-- && ( input[in_] != '=') && isbase64(input[in_]))
|
||||
{
|
||||
char_array_4[i++] = input[in_]; in_++;
|
||||
|
||||
if (i ==4)
|
||||
{
|
||||
for (i = 0; i <4; i++)
|
||||
char_array_4[i] = base64_char_find(char_array_4[i]);
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
if(os->Write(char_array_3,3)!=3)
|
||||
return(false);
|
||||
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i)
|
||||
{
|
||||
for (j = i; j <4; j++)
|
||||
char_array_4[j] = 0;
|
||||
|
||||
for (j = 0; j <4; j++)
|
||||
char_array_4[j] = base64_char_find(char_array_4[j]);
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
if(os->Write(char_array_3,i-1)!=i-1)
|
||||
return(false);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
}//namespace crypt
|
||||
}//namespace hgl
|
412
src/crypt/ecc_arm.inc
Normal file
412
src/crypt/ecc_arm.inc
Normal file
@ -0,0 +1,412 @@
|
||||
#if !asm_add
|
||||
static uint32_t vli_add(uint32_t *p_result, uint32_t *p_left, uint32_t *p_right)
|
||||
{
|
||||
uint32_t l_counter = uECC_WORDS;
|
||||
uint32_t l_carry = 0; /* carry = 0 initially */
|
||||
uint32_t l_left;
|
||||
uint32_t l_right;
|
||||
|
||||
__asm__ volatile (
|
||||
".syntax unified \n\t"
|
||||
"1: \n\t"
|
||||
"ldmia %[lptr]!, {%[left]} \n\t" /* Load left word. */
|
||||
"ldmia %[rptr]!, {%[right]} \n\t" /* Load right word. */
|
||||
"lsrs %[carry], #1 \n\t" /* Set up carry flag (l_carry = 0 after this). */
|
||||
"adcs %[left], %[right] \n\t" /* Add with carry. */
|
||||
"adcs %[carry], %[carry] \n\t" /* Store carry bit in l_carry. */
|
||||
"stmia %[dptr]!, {%[left]} \n\t" /* Store result word. */
|
||||
"subs %[ctr], #1 \n\t" /* Decrement index. */
|
||||
"bne 1b \n\t" /* Loop until index == 0. */
|
||||
#if (uECC_PLATFORM != uECC_arm_thumb2)
|
||||
".syntax divided \n\t"
|
||||
#endif
|
||||
#if (uECC_PLATFORM == uECC_arm_thumb)
|
||||
: [dptr] "+l" (p_result), [lptr] "+l" (p_left), [rptr] "+l" (p_right),
|
||||
[ctr] "+l" (l_counter), [carry] "+l" (l_carry), [left] "=l" (l_left), [right] "=l" (l_right)
|
||||
#else
|
||||
: [dptr] "+r" (p_result), [lptr] "+r" (p_left), [rptr] "+r" (p_right),
|
||||
[ctr] "+r" (l_counter), [carry] "+r" (l_carry), [left] "=r" (l_left), [right] "=r" (l_right)
|
||||
#endif
|
||||
:
|
||||
: "cc", "memory"
|
||||
);
|
||||
return l_carry;
|
||||
}
|
||||
#define asm_add 1
|
||||
#endif
|
||||
|
||||
#if !asm_sub
|
||||
static uint32_t vli_sub(uint32_t *p_result, uint32_t *p_left, uint32_t *p_right)
|
||||
{
|
||||
uint32_t l_counter = uECC_WORDS;
|
||||
uint32_t l_carry = 1; /* carry = 1 initially (means don't borrow) */
|
||||
uint32_t l_left;
|
||||
uint32_t l_right;
|
||||
|
||||
__asm__ volatile (
|
||||
".syntax unified \n\t"
|
||||
"1: \n\t"
|
||||
"ldmia %[lptr]!, {%[left]} \n\t" /* Load left word. */
|
||||
"ldmia %[rptr]!, {%[right]} \n\t" /* Load right word. */
|
||||
"lsrs %[carry], #1 \n\t" /* Set up carry flag (l_carry = 0 after this). */
|
||||
"sbcs %[left], %[right] \n\t" /* Subtract with borrow. */
|
||||
"adcs %[carry], %[carry] \n\t" /* Store carry bit in l_carry. */
|
||||
"stmia %[dptr]!, {%[left]} \n\t" /* Store result word. */
|
||||
"subs %[ctr], #1 \n\t" /* Decrement index. */
|
||||
"bne 1b \n\t" /* Loop until index == 0. */
|
||||
#if (uECC_PLATFORM != uECC_arm_thumb2)
|
||||
".syntax divided \n\t"
|
||||
#endif
|
||||
#if (uECC_PLATFORM == uECC_arm_thumb)
|
||||
: [dptr] "+l" (p_result), [lptr] "+l" (p_left), [rptr] "+l" (p_right),
|
||||
[ctr] "+l" (l_counter), [carry] "+l" (l_carry), [left] "=l" (l_left), [right] "=l" (l_right)
|
||||
#else
|
||||
: [dptr] "+r" (p_result), [lptr] "+r" (p_left), [rptr] "+r" (p_right),
|
||||
[ctr] "+r" (l_counter), [carry] "+r" (l_carry), [left] "=r" (l_left), [right] "=r" (l_right)
|
||||
#endif
|
||||
:
|
||||
: "cc", "memory"
|
||||
);
|
||||
return !l_carry;
|
||||
}
|
||||
#define asm_sub 1
|
||||
#endif
|
||||
|
||||
#if !asm_mult
|
||||
static void vli_mult(uint32_t *p_result, uint32_t *p_left, uint32_t *p_right)
|
||||
{
|
||||
#if (uECC_PLATFORM != uECC_arm_thumb)
|
||||
uint32_t c0 = 0;
|
||||
uint32_t c1 = 0;
|
||||
uint32_t c2 = 0;
|
||||
uint32_t k = 0;
|
||||
uint32_t i;
|
||||
uint32_t t0, t1;
|
||||
|
||||
__asm__ volatile (
|
||||
".syntax unified \n\t"
|
||||
|
||||
"1: \n\t" /* outer loop (k < uECC_WORDS) */
|
||||
"movs %[i], #0 \n\t" /* i = 0 */
|
||||
"b 3f \n\t"
|
||||
|
||||
"2: \n\t" /* outer loop (k >= uECC_WORDS) */
|
||||
"movs %[i], %[k] \n\t" /* i = k */
|
||||
"subs %[i], %[eccdm1] \n\t" /* i = k - (uECC_WORDS - 1) (times 4) */
|
||||
|
||||
"3: \n\t" /* inner loop */
|
||||
"subs %[t0], %[k], %[i] \n\t" /* t0 = k-i */
|
||||
|
||||
"ldr %[t1], [%[right], %[t0]] \n\t" /* t1 = p_right[k-i] */
|
||||
"ldr %[t0], [%[left], %[i]] \n\t" /* t0 = p_left[i] */
|
||||
|
||||
"umull %[t0], %[t1], %[t0], %[t1] \n\t" /* (t0, t1) = p_left[i] * p_right[k-i] */
|
||||
|
||||
"adds %[c0], %[t0] \n\t" /* add low word to c0 */
|
||||
"adcs %[c1], %[t1] \n\t" /* add high word to c1, including carry */
|
||||
"adcs %[c2], #0 \n\t" /* add carry to c2 */
|
||||
|
||||
"adds %[i], #4 \n\t" /* i += 4 */
|
||||
"cmp %[i], %[eccd] \n\t" /* i < uECC_WORDS (times 4)? */
|
||||
"bge 4f \n\t" /* if not, exit the loop */
|
||||
"cmp %[i], %[k] \n\t" /* i <= k? */
|
||||
"ble 3b \n\t" /* if so, continue looping */
|
||||
|
||||
"4: \n\t" /* end inner loop */
|
||||
|
||||
"str %[c0], [%[result], %[k]] \n\t" /* p_result[k] = c0 */
|
||||
"mov %[c0], %[c1] \n\t" /* c0 = c1 */
|
||||
"mov %[c1], %[c2] \n\t" /* c1 = c2 */
|
||||
"movs %[c2], #0 \n\t" /* c2 = 0 */
|
||||
"adds %[k], #4 \n\t" /* k += 4 */
|
||||
"cmp %[k], %[eccd] \n\t" /* k < uECC_WORDS (times 4) ? */
|
||||
"blt 1b \n\t" /* if not, loop back, start with i = 0 */
|
||||
"cmp %[k], %[eccd2m1] \n\t" /* k < uECC_WORDS * 2 - 1 (times 4) ? */
|
||||
"blt 2b \n\t" /* if not, loop back, start with i = (k+1) - uECC_WORDS */
|
||||
/* end outer loop */
|
||||
|
||||
"str %[c0], [%[result], %[k]] \n\t" /* p_result[uECC_WORDS * 2 - 1] = c0 */
|
||||
#if (uECC_PLATFORM != uECC_arm_thumb2)
|
||||
".syntax divided \n\t"
|
||||
#endif
|
||||
: [c0] "+r" (c0), [c1] "+r" (c1), [c2] "+r" (c2), [k] "+r" (k), [i] "=&r" (i), [t0] "=&r" (t0), [t1] "=&r" (t1)
|
||||
: [result] "r" (p_result), [left] "r" (p_left), [right] "r" (p_right),
|
||||
[eccd] "I" (uECC_WORDS * 4), [eccdm1] "I" ((uECC_WORDS-1) * 4), [eccd2m1] "I" ((uECC_WORDS * 2 - 1) * 4)
|
||||
: "cc", "memory"
|
||||
);
|
||||
|
||||
#else /* Thumb-1 */
|
||||
|
||||
register uint32_t *r0 __asm__("r0") = p_result;
|
||||
register uint32_t *r1 __asm__("r1") = p_left;
|
||||
register uint32_t *r2 __asm__("r2") = p_right;
|
||||
|
||||
__asm__ volatile (
|
||||
".syntax unified \n\t"
|
||||
"movs r3, #0 \n\t" /* c0 = 0 */
|
||||
"movs r4, #0 \n\t" /* c1 = 0 */
|
||||
"movs r5, #0 \n\t" /* c2 = 0 */
|
||||
"movs r6, #0 \n\t" /* k = 0 */
|
||||
|
||||
"push {r0} \n\t" /* keep p_result on the stack */
|
||||
|
||||
"1: \n\t" /* outer loop (k < uECC_WORDS) */
|
||||
"movs r7, #0 \n\t" /* r7 = i = 0 */
|
||||
"b 3f \n\t"
|
||||
|
||||
"2: \n\t" /* outer loop (k >= uECC_WORDS) */
|
||||
"movs r7, r6 \n\t" /* r7 = k */
|
||||
"subs r7, %[eccdm1] \n\t" /* r7 = i = k - (uECC_WORDS - 1) (times 4) */
|
||||
|
||||
"3: \n\t" /* inner loop */
|
||||
"push {r3, r4, r5, r6} \n\t" /* push things, r3 (c0) is at the top of stack. */
|
||||
"subs r0, r6, r7 \n\t" /* r0 = k-i */
|
||||
|
||||
"ldr r4, [r2, r0] \n\t" /* r4 = p_right[k-i] */
|
||||
"ldr r0, [r1, r7] \n\t" /* r0 = p_left[i] */
|
||||
|
||||
"lsrs r3, r0, #16 \n\t" /* r3 = a1 */
|
||||
"uxth r0, r0 \n\t" /* r0 = a0 */
|
||||
|
||||
"lsrs r5, r4, #16 \n\t" /* r5 = b1 */
|
||||
"uxth r4, r4 \n\t" /* r4 = b0 */
|
||||
|
||||
"movs r6, r3 \n\t" /* r6 = a1 */
|
||||
"muls r6, r5, r6 \n\t" /* r6 = a1*b1 */
|
||||
"muls r3, r4, r3 \n\t" /* r3 = b0*a1 */
|
||||
"muls r5, r0, r5 \n\t" /* r5 = a0*b1 */
|
||||
"muls r0, r4, r0 \n\t" /* r0 = a0*b0 */
|
||||
|
||||
"movs r4, #0 \n\t" /* r4 = 0 */
|
||||
"adds r3, r5 \n\t" /* r3 = b0*a1 + a0*b1 */
|
||||
"adcs r4, r4 \n\t" /* r4 = carry */
|
||||
"lsls r4, #16 \n\t" /* r4 = carry << 16 */
|
||||
"adds r6, r4 \n\t" /* r6 = a1*b1 + carry */
|
||||
|
||||
"lsls r4, r3, #16 \n\t" /* r4 = (b0*a1 + a0*b1) << 16 */
|
||||
"lsrs r3, #16 \n\t" /* r3 = (b0*a1 + a0*b1) >> 16 */
|
||||
"adds r0, r4 \n\t" /* r0 = low word = a0*b0 + ((b0*a1 + a0*b1) << 16) */
|
||||
"adcs r6, r3 \n\t" /* r6 = high word = a1*b1 + carry + ((b0*a1 + a0*b1) >> 16) */
|
||||
|
||||
"pop {r3, r4, r5} \n\t" /* r3 = c0, r4 = c1, r5 = c2 */
|
||||
"adds r3, r0 \n\t" /* add low word to c0 */
|
||||
"adcs r4, r6 \n\t" /* add high word to c1, including carry */
|
||||
"movs r0, #0 \n\t" /* r0 = 0 (does not affect carry bit) */
|
||||
"adcs r5, r0 \n\t" /* add carry to c2 */
|
||||
|
||||
"pop {r6} \n\t" /* r6 = k */
|
||||
|
||||
"adds r7, #4 \n\t" /* i += 4 */
|
||||
"cmp r7, %[eccd] \n\t" /* i < uECC_WORDS (times 4)? */
|
||||
"bge 4f \n\t" /* if not, exit the loop */
|
||||
"cmp r7, r6 \n\t" /* i <= k? */
|
||||
"ble 3b \n\t" /* if so, continue looping */
|
||||
|
||||
"4: \n\t" /* end inner loop */
|
||||
|
||||
"ldr r0, [sp, #0] \n\t" /* r0 = p_result */
|
||||
|
||||
"str r3, [r0, r6] \n\t" /* p_result[k] = c0 */
|
||||
"mov r3, r4 \n\t" /* c0 = c1 */
|
||||
"mov r4, r5 \n\t" /* c1 = c2 */
|
||||
"movs r5, #0 \n\t" /* c2 = 0 */
|
||||
"adds r6, #4 \n\t" /* k += 4 */
|
||||
"cmp r6, %[eccd] \n\t" /* k < uECC_WORDS (times 4) ? */
|
||||
"blt 1b \n\t" /* if not, loop back, start with i = 0 */
|
||||
"cmp r6, %[eccd2m1] \n\t" /* k < uECC_WORDS * 2 - 1 (times 4) ? */
|
||||
"blt 2b \n\t" /* if not, loop back, start with i = (k+1) - uECC_WORDS */
|
||||
/* end outer loop */
|
||||
|
||||
"str r3, [r0, r6] \n\t" /* p_result[uECC_WORDS * 2 - 1] = c0 */
|
||||
"pop {r0} \n\t" /* pop p_result off the stack */
|
||||
|
||||
".syntax divided \n\t"
|
||||
:
|
||||
: [r0] "l" (r0), [r1] "l" (r1), [r2] "l" (r2), [eccd] "I" (uECC_WORDS * 4), [eccdm1] "I" ((uECC_WORDS-1) * 4), [eccd2m1] "I" ((uECC_WORDS * 2 - 1) * 4)
|
||||
: "r3", "r4", "r5", "r6", "r7", "cc", "memory"
|
||||
);
|
||||
#endif
|
||||
}
|
||||
#define asm_mult 1
|
||||
#endif /* !asm_mult */
|
||||
|
||||
#if uECC_SQUARE_FUNC
|
||||
#if !asm_square
|
||||
static void vli_square(uint32_t *p_result, uint32_t *p_left)
|
||||
{
|
||||
#if (uECC_PLATFORM != uECC_arm_thumb)
|
||||
uint32_t c0 = 0;
|
||||
uint32_t c1 = 0;
|
||||
uint32_t c2 = 0;
|
||||
uint32_t k = 0;
|
||||
uint32_t i, tt;
|
||||
uint32_t t0, t1;
|
||||
|
||||
__asm__ volatile (
|
||||
".syntax unified \n\t"
|
||||
|
||||
"1: \n\t" /* outer loop (k < uECC_WORDS) */
|
||||
"movs %[i], #0 \n\t" /* i = 0 */
|
||||
"b 3f \n\t"
|
||||
|
||||
"2: \n\t" /* outer loop (k >= uECC_WORDS) */
|
||||
"movs %[i], %[k] \n\t" /* i = k */
|
||||
"subs %[i], %[eccdm1] \n\t" /* i = k - (uECC_WORDS - 1) (times 4) */
|
||||
|
||||
"3: \n\t" /* inner loop */
|
||||
"subs %[tt], %[k], %[i] \n\t" /* tt = k-i */
|
||||
|
||||
"ldr %[t1], [%[left], %[tt]] \n\t" /* t1 = p_left[k-i] */
|
||||
"ldr %[t0], [%[left], %[i]] \n\t" /* t0 = p_left[i] */
|
||||
|
||||
"umull %[t0], %[t1], %[t0], %[t1] \n\t" /* (t0, t1) = p_left[i] * p_right[k-i] */
|
||||
|
||||
"cmp %[i], %[tt] \n\t" /* (i < k-i) ? */
|
||||
"bge 4f \n\t" /* if i >= k-i, skip */
|
||||
"lsls %[t1], #1 \n\t" /* high word << 1 */
|
||||
"adc %[c2], #0 \n\t" /* add carry bit to c2 */
|
||||
"lsls %[t0], #1 \n\t" /* low word << 1 */
|
||||
"adc %[t1], #0 \n\t" /* add carry bit to high word */
|
||||
|
||||
"4: \n\t"
|
||||
|
||||
"adds %[c0], %[t0] \n\t" /* add low word to c0 */
|
||||
"adcs %[c1], %[t1] \n\t" /* add high word to c1, including carry */
|
||||
"adc %[c2], #0 \n\t" /* add carry to c2 */
|
||||
|
||||
"adds %[i], #4 \n\t" /* i += 4 */
|
||||
"cmp %[i], %[k] \n\t" /* i <= k? */
|
||||
"bge 5f \n\t" /* if not, exit the loop */
|
||||
"subs %[tt], %[k], %[i] \n\t" /* tt = k-i */
|
||||
"cmp %[i], %[tt] \n\t" /* i <= k-i? */
|
||||
"ble 3b \n\t" /* if so, continue looping */
|
||||
|
||||
"5: \n\t" /* end inner loop */
|
||||
|
||||
"str %[c0], [%[result], %[k]] \n\t" /* p_result[k] = c0 */
|
||||
"mov %[c0], %[c1] \n\t" /* c0 = c1 */
|
||||
"mov %[c1], %[c2] \n\t" /* c1 = c2 */
|
||||
"movs %[c2], #0 \n\t" /* c2 = 0 */
|
||||
"adds %[k], #4 \n\t" /* k += 4 */
|
||||
"cmp %[k], %[eccd] \n\t" /* k < uECC_WORDS (times 4) ? */
|
||||
"blt 1b \n\t" /* if not, loop back, start with i = 0 */
|
||||
"cmp %[k], %[eccd2m1] \n\t" /* k < uECC_WORDS * 2 - 1 (times 4) ? */
|
||||
"blt 2b \n\t" /* if not, loop back, start with i = (k+1) - uECC_WORDS */
|
||||
/* end outer loop */
|
||||
|
||||
"str %[c0], [%[result], %[k]] \n\t" /* p_result[uECC_WORDS * 2 - 1] = c0 */
|
||||
#if (uECC_PLATFORM != uECC_arm_thumb2)
|
||||
".syntax divided \n\t"
|
||||
#endif
|
||||
: [c0] "+r" (c0), [c1] "+r" (c1), [c2] "+r" (c2), [k] "+r" (k), [i] "=&r" (i), [tt] "=&r" (tt), [t0] "=&r" (t0), [t1] "=&r" (t1)
|
||||
: [result] "r" (p_result), [left] "r" (p_left),
|
||||
[eccd] "I" (uECC_WORDS * 4), [eccdm1] "I" ((uECC_WORDS-1) * 4), [eccd2m1] "I" ((uECC_WORDS * 2 - 1) * 4)
|
||||
: "cc", "memory"
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
register uint32_t *r0 __asm__("r0") = p_result;
|
||||
register uint32_t *r1 __asm__("r1") = p_left;
|
||||
|
||||
__asm__ volatile (
|
||||
".syntax unified \n\t"
|
||||
"movs r2, #0 \n\t" /* c0 = 0 */
|
||||
"movs r3, #0 \n\t" /* c1 = 0 */
|
||||
"movs r4, #0 \n\t" /* c2 = 0 */
|
||||
"movs r5, #0 \n\t" /* k = 0 */
|
||||
|
||||
"push {r0} \n\t" /* keep p_result on the stack */
|
||||
|
||||
"1: \n\t" /* outer loop (k < uECC_WORDS) */
|
||||
"movs r6, #0 \n\t" /* r6 = i = 0 */
|
||||
"b 3f \n\t"
|
||||
|
||||
"2: \n\t" /* outer loop (k >= uECC_WORDS) */
|
||||
"movs r6, r5 \n\t" /* r6 = k */
|
||||
"subs r6, %[eccdm1] \n\t" /* r6 = i = k - (uECC_WORDS - 1) (times 4) */
|
||||
|
||||
"3: \n\t" /* inner loop */
|
||||
"push {r2, r3, r4, r5} \n\t" /* push things, r2 (c0) is at the top of stack. */
|
||||
"subs r7, r5, r6 \n\t" /* r7 = k-i */
|
||||
|
||||
"ldr r3, [r1, r7] \n\t" /* r3 = p_left[k-i] */
|
||||
"ldr r0, [r1, r6] \n\t" /* r0 = p_left[i] */
|
||||
|
||||
"lsrs r2, r0, #16 \n\t" /* r2 = a1 */
|
||||
"uxth r0, r0 \n\t" /* r0 = a0 */
|
||||
|
||||
"lsrs r4, r3, #16 \n\t" /* r4 = b1 */
|
||||
"uxth r3, r3 \n\t" /* r3 = b0 */
|
||||
|
||||
"movs r5, r2 \n\t" /* r5 = a1 */
|
||||
"muls r5, r4, r5 \n\t" /* r5 = a1*b1 */
|
||||
"muls r2, r3, r2 \n\t" /* r2 = b0*a1 */
|
||||
"muls r4, r0, r4 \n\t" /* r4 = a0*b1 */
|
||||
"muls r0, r3, r0 \n\t" /* r0 = a0*b0 */
|
||||
|
||||
"movs r3, #0 \n\t" /* r3 = 0 */
|
||||
"adds r2, r4 \n\t" /* r2 = b0*a1 + a0*b1 */
|
||||
"adcs r3, r3 \n\t" /* r3 = carry */
|
||||
"lsls r3, #16 \n\t" /* r3 = carry << 16 */
|
||||
"adds r5, r3 \n\t" /* r5 = a1*b1 + carry */
|
||||
|
||||
"lsls r3, r2, #16 \n\t" /* r3 = (b0*a1 + a0*b1) << 16 */
|
||||
"lsrs r2, #16 \n\t" /* r2 = (b0*a1 + a0*b1) >> 16 */
|
||||
"adds r0, r3 \n\t" /* r0 = low word = a0*b0 + ((b0*a1 + a0*b1) << 16) */
|
||||
"adcs r5, r2 \n\t" /* r5 = high word = a1*b1 + carry + ((b0*a1 + a0*b1) >> 16) */
|
||||
|
||||
"movs r3, #0 \n\t" /* r3 = 0 */
|
||||
"cmp r6, r7 \n\t" /* (i < k-i) ? */
|
||||
"mov r7, r3 \n\t" /* r7 = 0 (does not affect condition)*/
|
||||
"bge 4f \n\t" /* if i >= k-i, skip */
|
||||
"lsls r5, #1 \n\t" /* high word << 1 */
|
||||
"adcs r7, r3 \n\t" /* r7 = carry bit for c2 */
|
||||
"lsls r0, #1 \n\t" /* low word << 1 */
|
||||
"adcs r5, r3 \n\t" /* add carry from shift to high word */
|
||||
|
||||
"4: \n\t"
|
||||
"pop {r2, r3, r4} \n\t" /* r2 = c0, r3 = c1, r4 = c2 */
|
||||
"adds r2, r0 \n\t" /* add low word to c0 */
|
||||
"adcs r3, r5 \n\t" /* add high word to c1, including carry */
|
||||
"movs r0, #0 \n\t" /* r0 = 0 (does not affect carry bit) */
|
||||
"adcs r4, r0 \n\t" /* add carry to c2 */
|
||||
"adds r4, r7 \n\t" /* add carry from doubling (if any) */
|
||||
|
||||
"pop {r5} \n\t" /* r5 = k */
|
||||
|
||||
"adds r6, #4 \n\t" /* i += 4 */
|
||||
"cmp r6, r5 \n\t" /* i <= k? */
|
||||
"bge 5f \n\t" /* if not, exit the loop */
|
||||
"subs r7, r5, r6 \n\t" /* r7 = k-i */
|
||||
"cmp r6, r7 \n\t" /* i <= k-i? */
|
||||
"ble 3b \n\t" /* if so, continue looping */
|
||||
|
||||
"5: \n\t" /* end inner loop */
|
||||
|
||||
"ldr r0, [sp, #0] \n\t" /* r0 = p_result */
|
||||
|
||||
"str r2, [r0, r5] \n\t" /* p_result[k] = c0 */
|
||||
"mov r2, r3 \n\t" /* c0 = c1 */
|
||||
"mov r3, r4 \n\t" /* c1 = c2 */
|
||||
"movs r4, #0 \n\t" /* c2 = 0 */
|
||||
"adds r5, #4 \n\t" /* k += 4 */
|
||||
"cmp r5, %[eccd] \n\t" /* k < uECC_WORDS (times 4) ? */
|
||||
"blt 1b \n\t" /* if not, loop back, start with i = 0 */
|
||||
"cmp r5, %[eccd2m1] \n\t" /* k < uECC_WORDS * 2 - 1 (times 4) ? */
|
||||
"blt 2b \n\t" /* if not, loop back, start with i = (k+1) - uECC_WORDS */
|
||||
/* end outer loop */
|
||||
|
||||
"str r2, [r0, r5] \n\t" /* p_result[uECC_WORDS * 2 - 1] = c0 */
|
||||
"pop {r0} \n\t" /* pop p_result off the stack */
|
||||
|
||||
".syntax divided \n\t"
|
||||
: [r0] "+l" (r0), [r1] "+l" (r1)
|
||||
: [eccd] "I" (uECC_WORDS * 4), [eccdm1] "I" ((uECC_WORDS-1) * 4), [eccd2m1] "I" ((uECC_WORDS * 2 - 1) * 4)
|
||||
: "r2", "r3", "r4", "r5", "r6", "r7", "cc", "memory"
|
||||
);
|
||||
#endif
|
||||
}
|
||||
#define asm_square 1
|
||||
#endif /* !asm_square */
|
||||
#endif /* uECC_SQUARE_FUNC */
|
16276
src/crypt/ecc_avr.inc
Normal file
16276
src/crypt/ecc_avr.inc
Normal file
File diff suppressed because it is too large
Load Diff
155
src/crypt/rc4.cpp
Normal file
155
src/crypt/rc4.cpp
Normal file
@ -0,0 +1,155 @@
|
||||
#include<hgl/type/DataType.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
struct rc4_state
|
||||
{
|
||||
int x, y, m[256];
|
||||
};
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void rc4_setup( struct rc4_state *s, unsigned char *key, int length )
|
||||
{
|
||||
int i, j, k, *m, a;
|
||||
|
||||
s->x = 0;
|
||||
s->y = 0;
|
||||
m = s->m;
|
||||
|
||||
for( i = 0; i < 256; i++ )
|
||||
{
|
||||
m[i] = i;
|
||||
}
|
||||
|
||||
j = k = 0;
|
||||
|
||||
for( i = 0; i < 256; i++ )
|
||||
{
|
||||
a = m[i];
|
||||
j = (unsigned char) ( j + a + key[k] );
|
||||
m[i] = m[j]; m[j] = a;
|
||||
if( ++k >= length ) k = 0;
|
||||
}
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void rc4_crypt( struct rc4_state *s, unsigned char *data, int length )
|
||||
{
|
||||
int i, x, y, *m, a, b;
|
||||
|
||||
x = s->x;
|
||||
y = s->y;
|
||||
m = s->m;
|
||||
|
||||
for( i = 0; i < length; i++ )
|
||||
{
|
||||
x = (unsigned char) ( x + 1 ); a = m[x];
|
||||
y = (unsigned char) ( y + a );
|
||||
m[x] = b = m[y];
|
||||
m[y] = a;
|
||||
data[i] ^= m[(unsigned char) ( a + b )];
|
||||
}
|
||||
|
||||
s->x = x;
|
||||
s->y = y;
|
||||
}
|
||||
}//namespace
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
namespace hgl
|
||||
{
|
||||
namespace crypt
|
||||
{
|
||||
/**
|
||||
* RC4算法加密函数
|
||||
* @param data 数据
|
||||
* @param datasize 数据长度
|
||||
* @param key 密码
|
||||
* @param keysize 密码长度(字节)
|
||||
*/
|
||||
void RC4Encrypt(uint8 *data, int datasize, uint8 *key, int keysize)
|
||||
{
|
||||
rc4_state rs;
|
||||
|
||||
rc4_setup(&rs, key, keysize);
|
||||
rc4_crypt(&rs, data, datasize);
|
||||
}
|
||||
}//namespace crypt
|
||||
}//namespace hgl
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/*
|
||||
#ifdef TEST
|
||||
|
||||
#include <BaseString.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// the following tests come from the OpenSSL test suite
|
||||
// (crypto/rc4/rc4test.c)
|
||||
|
||||
static unsigned char keys[7][30]={
|
||||
{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
|
||||
{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
|
||||
{8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{4,0xef,0x01,0x23,0x45},
|
||||
{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
|
||||
{4,0xef,0x01,0x23,0x45},
|
||||
};
|
||||
|
||||
static unsigned char data_len[7]={8,8,8,20,28,10};
|
||||
static unsigned char data[7][30]={
|
||||
{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0xff},
|
||||
{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
|
||||
0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
|
||||
0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
|
||||
0x12,0x34,0x56,0x78,0xff},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
|
||||
{0},
|
||||
};
|
||||
|
||||
static unsigned char output[7][30]={
|
||||
{0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
|
||||
{0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
|
||||
{0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
|
||||
{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
|
||||
0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
|
||||
0x36,0xb6,0x78,0x58,0x00},
|
||||
{0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
|
||||
0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
|
||||
0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
|
||||
0x40,0x01,0x1e,0xcf,0x00},
|
||||
{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
|
||||
{0},
|
||||
};
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int i;
|
||||
struct rc4_state s;
|
||||
unsigned char buffer[30];
|
||||
|
||||
for( i = 0; i < 7; i++ )
|
||||
{
|
||||
rc4_setup( &s, &keys[i][1], keys[i][0] );
|
||||
memcpy( buffer, data[i], data_len[i] );
|
||||
rc4_crypt( &s, buffer, data_len[i] );
|
||||
|
||||
printf( "test %d ", i + 1 );
|
||||
|
||||
if( ! memcmp( buffer, output[i], data_len[i] ) )
|
||||
{
|
||||
printf( "passed\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
printf( "failed\n" );
|
||||
return( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
#endif*/
|
2335
src/crypt/uECC.c
Normal file
2335
src/crypt/uECC.c
Normal file
File diff suppressed because it is too large
Load Diff
171
src/crypt/uECC.h
Normal file
171
src/crypt/uECC.h
Normal file
@ -0,0 +1,171 @@
|
||||
/* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
|
||||
|
||||
#ifndef _MICRO_ECC_H_
|
||||
#define _MICRO_ECC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Platform selection options.
|
||||
If uECC_PLATFORM is not defined, the code will try to guess it based on compiler macros.
|
||||
Possible values for uECC_PLATFORM are defined below: */
|
||||
#define uECC_arch_other 0
|
||||
#define uECC_x86 1
|
||||
#define uECC_x86_64 2
|
||||
#define uECC_arm 3
|
||||
#define uECC_arm_thumb 4
|
||||
#define uECC_avr 5
|
||||
|
||||
/* If desired, you can define uECC_WORD_SIZE as appropriate for your platform (1, 4, or 8 bytes).
|
||||
If uECC_WORD_SIZE is not explicitly defined then it will be automatically set based on your platform. */
|
||||
|
||||
/* Inline assembly options.
|
||||
uECC_asm_none - Use standard C99 only.
|
||||
uECC_asm_small - Use GCC inline assembly for the target platform (if available), optimized for minimum size.
|
||||
uECC_asm_fast - Use GCC inline assembly optimized for maximum speed. */
|
||||
#define uECC_asm_none 0
|
||||
#define uECC_asm_small 1
|
||||
#define uECC_asm_fast 2
|
||||
#ifndef uECC_ASM
|
||||
#define uECC_ASM uECC_asm_fast
|
||||
#endif
|
||||
|
||||
/* Curve selection options. */
|
||||
#define uECC_secp160r1 1
|
||||
#define uECC_secp192r1 2
|
||||
#define uECC_secp256r1 3
|
||||
#define uECC_secp256k1 4
|
||||
#ifndef uECC_CURVE
|
||||
#define uECC_CURVE uECC_secp160r1
|
||||
#endif
|
||||
|
||||
/* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a specific function to be used for (scalar) squaring
|
||||
instead of the generic multiplication function. This will make things faster by about 8% but increases the code size. */
|
||||
#define uECC_SQUARE_FUNC 1
|
||||
|
||||
#define uECC_CONCAT1(a, b) a##b
|
||||
#define uECC_CONCAT(a, b) uECC_CONCAT1(a, b)
|
||||
|
||||
#define uECC_size_1 20 /* secp160r1 */
|
||||
#define uECC_size_2 24 /* secp192r1 */
|
||||
#define uECC_size_3 32 /* secp256r1 */
|
||||
#define uECC_size_4 32 /* secp256k1 */
|
||||
|
||||
#define uECC_BYTES uECC_CONCAT(uECC_size_, uECC_CURVE)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* uECC_RNG_Function type
|
||||
The RNG function should fill p_size random bytes into p_dest. It should return 1 if
|
||||
p_dest was filled with random data, or 0 if the random data could not be generated.
|
||||
The filled-in values should be either truly random, or from a cryptographically-secure PRNG.
|
||||
|
||||
A correctly functioning RNG function must be set (using uECC_set_rng()) before calling
|
||||
uECC_make_key() or uECC_sign().
|
||||
|
||||
A correct RNG function is set by default when building for Windows, Linux, or OS X.
|
||||
If you are building on another POSIX-compliant system that supports /dev/random or /dev/urandom,
|
||||
you can define uECC_POSIX to use the predefined RNG. For embedded platforms there is no predefined
|
||||
RNG function; you must provide your own.
|
||||
*/
|
||||
typedef int (*uECC_RNG_Function)(uint8_t *p_dest, unsigned p_size);
|
||||
|
||||
/* uECC_set_rng() function.
|
||||
Set the function that will be used to generate random bytes. The RNG function should
|
||||
return 1 if the random data was generated, or 0 if the random data could not be generated.
|
||||
|
||||
On platforms where there is no predefined RNG function (eg embedded platforms), this must
|
||||
be called before uECC_make_key() or uECC_sign() are used.
|
||||
|
||||
Inputs:
|
||||
p_rng - The function that will be used to generate random bytes.
|
||||
*/
|
||||
void uECC_set_rng(uECC_RNG_Function p_rng);
|
||||
|
||||
/* uECC_make_key() function.
|
||||
Create a public/private key pair.
|
||||
|
||||
Outputs:
|
||||
p_publicKey - Will be filled in with the public key.
|
||||
p_privateKey - Will be filled in with the private key.
|
||||
|
||||
Returns 1 if the key pair was generated successfully, 0 if an error occurred.
|
||||
*/
|
||||
int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_BYTES]);
|
||||
|
||||
/* uECC_shared_secret() function.
|
||||
Compute a shared secret given your secret key and someone else's public key.
|
||||
Note: It is recommended that you hash the result of uECC_shared_secret() before using it for symmetric encryption or HMAC.
|
||||
|
||||
Inputs:
|
||||
p_publicKey - The public key of the remote party.
|
||||
p_privateKey - Your private key.
|
||||
|
||||
Outputs:
|
||||
p_secret - Will be filled in with the shared secret value.
|
||||
|
||||
Returns 1 if the shared secret was generated successfully, 0 if an error occurred.
|
||||
*/
|
||||
int uECC_shared_secret(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_privateKey[uECC_BYTES], uint8_t p_secret[uECC_BYTES]);
|
||||
|
||||
/* uECC_compress() function.
|
||||
Compress a public key.
|
||||
|
||||
Inputs:
|
||||
p_publicKey - The public key to compress.
|
||||
|
||||
Outputs:
|
||||
p_compressed - Will be filled in with the compressed public key.
|
||||
*/
|
||||
void uECC_compress(const uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_compressed[uECC_BYTES+1]);
|
||||
|
||||
/* uECC_decompress() function.
|
||||
Decompress a compressed public key.
|
||||
|
||||
Inputs:
|
||||
p_compressed - The compressed public key.
|
||||
|
||||
Outputs:
|
||||
p_publicKey - Will be filled in with the decompressed public key.
|
||||
*/
|
||||
void uECC_decompress(const uint8_t p_compressed[uECC_BYTES+1], uint8_t p_publicKey[uECC_BYTES*2]);
|
||||
|
||||
/* uECC_sign() function.
|
||||
Generate an ECDSA signature for a given hash value.
|
||||
|
||||
Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to
|
||||
this function along with your private key.
|
||||
|
||||
Inputs:
|
||||
p_privateKey - Your private key.
|
||||
p_hash - The message hash to sign.
|
||||
|
||||
Outputs:
|
||||
p_signature - Will be filled in with the signature value.
|
||||
|
||||
Returns 1 if the signature generated successfully, 0 if an error occurred.
|
||||
*/
|
||||
int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_BYTES], uint8_t p_signature[uECC_BYTES*2]);
|
||||
|
||||
/* uECC_verify() function.
|
||||
Verify an ECDSA signature.
|
||||
|
||||
Usage: Compute the hash of the signed data using the same hash as the signer and
|
||||
pass it to this function along with the signer's public key and the signature values (r and s).
|
||||
|
||||
Inputs:
|
||||
p_publicKey - The signer's public key
|
||||
p_hash - The hash of the signed data.
|
||||
p_signature - The signature value.
|
||||
|
||||
Returns 1 if the signature is valid, 0 if it is invalid.
|
||||
*/
|
||||
int uECC_verify(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_hash[uECC_BYTES], const uint8_t p_signature[uECC_BYTES*2]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* end of extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* _MICRO_ECC_H_ */
|
29
src/csv/CSVOutputStream.cpp
Normal file
29
src/csv/CSVOutputStream.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/csv/CSVOutputStream.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
CSVOutputStream<char> *CreateCSVOutputToUTF8File(const OSString &filename,const uint field_count,const u8char fields_terminated_char,const u8char enclosed_char)
|
||||
{
|
||||
io::FileOutputStream *fos=io::CreateFileOutputStream(filename);
|
||||
|
||||
if(!fos)return(nullptr);
|
||||
|
||||
return CreateCSVOutputToStream<u8char>(fos,new io::UTF8TextOutputStream(fos),field_count,fields_terminated_char,enclosed_char);
|
||||
}
|
||||
|
||||
CSVOutputStream<u16char> *CreateCSVOutputToUTF16LEFile(const OSString &filename,const uint field_count,const u16char fields_terminated_char,const u16char enclosed_char)
|
||||
{
|
||||
io::FileOutputStream *fos=io::CreateFileOutputStream(filename);
|
||||
|
||||
if(!fos)return(nullptr);
|
||||
|
||||
return CreateCSVOutputToStream<u16char>(fos,new io::UTF16LETextOutputStream(fos),field_count,fields_terminated_char,enclosed_char);
|
||||
}
|
||||
}//namespace util
|
||||
}//namespace hgl
|
@ -15,6 +15,9 @@ namespace hgl
|
||||
|
||||
memset(header,0,sizeof(TGAHeader));
|
||||
|
||||
header->width=width;
|
||||
header->height=height;
|
||||
|
||||
TGAImageDesc desc;
|
||||
|
||||
desc.image_desc=0;
|
||||
@ -33,6 +36,8 @@ namespace hgl
|
||||
desc.alpha_depth=8;
|
||||
}
|
||||
|
||||
desc.direction=TGA_DIRECTION_UPPER_LEFT;
|
||||
|
||||
header->image_desc=desc.image_desc;
|
||||
return(true);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user