예제 #1
0
        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        /// <summary>
        /// Ensure that the version is persisted
        /// </summary>
        /// <remarks>Leaves stream at position just after the FormatVersion.
        /// Destructive.  This is called automatically from WriteAttempt but callers
        /// can call directly if they have changed the stream contents to a format
        /// that is no longer compatible with the persisted FormatVersion.  If
        /// this is not called directly, and a FormatVersion was found in the file
        /// then only the Updater field is modified.
        /// </remarks>
        private void PersistVersion(FormatVersion version)
        {
            if (!BaseStream.CanWrite)
            {
                throw new NotSupportedException(SR.Get(SRID.WriteNotSupported));
            }

            // normalize and save
            long tempPos = checked (BaseStream.Position - _dataOffset);

            BaseStream.Seek(0, SeekOrigin.Begin);

            // update _dataOffset
            long offset = version.SaveToStream(BaseStream);

            _fileVersion = version;     // we know what it is - no need to deserialize

            // existing value - ensure we didn't change sizes as this could lead to
            // data corruption
            if ((_dataOffset != 0) && (offset != _dataOffset))
            {
                throw new FileFormatException(SR.Get(SRID.VersionUpdateFailure));
            }

            // at this point we know the offset
            _dataOffset = offset;

            // restore and shift
            checked { BaseStream.Position = tempPos + _dataOffset; }
        }
예제 #2
0
        /// <summary>
        /// Eaual comparison operator
        /// </summary>
        /// <param name="obj">Object to compare</param>
        /// <returns>ture if the object is equal to this instance</returns>
        public override bool Equals(Object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj.GetType() != typeof(FormatVersion))
            {
                return(false);
            }

            FormatVersion v = (FormatVersion)obj;

            //PRESHARP:Parameter to this public method must be validated:  A null-dereference can occur here.
            //    Parameter 'v' to this public method must be validated:  A null-dereference can occur here.
            //This is a false positive as the checks above can gurantee no null dereference will occur
#pragma warning disable 6506
            if (String.CompareOrdinal(_featureIdentifier.ToUpperInvariant(), v.FeatureIdentifier.ToUpperInvariant()) != 0 ||
                _reader != v.ReaderVersion ||
                _writer != v.WriterVersion ||
                _updater != v.UpdaterVersion)
            {
                return(false);
            }
#pragma warning restore 6506

            return(true);
        }
예제 #3
0
        /// <summary>
        /// Load and compare feature identifier
        /// </summary>
        /// <remarks>There is no need for this method to maintain any previous Seek pointer.
        /// This method only modifies the stream position when called for the first time with a non-empty
        /// stream.  It is always called from Seek() and set_Position, which subsequently modify the stream
        /// pointer as appropriate after the call.</remarks>
        private void EnsureParsed()
        {
            // empty stream cannot have a version in it
            if ((_fileVersion == null) && (BaseStream.Length > 0))
            {
                Debug.Assert(_dataOffset == 0);

                // if no version was found and we cannot read from it, then the format is invalid
                if (!BaseStream.CanRead)
                {
                    throw new NotSupportedException(SR.Get(SRID.ReadNotSupported));
                }

                //
                // The physical stream begins with a header that identifies the transform to
                // which the stream belongs. The "logical" stream object handed to us by the
                // compound file begins -after- this stream header, so when we seek to the
                // "beginning" of this stream, we are actually seeking to the location after
                // the stream header, where the instance data starts.
                //
                BaseStream.Seek(0, SeekOrigin.Begin);

                //
                // The instance data starts with format version information for this transform.
                //
                _fileVersion = FormatVersion.LoadFromStream(BaseStream);

                //
                // Ensure that the feature name is as expected.
                //
                // NOTE: We preserve case, but do case-insensitive comparison.
                if (String.CompareOrdinal(
                        _fileVersion.FeatureIdentifier.ToUpper(CultureInfo.InvariantCulture),
                        _codeVersion.FeatureIdentifier.ToUpper(CultureInfo.InvariantCulture)) != 0)
                {
                    throw new FileFormatException(
                              SR.Get(
                                  SRID.InvalidTransformFeatureName,
                                  _fileVersion.FeatureIdentifier,
                                  _codeVersion.FeatureIdentifier
                                  )
                              );
                }

                _dataOffset = BaseStream.Position;
            }
        }
예제 #4
0
        //------------------------------------------------------
        //
        //  Public Events
        //
        //------------------------------------------------------
        // None
        //------------------------------------------------------
        //
        //  Internal Constructors
        //
        //------------------------------------------------------
        // None       
        //------------------------------------------------------
        //
        //  Internal Properties
        //
        //------------------------------------------------------
        // None       
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
#if !PBTCOMPILER
        /// <summary>
        /// Constructor for FormatVersion with information read from the given BinaryReader
        /// </summary>
        /// <param name="reader">BinaryReader where version information is read from</param>
        /// <param name="bytesRead">number of bytes read including padding</param>
        /// <returns>FormatVersion object</returns>
        /// <remarks>
        /// This operation will change the stream pointer. This function is preferred over the 
        /// LoadFromStream as it doesn't leave around Undisposed BinaryReader, which 
        /// LoadFromStream will
        /// </remarks>
        private static FormatVersion LoadFromBinaryReader(BinaryReader reader, out Int32 bytesRead)
        {
            checked
            {
                if (reader == null)
                {
                    throw new ArgumentNullException("reader");
                }

                FormatVersion ver = new FormatVersion();

                bytesRead = 0;            // Initialize the number of bytes read

                // **************
                //  feature ID
                // **************

                Int32 strBytes;

                ver._featureIdentifier = ContainerUtilities.ReadByteLengthPrefixedDWordPaddedUnicodeString(reader, out strBytes);
                bytesRead += strBytes;

                Int16 major;
                Int16 minor;

                // ****************
                //  Reader Version
                // ****************

                major = reader.ReadInt16();   // Major number
                bytesRead += ContainerUtilities.Int16Size;
                minor = reader.ReadInt16();   // Minor number
                bytesRead += ContainerUtilities.Int16Size;

                ver.ReaderVersion = new VersionPair(major, minor);

                // *****************
                //  Updater Version
                // *****************

                major = reader.ReadInt16();   // Major number
                bytesRead += ContainerUtilities.Int16Size;
                minor = reader.ReadInt16();   // Minor number
                bytesRead += ContainerUtilities.Int16Size;

                ver.UpdaterVersion = new VersionPair(major, minor);

                // ****************
                //  Writer Version
                // ****************

                major = reader.ReadInt16();   // Major number
                bytesRead += ContainerUtilities.Int16Size;
                minor = reader.ReadInt16();   // Minor number
                bytesRead += ContainerUtilities.Int16Size;

                ver.WriterVersion = new VersionPair(major, minor);

                return ver;
            }
        }
예제 #5
0
        //------------------------------------------------------
        //
        //  Public Events
        //
        //------------------------------------------------------
        // None
        //------------------------------------------------------
        //
        //  Internal Constructors
        //
        //------------------------------------------------------
        // None
        //------------------------------------------------------
        //
        //  Internal Properties
        //
        //------------------------------------------------------
        // None
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
#if !PBTCOMPILER
        /// <summary>
        /// Constructor for FormatVersion with information read from the given BinaryReader
        /// </summary>
        /// <param name="reader">BinaryReader where version information is read from</param>
        /// <param name="bytesRead">number of bytes read including padding</param>
        /// <returns>FormatVersion object</returns>
        /// <remarks>
        /// This operation will change the stream pointer. This function is preferred over the
        /// LoadFromStream as it doesn't leave around Undisposed BinaryReader, which
        /// LoadFromStream will
        /// </remarks>
        private static FormatVersion LoadFromBinaryReader(BinaryReader reader, out Int32 bytesRead)
        {
            checked
            {
                if (reader == null)
                {
                    throw new ArgumentNullException("reader");
                }

                FormatVersion ver = new FormatVersion();

                bytesRead = 0;            // Initialize the number of bytes read

                // **************
                //  feature ID
                // **************

                Int32 strBytes;

                ver._featureIdentifier = ContainerUtilities.ReadByteLengthPrefixedDWordPaddedUnicodeString(reader, out strBytes);
                bytesRead += strBytes;

                Int16 major;
                Int16 minor;

                // ****************
                //  Reader Version
                // ****************

                major      = reader.ReadInt16(); // Major number
                bytesRead += ContainerUtilities.Int16Size;
                minor      = reader.ReadInt16(); // Minor number
                bytesRead += ContainerUtilities.Int16Size;

                ver.ReaderVersion = new VersionPair(major, minor);

                // *****************
                //  Updater Version
                // *****************

                major      = reader.ReadInt16(); // Major number
                bytesRead += ContainerUtilities.Int16Size;
                minor      = reader.ReadInt16(); // Minor number
                bytesRead += ContainerUtilities.Int16Size;

                ver.UpdaterVersion = new VersionPair(major, minor);

                // ****************
                //  Writer Version
                // ****************

                major      = reader.ReadInt16(); // Major number
                bytesRead += ContainerUtilities.Int16Size;
                minor      = reader.ReadInt16(); // Minor number
                bytesRead += ContainerUtilities.Int16Size;

                ver.WriterVersion = new VersionPair(major, minor);

                return(ver);
            }
        }
예제 #6
0
 public BamlVersionHeader()
 {
     _bamlVersion = new FormatVersion("MSBAML", BamlWriterVersion);
 }
 /// <summary>
 /// If the DataSpace version information was not present in the file, we initialize it with the
 /// values for the current DataSpace software.
 /// If the information was present we should have already read it as a part of the ReadDataSpaceMap.
 /// </summary>
 private void EnsureDataSpaceVersionInformation()
 {
     if (_fileFormatVersion == null)
     {
         _fileFormatVersion = new FormatVersion(
                                     DataSpaceVersionIdentifier,
                                     DataSpaceCurrentWriterVersion,
                                     DataSpaceCurrentReaderVersion,
                                     DataSpaceCurrentUpdaterVersion
                                  );
     }
 }
 /// <summary>
 /// Read the version information that specifies the minimum versions of the 
 /// DataSpaceManager software that can read, write, or update the data space 
 /// information in this file.
 /// </summary>
 /// <param name="dataSpaceStorage"></param>
 /// <exception cref="FileFormatException">
 /// If the format version information in the stream is corrupt.
 /// </exception>
 private void ReadDataSpaceVersionInformation(StorageInfo dataSpaceStorage)
 {      
     if (_fileFormatVersion == null)
     {
         if (dataSpaceStorage.StreamExists( DataSpaceVersionName ))
         {
             StreamInfo versionStreamInfo = dataSpaceStorage.GetStreamInfo( DataSpaceVersionName );
             using (Stream versionStream = versionStreamInfo.GetStream(FileMode.Open))
             {
                 _fileFormatVersion = FormatVersion.LoadFromStream(versionStream);
                                     
                 // Transform Identifier: we preserve casing, but do case-insensitive comparison
                 //Case-insensitive comparison. As per recommendations, we convert both strings
                 //to Upper case and then compare with StringComparison.Ordinal
                 if (!((IEqualityComparer) CU.StringCaseInsensitiveComparer).Equals(_fileFormatVersion.FeatureIdentifier,
                                    DataSpaceVersionIdentifier))
                 {
                     throw new FileFormatException(
                                         SR.Get(SRID.InvalidTransformFeatureName,
                                         _fileFormatVersion.FeatureIdentifier,
                                         DataSpaceVersionIdentifier));                       
                 }
                 // If we ever write this version number out again, we will want to record
                 // the fact that it was done by the current version of the Dataspace software.
                 _fileFormatVersion.WriterVersion = DataSpaceCurrentWriterVersion;
             }
         }
     }        
 }
        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        /// <summary>
        /// Ensure that the version is persisted
        /// </summary>
        /// <remarks>Leaves stream at position just after the FormatVersion.
        /// Destructive.  This is called automatically from WriteAttempt but callers
        /// can call directly if they have changed the stream contents to a format
        /// that is no longer compatible with the persisted FormatVersion.  If
        /// this is not called directly, and a FormatVersion was found in the file
        /// then only the Updater field is modified.
        /// </remarks>
        private void PersistVersion(FormatVersion version)
        {
            if (!BaseStream.CanWrite)
                throw new NotSupportedException(SR.Get(SRID.WriteNotSupported));

            // normalize and save
            long tempPos = checked(BaseStream.Position - _dataOffset);
            BaseStream.Seek(0, SeekOrigin.Begin);

            // update _dataOffset
            long offset = version.SaveToStream(BaseStream);
            _fileVersion = version;     // we know what it is - no need to deserialize

            // existing value - ensure we didn't change sizes as this could lead to
            // data corruption
            if ((_dataOffset != 0) && (offset != _dataOffset))
                throw new FileFormatException(SR.Get(SRID.VersionUpdateFailure));

            // at this point we know the offset
            _dataOffset = offset;

            // restore and shift
            checked { BaseStream.Position = tempPos + _dataOffset; }
        }
 /// <summary>
 /// Constructor to use for the "versioned stream" - the one that actually houses the 
 /// persisted FormatVersion.
 /// </summary>
 /// <param name="baseStream"></param>
 /// <param name="codeVersion"></param>
 internal VersionedStreamOwner(Stream baseStream, FormatVersion codeVersion)
     : base(baseStream)
 {
     _codeVersion = codeVersion;
 }
예제 #11
0
 /// <summary>
 /// Constructor to use for the "versioned stream" - the one that actually houses the
 /// persisted FormatVersion.
 /// </summary>
 /// <param name="baseStream"></param>
 /// <param name="codeVersion"></param>
 internal VersionedStreamOwner(Stream baseStream, FormatVersion codeVersion)
     : base(baseStream)
 {
     _codeVersion = codeVersion;
 }