Ejemplo n.º 1
0
        public override void encode(EncoderContext context)
        {
            //step C
            var buffer      = new StringBuilder();
            int currentMode = EncodingMode;

            while (context.HasMoreCharacters)
            {
                char c = context.CurrentChar;
                context.Pos++;

                encodeChar(c, buffer);

                int count = buffer.Length;
                if ((count % 3) == 0)
                {
                    writeNextTriplet(context, buffer);

                    int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, currentMode);
                    if (newMode != currentMode)
                    {
                        // Return to ASCII encodation, which will actually handle latch to new mode
                        context.signalEncoderChange(Encodation.ASCII);
                        break;
                    }
                }
            }
            handleEOD(context, buffer);
        }
Ejemplo n.º 2
0
        public void encode(EncoderContext context)
        {
            //step F
            var buffer = new StringBuilder();

            while (context.HasMoreCharacters)
            {
                char c = context.CurrentChar;
                encodeChar(c, buffer);
                context.Pos++;

                int count = buffer.Length;
                if (count >= 4)
                {
                    context.writeCodewords(encodeToCodewords(buffer, 0));
                    buffer.Remove(0, 4);

                    int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode);
                    if (newMode != EncodingMode)
                    {
                        // Return to ASCII encodation, which will actually handle latch to new mode
                        context.signalEncoderChange(Encodation.ASCII);
                        break;
                    }
                }
            }
            buffer.Append((char)31); //Unlatch
            handleEOD(context, buffer);
        }
Ejemplo n.º 3
0
 private static char encodeASCIIDigits(char digit1, char digit2)
 {
     if (HighLevelEncoder.isDigit(digit1) && HighLevelEncoder.isDigit(digit2))
     {
         int num = (digit1 - 48) * 10 + (digit2 - 48);
         return((char)(num + 130));
     }
     throw new ArgumentException("not digits: " + digit1 + digit2);
 }
Ejemplo n.º 4
0
        public void encode(EncoderContext context)
        {
            var buffer = new StringBuilder();

            buffer.Append('\u0000'); //Initialize length field
            while (context.HasMoreCharacters)
            {
                char c = context.CurrentChar;
                buffer.Append(c);

                context.Pos++;

                int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode);
                if (newMode != EncodingMode)
                {
                    // Return to ASCII encodation, which will actually handle latch to new mode
                    context.signalEncoderChange(Encodation.ASCII);
                    break;
                }
            }
            int       dataCount       = buffer.Length - 1;
            const int lengthFieldSize = 1;
            int       currentSize     = context.CodewordCount + dataCount + lengthFieldSize;

            context.updateSymbolInfo(currentSize);
            bool mustPad = (context.SymbolInfo.dataCapacity - currentSize) > 0;

            if (context.HasMoreCharacters || mustPad)
            {
                if (dataCount <= 249)
                {
                    buffer[0] = (char)dataCount;
                }
                else if (dataCount <= 1555)
                {
                    buffer[0] = (char)((dataCount / 250) + 249);
                    buffer.Insert(1, new[] { (char)(dataCount % 250) });
                }
                else
                {
                    throw new InvalidOperationException(
                              "Message length not in valid ranges: " + dataCount);
                }
            }
            {
                var c = buffer.Length;
                for (int i = 0; i < c; i++)
                {
                    context.writeCodeword(randomize255State(
                                              buffer[i], context.CodewordCount + 1));
                }
            }
        }
Ejemplo n.º 5
0
        virtual public void encode(EncoderContext context)
        {
            //step C
            var buffer = new StringBuilder();

            while (context.HasMoreCharacters)
            {
                char c = context.CurrentChar;
                context.Pos++;

                int lastCharSize = encodeChar(c, buffer);

                int unwritten = (buffer.Length / 3) * 2;

                int curCodewordCount = context.CodewordCount + unwritten;
                context.updateSymbolInfo(curCodewordCount);
                int available = context.SymbolInfo.dataCapacity - curCodewordCount;

                if (!context.HasMoreCharacters)
                {
                    //Avoid having a single C40 value in the last triplet
                    var removed = new StringBuilder();
                    if ((buffer.Length % 3) == 2 &&
                        (available < 2 || available > 2))
                    {
                        lastCharSize = backtrackOneCharacter(context, buffer, removed, lastCharSize);
                    }
                    while ((buffer.Length % 3) == 1 &&
                           ((lastCharSize <= 3 && available != 1) || lastCharSize > 3))
                    {
                        lastCharSize = backtrackOneCharacter(context, buffer, removed, lastCharSize);
                    }
                    break;
                }

                int count = buffer.Length;
                if ((count % 3) == 0)
                {
                    int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode);
                    if (newMode != EncodingMode)
                    {
                        // Return to ASCII encodation, which will actually handle latch to new mode
                        context.signalEncoderChange(Encodation.ASCII);
                        break;
                    }
                }
            }
            handleEOD(context, buffer);
        }
Ejemplo n.º 6
0
 private static void encodeChar(char c, StringBuilder sb)
 {
     if (c >= ' ' && c <= '?')
     {
         sb.Append(c);
     }
     else if (c >= '@' && c <= '^')
     {
         sb.Append((char)(c - 64));
     }
     else
     {
         HighLevelEncoder.illegalCharacter(c);
     }
 }
Ejemplo n.º 7
0
        protected override int encodeChar(char c, StringBuilder sb)
        {
            switch (c)
            {
            case '\r':
                sb.Append('\u0000');
                break;

            case '*':
                sb.Append('\u0001');
                break;

            case '>':
                sb.Append('\u0002');
                break;

            case ' ':
                sb.Append('\u0003');
                break;

            default:
                if (c >= '0' && c <= '9')
                {
                    sb.Append((char)(c - 48 + 4));
                }
                else if (c >= 'A' && c <= 'Z')
                {
                    sb.Append((char)(c - 65 + 14));
                }
                else
                {
                    HighLevelEncoder.illegalCharacter(c);
                }
                break;
            }
            return(1);
        }
Ejemplo n.º 8
0
        public void encode(EncoderContext context)
        {
            //step B
            int n = HighLevelEncoder.determineConsecutiveDigitCount(context.Message, context.Pos);

            if (n >= 2)
            {
                context.writeCodeword(encodeASCIIDigits(context.Message[context.Pos],
                                                        context.Message[context.Pos + 1]));
                context.Pos += 2;
            }
            else
            {
                char c       = context.CurrentChar;
                int  newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode);
                if (newMode != EncodingMode)
                {
                    switch (newMode)
                    {
                    case Encodation.BASE256:
                        context.writeCodeword(HighLevelEncoder.LATCH_TO_BASE256);
                        context.signalEncoderChange(Encodation.BASE256);
                        return;

                    case Encodation.C40:
                        context.writeCodeword(HighLevelEncoder.LATCH_TO_C40);
                        context.signalEncoderChange(Encodation.C40);
                        return;

                    case Encodation.X12:
                        context.writeCodeword(HighLevelEncoder.LATCH_TO_ANSIX12);
                        context.signalEncoderChange(Encodation.X12);
                        break;

                    case Encodation.TEXT:
                        context.writeCodeword(HighLevelEncoder.LATCH_TO_TEXT);
                        context.signalEncoderChange(Encodation.TEXT);
                        break;

                    case Encodation.EDIFACT:
                        context.writeCodeword(HighLevelEncoder.LATCH_TO_EDIFACT);
                        context.signalEncoderChange(Encodation.EDIFACT);
                        break;

                    default:
                        throw new InvalidOperationException("Illegal mode: " + newMode);
                    }
                }
                else if (HighLevelEncoder.isExtendedASCII(c))
                {
                    context.writeCodeword(HighLevelEncoder.UPPER_SHIFT);
                    context.writeCodeword((char)(c - 128 + 1));
                    context.Pos++;
                }
                else
                {
                    if (c == 29 &&
                        !context.Fnc1CodewordIsWritten)
                    {
                        context.writeCodeword((char)HighLevelEncoder.FNC1);
                        context.Fnc1CodewordIsWritten = true;
                    }
                    else
                    {
                        context.writeCodeword((char)(c + 1));
                    }
                    context.Pos++;
                }
            }
        }
Ejemplo n.º 9
0
 override protected int encodeChar(char c, StringBuilder sb)
 {
     if (c == ' ')
     {
         sb.Append('\u0003');
         return(1);
     }
     if (c >= '0' && c <= '9')
     {
         sb.Append((char)(c - 48 + 4));
         return(1);
     }
     if (c >= 'a' && c <= 'z')
     {
         sb.Append((char)(c - 97 + 14));
         return(1);
     }
     if (c >= '\u0000' && c <= '\u001f')
     {
         sb.Append('\u0000'); //Shift 1 Set
         sb.Append(c);
         return(2);
     }
     if (c >= '!' && c <= '/')
     {
         sb.Append('\u0001'); //Shift 2 Set
         sb.Append((char)(c - 33));
         return(2);
     }
     if (c >= ':' && c <= '@')
     {
         sb.Append('\u0001'); //Shift 2 Set
         sb.Append((char)(c - 58 + 15));
         return(2);
     }
     if (c >= '[' && c <= '_')
     {
         sb.Append('\u0001'); //Shift 2 Set
         sb.Append((char)(c - 91 + 22));
         return(2);
     }
     if (c == '\u0060')
     {
         sb.Append('\u0002'); //Shift 3 Set
         sb.Append((char)(c - 96));
         return(2);
     }
     if (c >= 'A' && c <= 'Z')
     {
         sb.Append('\u0002'); //Shift 3 Set
         sb.Append((char)(c - 65 + 1));
         return(2);
     }
     if (c >= '{' && c <= '\u007f')
     {
         sb.Append('\u0002'); //Shift 3 Set
         sb.Append((char)(c - 123 + 27));
         return(2);
     }
     if (c >= '\u0080')
     {
         sb.Append("\u0001\u001e"); //Shift 2, Upper Shift
         int len = 2;
         len += encodeChar((char)(c - 128), sb);
         return(len);
     }
     HighLevelEncoder.illegalCharacter(c);
     return(-1);
 }