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