EncryptedPackageEnvelope( Stream envelopeStream, PublishLicense publishLicense, CryptoProvider cryptoProvider ) { if (envelopeStream == null) { throw new ArgumentNullException("envelopeStream"); } ThrowIfRMEncryptionInfoInvalid(publishLicense, cryptoProvider); _root = StorageRoot.CreateOnStream(envelopeStream, _defaultFileModeForCreate); // // CreateOnStream opens the stream for read access if it's readable, and for // read/write access if it's writable. We're going to need it to be writable, // so check that it is. // if (_root.OpenAccess != FileAccess.ReadWrite) { throw new NotSupportedException(SR.StreamNeedsReadWriteAccess); } InitializeRMForCreate(publishLicense, cryptoProvider); EmbedPackage(null); }
EncryptedPackageEnvelope( string envelopeFileName, Stream packageStream, PublishLicense publishLicense, CryptoProvider cryptoProvider ) { if (envelopeFileName == null) { throw new ArgumentNullException("envelopeFileName"); } if (packageStream == null) { throw new ArgumentNullException("packageStream"); } ThrowIfRMEncryptionInfoInvalid(publishLicense, cryptoProvider); _root = StorageRoot.Open( envelopeFileName, _defaultFileModeForCreate, _defaultFileAccess, _defaultFileShare ); InitializeRMForCreate(publishLicense, cryptoProvider); EmbedPackage(packageStream); }
IsEncryptedPackageEnvelope( string fileName ) { bool retval = false; if (fileName == null) { throw new ArgumentNullException("fileName"); } StorageRoot root = null; try { // // When StorageRoot.Open is called on a file that is not a compound file, // it throws an IOException whose inner exception is a COMException whose // error code is 0x80030050, STG_E_FILEALREADYEXISTS. Check for that case // and return false because that means that this is not an RM-protected file. // // Any other exception is a real error. For example, StorageRoot.Open // throws FileNotFoundException if path does not exist, and we let that // flow through. // root = StorageRoot.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // // It's a compound file. Does it contain an "EncryptedPackage" stream? // retval = ContainsEncryptedPackageStream(root); } catch (IOException ex) { COMException comException = ex.InnerException as COMException; if (comException != null && comException.ErrorCode == STG_E_FILEALREADYEXISTS) { return(false); } throw; // Any other kind of IOException is a real error. } finally { if (root != null) { root.Close(); } } return(retval); }
EncryptedPackageEnvelope( Stream envelopeStream ) { if (envelopeStream == null) { throw new ArgumentNullException("envelopeStream"); } _root = StorageRoot.CreateOnStream(envelopeStream, _defaultFileModeForOpen); InitForOpen(); }
IsEncryptedPackageEnvelope( Stream stream ) { if (stream == null) { throw new ArgumentNullException("stream"); } bool retval = false; StorageRoot root = null; try { // // When StorageRoot.CreateOnStream is called on a stream that is not // a storage object, it throws an IOException whose inner exception is // a COMException whose error code is 0x80030050, STG_E_FILEALREADYEXISTS. // Check for that case and return false because that means that this // stream is not an RM-protected file. // // Any other exception is a real error. // root = StorageRoot.CreateOnStream(stream, FileMode.Open); // // It's a compound file. Does it contain an "EncryptedPackage" stream? // retval = ContainsEncryptedPackageStream(root); } catch (IOException ex) { COMException comException = ex.InnerException as COMException; if (comException != null && comException.ErrorCode == STG_E_FILEALREADYEXISTS) { return(false); } throw; // Any other kind of IOException is a real error. } finally { if (root != null) { root.Close(); } } return(retval); }
StorageBasedPackageProperties( StorageRoot root ) { _pss = (IPropertySetStorage)root.GetRootIStorage(); // // Open the property sets with the same access with which the file itself // was opened. // _grfMode = SafeNativeCompoundFileConstants.STGM_DIRECT | SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE; SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess(root.OpenAccess, ref _grfMode); OpenPropertyStorage(ref FormatId.SummaryInformation, out _psSummInfo); OpenPropertyStorage(ref FormatId.DocumentSummaryInformation, out _psDocSummInfo); }
EncryptedPackageEnvelope( string envelopeFileName, FileAccess access, FileShare sharing ) { if (envelopeFileName == null) { throw new ArgumentNullException("envelopeFileName"); } _root = StorageRoot.Open( envelopeFileName, _defaultFileModeForOpen, access, sharing ); InitForOpen(); }
/// <summary> /// Creates a new instance relative to the root /// </summary> /// <param name="root">The root storage</param> /// <param name="streamPath">Path to stream under root storage</param> private StreamInfo(StorageRoot root, string streamPath) : this((StorageInfo)root, streamPath) { }
ContainsEncryptedPackageStream( StorageRoot root ) { return((new StreamInfo(root, PackageStreamName)).InternalExists()); }
/// <summary> /// Given a parent and a path under it, step through each of the path /// elements and create an intermediate StorageInfo at each step because /// a StorageInfo is only meaningful relative to its immediate parent - /// multi-step relations can't be represented. /// </summary> private void BuildStorageInfoRelativeToStorage( StorageInfo parent, string fileName ) { parentStorage = parent; core = parent.CoreForChildStorage( fileName ); rootStorage = parent.Root; }
/// <summary> /// Internally visible constructor /// </summary> /// <param name="containerInstance">The container instance we're associated with</param> internal DataSpaceManager( StorageRoot containerInstance ) { _associatedStorage = containerInstance; // Storage under which all data space information is stored. StorageInfo dataSpaceStorage = new StorageInfo( _associatedStorage, DataSpaceStorageName ); // Initialize internal data structures. // _dataSpaceMap = new SortedList(); _mapTableHeaderPreservation = new byte[0]; _dataSpaceDefinitions = new Hashtable(CU.StringCaseInsensitiveComparer); _transformDefinitions = new Hashtable(CU.StringCaseInsensitiveComparer); _transformedStreams = new ArrayList(); // Check to see if we have any data space information to read if (dataSpaceStorage.Exists) { // Read any existing data space mapping information from the container // ReadDataSpaceMap(); ReadDataSpaceDefinitions(); ReadTransformDefinitions(); } return; }
/// <summary> /// Creates a new instance relative to the root /// </summary> /// <param name="root">The root storage</param> /// <param name="streamPath">Path to stream under root storage</param> private StreamInfo( StorageRoot root, string streamPath ) : this((StorageInfo)root, streamPath) { }
ContainsEncryptedPackageStream( StorageRoot root ) { return ((new StreamInfo(root, PackageStreamName)).InternalExists()); }
/// <summary>Open a container given all the settings</summary> /// <param name="path">Path to container on local file system</param> /// <param name="mode">See System.IO.FileMode in .NET SDK</param> /// <param name="access">See System.IO.FileAccess in .NET SDK</param> /// <param name="share">See System.IO.FileShare in .NET SDK</param> /// <param name="sectorSize">Compound File sector size, must be 512 or 4096</param> /// <returns>StorageRoot instance representing the file</returns> internal static StorageRoot Open( string path, FileMode mode, FileAccess access, FileShare share, int sectorSize) { int grfMode = 0; int returnValue = 0; // Simple path validation ContainerUtilities.CheckStringAgainstNullAndEmpty(path, "Path"); Guid IID_IStorage = new Guid(0x0000000B, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); IStorage newRootStorage; //////////////////////////////////////////////////////////////////// // Generate STGM from FileMode switch (mode) { case FileMode.Append: throw new ArgumentException( SR.FileModeUnsupported); case FileMode.Create: grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE; break; case FileMode.CreateNew: { FileInfo existTest = new FileInfo(path); if (existTest.Exists) { throw new IOException( SR.FileAlreadyExists); } } goto case FileMode.Create; case FileMode.Open: break; case FileMode.OpenOrCreate: { FileInfo existTest = new FileInfo(path); if (existTest.Exists) { // File exists, use open code path goto case FileMode.Open; } else { // File does not exist, use create code path goto case FileMode.Create; } } case FileMode.Truncate: throw new ArgumentException( SR.FileModeUnsupported); default: throw new ArgumentException( SR.FileModeInvalid); } // Generate the access flags from the access parameter SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess(access, ref grfMode); // Generate STGM from FileShare // Note: the .NET SDK does not specify the proper behavior in reaction to // incompatible flags being sent in together. Should ArgumentException be // thrown? Or do some values "trump" others? if (0 != (share & FileShare.Inheritable)) { throw new ArgumentException( SR.FileShareUnsupported); } else if (share == FileShare.None) // FileShare.None is zero, using "&" to check causes unreachable code error { grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE; } else if (share == FileShare.Read) { grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_WRITE; } else if (share == FileShare.Write) { grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_READ; // Note that this makes little sense when we don't support combination of flags } else if (share == FileShare.ReadWrite) { grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_DENY_NONE; } else { throw new ArgumentException( SR.FileShareInvalid); } if (0 != (grfMode & SafeNativeCompoundFileConstants.STGM_CREATE)) { // STGM_CREATE set, call StgCreateStorageEx. returnValue = SafeNativeCompoundFileMethods.SafeStgCreateStorageEx( path, grfMode, stgFormatDocFile, 0, IntPtr.Zero, IntPtr.Zero, ref IID_IStorage, out newRootStorage); } else { // STGM_CREATE not set, call StgOpenStorageEx. returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageEx( path, grfMode, stgFormatDocFile, 0, IntPtr.Zero, IntPtr.Zero, ref IID_IStorage, out newRootStorage); } switch (returnValue) { case SafeNativeCompoundFileConstants.S_OK: return(StorageRoot.CreateOnIStorage( newRootStorage)); case SafeNativeCompoundFileConstants.STG_E_FILENOTFOUND: throw new FileNotFoundException( SR.ContainerNotFound); case SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG: throw new ArgumentException( SR.StorageFlagsUnsupported, new COMException( SR.CFAPIFailure, returnValue)); default: throw new IOException( SR.ContainerCanNotOpen, new COMException( SR.CFAPIFailure, returnValue)); } }
/// <summary> /// Create a container StorageRoot based on the given System.IO.Stream object /// </summary> /// <param name="baseStream">The new Stream upon which to build the new StorageRoot</param> /// <param name="mode">The mode (Open or Create) to use on the lock bytes</param> /// <returns>New StorageRoot object built on the given Stream</returns> internal static StorageRoot CreateOnStream(Stream baseStream, FileMode mode) { if (null == baseStream) { throw new ArgumentNullException("baseStream"); } IStorage storageOnStream; int returnValue; int openFlags = SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE; if (baseStream.CanRead) { if (baseStream.CanWrite) { openFlags |= SafeNativeCompoundFileConstants.STGM_READWRITE; } else { openFlags |= SafeNativeCompoundFileConstants.STGM_READ; if (FileMode.Create == mode) { throw new ArgumentException( SR.CanNotCreateContainerOnReadOnlyStream); } } } else { throw new ArgumentException( SR.CanNotCreateStorageRootOnNonReadableStream); } if (FileMode.Create == mode) { returnValue = SafeNativeCompoundFileMethods.SafeStgCreateDocfileOnStream( baseStream, openFlags | SafeNativeCompoundFileConstants.STGM_CREATE, out storageOnStream); } else if (FileMode.Open == mode) { returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageOnStream( baseStream, openFlags, out storageOnStream); } else { throw new ArgumentException( SR.CreateModeMustBeCreateOrOpen); } switch ((uint)returnValue) { case SafeNativeCompoundFileConstants.S_OK: return(StorageRoot.CreateOnIStorage( storageOnStream)); default: throw new IOException( SR.UnableToCreateOnStream, new COMException( SR.CFAPIFailure, returnValue)); } }