Example #1
0
        /// <summary>Read a CString from a bitstream</summary>
        /// <param name="s">Bitstream to read from</param>
        /// <param name="length">Optional length specification</param>
        /// <param name="actualCount">On return, the actual character byte count, or -1 if all bytes are valid</param>
        /// <param name="maxLength">Optional maximum length of this specific string</param>
        /// <returns>The character's bytes for the string we're reading</returns>
        byte[] ReadStrCString(IO.BitStream s, int length, out int actualCount, int maxLength)
        {
            byte[] bytes;

            actualCount = TypeExtensions.kNone;             // complete string case

            // the user was nice and saved us some CPU trying to feel around for the null
            // because we don't have a fixed length to speed things up
            if (!mStorage.IsFixedLength && length > 0)
            {
                bytes = s.ReadBytes(GetMaxCleanByteCount(length));
                s.ReadUInt32(mNullCharacterSize * Bits.kByteBitCount);
            }
            // F**K: figure out the length ourselves. Or maybe we're a fixed length CString...
            // in which case we'll ignore anything the user tried to tell us about the length
            else
            {
                using (var ms = new System.IO.MemoryStream(!mStorage.IsFixedLength ? 512 : mStorage.FixedLength))
                {
                    // The N-byte methods take care of reading past the
                    // null character, no need to do it in this case.
                    if (mNullCharacterSize == 1)
                    {
                        ReadCStringSingleByte(s, ms, maxLength);
                    }
                    else
                    {
                        ReadCStringMultiByte(s, ms, maxLength);
                    }

                    // We use ToArray instead of GetArray so all of [ms] can theoretically be disposed of
                    bytes = ms.ToArray();
                }
            }

            return(bytes);
        }