summaryrefslogtreecommitdiff
path: root/src/pal/src/safecrt/mbusafecrt.cpp
blob: 4446f77fd143504501b7af3f9167748eab371c3a (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/***
*   mbusafecrt.c - implementaion of support functions and data for MBUSafeCRT
*

*
*   Purpose:
*       This file contains the implementation of support functions and
*       data for MBUSafeCRT declared in mbusafecrt.h and mbusafecrt_internal.h.
****/

#include "pal/palinternal.h"
#include <string.h>
#include <errno.h>
#include <limits.h>

#include "mbusafecrt_internal.h"

/* global data */
tSafeCRT_AssertFuncPtr sMBUSafeCRTAssertFunc = NULL;

/***
*   MBUSafeCRTSetAssertFunc - Set the function called when an assert fails.
****/

void MBUSafeCRTSetAssertFunc( tSafeCRT_AssertFuncPtr inAssertFuncPtr )
{
    /* set it */
    sMBUSafeCRTAssertFunc = inAssertFuncPtr;
}

/***
*   _putc_nolock - putc for the miniFILE stream.
****/

int _putc_nolock( char inChar, miniFILE* inStream )
{
    int returnValue = EOF;

        inStream->_cnt -= sizeof( char );

    if ( ( inStream->_cnt ) >= 0 )
    {
        *( inStream->_ptr ) = inChar;
        inStream->_ptr += sizeof( char );
        returnValue = ( int )inChar;
    }

    return returnValue;
}

/***
*   _putwc_nolock - putwc for the miniFILE stream.
****/

int _putwc_nolock( wchar_t inChar, miniFILE* inStream )
{
    int returnValue = WEOF;

        inStream->_cnt -= sizeof( wchar_t );

    if ( ( inStream->_cnt ) >= 0 )
    {
        *( ( wchar_t* )( inStream->_ptr ) ) = inChar;
        inStream->_ptr += sizeof( wchar_t );
        returnValue = ( int )inChar;
    }

    return returnValue;
}

/***
*   _getc_nolock - getc for the miniFILE stream.
****/

int _getc_nolock( miniFILE* inStream )
{
    int returnValue = EOF;

    if ( ( inStream->_cnt ) >= ( int )( sizeof( char ) ) )
    {
        inStream->_cnt -= sizeof( char );
        returnValue = ( int )( *( inStream->_ptr ) );
        inStream->_ptr += sizeof( char );
    }

    return returnValue;
}

/***
*   _getwc_nolock - getc for the miniFILE stream.
****/

int _getwc_nolock( miniFILE* inStream )
{
    int returnValue = EOF;

    if ( ( inStream->_cnt ) >= ( int )( sizeof( wchar_t ) ) )
    {
        inStream->_cnt -= sizeof( wchar_t );
        returnValue = ( int )( *( ( wchar_t* )( inStream->_ptr ) ) );
        inStream->_ptr += sizeof( wchar_t );
    }

    return returnValue;
}

/***
*   _ungetc_nolock - ungetc for the miniFILE stream.
****/

int _ungetc_nolock( char inChar, miniFILE* inStream )
{
    int returnValue = EOF;

    if ( ( size_t )( ( inStream->_ptr ) - ( inStream->_base ) ) >= ( sizeof( char ) ) )
    {
        inStream->_cnt += sizeof( char );
        inStream->_ptr -= sizeof( char );
        return ( int )inChar;
    }

    return returnValue;
}

/***
*   _ungetwc_nolock - ungetwc for the miniFILE stream.
****/

int _ungetwc_nolock( wchar_t inChar, miniFILE* inStream )
{
    int returnValue = WEOF;
    
    if ( ( size_t )( ( inStream->_ptr ) - ( inStream->_base ) ) >= ( sizeof( wchar_t ) ) )
    {
        inStream->_cnt += sizeof( wchar_t );
        inStream->_ptr -= sizeof( wchar_t );
        returnValue = ( unsigned short )inChar;
    }
    
    return returnValue;
}


/***
*   _safecrt_cfltcvt - convert a float to an ascii string.
****/

/* routine used for floating-point output */
#define FORMATSIZE 30

// taken from output.inl
#define FL_ALTERNATE  0x00080   /* alternate form requested */

errno_t _safecrt_cfltcvt(double *arg, char *buffer, size_t sizeInBytes, int type, int precision, int flags)
{
    char format[FORMATSIZE];
    size_t formatlen = 0;
    int retvalue;

    if (flags & 1)
    {
        type -= 'a' - 'A';
    }
    formatlen = 0;
    format[formatlen++] = '%';
    if (flags & FL_ALTERNATE)
    {
        format[formatlen++] = '#';
    }
    format[formatlen++] = '.';
    _itoa_s(precision, format + formatlen, FORMATSIZE - formatlen, 10);
    formatlen = strlen(format);
    format[formatlen++] = (char)type;
    format[formatlen] = 0;

    buffer[sizeInBytes - 1] = 0;
    retvalue = snprintf(buffer, sizeInBytes, format, *arg);
    if (buffer[sizeInBytes - 1] != 0 || retvalue <= 0)
    {
        buffer[0] = 0;
        return EINVAL;
    }
    return 0;
}


/***
*   _safecrt_fassign - convert a string into a float or double.
****/

void _safecrt_fassign(int flag, void* argument, char* number )
{
    if ( flag != 0 )    // double
    {
        double dblValue = strtod(number, NULL);
        *( ( double* )argument ) = dblValue;
    }
    else                // float
    {
        float fltValue = strtof(number, NULL);
        *( ( float* )argument ) = fltValue;
    }
}


/***
*   _safecrt_wfassign - convert a wchar_t string into a float or double.
****/

void _safecrt_wfassign(int flag, void* argument, wchar_t* number )
{
    // We cannot use system functions for this - they
    // assume that wchar_t is four bytes, while we assume
    // two. So, we need to convert to a regular char string
    // without using any system functions. To do this,
    // we'll assume that the numbers are in the 0-9 range and
    // do a simple conversion.
    
    char* numberAsChars = ( char* )number;
    int position = 0;
    
    // do the convert
    while ( number[ position ] != 0 )
    {
        numberAsChars[ position ] = ( char )( number[ position ] & 0x00FF );
        position++;
    }
    numberAsChars[ position ] = ( char )( number[ position ] & 0x00FF );
    
    // call the normal char version
    _safecrt_fassign( flag, argument, numberAsChars );
}


/***
*   _minimal_chartowchar - do a simple char to wchar conversion.
****/

int _minimal_chartowchar( wchar_t* outWChar, const char* inChar )
{
    *outWChar = ( wchar_t )( ( unsigned short )( ( unsigned char )( *inChar ) ) );
    return 1;
}