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