Implemented ImageList_GetDragImage.
[wine/wine-kai.git] / server / mapping.c
blob8f4508be85899ef910eda8f0332d27006e9958b2
1 /*
2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
14 #include "winnt.h"
15 #include "winbase.h"
17 #include "handle.h"
18 #include "thread.h"
19 #include "request.h"
21 struct mapping
23 struct object obj; /* object header */
24 int size_high; /* mapping size */
25 int size_low; /* mapping size */
26 int protect; /* protection flags */
27 struct file *file; /* file mapped */
28 int header_size; /* size of headers (for PE image mapping) */
29 void *base; /* default base addr (for PE image mapping) */
30 struct file *shared_file; /* temp file for shared PE mapping */
31 int shared_size; /* shared mapping total size */
32 struct mapping *shared_next; /* next in shared PE mapping list */
33 struct mapping *shared_prev; /* prev in shared PE mapping list */
36 static int mapping_get_fd( struct object *obj );
37 static int mapping_get_info( struct object *obj, struct get_file_info_reply *reply );
38 static void mapping_dump( struct object *obj, int verbose );
39 static void mapping_destroy( struct object *obj );
41 static const struct object_ops mapping_ops =
43 sizeof(struct mapping), /* size */
44 mapping_dump, /* dump */
45 no_add_queue, /* add_queue */
46 NULL, /* remove_queue */
47 NULL, /* signaled */
48 NULL, /* satisfied */
49 NULL, /* get_poll_events */
50 NULL, /* poll_event */
51 mapping_get_fd, /* get_fd */
52 no_flush, /* flush */
53 mapping_get_info, /* get_file_info */
54 mapping_destroy /* destroy */
57 static struct mapping *shared_first;
59 #ifdef __i386__
61 /* These are always the same on an i386, and it will be faster this way */
62 # define page_mask 0xfff
63 # define page_shift 12
64 # define init_page_size() do { /* nothing */ } while(0)
66 #else /* __i386__ */
68 static int page_shift, page_mask;
70 static void init_page_size(void)
72 int page_size;
73 # ifdef HAVE_GETPAGESIZE
74 page_size = getpagesize();
75 # else
76 # ifdef __svr4__
77 page_size = sysconf(_SC_PAGESIZE);
78 # else
79 # error Cannot get the page size on this platform
80 # endif
81 # endif
82 page_mask = page_size - 1;
83 /* Make sure we have a power of 2 */
84 assert( !(page_size & page_mask) );
85 page_shift = 0;
86 while ((1 << page_shift) != page_size) page_shift++;
88 #endif /* __i386__ */
90 #define ROUND_ADDR(addr) \
91 ((int)(addr) & ~page_mask)
93 #define ROUND_SIZE(addr,size) \
94 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
96 /* get the fd to use for mmaping a file */
97 inline static int get_mmap_fd( struct file *file )
99 struct object *obj;
100 if (!(obj = (struct object *)file))
102 set_error( STATUS_INVALID_HANDLE );
103 return -1;
105 return obj->ops->get_fd( obj );
108 /* find the shared PE mapping for a given mapping */
109 static struct file *get_shared_file( struct mapping *mapping )
111 struct mapping *ptr;
113 for (ptr = shared_first; ptr; ptr = ptr->shared_next)
114 if (is_same_file( ptr->file, mapping->file ))
115 return (struct file *)grab_object( ptr->shared_file );
116 return NULL;
119 /* allocate and fill the temp file for a shared PE image mapping */
120 static int build_shared_mapping( struct mapping *mapping, int fd,
121 IMAGE_SECTION_HEADER *sec, int nb_sec )
123 int i, max_size, total_size, pos;
124 char *buffer = NULL;
125 int shared_fd;
126 long toread;
128 /* compute the total size of the shared mapping */
130 total_size = max_size = 0;
131 for (i = 0; i < nb_sec; i++)
133 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
134 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
136 int size = ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
137 if (size > max_size) max_size = size;
138 total_size += size;
141 if (!(mapping->shared_size = total_size)) return 1; /* nothing to do */
143 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
145 /* create a temp file for the mapping */
147 if (!(mapping->shared_file = create_temp_file( GENERIC_READ|GENERIC_WRITE ))) goto error;
148 if (!grow_file( mapping->shared_file, 0, total_size )) goto error;
149 if ((shared_fd = get_mmap_fd( mapping->shared_file )) == -1) goto error;
151 if (!(buffer = malloc( max_size ))) goto error;
153 /* copy the shared sections data into the temp file */
155 for (i = pos = 0; i < nb_sec; i++)
157 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
158 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
159 if (lseek( shared_fd, pos, SEEK_SET ) != pos) goto error;
160 pos += ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
161 if (sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
162 if (!sec[i].PointerToRawData || !sec[i].SizeOfRawData) continue;
163 if (lseek( fd, sec[i].PointerToRawData, SEEK_SET ) != sec[i].PointerToRawData) goto error;
164 toread = sec[i].SizeOfRawData;
165 while (toread)
167 long res = read( fd, buffer + sec[i].SizeOfRawData - toread, toread );
168 if (res <= 0) goto error;
169 toread -= res;
171 if (write( shared_fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
173 free( buffer );
174 return 1;
176 error:
177 if (buffer) free( buffer );
178 return 0;
181 /* retrieve the mapping parameters for an executable (PE) image */
182 static int get_image_params( struct mapping *mapping )
184 IMAGE_DOS_HEADER dos;
185 IMAGE_NT_HEADERS nt;
186 IMAGE_SECTION_HEADER *sec = NULL;
187 int fd, filepos, size;
189 /* load the headers */
191 if ((fd = get_mmap_fd( mapping->file )) == -1) return 0;
192 filepos = lseek( fd, 0, SEEK_SET );
193 if (read( fd, &dos, sizeof(dos) ) != sizeof(dos)) goto error;
194 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
195 if (lseek( fd, dos.e_lfanew, SEEK_SET ) == -1) goto error;
196 if (read( fd, &nt, sizeof(nt) ) != sizeof(nt)) goto error;
197 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
199 /* load the section headers */
201 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
202 if (!(sec = malloc( size ))) goto error;
203 if (read( fd, sec, size ) != size) goto error;
205 if (!build_shared_mapping( mapping, fd, sec, nt.FileHeader.NumberOfSections )) goto error;
207 if (mapping->shared_file) /* link it in the list */
209 if ((mapping->shared_next = shared_first)) shared_first->shared_prev = mapping;
210 mapping->shared_prev = NULL;
211 shared_first = mapping;
214 mapping->size_low = ROUND_SIZE( 0, nt.OptionalHeader.SizeOfImage );
215 mapping->size_high = 0;
216 mapping->base = (void *)nt.OptionalHeader.ImageBase;
217 mapping->header_size = ROUND_SIZE( mapping->base, nt.OptionalHeader.SizeOfHeaders );
218 mapping->protect = VPROT_IMAGE;
220 /* sanity check */
221 if (mapping->header_size > mapping->size_low) goto error;
223 lseek( fd, filepos, SEEK_SET );
224 free( sec );
225 return 1;
227 error:
228 lseek( fd, filepos, SEEK_SET );
229 if (sec) free( sec );
230 set_error( STATUS_INVALID_FILE_FOR_SECTION );
231 return 0;
235 static struct object *create_mapping( int size_high, int size_low, int protect,
236 handle_t handle, const WCHAR *name, size_t len )
238 struct mapping *mapping;
239 int access = 0;
241 if (!page_mask) init_page_size();
243 if (!(mapping = create_named_object( &mapping_ops, name, len )))
244 return NULL;
245 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
246 return &mapping->obj; /* Nothing else to do */
248 mapping->header_size = 0;
249 mapping->base = NULL;
250 mapping->shared_file = NULL;
251 mapping->shared_size = 0;
253 if (protect & VPROT_READ) access |= GENERIC_READ;
254 if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
256 if (handle)
258 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
259 if (protect & VPROT_IMAGE)
261 if (!get_image_params( mapping )) goto error;
262 return &mapping->obj;
264 if (!size_high && !size_low)
266 struct get_file_info_reply reply;
267 struct object *obj = (struct object *)mapping->file;
268 obj->ops->get_file_info( obj, &reply );
269 size_high = reply.size_high;
270 size_low = ROUND_SIZE( 0, reply.size_low );
272 else if (!grow_file( mapping->file, size_high, size_low )) goto error;
274 else /* Anonymous mapping (no associated file) */
276 if ((!size_high && !size_low) || (protect & VPROT_IMAGE))
278 set_error( STATUS_INVALID_PARAMETER );
279 mapping->file = NULL;
280 goto error;
282 if (!(mapping->file = create_temp_file( access ))) goto error;
283 if (!grow_file( mapping->file, size_high, size_low )) goto error;
285 mapping->size_high = size_high;
286 mapping->size_low = ROUND_SIZE( 0, size_low );
287 mapping->protect = protect;
288 return &mapping->obj;
290 error:
291 release_object( mapping );
292 return NULL;
295 static void mapping_dump( struct object *obj, int verbose )
297 struct mapping *mapping = (struct mapping *)obj;
298 assert( obj->ops == &mapping_ops );
299 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
300 "shared_file=%p shared_size=%08x ",
301 mapping->size_high, mapping->size_low, mapping->protect, mapping->file,
302 mapping->header_size, mapping->base, mapping->shared_file, mapping->shared_size );
303 dump_object_name( &mapping->obj );
304 fputc( '\n', stderr );
307 static int mapping_get_fd( struct object *obj )
309 struct mapping *mapping = (struct mapping *)obj;
310 assert( obj->ops == &mapping_ops );
311 return get_mmap_fd( mapping->file );
314 static int mapping_get_info( struct object *obj, struct get_file_info_reply *reply )
316 struct mapping *mapping = (struct mapping *)obj;
317 struct object *file = (struct object *)mapping->file;
319 assert( obj->ops == &mapping_ops );
320 assert( file );
321 return file->ops->get_file_info( file, reply );
324 static void mapping_destroy( struct object *obj )
326 struct mapping *mapping = (struct mapping *)obj;
327 assert( obj->ops == &mapping_ops );
328 if (mapping->file) release_object( mapping->file );
329 if (mapping->shared_file)
331 release_object( mapping->shared_file );
332 if (mapping->shared_next) mapping->shared_next->shared_prev = mapping->shared_prev;
333 if (mapping->shared_prev) mapping->shared_prev->shared_next = mapping->shared_next;
334 else shared_first = mapping->shared_next;
338 int get_page_size(void)
340 if (!page_mask) init_page_size();
341 return page_mask + 1;
344 /* create a file mapping */
345 DECL_HANDLER(create_mapping)
347 struct object *obj;
349 reply->handle = 0;
350 if ((obj = create_mapping( req->size_high, req->size_low,
351 req->protect, req->file_handle,
352 get_req_data(), get_req_data_size() )))
354 int access = FILE_MAP_ALL_ACCESS;
355 if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
356 reply->handle = alloc_handle( current->process, obj, access, req->inherit );
357 release_object( obj );
361 /* open a handle to a mapping */
362 DECL_HANDLER(open_mapping)
364 reply->handle = open_object( get_req_data(), get_req_data_size(),
365 &mapping_ops, req->access, req->inherit );
368 /* get a mapping information */
369 DECL_HANDLER(get_mapping_info)
371 struct mapping *mapping;
373 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
374 0, &mapping_ops )))
376 reply->size_high = mapping->size_high;
377 reply->size_low = mapping->size_low;
378 reply->protect = mapping->protect;
379 reply->header_size = mapping->header_size;
380 reply->base = mapping->base;
381 reply->shared_file = 0;
382 reply->shared_size = mapping->shared_size;
383 reply->drive_type = get_file_drive_type( mapping->file );
384 if (mapping->shared_file)
385 reply->shared_file = alloc_handle( current->process, mapping->shared_file,
386 GENERIC_READ|GENERIC_WRITE, 0 );
387 release_object( mapping );