Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / mm / memsafe.c
1 /* $Id$
2  * reactos memory user/kernel memory handling functions emulation of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "reactos/internal/mm.h"        /* self */
23 #include <glib/gmessages.h>
24 #include <string.h>
25
26
27 /**
28  * MmSafeCopyFromUser:
29  * @Dest: Destination memory area address.
30  * %NULL value is forbidden.
31  * @Src: Source memory area address.
32  * %NULL value is forbidden.
33  * @NumberOfBytes: Length of memory area.
34  *
35  * This function moves the data between two memory areas.
36  * Given memory areas must not overlap.
37  * libcaptive does not differentiate between user and kernel memory - this functions
38  * implements the simple memcpy() function.
39  * 
40  * Returns: %STATUS_SUCCESS if the memory was copied successfuly.
41  */
42 NTSTATUS MmSafeCopyFromUser(PVOID Dest,const VOID *Src,ULONG NumberOfBytes)
43 {
44         g_return_val_if_fail(Dest!=NULL,STATUS_INVALID_PARAMETER);
45         g_return_val_if_fail(Dest+NumberOfBytes>=Dest,STATUS_INVALID_PARAMETER);
46         g_return_val_if_fail(Src!=NULL,STATUS_INVALID_PARAMETER);
47         g_return_val_if_fail(Src+NumberOfBytes>=Src,STATUS_INVALID_PARAMETER);
48         /* do not overlap: */
49         g_return_val_if_fail((Dest+NumberOfBytes<=Src || Dest>=Src+NumberOfBytes),STATUS_INVALID_PARAMETER);
50
51         memcpy(Dest,Src,NumberOfBytes);
52
53         return STATUS_SUCCESS;
54 }
55
56
57 /**
58  * MmSafeCopyToUser:
59  * @Dest: Destination memory area address.
60  * %NULL value is forbidden.
61  * @Src: Source memory area address.
62  * %NULL value is forbidden.
63  * @NumberOfBytes: Length of memory area.
64  *
65  * This function moves the data between two memory areas.
66  * Given memory areas must not overlap.
67  * libcaptive does not differentiate between user and kernel memory - this functions
68  * implements the simple memcpy() function.
69  * 
70  * Returns: %STATUS_SUCCESS if the memory was copied successfuly.
71  */
72 NTSTATUS MmSafeCopyToUser(PVOID Dest,const VOID *Src,ULONG NumberOfBytes)
73 {
74         g_return_val_if_fail(Dest!=NULL,STATUS_INVALID_PARAMETER);
75         g_return_val_if_fail(Dest+NumberOfBytes>=Dest,STATUS_INVALID_PARAMETER);
76         g_return_val_if_fail(Src!=NULL,STATUS_INVALID_PARAMETER);
77         g_return_val_if_fail(Src+NumberOfBytes>=Src,STATUS_INVALID_PARAMETER);
78         /* do not overlap: */
79         g_return_val_if_fail((Dest+NumberOfBytes<=Src || Dest>=Src+NumberOfBytes),STATUS_INVALID_PARAMETER);
80
81         memcpy(Dest,Src,NumberOfBytes);
82
83         return STATUS_SUCCESS;
84 }