Exemple #1
0
        /// <summary>
        /// Reads strings from BIFF stream into SST array
        /// </summary>
        public void ReadStrings()
        {
            uint offset       = (uint)RecordContentOffset + 8;
            uint last         = (uint)RecordContentOffset + RecordSize;
            int  lastcontinue = 0;
            uint count        = UniqueCount;

            while (offset < last)
            {
                var  str     = CreateXlsString(Bytes, offset);
                uint prefix  = str.HeadSize;
                uint postfix = str.TailSize;
                uint len     = str.CharacterCount;
                uint size    = prefix + postfix + len + (str.IsMultiByte ? len : 0);
                if (offset + size > last)
                {
                    if (lastcontinue >= _continues.Count)
                    {
                        break;
                    }
                    uint contoffset = _continues[lastcontinue];

                    byte[] buff = new byte[size * 2];
                    Buffer.BlockCopy(Bytes, (int)offset, buff, 0, (int)(last - offset));

                    // If we're past the string data then we won't have a unicode string option flags.
                    if (offset + prefix + len + (str.IsMultiByte ? len : 0) <= last)
                    {
                        Buffer.BlockCopy(Bytes, (int)contoffset + 4, buff, (int)(last - offset), (int)(size - last + offset));
                        offset = contoffset + 4 + size - last + offset;
                    }
                    else
                    {
                        bool isMultiByte = (Buffer.GetByte(Bytes, (int)contoffset + 4) & 0x1) == 1;
                        if (!isMultiByte && str.IsMultiByte)
                        {
                            len -= (last - prefix - offset) / 2;
                            byte[] tempbytes = new byte[len * 2];
                            for (int i = 0; i < len; i++)
                            {
                                tempbytes[i * 2] = Bytes[contoffset + 5 + i];
                            }

                            Buffer.BlockCopy(tempbytes, 0, buff, (int)(last - offset), tempbytes.Length);
                            Buffer.BlockCopy(Bytes, (int)(contoffset + 5 + len), buff, (int)(last - offset + tempbytes.Length), (int)postfix);
                            offset = contoffset + 5 + len + postfix;
                        }
                        else if (isMultiByte && !str.IsMultiByte)
                        {
                            len -= last - offset - prefix;

                            int    templen   = (int)(last - offset - prefix);
                            byte[] tempbytes = new byte[templen * 2];
                            for (int i = 0; i < templen; i++)
                            {
                                tempbytes[i * 2] = Bytes[offset + prefix + i];
                            }

                            Buffer.BlockCopy(tempbytes, 0, buff, (int)prefix, tempbytes.Length);
                            int buffOffset = (int)(prefix + tempbytes.Length);

                            Buffer.BlockCopy(Bytes, (int)(contoffset + 5), buff, buffOffset, (int)(len + len));
                            Buffer.BlockCopy(Bytes, (int)(contoffset + 5 + len + len), buff, (int)(buffOffset + len + len), (int)postfix);
                            buff[2] = (byte)((XlsFormattedUnicodeString.FormattedUnicodeStringFlags)buff[2] | XlsFormattedUnicodeString.FormattedUnicodeStringFlags.MultiByte);
                            offset  = contoffset + 5 + len + len + postfix;
                        }
                        else
                        {
                            Buffer.BlockCopy(Bytes, (int)contoffset + 5, buff, (int)(last - offset), (int)(size - last + offset));
                            offset = contoffset + 5 + size - last + offset;
                        }
                    }

                    last = contoffset + 4 + BitConverter.ToUInt16(Bytes, (int)contoffset + 2);
                    lastcontinue++;

                    str = new XlsFormattedUnicodeString(buff, 0);
                }
                else
                {
                    offset += size;
                    if (offset == last)
                    {
                        if (lastcontinue < _continues.Count)
                        {
                            uint contoffset = _continues[lastcontinue];
                            offset = contoffset + 4;
                            last   = offset + BitConverter.ToUInt16(Bytes, (int)contoffset + 2);
                            lastcontinue++;
                        }
                        else
                        {
                            count = 1;
                        }
                    }
                }

                _strings.Add(str);
                count--;
                if (count == 0)
                {
                    break;
                }
            }
        }
 internal XlsBiffFormatString(byte[] bytes, uint offset, ExcelBinaryReader reader)
     : base(bytes, offset, reader)
 {
     unicodeString = new XlsFormattedUnicodeString(bytes, offset + 6);
     //unicodeString = new XlsFormattedUnicodeString(bytes, offset + 6, reader.Encoding);
 }
Exemple #3
0
        /// <summary>
        /// Reads strings from BIFF stream into SST array
        /// </summary>
        public void ReadStrings()
        {
            uint offset       = (uint)m_readoffset + 8;
            uint last         = (uint)m_readoffset + RecordSize;
            int  lastcontinue = 0;
            uint count        = UniqueCount;

            while (offset < last)
            {
                //var str = new XlsFormattedUnicodeString(m_bytes, offset, reader.Encoding);
                var  str     = XlsStringFactory.CreateXlsString(m_bytes, offset, reader);
                uint prefix  = str.HeadSize;
                uint postfix = str.TailSize;
                uint len     = str.CharacterCount;
                uint size    = prefix + postfix + len + ((str.IsMultiByte) ? len : 0);
                if (offset + size > last)
                {
                    if (lastcontinue >= continues.Count)
                    {
                        break;
                    }
                    uint   contoffset = continues[lastcontinue];
                    byte   encoding   = Buffer.GetByte(m_bytes, (int)contoffset + 4);
                    byte[] buff       = new byte[size * 2];
                    Buffer.BlockCopy(m_bytes, (int)offset, buff, 0, (int)(last - offset));
                    if (encoding == 0 && str.IsMultiByte)
                    {
                        len -= (last - prefix - offset) / 2;
                        string temp = Encoding.Unicode.GetString(m_bytes,
                                                                 (int)contoffset + 5,
                                                                 (int)len);
                        byte[] tempbytes = Encoding.Unicode.GetBytes(temp);
                        Buffer.BlockCopy(tempbytes, 0, buff, (int)(last - offset), tempbytes.Length);
                        Buffer.BlockCopy(m_bytes, (int)(contoffset + 5 + len), buff, (int)(last - offset + len + len), (int)postfix);
                        offset = contoffset + 5 + len + postfix;
                    }
                    else if (encoding == 1 && str.IsMultiByte == false)
                    {
                        len -= (last - offset - prefix);
                        string temp = Encoding.Unicode.GetString(m_bytes,
                                                                 (int)contoffset + 5,
                                                                 (int)(len + len));
                        byte[] tempbytes = Encoding.Unicode.GetBytes(temp);
                        Buffer.BlockCopy(tempbytes, 0, buff, (int)(last - offset), tempbytes.Length);
                        Buffer.BlockCopy(m_bytes, (int)(contoffset + 5 + len + len), buff, (int)(last - offset + len), (int)postfix);
                        offset = contoffset + 5 + len + len + postfix;
                    }
                    else
                    {
                        Buffer.BlockCopy(m_bytes, (int)contoffset + 5, buff, (int)(last - offset), (int)(size - last + offset));
                        offset = contoffset + 5 + size - last + offset;
                    }
                    last = contoffset + 4 + BitConverter.ToUInt16(m_bytes, (int)contoffset + 2);
                    lastcontinue++;

                    //str = new XlsFormattedUnicodeString(buff, 0, reader.Encoding);
                    str = new XlsFormattedUnicodeString(buff, 0);
                }
                else
                {
                    offset += size;
                    if (offset == last)
                    {
                        if (lastcontinue < continues.Count)
                        {
                            uint contoffset = continues[lastcontinue];
                            offset = contoffset + 4;
                            last   = offset + BitConverter.ToUInt16(m_bytes, (int)contoffset + 2);
                            lastcontinue++;
                        }
                        else
                        {
                            count = 1;
                        }
                    }
                }
                m_strings.Add(str.Value);
                count--;
                if (count == 0)
                {
                    break;
                }
            }
        }