Skip to content
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

Enable binary decoding. #80

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
language: cpp
script: mkdir build && cd build && cmake -G "Unix Makefiles" .. && make
script:
- mkdir build
- cd build
- cmake -G "Unix Makefiles" ..
- make
- export PATH=$(pwd):$PATH
- ../tests/test.sh
9 changes: 8 additions & 1 deletion core/src/zxing/common/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ using namespace zxing::common;

// N.B.: these are the iconv strings for at least some versions of iconv

char const* const StringUtils::PLATFORM_DEFAULT_ENCODING = "UTF-8";
char const* const StringUtils::PLATFORM_DEFAULT_ENCODING = "ISO8859-1";
// This default is an 8-bit fixed-width encoding and it's identical with
// unicode in its range, which reduces accidental mangling during decode.
char const* const StringUtils::ASCII = "ASCII";
char const* const StringUtils::SHIFT_JIS = "SHIFT_JIS";
char const* const StringUtils::GB2312 = "GBK";
Expand Down Expand Up @@ -76,6 +78,11 @@ StringUtils::guessEncoding(char* bytes, int length,

int value = bytes[i] & 0xFF;

// embedded nuls are a sure sign of binary data
if(value == '\x00') {
canBeISO88591 = canBeShiftJIS = canBeUTF8 = false;
}

// UTF-8 stuff
if (canBeUTF8) {
if (utf8BytesLeft > 0) {
Expand Down
29 changes: 20 additions & 9 deletions core/src/zxing/qrcode/decoder/DecodedBitStreamParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ void DecodedBitStreamParser::append(std::string &result,
return;
}

if(src == NULL) {
// don't try to recode un-encoded data.
result.append((const char *)bufIn, nIn);
return;
}

iconv_t cd = iconv_open(StringUtils::UTF8, src);
if (cd == (iconv_t)-1) {
result.append((const char *)bufIn, nIn);
Expand Down Expand Up @@ -193,19 +199,24 @@ void DecodedBitStreamParser::decodeByteSegment(Ref<BitSource> bits_,
for (int i = 0; i < count; i++) {
readBytes[i] = (char) bits.readBits(8);
}
string encoding;
if (currentCharacterSetECI == 0) {
// The spec isn't clear on this mode; see
// section 6.4.5: t does not say which encoding to assuming
// upon decoding. I have seen ISO-8859-1 used as well as
// Shift_JIS -- without anything like an ECI designator to
// give a hint.
encoding = StringUtils::guessEncoding(readBytes, count, hints);
const char* encoding = NULL;
if (currentCharacterSetECI == NULL) {
// The spec says
// 8.3.1: The default interpretation for QR Code is ECI 000020 representing the JIS8 and Shift JIS character sets.
// 8.4.4: In [8-bit Byte Mode], one 8 bit codeword directly represents the JIS8 character [...].
// In ECIs other than the default ECI, it represents an 8-bit byte value directly.
// If I'm reading that right, *if* the character set is unspecified *or* explicitly set to Shift-JIS,
// *then* use JIS8 but *otherwise* don't try to decode the value.
//
// That's a stupid spec.
// Instead we follow qrencode:
// unspecified ECI <=> unmolested binary
encoding = NULL;
} else {
encoding = currentCharacterSetECI->name();
}
try {
append(result, readBytes, nBytes, encoding.c_str());
append(result, readBytes, nBytes, encoding);
} catch (ReaderException const& ignored) {
(void)ignored;
throw FormatException();
Expand Down
Binary file added tests/amen-01.bin
Binary file not shown.
Binary file added tests/amen-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash


cd "$(dirname "$0")";

find . -name "*.png" | while read file; do
#zxing has a --test-mode but it's not working for binary files
# perl is here because in textmode, zxing appends a newline, but a lot of test files don't have that
zxing "$file" | perl -pe 'chomp if eof' | diff -u "$(ls "${file%.*}".{txt,bin} 2>/dev/null)" -
done