forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc_string.cpp
169 lines (152 loc) · 4.46 KB
/
pyc_string.cpp
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
#include "pyc_string.h"
#include "pyc_module.h"
#include "data.h"
#include <cstring>
static void ascii_to_utf8(char** data)
{
size_t utf8len = 0, asciilen = 0;
unsigned char* cp = reinterpret_cast<unsigned char*>(*data);
while (*cp) {
if (*cp & 0x80)
utf8len += 2;
else
utf8len += 1;
// Advance ASCII pointer
++asciilen;
++cp;
}
if (asciilen == utf8len) {
// This can only happen if all characters are [0x00-0x7f].
// If that happens, we don't need to do any conversion, nor
// reallocate any buffers. Woot!
return;
}
char* utf8_buffer = new char[utf8len + 1];
unsigned char* up = reinterpret_cast<unsigned char*>(utf8_buffer);
cp = reinterpret_cast<unsigned char*>(*data);
while (*cp) {
if (*cp & 0x80) {
*up++ = 0xC0 | ((*cp >> 6) & 0x1F);
*up++ = 0x80 | ((*cp ) & 0x3F);
} else {
*up++ = *cp;
}
++cp;
}
utf8_buffer[utf8len] = 0;
delete[] *data;
*data = utf8_buffer;
}
/* PycString */
void PycString::load(PycData* stream, PycModule* mod)
{
delete[] m_value;
if (type() == TYPE_STRINGREF) {
PycRef<PycString> str = mod->getIntern(stream->get32());
m_length = str->length();
if (m_length) {
m_value = new char[m_length+1];
memcpy(m_value, str->value(), m_length);
m_value[m_length] = 0;
} else {
m_value = 0;
}
} else {
if (type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
m_length = stream->getByte();
else
m_length = stream->get32();
if (m_length) {
m_value = new char[m_length+1];
stream->getBuffer(m_length, m_value);
m_value[m_length] = 0;
if (type() == TYPE_ASCII || type() == TYPE_ASCII_INTERNED ||
type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
ascii_to_utf8(&m_value);
} else {
m_value = 0;
}
if (type() == TYPE_INTERNED || type() == TYPE_ASCII_INTERNED ||
type() == TYPE_SHORT_ASCII_INTERNED)
mod->intern(this);
}
}
bool PycString::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycString> strObj = obj.cast<PycString>();
return isEqual(strObj->m_value);
}
bool PycString::isEqual(const char* str) const
{
if (m_value == str)
return true;
return (strcmp(m_value, str) == 0);
}
void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F)
{
if (prefix != 0)
fputc(prefix, F);
const char* ch = str->value();
int len = str->length();
if (ch == 0) {
fprintf(F, "''");
return;
}
// Determine preferred quote style (Emulate Python's method)
bool useQuotes = false;
while (len--) {
if (*ch == '\'') {
useQuotes = true;
} else if (*ch == '"') {
useQuotes = false;
break;
}
ch++;
}
ch = str->value();
len = str->length();
// Output the string
if (triple)
fprintf(F, useQuotes ? "\"\"\"" : "'''");
else
fputc(useQuotes ? '"' : '\'', F);
while (len--) {
if (*ch < 0x20 || *ch == 0x7F) {
if (*ch == '\r') {
fprintf(F, "\\r");
} else if (*ch == '\n') {
if (triple)
fputc('\n', F);
else
fprintf(F, "\\n");
} else if (*ch == '\t') {
fprintf(F, "\\t");
} else {
fprintf(F, "\\x%02x", (*ch & 0xFF));
}
} else if ((unsigned char)(*ch) >= 0x80) {
if (str->type() == PycObject::TYPE_UNICODE) {
// Unicode stored as UTF-8... Let the stream interpret it
fputc(*ch, F);
} else {
fprintf(F, "\\x%x", (*ch & 0xFF));
}
} else {
if (!useQuotes && *ch == '\'')
fprintf(F, "\\'");
else if (useQuotes && *ch == '"')
fprintf(F, "\\\"");
else if (*ch == '\\')
fprintf(F, "\\\\");
else
fputc(*ch, F);
}
ch++;
}
if (triple)
fprintf(F, useQuotes ? "\"\"\"" : "'''");
else
fputc(useQuotes ? '"' : '\'', F);
}