Пример #1
0
        private string ReadString(string label, out TextLineSections[] textList)
        {
            long   pos = Position;
            string val = _reader.ReadString();
            int    sizeInBytesOfString = (int)(Position - pos);

            Position = pos;

            // Can't use val.Length to compute number of bytes because multiple
            // bytes might make one Unicode character.
            // For Example:   the bytes 03 E2 97 8f make one Unicode char (The
            // Dot used in password boxes).
            byte[] bytes = _reader.ReadBytes(sizeInBytesOfString);
            if (sizeInBytesOfString <= LineLength)
            {
                string meaning = string.Format("{0}='{1}'", label, val);
                textList    = new TextLineSections[1];
                textList[0] = new TextLineSections(pos, bytes, meaning);
            }
            else
            {
                TextLineSections[] textListCut = CutIntoLines(pos, bytes, val, label);
                textList = new TextLineSections[textListCut.Length + 1];
                Array.Copy(textListCut, textList, textListCut.Length);
                textList[textListCut.Length] = new TextLineSections(pos, new byte[0], val);
            }
            return(val);
        }
Пример #2
0
        private short[] ReadIntArrayType(string label, out TextLineSections[] textList)
        {
            long pos = Position;

            short arrayLength = (short)_reader.ReadInt16();

            short[] array = new short[arrayLength];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = (short)_reader.ReadInt16();
            }
            int sizeOfArray = (int)(Position - pos);

            Position = pos;

            textList = new TextLineSections[arrayLength + 1];
            string meaning = string.Format("{0}={1}", label, array.Length);

            byte[] subBytes = _reader.ReadBytes(2);
            textList[0] = new TextLineSections(Position - 2, subBytes, meaning);

            for (int i = 0; i < array.Length; i++)
            {
                meaning         = string.Format("Assembly ID[{0}]={1}", i, array[i]);
                subBytes        = _reader.ReadBytes(2);
                textList[i + 1] = new TextLineSections(Position - 2, subBytes, meaning);
            }
            return(array);
        }
Пример #3
0
        private void ReadFormatVersion(out TextLineSections[] text)
        {
            TextLineSections UnicodeLength;

            TextLineSections[] Unicode;
            TextLineSections   Version1;
            TextLineSections   Version2;
            TextLineSections   Version3;

            int strByteLength = (int)ReadIntType(4, "Unicode String Length(bytes)", out UnicodeLength);

            ReadByteLengthPrefixedDWordPaddedUnicodeString(strByteLength, out Unicode);
            ReadVersion("Reader Version", out Version1);
            ReadVersion("Update Version", out Version2);
            ReadVersion("Writer Version", out Version3);

            text    = new TextLineSections[4 + Unicode.Length];
            text[0] = UnicodeLength;
            for (int i = 0; i < Unicode.Length; i++)
            {
                text[1 + i] = Unicode[i];
            }
            text[1 + Unicode.Length] = Version1;
            text[2 + Unicode.Length] = Version2;
            text[3 + Unicode.Length] = Version3;
            return;
        }
Пример #4
0
        private bool ReadBool(string label, out TextLineSections text)
        {
            long pos = Position;
            bool val = _reader.ReadBoolean();

            Position = pos;

            byte[] bytes   = _reader.ReadBytes(1);
            string meaning = string.Format("{0}={1}", label, val.ToString());

            text = new TextLineSections(pos, bytes, meaning);
            return(val);
        }
Пример #5
0
        private int Read7bitEncodedInt(string label, out TextLineSections text)
        {
            long pos            = Position;
            int  val            = _reader.Read7BitEncodedInt();
            int  sizeOfEncoding = (int)(Position - pos);

            Position = pos;

            byte[] bytes   = _reader.ReadBytes(sizeOfEncoding);
            string meaning = string.Format("{0}={1}", label, val.ToString());

            text = new TextLineSections(pos, bytes, meaning);
            return(val);
        }
Пример #6
0
        private BamlRecordType ReadRecordType(out TextLineSections text)
        {
            long pos      = Position;
            int  recordId = _reader.ReadByte();

            Position = pos;

            BamlRecordType val     = (BamlRecordType)recordId;
            string         meaning = string.Format("{0} [BamlRecord]", val.ToString());

            byte[] bytes = _reader.ReadBytes(1);
            text = new TextLineSections(pos, bytes, meaning);
            return(val);
        }
Пример #7
0
        private void ReadVersion(string label, out TextLineSections text)
        {
            short val1;
            short val2;

            long pos = Position;

            val1     = _reader.ReadInt16();
            val2     = _reader.ReadInt16();
            Position = pos;

            byte[] bytes   = _reader.ReadBytes(4);
            string meaning = string.Format("{0}={1}.{2}", label, val1.ToString(), val2.ToString());

            text = new TextLineSections(pos, bytes, meaning);
        }
Пример #8
0
        private object ReadField(BamlField field, out TextLineSections[] textList)
        {
            TextLineSections text = new TextLineSections("Err", "Err", "Err");
            object           val  = null;
            Type             t    = field.ClrType;

            textList = null;
            if (t == typeof(short))
            {
                val = (Int16)ReadIntType(2, field.Name, out text);
            }
            else if (t == typeof(int))
            {
                val = (Int32)ReadIntType(4, field.Name, out text);
            }
            else if (t == typeof(string))
            {
                val = (string)ReadString(field.Name, out textList);
            }
            else if (t == typeof(bool))
            {
                val = (bool)ReadBool(field.Name, out text);
            }
            else if (t == typeof(byte))
            {
                val = (byte)ReadIntType(1, field.Name, out text);
            }
            else if (t == FormatVersionType)
            {
                ReadFormatVersion(out textList);
            }
            else if (t == typeof(Int16[]))
            {
                val = (Int16[])ReadIntArrayType(field.Name, out textList);
            }
            else
            {
                throw new InvalidOperationException("Field Type not known");
            }
            if (null == textList)
            {
                textList    = new TextLineSections[1];
                textList[0] = text;
            }

            return(val);
        }
Пример #9
0
        // OUTPUT methods.


        private string OutputTextLine(TextLineSections text, TextType textType)
        {
            string outputLine = String.Empty;

            if (ShowRecordNumbers)
            {
                string numString = String.Format("#{0:d3}: ", _recordNumber);
                if (textType == TextType.Record)
                {
                    outputLine += numString;
                }
                else
                {
                    outputLine += FormatPadToColumn(numString.Length, 0);
                }
            }
            if (ShowAddresses)
            {
                outputLine += text.addrStr;
            }
            outputLine += string.Format(" {0}", text.hexStr);

            int column = 0;

            if (outputLine.Length < 40)
            {
                column = 40;
            }
            else
            {
                column = 60;
            }
            outputLine += this.FormatPadToColumn(column, outputLine.Length);

            outputLine += text.meaningStr;

            return(outputLine);
        }
Пример #10
0
        // Output a long block of bytes possibly over several lines.
        // If "label" is non-null then there is a header line of output.
        // If "val" is non-null then the byte block has a parallel "string" version.
        private TextLineSections[] CutIntoLines(long position, byte[] bytes, string val, string label)
        {
            TextLineSections[] textList;

            bool hasLabel  = (label != null && label.Length > 0);
            bool hasString = (val != null && val.Length > 0);

            // Output the first "header" line.
            // If there is a Lable then the header line is the string length
            //   and label text.
            // If there is a Lable but no string, then the bytes is not a string
            //   is the just a blob of bytes.
            int headerOffset = 0;
            int currentLine  = 0;

            if (!hasLabel)
            {
                int lineCount = (bytes.Length + LineLength - 1) / LineLength;
                textList = new TextLineSections[lineCount];
            }
            else
            {
                int              sizeOfHeader = 0;
                byte[]           headerBytes  = null;
                TextLineSections labelLine;

                if (!hasString)
                {
                    sizeOfHeader = (bytes.Length > LineLength) ? LineLength : bytes.Length;
                    headerBytes  = SnipBytes(bytes, 0, LineLength);
                    labelLine    = new TextLineSections(position, headerBytes, label);
                    headerOffset = sizeOfHeader;
                }
                else
                {
                    sizeOfHeader = ComputeLengthOf7bitEncodedLengthPrefix(bytes.Length);
                    headerBytes  = SnipBytes(bytes, 0, sizeOfHeader);
                    int    stringLength = val.Length;
                    string meaning      = string.Format("{0}= string of length {1}({2})", label, stringLength.ToString("x2"), stringLength);
                    labelLine    = new TextLineSections(position, headerBytes, meaning);
                    headerOffset = sizeOfHeader;
                }
                int lineCount = 1 + ((bytes.Length - sizeOfHeader) + LineLength - 1) / LineLength;
                textList = new TextLineSections[lineCount];
                textList[currentLine++] = labelLine;
            }

            // Output the middle "full length lines"
            int byteOffset = 0;
            int valOffset  = 0;

            for (valOffset = 0; (byteOffset = valOffset + headerOffset) < bytes.Length - LineLength; valOffset += LineLength)
            {
                byte[] subBytes = SnipBytes(bytes, byteOffset, LineLength);
                string subText  = "";
                if (hasString)
                {   // The unicode string may run short early.
                    int remainingVal = val.Length - valOffset;
                    if (remainingVal > 0)
                    {
                        int    subValLen = (remainingVal < LineLength) ? remainingVal : LineLength;
                        char[] subChars  = val.ToCharArray(valOffset, subValLen);
                        subText = new string(subChars);
                    }
                }
                // March 2010: removed the subStrings at the ends of the lines.
                textList[currentLine++] = new TextLineSections(position + byteOffset, subBytes, /*subText*/ String.Empty);
            }

            // output the last line.
            int remaining = bytes.Length - byteOffset;

            if (remaining > 0)
            {
                byte[] subBytes = SnipBytes(bytes, byteOffset, remaining);
                string subText  = "";
                if (hasString)
                {
                    int remainingVal = val.Length - valOffset;
                    if (remainingVal > 0)
                    {
                        char[] subChars = val.ToCharArray(valOffset, remainingVal);
                        subText = new string(subChars);
                    }
                }
                // March 2010: removed the subStrings at the ends of the lines.
                textList[currentLine++] = new TextLineSections(position + byteOffset, subBytes, /*subText*/ String.Empty);
            }
            return(textList);
        }
Пример #11
0
        private long ReadIntType(int size, string label, out TextLineSections text)
        {
            long val = -1;


            string meaning = "";

            long pos = Position;

            switch (size)
            {
            case 1:
                val = (sbyte)_reader.ReadSByte();
                break;

            case 2:
                val = (short)_reader.ReadInt16();
                break;

            case 4:
                val = (int)_reader.ReadInt32();
                break;

            case 8:
                val = (long)_reader.ReadInt64();
                break;

            default:
                throw new InvalidOperationException("Bad integral datatype size");
            }
            Position = pos;

            byte[] bytes = _reader.ReadBytes(size);

            sbyte  val8         = -1;
            short  val16        = -1;
            int    val32        = -1;
            long   val64        = -1;
            string formatString = (0 <= val && val < 10) ? "{0}={1}" : "{0}={1}({2})";

            switch (size)
            {
            case 1:
                val8    = (sbyte)val;
                meaning = string.Format(formatString, label, val8.ToString("x"), val8.ToString("d"));
                break;

            case 2:
                val16   = (short)val;
                meaning = string.Format(formatString, label, val16.ToString("x"), val16.ToString("d"));
                break;

            case 4:
                val32   = (int)val;
                meaning = string.Format(formatString, label, val32.ToString("x"), val32.ToString("d"));
                break;

            case 8:
                val64   = (long)val;
                meaning = string.Format(formatString, label, val64.ToString("x"), val64.ToString("d"));
                break;
            }

            text = new TextLineSections(pos, bytes, meaning);
            return(val);
        }