+RtlCompareMemory()
[reactos.git] / ntoskrnl / rtl / mem.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            kernel/rtl/mem.c
6  * PURPOSE:         Memory functions
7  * PROGRAMMER:      David Welch (welch@mcmail.com)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/mm.h>
16
17 #define NDEBUG
18 #include <internal/debug.h>
19
20 /* FUNCTIONS *****************************************************************/
21
22 #ifndef LIBCAPTIVE
23
24 NTSTATUS STDCALL
25 MmCopyToCaller(PVOID Dest, PVOID Src, ULONG NumberOfBytes)
26 {
27   NTSTATUS Status;
28
29   if (ExGetPreviousMode() == UserMode)
30     {
31       if ((ULONG)Dest >= KERNEL_BASE)
32         {
33           return(STATUS_ACCESS_VIOLATION);
34         }
35       Status = MmSafeCopyToUser(Dest, Src, NumberOfBytes);
36       return(Status);
37     }
38   else
39     {
40       memcpy(Dest, Src, NumberOfBytes);
41       return(STATUS_SUCCESS);
42     }
43 }
44
45 NTSTATUS STDCALL
46 MmCopyFromCaller(PVOID Dest, PVOID Src, ULONG NumberOfBytes)
47 {
48   NTSTATUS Status;
49
50   if (ExGetPreviousMode() == UserMode)
51     {
52       if ((ULONG)Src >= KERNEL_BASE)
53         {
54           return(STATUS_ACCESS_VIOLATION);
55         }
56       Status = MmSafeCopyFromUser(Dest, Src, NumberOfBytes);
57       return(Status);
58     }
59   else
60     {
61       memcpy(Dest, Src, NumberOfBytes);
62       return(STATUS_SUCCESS);
63     }
64 }
65
66 #endif /* LIBCAPTIVE */
67
68 ULONG
69 STDCALL
70 RtlCompareMemory (
71         PVOID   Source1,
72         PVOID   Source2,
73         ULONG   Length
74         )
75 /*
76  * FUNCTION: Compares blocks of memory and returns the number of equal bytes
77  * ARGUMENTS:
78  *      Source1 = Block to compare
79  *      Source2 = Block to compare
80  *      Length = Number of bytes to compare
81  * RETURNS: Number of equal bytes
82  */
83 {
84    ULONG i,total;
85
86    for (i=0,total=0;i<Length;i++)
87      {
88         if ( ((PUCHAR)Source1)[i] == ((PUCHAR)Source2)[i] )
89           {
90              total++;
91           }
92      }
93    return(total);
94 }
95
96 #ifndef LIBCAPTIVE
97
98 ULONG
99 STDCALL
100 RtlCompareMemoryUlong (
101         PVOID   Source,
102         ULONG   Length,
103         ULONG   Value
104         )
105 /*
106  * FUNCTION: Compares a block of ULONGs with an ULONG and returns the number of equal bytes
107  * ARGUMENTS:
108  *      Source = Block to compare
109  *      Length = Number of bytes to compare
110  *      Value = Value to compare
111  * RETURNS: Number of equal bytes
112  */
113 {
114         PULONG ptr = (PULONG)Source;
115         ULONG  len = Length / sizeof(ULONG);
116         ULONG i;
117
118         for (i = 0; i < len; i++)
119         {
120                 if (*ptr != Value)
121                         break;
122                 ptr++;
123         }
124
125         return (ULONG)((PCHAR)ptr - (PCHAR)Source);
126 }
127
128
129 #if 0
130 VOID RtlCopyBytes(PVOID Destination,
131                   CONST VOID* Source,
132                   ULONG Length)
133 {
134    RtlCopyMemory(Destination,Source,Length);
135 }
136 #endif
137
138 #if 0
139 VOID RtlCopyMemory(VOID* Destination, CONST VOID* Source, ULONG Length)
140 {
141    DPRINT("RtlCopyMemory(Destination %x Source %x Length %d\n",
142           Destination,Source,Length);
143    memcpy(Destination,Source,Length);
144    DPRINT("*Destination %x\n",*(PULONG)Destination);
145 }
146 #endif
147
148 #endif /* LIBCAPTIVE */
149
150 VOID
151 STDCALL
152 RtlFillMemory (
153         PVOID   Destination,
154         ULONG   Length,
155         UCHAR   Fill
156         )
157 {
158         memset(Destination,Fill,Length);
159 }
160
161 #ifndef LIBCAPTIVE
162
163 VOID
164 STDCALL
165 RtlFillMemoryUlong (
166         PVOID   Destination,
167         ULONG   Length,
168         ULONG   Fill
169         )
170 {
171         PULONG Dest  = Destination;
172         ULONG  Count = Length / sizeof(ULONG);
173
174         while (Count > 0)
175         {
176                 *Dest = Fill;
177                 Dest++;
178                 Count--;
179         }
180 }
181
182
183 VOID
184 STDCALL
185 RtlMoveMemory (
186         PVOID           Destination,
187         CONST VOID      * Source,
188         ULONG           Length
189         )
190 {
191         memmove (
192                 Destination,
193                 Source,
194                 Length
195                 );
196 }
197
198 #endif /* LIBCAPTIVE */
199
200 VOID
201 STDCALL
202 RtlZeroMemory (
203         PVOID   Destination,
204         ULONG   Length
205         )
206 {
207         RtlFillMemory (
208                 Destination,
209                 Length,
210                 0
211                 );
212 }
213
214 /* EOF */