Files
Pyarmor-Static-Unpack-1shot/pyc_string.cpp

142 lines
4.0 KiB
C++
Raw Normal View History

2011-10-23 17:48:10 -07:00
#include "pyc_string.h"
#include "pyc_module.h"
2009-07-24 08:35:21 +00:00
#include "data.h"
#include <stdexcept>
2009-07-24 08:35:21 +00:00
static bool check_ascii(const std::string& data)
{
auto cp = reinterpret_cast<const unsigned char*>(data.c_str());
while (*cp) {
if (*cp & 0x80)
return false;
++cp;
}
return true;
}
2009-07-24 08:35:21 +00:00
/* PycString */
void PycString::load(PycData* stream, PycModule* mod)
{
if (type() == TYPE_STRINGREF) {
PycRef<PycString> str = mod->getIntern(stream->get32());
m_value.resize(str->length());
if (str->length())
std::char_traits<char>::copy(&m_value.front(), str->value(), str->length());
2009-07-24 08:35:21 +00:00
} else {
int length;
if (type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
length = stream->getByte();
else
length = stream->get32();
if (length < 0)
throw std::bad_alloc();
m_value.resize(length);
if (length) {
stream->getBuffer(length, &m_value.front());
if (type() == TYPE_ASCII || type() == TYPE_ASCII_INTERNED ||
type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED) {
if (!check_ascii(m_value))
throw std::runtime_error("Invalid bytes in ASCII string");
}
2009-07-24 08:35:21 +00:00
}
if (type() == TYPE_INTERNED || type() == TYPE_ASCII_INTERNED ||
type() == TYPE_SHORT_ASCII_INTERNED)
2009-07-24 08:35:21 +00:00
mod->intern(this);
}
}
2009-07-24 19:52:47 +00:00
2009-07-24 21:15:51 +00:00
bool PycString::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
2009-07-24 21:15:51 +00:00
PycRef<PycString> strObj = obj.cast<PycString>();
return isEqual(strObj->m_value);
2009-07-24 21:15:51 +00:00
}
void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F, const char* parent_f_string_quote)
2009-07-24 19:52:47 +00:00
{
if (prefix != 0)
fputc(prefix, F);
2009-07-24 19:52:47 +00:00
const char* ch = str->value();
int len = str->length();
if (ch == 0) {
fputs("''", F);
2009-07-24 19:52:47 +00:00
return;
}
// Determine preferred quote style (Emulate Python's method)
bool useQuotes = false;
2020-10-17 21:04:39 +11:00
if (!parent_f_string_quote) {
while (len--) {
if (*ch == '\'') {
useQuotes = true;
2020-10-20 21:08:02 -07:00
} else if (*ch == '"') {
useQuotes = false;
break;
}
ch++;
}
2020-10-20 21:08:02 -07:00
} else {
useQuotes = parent_f_string_quote[0] == '"';
}
ch = str->value();
len = str->length();
// Output the string
if (!parent_f_string_quote) {
if (triple)
fputs(useQuotes ? "\"\"\"" : "'''", F);
else
fputc(useQuotes ? '"' : '\'', F);
}
while (len--) {
if (*ch < 0x20 || *ch == 0x7F) {
2009-07-24 19:52:47 +00:00
if (*ch == '\r') {
fputs("\\r", F);
2009-07-24 19:52:47 +00:00
} else if (*ch == '\n') {
if (triple)
2009-07-24 19:52:47 +00:00
fputc('\n', F);
else
fputs("\\n", F);
2009-07-24 19:52:47 +00:00
} else if (*ch == '\t') {
fputs("\\t", F);
2009-07-24 19:52:47 +00:00
} else {
fprintf(F, "\\x%02x", (*ch & 0xFF));
2009-07-24 19:52:47 +00:00
}
} 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));
}
2009-07-24 19:52:47 +00:00
} else {
if (!useQuotes && *ch == '\'')
fputs("\\'", F);
else if (useQuotes && *ch == '"')
fputs("\\\"", F);
2011-10-23 19:33:24 -07:00
else if (*ch == '\\')
fputs("\\\\", F);
else if (parent_f_string_quote && *ch == '{')
fputs("{{", F);
else if (parent_f_string_quote && *ch == '}')
fputs("}}", F);
2009-07-24 19:52:47 +00:00
else
fputc(*ch, F);
}
ch++;
}
if (!parent_f_string_quote) {
2020-10-17 21:04:39 +11:00
if (triple)
fputs(useQuotes ? "\"\"\"" : "'''", F);
else
fputc(useQuotes ? '"' : '\'', F);
}
2009-07-24 19:52:47 +00:00
}