ecf03848dff912930f2b17569f7d8b5f12054020
[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, PVOID 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, PVOID 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 ULONG
66 STDCALL
67 RtlCompareMemory(PVOID  Source1,
68         PVOID   Source2,
69         ULONG   Length)
70 /*
71  * FUNCTION: Compares blocks of memory and returns the number of equal bytes
72  * ARGUMENTS:
73  *      Source1 = Block to compare
74  *      Source2 = Block to compare
75  *      Length = Number of bytes to compare
76  * RETURNS: Number of equal bytes
77  */
78 {
79    ULONG i,total;
80
81    for (i=0,total=0;i<Length;i++)
82      {
83         if ( ((PUCHAR)Source1)[i] == ((PUCHAR)Source2)[i] )
84           {
85              total++;
86           }
87      }
88    return(total);
89 }
90
91
92 ULONG
93 STDCALL
94 RtlCompareMemoryUlong (
95         PVOID   Source,
96         ULONG   Length,
97         ULONG   Value
98         )
99 /*
100  * FUNCTION: Compares a block of ULONGs with an ULONG and returns the number of equal bytes
101  * ARGUMENTS:
102  *      Source = Block to compare
103  *      Length = Number of bytes to compare
104  *      Value = Value to compare
105  * RETURNS: Number of equal bytes
106  */
107 {
108         PULONG ptr = (PULONG)Source;
109         ULONG  len = Length / sizeof(ULONG);
110         ULONG i;
111
112         for (i = 0; i < len; i++)
113         {
114                 if (*ptr != Value)
115                         break;
116                 ptr++;
117         }
118
119         return (ULONG)((PCHAR)ptr - (PCHAR)Source);
120 }
121
122
123 #if 0
124 VOID RtlCopyBytes(PVOID Destination,
125                   CONST VOID* Source,
126                   ULONG Length)
127 {
128    RtlCopyMemory(Destination,Source,Length);
129 }
130 #endif
131
132 #if 0
133 VOID RtlCopyMemory(VOID* Destination, CONST VOID* Source, ULONG Length)
134 {
135    DPRINT("RtlCopyMemory(Destination %x Source %x Length %d\n",
136           Destination,Source,Length);
137    memcpy(Destination,Source,Length);
138    DPRINT("*Destination %x\n",*(PULONG)Destination);
139 }
140 #endif
141
142 VOID
143 STDCALL
144 RtlFillMemory (
145         PVOID   Destination,
146         ULONG   Length,
147         UCHAR   Fill
148         )
149 {
150         memset(Destination,Fill,Length);
151 }
152
153
154 VOID
155 STDCALL
156 RtlFillMemoryUlong (
157         PVOID   Destination,
158         ULONG   Length,
159         ULONG   Fill
160         )
161 {
162         PULONG Dest  = Destination;
163         ULONG  Count = Length / sizeof(ULONG);
164
165         while (Count > 0)
166         {
167                 *Dest = Fill;
168                 Dest++;
169                 Count--;
170         }
171 }
172
173
174 VOID
175 STDCALL
176 RtlMoveMemory (
177         PVOID           Destination,
178         CONST VOID      * Source,
179         ULONG           Length
180         )
181 {
182         memmove (
183                 Destination,
184                 Source,
185                 Length
186                 );
187 }
188
189
190 VOID
191 STDCALL
192 RtlZeroMemory (
193         PVOID   Destination,
194         ULONG   Length
195         )
196 {
197         RtlFillMemory (
198                 Destination,
199                 Length,
200                 0
201                 );
202 }
203
204 /* EOF */