home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / pmod01 / source / alias.c next >
Encoding:
C/C++ Source or Header  |  1994-03-10  |  3.2 KB  |  128 lines

  1. /****************************************************************************
  2. *
  3. *                          Protected Mode Library
  4. *
  5. *                   Copyright (C) 1994 SciTech Software.
  6. *                            All rights reserved.
  7. *
  8. * Filename:        $RCSfile: alias.c $
  9. * Version:        $Revision: 1.1 $
  10. *
  11. * Language:        ANSI C
  12. * Environment:    any
  13. *
  14. * Description:  Test program to check the ability to create both code
  15. *                and data segment aliases from protected mode. This allows
  16. *                one to modify code and to load and run code dynamically
  17. *                at runtime. Compile and link with the appropriate command
  18. *                line for your DOS extender.
  19. *
  20. *                Functions tested:    PMODE_allocCSAlias()
  21. *                                    PMODE_allocDSAlias()
  22. *                                    PMODE_freeAlias()
  23. *
  24. * $Id: alias.c 1.1 1994/03/10 09:05:43 kjb release $
  25. *
  26. ****************************************************************************/
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <mem.h>
  31. #include "pmode.h"
  32.  
  33. /* Block of dummy code in the data segment that we wish to execute. Since
  34.  * it must be relocateable we just simply make it do a far return (a near
  35.  * return for 386 protected mode).
  36.  */
  37.  
  38. unsigned char DummyCode[] = {
  39. #ifdef    PMODE386
  40.     0xC3
  41. #else
  42.     0xCB
  43. #endif
  44.     };
  45.  
  46. void DummyData(void)
  47. {
  48.     /* Dummy routine that we obtain a DS alias to and modify */
  49.  
  50.     printf("You should not get here!!!!!\n");
  51. }
  52.  
  53. void main(void)
  54. {
  55.     void (*csalias)(void);
  56.     unsigned short far *dsalias;
  57.     void *memblock;
  58.  
  59.     printf("Program running in ");
  60.     switch (_PMODE_modeType) {
  61.         case PMODE_realMode:
  62.             printf("real mode.\n\n");
  63.             break;
  64.         case PMODE_286:
  65.             printf("16 bit protected mode.\n\n");
  66.             break;
  67.         case PMODE_386:
  68.             printf("32 bit protected mode.\n\n");
  69.             break;
  70.         }
  71.  
  72.     /* Now create a DS alias for the literal value stored in the code
  73.      * segment at DummyData.
  74.      */
  75.  
  76.     dsalias = PMODE_createDSAlias(&DummyData);
  77.     if (dsalias == NULL) {
  78.         printf("Unable to create data segment alias!\n");
  79.         exit(1);
  80.         }
  81.  
  82.     /* Display the value stored in the DS alias, modify it and display
  83.      * the new value.
  84.      */
  85.  
  86.     printf("Old value at &Dummy(): 0x%04X\n", *dsalias);
  87.     *dsalias = 0xCBAD;
  88.     printf("New value at &Dummy(): 0x%04X\n", *dsalias);
  89.     PMODE_freeDSAlias(dsalias);
  90.  
  91.     /* Now create a code segment alias to the block of dummy code we
  92.      * have allocated in the default data segment, and attempt to
  93.      * execute it.
  94.      */
  95.  
  96.     csalias = PMODE_createCSAlias(DummyCode);
  97.     if (csalias == NULL) {
  98.         printf("Unable to create code segment alias!\n");
  99.         exit(1);
  100.         }
  101.  
  102.     printf("Executing data in DGROUP ... ");
  103.     (*csalias)();
  104.     printf("Success!\n");
  105.     PMODE_freeCSAlias(csalias);
  106.  
  107.     /* Now malloc a block of memory, copy the dummy code into it and
  108.      * attempt to execute the code from the heap.
  109.      */
  110.  
  111.     memblock = malloc(sizeof(DummyCode));
  112.     if (memblock == NULL) {
  113.         printf("Unable to allocate memory block!\n");
  114.         exit(1);
  115.         }
  116.     memcpy(memblock, &DummyCode, sizeof(DummyCode));
  117.     csalias = PMODE_createCSAlias(memblock);
  118.     if (csalias == NULL) {
  119.         printf("Unable to create code segment alias to malloc()'ed memory!\n");
  120.         exit(1);
  121.         }
  122.  
  123.     printf("Executing data on heap ... ");
  124.     (*csalias)();
  125.     printf("Success!\n");
  126.     PMODE_freeCSAlias(csalias);
  127. }
  128.