Skip to content

Don't apply any text encoding to binary input data #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions core/src/TextDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace ZXing {

void TextDecoder::Append(std::string& str, const uint8_t* bytes, size_t length, CharacterSet charset, bool sjisASCII)
{
if (charset == CharacterSet::BINARY) {
str.reserve(str.length() + length);
std::copy(bytes, bytes + length, std::back_inserter(str));
return;
}

int eci = ToInt(ToECI(charset));
const size_t str_len = str.length();
const int bytes_len = narrow_cast<int>(length);
Expand Down Expand Up @@ -46,9 +52,14 @@ void TextDecoder::Append(std::string& str, const uint8_t* bytes, size_t length,

void TextDecoder::Append(std::wstring& str, const uint8_t* bytes, size_t length, CharacterSet charset)
{
std::string u8str;
Append(u8str, bytes, length, charset);
str.append(FromUtf8(u8str));
if (charset == CharacterSet::BINARY) {
str.reserve(str.size() + length);
std::copy(bytes, bytes + length, std::back_inserter(str));
} else {
std::string u8str;
Append(u8str, bytes, length, charset);
str.append(FromUtf8(u8str));
}
}

/**
Expand Down
13 changes: 12 additions & 1 deletion core/src/TextEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace ZXing {

void TextEncoder::GetBytes(const std::string& str, CharacterSet charset, std::string& bytes)
{
if (charset == CharacterSet::BINARY) {
bytes = str;
return;
}

int eci = ToInt(ToECI(charset));
const int str_len = narrow_cast<int>(str.length());
int eci_len;
Expand Down Expand Up @@ -44,7 +49,13 @@ void TextEncoder::GetBytes(const std::string& str, CharacterSet charset, std::st

void TextEncoder::GetBytes(const std::wstring& str, CharacterSet charset, std::string& bytes)
{
GetBytes(ToUtf8(str), charset, bytes);
if (charset == CharacterSet::BINARY) {
bytes.clear();
bytes.reserve(str.size());
std::copy(str.begin(), str.end(), std::back_inserter(bytes));
} else {
GetBytes(ToUtf8(str), charset, bytes);
}
}

} // ZXing
2 changes: 1 addition & 1 deletion test/unit/TextEncoderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TEST(TextEncoderTest, FullCycleEncodeDecode)
EnDeCode(CharacterSet::UTF32BE, u8"\u20AC", std::string("\x00\x00\x20\xAC", 4)); // EURO SIGN
EnDeCode(CharacterSet::UTF32LE, u8"\u20AC", std::string("\xAC\x20\x00\x00", 4)); // EURO SIGN
// EnDeCode(CharacterSet::ISO646_Inv, "%", "%");
EnDeCode(CharacterSet::BINARY, u8"\u0080\u00FF", "\x80\xFF");
EnDeCode(CharacterSet::BINARY, u8"\u0080\u00FF", "\xC2\x80\xC3\xBF");
EnDeCode(CharacterSet::Unknown, u8"\u0080", "\x80"); // Treated as binary
EnDeCode(CharacterSet::EUC_JP, u8"\u0080", "\x80"); // Not supported, treated as binary
}