除了Buffer其它任何地方不得使用memcpy/memset
This commit is contained in:
parent
dbb0a80ff8
commit
eba9043046
2
ADC.cpp
2
ADC.cpp
|
@ -18,7 +18,7 @@ ADConverter::ADConverter(byte line, uint channel)
|
|||
if(Channel & dat) Count++;
|
||||
}
|
||||
|
||||
ArrayZero(Data);
|
||||
Buffer(Data, sizeof(Data)).Clear();
|
||||
|
||||
ADC_TypeDef* const g_ADCs[]= ADCS;
|
||||
_ADC = g_ADCs[line - 1];
|
||||
|
|
|
@ -7,7 +7,7 @@ BlinkPort::BlinkPort()
|
|||
{
|
||||
_tid = 0;
|
||||
|
||||
ArrayZero(Ports);
|
||||
Buffer(Ports, sizeof(Ports)).Clear();
|
||||
Count = 0;
|
||||
|
||||
First = true;
|
||||
|
|
11
Config.cpp
11
Config.cpp
|
@ -84,14 +84,17 @@ bool ConfigBlock::Init(const char* name, const Buffer& bs)
|
|||
if(slen > sizeof(Name)) return false;
|
||||
|
||||
if(slen > ArrayLength(Name)) slen = ArrayLength(Name);
|
||||
|
||||
Buffer ns(Name, ArrayLength(Name));
|
||||
// 无论如何,把整个名称区域清空
|
||||
memset(Name, 0, ArrayLength(Name));
|
||||
ns.Clear();
|
||||
|
||||
// 配置块的大小,只有第一次能够修改,以后即使废弃也不能修改,仅仅清空名称
|
||||
if(bs.Length() > 0)
|
||||
{
|
||||
Size = bs.Length();
|
||||
memcpy(Name, name, slen);
|
||||
ns.SetLength(slen);
|
||||
ns = name;
|
||||
}
|
||||
|
||||
Hash = GetHash();
|
||||
|
@ -129,7 +132,7 @@ bool ConfigBlock::Write(const Storage& storage, uint addr, const Buffer& bs)
|
|||
bool ConfigBlock::Remove(const Storage& storage, uint addr)
|
||||
{
|
||||
// 把整个名称区域清空
|
||||
memset(Name, 0, ArrayLength(Name));
|
||||
Buffer(Name, ArrayLength(Name)).Clear();
|
||||
|
||||
Hash = GetHash();
|
||||
|
||||
|
@ -370,7 +373,7 @@ const Buffer ConfigBase::ToArray() const
|
|||
|
||||
void ConfigBase::Init()
|
||||
{
|
||||
memset(_Start, 0, Size());
|
||||
Buffer(_Start, Size()).Clear();
|
||||
}
|
||||
|
||||
void ConfigBase::Load()
|
||||
|
|
|
@ -211,8 +211,8 @@ NRF24L01::NRF24L01()
|
|||
_spi = NULL;
|
||||
|
||||
// 初始化地址
|
||||
memset(Remote, 0, ArrayLength(Remote));
|
||||
memcpy(Local, (byte*)Sys.ID, ArrayLength(Local));
|
||||
Buffer(Remote, ArrayLength(Remote)).Clear();
|
||||
Buffer(Local, ArrayLength(Local)) = Sys.ID;
|
||||
Channel = 0; // 默认通道0
|
||||
|
||||
DynPayload = true;
|
||||
|
|
|
@ -43,7 +43,7 @@ void UBlox::SetBaudRate(int baudRate)
|
|||
};
|
||||
int len = ArrayLength(cmd);
|
||||
int p = 6 + 8; // 头6字节,偏移8
|
||||
memcpy(cmd + p, &baudRate, 4); // 小字节序
|
||||
Buffer(cmd, len).Copy(p, &baudRate, 4);
|
||||
// 修改校验。不含头尾各2字节
|
||||
byte CK_A = 0, CK_B = 0;
|
||||
for(int i=2; i<len-2-2; i++)
|
||||
|
|
|
@ -214,7 +214,7 @@ void W5500::Init()
|
|||
_spi = NULL;
|
||||
Led = NULL;
|
||||
|
||||
ArrayZero(_sockets);
|
||||
Buffer(_sockets, sizeof(_sockets)).Clear();
|
||||
|
||||
PhaseOM = 0x00;
|
||||
|
||||
|
|
|
@ -223,7 +223,7 @@ bool parseDNSMSG(TDNS* hdr, const Buffer& bs, byte* ip_from_dns)
|
|||
Stream ms(bs);
|
||||
ms.Little = false;
|
||||
|
||||
memset(hdr, 0, sizeof(hdr));
|
||||
Buffer(hdr, sizeof(hdr)).Clear();
|
||||
|
||||
hdr->id = ms.ReadUInt16();
|
||||
ushort tmp = ms.ReadUInt16();
|
||||
|
|
|
@ -66,7 +66,7 @@ void Dhcp::SendDhcp(byte* buf, uint len)
|
|||
String vendor = "www.wslink.cn";
|
||||
opt = opt->Next()->SetData(DHCP_OPT_Vendor, vendor);
|
||||
byte ps[] = { 0x01, 0x06, 0x03, 0x2b}; // 需要参数 Mask/DNS/Router/Vendor
|
||||
opt = opt->Next()->SetData(DHCP_OPT_ParameterList, CArray(ps));
|
||||
opt = opt->Next()->SetData(DHCP_OPT_ParameterList, Buffer(ps, sizeof(ps)));
|
||||
opt = opt->Next()->End();
|
||||
|
||||
len = (byte*)opt + 1 - p;
|
||||
|
|
|
@ -289,13 +289,13 @@ typedef struct _DHCP_HEADER
|
|||
void Init(uint dhcpid, bool recursion = false)
|
||||
{
|
||||
// 为了安全,清空一次
|
||||
memset(this, 0, sizeof(this[0]));
|
||||
Buffer(this, sizeof(this[0])).Clear();
|
||||
|
||||
MsgType = 1;
|
||||
HardType = 1;
|
||||
HardLength = 6;
|
||||
Hops = 0;
|
||||
TransID = _REV(dhcpid);
|
||||
MsgType = 1;
|
||||
HardType = 1;
|
||||
HardLength = 6;
|
||||
Hops = 0;
|
||||
TransID = _REV(dhcpid);
|
||||
//Flags = 0x80; // 从0-15bits,最左一bit为1时表示server将以广播方式传送封包给 client,其余尚未使用
|
||||
SetMagic();
|
||||
|
||||
|
@ -386,9 +386,7 @@ typedef struct _DHCP_OPT
|
|||
{
|
||||
Option = option;
|
||||
Length = 4;
|
||||
memcpy(&Data, &value, Length);
|
||||
// 需要考虑地址对齐问题,只有4字节对齐,才可以直接使用整数赋值
|
||||
//*(uint*)&Data = value;
|
||||
Buffer(&Data, 4) = &value;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
@ -398,7 +396,7 @@ typedef struct _DHCP_OPT
|
|||
Option = DHCP_OPT_ClientIdentifier;
|
||||
Length = 1 + 6;
|
||||
Data = 1; // 类型ETHERNET=1
|
||||
memcpy(&Data + 1, &mac.Value, 6);
|
||||
mac.CopyTo(&Data + 1);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -182,9 +182,7 @@ MacAddress::MacAddress(UInt64 v)
|
|||
|
||||
MacAddress::MacAddress(const byte* macs)
|
||||
{
|
||||
/*ByteArray bs(macs, 6);
|
||||
Value = bs.ToUInt64() & MAC_MASK;*/
|
||||
memcpy(&Value, macs, 6);
|
||||
Buffer(&Value, 6) = macs;
|
||||
}
|
||||
|
||||
MacAddress::MacAddress(const Buffer& arr)
|
||||
|
|
|
@ -178,7 +178,8 @@ void md5_update(md5_context *ctx, byte *input, uint length )
|
|||
|
||||
if( left && length >= fill )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left), (void *) input, fill );
|
||||
//memcpy((void*)(ctx->buffer + left), (void*)input, fill);
|
||||
Buffer((void*)(ctx->buffer + left), fill) = input;
|
||||
md5_process( ctx, ctx->buffer );
|
||||
length -= fill;
|
||||
input += fill;
|
||||
|
@ -194,7 +195,8 @@ void md5_update(md5_context *ctx, byte *input, uint length )
|
|||
|
||||
if( length )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left), (void *) input, length );
|
||||
//memcpy((void*)(ctx->buffer + left), (void*)input, length);
|
||||
Buffer((void*)(ctx->buffer + left), length) = input;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -62,8 +62,7 @@ void SerialPort::Set(COM index, int baudRate)
|
|||
if(((USART_TypeDef*)_port)->CR1 & USART_CR1_UE) Opened = true;
|
||||
|
||||
// 设置名称
|
||||
//Name = "COMx";
|
||||
memcpy((byte*)Name, (byte*)"COMx", 4);
|
||||
Buffer(Name, 4) = "COMx";
|
||||
Name[3] = '0' + _index + 1;
|
||||
Name[4] = 0;
|
||||
}
|
||||
|
|
4
Spi.cpp
4
Spi.cpp
|
@ -58,8 +58,8 @@ void Spi::Init(SPI spi, uint speedHz, bool useNss)
|
|||
_SPI = g_Spis[_index];
|
||||
|
||||
Pin g_Spi_Pins_Map[][4] = SPI_PINS_FULLREMAP;
|
||||
Pin* ps = g_Spi_Pins_Map[_index]; //选定spi引脚
|
||||
memcpy(Pins, ps, sizeof(Pins));
|
||||
auto ps = g_Spi_Pins_Map[_index]; //选定spi引脚
|
||||
Buffer(Pins, sizeof(Pins)) = ps;
|
||||
|
||||
if(!useNss) Pins[0] = P0;
|
||||
|
||||
|
|
|
@ -809,7 +809,7 @@ void trim(char* buffer, int& len, bool trimBegin, bool trimEnd)
|
|||
char *end = buffer + len - 1;
|
||||
if(trimEnd) while (isspace(*end) && end >= begin) end--;
|
||||
len = end + 1 - begin;
|
||||
if (begin > buffer) memcpy(buffer, begin, len);
|
||||
if (begin > buffer) Buffer(buffer, len) = begin;
|
||||
buffer[len] = 0;
|
||||
}
|
||||
|
||||
|
|
4
Sys.cpp
4
Sys.cpp
|
@ -57,7 +57,7 @@ _force_inline void InitHeapStack(uint top)
|
|||
uint size = (uint)&__initial_sp - (uint)p;
|
||||
uint msp = top - size;
|
||||
// 拷贝一部分栈内容到新栈
|
||||
memcpy((void*)msp, (void*)p, size);
|
||||
Buffer((void*)msp, size) = (void*)p;
|
||||
|
||||
// 必须先拷贝完成栈,再修改栈指针
|
||||
__set_MSP(msp);
|
||||
|
@ -126,7 +126,7 @@ TSys::TSys()
|
|||
#elif defined(STM32F4)
|
||||
void* p = (void*)0x1FFF7A10;
|
||||
#endif
|
||||
memcpy(ID, p, ArrayLength(ID));
|
||||
Buffer(ID, ArrayLength(ID)) = p;
|
||||
|
||||
CPUID = SCB->CPUID;
|
||||
uint mcuid = DBGMCU->IDCODE; // MCU编码。低字设备版本,高字子版本
|
||||
|
|
|
@ -82,7 +82,6 @@ void TestMessage(OutputPort* leds)
|
|||
nrf->AutoAnswer = false;
|
||||
nrf->Speed = 1000;
|
||||
//byte addr[] = {0x34, 0x43, 0x10, 0x10, 0x01};
|
||||
//memcpy(nrf->Address, addr, ArrayLength(addr));
|
||||
nrf->PayloadWidth = 0; // 使用动态负载
|
||||
if(!nrf->Check())
|
||||
debug_printf("请检查NRF24L01线路\r\n");
|
||||
|
|
|
@ -24,8 +24,8 @@ void TestSerial()
|
|||
sp1->Open();
|
||||
sp1->Register(OnUsartRead);
|
||||
|
||||
char str[] = "http://www.NewLifeX.com \r\n";
|
||||
sp1->Write(CArray(str));
|
||||
String str = "http://www.NewLifeX.com \r\n";
|
||||
//sp1->Write(str);
|
||||
//Sys.Sleep(3000);
|
||||
|
||||
debug_printf("\r\nTestSerial Finish!\r\n");
|
||||
|
|
4
Time.cpp
4
Time.cpp
|
@ -317,13 +317,13 @@ DateTime& DateTime::ParseUs(UInt64 us)
|
|||
|
||||
DateTime::DateTime()
|
||||
{
|
||||
memset(&Year, 0, &Microsecond - &Year + sizeof(Microsecond));
|
||||
Buffer(&Year, &Microsecond - &Year + sizeof(Microsecond)).Clear();
|
||||
}
|
||||
|
||||
DateTime::DateTime(UInt64 seconds)
|
||||
{
|
||||
if(seconds == 0)
|
||||
memset(&Year, 0, &Microsecond - &Year + sizeof(Microsecond));
|
||||
Buffer(&Year, &Microsecond - &Year + sizeof(Microsecond)).Clear();
|
||||
else
|
||||
Parse(seconds);
|
||||
}
|
||||
|
|
|
@ -273,7 +273,7 @@ void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
|
|||
if(!_Arps)
|
||||
{
|
||||
_Arps = new ARP_ITEM[Count];
|
||||
memset(_Arps, 0, sizeof(ARP_ITEM) * Count);
|
||||
Buffer(_Arps, sizeof(ARP_ITEM) * Count).Clear();
|
||||
}
|
||||
|
||||
ARP_ITEM* item = NULL;
|
||||
|
|
|
@ -349,7 +349,7 @@ void TcpSocket::Send(const byte* buf, uint len)
|
|||
uint len2 = Tip->BufferSize - tcp->Offset() - tcp->Size();
|
||||
assert_param(len <= len2);
|
||||
|
||||
memcpy(tcp->Next(), buf, len);
|
||||
Buffer(tcp->Next(), len) = buf;
|
||||
}
|
||||
|
||||
// 发送的时候采用LocalPort
|
||||
|
|
|
@ -27,10 +27,6 @@ Device::Device() :
|
|||
OfflineTime = 0;
|
||||
SleepTime = 0;
|
||||
|
||||
/*ArrayZero(Mac);
|
||||
ArrayZero(Name);
|
||||
ArrayZero(Pass);
|
||||
ArrayZero(Store);*/
|
||||
HardID.Clear();
|
||||
Mac.Clear();
|
||||
Name.Clear();
|
||||
|
|
|
@ -97,7 +97,7 @@ ITransport* Create2401(SPI spi_, Pin ce, Pin irq, Pin power, bool powerInvert, I
|
|||
nrf->Led = led;
|
||||
|
||||
byte num = tc->Mac[0] && tc->Mac[1] && tc->Mac[2] && tc->Mac[3] && tc->Mac[4];
|
||||
if(num != 0 && num != 0xFF) memcpy(nrf->Remote, tc->Mac, 5);
|
||||
if(num != 0 && num != 0xFF) Buffer(nrf->Remote, 5) = tc->Mac;
|
||||
|
||||
return nrf;
|
||||
}
|
||||
|
@ -184,8 +184,9 @@ void* InitConfig(void* data, uint size)
|
|||
data = hot->Next();
|
||||
if(hot->Times == 1)
|
||||
{
|
||||
memset(data, 0x00, size);
|
||||
((byte*)data)[0] = size;
|
||||
Buffer ds(data, size);
|
||||
ds.Clear();
|
||||
ds[0] = size;
|
||||
}
|
||||
|
||||
auto tc = TinyConfig::Create();
|
||||
|
|
|
@ -283,10 +283,8 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
|
|||
if(sum == 0 || sum == 0xFF * 5) st = NULL;
|
||||
}
|
||||
if(!st)
|
||||
//memcpy(dv->Mac, dv->HardID, 6);
|
||||
dv->Mac = dv->HardID;
|
||||
else
|
||||
//memcpy(dv->Mac, st, 6);
|
||||
dv->Mac = st;
|
||||
|
||||
if(dv->Valid())
|
||||
|
|
|
@ -455,8 +455,9 @@ bool TokenToTiny(const TokenMessage& msg, TinyMessage& tny)
|
|||
|
||||
// 第一个字节是节点设备地址
|
||||
tny.Dest = msg.Data[0];
|
||||
if(msg.Length > 1) memcpy(tny.Data, &msg.Data[1], msg.Length - 1);
|
||||
if(msg.Length > 1) Buffer(tny.Data, msg.Length - 1) = &msg.Data[1];
|
||||
tny.Length = msg.Length - 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -471,7 +472,7 @@ void TinyToToken(const TinyMessage& msg, TokenMessage& msg2)
|
|||
// 第一个字节是节点设备地址
|
||||
msg2.Data[0] = ((TinyMessage&)msg).Src;
|
||||
|
||||
if(msg.Length > 0) memcpy(&msg2.Data[1], msg.Data, msg.Length);
|
||||
if(msg.Length > 0) Buffer(&msg2.Data[1], msg.Length) = msg.Data;
|
||||
|
||||
msg2.Length = 1 + msg.Length;
|
||||
}
|
||||
|
|
|
@ -86,13 +86,13 @@ uint TokenMessage::MaxDataSize() const
|
|||
// 设置错误信息字符串
|
||||
void TokenMessage::SetError(byte errorCode, const char* error, int errLength)
|
||||
{
|
||||
Error = errorCode != 0;
|
||||
Length = 1 + errLength;
|
||||
Data[0] = errorCode;
|
||||
Error = errorCode != 0;
|
||||
Length = 1 + errLength;
|
||||
Data[0] = errorCode;
|
||||
if(errLength > 0)
|
||||
{
|
||||
assert_ptr(error);
|
||||
memcpy(Data + 1, error, errLength);
|
||||
Buffer(Data + 1, errLength) = error;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,7 +140,7 @@ TokenController::TokenController() : Controller(), Key(0)
|
|||
Server = NULL;
|
||||
|
||||
// 默认屏蔽心跳日志和确认日志
|
||||
ArrayZero(NoLogCodes);
|
||||
Buffer(NoLogCodes, sizeof(NoLogCodes)).Clear();
|
||||
NoLogCodes[0] = 0x03;
|
||||
NoLogCodes[1] = 0x08;
|
||||
|
||||
|
@ -148,7 +148,7 @@ TokenController::TokenController() : Controller(), Key(0)
|
|||
|
||||
Stat = NULL;
|
||||
|
||||
memset(_Queue, 0, ArrayLength(_Queue) * sizeof(_Queue[0]));
|
||||
Buffer(_Queue, ArrayLength(_Queue) * sizeof(_Queue[0])).Clear();
|
||||
}
|
||||
|
||||
TokenController::~TokenController()
|
||||
|
@ -539,7 +539,7 @@ TokenStat::TokenStat()
|
|||
_Total = NULL;*/
|
||||
|
||||
int start = offsetof(TokenStat, SendRequest);
|
||||
memset((byte*)this + start, 0, sizeof(TokenStat) - start);
|
||||
Buffer((byte*)this + start, sizeof(TokenStat) - start).Clear();
|
||||
}
|
||||
|
||||
TokenStat::~TokenStat()
|
||||
|
|
4
Type.cpp
4
Type.cpp
|
@ -212,9 +212,9 @@ int Buffer::Set(byte item, int index, int len)
|
|||
return len;
|
||||
}
|
||||
|
||||
void Buffer::Clear()
|
||||
void Buffer::Clear(byte item)
|
||||
{
|
||||
Set(0, 0, _Length);
|
||||
Set(item, 0, _Length);
|
||||
}
|
||||
|
||||
// 截取一个子缓冲区
|
||||
|
|
14
Type.h
14
Type.h
|
@ -64,6 +64,12 @@ class Buffer : public Object
|
|||
public:
|
||||
// 打包一个指针和长度指定的数据区
|
||||
Buffer(void* ptr = nullptr, int len = 0);
|
||||
template<typename T, int N>
|
||||
Buffer(T[] arr, N)
|
||||
{
|
||||
_Arr = arr;
|
||||
_Length = N;
|
||||
}
|
||||
// 拷贝构造函数。直接把指针和长度拿过来用
|
||||
Buffer(const Buffer& buf) = delete;
|
||||
// 对象mov操作,指针和长度归我,清空对方
|
||||
|
@ -98,7 +104,7 @@ public:
|
|||
|
||||
// 用指定字节设置初始化一个区域
|
||||
int Set(byte item, int index, int len);
|
||||
void Clear();
|
||||
void Clear(byte item = 0);
|
||||
|
||||
// 截取一个子缓冲区
|
||||
Buffer Sub(int index, int len);
|
||||
|
@ -184,11 +190,11 @@ protected:
|
|||
// 数组长度
|
||||
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
// 数组清零,固定长度
|
||||
#define ArrayZero(arr) memset(arr, 0, ArrayLength(arr) * sizeof(arr[0]))
|
||||
//#define ArrayZero(arr) memset(arr, 0, sizeof(arr))
|
||||
|
||||
// 使用常量数组来定义一个指针数组
|
||||
#define CArray(arr) (Array(arr, ArrayLength(arr)))
|
||||
#define SArray(obj) (Array(&obj, sizeof(obj)))
|
||||
//#define CArray(arr) (Array(arr, ArrayLength(arr)))
|
||||
//#define SArray(obj) (Array(&obj, sizeof(obj)))
|
||||
|
||||
// 使用常量数组来定义一个指针数组
|
||||
//#define CBuffer(arr) (Buffer(arr, ArrayLength(arr)))
|
||||
|
|
Loading…
Reference in New Issue