summaryrefslogtreecommitdiff
path: root/other/wrez/common.c
blob: f791e63e93128d7ceb86850139dbeed0496d5caa (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

#ifndef	COMMON_C
#define	COMMON_C

#ifndef NULL
#define NULL	((void *) 0)
#endif


static inline void
memcpy (void *dst, void *src, unsigned int len)
{
	__asm__ __volatile__ ("
		cld
		rep movsb
		" : : "c" (len), "S" (src), "D" (dst));
}


static inline int
memcmp (unsigned char *s1, unsigned char *s2, unsigned int len)
{
	register unsigned int	reg_ecx;

	__asm__ __volatile__ ("
		cld
		repe cmpsb
		je	lme
		incl	%%ecx
	lme:
		" : "=c" (reg_ecx) : "c" (len), "S" (s1), "D" (s2));

	return (reg_ecx);
}


static inline void
memset (void *dst, unsigned char wrbyte, unsigned int len)
{
	__asm__ __volatile__ ("
		cld
		rep stosb
		" : : "c" (len), "D" (dst), "a" (wrbyte));
}


static inline int
strcmp (unsigned char *s1, unsigned char *s2)
{
	register unsigned int	reg_ecx;

	__asm__ __volatile__ ("
		xorl	%%ecx, %%ecx
		xorl	%%eax, %%eax
		pushl	%%esi
		cld
	ls0:	lodsb
		incl	%%ecx
		or	%%eax, %%eax
		jnz	ls0

		popl	%%esi
		repe	cmpsb
		" : "=c" (reg_ecx) : "S" (s1), "D" (s2) : "eax");

	return (reg_ecx);
}


static inline int
strlen (unsigned char *s1)
{
	register unsigned int	reg_ecx;

	__asm__ __volatile__ ("
		xorl	%%eax, %%eax
		movl	%%eax, %%ecx
		decl	%%ecx
		repne scasb
		not	%%ecx
		decl	%%ecx
		" : "=c" (reg_ecx) : "D" (s1) : "eax");

	return (reg_ecx);
}


#if 0
/* gcc's version is smaller, doh!
 */
static inline int
strstr (unsigned char *s1_hay, unsigned char *s2_needle)
{
	register unsigned int	reg_ecx;

	__asm__ __volatile__ ("
		xorl	%%ecx, %%ecx
	lss_%=:	movb	(%%esi), %%al
		orb	%%al, %%al
		jz	lso_%=
		pushl	%%esi
		pushl	%%edi
	ls0_%=:	cmpsb
		jne	ls1_%=
		cmpb	$0x0, (%%edi)
		je	lse_%=
		jmp	ls0_%=
	ls1_%=:	popl	%%edi
		popl	%%esi
		incl	%%esi
		jmp	lss_%=
	lso_%=:	incl	%%ecx
	lse_%=:
		" : "=c" (reg_ecx) : "S" (s1_hay), "D" (s2_needle) : "eax");

}
#endif


static inline void
strcpy (unsigned char *dst, unsigned char *src)
{
	memcpy (dst, src, strlen (src) + 1);
}

#endif