예제 #1
0
        private void CheckEnd(int capacity)
        {
            var item = _data.Last();

            if (item.Format == EncoderFormat.AsciiLower || item.Format == EncoderFormat.Byte)
            {
                return;
            }

            if (item.Format == EncoderFormat.EDIFACT)
            {
                var tmp       = item.Data.TrimEnd(SWITCHASCII);
                var remaining = (tmp.Length) % 4;
                if (remaining == 1 || remaining == 2)
                {
                    _data.Add(new DataItem(EncoderFormat.AsciiLower, tmp.Substring(tmp.Length - remaining)));

                    item.Data      = tmp.Remove(tmp.Length - remaining);
                    _currentFormat = EncoderFormat.AsciiLower;
                }
                else if (remaining == 0)
                {
                    item.Data = tmp;
                }

                return;
            }

            //handle C40 end rules
        }
예제 #2
0
        /// <summary>
        /// Compacts 3 bytes into 2.
        /// </summary>
        /// <param name="stream">stream to compact from</param>
        private void Compact(System.IO.MemoryStream stream)
        {
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            int tmp = 0;

            while (stream.Position + 3 < stream.Length)
            {
                tmp = (tmp * 40) + stream.ReadByte();

                if (stream.Position > 1 && stream.Position % 3 == 1)
                {
                    tmp++;
                    _Stream.WriteByte((byte)(tmp / 256));
                    _Stream.WriteByte((byte)(tmp % 256));
                    tmp = 0;
                }
            }

            if (stream.Position != stream.Length)
            {
                _Format = EncoderFormat.AsciiLower;
                _Stream.WriteByte((byte)SWITCHASCII);
                _Index -= (int)(stream.Length - stream.Position - 1);
            }
            else if (CanRead())
            {
                _Stream.WriteByte((byte)SWITCHASCII);
            }
        }
예제 #3
0
        public void Write(string value, EncoderFormat format)
        {
            if (format == EncoderFormat.Auto)
            {
                format = EncoderFormat.AsciiLower;
            }

            _data.Add(new DataItem(format, value));
        }
예제 #4
0
        /// <summary>
        /// Encodes 3 bytes into 2 bytes. Used by C40 and TEXT
        /// </summary>
        /// <param name="format">format to use</param>
        private void DoubleByteEncoder(EncoderFormat format)
        {
            MemoryStream buffer = new MemoryStream();

            while (CanRead() && !IsFormatSwitch(Current))
            {
                CharToInt(Current, buffer, format);
                _Index++;
            }

            Compact(buffer);
        }
예제 #5
0
        /// <summary>
        /// Encodes a value using the selcted format.
        /// </summary>
        /// <param name="value">string value to encode</param>
        /// <param name="format">encoding format to use</param>
        /// <returns>encoded data as a byte array</returns>
        public byte[] Encode(string value, EncoderFormat format)
        {
            _stream        = new MemoryStream();
            _valueToEncode = value;
            _currentIndex  = 0;
            _currentFormat = format;

            if (format == EncoderFormat.Auto)
            {
                _currentFormat = EncoderFormat.AsciiLower;
            }

            while (_currentIndex < _valueToEncode.Length)
            {
                switch (_currentFormat)
                {
                case EncoderFormat.Auto:
                case EncoderFormat.AsciiLower:
                case EncoderFormat.AsciiExtended:
                    AsciiEncode();
                    break;

                case EncoderFormat.AsciiNumber:
                    break;

                case EncoderFormat.C40:
                    C40Encoder();
                    break;

                case EncoderFormat.TEXT:
                    TEXTEncoder();
                    break;

                case EncoderFormat.X12:
                    AnsiX12();
                    break;

                case EncoderFormat.EDIFACT:
                    EdifactEncoder();
                    break;

                case EncoderFormat.Byte:
                    ByteEncoder(Encoding.ASCII.GetBytes(_valueToEncode.Substring(_currentIndex)));
                    break;

                default:
                    break;
                }
            }

            return(_stream.ToArray());
        }
예제 #6
0
        /// <summary>
        /// Encodes using EDIFACT rules
        /// </summary>
        private void EdifactEncoder()
        {
            MemoryStream buffer = new MemoryStream();

            while (CanRead() && !IsFormatSwitch(Current))
            {
                if (Current >= 64 && Current <= 94)
                {
                    buffer.WriteByte((byte)(Current - 64));
                }
                else if (Current >= 32 && Current <= 63)
                {
                    buffer.WriteByte((byte)Current);
                }
                else
                {
                    buffer.WriteByte(31);
                    _Format = EncoderFormat.AsciiLower;
                    break;
                }
            }

            while (buffer.Length % 4 != 0)
            {
                buffer.WriteByte(0);
            }

            _Stream.WriteByte((byte)SWITCHEDIFACT);

            buffer.Seek(0, SeekOrigin.Begin);
            int tmp = 0;

            while (buffer.Position + 4 < buffer.Length)
            {
                tmp = (tmp * 64) + buffer.ReadByte();

                if (buffer.Position > 1 && buffer.Position % 4 == 1)
                {
                    _Stream.WriteByte((byte)(tmp / 65536));
                    _Stream.WriteByte((byte)((tmp % 65536) / 256));
                    _Stream.WriteByte((byte)(tmp % 256));

                    tmp = 0;
                }
            }
        }
예제 #7
0
        private void SwitchFormat(DataItem item)
        {
            if (_currentFormat == item.Format)
            {
                return;
            }

            if (_currentFormat == EncoderFormat.AsciiLower)
            {
                switch (item.Format)
                {
                case EncoderFormat.C40:
                    _output.WriteInt(SWITCHC40);
                    break;

                case EncoderFormat.TEXT:
                    _output.WriteInt(SWITCHTEXT);
                    break;

                case EncoderFormat.X12:
                    _output.WriteInt(SWITCHX12);
                    break;

                case EncoderFormat.EDIFACT:
                    _output.WriteInt(SWITCHEDIFACT);
                    break;

                case EncoderFormat.Byte:
                    _currentFormat = EncoderFormat.AsciiLower;
                    break;

                default:
                    throw new BarcodeException("Unknown or unsupported encoding format");
                }

                return;
            }

            if (item.Data.Last() != SWITCHASCII)
            {
                item.Data += SWITCHASCII;
            }
            _currentFormat = EncoderFormat.AsciiLower;
            SwitchFormat(item);
        }
예제 #8
0
        public byte[] ToArray(int capacity)
        {
            _currentFormat = EncoderFormat.AsciiLower;
            _output        = new MemoryStream();

            for (int i = 0; i < _data.Count; i++)
            {
                var item = _data[i];

                SwitchFormat(item);

                if (i + 1 == _data.Count)
                {
                    CheckEnd(capacity);
                }

                switch (item.Format)
                {
                case EncoderFormat.AsciiLower:
                    ParseAscii(item.Data);
                    break;

                case EncoderFormat.C40:
                case EncoderFormat.TEXT:
                case EncoderFormat.X12:
                    ParseC40(item.Data);
                    break;

                case EncoderFormat.EDIFACT:
                    ParseEdifact(item.Data);
                    break;

                case EncoderFormat.Byte:
                    ParseBinary(item.BinaryData);
                    break;
                }
            }

            if (capacity > -1 && _output.Length > capacity)
            {
                throw new BarcodeException("Data exceeds barcode capacity");
            }

            return(_output.ToArray());
        }
예제 #9
0
        /// <summary>
        /// Encodes using ANSI X12 rules
        /// </summary>
        private void AnsiX12()
        {
            _Stream.WriteByte((byte)SWITCHX12);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            while (CanRead() && !IsFormatSwitch(Current))
            {
                if (Current == 13)
                {
                    ms.WriteByte(0);
                }
                else if (Current == 42)
                {
                    ms.WriteByte(1);
                }
                else if (Current == 62)
                {
                    ms.WriteByte(2);
                }
                else if (Current == 32)
                {
                    ms.WriteByte(3);
                }
                else if (Current >= 48 && Current <= 57)
                {
                    ms.WriteByte((byte)(Current - 44));
                }
                else if (Current >= 65 && Current <= 90)
                {
                    ms.WriteByte((byte)(Current - 51));
                }
                else
                {
                    _Format = EncoderFormat.AsciiLower;
                    break;
                }

                _Index++;
            }

            Compact(ms);
        }
예제 #10
0
        /// <summary>
        /// Checks the supplied value to see if a switch in encoding rules is needed
        /// </summary>
        /// <param name="value">value to check</param>
        /// <returns>true if a switch is needed. the target format is set as the current format</returns>
        private bool IsFormatSwitch(int value)
        {
            if (_currentFormat != EncoderFormat.AsciiLower && value == SWITCHASCII)
            {
                _currentFormat = EncoderFormat.AsciiLower;
                return(true);
            }

            switch (value)
            {
            case SWITCHASCII:
                _currentFormat = EncoderFormat.AsciiLower;
                return(true);

            case SWITCHBYTE:
                _currentFormat = EncoderFormat.Byte;
                return(true);

            case SWITCHC40:
                _currentFormat = EncoderFormat.C40;
                return(true);

            case SWITCHEDIFACT:
                _currentFormat = EncoderFormat.EDIFACT;
                return(true);

            case SWITCHTEXT:
                _currentFormat = EncoderFormat.TEXT;
                return(true);

            case SWITCHX12:
                _currentFormat = EncoderFormat.X12;
                return(true);

            default:
                return(false);
            }
        }
예제 #11
0
 public DataItem(EncoderFormat format, byte[] data)
 {
     Format     = format;
     BinaryData = data;
     Data       = null;
 }
예제 #12
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Checks the supplied value to see if a switch in encoding rules is needed
        /// </summary>
        /// <param name="value">value to check</param>
        /// <returns>true if a switch is needed. the target format is set as the current format</returns>
        private bool IsFormatSwitch(int value)
        {
            if (_Format != EncoderFormat.AsciiLower && value == SWITCHASCII)
            {
                _Format = EncoderFormat.AsciiLower;
                return true;
            }

            switch (value)
            {
                case SWITCHASCII:
                    return true;
                case SWITCHBYTE:
                    _Format = EncoderFormat.Byte;
                    return true;
                case SWITCHC40:
                    _Format = EncoderFormat.C40;
                    return true;
                case SWITCHEDIFACT:
                    _Format = EncoderFormat.EDIFACT;
                    return true;
                case SWITCHTEXT:
                    _Format = EncoderFormat.TEXT;
                    return true;
                case SWITCHX12:
                    _Format = EncoderFormat.X12;
                    return true;
                default:
                    return false;
            }
        }
예제 #13
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Encodes using EDIFACT rules
        /// </summary>
        private void EdifactEncoder()
        {
            MemoryStream buffer = new MemoryStream();

            while (CanRead() && !IsFormatSwitch(Current))
            {
                if (Current >= 64 && Current <= 94)
                    buffer.WriteByte((byte)(Current - 64));
                else if (Current >= 32 && Current <= 63)
                    buffer.WriteByte((byte)Current);
                else
                {
                    buffer.WriteByte(31);
                    _Format = EncoderFormat.AsciiLower;
                    break;
                }
            }

            while (buffer.Length % 4 != 0)
            {
                buffer.WriteByte(0);
            }

            _Stream.WriteByte((byte)SWITCHEDIFACT);

            buffer.Seek(0, SeekOrigin.Begin);
            int tmp = 0;
            while (buffer.Position + 4 < buffer.Length)
            {
                tmp = (tmp * 64) + buffer.ReadByte();

                if (buffer.Position > 1 && buffer.Position % 4 == 1)
                {
                    _Stream.WriteByte((byte)(tmp / 65536));
                    _Stream.WriteByte((byte)((tmp % 65536) / 256));
                    _Stream.WriteByte((byte)(tmp % 256));

                    tmp = 0;
                }
            }
        }
예제 #14
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Encodes 3 bytes into 2 bytes. Used by C40 and TEXT
        /// </summary>
        /// <param name="format">format to use</param>
        private void DoubleByteEncoder(EncoderFormat format)
        {
            MemoryStream buffer = new MemoryStream();

            while (CanRead() && !IsFormatSwitch(Current))
            {
                CharToInt(Current, buffer, format);
                _Index++;
            }

            Compact(buffer);
        }
예제 #15
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Compacts 3 bytes into 2.
        /// </summary>
        /// <param name="stream">stream to compact from</param>
        private void Compact(System.IO.MemoryStream stream)
        {
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            int tmp = 0;
            while (stream.Position + 3 < stream.Length)
            {
                tmp = (tmp * 40) + stream.ReadByte();

                if (stream.Position > 1 && stream.Position % 3 == 1)
                {
                    tmp++;
                    _Stream.WriteByte((byte)(tmp / 256));
                    _Stream.WriteByte((byte)(tmp % 256));
                    tmp = 0;
                }
            }

            if (stream.Position != stream.Length)
            {
                _Format = EncoderFormat.AsciiLower;
                _Stream.WriteByte((byte)SWITCHASCII);
                _Index -= (int)(stream.Length - stream.Position - 1);
            }
            else if (CanRead())
                _Stream.WriteByte((byte)SWITCHASCII);
        }
예제 #16
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Encodes the char using the specified format and writes the result to the stream
        /// </summary>
        /// <param name="value">value to encode</param>
        /// <param name="stream">stream to write to</param>
        /// <param name="format">Encoding format</param>
        private void CharToInt(char value, System.IO.MemoryStream stream, EncoderFormat format)
        {
            //Extended ASCII
            if (value > 127)
            {
                stream.WriteByte(1);
                stream.WriteByte(30);
                value = (char)(value - 128);
            }

            if (value == 32)
            {
                stream.WriteByte(3);
                return;
            }

            if (value >= 48 && value <= 57)
            {
                stream.WriteByte((byte)(value - 44));
                return;
            }
            //Shift set 1
            if (value >= 0 && value <= 31)
            {
                stream.WriteByte(0);
                stream.WriteByte((byte)value);
                return;
            }

            //Shift set 2
            if (value >= 33 && value <= 47)
            {
                stream.WriteByte(1);
                stream.WriteByte((byte)(value - 33));
                return;
            }
            if (value >= 58 && value <= 64)
            {
                stream.WriteByte(1);
                stream.WriteByte((byte)(value - 58 + 15));
                return;
            }
            if (value >= 91 && value <= 95)
            {

                stream.WriteByte(1);
                stream.WriteByte((byte)(value - 91 + 22));
                return;
            }

            if (format == EncoderFormat.C40)
            {
                //Shift 3
                if (value >= 96 && value <= 127)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 96));
                    return;
                }
            }

            if (format == EncoderFormat.TEXT)
            {
                //Shift 3
                if (value == 96)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 96));
                    return;
                }
                if (value >= 65 && value <= 90)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 65 + 1));
                    return;
                }
                if (value >= 123 && value <= 127)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 123 + 27));
                    return;
                }
            }
        }
예제 #17
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Encodes using ANSI X12 rules
        /// </summary>
        private void AnsiX12()
        {
            _Stream.WriteByte((byte)SWITCHX12);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            while (CanRead() && !IsFormatSwitch(Current))
            {
                if (Current == 13)
                    ms.WriteByte(0);
                else if (Current == 42)
                    ms.WriteByte(1);
                else if (Current == 62)
                    ms.WriteByte(2);
                else if (Current == 32)
                    ms.WriteByte(3);
                else if (Current >= 48 && Current <= 57)
                    ms.WriteByte((byte)(Current - 44));
                else if (Current >= 65 && Current <= 90)
                    ms.WriteByte((byte)(Current - 51));
                else
                {
                    _Format = EncoderFormat.AsciiLower;
                    break;
                }

                _Index++;
            }

            Compact(ms);
        }
예제 #18
0
 public DataItem(EncoderFormat format, string data)
 {
     Format = format;
     Data = data;
     BinaryData = null;
 }
예제 #19
0
        /// <summary>
        /// Encodes the char using the specified format and writes the result to the stream
        /// </summary>
        /// <param name="value">value to encode</param>
        /// <param name="stream">stream to write to</param>
        /// <param name="format">Encoding format</param>
        private void CharToInt(char value, System.IO.MemoryStream stream, EncoderFormat format)
        {
            //Extended ASCII
            if (value > 127)
            {
                stream.WriteByte(1);
                stream.WriteByte(30);
                value = (char)(value - 128);
            }

            if (value == 32)
            {
                stream.WriteByte(3);
                return;
            }

            if (value >= 48 && value <= 57)
            {
                stream.WriteByte((byte)(value - 44));
                return;
            }
            //Shift set 1
            if (value >= 0 && value <= 31)
            {
                stream.WriteByte(0);
                stream.WriteByte((byte)value);
                return;
            }

            //Shift set 2
            if (value >= 33 && value <= 47)
            {
                stream.WriteByte(1);
                stream.WriteByte((byte)(value - 33));
                return;
            }
            if (value >= 58 && value <= 64)
            {
                stream.WriteByte(1);
                stream.WriteByte((byte)(value - 58 + 15));
                return;
            }
            if (value >= 91 && value <= 95)
            {
                stream.WriteByte(1);
                stream.WriteByte((byte)(value - 91 + 22));
                return;
            }

            if (format == EncoderFormat.C40)
            {
                //Shift 3
                if (value >= 96 && value <= 127)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 96));
                    return;
                }
            }

            if (format == EncoderFormat.TEXT)
            {
                //Shift 3
                if (value == 96)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 96));
                    return;
                }
                if (value >= 65 && value <= 90)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 65 + 1));
                    return;
                }
                if (value >= 123 && value <= 127)
                {
                    stream.WriteByte(2);
                    stream.WriteByte((byte)(value - 123 + 27));
                    return;
                }
            }
        }
예제 #20
0
 public DataItem(EncoderFormat format, byte[] data)
 {
     Format = format;
     BinaryData = data;
     Data = null;
 }
예제 #21
0
 public DataItem(EncoderFormat format, string data)
 {
     Format     = format;
     Data       = data;
     BinaryData = null;
 }
예제 #22
0
        private void CheckEnd(int capacity)
        {
            var item = _data.Last();

            if (item.Format == EncoderFormat.AsciiLower || item.Format == EncoderFormat.Byte)
                return;

            if (item.Format == EncoderFormat.EDIFACT)
            {
                var tmp = item.Data.TrimEnd(SWITCHASCII);
                var remaining = (tmp.Length) % 4;
                if (remaining == 1 || remaining == 2)
                {
                    _data.Add(new DataItem(EncoderFormat.AsciiLower, tmp.Substring(tmp.Length - remaining)));

                    item.Data = tmp.Remove(tmp.Length - remaining);
                    _currentFormat = EncoderFormat.AsciiLower;
                }
                else if (remaining == 0)
                    item.Data = tmp;

                return;
            }

            //handle C40 end rules
        }
예제 #23
0
        public void Write(string value, EncoderFormat format)
        {
            if (format == EncoderFormat.Auto)
                format = EncoderFormat.AsciiLower;

            _data.Add(new DataItem(format, value));
        }
예제 #24
0
파일: Encoder.cs 프로젝트: noikiy/Barcodes
        /// <summary>
        /// Encodes a value using the selcted format.
        /// </summary>
        /// <param name="value">string value to encode</param>
        /// <param name="format">encoding format to use</param>
        /// <returns>encoded data as a byte array</returns>
        public byte[] Encode(string value, EncoderFormat format)
        {
            _Stream = new MemoryStream();
            _Value = value;
            _Index = 0;

            if (format == EncoderFormat.Auto)
                _Format = EncoderFormat.AsciiLower;

            while (_Index < _Value.Length)
            {
                switch (_Format)
                {
                    case EncoderFormat.Auto:
                    case EncoderFormat.AsciiLower:
                    case EncoderFormat.AsciiExtended:
                        AsciiEncode();
                        break;
                    case EncoderFormat.AsciiNumber:
                        break;
                    case EncoderFormat.C40:
                        C40Encoder();
                        break;
                    case EncoderFormat.TEXT:
                        TEXTEncoder();
                        break;
                    case EncoderFormat.X12:
                        AnsiX12();
                        break;
                    case EncoderFormat.EDIFACT:
                        EdifactEncoder();
                        break;
                    case EncoderFormat.Byte:
                        ByteEncoder(Encoding.ASCII.GetBytes(_Value.Substring(_Index)));
                        break;
                    default:
                        break;
                }
            }

            return _Stream.ToArray();
        }
예제 #25
0
        public byte[] ToArray(int capacity)
        {
            _currentFormat = EncoderFormat.AsciiLower;
            _output = new MemoryStream();

            for (int i = 0; i < _data.Count; i++)
            {
                var item = _data[i];

                SwitchFormat(item);

                if (i + 1 == _data.Count)
                    CheckEnd(capacity);

                switch (item.Format)
                {
                    case EncoderFormat.AsciiLower:
                        ParseAscii(item.Data);
                        break;
                    case EncoderFormat.C40:
                    case EncoderFormat.TEXT:
                    case EncoderFormat.X12:
                        ParseC40(item.Data);
                        break;
                    case EncoderFormat.EDIFACT:
                        ParseEdifact(item.Data);
                        break;
                    case EncoderFormat.Byte:
                        ParseBinary(item.BinaryData);
                        break;
                }
            }

            if (capacity > -1 && _output.Length > capacity)
                throw new BarcodeException("Data exceeds barcode capacity");

            return _output.ToArray();
        }
예제 #26
0
        private void SwitchFormat(DataItem item)
        {
            if (_currentFormat == item.Format)
                return;

            if (_currentFormat == EncoderFormat.AsciiLower)
            {
                switch (item.Format)
                {
                    case EncoderFormat.C40:
                        _output.WriteInt(SWITCHC40);
                        break;
                    case EncoderFormat.TEXT:
                        _output.WriteInt(SWITCHTEXT);
                        break;
                    case EncoderFormat.X12:
                        _output.WriteInt(SWITCHX12);
                        break;
                    case EncoderFormat.EDIFACT:
                        _output.WriteInt(SWITCHEDIFACT);
                        break;
                    case EncoderFormat.Byte:
                        _currentFormat = EncoderFormat.AsciiLower;
                        break;
                    default:
                        throw new BarcodeException("Unknown or unsupported encoding format");
                }

                return;
            }

            if (item.Data.Last() != SWITCHASCII)
                item.Data += SWITCHASCII;
            _currentFormat = EncoderFormat.AsciiLower;
            SwitchFormat(item);
        }