void PushOutputC40TextWord(ref C40TextState state, int value) { if (!(value >= 0 && value < 256)) { throw new ArgumentException("Invalid value: Exceeds range for conversion to byte"); } this._output[this._outputIdx] = (byte)value; if (state.UpperShift == true) { if (!(value >= 0 && value < 256)) { throw new ArgumentException("Invalid value: Exceeds range for conversion to upper case character"); } this._output[this._outputIdx] += 128; } this._outputIdx++; state.Shift = DmtxConstants.DmtxC40TextBasicSet; state.UpperShift = false; }
int DecodeSchemeC40Text(int startIndex, int endIndex, DmtxScheme encScheme) { int i; int packed; int[] c40Values = new int[3]; C40TextState state = new C40TextState(); state.Shift = DmtxConstants.DmtxC40TextBasicSet; state.UpperShift = false; if (!(encScheme == DmtxScheme.DmtxSchemeC40 || encScheme == DmtxScheme.DmtxSchemeText)) { throw new ArgumentException("Invalid scheme selected for decodind!"); } while (startIndex < endIndex) { /* FIXME Also check that ptr+1 is safe to access */ packed = (((int)this._code[startIndex]) << 8) | (int)this._code[startIndex + 1]; c40Values[0] = ((packed - 1) / 1600); c40Values[1] = ((packed - 1) / 40) % 40; c40Values[2] = (packed - 1) % 40; startIndex += 2; for (i = 0; i < 3; i++) { if (state.Shift == DmtxConstants.DmtxC40TextBasicSet) { /* Basic set */ if (c40Values[i] <= 2) { state.Shift = c40Values[i] + 1; } else if (c40Values[i] == 3) { PushOutputC40TextWord(ref state, ' '); } else if (c40Values[i] <= 13) { PushOutputC40TextWord(ref state, c40Values[i] - 13 + '9'); /* 0-9 */ } else if (c40Values[i] <= 39) { if (encScheme == DmtxScheme.DmtxSchemeC40) { PushOutputC40TextWord(ref state, c40Values[i] - 39 + 'Z'); /* A-Z */ } else if (encScheme == DmtxScheme.DmtxSchemeText) { PushOutputC40TextWord(ref state, c40Values[i] - 39 + 'z'); /* a-z */ } } } else if (state.Shift == DmtxConstants.DmtxC40TextShift1) { /* Shift 1 set */ PushOutputC40TextWord(ref state, c40Values[i]); /* ASCII 0 - 31 */ } else if (state.Shift == DmtxConstants.DmtxC40TextShift2) { /* Shift 2 set */ if (c40Values[i] <= 14) { PushOutputC40TextWord(ref state, c40Values[i] + 33); /* ASCII 33 - 47 */ } else if (c40Values[i] <= 21) { PushOutputC40TextWord(ref state, c40Values[i] + 43); /* ASCII 58 - 64 */ } else if (c40Values[i] <= 26) { PushOutputC40TextWord(ref state, c40Values[i] + 69); /* ASCII 91 - 95 */ } else if (c40Values[i] == 27) { PushOutputC40TextWord(ref state, 0x1d); /* FNC1 -- XXX depends on position? */ } else if (c40Values[i] == 30) { state.UpperShift = true; state.Shift = DmtxConstants.DmtxC40TextBasicSet; } } else if (state.Shift == DmtxConstants.DmtxC40TextShift3) { /* Shift 3 set */ if (encScheme == DmtxScheme.DmtxSchemeC40) { PushOutputC40TextWord(ref state, c40Values[i] + 96); } else if (encScheme == DmtxScheme.DmtxSchemeText) { if (c40Values[i] == 0) { PushOutputC40TextWord(ref state, c40Values[i] + 96); } else if (c40Values[i] <= 26) { PushOutputC40TextWord(ref state, c40Values[i] - 26 + 'Z'); /* A-Z */ } else { PushOutputC40TextWord(ref state, c40Values[i] - 31 + 127); /* { | } ~ DEL */ } } } } /* Unlatch if codeword 254 follows 2 codewords in C40/Text encodation */ if (_code[startIndex] == DmtxConstants.DmtxCharTripletUnlatch) { return(startIndex + 1); } /* Unlatch is implied if only one codeword remains */ if (endIndex - startIndex == 1) { return(startIndex); } } return(startIndex); }