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

86 lines
2.3 KiB
C++
Raw Normal View History

2009-07-24 08:35:21 +00:00
#include "string.h"
#include "data.h"
#include "module.h"
#include <cstring>
/* PycString */
void PycString::load(PycData* stream, PycModule* mod)
{
if (m_value) 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 {
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;
} else {
m_value = 0;
}
if (type() == TYPE_INTERNED)
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>();
if (m_value == strObj->m_value)
return true;
return (strcmp(m_value, strObj->m_value) == 0);
}
2009-07-24 19:52:47 +00:00
void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F)
{
const char* ch = str->value();
int len = str->length();
2009-07-24 19:52:47 +00:00
if (ch == 0)
return;
while (len--) {
if (*ch < 0x20 || *ch == 0x7F) {
2009-07-24 19:52:47 +00:00
if (*ch == '\r') {
fprintf(F, "\\r");
} else if (*ch == '\n') {
if (style == QS_BlockSingle || style == QS_BlockDouble)
fputc('\n', F);
else
fprintf(F, "\\n");
} else if (*ch == '\t') {
fprintf(F, "\\t");
} else {
fprintf(F, "\\x%x", *ch);
}
} else if (*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);
}
2009-07-24 19:52:47 +00:00
} else {
if (style == QS_Single && *ch == '\'')
fprintf(F, "\\'");
else if (style == QS_Double && *ch == '"')
fprintf(F, "\\\"");
else
fputc(*ch, F);
}
ch++;
}
}