]> Dogcows Code - chaz/thecheat/blob - VMRegion.h
update contact information and project URL
[chaz/thecheat] / VMRegion.h
1
2 //
3 // VMRegion 0.1
4 // Virtual Memory Wrapper
5 //
6 // Copyright (c) 2004, Charles McGarvey
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without modification, are
10 // permitted provided that the following conditions are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright notice, this list
13 // of conditions and the following disclaimer.
14 //
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this
16 // list of conditions and the following disclaimer in the documentation and/or other
17 // materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22 // SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 // DAMAGE.
29 //
30
31 #import <Cocoa/Cocoa.h>
32
33 #include <mach/mach_vm.h>
34 #include <mach/vm_map.h>
35
36
37 /* Compiler macros */
38 #if defined( __cplusplus )
39 #define VMREGION_EXPORT extern "C"
40 #define VMREGION_IMPORT extern "C"
41 #else
42 #define VMREGION_EXPORT extern
43 #define VMREGION_IMPORT extern
44 #endif
45
46 #if !defined( VMREGION_STATIC_INLINE )
47 #define VMREGION_STATIC_INLINE static __inline__
48 #endif
49
50 #if !defined( VMREGION_EXTERN_INLINE )
51 #define VMREGION_EXTERN_INLINE extern __inline__
52 #endif
53
54
55 // attributes of memory regions
56 enum _VMRegionAttributes
57 {
58 VMREGION_READABLE = 1,
59 VMREGION_WRITABLE = 2,
60 VMREGION_EXECUTABLE = 4
61 };
62
63
64 #pragma mark -
65 #pragma mark VMRegion
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67
68 typedef struct _VMRegion
69 {
70 // process information
71 pid_t _process;
72 // region information
73 mach_vm_address_t _address;
74 mach_vm_size_t _size;
75 unsigned _attributes;
76 } VMRegion;
77
78 // common regions
79 VMREGION_EXPORT const VMRegion VMNullRegion; /* <0,0,0> */
80
81
82 #pragma mark -
83 #pragma mark Utility VM Functions
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85
86 // get the number of regions a process has.
87 unsigned VMCountRegions( pid_t process );
88 unsigned VMCountRegionsWithAttributes( pid_t process, unsigned attribs );
89
90 // these functions return allocated VMRegion objects.
91 // use VMReleaseRegion(1) to free up used memory.
92 // returns nil on error or if there is no region after prevRegion.
93 // pass nil for prevRegion to access the first region.
94 VMRegion VMNextRegion( pid_t process, VMRegion previous );
95 VMRegion VMNextRegionWithAttributes( pid_t process, VMRegion previous, unsigned attribs );
96
97 // UTILITY functions - stop/resume processes
98 // returns YES on success, NO on failure
99 VMREGION_STATIC_INLINE BOOL VMStopProcess( pid_t process ) { return (kill( process, SIGSTOP ) == 0); }
100 VMREGION_STATIC_INLINE BOOL VMContinueProcess( pid_t process ) { return (kill( process, SIGCONT ) == 0); }
101
102 // lower-level reading/writing functions
103 // the returned NSData object should be retained by the caller.
104 NSData *VMReadData( pid_t process, mach_vm_address_t address, mach_vm_size_t size );
105 BOOL VMReadBytes( pid_t process, mach_vm_address_t address, void *bytes, mach_vm_size_t *size ); // size is # bytes read after call
106 BOOL VMWriteData( pid_t process, mach_vm_address_t address, NSData *data ); // returns YES on success, NO on failure
107 BOOL VMWriteBytes( pid_t process, mach_vm_address_t address, const void *bytes, mach_vm_size_t size );
108
109
110 #pragma mark -
111 #pragma mark Exported VM Functions
112 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113
114 VMREGION_EXPORT VMRegion VMMakeRegion( pid_t process, mach_vm_address_t address, mach_vm_size_t size );
115
116 VMREGION_EXPORT BOOL VMRegionSetData( VMRegion region, NSData *data );
117
118 VMREGION_EXPORT NSString *VMStringFromRegion( VMRegion region );
119
120
121 #pragma mark -
122 #pragma mark Imported VM Functions
123 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
124
125 VMREGION_STATIC_INLINE mach_vm_address_t VMRegionProcess( VMRegion region )
126 {
127 return region._process;
128 }
129
130 VMREGION_STATIC_INLINE mach_vm_address_t VMRegionAddress( VMRegion region )
131 {
132 return region._address;
133 }
134
135 VMREGION_STATIC_INLINE mach_vm_size_t VMRegionSize( VMRegion region )
136 {
137 return region._size;
138 }
139
140 VMREGION_STATIC_INLINE unsigned VMRegionAttributes( VMRegion region )
141 {
142 return region._attributes;
143 }
144
145 VMREGION_STATIC_INLINE BOOL VMRegionReadable( VMRegion region )
146 {
147 return region._attributes & VMREGION_READABLE;
148 }
149
150 VMREGION_STATIC_INLINE BOOL VMRegionWritable( VMRegion region )
151 {
152 return region._attributes & VMREGION_WRITABLE;
153 }
154
155 VMREGION_STATIC_INLINE BOOL VMRegionExecutable( VMRegion region )
156 {
157 return region._attributes & VMREGION_EXECUTABLE;
158 }
159
160
161 VMREGION_STATIC_INLINE NSData *VMRegionData( VMRegion region )
162 {
163 return VMReadData( region._process, region._address, region._size );
164 }
165
166 VMREGION_STATIC_INLINE BOOL VMRegionBytes( VMRegion region, void *bytes, mach_vm_size_t *size )
167 {
168 *size = region._size;
169 return VMReadBytes( region._process, region._address, bytes, size );
170 }
171
172
173
174 VMREGION_STATIC_INLINE BOOL VMRegionIsNotNull( VMRegion region )
175 {
176 return (region._process != 0);
177 }
178
179 VMREGION_STATIC_INLINE BOOL VMEqualRegions( VMRegion region1, VMRegion region2 )
180 {
181 return (region1._process == region2._process &&
182 region1._address == region2._address &&
183 region1._size == region2._size &&
184 region1._attributes == region2._attributes);
185 }
186
187
This page took 0.036051 seconds and 4 git commands to generate.