branch update for HEAD-2003050101
[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
67 /* FIXME: It should return the length of initial sequence of matching characters,
68  * not the total amount of matching/differing bytes. We are not interested in
69  * the tail after the first non-matching byte.
70  * W32 doc formulation is a bit misleading.
71  */
72 ULONG
73 STDCALL
74 RtlCompareMemory(PVOID  Source1,
75         PVOID   Source2,
76         ULONG   Length)
77 /*
78  * FUNCTION: Compares blocks of memory and returns the number of equal bytes
79  * ARGUMENTS:
80  *      Source1 = Block to compare
81  *      Source2 = Block to compare
82  *      Length = Number of bytes to compare
83  * RETURNS: Number of equal bytes
84  */
85 {
86    ULONG i,total;
87
88    for (i=0,total=0;i<Length;i++)
89      {
90         if ( ((PUCHAR)Source1)[i] == ((PUCHAR)Source2)[i] )
91           {
92              total++;
93           }
94      }
95    return(total);
96 }
97
98
99 ULONG
100 STDCALL
101 RtlCompareMemoryUlong (
102         PVOID   Source,
103         ULONG   Length,
104         ULONG   Value
105         )
106 /*
107  * FUNCTION: Compares a block of ULONGs with an ULONG and returns the number of equal bytes
108  * ARGUMENTS:
109  *      Source = Block to compare
110  *      Length = Number of bytes to compare
111  *      Value = Value to compare
112  * RETURNS: Number of equal bytes
113  */
114 {
115         PULONG ptr = (PULONG)Source;
116         ULONG  len = Length / sizeof(ULONG);
117         ULONG i;
118
119         for (i = 0; i < len; i++)
120         {
121                 if (*ptr != Value)
122                         break;
123                 ptr++;
124         }
125
126         return (ULONG)((PCHAR)ptr - (PCHAR)Source);
127 }
128
129
130 #if 0
131 VOID RtlCopyBytes(PVOID Destination,
132                   CONST VOID* Source,
133                   ULONG Length)
134 {
135    RtlCopyMemory(Destination,Source,Length);
136 }
137 #endif
138
139 #if 0
140 VOID RtlCopyMemory(VOID* Destination, CONST VOID* Source, ULONG Length)
141 {
142    DPRINT("RtlCopyMemory(Destination %x Source %x Length %d\n",
143           Destination,Source,Length);
144    memcpy(Destination,Source,Length);
145    DPRINT("*Destination %x\n",*(PULONG)Destination);
146 }
147 #endif
148
149 #endif /* LIBCAPTIVE */
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 #ifndef LIBCAPTIVE
163
164 VOID
165 STDCALL
166 RtlFillMemoryUlong (
167         PVOID   Destination,
168         ULONG   Length,
169         ULONG   Fill
170         )
171 {
172         PULONG Dest  = Destination;
173         ULONG  Count = Length / sizeof(ULONG);
174
175         while (Count > 0)
176         {
177                 *Dest = Fill;
178                 Dest++;
179                 Count--;
180         }
181 }
182
183
184 VOID
185 STDCALL
186 RtlMoveMemory (
187         PVOID           Destination,
188         CONST VOID      * Source,
189         ULONG           Length
190         )
191 {
192         memmove (
193                 Destination,
194                 Source,
195                 Length
196                 );
197 }
198
199 #endif /* LIBCAPTIVE */
200
201 VOID
202 STDCALL
203 RtlZeroMemory (
204         PVOID   Destination,
205         ULONG   Length
206         )
207 {
208         RtlFillMemory (
209                 Destination,
210                 Length,
211                 0
212                 );
213 }
214
215 /* EOF */