Correct the number of parameters for StrRStrIA/W.
[wine.git] / memory / selector.c
blob38792528436cc1897b3093f5d5391b68624986c4
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
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"
35 WINE_DEFAULT_DEBUG_CHANNEL(selector);
37 #define LDT_SIZE 8192
39 /* get the number of selectors needed to cover up to the selector limit */
40 inline static WORD get_sel_count( WORD sel )
42 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
45 static const LDT_ENTRY null_entry; /* all-zeros, used to clear LDT entries */
47 /***********************************************************************
48 * SELECTOR_AllocArray
50 * Allocate a selector array without setting the LDT entries
52 static WORD SELECTOR_AllocArray( WORD count )
54 WORD i, sel, size = 0;
56 if (!count) return 0;
57 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
59 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
60 else if (++size >= count) break;
62 if (i == LDT_SIZE) return 0;
63 sel = i - size + 1;
65 /* mark selectors as allocated */
66 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
68 return (sel << __AHSHIFT) | 7;
72 /***********************************************************************
73 * AllocSelectorArray (KERNEL.206)
75 WORD WINAPI AllocSelectorArray16( WORD count )
77 WORD i, sel = SELECTOR_AllocArray( count );
79 if (sel)
81 LDT_ENTRY entry;
82 wine_ldt_set_base( &entry, 0 );
83 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
84 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
85 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
87 return sel;
91 /***********************************************************************
92 * AllocSelector (KERNEL.175)
94 WORD WINAPI AllocSelector16( WORD sel )
96 WORD newsel, count, i;
98 count = sel ? get_sel_count(sel) : 1;
99 newsel = SELECTOR_AllocArray( count );
100 TRACE("(%04x): returning %04x\n", sel, newsel );
101 if (!newsel) return 0;
102 if (!sel) return newsel; /* nothing to copy */
103 for (i = 0; i < count; i++)
105 LDT_ENTRY entry;
106 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
107 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
109 return newsel;
113 /***********************************************************************
114 * FreeSelector (KERNEL.176)
116 WORD WINAPI FreeSelector16( WORD sel )
118 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
120 #ifdef __i386__
121 /* Check if we are freeing current %fs or %gs selector */
122 if (!((wine_get_fs() ^ sel) & ~3))
124 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
125 wine_set_fs( 0 );
127 if (!((wine_get_gs() ^ sel) & ~3)) wine_set_gs( 0 );
128 #endif /* __i386__ */
130 wine_ldt_set_entry( sel, &null_entry );
131 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
132 return 0;
136 /***********************************************************************
137 * SELECTOR_FreeFs
139 * Free the current %fs selector.
141 void SELECTOR_FreeFs(void)
143 WORD fs = wine_get_fs();
144 if (fs)
146 wine_ldt_copy.flags[fs >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
147 wine_set_fs(0);
148 wine_ldt_set_entry( fs, &null_entry );
153 /***********************************************************************
154 * SELECTOR_SetEntries
156 * Set the LDT entries for an array of selectors.
158 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
160 LDT_ENTRY entry;
161 WORD i, count;
163 wine_ldt_set_base( &entry, base );
164 wine_ldt_set_limit( &entry, size - 1 );
165 wine_ldt_set_flags( &entry, flags );
166 count = (size + 0xffff) / 0x10000;
167 for (i = 0; i < count; i++)
169 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
170 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
171 /* yep, Windows sets limit like that, not 64K sel units */
172 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
177 /***********************************************************************
178 * SELECTOR_AllocBlock
180 * Allocate selectors for a block of linear memory.
182 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
184 WORD sel, count;
186 if (!size) return 0;
187 count = (size + 0xffff) / 0x10000;
188 sel = SELECTOR_AllocArray( count );
189 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
190 return sel;
194 /***********************************************************************
195 * SELECTOR_FreeBlock
197 * Free a block of selectors.
199 void SELECTOR_FreeBlock( WORD sel )
201 WORD i, count = get_sel_count( sel );
203 TRACE("(%04x,%d)\n", sel, count );
204 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
208 /***********************************************************************
209 * SELECTOR_ReallocBlock
211 * Change the size of a block of selectors.
213 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
215 LDT_ENTRY entry;
216 WORD i, oldcount, newcount;
218 if (!size) size = 1;
219 oldcount = get_sel_count( sel );
220 newcount = (size + 0xffff) >> 16;
221 wine_ldt_get_entry( sel, &entry );
223 if (oldcount < newcount) /* We need to add selectors */
225 WORD index = sel >> __AHSHIFT;
226 /* Check if the next selectors are free */
227 if (index + newcount > LDT_SIZE) i = oldcount;
228 else
229 for (i = oldcount; i < newcount; i++)
230 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
232 if (i < newcount) /* they are not free */
234 SELECTOR_FreeBlock( sel );
235 sel = SELECTOR_AllocArray( newcount );
237 else /* mark the selectors as allocated */
239 for (i = oldcount; i < newcount; i++)
240 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
243 else if (oldcount > newcount) /* We need to remove selectors */
245 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
247 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
248 return sel;
252 /***********************************************************************
253 * PrestoChangoSelector (KERNEL.177)
255 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
257 LDT_ENTRY entry;
258 wine_ldt_get_entry( selSrc, &entry );
259 /* toggle the executable bit */
260 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
261 wine_ldt_set_entry( selDst, &entry );
262 return selDst;
266 /***********************************************************************
267 * AllocCStoDSAlias (KERNEL.170)
268 * AllocAlias (KERNEL.172)
270 WORD WINAPI AllocCStoDSAlias16( WORD sel )
272 WORD newsel;
273 LDT_ENTRY entry;
275 newsel = SELECTOR_AllocArray( 1 );
276 TRACE("(%04x): returning %04x\n",
277 sel, newsel );
278 if (!newsel) return 0;
279 wine_ldt_get_entry( sel, &entry );
280 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
281 wine_ldt_set_entry( newsel, &entry );
282 return newsel;
286 /***********************************************************************
287 * AllocDStoCSAlias (KERNEL.171)
289 WORD WINAPI AllocDStoCSAlias16( WORD sel )
291 WORD newsel;
292 LDT_ENTRY entry;
294 newsel = SELECTOR_AllocArray( 1 );
295 TRACE("(%04x): returning %04x\n",
296 sel, newsel );
297 if (!newsel) return 0;
298 wine_ldt_get_entry( sel, &entry );
299 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
300 wine_ldt_set_entry( newsel, &entry );
301 return newsel;
305 /***********************************************************************
306 * LongPtrAdd (KERNEL.180)
308 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
310 LDT_ENTRY entry;
311 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
312 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
313 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
317 /***********************************************************************
318 * GetSelectorBase (KERNEL.186)
320 DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
323 * Note: For Win32s processes, the whole linear address space is
324 * shifted by 0x10000 relative to the OS linear address space.
325 * See the comment in msdos/vxd.c.
328 DWORD base = GetSelectorBase( sel );
329 return W32S_WINE2APP( base );
331 DWORD WINAPI GetSelectorBase( WORD sel )
333 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
335 /* if base points into DOSMEM, assume we have to
336 * return pointer into physical lower 1MB */
338 return DOSMEM_MapLinearToDos( base );
342 /***********************************************************************
343 * SetSelectorBase (KERNEL.187)
345 DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
348 * Note: For Win32s processes, the whole linear address space is
349 * shifted by 0x10000 relative to the OS linear address space.
350 * See the comment in msdos/vxd.c.
353 SetSelectorBase( sel, W32S_APP2WINE( base ) );
354 return sel;
356 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
358 LDT_ENTRY entry;
359 wine_ldt_get_entry( sel, &entry );
360 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
361 wine_ldt_set_entry( sel, &entry );
362 return sel;
366 /***********************************************************************
367 * GetSelectorLimit (KERNEL.188)
369 DWORD WINAPI GetSelectorLimit16( WORD sel )
371 return wine_ldt_copy.limit[sel >> __AHSHIFT];
375 /***********************************************************************
376 * SetSelectorLimit (KERNEL.189)
378 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
380 LDT_ENTRY entry;
381 wine_ldt_get_entry( sel, &entry );
382 wine_ldt_set_limit( &entry, limit );
383 wine_ldt_set_entry( sel, &entry );
384 return sel;
388 /***********************************************************************
389 * SelectorAccessRights (KERNEL.196)
391 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
393 LDT_ENTRY entry;
394 wine_ldt_get_entry( sel, &entry );
396 if (op == 0) /* get */
398 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
400 else /* set */
402 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
403 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
404 wine_ldt_set_entry( sel, &entry );
405 return 0;
410 /***********************************************************************
411 * IsBadCodePtr (KERNEL.336)
413 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
415 WORD sel;
416 LDT_ENTRY entry;
418 sel = SELECTOROF(lpfn);
419 if (!sel) return TRUE;
420 if (IS_SELECTOR_FREE(sel)) return TRUE;
421 wine_ldt_get_entry( sel, &entry );
422 /* check for code segment, ignoring conforming, read-only and accessed bits */
423 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
424 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
425 return FALSE;
429 /***********************************************************************
430 * IsBadStringPtr (KERNEL.337)
432 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
434 WORD sel;
435 LDT_ENTRY entry;
437 sel = SELECTOROF(ptr);
438 if (!sel) return TRUE;
439 if (IS_SELECTOR_FREE(sel)) return TRUE;
440 wine_ldt_get_entry( sel, &entry );
441 /* check for data or readable code segment */
442 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
443 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
444 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
445 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
446 return FALSE;
450 /***********************************************************************
451 * IsBadHugeReadPtr (KERNEL.346)
453 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
455 WORD sel;
456 LDT_ENTRY entry;
458 sel = SELECTOROF(ptr);
459 if (!sel) return TRUE;
460 if (IS_SELECTOR_FREE(sel)) return TRUE;
461 wine_ldt_get_entry( sel, &entry );
462 /* check for data or readable code segment */
463 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
464 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
465 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
466 return FALSE;
470 /***********************************************************************
471 * IsBadHugeWritePtr (KERNEL.347)
473 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
475 WORD sel;
476 LDT_ENTRY entry;
478 sel = SELECTOROF(ptr);
479 if (!sel) return TRUE;
480 if (IS_SELECTOR_FREE(sel)) return TRUE;
481 wine_ldt_get_entry( sel, &entry );
482 /* check for writeable data segment, ignoring expand-down and accessed flags */
483 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
484 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
485 return FALSE;
488 /***********************************************************************
489 * IsBadReadPtr (KERNEL.334)
491 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
493 return IsBadHugeReadPtr16( ptr, size );
497 /***********************************************************************
498 * IsBadWritePtr (KERNEL.335)
500 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
502 return IsBadHugeWritePtr16( ptr, size );
506 /***********************************************************************
507 * IsBadFlatReadWritePtr (KERNEL.627)
509 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
511 return bWrite? IsBadHugeWritePtr16( ptr, size )
512 : IsBadHugeReadPtr16( ptr, size );
516 /***********************************************************************
517 * MemoryRead (TOOLHELP.78)
519 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
521 WORD index = sel >> __AHSHIFT;
523 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
524 if (offset > wine_ldt_copy.limit[index]) return 0;
525 if (offset + count > wine_ldt_copy.limit[index] + 1)
526 count = wine_ldt_copy.limit[index] + 1 - offset;
527 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
528 return count;
532 /***********************************************************************
533 * MemoryWrite (TOOLHELP.79)
535 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
537 WORD index = sel >> __AHSHIFT;
539 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
540 if (offset > wine_ldt_copy.limit[index]) return 0;
541 if (offset + count > wine_ldt_copy.limit[index] + 1)
542 count = wine_ldt_copy.limit[index] + 1 - offset;
543 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
544 return count;
547 /************************************* Win95 pointer mapping functions *
551 struct mapls_entry
553 struct mapls_entry *next;
554 void *addr; /* linear address */
555 int count; /* ref count */
556 WORD sel; /* selector */
559 static struct mapls_entry *first_entry;
562 /***********************************************************************
563 * MapLS (KERNEL32.@)
564 * MapLS (KERNEL.358)
566 * Maps linear pointer to segmented.
568 SEGPTR WINAPI MapLS( LPCVOID ptr )
570 struct mapls_entry *entry, *free = NULL;
571 void *base;
572 SEGPTR ret = 0;
574 if (!HIWORD(ptr)) return (SEGPTR)ptr;
576 base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
577 HeapLock( GetProcessHeap() );
578 for (entry = first_entry; entry; entry = entry->next)
580 if (entry->addr == base) break;
581 if (!entry->count) free = entry;
584 if (!entry)
586 if (!free) /* no free entry found, create a new one */
588 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
589 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
591 HeapFree( GetProcessHeap(), 0, free );
592 goto done;
594 free->count = 0;
595 free->next = first_entry;
596 first_entry = free;
598 SetSelectorBase( free->sel, (DWORD)base );
599 free->addr = base;
600 entry = free;
602 entry->count++;
603 ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
604 done:
605 HeapUnlock( GetProcessHeap() );
606 return ret;
609 /***********************************************************************
610 * UnMapLS (KERNEL32.@)
611 * UnMapLS (KERNEL.359)
613 * Free mapped selector.
615 void WINAPI UnMapLS( SEGPTR sptr )
617 struct mapls_entry *entry;
618 WORD sel = SELECTOROF(sptr);
620 if (sel)
622 HeapLock( GetProcessHeap() );
623 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
624 if (entry && entry->count > 0) entry->count--;
625 HeapUnlock( GetProcessHeap() );
629 /***********************************************************************
630 * MapSL (KERNEL32.@)
631 * MapSL (KERNEL.357)
633 * Maps fixed segmented pointer to linear.
635 LPVOID WINAPI MapSL( SEGPTR sptr )
637 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
640 /***********************************************************************
641 * MapSLFix (KERNEL32.@)
643 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
644 * unexpected linear address change when GlobalCompact() shuffles
645 * moveable blocks.
648 LPVOID WINAPI MapSLFix( SEGPTR sptr )
650 return MapSL(sptr);
653 /***********************************************************************
654 * UnMapSLFixArray (KERNEL32.@)
657 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
659 /* Must not change EAX, hence defined as 'register' function */
662 /***********************************************************************
663 * GetThreadSelectorEntry (KERNEL32.@)
665 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
667 #ifdef __i386__
668 BOOL ret;
670 if (!(sel & 4)) /* GDT selector */
672 sel &= ~3; /* ignore RPL */
673 if (!sel) /* null selector */
675 memset( ldtent, 0, sizeof(*ldtent) );
676 return TRUE;
678 ldtent->BaseLow = 0;
679 ldtent->HighWord.Bits.BaseMid = 0;
680 ldtent->HighWord.Bits.BaseHi = 0;
681 ldtent->LimitLow = 0xffff;
682 ldtent->HighWord.Bits.LimitHi = 0xf;
683 ldtent->HighWord.Bits.Dpl = 3;
684 ldtent->HighWord.Bits.Sys = 0;
685 ldtent->HighWord.Bits.Pres = 1;
686 ldtent->HighWord.Bits.Granularity = 1;
687 ldtent->HighWord.Bits.Default_Big = 1;
688 ldtent->HighWord.Bits.Type = 0x12;
689 /* it has to be one of the system GDT selectors */
690 if (sel == (wine_get_ds() & ~3)) return TRUE;
691 if (sel == (wine_get_ss() & ~3)) return TRUE;
692 if (sel == (wine_get_cs() & ~3))
694 ldtent->HighWord.Bits.Type |= 8; /* code segment */
695 return TRUE;
697 SetLastError( ERROR_NOACCESS );
698 return FALSE;
701 SERVER_START_REQ( get_selector_entry )
703 req->handle = hthread;
704 req->entry = sel >> __AHSHIFT;
705 if ((ret = !wine_server_call_err( req )))
707 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
709 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
710 ret = FALSE;
712 else
714 wine_ldt_set_base( ldtent, (void *)reply->base );
715 wine_ldt_set_limit( ldtent, reply->limit );
716 wine_ldt_set_flags( ldtent, reply->flags );
720 SERVER_END_REQ;
721 return ret;
722 #else
723 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
724 return FALSE;
725 #endif
729 /**********************************************************************
730 * SMapLS* (KERNEL32)
731 * These functions map linear pointers at [EBP+xxx] to segmented pointers
732 * and return them.
733 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
734 * unravel them at SUnMapLS. We just store the segmented pointer there.
736 static void
737 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
738 DWORD val,ptr;
740 val =*(DWORD*)(context->Ebp + argoff);
741 if (val<0x10000) {
742 ptr=val;
743 *(DWORD*)(context->Ebp + argoff) = 0;
744 } else {
745 ptr = MapLS((LPVOID)val);
746 *(DWORD*)(context->Ebp + argoff) = ptr;
748 context->Eax = ptr;
751 /***********************************************************************
752 * SMapLS_IP_EBP_8 (KERNEL32.@)
754 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
756 /***********************************************************************
757 * SMapLS_IP_EBP_12 (KERNEL32.@)
759 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
761 /***********************************************************************
762 * SMapLS_IP_EBP_16 (KERNEL32.@)
764 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
766 /***********************************************************************
767 * SMapLS_IP_EBP_20 (KERNEL32.@)
769 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
771 /***********************************************************************
772 * SMapLS_IP_EBP_24 (KERNEL32.@)
774 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
776 /***********************************************************************
777 * SMapLS_IP_EBP_28 (KERNEL32.@)
779 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
781 /***********************************************************************
782 * SMapLS_IP_EBP_32 (KERNEL32.@)
784 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
786 /***********************************************************************
787 * SMapLS_IP_EBP_36 (KERNEL32.@)
789 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
791 /***********************************************************************
792 * SMapLS_IP_EBP_40 (KERNEL32.@)
794 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
796 /***********************************************************************
797 * SMapLS (KERNEL32.@)
799 void WINAPI SMapLS( CONTEXT86 *context )
801 if (HIWORD(context->Eax))
803 context->Eax = MapLS( (LPVOID)context->Eax );
804 context->Edx = context->Eax;
805 } else {
806 context->Edx = 0;
810 /***********************************************************************
811 * SUnMapLS (KERNEL32.@)
814 void WINAPI SUnMapLS( CONTEXT86 *context )
816 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
819 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
821 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
822 if (*ptr)
824 UnMapLS( *ptr );
825 *ptr = 0;
829 /***********************************************************************
830 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
832 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
834 /***********************************************************************
835 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
837 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
839 /***********************************************************************
840 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
842 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
844 /***********************************************************************
845 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
847 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
849 /***********************************************************************
850 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
852 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
854 /***********************************************************************
855 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
857 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
859 /***********************************************************************
860 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
862 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
864 /***********************************************************************
865 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
867 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
869 /***********************************************************************
870 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
872 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
874 /**********************************************************************
875 * AllocMappedBuffer (KERNEL32.38)
877 * This is a undocumented KERNEL32 function that
878 * SMapLS's a GlobalAlloc'ed buffer.
880 * Input: EDI register: size of buffer to allocate
881 * Output: EDI register: pointer to buffer
883 * Note: The buffer is preceded by 8 bytes:
884 * ...
885 * edi+0 buffer
886 * edi-4 SEGPTR to buffer
887 * edi-8 some magic Win95 needs for SUnMapLS
888 * (we use it for the memory handle)
890 * The SEGPTR is used by the caller!
893 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
895 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
896 DWORD *buffer = (DWORD *)GlobalLock(handle);
897 SEGPTR ptr = 0;
899 if (buffer)
900 if (!(ptr = MapLS(buffer + 2)))
902 GlobalUnlock(handle);
903 GlobalFree(handle);
906 if (!ptr)
907 context->Eax = context->Edi = 0;
908 else
910 buffer[0] = (DWORD)handle;
911 buffer[1] = ptr;
913 context->Eax = (DWORD) ptr;
914 context->Edi = (DWORD)(buffer + 2);
918 /**********************************************************************
919 * FreeMappedBuffer (KERNEL32.39)
921 * Free a buffer allocated by AllocMappedBuffer
923 * Input: EDI register: pointer to buffer
926 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
928 if (context->Edi)
930 DWORD *buffer = (DWORD *)context->Edi - 2;
932 UnMapLS(buffer[1]);
934 GlobalUnlock((HGLOBAL)buffer[0]);
935 GlobalFree((HGLOBAL)buffer[0]);