aboutsummaryrefslogtreecommitdiff
path: root/src/third_party/libdisasm/x86_imm.c
blob: cd59bfc9abf8a57ce6fbd23ed08d6f713e9a8787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "qword.h"
#include "x86_imm.h"

#include <stdio.h>

unsigned int x86_imm_signsized( unsigned char * buf, size_t buf_len,
				void *dest, unsigned int size ) {
	signed char *cp = (signed char *) dest;
	signed short *sp = (signed short *) dest;
	int32_t *lp = (int32_t *) dest;
	qword_t *qp = (qword_t *) dest;

	if ( size > buf_len ) {
		return 0;
	}

	/* Copy 'size' bytes from *buf to *op
	 * return number of bytes copied */
	switch (size) {
		case 1:		/* BYTE */
			*cp =  *((signed char *) buf);
			break;
		case 2:		/* WORD */
			*sp =  *((signed short *) buf);
			break;
		case 6:
		case 8:		/* QWORD */
			*qp = *((qword_t *) buf);
			break;
		case 4:		/* DWORD */
		default:
			*lp = *((int32_t *) buf);
			break;
	}
	return (size);
}

unsigned int x86_imm_sized( unsigned char * buf, size_t buf_len, void *dest,
			    unsigned int size ) {
	unsigned char *cp = (unsigned char *) dest;
	unsigned short *sp = (unsigned short *) dest;
	uint32_t *lp = (uint32_t *) dest;
	qword_t *qp = (qword_t *) dest;

	if ( size > buf_len ) {
		return 0;
	}

	/* Copy 'size' bytes from *buf to *op
	 * return number of bytes copied */
	switch (size) {
		case 1:		/* BYTE */
			*cp =  *((unsigned char *) buf);
			break;
		case 2:		/* WORD */
			*sp =  *((unsigned short *) buf);
			break;
		case 6:
		case 8:		/* QWORD */
			*qp = *((qword_t *) buf);
			break;
		case 4:		/* DWORD */
		default:
			*lp = *((uint32_t *) buf);
			break;
	}

	return (size);
}