summaryrefslogtreecommitdiff
path: root/src/binder/inc/stringlexer.inl
blob: f8bc3816a4177ffb1a677db29ce8932b9e7cb057 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 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.
// ============================================================
//
// StringLexer.inl
//


//
// Implements the inlined methods of StringLexer class
//
// ============================================================

#ifndef __BINDER__STRING_LEXER_INL__
#define __BINDER__STRING_LEXER_INL__

StringLexer::StringLexer()
{
    m_wcCurrentChar = INVALID_CHARACTER;
    m_fCurrentCharIsEscaped = FALSE;
}

StringLexer::~StringLexer()
{
    // Nothing to do here
}

void StringLexer::Init(SString &inputString, BOOL fSupportEscaping)
{
    m_cursor = inputString.Begin();
    m_end = inputString.End();
    m_fSupportEscaping = fSupportEscaping;
    m_fReadRawCharacter = FALSE;
}

BOOL StringLexer::IsWhitespace(WCHAR wcChar)
{
    return ((wcChar == L'\n') || (wcChar == L'\r') || (wcChar == L' ') || (wcChar == L'\t'));
}

BOOL StringLexer::IsEOS(WCHAR wcChar)
{
    return (wcChar == 0);
}
        
BOOL StringLexer::IsQuoteCharacter(WCHAR wcChar)
{
    return ((wcChar == L'\'') || (wcChar == L'"'));
}

WCHAR StringLexer::PopCharacter(BOOL *pfIsEscaped)
{
    WCHAR wcCurrentChar = m_wcCurrentChar;
    BINDER_LOG_ENTER(L"StringLexer::PopCharacter");

    if (wcCurrentChar != INVALID_CHARACTER)
    {
        BINDER_LOG(L"HAVE wcCurrentChar");
        m_wcCurrentChar = INVALID_CHARACTER;
        *pfIsEscaped = m_fCurrentCharIsEscaped;
    }
    else
    {
        BINDER_LOG(L"GET wcCurrentChar");
        wcCurrentChar = GetNextCharacter(pfIsEscaped);
    }

#ifdef BINDER_DEBUG_LOG
    PathString info;

    info.Printf(L"wcCurrentChar=%p", (void *) wcCurrentChar);
    BINDER_LOG((WCHAR *) info.GetUnicode());
#endif

    BINDER_LOG_LEAVE(L"StringLexer::PopCharacter");
    return wcCurrentChar;
}

void StringLexer::PushCharacter(WCHAR wcCurrentChar,
                                BOOL  fIsEscaped)
{
    BINDER_LOG_ENTER(L"StringLexer::PushCharacter");

#ifdef BINDER_DEBUG_LOG
    PathString info;

    info.Printf(L"wcCurrentChar=%p, fIsEscaped=%d", (void *) wcCurrentChar, fIsEscaped);
    BINDER_LOG((WCHAR *) info.GetUnicode());
#endif

    _ASSERTE(m_wcCurrentChar == INVALID_CHARACTER);

    m_wcCurrentChar = wcCurrentChar;
    m_fCurrentCharIsEscaped = fIsEscaped;

    BINDER_LOG_LEAVE(L"StringLexer::PushCharacter");
}

WCHAR StringLexer::GetRawCharacter()
{
    WCHAR wcCurrentChar = 0;

    if (m_cursor <= m_end)
    {
        wcCurrentChar = m_cursor[0];
        m_fReadRawCharacter = TRUE;
        m_cursor++;
    }
    else
    {
        m_fReadRawCharacter = FALSE;
    }

    return wcCurrentChar;
}

void StringLexer::PushRawCharacter()
{
    if (m_fReadRawCharacter)
    {
        m_cursor--;
        m_fReadRawCharacter = FALSE;
    }
}

WCHAR StringLexer::DecodeUTF16Character()
{
    // See http://www.ietf.org/rfc/rfc2781.txt for details on UTF-16 encoding.

    WCHAR wcCurrentChar = 0;
    SIZE_T nCharacters = m_end - m_cursor + 1;
    WCHAR wcChar1 = GetRawCharacter();

    if (wcChar1 < 0xd800)
    {
        wcCurrentChar = wcChar1;
    }
    else
    {
        // StringLexer is not designed to handle UTF-16 characters beyond the Basic Multilingual Plane,
        // since it stores all characters in 16-bit WCHARs.
        // However, since the vast majority of the time, we (Microsoft) produce the manifests,
        // this is likely a non-scenario, as the other Unicode planes would never be used in practice.

        if (wcChar1 <= 0xdbff) // 0xd800 - 0xdbff indicates the first WCHAR of a surrogate pair
        {
            if (nCharacters >= 2)
            {
                GetRawCharacter(); // Skip the second WCHAR of the surrogate pair
            }
        }
        // Otherwise, the character is either in the 0xdc00 - 0xdfff range, indicating the second WCHAR of a surrogate pair,
        // or in the 0xE000 - 0xFFFF range, which has within it ranges of invalid characters, and which we conservatively treat
        // as invalid.

        wcCurrentChar = INVALID_CHARACTER;
    }

    return wcCurrentChar;
}


WCHAR StringLexer::GetNextCharacter(BOOL *pfIsEscaped)
{
    *pfIsEscaped = FALSE;

    WCHAR wcCurrentChar = GetRawCharacter(); // DecodeUTF16Character()
    if (wcCurrentChar == L'\\')
    {
        WCHAR wcTempChar = GetRawCharacter(); // DecodeUTF16Character()

        if (m_fSupportEscaping)
        {
            // Handle standard escapes
            switch (wcTempChar)
            {
            case L'"':
            case L'\'':
            case L',':
            case L'\\':
            case L'/':
            case L'=':
                break;
            case L't':
                wcTempChar = 9;
                break;
            case L'n':
                wcTempChar = 10;
                break;
            case L'r':
                wcTempChar = 13;
                break;
            case L'u':
                wcTempChar = ParseUnicode();
                break;
            default:
                return INVALID_CHARACTER;
            }

            *pfIsEscaped = TRUE;
            wcCurrentChar = wcTempChar;
        }
        else
        {
            // Do not handle escapes except for quotes
            switch (wcTempChar)
            {
            case L'"':
            case L'\'':
                *pfIsEscaped = TRUE;
                wcCurrentChar = wcTempChar;
                break;
            default:
                PushRawCharacter();
                break;
            }
        }
    }

    return wcCurrentChar;
}

WCHAR StringLexer::ParseUnicode()
{
    int nCharacters = 0;
    WCHAR wcUnicodeChar = 0;

    for(;;)
    {
        WCHAR wcCurrentChar = DecodeUTF16Character();
        nCharacters++;

        if (wcCurrentChar == L';')
        {
            break;
        }
        else if ((wcCurrentChar == INVALID_CHARACTER) || (nCharacters >= 9))
        {
            return INVALID_CHARACTER;
        }

        wcUnicodeChar <<= 4;

        if ((wcCurrentChar >= L'0') && (wcCurrentChar <= L'9'))
        {
            wcUnicodeChar += (wcCurrentChar - L'0');
        }
        else if ((wcCurrentChar >= L'a') && (wcCurrentChar <= L'f'))
        {
            wcUnicodeChar += (wcCurrentChar - L'a') + 10;
        }
        else if ((wcCurrentChar >= L'A') && (wcCurrentChar <= L'F'))
        {
            wcUnicodeChar += (wcCurrentChar - L'A') + 10;
        }
        else
        {
            return INVALID_CHARACTER;
        }
    }
    
    return wcUnicodeChar;
}

#endif