Example #1
0
File: dns.c Project: jintwo/sns
void unpack_request(char *buf, dns_packet_t *packet)
{
    dns_header_t header;
    unpack_header(buf, &header);
    packet->header = header;

    char *qdata = buf + sizeof(dns_header_t);
    packet->questions = calloc(packet->header.qdcount, sizeof(dns_question_t));
    dns_question_t *questions = packet->questions;
    for (int i = 0; i < packet->header.qdcount; i++) {
        dns_question_t question;
        unpack_question(qdata, &question);
        questions[i] = question;
#ifdef DEBUG
        printf("query for: %s\n", label_to_string(question->label, NULL));
#endif
    }
    packet->answers = NULL;
}
Example #2
0
int write_inst(struct self_s *self, struct string_s *string, struct instruction_s *instruction, int instruction_number, struct label_s *labels)
{
	int ret = 1; /* Default to failed */
	int tmp;
	int tmp_state = 0;
	int n, l;
	char buffer[1024];
	struct external_entry_point_s *external_entry_points = self->external_entry_points;

	debug_print(DEBUG_OUTPUT, 1, "string len = 0x%x, max = 0x%x\n", string->len, string->max);

	debug_print(DEBUG_OUTPUT, 1, "opcode = 0x%x\n", instruction->opcode);
	debug_print(DEBUG_OUTPUT, 1, "opcode = 0x%x\n", instruction->flags);
	tmp = snprintf(buffer, 1023, "// 0x%04x:%s%s",
		instruction_number,
		opcode_table[instruction->opcode],
		dis_flags_table[instruction->flags]);
	tmp = string_cat(string, buffer, strlen(buffer));

	switch (instruction->opcode) {
	case MOV:
	case SEX:
	case ZEXT:
		if (instruction->srcA.indirect ||
			(instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case LOAD:
		if (!instruction->srcA.indirect ||
			(instruction->srcB.indirect) ||
			(instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d,",
			indirect_table[instruction->srcA.indirect],
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcB.store],
			instruction->srcB.index,
			instruction->srcB.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case STORE:
		if (instruction->srcA.indirect ||
			(instruction->srcB.indirect) ||
			(!instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcB.store],
			instruction->srcB.index,
			instruction->srcB.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d",
			indirect_table[instruction->dstA.indirect],
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case ADD:
	case ADC:
	case SUB:
	case SBB:
	case MUL:
	case IMUL:
	case rAND:
	case OR:
	case XOR:
	case SHL:
	case SHR:
	case SAL:
	case SAR:
	case NOT:
	case NEG:
	/* FIXME: Add DIV */
	//case DIV:
		if (instruction->srcA.indirect ||
			(instruction->srcB.indirect) ||
			(instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcB.store],
			instruction->srcB.index,
			instruction->srcB.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case GEP1:
		if (instruction->srcA.indirect ||
			(instruction->srcB.indirect) ||
			(instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		if (instruction->srcB.index > INT64_MAX) {
			tmp = snprintf(buffer, 1023, " %s-0x%"PRIx64"/%d,",
				store_table[instruction->srcB.store],
				-instruction->srcB.index,
				instruction->srcB.value_size);
		} else {
			tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
				store_table[instruction->srcB.store],
				instruction->srcB.index,
				instruction->srcB.value_size);
		}
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case CMP:
	case TEST:
		if (instruction->srcA.indirect ||
			(instruction->srcB.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->srcB.store],
			instruction->srcB.index,
			instruction->srcB.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case ICMP:
		if (instruction->srcA.indirect ||
			(instruction->srcB.indirect) ||
			(instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " COND 0x%x,",
			instruction->predicate);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcB.store],
			instruction->srcB.index,
			instruction->srcB.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case JMP:
		if (instruction->srcA.indirect) {
			tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d,",
				indirect_table[instruction->srcA.indirect],
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else {
			tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		}
		if (instruction->dstA.indirect) {
			tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d",
				indirect_table[instruction->dstA.indirect],
				store_table[instruction->dstA.store],
				instruction->dstA.index,
				instruction->dstA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else {
			tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
				store_table[instruction->dstA.store],
				instruction->dstA.index,
				instruction->dstA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		}
		ret = 0;
		break;
	case BC:
		if (instruction->srcA.indirect) {
			tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d,",
				indirect_table[instruction->srcA.indirect],
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else {
			tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		}
		ret = 0;
		break;
	case JMPT:
		if (instruction->srcA.indirect) {
			debug_print(DEBUG_OUTPUT, 1, "JMPT 0x%x 0x%x 0x%x\n", instruction->srcA.indirect, instruction->srcA.store, instruction->srcA.value_size);
			/* FIXME: This processing should not be in an output function. It should be in the EXE function. */
			if (instruction->srcA.indirect > 4) {
				instruction->srcA.indirect = 0;
			}
			if (instruction->srcA.indirect < 0) {
				instruction->srcA.indirect = 0;
			}
			if (instruction->srcA.store > 4) {
				instruction->srcA.store = 0;
			}
			if (instruction->srcA.store < 0) {
				instruction->srcA.store = 0;
			}
			debug_print(DEBUG_OUTPUT, 1, "JMPT 0x%x 0x%x 0x%x\n", instruction->srcA.indirect, instruction->srcA.store, instruction->srcA.value_size);
			tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d,",
				indirect_table[instruction->srcA.indirect],
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else {
			tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		}
		if (instruction->dstA.indirect) {
			tmp = snprintf(buffer, 1023, " %s[%s0x%"PRIx64"]/%d",
				indirect_table[instruction->dstA.indirect],
				store_table[instruction->dstA.store],
				instruction->dstA.index,
				instruction->dstA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else {
			tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
				store_table[instruction->dstA.store],
				instruction->dstA.index,
				instruction->dstA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		}
		ret = 0;
		break;
	case IF:
		tmp = snprintf(buffer, 1023, " cond=%"PRIu64"", instruction->srcA.index);
		tmp = string_cat(string, buffer, strlen(buffer));
		tmp = snprintf(buffer, 1023, " JMP-REL=0x%"PRIx64"", instruction->dstA.index);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	case CALL:
#if 0
		/* FIXME: This processing should not be in an output function. It should be in the EXE function. */
		if (instruction->srcA.relocated == 2) {
			for (n = 0; n < EXTERNAL_ENTRY_POINTS_MAX; n++) {
				if ((external_entry_points[n].valid != 0) &&
					(external_entry_points[n].type == 1) &&
					(external_entry_points[n].value == instruction->srcA.relocated_index)) {
					//debug_print(DEBUG_OUTPUT, 1, "found external relocated 0x%x\n", n);
					instruction->srcA.index = n;
					instruction->srcA.relocated = 1;
					break;
				}
			}
		}
#endif
		if ((instruction->srcA.indirect == IND_DIRECT) &&
			(instruction->srcA.relocated == 1)) {
			tmp = snprintf(buffer, 1023, " CALL2 0x%"PRIx64":%s(",
				instruction->srcA.index,
				external_entry_points[instruction->srcA.index].name);
			tmp = string_cat(string, buffer, strlen(buffer));
			tmp_state = 0;
			l = instruction->srcA.index;
			for (n = 0; n < external_entry_points[l].reg_params_size; n++) {
				struct label_s *label;
				label = &labels[external_entry_points[l].param_reg_label[reg_params_order[n]]];
				debug_print(DEBUG_OUTPUT, 1, "reg_params_order = 0x%x, label->value = 0x%"PRIx64"\n", reg_params_order[n], label->value);
				if ((label->scope == 2) &&
					(label->type == 1)) {
					if (tmp_state > 0) {
						snprintf(buffer, 1023, ", ");
						tmp = string_cat(string, buffer, strlen(buffer));
					}
					snprintf(buffer, 1023, "int%"PRId64"_t ",
						label->size_bits);
					tmp = string_cat(string, buffer, strlen(buffer));
					tmp = label_to_string(label, buffer, 1023);
					//tmp = snprintf(buffer, 1023, "%s", buffer);
					tmp = string_cat(string, buffer, strlen(buffer));
					tmp_state++;
				}
			}
			for (n = 0; n < external_entry_points[l].reg_params_size; n++) {
				struct label_s *label;
				label = &labels[external_entry_points[l].param_reg_label[reg_params_order[n]]];
				if ((label->scope == 2) &&
					(label->type == 1)) {
					continue;
				}
				if (tmp_state > 0) {
					snprintf(buffer, 1023, ", ");
					tmp = string_cat(string, buffer, strlen(buffer));
				}
				snprintf(buffer, 1023, "int%"PRId64"_t ",
					label->size_bits);
				tmp = string_cat(string, buffer, strlen(buffer));
				tmp = label_to_string(label, buffer, 1023);
				//tmp = snprintf(buffer, 1023, "%s", buffer);
				tmp = string_cat(string, buffer, strlen(buffer));
				tmp_state++;
			}
			tmp = snprintf(buffer, 1023, ");");
			tmp = string_cat(string, buffer, strlen(buffer));
		} else if (instruction->srcA.indirect == IND_MEM) {
			tmp = snprintf(buffer, 1023, "(*r0x%"PRIx64") ();", 
				instruction->srcA.index);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else if (instruction->srcA.store == STORE_REG) {
			tmp = snprintf(buffer, 1023, " (%s0x%"PRIx64"/%d) ();",
				store_table[instruction->srcA.store],
				instruction->srcA.index,
				instruction->srcA.value_size);
			tmp = string_cat(string, buffer, strlen(buffer));
		} else {
			tmp = snprintf(buffer, 1023, " CALL FAILED index=0x%"PRIx64"",
				instruction->srcA.index);
			tmp = string_cat(string, buffer, strlen(buffer));
		}
		ret = 0;
		break;
	case NOP:
		//tmp = snprintf(buffer, 1023, "");
		ret = 0;
		break;
	case RET:
		//tmp = snprintf(buffer, 1023, "");
		ret = 0;
		break;
	case TRUNC:
		if (instruction->srcA.indirect ||
			(instruction->dstA.indirect)) {
			ret = 1;
			break;
		}
		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d,",
			store_table[instruction->srcA.store],
			instruction->srcA.index,
			instruction->srcA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));

		tmp = snprintf(buffer, 1023, " %s0x%"PRIx64"/%d",
			store_table[instruction->dstA.store],
			instruction->dstA.index,
			instruction->dstA.value_size);
		tmp = string_cat(string, buffer, strlen(buffer));
		ret = 0;
		break;
	default:
		debug_print(DEBUG_OUTPUT, 1, "Print inst fails. Opcode = 0x%x\n", instruction->opcode);
		exit(1);
	}
	return ret;
}