Exemple #1
0
        /// <summary>
        /// Pack an <see cref="EmvTags"/> object into a byte array
        /// </summary>
        /// <param name="tags">
        /// The tags
        /// </param>
        /// <returns>
        /// The packed byte array
        /// </returns>
        public static byte[] Pack(this EmvTags tags)
        {
            var stream = new MemoryStream();

            foreach (var kvp in tags)
            {
                var field = PackField(kvp.Key, kvp.Value);
                stream.Write(field, 0, field.Length);
            }

            return(stream.ToArray());
        }
Exemple #2
0
        /// <summary>
        /// Unpack a byte array into an <see cref="EmvTags"/> object
        /// </summary>
        /// <param name="msg">
        /// The data
        /// </param>
        /// <returns>
        /// The <see cref="EmvTags"/> object
        /// </returns>
        public static EmvTags UnpackEmvTags(byte[] msg)
        {
            var emvTags = new EmvTags();
            var stream  = new MemoryStream(msg);

            while (stream.Position < stream.Length)
            {
                var tagValue = stream.ReadByte();

                // Extended tag
                if ((tagValue & 0x1F) == 0x1F)
                {
                    tagValue <<= 8;
                    tagValue  += stream.ReadByte();
                }

                var tag    = (Tag)tagValue;
                var length = stream.ReadByte();
                if (length > 127)
                {
                    int lengthOfLength = length - 128;
                    stream.Seek(lengthOfLength, SeekOrigin.Current);
                    length = stream.ReadByte();
                }

                byte[] data = null;

                if (length > 0)
                {
                    data = new byte[length];
                    stream.Read(data, 0, length);
                }

                emvTags.Add(tag, data);
            }

            return(emvTags);
        }