- Oops.. fix a bug in RtlRemoveUnicodePrefix: edit the parent, not the entry itself.
[reactos.git] / reactos / lib / rtl / mem.c
1 /* COPYRIGHT: See COPYING in the top level directory
2 * PROJECT: ReactOS system libraries
3 * FILE: lib/rtl/mem.c
4 * PURPOSE: Memory functions
5 * PROGRAMMER: David Welch (welch@mcmail.com)
6 */
7
8 /* INCLUDES *****************************************************************/
9
10 #include <rtl.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 #undef RtlUlonglongByteSwap
16 #undef RtlUlongByteSwap
17 #undef RtlUshortByteSwap
18
19 /* FUNCTIONS *****************************************************************/
20
21 /******************************************************************************
22 * RtlCompareMemory [NTDLL.@]
23 *
24 * Compare one block of memory with another
25 *
26 * PARAMS
27 * Source1 [I] Source block
28 * Source2 [I] Block to compare to Source1
29 * Length [I] Number of bytes to fill
30 *
31 * RETURNS
32 * The length of the first byte at which Source1 and Source2 differ, or Length
33 * if they are the same.
34 *
35 * @implemented
36 */
37 SIZE_T NTAPI
38 RtlCompareMemory(IN const VOID *Source1,
39 IN const VOID *Source2,
40 IN SIZE_T Length)
41 {
42 SIZE_T i;
43 for(i=0; (i<Length) && (((PUCHAR)Source1)[i]==((PUCHAR)Source2)[i]); i++)
44 ;
45 return i;
46 }
47
48
49 /*
50 * @implemented
51 */
52 ULONG
53 NTAPI
54 RtlCompareMemoryUlong (
55 PVOID Source,
56 ULONG Length,
57 ULONG Value
58 )
59 /*
60 * FUNCTION: Compares a block of ULONGs with an ULONG and returns the number of equal bytes
61 * ARGUMENTS:
62 * Source = Block to compare
63 * Length = Number of bytes to compare
64 * Value = Value to compare
65 * RETURNS: Number of equal bytes
66 */
67 {
68 PULONG ptr = (PULONG)Source;
69 ULONG len = Length / sizeof(ULONG);
70 ULONG i;
71
72 for (i = 0; i < len; i++)
73 {
74 if (*ptr != Value)
75 break;
76 ptr++;
77 }
78
79 return (ULONG)((PCHAR)ptr - (PCHAR)Source);
80 }
81
82
83 #undef RtlFillMemory
84 /*
85 * @implemented
86 */
87 VOID
88 NTAPI
89 RtlFillMemory (
90 PVOID Destination,
91 ULONG Length,
92 UCHAR Fill
93 )
94 {
95 memset(Destination, Fill, Length);
96 }
97
98
99
100 /*
101 * @implemented
102 */
103 VOID
104 NTAPI
105 RtlFillMemoryUlong (
106 PVOID Destination,
107 ULONG Length,
108 ULONG Fill
109 )
110 {
111 PULONG Dest = Destination;
112 ULONG Count = Length / sizeof(ULONG);
113
114 while (Count > 0)
115 {
116 *Dest = Fill;
117 Dest++;
118 Count--;
119 }
120 }
121
122
123 #undef RtlMoveMemory
124 /*
125 * @implemented
126 */
127 VOID
128 NTAPI
129 RtlMoveMemory (
130 PVOID Destination,
131 CONST VOID * Source,
132 ULONG Length
133 )
134 {
135 memmove (
136 Destination,
137 Source,
138 Length
139 );
140 }
141
142 /*
143 * @unimplemented
144 */
145 VOID
146 FASTCALL
147 RtlPrefetchMemoryNonTemporal(
148 IN PVOID Source,
149 IN SIZE_T Length
150 )
151 {
152 UNIMPLEMENTED;
153 }
154
155
156 #undef RtlZeroMemory
157 /*
158 * @implemented
159 */
160 VOID
161 NTAPI
162 RtlZeroMemory (
163 PVOID Destination,
164 ULONG Length
165 )
166 {
167 RtlFillMemory (
168 Destination,
169 Length,
170 0
171 );
172 }
173
174
175 /*************************************************************************
176 * RtlUshortByteSwap
177 *
178 * Swap the bytes of an unsigned short value.
179 *
180 * NOTES
181 * Based on the inline versions in Wine winternl.h
182 *
183 * @implemented
184 */
185 USHORT FASTCALL
186 RtlUshortByteSwap (IN USHORT Source)
187 {
188 return (Source >> 8) | (Source << 8);
189 }
190
191
192
193 /*************************************************************************
194 * RtlUlongByteSwap [NTDLL.@]
195 *
196 * Swap the bytes of an unsigned int value.
197 *
198 * NOTES
199 * Based on the inline versions in Wine winternl.h
200 *
201 * @implemented
202 */
203 ULONG
204 FASTCALL
205 RtlUlongByteSwap(
206 IN ULONG Source
207 )
208 {
209 #if defined(__i386__) && defined(__GNUC__)
210 ULONG ret;
211 __asm__("bswap %0" : "=r" (ret) : "0" (Source) );
212 return ret;
213 #else
214
215 return ((ULONG)RtlUshortByteSwap((USHORT)Source) << 16) | RtlUshortByteSwap((USHORT)(Source >> 16));
216 #endif
217 }
218
219
220 /*************************************************************************
221 * RtlUlonglongByteSwap
222 *
223 * Swap the bytes of an unsigned long long value.
224 *
225 * PARAMS
226 * i [I] Value to swap bytes of
227 *
228 * RETURNS
229 * The value with its bytes swapped.
230 *
231 * @implemented
232 */
233 ULONGLONG FASTCALL
234 RtlUlonglongByteSwap (IN ULONGLONG Source)
235 {
236 return ((ULONGLONG) RtlUlongByteSwap (Source) << 32) | RtlUlongByteSwap (Source>>32);
237 }
238
239
240 /* EOF */