summaryrefslogtreecommitdiff
path: root/src/inc/fixedsizestring.h
blob: 18becea3491a39472d98b74070fa6755edfe8ccf (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
// 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.

// 

// Fixed size string: no dynamic memory allocation, no destructor, no exception
template<typename T>
class FixedSizeString
{
    enum { FixedStringSize = 256 };

    T        m_string[FixedStringSize]; // Using template to support both char and wchar_t
    HRESULT  m_error;
    unsigned m_pos;

public:
    FixedSizeString()
    {
        Reset();
    }

    void Reset()
    {
        m_string[0] = 0;
        m_pos       = 0;
        m_error     = S_OK;
    }

    HRESULT GetError() const
    {
        return m_error;
    }

    void Append(char ch)
    {
        _ASSERTE((m_pos + 1) < FixedStringSize);

        if ((m_pos + 1) < FixedStringSize)
        {
            m_string[m_pos ++] = ch;
            m_string[m_pos ] = 0;
        }
        else
        {
            m_error = E_OUTOFMEMORY;
        }
    }

    void Append(const char * pStr)
    {
        while (* pStr)
        {
            _ASSERTE((m_pos + 1) < FixedStringSize);

            if ((m_pos + 1) < FixedStringSize)
            {
                _ASSERTE((pStr[0] & 0x80) == 0);
                m_string[m_pos ++] = * pStr ++;
            }
            else
            {
                m_error = E_OUTOFMEMORY;
                return;
            }
        }

        m_string[m_pos] = 0;
    }

    operator const T * () const
    {
        return m_string;
    }

    // Decode encoded assembly/function/field/attribute name back to string, add '.' as seperator
    void DecodeName(int count, const unsigned char * pCode, const LPCSTR * pDic)
    {
        Reset();

        for (int i = 0; i < count; i ++)
        {
            unsigned char code = pCode[i];

            if (code == 0)
            {
                break;
            }

            if (i != 0)
            {
                Append('.');
            }

            Append(pDic[code]);
        }

        _ASSERTE(SUCCEEDED(GetError()));
    }
};


// Same as fusion\utils\helpers.cpp HashString(wsKey, 0, dwHashSize, FALSE), duplicated here because cee_wks does not include fusion\utils\helpers.cpp

// Needs to match public static uint HashLCString(string str) in OptimizeFxRetarget.csscript
    
inline DWORD HashLCString(LPCSTR pKey)
{
    DWORD dwHash = 0;
    
    while (* pKey)
    {
        char ch = * pKey ++;

        if ((ch >= 'A') && (ch <= 'Z'))
        {
            ch += 32;
        }

        dwHash = (dwHash * 65599) + (DWORD) ch;
    }

    return dwHash;
}


inline DWORD HashLCString(LPCWSTR pKey)
{
    DWORD dwHash = 0;
    
    while (* pKey)
    {
        wchar_t ch = * pKey ++;

        if ((ch >= 'A') && (ch <= 'Z'))
        {
            ch += 32;
        }

        dwHash = (dwHash * 65599) + (DWORD) ch;
    }

    return dwHash;
}


// Enumerator for auto-generated hash table

// There are two arrays in the hash: hash array and collision array
// Each entry is two bytes : <index + 1, collision index>
// The first entry in collision array is always (0, 0) for termination

class StringHashEnumerator
{
    const BYTE * m_pHash;
    const BYTE * m_pCollision;

public:

    StringHashEnumerator(LPCWSTR pStr, const BYTE * pHash, size_t hashCount, const BYTE * pCollision)
    {
        // lower case string hashing, half the size of hash array
        DWORD hash = HashLCString(pStr) % ((DWORD) hashCount / 2);

        m_pCollision = pCollision;
        m_pHash      = pHash + hash * 2; // pointing to entry in hash array
    }

    StringHashEnumerator(LPCSTR pStr, const BYTE * pHash, size_t hashCount, const BYTE * pCollision)
    {
        // lower case string hashing, half the size of hash array
        DWORD hash = HashLCString(pStr) % ((DWORD) hashCount / 2);

        m_pCollision = pCollision;
        m_pHash      = pHash + hash * 2; // pointing to entry in hash array
    }

    int GetNext() // negative is ending
    {
        BYTE index = m_pHash[0];

        m_pHash = m_pCollision + m_pHash[1] * 2; // move to the next one: collision array

        return index - 1;
    }
};