static void set_bitfield (char *f, bitfield *array, int value, unsigned int size, int lineno) { unsigned int i; if (strcmp (f, "CpuFP") == 0) { set_bitfield("Cpu387", array, value, size, lineno); set_bitfield("Cpu287", array, value, size, lineno); f = "Cpu8087"; } else if (strcmp (f, "Mmword") == 0) f= "Qword"; else if (strcmp (f, "Oword") == 0) f= "Xmmword"; for (i = 0; i < size; i++) if (strcasecmp (array[i].name, f) == 0) { array[i].value = value; return; } if (value) { const char *v = strchr (f, '='); if (v) { size_t n = v - f; char *end; for (i = 0; i < size; i++) if (strncasecmp (array[i].name, f, n) == 0) { value = strtol (v + 1, &end, 0); if (*end == '\0') { array[i].value = value; return; } break; } } } /* Handle CPU_XXX_FLAGS. */ if (!set_bitfield_from_cpu_flag_init (f, array, value, size, lineno)) return; if (lineno != -1) fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f); else fail (_("Unknown bitfield: %s\n"), f); }
static void process_i386_cpu_flag (FILE *table, char *flag, int macro, const char *comma, const char *indent) { char *str, *next, *last; bitfield flags [ARRAY_SIZE (cpu_flags)]; /* Copy the default cpu flags. */ memcpy (flags, cpu_flags, sizeof (cpu_flags)); if (strcasecmp (flag, "unknown") == 0) { unsigned int i; /* We turn on everything except for cpu64 in case of CPU_UNKNOWN_FLAGS. */ for (i = 0; i < ARRAY_SIZE (flags); i++) if (flags[i].position != Cpu64) flags[i].value = 1; } else if (strcmp (flag, "0")) { last = flag + strlen (flag); for (next = flag; next && next < last; ) { str = next_field (next, '|', &next); if (str) set_bitfield (str, flags, ARRAY_SIZE (flags)); } } output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro, comma, indent); }
static int set_bitfield_from_cpu_flag_init (char *f, bitfield *array, int value, unsigned int size, int lineno) { char *str, *next, *last; unsigned int i; for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++) if (strcmp (cpu_flag_init[i].name, f) == 0) { /* Turn on selective bits. */ char *init = xstrdup (cpu_flag_init[i].init); last = init + strlen (init); for (next = init; next && next < last; ) { str = next_field (next, '|', &next, last); if (str) set_bitfield (str, array, 1, size, lineno); } free (init); return 0; } return -1; }
static void process_i386_opcode_modifier (FILE *table, char *mod) { char *str, *next, *last; bitfield modifiers [ARRAY_SIZE (opcode_modifiers)]; /* Copy the default opcode modifier. */ memcpy (modifiers, opcode_modifiers, sizeof (modifiers)); if (strcmp (mod, "0")) { last = mod + strlen (mod); for (next = mod; next && next < last; ) { str = next_field (next, '|', &next); if (str) set_bitfield (str, modifiers, ARRAY_SIZE (modifiers)); } } output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers)); }
static void process_i386_operand_type (FILE *table, char *op, int macro, const char *indent, int lineno) { char *str, *next, *last; bitfield types [ARRAY_SIZE (operand_types)]; /* Copy the default operand type. */ memcpy (types, operand_types, sizeof (types)); if (strcmp (op, "0")) { last = op + strlen (op); for (next = op; next && next < last; ) { str = next_field (next, '|', &next, last); if (str) set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno); } } output_operand_type (table, types, ARRAY_SIZE (types), macro, indent); }
int Peer::process_messages() { //is handshake if (m_buf.length - m_buf.pos >= HANDSHAKE_LENGHT && m_buf.data[m_buf.pos] == '\x13' && memcmp(&m_buf.data[m_buf.pos + 1], "BitTorrent protocol", 19) == 0 && m_torrent->memcmp_infohash_bin((unsigned char*)&m_buf.data[m_buf.pos + 28])) { if (m_state != PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; m_buf.pos += HANDSHAKE_LENGHT; m_state = PEER_STATE_GENERAL_READY; #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received handshake\n"; #endif } char ip[16]; memset(ip, 0, 16); strcpy(ip, inet_ntoa(m_sock->m_peer.sin_addr)); uint32_t piece_count = m_torrent->get_piece_count(); BLOCK_ID block_id; while((int)m_buf.length - (int)m_buf.pos > 4) { uint32_t len = 0; memcpy(&len, &m_buf.data[m_buf.pos], 4); len = ntohl(len); if (m_buf.length - m_buf.pos < len + 4) goto end; m_buf.pos += 4; if (len != 0) { char id = m_buf.data[m_buf.pos++]; switch (id) { case '\x00'://choke: <len=0001><id=0> #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received choke"; #endif if ( m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; m_peer_choking = true; if (m_state == PEER_STATE_WAIT_UNCHOKE) return ERR_INTERNAL;//m_state = PEER_STATE_GENERAL_READY; иначе зацикливаемся break; case '\x01'://unchoke: <len=0001><id=1> #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received unchoke"; #endif if ( m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; m_peer_choking = false; if (m_state == PEER_STATE_WAIT_UNCHOKE) m_state = PEER_STATE_REQUEST_READY; break; case '\x02'://interested: <len=0001><id=2> #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received interested"; #endif if ( m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; m_peer_interested = true; send_unchoke(); break; case '\x03'://not interested: <len=0001><id=3> #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received not interested"; #endif if ( m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; m_peer_interested = false; break; case '\x04'://have: <len=0005><id=4><piece index> #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received have"; #endif if ( m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; PIECE_INDEX piece_index; memcpy(&piece_index, &m_buf.data[m_buf.pos], 4); piece_index = ntohl(piece_index); if (piece_index >= piece_count) return ERR_INTERNAL; m_available_pieces.insert(piece_index); set_bitfield(piece_index, piece_count, m_bitfield); break; case '\x05'://bitfield: <len=0001+X><id=5><bitfield> #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received bitfield"; #endif if (len - 1 != m_torrent->get_bitfield_length()) return ERR_INTERNAL; if (m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; memcpy(m_bitfield, &m_buf.data[m_buf.pos], len - 1); for(PIECE_INDEX i = 0; i < piece_count; i++) { if (bit_in_bitfield(i, piece_count, m_bitfield)) m_available_pieces.insert(i); } break; case '\x06'://request: <len=0013><id=6><index><begin><length> if ( m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; PIECE_INDEX index;//индекс куска BLOCK_OFFSET offset; uint32_t length; memcpy(&index,&m_buf.data[m_buf.pos], 4); index = ntohl(index); memcpy(&offset,&m_buf.data[m_buf.pos + 4], 4); offset = ntohl(offset); memcpy(&length,&m_buf.data[m_buf.pos + 8], 4); length = ntohl(length); //проверяем валидность индексов, смещений if (length == 0 || length > BLOCK_LENGTH || offset % BLOCK_LENGTH != 0) return ERR_INTERNAL; BLOCK_INDEX block_index; m_torrent->get_block_index_by_offset(index, offset, block_index) ; uint32_t real_block_length; m_torrent->get_block_length_by_index(index, block_index, real_block_length); if (real_block_length != length) return ERR_INTERNAL; //кладем индекс блока в очередь запросов generate_block_id(index, block_index, block_id); m_requests_queue.insert(block_id); #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received request piece=" << index << " block=" << block_index; #endif break; case '\x07'://piece: <len=0009+X><id=7><index><begin><block> { PIECE_INDEX index;//индекс куска BLOCK_OFFSET offset; uint32_t block_length = len - 9; if (m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; if (block_length == 0 || block_length > BLOCK_LENGTH) return ERR_INTERNAL; memcpy(&index,&m_buf.data[m_buf.pos], 4); index = ntohl(index); memcpy(&offset,&m_buf.data[m_buf.pos + 4], 4); offset = ntohl(offset); if (offset % BLOCK_LENGTH != 0) return ERR_INTERNAL; generate_block_id(index, offset / BLOCK_LENGTH, block_id); std::set<BLOCK_ID>::iterator iter = m_requested_blocks.find(block_id); #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received piece piece=" << index << " block=" << offset / BLOCK_LENGTH; #endif if (iter == m_requested_blocks.end()) return ERR_INTERNAL; if (m_torrent->save_block(index, offset, block_length, &m_buf.data[m_buf.pos + 8]) != ERR_NO_ERROR) return ERR_INTERNAL; m_requested_blocks.erase(iter); m_downloaded += block_length; break; } case '\x08'://cancel: <len=0013><id=8><index><begin><length> { if (m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; PIECE_INDEX index;//индекс куска BLOCK_OFFSET offset; uint32_t length; memcpy(&index,&m_buf.data[m_buf.pos], 4); index = ntohl(index); memcpy(&offset,&m_buf.data[m_buf.pos + 4], 4); offset = ntohl(offset); memcpy(&length,&m_buf.data[m_buf.pos + 8], 4); length = ntohl(length); //проверяем валидность индексов, смещений if (length == 0 || length > BLOCK_LENGTH || offset % BLOCK_LENGTH != 0) return ERR_INTERNAL; uint32_t block_index; m_torrent->get_block_index_by_offset(index, offset, block_index); #ifdef PEER_DEBUG logger::LOGGER() << "Peer " << m_ip.c_str() << ": is received cancel piece=" << index << " block=" << block_index; #endif uint32_t real_block_length; m_torrent->get_block_length_by_index(index, block_index, real_block_length); if (real_block_length != length) return ERR_INTERNAL; generate_block_id(index, block_index, block_id); m_requested_blocks.erase(block_id); break; } case '\x09'://port: <len=0003><id=9><listen-port> if (m_state == PEER_STATE_WAIT_HANDSHAKE) return ERR_INTERNAL; break; default: return ERR_INTERNAL; break; } m_buf.pos += len - 1; } } end: char temp[BUFFER_SIZE]; size_t len = m_buf.length - m_buf.pos; memcpy(temp, &m_buf.data[m_buf.pos], len); m_buf.length = len; m_buf.pos = 0; memcpy(m_buf.data, temp, len); return ERR_NO_ERROR; }
static void process_i386_cpu_flag (FILE *table, char *flag, int macro, const char *comma, const char *indent, int lineno) { char *str, *next, *last; unsigned int i; bitfield flags [ARRAY_SIZE (cpu_flags)]; /* Copy the default cpu flags. */ memcpy (flags, cpu_flags, sizeof (cpu_flags)); if (strcasecmp (flag, "unknown") == 0) { /* We turn on everything except for cpu64 in case of CPU_UNKNOWN_FLAGS. */ for (i = 0; i < ARRAY_SIZE (flags); i++) if (flags[i].position != Cpu64) flags[i].value = 1; } else if (flag[0] == '~') { last = flag + strlen (flag); if (flag[1] == '(') { last -= 1; next = flag + 2; if (*last != ')') fail (_("%s: %d: Missing `)' in bitfield: %s\n"), filename, lineno, flag); *last = '\0'; } else next = flag + 1; /* First we turn on everything except for cpu64. */ for (i = 0; i < ARRAY_SIZE (flags); i++) if (flags[i].position != Cpu64) flags[i].value = 1; /* Turn off selective bits. */ for (; next && next < last; ) { str = next_field (next, '|', &next, last); if (str) set_bitfield (str, flags, 0, ARRAY_SIZE (flags), lineno); } } else if (strcmp (flag, "0")) { /* Turn on selective bits. */ last = flag + strlen (flag); for (next = flag; next && next < last; ) { str = next_field (next, '|', &next, last); if (str) set_bitfield (str, flags, 1, ARRAY_SIZE (flags), lineno); } } output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro, comma, indent); }
static u16 set_ddr3_mem_reg_col(u16 reg, u16 col) { return set_bitfield(reg, col, 3, 0); }
static u16 set_ddr3_mem_reg_c1m(u16 reg, u16 c1m) { return set_bitfield(reg, c1m, 1, 13); }
static u16 set_ddr3_mem_reg_size(u16 reg, u16 size) { return set_bitfield(reg, size, 4, 8); }
static u16 set_ddr3_mem_reg_row(u16 reg, u16 row) { return set_bitfield(reg, row, 3, 5); }