int main (void) { // Read in number to print in hex print_string("Enter decimal number: "); input_num = read_int(); print_string("\nThe number in hex is: "); // Loop 8 times, once for each hex digit. loop_count = 8; while (loop_count != 0) { // Get the leftmost nibble (4 bits) of input_num. // The >> operation on an int var is arithmetic shift right. // We use the bitwise and to get the effect of a logical shift right. int left_nibble = (input_num >> 28) & 0xf; // Compute the hex character corresponding to the leftmost nibble. int hex_char = get_hex_char(left_nibble); // Output the hex character. print_char(hex_char); // Make the next nibble of input_num the leftmost one input_num = input_num << 4; loop_count--; } print_string("\n"); return 0; }
static void send_signal_reply_packet ( void ) { // In GDB jargon exceptions are called "signals" and have an associated signal ID. rsp_buf buf; buf.data[0] = 'S'; buf.data[1] = get_hex_char( rsp.sigval >> 4 ); buf.data[2] = get_hex_char( rsp.sigval % 16 ); buf.data[3] = 0; buf.len = strlen (buf.data); put_packet( rsp.client_fd, &buf ); }
char get_escape_char(const char *s, int *i) { char c = s[*i]; int j = *i + 1; char val; assert(c); switch (c) { case 'a': val = '\a'; break; case 'b': val = '\b'; break; case 't': val = '\t'; break; case 'n': val = '\n'; break; case 'v': val = '\v'; break; case 'f': val = '\f'; break; case 'r': val = '\r'; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': j--; /* need to re-read the first digit as * part of the octal value */ val = get_oct_char(s, &j); break; case 'x': val = get_hex_char(s, &j); break; default: val = c; } (*i) = j; return val; }
static void rsp_read_mem ( const rsp_buf * const buf ) { unsigned int addr; unsigned int len; assert( buf->data[0] == 'm' ); if ( 2 != sscanf( buf->data, "m%x,%x:", &addr, &len ) || len <= 0 ) { throw std::runtime_error( "Illegal read memory packet." ); } // Make sure we won't overflow the buffer (2 chars per byte). if ( len * 2 >= GDB_BUF_MAX ) { throw std::runtime_error( "The read memory packet's reponse would overflow the packet buffer." ); } std::vector< uint8_t > data; dbg_cpu0_read_mem( uint32_t( addr ), uint32_t( len ), &data ); const unsigned actually_read_len = data.size(); rsp_buf reply; for ( unsigned off = 0; off < actually_read_len; off++ ) { const unsigned char ch = data[ off ]; // printf( "Memory read, byte at %u: 0x%02X\n", off, ch ); reply.data[off * 2] = get_hex_char( ch >> 4 ); reply.data[off * 2 + 1] = get_hex_char( ch & 0xf ); } reply.data[actually_read_len * 2] = 0; // End of string. reply.len = actually_read_len * 2; put_packet( rsp.client_fd, &reply ); }
struct data data_copy_escape_string(const char *s, int len) { int i = 0; struct data d; char *q; d = data_grow_for(empty_data, strlen(s)+1); q = d.val; while (i < len) { char c = s[i++]; if (c != '\\') { q[d.len++] = c; continue; } c = s[i++]; assert(c); switch (c) { case 'a': q[d.len++] = '\a'; break; case 'b': q[d.len++] = '\b'; break; case 't': q[d.len++] = '\t'; break; case 'n': q[d.len++] = '\n'; break; case 'v': q[d.len++] = '\v'; break; case 'f': q[d.len++] = '\f'; break; case 'r': q[d.len++] = '\r'; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': i--; /* need to re-read the first digit as * part of the octal value */ q[d.len++] = get_oct_char(s, &i); break; case 'x': q[d.len++] = get_hex_char(s, &i); break; default: q[d.len++] = c; } } q[d.len++] = '\0'; return d; }