Fixed format string.
[wine/multimedia.git] / memory / selector.c
blob7c82d4e44dd3536cc42bd81add37f710f37fd538
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) & ~7))
124 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
125 wine_set_fs( 0 );
127 if (!((wine_get_gs() ^ sel) & ~7)) 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 /* Make sure base and limit are not 0 together if the size is not 0 */
165 wine_ldt_set_limit( &entry, (!base && size == 1) ? 1 : size - 1 );
166 wine_ldt_set_flags( &entry, flags );
167 count = (size + 0xffff) / 0x10000;
168 for (i = 0; i < count; i++)
170 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
171 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
172 /* yep, Windows sets limit like that, not 64K sel units */
173 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
178 /***********************************************************************
179 * SELECTOR_AllocBlock
181 * Allocate selectors for a block of linear memory.
183 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
185 WORD sel, count;
187 if (!size) return 0;
188 count = (size + 0xffff) / 0x10000;
189 sel = SELECTOR_AllocArray( count );
190 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
191 return sel;
195 /***********************************************************************
196 * SELECTOR_FreeBlock
198 * Free a block of selectors.
200 void SELECTOR_FreeBlock( WORD sel )
202 WORD i, count = get_sel_count( sel );
204 TRACE("(%04x,%d)\n", sel, count );
205 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
209 /***********************************************************************
210 * SELECTOR_ReallocBlock
212 * Change the size of a block of selectors.
214 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
216 LDT_ENTRY entry;
217 WORD i, oldcount, newcount;
219 if (!size) size = 1;
220 oldcount = get_sel_count( sel );
221 newcount = (size + 0xffff) >> 16;
222 wine_ldt_get_entry( sel, &entry );
224 if (oldcount < newcount) /* We need to add selectors */
226 WORD index = sel >> __AHSHIFT;
227 /* Check if the next selectors are free */
228 if (index + newcount > LDT_SIZE) i = oldcount;
229 else
230 for (i = oldcount; i < newcount; i++)
231 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
233 if (i < newcount) /* they are not free */
235 SELECTOR_FreeBlock( sel );
236 sel = SELECTOR_AllocArray( newcount );
238 else /* mark the selectors as allocated */
240 for (i = oldcount; i < newcount; i++)
241 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
244 else if (oldcount > newcount) /* We need to remove selectors */
246 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
248 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
249 return sel;
253 /***********************************************************************
254 * PrestoChangoSelector (KERNEL.177)
256 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
258 LDT_ENTRY entry;
259 wine_ldt_get_entry( selSrc, &entry );
260 /* toggle the executable bit */
261 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
262 wine_ldt_set_entry( selDst, &entry );
263 return selDst;
267 /***********************************************************************
268 * AllocCStoDSAlias (KERNEL.170)
269 * AllocAlias (KERNEL.172)
271 WORD WINAPI AllocCStoDSAlias16( WORD sel )
273 WORD newsel;
274 LDT_ENTRY entry;
276 newsel = SELECTOR_AllocArray( 1 );
277 TRACE("(%04x): returning %04x\n",
278 sel, newsel );
279 if (!newsel) return 0;
280 wine_ldt_get_entry( sel, &entry );
281 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
282 wine_ldt_set_entry( newsel, &entry );
283 return newsel;
287 /***********************************************************************
288 * AllocDStoCSAlias (KERNEL.171)
290 WORD WINAPI AllocDStoCSAlias16( WORD sel )
292 WORD newsel;
293 LDT_ENTRY entry;
295 newsel = SELECTOR_AllocArray( 1 );
296 TRACE("(%04x): returning %04x\n",
297 sel, newsel );
298 if (!newsel) return 0;
299 wine_ldt_get_entry( sel, &entry );
300 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
301 wine_ldt_set_entry( newsel, &entry );
302 return newsel;
306 /***********************************************************************
307 * LongPtrAdd (KERNEL.180)
309 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
311 LDT_ENTRY entry;
312 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
313 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
314 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
318 /***********************************************************************
319 * GetSelectorBase (KERNEL.186)
321 DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
324 * Note: For Win32s processes, the whole linear address space is
325 * shifted by 0x10000 relative to the OS linear address space.
326 * See the comment in msdos/vxd.c.
329 DWORD base = GetSelectorBase( sel );
330 return W32S_WINE2APP( base );
332 DWORD WINAPI GetSelectorBase( WORD sel )
334 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
336 /* if base points into DOSMEM, assume we have to
337 * return pointer into physical lower 1MB */
339 return DOSMEM_MapLinearToDos( base );
343 /***********************************************************************
344 * SetSelectorBase (KERNEL.187)
346 DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
349 * Note: For Win32s processes, the whole linear address space is
350 * shifted by 0x10000 relative to the OS linear address space.
351 * See the comment in msdos/vxd.c.
354 SetSelectorBase( sel, W32S_APP2WINE( base ) );
355 return sel;
357 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
359 LDT_ENTRY entry;
360 wine_ldt_get_entry( sel, &entry );
361 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
362 wine_ldt_set_entry( sel, &entry );
363 return sel;
367 /***********************************************************************
368 * GetSelectorLimit (KERNEL.188)
370 DWORD WINAPI GetSelectorLimit16( WORD sel )
372 return wine_ldt_copy.limit[sel >> __AHSHIFT];
376 /***********************************************************************
377 * SetSelectorLimit (KERNEL.189)
379 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
381 LDT_ENTRY entry;
382 wine_ldt_get_entry( sel, &entry );
383 wine_ldt_set_limit( &entry, limit );
384 wine_ldt_set_entry( sel, &entry );
385 return sel;
389 /***********************************************************************
390 * SelectorAccessRights (KERNEL.196)
392 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
394 LDT_ENTRY entry;
395 wine_ldt_get_entry( sel, &entry );
397 if (op == 0) /* get */
399 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
401 else /* set */
403 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
404 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
405 wine_ldt_set_entry( sel, &entry );
406 return 0;
411 /***********************************************************************
412 * IsBadCodePtr (KERNEL.336)
414 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
416 WORD sel;
417 LDT_ENTRY entry;
419 sel = SELECTOROF(lpfn);
420 if (!sel) return TRUE;
421 if (IS_SELECTOR_FREE(sel)) return TRUE;
422 wine_ldt_get_entry( sel, &entry );
423 /* check for code segment, ignoring conforming, read-only and accessed bits */
424 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
425 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
426 return FALSE;
430 /***********************************************************************
431 * IsBadStringPtr (KERNEL.337)
433 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
435 WORD sel;
436 LDT_ENTRY entry;
438 sel = SELECTOROF(ptr);
439 if (!sel) return TRUE;
440 if (IS_SELECTOR_FREE(sel)) return TRUE;
441 wine_ldt_get_entry( sel, &entry );
442 /* check for data or readable code segment */
443 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
444 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
445 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
446 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
447 return FALSE;
451 /***********************************************************************
452 * IsBadHugeReadPtr (KERNEL.346)
454 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
456 WORD sel;
457 LDT_ENTRY entry;
459 sel = SELECTOROF(ptr);
460 if (!sel) return TRUE;
461 if (IS_SELECTOR_FREE(sel)) return TRUE;
462 wine_ldt_get_entry( sel, &entry );
463 /* check for data or readable code segment */
464 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
465 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
466 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
467 return FALSE;
471 /***********************************************************************
472 * IsBadHugeWritePtr (KERNEL.347)
474 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
476 WORD sel;
477 LDT_ENTRY entry;
479 sel = SELECTOROF(ptr);
480 if (!sel) return TRUE;
481 if (IS_SELECTOR_FREE(sel)) return TRUE;
482 wine_ldt_get_entry( sel, &entry );
483 /* check for writeable data segment, ignoring expand-down and accessed flags */
484 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
485 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
486 return FALSE;
489 /***********************************************************************
490 * IsBadReadPtr (KERNEL.334)
492 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
494 return IsBadHugeReadPtr16( ptr, size );
498 /***********************************************************************
499 * IsBadWritePtr (KERNEL.335)
501 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
503 return IsBadHugeWritePtr16( ptr, size );
507 /***********************************************************************
508 * IsBadFlatReadWritePtr (KERNEL.627)
510 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
512 return bWrite? IsBadHugeWritePtr16( ptr, size )
513 : IsBadHugeReadPtr16( ptr, size );
517 /***********************************************************************
518 * MemoryRead (TOOLHELP.78)
520 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
522 WORD index = sel >> __AHSHIFT;
524 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
525 if (offset > wine_ldt_copy.limit[index]) return 0;
526 if (offset + count > wine_ldt_copy.limit[index] + 1)
527 count = wine_ldt_copy.limit[index] + 1 - offset;
528 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
529 return count;
533 /***********************************************************************
534 * MemoryWrite (TOOLHELP.79)
536 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
538 WORD index = sel >> __AHSHIFT;
540 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
541 if (offset > wine_ldt_copy.limit[index]) return 0;
542 if (offset + count > wine_ldt_copy.limit[index] + 1)
543 count = wine_ldt_copy.limit[index] + 1 - offset;
544 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
545 return count;
548 /************************************* Win95 pointer mapping functions *
552 struct mapls_entry
554 struct mapls_entry *next;
555 void *addr; /* linear address */
556 int count; /* ref count */
557 WORD sel; /* selector */
560 static struct mapls_entry *first_entry;
563 /***********************************************************************
564 * MapLS (KERNEL32.@)
565 * MapLS (KERNEL.358)
567 * Maps linear pointer to segmented.
569 SEGPTR WINAPI MapLS( LPCVOID ptr )
571 struct mapls_entry *entry, *free = NULL;
572 void *base;
573 SEGPTR ret = 0;
575 if (!HIWORD(ptr)) return (SEGPTR)ptr;
577 base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
578 HeapLock( GetProcessHeap() );
579 for (entry = first_entry; entry; entry = entry->next)
581 if (entry->addr == base) break;
582 if (!entry->count) free = entry;
585 if (!entry)
587 if (!free) /* no free entry found, create a new one */
589 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
590 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
592 HeapFree( GetProcessHeap(), 0, free );
593 goto done;
595 free->count = 0;
596 free->next = first_entry;
597 first_entry = free;
599 SetSelectorBase( free->sel, (DWORD)base );
600 free->addr = base;
601 entry = free;
603 entry->count++;
604 ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
605 done:
606 HeapUnlock( GetProcessHeap() );
607 return ret;
610 /***********************************************************************
611 * UnMapLS (KERNEL32.@)
612 * UnMapLS (KERNEL.359)
614 * Free mapped selector.
616 void WINAPI UnMapLS( SEGPTR sptr )
618 struct mapls_entry *entry;
619 WORD sel = SELECTOROF(sptr);
621 if (sel)
623 HeapLock( GetProcessHeap() );
624 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
625 if (entry && entry->count > 0) entry->count--;
626 HeapUnlock( GetProcessHeap() );
630 /***********************************************************************
631 * MapSL (KERNEL32.@)
632 * MapSL (KERNEL.357)
634 * Maps fixed segmented pointer to linear.
636 LPVOID WINAPI MapSL( SEGPTR sptr )
638 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
641 /***********************************************************************
642 * MapSLFix (KERNEL32.@)
644 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
645 * unexpected linear address change when GlobalCompact() shuffles
646 * moveable blocks.
649 LPVOID WINAPI MapSLFix( SEGPTR sptr )
651 return MapSL(sptr);
654 /***********************************************************************
655 * UnMapSLFixArray (KERNEL32.@)
658 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
660 /* Must not change EAX, hence defined as 'register' function */
663 /***********************************************************************
664 * GetThreadSelectorEntry (KERNEL32.@)
666 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
668 #ifdef __i386__
669 BOOL ret;
671 if (!(sel & 4)) /* GDT selector */
673 sel &= ~3; /* ignore RPL */
674 if (!sel) /* null selector */
676 memset( ldtent, 0, sizeof(*ldtent) );
677 return TRUE;
679 ldtent->BaseLow = 0;
680 ldtent->HighWord.Bits.BaseMid = 0;
681 ldtent->HighWord.Bits.BaseHi = 0;
682 ldtent->LimitLow = 0xffff;
683 ldtent->HighWord.Bits.LimitHi = 0xf;
684 ldtent->HighWord.Bits.Dpl = 3;
685 ldtent->HighWord.Bits.Sys = 0;
686 ldtent->HighWord.Bits.Pres = 1;
687 ldtent->HighWord.Bits.Granularity = 1;
688 ldtent->HighWord.Bits.Default_Big = 1;
689 ldtent->HighWord.Bits.Type = 0x12;
690 /* it has to be one of the system GDT selectors */
691 if (sel == (wine_get_ds() & ~3)) return TRUE;
692 if (sel == (wine_get_ss() & ~3)) return TRUE;
693 if (sel == (wine_get_cs() & ~3))
695 ldtent->HighWord.Bits.Type |= 8; /* code segment */
696 return TRUE;
698 SetLastError( ERROR_NOACCESS );
699 return FALSE;
702 SERVER_START_REQ( get_selector_entry )
704 req->handle = hthread;
705 req->entry = sel >> __AHSHIFT;
706 if ((ret = !wine_server_call_err( req )))
708 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
710 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
711 ret = FALSE;
713 else
715 wine_ldt_set_base( ldtent, (void *)reply->base );
716 wine_ldt_set_limit( ldtent, reply->limit );
717 wine_ldt_set_flags( ldtent, reply->flags );
721 SERVER_END_REQ;
722 return ret;
723 #else
724 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
725 return FALSE;
726 #endif
730 /**********************************************************************
731 * SMapLS* (KERNEL32)
732 * These functions map linear pointers at [EBP+xxx] to segmented pointers
733 * and return them.
734 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
735 * unravel them at SUnMapLS. We just store the segmented pointer there.
737 static void
738 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
739 DWORD val,ptr;
741 val =*(DWORD*)(context->Ebp + argoff);
742 if (val<0x10000) {
743 ptr=val;
744 *(DWORD*)(context->Ebp + argoff) = 0;
745 } else {
746 ptr = MapLS((LPVOID)val);
747 *(DWORD*)(context->Ebp + argoff) = ptr;
749 context->Eax = ptr;
752 /***********************************************************************
753 * SMapLS_IP_EBP_8 (KERNEL32.@)
755 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
757 /***********************************************************************
758 * SMapLS_IP_EBP_12 (KERNEL32.@)
760 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
762 /***********************************************************************
763 * SMapLS_IP_EBP_16 (KERNEL32.@)
765 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
767 /***********************************************************************
768 * SMapLS_IP_EBP_20 (KERNEL32.@)
770 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
772 /***********************************************************************
773 * SMapLS_IP_EBP_24 (KERNEL32.@)
775 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
777 /***********************************************************************
778 * SMapLS_IP_EBP_28 (KERNEL32.@)
780 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
782 /***********************************************************************
783 * SMapLS_IP_EBP_32 (KERNEL32.@)
785 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
787 /***********************************************************************
788 * SMapLS_IP_EBP_36 (KERNEL32.@)
790 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
792 /***********************************************************************
793 * SMapLS_IP_EBP_40 (KERNEL32.@)
795 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
797 /***********************************************************************
798 * SMapLS (KERNEL32.@)
800 void WINAPI SMapLS( CONTEXT86 *context )
802 if (HIWORD(context->Eax))
804 context->Eax = MapLS( (LPVOID)context->Eax );
805 context->Edx = context->Eax;
806 } else {
807 context->Edx = 0;
811 /***********************************************************************
812 * SUnMapLS (KERNEL32.@)
815 void WINAPI SUnMapLS( CONTEXT86 *context )
817 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
820 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
822 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
823 if (*ptr)
825 UnMapLS( *ptr );
826 *ptr = 0;
830 /***********************************************************************
831 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
833 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
835 /***********************************************************************
836 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
838 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
840 /***********************************************************************
841 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
843 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
845 /***********************************************************************
846 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
848 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
850 /***********************************************************************
851 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
853 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
855 /***********************************************************************
856 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
858 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
860 /***********************************************************************
861 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
863 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
865 /***********************************************************************
866 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
868 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
870 /***********************************************************************
871 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
873 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
875 /**********************************************************************
876 * AllocMappedBuffer (KERNEL32.38)
878 * This is a undocumented KERNEL32 function that
879 * SMapLS's a GlobalAlloc'ed buffer.
881 * Input: EDI register: size of buffer to allocate
882 * Output: EDI register: pointer to buffer
884 * Note: The buffer is preceded by 8 bytes:
885 * ...
886 * edi+0 buffer
887 * edi-4 SEGPTR to buffer
888 * edi-8 some magic Win95 needs for SUnMapLS
889 * (we use it for the memory handle)
891 * The SEGPTR is used by the caller!
894 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
896 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
897 DWORD *buffer = (DWORD *)GlobalLock(handle);
898 SEGPTR ptr = 0;
900 if (buffer)
901 if (!(ptr = MapLS(buffer + 2)))
903 GlobalUnlock(handle);
904 GlobalFree(handle);
907 if (!ptr)
908 context->Eax = context->Edi = 0;
909 else
911 buffer[0] = handle;
912 buffer[1] = ptr;
914 context->Eax = (DWORD) ptr;
915 context->Edi = (DWORD)(buffer + 2);
919 /**********************************************************************
920 * FreeMappedBuffer (KERNEL32.39)
922 * Free a buffer allocated by AllocMappedBuffer
924 * Input: EDI register: pointer to buffer
927 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
929 if (context->Edi)
931 DWORD *buffer = (DWORD *)context->Edi - 2;
933 UnMapLS(buffer[1]);
935 GlobalUnlock(buffer[0]);
936 GlobalFree(buffer[0]);