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