aboutsummaryrefslogtreecommitdiff
path: root/board/MAI/bios_emulator/scitech/src/pm/ntdrv/stdlib.c
blob: d7705130b73139a6344797920a6fd1d0ece96c4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/****************************************************************************
*
*                   SciTech OS Portability Manager Library
*
*  ========================================================================
*
*    The contents of this file are subject to the SciTech MGL Public
*    License Version 1.0 (the "License"); you may not use this file
*    except in compliance with the License. You may obtain a copy of
*    the License at http://www.scitechsoft.com/mgl-license.txt
*
*    Software distributed under the License is distributed on an
*    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
*    implied. See the License for the specific language governing
*    rights and limitations under the License.
*
*    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
*
*    The Initial Developer of the Original Code is SciTech Software, Inc.
*    All Rights Reserved.
*
*  ========================================================================
*
* Language:     ANSI C
* Environment:  32-bit Windows NT driver
*
* Description:  C library compatible stdlib.h functions for use within a
*               Windows NT driver.
*
****************************************************************************/

#include "pmapi.h"
#include "oshdr.h"

/*------------------------ Main Code Implementation -----------------------*/

/****************************************************************************
REMARKS:
PM_malloc override function for Nucleus drivers loaded in NT drivers's.
****************************************************************************/
void * malloc(
    size_t size)
{
    return PM_mallocShared(size);
}

/****************************************************************************
REMARKS:
calloc library function for Nucleus drivers loaded in NT drivers's.
****************************************************************************/
void * calloc(
    size_t nelem,
    size_t size)
{
    void *p = PM_mallocShared(nelem * size);
    if (p)
        memset(p,0,nelem * size);
    return p;
}

/****************************************************************************
REMARKS:
PM_realloc override function for Nucleus drivers loaded in VxD's.
****************************************************************************/
void * realloc(
    void *ptr,
    size_t size)
{
    void *p = PM_mallocShared(size);
    if (p) {
        memcpy(p,ptr,size);
        PM_freeShared(ptr);
        }
    return p;
}

/****************************************************************************
REMARKS:
PM_free override function for Nucleus drivers loaded in VxD's.
****************************************************************************/
void free(
    void *p)
{
    PM_freeShared(p);
}

/****************************************************************************
PARAMETERS:
cstr    - C style ANSI string to convert

RETURNS:
Pointer to the UniCode string structure or NULL on failure to allocate memory

REMARKS:
Converts a C style string to a UniCode string structure that can be passed
directly to NT kernel functions.
****************************************************************************/
UNICODE_STRING *_PM_CStringToUnicodeString(
    const char *cstr)
{
    int             length;
    ANSI_STRING     ansiStr;
    UNICODE_STRING  *uniStr;

    // Allocate memory for the string structure
    if ((uniStr = ExAllocatePool(NonPagedPool, sizeof(UNICODE_STRING))) == NULL)
        return NULL;

    // Allocate memory for the wide string itself
    length = (strlen(cstr) * sizeof(WCHAR)) + sizeof(WCHAR);
    if ((uniStr->Buffer = ExAllocatePool(NonPagedPool, length)) == NULL) {
        ExFreePool(uniStr);
        return NULL;
        }
    RtlZeroMemory(uniStr->Buffer, length);
    uniStr->Length = 0;
    uniStr->MaximumLength = (USHORT)length;

    // Convert filename string to ansi string and then to UniCode string
    RtlInitAnsiString(&ansiStr, cstr);
    RtlAnsiStringToUnicodeString(uniStr, &ansiStr, FALSE);
    return uniStr;
}

/****************************************************************************
PARAMETERS:
uniStr  - UniCode string structure to free

REMARKS:
Frees a string allocated by the above _PM_CStringToUnicodeString function.
****************************************************************************/
void _PM_FreeUnicodeString(
    UNICODE_STRING *uniStr)
{
    if (uniStr) {
        ExFreePool(uniStr->Buffer);
        ExFreePool(uniStr);
        }
}