#include <zxing/FormatException.h>
#include <zxing/datamatrix/decoder/DecodedBitStreamParser.h>
#include <iostream>
#include <zxing/common/DecoderResult.h>
namespace zxing {
namespace datamatrix {
using namespace std;
const char DecodedBitStreamParser::C40_BASIC_SET_CHARS[] = {
'*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
const char DecodedBitStreamParser::C40_SHIFT2_SET_CHARS[] = {
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.',
'/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'
};
const char DecodedBitStreamParser::TEXT_BASIC_SET_CHARS[] = {
'*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
const char DecodedBitStreamParser::TEXT_SHIFT3_SET_CHARS[] = {
'\'', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', (byte) 127
};
Ref<DecoderResult> DecodedBitStreamParser::decode(ArrayRef<byte> bytes) {
Ref<BitSource> bits(new BitSource(bytes));
ostringstream result;
ostringstream resultTrailer;
vector<byte> byteSegments;
int mode = ASCII_ENCODE;
do {
if (mode == ASCII_ENCODE) {
mode = decodeAsciiSegment(bits, result, resultTrailer);
} else {
switch (mode) {
case C40_ENCODE:
decodeC40Segment(bits, result);
break;
case TEXT_ENCODE:
decodeTextSegment(bits, result);
break;
case ANSIX12_ENCODE:
decodeAnsiX12Segment(bits, result);
break;
case EDIFACT_ENCODE:
decodeEdifactSegment(bits, result);
break;
case BASE256_ENCODE:
decodeBase256Segment(bits, result, byteSegments);
break;
default:
throw FormatException("Unsupported mode indicator");
}
mode = ASCII_ENCODE;
}
} while (mode != PAD_ENCODE && bits->available() > 0);
if (resultTrailer.str().size() > 0) {
result << resultTrailer.str();
}
ArrayRef<byte> rawBytes(bytes);
Ref<String> text(new String(result.str()));
return Ref<DecoderResult>(new DecoderResult(rawBytes, text));
}
int DecodedBitStreamParser::decodeAsciiSegment(Ref<BitSource> bits, ostringstream & result,
ostringstream & resultTrailer) {
bool upperShift = false;
do {
int oneByte = bits->readBits(8);
if (oneByte == 0) {
throw FormatException("Not enough bits to decode");
} else if (oneByte <= 128) {
oneByte = upperShift ? (oneByte + 128) : oneByte;
result << (byte) (oneByte - 1);
return ASCII_ENCODE;
} else if (oneByte == 129) {
return PAD_ENCODE;
} else if (oneByte <= 229) {
int value = oneByte - 130;
if (value < 10) {
result << '0';
}
result << value;
} else if (oneByte == 230) {
return C40_ENCODE;
} else if (oneByte == 231) {
return BASE256_ENCODE;
} else if (oneByte == 232) {
result << ((byte) 29);
} else if (oneByte == 233 || oneByte == 234) {
} else if (oneByte == 235) {
upperShift = true;
} else if (oneByte == 236) {
result << ("[)>RS05GS");
resultTrailer << ("RSEOT");
} else if (oneByte == 237) {
result << ("[)>RS06GS");
resultTrailer << ("RSEOT");
} else if (oneByte == 238) {
return ANSIX12_ENCODE;
} else if (oneByte == 239) {
return TEXT_ENCODE;
} else if (oneByte == 240) {
return EDIFACT_ENCODE;
} else if (oneByte == 241) {
} else if (oneByte >= 242) {
if (oneByte != 254 || bits->available() != 0) {
throw FormatException("Not to be used in ASCII encodation");
}
}
} while (bits->available() > 0);
return ASCII_ENCODE;
}
void DecodedBitStreamParser::decodeC40Segment(Ref<BitSource> bits, ostringstream & result) {
bool upperShift = false;
int cValues[3];
int shift = 0;
do {
if (bits->available() == 8) {
return;
}
int firstByte = bits->readBits(8);
if (firstByte == 254) {
return;
}
parseTwoBytes(firstByte, bits->readBits(8), cValues);
for (int i = 0; i < 3; i++) {
int cValue = cValues[i];
switch (shift) {
case 0:
if (cValue < 3) {
shift = cValue + 1;
} else {
if (upperShift) {
result << (byte) (C40_BASIC_SET_CHARS[cValue] + 128);
upperShift = false;
} else {
result << C40_BASIC_SET_CHARS[cValue];
}
}
break;
case 1:
if (upperShift) {
result << (byte) (cValue + 128);
upperShift = false;
} else {
result << (byte) cValue;
}
shift = 0;
break;
case 2:
if (cValue < 27) {
if (upperShift) {
result << (byte) (C40_SHIFT2_SET_CHARS[cValue] + 128);
upperShift = false;
} else {
result << C40_SHIFT2_SET_CHARS[cValue];
}
} else if (cValue == 27) {
result << ((byte) 29);
} else if (cValue == 30) {
upperShift = true;
} else {
throw FormatException("decodeC40Segment: Upper Shift");
}
shift = 0;
break;
case 3:
if (upperShift) {
result << (byte) (cValue + 224);
upperShift = false;
} else {
result << (byte) (cValue + 96);
}
shift = 0;
break;
default:
throw FormatException("decodeC40Segment: no case");
}
}
} while (bits->available() > 0);
}
void DecodedBitStreamParser::decodeTextSegment(Ref<BitSource> bits, ostringstream & result) {
bool upperShift = false;
int cValues[3];
int shift = 0;
do {
if (bits->available() == 8) {
return;
}
int firstByte = bits->readBits(8);
if (firstByte == 254) {
return;
}
parseTwoBytes(firstByte, bits->readBits(8), cValues);
for (int i = 0; i < 3; i++) {
int cValue = cValues[i];
switch (shift) {
case 0:
if (cValue < 3) {
shift = cValue + 1;
} else {
if (upperShift) {
result << (byte) (TEXT_BASIC_SET_CHARS[cValue] + 128);
upperShift = false;
} else {
result << (TEXT_BASIC_SET_CHARS[cValue]);
}
}
break;
case 1:
if (upperShift) {
result << (byte) (cValue + 128);
upperShift = false;
} else {
result << (byte) (cValue);
}
shift = 0;
break;
case 2:
if (cValue < 27) {
if (upperShift) {
result << (byte) (C40_SHIFT2_SET_CHARS[cValue] + 128);
upperShift = false;
} else {
result << (C40_SHIFT2_SET_CHARS[cValue]);
}
} else if (cValue == 27) {
result << ((byte) 29);
} else if (cValue == 30) {
upperShift = true;
} else {
throw FormatException("decodeTextSegment: Upper Shift");
}
shift = 0;
break;
case 3:
if (upperShift) {
result << (byte) (TEXT_SHIFT3_SET_CHARS[cValue] + 128);
upperShift = false;
} else {
result << (TEXT_SHIFT3_SET_CHARS[cValue]);
}
shift = 0;
break;
default:
throw FormatException("decodeTextSegment: no case");
}
}
} while (bits->available() > 0);
}
void DecodedBitStreamParser::decodeAnsiX12Segment(Ref<BitSource> bits, ostringstream & result) {
int cValues[3];
do {
if (bits->available() == 8) {
return;
}
int firstByte = bits->readBits(8);
if (firstByte == 254) {
return;
}
parseTwoBytes(firstByte, bits->readBits(8), cValues);
for (int i = 0; i < 3; i++) {
int cValue = cValues[i];
if (cValue == 0) {
result << '\r';
} else if (cValue == 1) {
result << '*';
} else if (cValue == 2) {
result << '>';
} else if (cValue == 3) {
result << ' ';
} else if (cValue < 14) {
result << (byte) (cValue + 44);
} else if (cValue < 40) {
result << (byte) (cValue + 51);
} else {
throw FormatException("decodeAnsiX12Segment: no case");
}
}
} while (bits->available() > 0);
}
void DecodedBitStreamParser::parseTwoBytes(int firstByte, int secondByte, int* result) {
int fullBitValue = (firstByte << 8) + secondByte - 1;
int temp = fullBitValue / 1600;
result[0] = temp;
fullBitValue -= temp * 1600;
temp = fullBitValue / 40;
result[1] = temp;
result[2] = fullBitValue - temp * 40;
}
void DecodedBitStreamParser::decodeEdifactSegment(Ref<BitSource> bits, ostringstream & result) {
do {
if (bits->available() <= 16) {
return;
}
for (int i = 0; i < 4; i++) {
int edifactValue = bits->readBits(6);
if (edifactValue == 0x1f) {
int bitsLeft = 8 - bits->getBitOffset();
if (bitsLeft != 8) {
bits->readBits(bitsLeft);
}
return;
}
if ((edifactValue & 0x20) == 0) {
edifactValue |= 0x40;
}
result << (byte)(edifactValue);
}
} while (bits->available() > 0);
}
void DecodedBitStreamParser::decodeBase256Segment(Ref<BitSource> bits, ostringstream& result, vector<byte> byteSegments) {
int codewordPosition = 1 + bits->getByteOffset();
int d1 = unrandomize255State(bits->readBits(8), codewordPosition++);
int count;
if (d1 == 0) {
count = bits->available() / 8;
} else if (d1 < 250) {
count = d1;
} else {
count = 250 * (d1 - 249) + unrandomize255State(bits->readBits(8), codewordPosition++);
}
if (count < 0) {
throw FormatException("NegativeArraySizeException");
}
char* bytes = new char[count];
for (int i = 0; i < count; i++) {
if (bits->available() < 8) {
throw FormatException("byteSegments");
}
bytes[i] = unrandomize255State(bits->readBits(8), codewordPosition++);
byteSegments.push_back(bytes[i]);
result << (byte)bytes[i];
}
delete bytes;
}
}
}