Beispiel #1
0
 internal Record(byte[] rid, Bytes data)
 {
     _rid = org.in2bits.MyXls.RID.ByteArray(rid);
     int offset = 0;
     int bytesRemaining = data.Length;
     int continueIndex = -1;
     while (bytesRemaining > 0)
     {
         int bytesToAppend = Math.Min(bytesRemaining, BIFF8.MaxDataBytesPerRecord);
         Bytes target;
         if (continueIndex == -1)
             _data = data.Get(offset, bytesToAppend);
         else
             _continues.Add(new Record(MyXls.RID.CONTINUE, data.Get(offset, bytesToAppend)));
         offset += bytesToAppend;
         bytesRemaining -= bytesToAppend;
         continueIndex++;
     }
 }
Beispiel #2
0
 private void ReadBytes(Bytes bytes)
 {
     byte[] byteArray = bytes.ByteArray;
     _height = BitConverter.ToUInt16(byteArray, 0);
     SetOptionsValue(bytes.Get(2, 2));
     _color = _doc.Workbook.Palette.GetColor(BitConverter.ToUInt16(byteArray, 4));
     _weight = FontWeightConverter.Convert(BitConverter.ToUInt16(byteArray, 6));
     _escapement = (EscapementTypes) BitConverter.ToUInt16(byteArray, 8);
     _underline = (UnderlineTypes) byteArray[10];
     _fontFamily = (FontFamilies) byteArray[11];
     _characterSet = (CharacterSets) byteArray[12];
     //skip byte index 13
     _fontName = UnicodeBytes.Read(bytes.Get(14, bytes.Length - 14), 8);
 }
Beispiel #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static List<Record> GetAll(Bytes stream)
        {
            int i = 0;
            List<Record> records = new List<Record>();
            Record lastNonContinue = Record.Empty;
            while (i < (stream.Length - 4))
            {
                byte[] rid = org.in2bits.MyXls.RID.ByteArray(stream.Get(i, 2).ByteArray);
                Bytes data = new Bytes();
                if (rid == MyXls.RID.Empty)
                    break;
                int length = BitConverter.ToUInt16(stream.Get(i + 2, 2).ByteArray, 0);
                data = stream.Get(i + 4, length);
                Record record = new Record(rid, data);
                i += (4 + length);
                if (rid == MyXls.RID.CONTINUE)
                {
                    if (lastNonContinue == Record.Empty)
                        throw new Exception("Found CONTINUE record without previous/parent record.");

                    lastNonContinue.Continues.Add(record);
                }
                else
                {
                    lastNonContinue = record;
                    records.Add(record);
                }
            }

            return records;
        }
Beispiel #4
0
        internal static Bytes GetBytes(byte[] rid, Bytes data)
        {
            if (rid.Length != 2)
                throw new ArgumentException("must be 2 bytes", "rid");

            Bytes record = new Bytes();

            ushort offset = 0;
            ushort totalLength = (ushort)data.Length;
            do
            {
                ushort length = Math.Min((ushort) (totalLength - offset), BIFF8.MaxDataBytesPerRecord);

                if (offset == 0)
                {
                    record.Append(rid);
                    record.Append(BitConverter.GetBytes(length));
                    record.Append(data.Get(offset, length));
                }
                else
                {
                    record.Append(MyXls.RID.CONTINUE);
                    record.Append(BitConverter.GetBytes(length));
                    record.Append(data.Get(offset, length));
                }

                offset += length;
            } while (offset < totalLength);

            return record;
        }
Beispiel #5
0
        private static Bytes GetShortSectorBytes(Bytes fromShortSectorStream, int shortSectorSize, int sid)
        {
            int i = (int) Math.Pow(2, shortSectorSize);

            if (fromShortSectorStream.Length < (sid * i))
                throw new Exception(string.Format("Invalid SID [{0}] (EOF reached)", sid));

            //	        return fromShortSectorStream.Get((sid * i) + 1, i);
            return fromShortSectorStream.Get(sid * i, i);
        }
Beispiel #6
0
        private Bytes GetStream(System.IO.Stream fromDocumentStream, int did, Dictionary<int, byte[]> dir, ushort sectorSize, int[] sat, ushort shortSectorSize, int[] ssat, uint minStandardStreamSize)
        {
            Bytes stream = new Bytes();
            Bytes fromBytes;
            int[] fromSAT;
            ushort fromSectorSize;
            int sidNext;
            string shortness;

            int streamLength = BitConverter.ToInt32(MidByteArray(dir[did], 120, 4), 0);

            Bytes streamBytes = null;

            if (did == 0 || (streamLength >= minStandardStreamSize))
            {
                byte[] streamByteArray;
                streamByteArray = new byte[fromDocumentStream.Length];
                fromDocumentStream.Position = 0;
                fromDocumentStream.Read(streamByteArray, 0, streamByteArray.Length);
                streamBytes = new Bytes(streamByteArray);
            }

            if (did == 0)
            {
                fromSectorSize = sectorSize;
                fromSAT = sat;
                shortness = string.Empty;
                fromBytes = streamBytes;
            }
            else if (streamLength < minStandardStreamSize)
            {
                fromSectorSize = shortSectorSize;
                fromSAT = ssat;
                shortness = "Short ";
                fromBytes = GetStream(fromDocumentStream, 0, dir, sectorSize, sat, shortSectorSize, ssat, minStandardStreamSize);
            }
            else
            {
                fromSectorSize = sectorSize;
                fromSAT = sat;
                shortness = string.Empty;
                fromBytes = streamBytes;
            }

            sidNext = BitConverter.ToInt32(MidByteArray(dir[did], 116, 4), 0);
            while (sidNext > -2)
            {
                Bytes sector;
                if (did > 0 && streamLength < minStandardStreamSize)
                    sector = GetShortSectorBytes(fromBytes, fromSectorSize, sidNext);
                else
                    sector = GetSectorBytes(fromBytes, fromSectorSize, sidNext);

                if (sector.Length == 0)
                    throw new Exception(string.Format("{0}Sector not found [SID{1}]", shortness, sidNext));

                stream.Append(sector);

                sidNext = fromSAT[sidNext];
            }

            return stream.Get(streamLength);
        }
Beispiel #7
0
 private void ReadBytes(Bytes bytes, Font font, string format)
 {
     _font = font;
     _format = format;
     ReadXF_3(bytes.Get(4, 2));
 }
Beispiel #8
0
 private static object DecodeRKInt(Bytes.Bits bits, bool div100)
 {
     object val = bits.Get(2, 30).ToInt32();
     if (div100)
     {
         val = Convert.ToDouble(val) / 100.0;
     }
     return val;
 }
Beispiel #9
0
 private static double DecodeRKFloat(Bytes.Bits bits, bool div100)
 {
     Bytes.Bits floatBits = bits.Get(2, 30);
     floatBits.Prepend(false); //right-shift to full 8 bytes
     floatBits.Prepend(false);
     byte[] floatBytes = new byte[8];
     floatBits.GetBytes().ByteArray.CopyTo(floatBytes, 4);
     byte[] double1Bytes = BitConverter.GetBytes((double) 1);
     double val = BitConverter.ToDouble(floatBytes, 0);
     if (div100)
     {
         val /= 100.0;
     }
     return val;
 }
Beispiel #10
0
 private static void WriteRecordsToFile(Bytes bytes, string fileName)
 {
     FileInfo fi = new FileInfo(fileName);
     FileStream fs = fi.Open(FileMode.Create, FileAccess.Write);
     int pos = 0;
     StringBuilder sb = new StringBuilder();
     while (pos < bytes.Length)
     {
         byte[] ridArray = bytes.Get(pos, 2).ByteArray;
         sb.Append(RID.Name(ridArray));
         sb.Append(" ");
         pos += 2;
         ushort len = BitConverter.ToUInt16(bytes.Get(pos, 2).ByteArray, 0);
         pos += 2;
         byte[] byteArray = bytes.Get(pos, len).ByteArray;
         foreach (byte b in byteArray)
         {
             sb.Append(GetByteHex(b));
             sb.Append(" ");
         }
         sb.Append(Environment.NewLine);
         pos += byteArray.Length;
     }
     string records = sb.ToString();
     fs.Write(Encoding.ASCII.GetBytes(records), 0, records.Length);
     fs.Flush();
     fs.Close();
 }
Beispiel #11
0
        public void Test_Get()
        {
            Bytes bytes = new Bytes();

            Bytes newBytes = new Bytes();
            newBytes.Append(new byte[] { sixteenBytes[0], sixteenBytes[1] });
            newBytes.Append(new byte[] { sixteenBytes[2], sixteenBytes[3] });
            bytes.Append(newBytes);

            bytes.Append(new Bytes(new byte[] { sixteenBytes[4], sixteenBytes[5] }));

            newBytes = new Bytes();
            newBytes.Append(new byte[] { sixteenBytes[6], sixteenBytes[7], sixteenBytes[8] });
            newBytes.Append(new byte[] { sixteenBytes[9] });
            bytes.Append(newBytes);

            newBytes = new Bytes();
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[10] }));
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[11] }));
            bytes.Append(newBytes);

            newBytes = new Bytes();
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[12] }));
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[13], sixteenBytes[14] }));
            newBytes.Append(new Bytes(new byte[] { sixteenBytes[15] }));
            bytes.Append(newBytes);

            AssertArraysAreEqual(bytes.ByteArray, sixteenBytes);

            for (int offset = 0; offset < 16; offset++)
            {
                for (int length = 0; length <= (16 - offset); length++)
                {
                    AssertArraysAreEqual(Bytes.MidByteArray(sixteenBytes, offset, length), bytes.Get(offset, length).ByteArray);
                }
            }
        }