Beispiel #1
0
static void gdb_read_mem()
{
  static u8 reply[GDB_BFR_MAX - 4];
  u32 addr, len;
  u32 i;

  i = 1;
  addr = 0;
  while (cmd_bfr[i] != ',')
    addr = (addr << 4) | hex2char(cmd_bfr[i++]);
  i++;

  len = 0;
  while (i < cmd_len)
    len = (len << 4) | hex2char(cmd_bfr[i++]);
  DEBUG_LOG(GDB_STUB, "gdb: read memory: %08x bytes from %08x", len, addr);

  if (len * 2 > sizeof reply)
    gdb_reply("E01");
  u8* data = Memory::GetPointer(addr);
  if (!data)
    return gdb_reply("E0");
  mem2hex(reply, data, len);
  reply[len * 2] = '\0';
  gdb_reply((char*)reply);
}
Beispiel #2
0
int POCO_CVT::str2char(const char* str, char& c)
{
	if( str == NULL ) {
		c = '\0';
		return 1;
	}

	int len = strlen(str);

	if( len == 1 ) {
		c = str[0];
		return 1;
	}

	if( len >= 3
	 && str[0] == '0'
	 && (str[1] == 'x' || str[1] == 'X') ) {
		if( hex2char(str[2], c) == 0 ) {
			return 0;
		}

		if( len > 3 ) {
			char a;
			if( hex2char(str[3], a) == 0 ) {
				return 0;
			}

			c = c*0x10 + a;
		}
	}

	return 1;
}
Beispiel #3
0
static void hex2mem(u8* dst, u8* src, u32 len)
{
  while (len-- > 0)
  {
    *dst++ = (hex2char(*src) << 4) | hex2char(*(src + 1));
    src += 2;
  }
}
Beispiel #4
0
void buildAndCheckSentence(unsigned char characterIn) {
    // Full specification for NMEA0138 specifies a maximum sentence length
    // of 255 characters. We're going to ignore this for half the length as
    // we shouldn't get anything that big.
    // This contains the function's state of whether
    // it is currently building a sentence.
    // 0 - Awaiting start character ($)
    // 1 - Building sentence
    // 2 - Building first checksum character
    // 3 - Building second checksum character
    //printf("char: %c\r\n",characterIn);
    // We start recording a new sentence if we see a dollarsign.
    // The sentenceIndex is hard-set to 1 so that multiple dollar-signs
    // keep you at the beginning.
    if (characterIn == '$') {
        //printf("start character at least");
        sentence[0] = characterIn;
        sentenceIndex = 1;
        sentenceState = 1;
    } else if (sentenceState == 1) {
        // Record every character that comes in now that we're building a sentence.
        // Only stop if we run out of room or an asterisk is found.
        sentence[sentenceIndex++] = characterIn;
        if (characterIn == '*') {
            sentenceState = 2;
        } else if (sentenceIndex > 127) {
            // If we've filled up the buffer, ignore the entire message as we can't store it all
            sentenceState = 0;
            sentenceIndex = 0;
        }
    } else if (sentenceState == 2) {
        // Record the first ASCII-hex character of the checksum byte.
        checksum = hex2char(characterIn) << 4;
        sentenceState = 3;
    } else if (sentenceState == 3) {
        // Record the second ASCII-hex character of the checksum byte.
        checksum |= hex2char(characterIn);

        // Now that we've compiled a complete GPS sentence, let's check the checksum and parse it.
        // This code currently only supports RMC and GGA messages.
        if (checksum == getChecksum(sentence, sentenceIndex)) {
            if (sentence[3] == 'R' &&
                    sentence[4] == 'M' &&
                    sentence[5] == 'C') {
                parseRMC(sentence);
            } else if (sentence[3] == 'G' &&
                    sentence[4] == 'G' &&
                    sentence[5] == 'A') {
                parseGGA(sentence);
            }
        }

        // We clear all state variables here regardless of success.
        sentenceIndex = 0;
        sentenceState = 0;
    }
}
Beispiel #5
0
static void gdb_read_command()
{
  u8 c;
  u8 chk_read, chk_calc;

  cmd_len = 0;
  memset(cmd_bfr, 0, sizeof cmd_bfr);

  c = gdb_read_byte();
  if (c == '+')
  {
    // ignore ack
    return;
  }
  else if (c == 0x03)
  {
    CPU::Break();
    gdb_signal(GDB_SIGTRAP);
    return;
  }
  else if (c != GDB_STUB_START)
  {
    DEBUG_LOG(GDB_STUB, "gdb: read invalid byte %02x", c);
    return;
  }

  while ((c = gdb_read_byte()) != GDB_STUB_END)
  {
    cmd_bfr[cmd_len++] = c;
    if (cmd_len == sizeof cmd_bfr)
    {
      ERROR_LOG(GDB_STUB, "gdb: cmd_bfr overflow");
      gdb_nak();
      return;
    }
  }

  chk_read = hex2char(gdb_read_byte()) << 4;
  chk_read |= hex2char(gdb_read_byte());

  chk_calc = gdb_calc_chksum();

  if (chk_calc != chk_read)
  {
    ERROR_LOG(GDB_STUB,
              "gdb: invalid checksum: calculated %02x and read %02x for $%s# (length: %d)",
              chk_calc, chk_read, cmd_bfr, cmd_len);
    cmd_len = 0;

    gdb_nak();
    return;
  }

  DEBUG_LOG(GDB_STUB, "gdb: read command %c with a length of %d: %s", cmd_bfr[0], cmd_len, cmd_bfr);
  gdb_ack();
}
Beispiel #6
0
/**@brief Constructs String as string representation of byte sequence in hex form.
 * e.g 234F11A1...*/
String String::fromByteArray(const ByteArray& bytes)
{
	if(bytes.empty())
		return String();
	String result;
	for(ByteArray::const_iterator i = bytes.begin(); i != bytes.end(); ++i)
	{
		result += hex2char(((*i)/0x10)%0x10);
		result += hex2char((*i)%0x10);
	}
	return result;
}
Beispiel #7
0
static void gdb_write_register()
{
  u32 id;

  u8* bufptr = cmd_bfr + 3;

  id = hex2char(cmd_bfr[1]);
  if (cmd_bfr[2] != '=')
  {
    ++bufptr;
    id <<= 4;
    id |= hex2char(cmd_bfr[2]);
  }

  switch (id)
  {
  case 0 ... 31:
    GPR(id) = re32hex(bufptr);
    break;
  case 32 ... 63:
    riPS0(id - 32) = re64hex(bufptr);
    break;
  case 64:
    PC = re32hex(bufptr);
    break;
  case 65:
    MSR.Hex = re32hex(bufptr);
    break;
  case 66:
    PowerPC::SetCR(re32hex(bufptr));
    break;
  case 67:
    LR = re32hex(bufptr);
    break;
  case 68:
    CTR = re32hex(bufptr);
    break;
  case 69:
    PowerPC::ppcState.spr[SPR_XER] = re32hex(bufptr);
    break;
  case 70:
    // do nothing, we dont have MQ
    break;
  case 71:
    FPSCR.Hex = re32hex(bufptr);
    break;
  default:
    return gdb_reply("E01");
    break;
  }

  gdb_reply("OK");
}
Beispiel #8
0
static void gdb_read_register()
{
  static u8 reply[64];
  u32 id;

  memset(reply, 0, sizeof reply);
  id = hex2char(cmd_bfr[1]);
  if (cmd_bfr[2] != '\0')
  {
    id <<= 4;
    id |= hex2char(cmd_bfr[2]);
  }

  switch (id)
  {
  case 0 ... 31:
    wbe32hex(reply, GPR(id));
    break;
  case 32 ... 63:
    wbe64hex(reply, riPS0(id - 32));
    break;
  case 64:
    wbe32hex(reply, PC);
    break;
  case 65:
    wbe32hex(reply, MSR.Hex);
    break;
  case 66:
    wbe32hex(reply, PowerPC::GetCR());
    break;
  case 67:
    wbe32hex(reply, LR);
    break;
  case 68:
    wbe32hex(reply, CTR);
    break;
  case 69:
    wbe32hex(reply, PowerPC::ppcState.spr[SPR_XER]);
    break;
  case 70:
    wbe32hex(reply, 0x0BADC0DE);
    break;
  case 71:
    wbe32hex(reply, FPSCR.Hex);
    break;
  default:
    return gdb_reply("E01");
    break;
  }

  gdb_reply((char*)reply);
}
Beispiel #9
0
void buildAndCheckSentence(unsigned char characterIn, char *sentence, unsigned char *sentenceIndex, unsigned char *sentenceState, unsigned char *checksum, void (*processResult)(char *)) {
	// Full specification for NMEA0138 specifies a maximum sentence length
	// of 255 characters. We're going to ignore this for half the length as
	// we shouldn't get anything that big.

	// This contains the function's state of whether
	// it is currently building a sentence.
	// 0 - Awaiting start character ($)
	// 1 - Building sentence
	// 2 - Building first checksum character
	// 3 - Building second checksum character
	
	// We start recording a new sentence if we see a dollarsign.
	// The sentenceIndex is hard-set to 1 so that multiple dollar-signs
	// keep you at the beginning.
	if ((*sentenceState) == 0) {
		if (characterIn == '$') {
			(*sentenceIndex) = 0;
			(*sentenceState) = 1;
		}
	} else if ((*sentenceState) == 1) {
		// Record every character that comes in now that we're building a sentence.
		// Only stop if we run out of room or an asterisk is found.
		if (characterIn == '*') {
			(*sentenceState) = 2;
		} else if ((*sentenceIndex) > 127) {
			// If we've filled up the buffer, ignore the entire message as we can't store it all
			(*sentenceState) = 0;
		} else {
			sentence[(*sentenceIndex)++] = characterIn;
		}
	} else if ((*sentenceState) == 2) {
		// Record the first ASCII-hex character of the checksum byte.
		(*checksum) = hex2char(characterIn) << 4;
		(*sentenceState) = 3;
	} else if ((*sentenceState) == 3) {
		// Record the second ASCII-hex character of the checksum byte.
		(*checksum) |= hex2char(characterIn);

		// Now that we've compiled a complete GPS sentence, let's check the checksum and parse it.
		// This code currently only supports RMC and GGA messages.
		unsigned char test = getChecksum(sentence, (*sentenceIndex));
		if ((*checksum) == test) {
			processResult(sentence);
		}

		// We clear all state variables here regardless of success.
		(*sentenceState) = 0;
	}
}
Beispiel #10
0
void
gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx)
{
    char buf[sizeof(unsigned long) * 2 + 1];
    int i = sizeof(unsigned long) * 2;
    int width = int_size * 2;

    buf[sizeof(unsigned long) * 2] = 0;

    switch ( int_size )
    {
    case sizeof(u8):
    case sizeof(u16):
    case sizeof(u32):
    case sizeof(u64):
        break;
    default:
        dbg_printk("WARNING: %s x: 0x%lx int_size: %d\n",
                   __func__, x, int_size);
        break;
    }

    do {
        buf[--i] = hex2char(x & 15);
        x >>= 4;
    } while ( x );

    while ( (i + width) > (sizeof(unsigned long) * 2) )
        buf[--i] = '0';

    gdb_write_to_packet(&buf[i], width, ctx);
}
Beispiel #11
0
void
dbg_print_val(const char* s, unsigned long num)
{
#ifdef DEBUG
	for ( ; *s != '\0'; s++) {
		if (*s == '\n') {
			putc('\r');
			putc('\n');
		} else if (*s == '%') {
			s++;
			if (*s == 'c') {
				putc(*(char *)num);
			} else if (*s == 's') {
				dbg_print((char *)num);
			} else if (*s == 'x') {
				hex2char(num);
			} else {
				putc('%');
				putc(*s);
			}
		} else {
			putc(*s);
		}
    }
#endif
}
Beispiel #12
0
	string printByHex(const unsigned char* message, const int length)
	{
		string out;

		for (int i = 0; i < length; i++)
		{
			int k = (unsigned char)*(message + i)/ 16;
			out += hex2char(k);
			k = (unsigned char)*(message + i)% 16;
			out += hex2char(k);
			out += " ";
		}

		// LOG_DEBUG(out);

		return out;
	}
Beispiel #13
0
void
gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx)
{
    char buf[sizeof(unsigned long) * 2 + 1];
    int i, width = int_size * 2;

    buf[sizeof(unsigned long) * 2] = 0;

    switch ( int_size )
    {
    case sizeof(u8):
    case sizeof(u16):
    case sizeof(u32):
    case sizeof(u64):
        break;
    default:
        dbg_printk("WARNING: %s x: 0x%lx int_size: %d\n",
                   __func__, x, int_size);
        break;
    }

#ifdef __BIG_ENDIAN
    i = sizeof(unsigned long) * 2
    do {
        buf[--i] = hex2char(x & 15);
        x >>= 4;
    } while ( x );

    while ( (i + width) > (sizeof(unsigned long) * 2) )
        buf[--i] = '0';

    gdb_write_to_packet(&buf[i], width, ctx);
#elif defined(__LITTLE_ENDIAN)
    i = 0;
    while ( i < width )
    {
        buf[i++] = hex2char(x>>4);
        buf[i++] = hex2char(x);
        x >>= 8;
    }
    gdb_write_to_packet(buf, width, ctx);
#else
# error unknown endian
#endif
}
Beispiel #14
0
static int hexdump(char* buf, const char* ptr, size_t length)
{
    int idx = 0;
    buf[idx++] = '0';
    buf[idx++] = 'x';
    
    for (int i = 0; i < length; ++i) {
#if (defined __BIG_ENDIAN__)
	const unsigned char c = ptr[i];
#else
	const unsigned char c = ptr[length - 1 - i];
#endif
	buf[idx++] = hex2char(c / 16);
	buf[idx++] = hex2char(c % 16);
    }

    return idx;
}
Beispiel #15
0
static u32 re32hex(u8* p)
{
  u32 i;
  u32 res = 0;

  for (i = 0; i < 8; i++)
    res = (res << 4) | hex2char(p[i]);

  return res;
}
Beispiel #16
0
static u64 re64hex(u8* p)
{
  u32 i;
  u64 res = 0;

  for (i = 0; i < 16; i++)
    res = (res << 4) | hex2char(p[i]);

  return res;
}
Beispiel #17
0
/* RFC */
void unescape_url(char *url)
{
	int n, k;
	for (n = 0, k = 0; url[k]; ++n, ++k) {
		if ((url[n] = url[k]) == '%') {
			url[n] = hex2char(&url[k + 1]);
			k += 2;
		}
	}
	url[n] = '\0';
}
Beispiel #18
0
int gdbr_write_registers(libgdbr_t* g, char* registers) {
	// read current register set
	gdbr_read_registers(g);

	int x, len = strlen(registers);
	char* buff = calloc(len, sizeof(char));
	memcpy(buff, registers, len);
	char* reg = strtok(buff, ",");
	while ( reg != NULL ) {
		char* name_end = strchr(reg, '=');
		if (name_end == NULL) {
			printf("Malformed argument: %s\n", reg);
			free(buff);
			return -1;
		}
		*name_end = '\0'; // change '=' to '\0'

		// time to find the current register
		int i = 0;
		while ( g->registers[i].size > 0) {
			if (strcmp(g->registers[i].name, reg) == 0) {
				uint64_t register_size = g->registers[i].size;
				uint64_t offset = g->registers[i].offset;
				char* value = calloc (register_size * 2, sizeof(char));

				memset (value, '0', register_size * 2);
				name_end++; 
				// be able to take hex with and without 0x
				if (name_end[1] == 'x' || name_end[1] == 'X') name_end += 2;
				int val_len = strlen (name_end); // size of the rest
				strcpy (value+(register_size * 2 - val_len), name_end);

				for (x=0; x < register_size; x++) {
					g->data[offset + register_size - x - 1] = hex2char(&value[x * 2]);
				}
				free(value);
			}
			i++;
		}
		reg = strtok(NULL, " ,");
	}

	free(buff);

	uint64_t buffer_size = g->data_len * 2 + 8;
	char* command = calloc(buffer_size, sizeof(char));
	snprintf (command, buffer_size, "%s", CMD_WRITEREGS);
	pack_hex (g->data, g->data_len, command+1);
	send_command (g, command);
	read_packet (g);
	free (command);
	handle_G (g);
	return 0;
}
Beispiel #19
0
// key是静态文本,简单转化一下更好
// 客户端需这般处理,服务端不需要
std::string se(std::string src)
{
    string result;
    
    int len = src.length();
    for (int i = 0; i < len; i++) {
        int sum = src[i]*2 + i/2;
        result += hex2char( sum%16 );
    }
    
    return result;
}
Beispiel #20
0
void scan_ex(void)
{
	lcd_goto(0x40);
	char ad = 0x00;
	char test;
	char before_div;
	char dived_data[2];
	
	lcd_goto(0x00);
	
	while(1)
	{
	test = getch();
	
	data_div(test, dived_data);
	dived_data[0] = hex2char(dived_data[0]);
	dived_data[1] = hex2char(dived_data[1]);

	if(ad == 0x0F)
	ad = 0x40;
	else if(ad == 0x4F)
	ad = 0x00;
	else	
	ad++;
	lcd_goto(ad);

	lcd_putch(dived_data[0]);
	
	if(ad == 0x0F)
	ad = 0x40;
	else if(ad == 0x4F)
	ad = 0x00;
	else	
	ad++;
	lcd_goto(ad);
	
	lcd_putch(dived_data[1]);
	}
	
}
Beispiel #21
0
static void gdb_write_mem()
{
  u32 addr, len;
  u32 i;

  i = 1;
  addr = 0;
  while (cmd_bfr[i] != ',')
    addr = (addr << 4) | hex2char(cmd_bfr[i++]);
  i++;

  len = 0;
  while (cmd_bfr[i] != ':')
    len = (len << 4) | hex2char(cmd_bfr[i++]);
  DEBUG_LOG(GDB_STUB, "gdb: write memory: %08x bytes to %08x", len, addr);

  u8* dst = Memory::GetPointer(addr);
  if (!dst)
    return gdb_reply("E00");
  hex2mem(dst, cmd_bfr + i + 1, len);
  gdb_reply("OK");
}
Beispiel #22
0
// Hex code solver for a given string.
std::string stringSolver(const std::string& hexMessage)
{
     // Tokenize the encoded message
    std::vector<std::string> tokens;
    boost::split(tokens, hexMessage, boost::is_any_of(", \r\n"), boost::token_compress_on);
    std::vector<char> ret(tokens.size());
    ret.clear();
    for (auto tok : tokens) {
        if(!tok.empty())
            ret.push_back(hex2char(tok));
    }
    return std::string(&ret[0], ret.size() * sizeof(char));
}
Beispiel #23
0
static void gdb_remove_bp()
{
  u32 type, addr, len, i;

  type = hex2char(cmd_bfr[1]);
  switch (type)
  {
  case 0:
  case 1:
    type = GDB_BP_TYPE_X;
    break;
  case 2:
    type = GDB_BP_TYPE_W;
    break;
  case 3:
    type = GDB_BP_TYPE_R;
    break;
  case 4:
    type = GDB_BP_TYPE_A;
    break;
  default:
    return gdb_reply("E01");
  }

  addr = 0;
  len = 0;

  i = 3;
  while (cmd_bfr[i] != ',')
    addr = (addr << 4) | hex2char(cmd_bfr[i++]);
  i++;

  while (i < cmd_len)
    len = (len << 4) | hex2char(cmd_bfr[i++]);

  gdb_bp_remove(type, addr, len);
  gdb_reply("OK");
}
Beispiel #24
0
static void _gdb_add_bp()
{
  u32 type;
  u32 i, addr = 0, len = 0;

  type = hex2char(cmd_bfr[1]);
  switch (type)
  {
  case 0:
  case 1:
    type = GDB_BP_TYPE_X;
    break;
  case 2:
    type = GDB_BP_TYPE_W;
    break;
  case 3:
    type = GDB_BP_TYPE_R;
    break;
  case 4:
    type = GDB_BP_TYPE_A;
    break;
  default:
    return gdb_reply("E01");
  }

  i = 3;
  while (cmd_bfr[i] != ',')
    addr = addr << 4 | hex2char(cmd_bfr[i++]);
  i++;

  while (i < cmd_len)
    len = len << 4 | hex2char(cmd_bfr[i++]);

  if (!gdb_add_bp(type, addr, len))
    return gdb_reply("E02");
  gdb_reply("OK");
}
void long2ascii( signed long input_long, char * target_string )
{
	//Vars
	long decumulator;
	//int dec_fact = 10;
	char out_buffer[10];
	char target_string_ptr = 0;
	int out_buffer_ptr;

	//take care of the pesky negative.
	if( input_long < 0 )
	{
		target_string[target_string_ptr] = '-';
		target_string_ptr++;
		decumulator = input_long ^ 0xFFFFFFFF;  //convert to positive
		decumulator++;
	}
	else
	{
		decumulator = input_long;  //Leave as positive
	}
	//Perform the modulus math
	for( out_buffer_ptr = 0; out_buffer_ptr < 10; out_buffer_ptr++ )
	{
		//perform the mod
		//buffer the base 10 value
		int temp =  ( decumulator % 10 );
		out_buffer[out_buffer_ptr] = temp;
		//decrement the decumulator-- get rid of the last remainder and divide by 10
		decumulator = (decumulator - out_buffer[out_buffer_ptr]) / 10;
		//multiply by 10
		//dec_fact = dec_fact * 10;
	}

	//Now convert the base 10 int array to the string
	//First seek first non-zero place
	for( out_buffer_ptr = 9; (out_buffer[out_buffer_ptr] == 0) && (out_buffer_ptr > 0) ; out_buffer_ptr-- );  //oh, look at that tricky shit!

	//roll out the out_buffer upwards till pointer = 0
	for( ; out_buffer_ptr >= 0; out_buffer_ptr-- ) //What is this?  Is it even valid?
	{
		target_string[target_string_ptr] = hex2char(out_buffer[out_buffer_ptr]);
		target_string_ptr++;
	}
	//slap a null on it
	target_string[target_string_ptr] = 0x00;

	return;
}
Beispiel #26
0
static void
gdbstub_console_puts(const char *str)
{
    const char *p;

    gdb_start_packet(gdb_ctx);
    gdb_write_to_packet_char('O', gdb_ctx);

    for ( p = str; *p != '\0'; p++ )
    {
        gdb_write_to_packet_char(hex2char((*p>>4) & 0x0f), gdb_ctx );
        gdb_write_to_packet_char(hex2char((*p) & 0x0f), gdb_ctx );
    }

    gdb_send_packet(gdb_ctx);
}
const char* hex2str(const char* bin, int bin_size, char* buff, int buff_size)
{
	char* nptr = buff;
	
	if (NULL == buff) {
		return "buffer is NULL";
	}		
	if (buff_size < bin_size*2+1) {
		return "buffer too small";
	}
	
	while (bin_size--) {
		nptr = hex2char(nptr, *bin++);
	}
	
	*nptr = 0;
	
	return buff;
}
Beispiel #28
0
// md5加密后,根据key获得校验码
std::string mcc(std::string param, std::string key)
{
    string result;
    string md5Param = md5( param );
    
    int paramLenght = param.length();
    int keyLenght = param.length();
    
    int j = 0;
    for (int i = 0; i < paramLenght; i++) {
        if (j < keyLenght-1) {
            j++;
        } else {
            j = keyLenght/2;
        }
        
        int sum = param[i] + key[j] + i*2 + j*3;
        result += hex2char( sum%16 );  //求余并转为16进制
    }
    
    return result;
}
int main (int argc, char *argv[])
{
      FILE *fp  = NULL;
      char *buf = NULL, *getbuf(void);
      fpos_t rpos;
      int i, patterns, max_bytes = 0;
      LOGICAL hex2char(const char *, char *);

      if (2 > argc)                             /* no filename          */
            return 1;
      if (3 > argc)                             /* no argument          */
            return 2;
      if (NULL == (fp = fopen(argv[1], "r+b")))
            return 3;                           /* file open error      */
      if (NULL == (buf = getbuf()))
            return 4;                           /* no memory for buffer */

      patterns = argc - 2;                      /* process arguments    */
      for (i = 2; i < argc; ++i)
      {
            char *p, *ptr;

            if (NULL != (ptr = strtok(argv[i], ",")))
            {
                  p = search[i - 2].pattern;
                  do
                  {
                        search[i - 2].numbytes++;
                        if (1 == strlen(ptr))
                        {
                              *p++ = *ptr;
                              continue;
                        }
                        switch (toupper(LAST_CHAR(ptr)))
                        {
                        case 'D':
                              LAST_CHAR(ptr) = '\0';
                              *p++ = (char)atoi(ptr);
                              break;
                        case 'H':
                              LAST_CHAR(ptr) = '\0';
                              if (ERROR == hex2char(ptr, p++))
                                    return 5;
                              break;
                        default:
                              return 5;
                        }
                  } while (NULL != (ptr = strtok(NULL, ",")));
                  *p = '\0';
                  max_bytes = max(max_bytes, search[i - 2].numbytes);
            }
            else  return 5;
      }

      fgetpos(fp, &rpos);                       /* save where we are    */
      while (1)
      {
            int bytes, n;
            LOGICAL modified;

            if (max_bytes > (bytes = (int)fread(buf, 1, bufsize, fp)))
            {
                  if (0 == bytes && !feof(fp))
                        return 6;               /* something's wrong!   */
                  else  break;                  /* all done!            */
            }
            for (n = 0, modified = FALSE; n < patterns; ++n)
            {
                  /* check each pattern in turn                         */

                  for (i = 0; i < (bytes - max_bytes + 1); ++i)
                  {
                        int j;

                        if (buf[i] != *(search[n].pattern))
                              continue;
                        if (SUCCESS != strncmp(&buf[i],
                              search[n].pattern, search[n].numbytes))
                        {
                              continue;
                        }

                        /* found one! replace it in the buffer          */

                        for (j = 0; j < search[n].numbytes; ++j, ++i)
                              buf[i] = '\0';
                        modified = TRUE;
                  }
            }
            if (modified)                       /* write changes, if any*/
            {
                  fpos_t wpos = rpos;

                  fsetpos(fp, &wpos);
                  if (bytes != (int)fwrite(buf, 1, bytes, fp))
                        return 7;
                  fsetpos(fp, &rpos);
            }
            rpos += bytes - max_bytes + 1;      /* get another buffer   */
            fsetpos(fp, &rpos);
      }
      fclose(fp);
      return SUCCESS;
}
Beispiel #30
0
int gdbr_write_registers(libgdbr_t *g, char *registers) {
	uint64_t buffer_size;
	int ret, i = 0;
	unsigned int x, len;
	char *command, *reg, *buff;
	// read current register set

	if (!g) {
		return -1;
	}
	gdbr_read_registers (g);
	reg_cache.valid = false;
	len = strlen (registers);
	buff = calloc (len, sizeof (char));
	if (!buff) {
		return -1;
	}
	memcpy (buff, registers, len);
	reg = strtok (buff, ",");
	while (reg != NULL) {
		char *name_end = strchr (reg, '=');
		if (name_end == NULL) {
			eprintf ("Malformed argument: %s\n", reg);
			free (buff);
			return -1;
		}
		*name_end = '\0'; // change '=' to '\0'

		// time to find the current register
		while (g->registers[i].size > 0) {
			if (strcmp (g->registers[i].name, reg) == 0) {
				const ut64 register_size = g->registers[i].size;
				const ut64 offset = g->registers[i].offset;
				char *value = calloc (register_size + 1, 2);
				if (!value) {
					free (buff);
					return -1;
				}

				memset (value, '0', register_size * 2);
				name_end++;
				// be able to take hex with and without 0x
				if (name_end[1] == 'x' || name_end[1] == 'X') {
					name_end += 2;
				}
				const int val_len = strlen (name_end); // size of the rest
				strcpy (value + (register_size * 2 - val_len), name_end);

				for (x = 0; x < register_size; x++) {
					g->data[offset + register_size - x - 1] = hex2char (&value[x * 2]);
				}
				free (value);
			}
			i++;
		}
		reg = strtok (NULL, " ,");
	}

	free (buff);

	buffer_size = g->data_len * 2 + 8;
	command = calloc (buffer_size, sizeof(char));
	if (!command) {
		return -1;
	}
	snprintf (command, buffer_size, "%s", CMD_WRITEREGS);
	pack_hex (g->data, g->data_len, command + 1);
	ret = send_msg (g, command);
	if (ret < 0) {
		free (command);
		return ret;
	}
	read_packet (g);
	free (command);
	handle_G (g);
	return 0;
}