Change __darwin__ to __APPLE__.
[wine] / memory / selector.c
1 /*
2  * Selector manipulation functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <string.h>
25
26 #include "winerror.h"
27 #include "wine/winbase16.h"
28 #include "miscemu.h"
29 #include "selectors.h"
30 #include "stackframe.h"
31 #include "wine/server.h"
32 #include "wine/debug.h"
33 #include "toolhelp.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(selector);
36
37 #define LDT_SIZE 8192
38
39 /* get the number of selectors needed to cover up to the selector limit */
40 inline static WORD get_sel_count( WORD sel )
41 {
42     return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
43 }
44
45
46 /***********************************************************************
47  *           AllocSelectorArray   (KERNEL.206)
48  */
49 WORD WINAPI AllocSelectorArray16( WORD count )
50 {
51     WORD i, sel = wine_ldt_alloc_entries( count );
52
53     if (sel)
54     {
55         LDT_ENTRY entry;
56         wine_ldt_set_base( &entry, 0 );
57         wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
58         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
59         for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
60     }
61     return sel;
62 }
63
64
65 /***********************************************************************
66  *           AllocSelector   (KERNEL.175)
67  */
68 WORD WINAPI AllocSelector16( WORD sel )
69 {
70     WORD newsel, count, i;
71
72     count = sel ? get_sel_count(sel) : 1;
73     newsel = wine_ldt_alloc_entries( count );
74     TRACE("(%04x): returning %04x\n", sel, newsel );
75     if (!newsel) return 0;
76     if (!sel) return newsel;  /* nothing to copy */
77     for (i = 0; i < count; i++)
78     {
79         LDT_ENTRY entry;
80         wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
81         wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
82     }
83     return newsel;
84 }
85
86
87 /***********************************************************************
88  *           FreeSelector   (KERNEL.176)
89  */
90 WORD WINAPI FreeSelector16( WORD sel )
91 {
92     LDT_ENTRY entry;
93
94     wine_ldt_get_entry( sel, &entry );
95     if (wine_ldt_is_empty( &entry )) return sel;  /* error */
96 #ifdef __i386__
97     /* Check if we are freeing current %fs selector */
98     if (!((wine_get_fs() ^ sel) & ~3))
99         WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
100 #endif  /* __i386__ */
101     wine_ldt_free_entries( sel, 1 );
102     return 0;
103 }
104
105
106 /***********************************************************************
107  *           SELECTOR_SetEntries
108  *
109  * Set the LDT entries for an array of selectors.
110  */
111 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
112 {
113     LDT_ENTRY entry;
114     WORD i, count;
115
116     wine_ldt_set_base( &entry, base );
117     wine_ldt_set_limit( &entry, size - 1 );
118     wine_ldt_set_flags( &entry, flags );
119     count = (size + 0xffff) / 0x10000;
120     for (i = 0; i < count; i++)
121     {
122         wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
123         wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
124         /* yep, Windows sets limit like that, not 64K sel units */
125         wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
126     }
127 }
128
129
130 /***********************************************************************
131  *           SELECTOR_AllocBlock
132  *
133  * Allocate selectors for a block of linear memory.
134  */
135 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
136 {
137     WORD sel, count;
138
139     if (!size) return 0;
140     count = (size + 0xffff) / 0x10000;
141     sel = wine_ldt_alloc_entries( count );
142     if (sel) SELECTOR_SetEntries( sel, base, size, flags );
143     return sel;
144 }
145
146
147 /***********************************************************************
148  *           SELECTOR_FreeBlock
149  *
150  * Free a block of selectors.
151  */
152 void SELECTOR_FreeBlock( WORD sel )
153 {
154     WORD i, count = get_sel_count( sel );
155
156     TRACE("(%04x,%d)\n", sel, count );
157     for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
158 }
159
160
161 /***********************************************************************
162  *           SELECTOR_ReallocBlock
163  *
164  * Change the size of a block of selectors.
165  */
166 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
167 {
168     LDT_ENTRY entry;
169     int oldcount, newcount;
170
171     if (!size) size = 1;
172     wine_ldt_get_entry( sel, &entry );
173     oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
174     newcount = (size + 0xffff) >> 16;
175
176     sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
177     if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
178     return sel;
179 }
180
181
182 /***********************************************************************
183  *           PrestoChangoSelector   (KERNEL.177)
184  */
185 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
186 {
187     LDT_ENTRY entry;
188     wine_ldt_get_entry( selSrc, &entry );
189     /* toggle the executable bit */
190     entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
191     wine_ldt_set_entry( selDst, &entry );
192     return selDst;
193 }
194
195
196 /***********************************************************************
197  *           AllocCStoDSAlias   (KERNEL.170)
198  *           AllocAlias         (KERNEL.172)
199  */
200 WORD WINAPI AllocCStoDSAlias16( WORD sel )
201 {
202     WORD newsel;
203     LDT_ENTRY entry;
204
205     newsel = wine_ldt_alloc_entries( 1 );
206     TRACE("(%04x): returning %04x\n",
207                       sel, newsel );
208     if (!newsel) return 0;
209     wine_ldt_get_entry( sel, &entry );
210     entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
211     wine_ldt_set_entry( newsel, &entry );
212     return newsel;
213 }
214
215
216 /***********************************************************************
217  *           AllocDStoCSAlias   (KERNEL.171)
218  */
219 WORD WINAPI AllocDStoCSAlias16( WORD sel )
220 {
221     WORD newsel;
222     LDT_ENTRY entry;
223
224     newsel = wine_ldt_alloc_entries( 1 );
225     TRACE("(%04x): returning %04x\n",
226                       sel, newsel );
227     if (!newsel) return 0;
228     wine_ldt_get_entry( sel, &entry );
229     entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
230     wine_ldt_set_entry( newsel, &entry );
231     return newsel;
232 }
233
234
235 /***********************************************************************
236  *           LongPtrAdd   (KERNEL.180)
237  */
238 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
239 {
240     LDT_ENTRY entry;
241     wine_ldt_get_entry( SELECTOROF(ptr), &entry );
242     wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
243     wine_ldt_set_entry( SELECTOROF(ptr), &entry );
244 }
245
246
247 /***********************************************************************
248  *             GetSelectorBase   (KERNEL.186)
249  */
250 DWORD WINAPI GetSelectorBase( WORD sel )
251 {
252     void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
253
254     /* if base points into DOSMEM, assume we have to
255      * return pointer into physical lower 1MB */
256
257     return DOSMEM_MapLinearToDos( base );
258 }
259
260
261 /***********************************************************************
262  *             SetSelectorBase   (KERNEL.187)
263  */
264 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
265 {
266     LDT_ENTRY entry;
267     wine_ldt_get_entry( sel, &entry );
268     wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
269     wine_ldt_set_entry( sel, &entry );
270     return sel;
271 }
272
273
274 /***********************************************************************
275  *           GetSelectorLimit   (KERNEL.188)
276  */
277 DWORD WINAPI GetSelectorLimit16( WORD sel )
278 {
279     return wine_ldt_copy.limit[sel >> __AHSHIFT];
280 }
281
282
283 /***********************************************************************
284  *           SetSelectorLimit   (KERNEL.189)
285  */
286 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
287 {
288     LDT_ENTRY entry;
289     wine_ldt_get_entry( sel, &entry );
290     wine_ldt_set_limit( &entry, limit );
291     wine_ldt_set_entry( sel, &entry );
292     return sel;
293 }
294
295
296 /***********************************************************************
297  *           SelectorAccessRights   (KERNEL.196)
298  */
299 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
300 {
301     LDT_ENTRY entry;
302     wine_ldt_get_entry( sel, &entry );
303
304     if (op == 0)  /* get */
305     {
306         return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
307     }
308     else  /* set */
309     {
310         entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
311         entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
312         wine_ldt_set_entry( sel, &entry );
313         return 0;
314     }
315 }
316
317
318 /***********************************************************************
319  *           IsBadCodePtr   (KERNEL.336)
320  */
321 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
322 {
323     WORD sel;
324     LDT_ENTRY entry;
325
326     sel = SELECTOROF(lpfn);
327     if (!sel) return TRUE;
328     wine_ldt_get_entry( sel, &entry );
329     if (wine_ldt_is_empty( &entry )) return TRUE;
330     /* check for code segment, ignoring conforming, read-only and accessed bits */
331     if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
332     if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
333     return FALSE;
334 }
335
336
337 /***********************************************************************
338  *           IsBadStringPtr   (KERNEL.337)
339  */
340 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
341 {
342     WORD sel;
343     LDT_ENTRY entry;
344
345     sel = SELECTOROF(ptr);
346     if (!sel) return TRUE;
347     wine_ldt_get_entry( sel, &entry );
348     if (wine_ldt_is_empty( &entry )) return TRUE;
349     /* check for data or readable code segment */
350     if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
351     if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
352     if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
353     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
354     return FALSE;
355 }
356
357
358 /***********************************************************************
359  *           IsBadHugeReadPtr   (KERNEL.346)
360  */
361 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
362 {
363     WORD sel;
364     LDT_ENTRY entry;
365
366     sel = SELECTOROF(ptr);
367     if (!sel) return TRUE;
368     wine_ldt_get_entry( sel, &entry );
369     if (wine_ldt_is_empty( &entry )) return TRUE;
370     /* check for data or readable code segment */
371     if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
372     if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
373     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
374     return FALSE;
375 }
376
377
378 /***********************************************************************
379  *           IsBadHugeWritePtr   (KERNEL.347)
380  */
381 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
382 {
383     WORD sel;
384     LDT_ENTRY entry;
385
386     sel = SELECTOROF(ptr);
387     if (!sel) return TRUE;
388     wine_ldt_get_entry( sel, &entry );
389     if (wine_ldt_is_empty( &entry )) return TRUE;
390     /* check for writeable data segment, ignoring expand-down and accessed flags */
391     if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
392     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
393     return FALSE;
394 }
395
396 /***********************************************************************
397  *           IsBadReadPtr   (KERNEL.334)
398  */
399 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
400 {
401     return IsBadHugeReadPtr16( ptr, size );
402 }
403
404
405 /***********************************************************************
406  *           IsBadWritePtr   (KERNEL.335)
407  */
408 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
409 {
410     return IsBadHugeWritePtr16( ptr, size );
411 }
412
413
414 /***********************************************************************
415  *           IsBadFlatReadWritePtr   (KERNEL.627)
416  */
417 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
418 {
419     return bWrite? IsBadHugeWritePtr16( ptr, size )
420                  : IsBadHugeReadPtr16( ptr, size );
421 }
422
423
424 /***********************************************************************
425  *           MemoryRead   (TOOLHELP.78)
426  */
427 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
428 {
429     LDT_ENTRY entry;
430     DWORD limit;
431
432     wine_ldt_get_entry( sel, &entry );
433     if (wine_ldt_is_empty( &entry )) return 0;
434     limit = wine_ldt_get_limit( &entry );
435     if (offset > limit) return 0;
436     if (offset + count > limit + 1) count = limit + 1 - offset;
437     memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
438     return count;
439 }
440
441
442 /***********************************************************************
443  *           MemoryWrite   (TOOLHELP.79)
444  */
445 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
446 {
447     LDT_ENTRY entry;
448     DWORD limit;
449
450     wine_ldt_get_entry( sel, &entry );
451     if (wine_ldt_is_empty( &entry )) return 0;
452     limit = wine_ldt_get_limit( &entry );
453     if (offset > limit) return 0;
454     if (offset + count > limit) count = limit + 1 - offset;
455     memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
456     return count;
457 }
458
459 /************************************* Win95 pointer mapping functions *
460  *
461  */
462
463 struct mapls_entry
464 {
465     struct mapls_entry *next;
466     void               *addr;   /* linear address */
467     int                 count;  /* ref count */
468     WORD                sel;    /* selector */
469 };
470
471 static struct mapls_entry *first_entry;
472
473
474 /***********************************************************************
475  *           MapLS   (KERNEL32.@)
476  *           MapLS   (KERNEL.358)
477  *
478  * Maps linear pointer to segmented.
479  */
480 SEGPTR WINAPI MapLS( LPCVOID ptr )
481 {
482     struct mapls_entry *entry, *free = NULL;
483     void *base;
484     SEGPTR ret = 0;
485
486     if (!HIWORD(ptr)) return (SEGPTR)ptr;
487
488     base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
489     HeapLock( GetProcessHeap() );
490     for (entry = first_entry; entry; entry = entry->next)
491     {
492         if (entry->addr == base) break;
493         if (!entry->count) free = entry;
494     }
495
496     if (!entry)
497     {
498         if (!free)  /* no free entry found, create a new one */
499         {
500             if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
501             if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
502             {
503                 HeapFree( GetProcessHeap(), 0, free );
504                 goto done;
505             }
506             free->count = 0;
507             free->next = first_entry;
508             first_entry = free;
509         }
510         SetSelectorBase( free->sel, (DWORD)base );
511         free->addr = base;
512         entry = free;
513     }
514     entry->count++;
515     ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
516  done:
517     HeapUnlock( GetProcessHeap() );
518     return ret;
519 }
520
521 /***********************************************************************
522  *           UnMapLS   (KERNEL32.@)
523  *           UnMapLS   (KERNEL.359)
524  *
525  * Free mapped selector.
526  */
527 void WINAPI UnMapLS( SEGPTR sptr )
528 {
529     struct mapls_entry *entry;
530     WORD sel = SELECTOROF(sptr);
531
532     if (sel)
533     {
534         HeapLock( GetProcessHeap() );
535         for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
536         if (entry && entry->count > 0) entry->count--;
537         HeapUnlock( GetProcessHeap() );
538     }
539 }
540
541 /***********************************************************************
542  *           MapSL   (KERNEL32.@)
543  *           MapSL   (KERNEL.357)
544  *
545  * Maps fixed segmented pointer to linear.
546  */
547 LPVOID WINAPI MapSL( SEGPTR sptr )
548 {
549     return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
550 }
551
552 /***********************************************************************
553  *           MapSLFix   (KERNEL32.@)
554  *
555  * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
556  * unexpected linear address change when GlobalCompact() shuffles
557  * moveable blocks.
558  */
559
560 LPVOID WINAPI MapSLFix( SEGPTR sptr )
561 {
562     return MapSL(sptr);
563 }
564
565 /***********************************************************************
566  *           UnMapSLFixArray   (KERNEL32.@)
567  */
568
569 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
570 {
571     /* Must not change EAX, hence defined as 'register' function */
572 }
573
574 /***********************************************************************
575  *           GetThreadSelectorEntry   (KERNEL32.@)
576  */
577 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
578 {
579 #ifdef __i386__
580     BOOL ret;
581
582     if (!(sel & 4))  /* GDT selector */
583     {
584         sel &= ~3;  /* ignore RPL */
585         if (!sel)  /* null selector */
586         {
587             memset( ldtent, 0, sizeof(*ldtent) );
588             return TRUE;
589         }
590         ldtent->BaseLow                   = 0;
591         ldtent->HighWord.Bits.BaseMid     = 0;
592         ldtent->HighWord.Bits.BaseHi      = 0;
593         ldtent->LimitLow                  = 0xffff;
594         ldtent->HighWord.Bits.LimitHi     = 0xf;
595         ldtent->HighWord.Bits.Dpl         = 3;
596         ldtent->HighWord.Bits.Sys         = 0;
597         ldtent->HighWord.Bits.Pres        = 1;
598         ldtent->HighWord.Bits.Granularity = 1;
599         ldtent->HighWord.Bits.Default_Big = 1;
600         ldtent->HighWord.Bits.Type        = 0x12;
601         /* it has to be one of the system GDT selectors */
602         if (sel == (wine_get_ds() & ~3)) return TRUE;
603         if (sel == (wine_get_ss() & ~3)) return TRUE;
604         if (sel == (wine_get_cs() & ~3))
605         {
606             ldtent->HighWord.Bits.Type |= 8;  /* code segment */
607             return TRUE;
608         }
609         SetLastError( ERROR_NOACCESS );
610         return FALSE;
611     }
612
613     SERVER_START_REQ( get_selector_entry )
614     {
615         req->handle = hthread;
616         req->entry = sel >> __AHSHIFT;
617         if ((ret = !wine_server_call_err( req )))
618         {
619             if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
620             {
621                 SetLastError( ERROR_MR_MID_NOT_FOUND );  /* sic */
622                 ret = FALSE;
623             }
624             else
625             {
626                 wine_ldt_set_base( ldtent, (void *)reply->base );
627                 wine_ldt_set_limit( ldtent, reply->limit );
628                 wine_ldt_set_flags( ldtent, reply->flags );
629             }
630         }
631     }
632     SERVER_END_REQ;
633     return ret;
634 #else
635     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
636     return FALSE;
637 #endif
638 }
639
640
641 /**********************************************************************
642  *              SMapLS*         (KERNEL32)
643  * These functions map linear pointers at [EBP+xxx] to segmented pointers
644  * and return them.
645  * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
646  * unravel them at SUnMapLS. We just store the segmented pointer there.
647  */
648 static void
649 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
650     DWORD       val,ptr;
651
652     val =*(DWORD*)(context->Ebp + argoff);
653     if (val<0x10000) {
654         ptr=val;
655         *(DWORD*)(context->Ebp + argoff) = 0;
656     } else {
657         ptr = MapLS((LPVOID)val);
658         *(DWORD*)(context->Ebp + argoff) = ptr;
659     }
660     context->Eax = ptr;
661 }
662
663 /***********************************************************************
664  *              SMapLS_IP_EBP_8 (KERNEL32.@)
665  */
666 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
667
668 /***********************************************************************
669  *              SMapLS_IP_EBP_12 (KERNEL32.@)
670  */
671 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
672
673 /***********************************************************************
674  *              SMapLS_IP_EBP_16 (KERNEL32.@)
675  */
676 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
677
678 /***********************************************************************
679  *              SMapLS_IP_EBP_20 (KERNEL32.@)
680  */
681 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
682
683 /***********************************************************************
684  *              SMapLS_IP_EBP_24 (KERNEL32.@)
685  */
686 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
687
688 /***********************************************************************
689  *              SMapLS_IP_EBP_28 (KERNEL32.@)
690  */
691 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
692
693 /***********************************************************************
694  *              SMapLS_IP_EBP_32 (KERNEL32.@)
695  */
696 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
697
698 /***********************************************************************
699  *              SMapLS_IP_EBP_36 (KERNEL32.@)
700  */
701 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
702
703 /***********************************************************************
704  *              SMapLS_IP_EBP_40 (KERNEL32.@)
705  */
706 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
707
708 /***********************************************************************
709  *              SMapLS (KERNEL32.@)
710  */
711 void WINAPI SMapLS( CONTEXT86 *context )
712 {
713     if (HIWORD(context->Eax))
714     {
715         context->Eax = MapLS( (LPVOID)context->Eax );
716         context->Edx = context->Eax;
717     } else {
718         context->Edx = 0;
719     }
720 }
721
722 /***********************************************************************
723  *              SUnMapLS (KERNEL32.@)
724  */
725
726 void WINAPI SUnMapLS( CONTEXT86 *context )
727 {
728     if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
729 }
730
731 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
732 {
733     SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
734     if (*ptr)
735     {
736         UnMapLS( *ptr );
737         *ptr = 0;
738     }
739 }
740
741 /***********************************************************************
742  *              SUnMapLS_IP_EBP_8 (KERNEL32.@)
743  */
744 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
745
746 /***********************************************************************
747  *              SUnMapLS_IP_EBP_12 (KERNEL32.@)
748  */
749 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
750
751 /***********************************************************************
752  *              SUnMapLS_IP_EBP_16 (KERNEL32.@)
753  */
754 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
755
756 /***********************************************************************
757  *              SUnMapLS_IP_EBP_20 (KERNEL32.@)
758  */
759 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
760
761 /***********************************************************************
762  *              SUnMapLS_IP_EBP_24 (KERNEL32.@)
763  */
764 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
765
766 /***********************************************************************
767  *              SUnMapLS_IP_EBP_28 (KERNEL32.@)
768  */
769 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
770
771 /***********************************************************************
772  *              SUnMapLS_IP_EBP_32 (KERNEL32.@)
773  */
774 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
775
776 /***********************************************************************
777  *              SUnMapLS_IP_EBP_36 (KERNEL32.@)
778  */
779 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
780
781 /***********************************************************************
782  *              SUnMapLS_IP_EBP_40 (KERNEL32.@)
783  */
784 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
785
786 /**********************************************************************
787  *              AllocMappedBuffer       (KERNEL32.38)
788  *
789  * This is a undocumented KERNEL32 function that
790  * SMapLS's a GlobalAlloc'ed buffer.
791  *
792  * Input:   EDI register: size of buffer to allocate
793  * Output:  EDI register: pointer to buffer
794  *
795  * Note: The buffer is preceded by 8 bytes:
796  *        ...
797  *       edi+0   buffer
798  *       edi-4   SEGPTR to buffer
799  *       edi-8   some magic Win95 needs for SUnMapLS
800  *               (we use it for the memory handle)
801  *
802  *       The SEGPTR is used by the caller!
803  */
804
805 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
806 {
807     HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
808     DWORD *buffer = (DWORD *)GlobalLock(handle);
809     SEGPTR ptr = 0;
810
811     if (buffer)
812         if (!(ptr = MapLS(buffer + 2)))
813         {
814             GlobalUnlock(handle);
815             GlobalFree(handle);
816         }
817
818     if (!ptr)
819         context->Eax = context->Edi = 0;
820     else
821     {
822         buffer[0] = (DWORD)handle;
823         buffer[1] = ptr;
824
825         context->Eax = (DWORD) ptr;
826         context->Edi = (DWORD)(buffer + 2);
827     }
828 }
829
830 /**********************************************************************
831  *              FreeMappedBuffer        (KERNEL32.39)
832  *
833  * Free a buffer allocated by AllocMappedBuffer
834  *
835  * Input: EDI register: pointer to buffer
836  */
837
838 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
839 {
840     if (context->Edi)
841     {
842         DWORD *buffer = (DWORD *)context->Edi - 2;
843
844         UnMapLS(buffer[1]);
845
846         GlobalUnlock((HGLOBAL)buffer[0]);
847         GlobalFree((HGLOBAL)buffer[0]);
848     }
849 }