Пример #1
0
static char read_hexword(char c1, char c2, char c3, char c4)
{unsigned v,w;

	v=read_hexbyte(c1,c2);
	w=read_hexbyte(c3,c4);
	return(v*256+w);
}
Пример #2
0
bool expand_data(const std::string s, void **d, unsigned *siz)
{std::string buf;
 unsigned tam,t,f,g,len,sl,r=0,pos;
 bool err;
 char c[2];

	if (!bracketed_substr(s,buf,'(',')' ,&g)) return(false);
	string_to_number(buf,tam);
	if (siz!=0) *siz=tam;
	if (tam==0)
	{
		*d=0;
		return(true);
	}
	*d=new char[tam];
	pos=0;
	len=(unsigned)s.length();
	for (f=g; f<len; f++) if (s[f]=='\n') break;
	if (f>=len) return(false);
	f+=2;
	t=0;
	c[1]=0;
	err=false;
	while ((r<tam)&&(f<len))
	{
		while ((f<len)&&(s[f]=='\t')) f++;
		if (f<len)
		{
			g=f;
			while ((g<len)&&(s[g]!='\n')) g++;
			if (g<len)
			{
				buf=s.substr(f,g-f);
				sl=(unsigned)buf.length();
				for (unsigned h=0; h<sl; h+=2)
				{
					c[0]=read_hexbyte(buf[h],buf[h+1]);
					if (!err)
					{
						r++;
						((unsigned char *)(*d))[pos]=c[0];
						pos++;
					}
				}
				if (!err) f=g+1;
			}
			else
			{
				f=len;
				err=true;
			}
		}
		else err=true;
	}
	if (err) return(false);
	return(true);
}
Пример #3
0
std::string expand_str(const std::string &s)
{std::string res,buf;
 unsigned tam,t,f,g,len,sl,r=0;
 bool err;
 char c[2];

	if (!bracketed_substr(s,buf,'(',')')) return(res);
	string_to_number(buf,tam);
	len=(unsigned)s.length();
	for (f=0; f<len; f++) if (s[f]=='\n') break;
	if (f>=len) return(res);
	f+=2;
	t=0;
	c[1]=0;
	err=false;
	while ((r<tam)&&(f<len))
	{
		while ((f<len)&&(s[f]=='\t')) f++;
		if (f<len)
		{
			g=f;
			while ((g<len)&&(s[g]!='\n')) g++;
			if (g<len)
			{
				buf=s.substr(f,g-f);
				sl=(unsigned)buf.length();
				for (unsigned h=0; h<sl; h+=2)
				{
					if (buf[h]!='*') c[0]=read_hexbyte(buf[h],buf[h+1]);
					else
					{
						if (h+4>=sl) err=true;
						else
						{
							c[0]=read_hexword(buf[h+1],buf[h+2],buf[h+3],buf[h+4]);
							h+=3;
						}
					}
					if (!err)
					{
						r++;
						res+=c;
					}
				}
				if (!err) f=g+1;
			}
			else
			{
				f=len;
				err=true;
			}
		}
		else err=true;
	}
	if (err) return("");
	return(res);
}
Пример #4
0
void GDBDebugServer::try_read_cmd(gdb_cmd & out_cmd)
{
	char c = read_char();
	//interrupt
	if (UNLIKELY(c == 0x03)) {
		out_cmd.cmd = '\x03';
		out_cmd.data = "";
		out_cmd.checksum = 0;
		return;
	}
	if (UNLIKELY(c != '$')) {
		//gdb starts conversation with + for some reason
		if (c == '+') {
			c = read_char();
		}
		if (c != '$') {
			fmt::throw_exception("Expected start of packet character '$', got '%c' instead" HERE, c);
		}
	}
	//clear packet data
	out_cmd.cmd = "";
	out_cmd.data = "";
	out_cmd.checksum = 0;
	bool cmd_part = true;
	u8 checksum = 0;
	while(true) {
		c = read_char();
		if (c == '#') {
			break;
		}
		checksum = (checksum + reinterpret_cast<u8&>(c)) % 256;
		//escaped char
		if (c == '}') {
			c = read_char() ^ 0x20;
			checksum = (checksum + reinterpret_cast<u8&>(c)) % 256;
		}
		//cmd-data splitters
		if (cmd_part && ((c == ':') || (c == '.') || (c == ';'))) {
			cmd_part = false;
		}
		if (cmd_part) {
			out_cmd.cmd += c;
			//only q and v commands can have multi-char command
			if ((out_cmd.cmd.length() == 1) && (c != 'q') && (c != 'v')) {
				cmd_part = false;
			}
		} else {
			out_cmd.data += c;
		}
	}
	out_cmd.checksum = read_hexbyte();
	if (out_cmd.checksum != checksum) {
		throw wrong_checksum_exception("Wrong checksum for packet" HERE);
	}
}