update for HEAD-2003091401
[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 NTSTATUS STDCALL
23 MmCopyToCaller(PVOID Dest, const VOID *Src, ULONG NumberOfBytes)
24 {
25   NTSTATUS Status;
26
27   if (ExGetPreviousMode() == UserMode)
28     {
29       if ((ULONG)Dest >= KERNEL_BASE)
30         {
31           return(STATUS_ACCESS_VIOLATION);
32         }
33       Status = MmSafeCopyToUser(Dest, Src, NumberOfBytes);
34       return(Status);
35     }
36   else
37     {
38       memcpy(Dest, Src, NumberOfBytes);
39       return(STATUS_SUCCESS);
40     }
41 }
42
43 NTSTATUS STDCALL
44 MmCopyFromCaller(PVOID Dest, const VOID *Src, ULONG NumberOfBytes)
45 {
46   NTSTATUS Status;
47
48   if (ExGetPreviousMode() == UserMode)
49     {
50       if ((ULONG)Src >= KERNEL_BASE)
51         {
52           return(STATUS_ACCESS_VIOLATION);
53         }
54       Status = MmSafeCopyFromUser(Dest, Src, NumberOfBytes);
55       return(Status);
56     }
57   else
58     {
59       memcpy(Dest, Src, NumberOfBytes);
60       return(STATUS_SUCCESS);
61     }
62 }
63
64
65 /*
66  * @implemented
67  */
68 ULONG
69 STDCALL
70 RtlCompareMemory(PVOID  Source1,
71         PVOID   Source2,
72         ULONG   Length)
73 /*
74  * FUNCTION: Compares blocks of memory and returns the number of equal bytes
75  * ARGUMENTS:
76  *      Source1 = Block to compare
77  *      Source2 = Block to compare
78  *      Length = Number of bytes to compare
79  * RETURNS: Number of equal bytes
80  */
81 {
82    ULONG i,total;
83
84    for (i=0,total=0;i<Length;i++)
85      {
86         if ( ((PUCHAR)Source1)[i] == ((PUCHAR)Source2)[i] )
87           {
88              total++;
89           }
90      }
91    return(total);
92 }
93
94
95 /*
96  * @implemented
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 /*
149  * @implemented
150  */
151 VOID
152 STDCALL
153 RtlFillMemory (
154         PVOID   Destination,
155         ULONG   Length,
156         UCHAR   Fill
157         )
158 {
159         memset(Destination,Fill,Length);
160 }
161
162
163 /*
164  * @implemented
165  */
166 VOID
167 STDCALL
168 RtlFillMemoryUlong (
169         PVOID   Destination,
170         ULONG   Length,
171         ULONG   Fill
172         )
173 {
174         PULONG Dest  = Destination;
175         ULONG  Count = Length / sizeof(ULONG);
176
177         while (Count > 0)
178         {
179                 *Dest = Fill;
180                 Dest++;
181                 Count--;
182         }
183 }
184
185
186 /*
187  * @implemented
188  */
189 VOID
190 STDCALL
191 RtlMoveMemory (
192         PVOID           Destination,
193         CONST VOID      * Source,
194         ULONG           Length
195         )
196 {
197         memmove (
198                 Destination,
199                 Source,
200                 Length
201                 );
202 }
203
204
205 /*
206  * @implemented
207  */
208 VOID
209 STDCALL
210 RtlZeroMemory (
211         PVOID   Destination,
212         ULONG   Length
213         )
214 {
215         RtlFillMemory (
216                 Destination,
217                 Length,
218                 0
219                 );
220 }
221
222 /* EOF */