InitForOpen() { StreamInfo siPackage = new StreamInfo(_root, PackageStreamName); if (!siPackage.InternalExists()) { throw new FileFormatException(SR.PackageNotFound); } //If the StreamInfo exists we go on to check if correct transform has been //applied to the Stream DataSpaceManager dsm = _root.GetDataSpaceManager(); List <IDataTransform> transforms = dsm.GetTransformsForStreamInfo(siPackage); RightsManagementEncryptionTransform rmet = null; foreach (IDataTransform dataTransform in transforms) { string id = dataTransform.TransformIdentifier as string; if (id != null && String.CompareOrdinal(id.ToUpperInvariant(), RightsManagementEncryptionTransform.ClassTransformIdentifier.ToUpperInvariant()) == 0) { // Do not allow more than one RM Transform if (rmet != null) { throw new FileFormatException(SR.MultipleRightsManagementEncryptionTransformFound); } rmet = dataTransform as RightsManagementEncryptionTransform; } } if (rmet == null) { throw new FileFormatException(SR.RightsManagementEncryptionTransformNotFound); } // // There is no reason to further push initialization of the Rights Management // data (parsing publish / use license). It will add unnecessary costs to the // scenarios where RM license are not relevant, for example indexing and // working with document properties // // // Make the rights management information stored in the compound file // available to the application through the RightsManagementInformation // property. // _rmi = new RightsManagementInformation(rmet); }
/// <summary> /// Retrieve the package stream contained in the compound file. /// </summary> private void EnsurePackageStream() { if (_packageStream == null) { StreamInfo siPackage = new StreamInfo(_root, PackageStreamName); if (siPackage.InternalExists()) { // // Open the existing package stream with the same level of access // that the compound file is open. // _packageStream = siPackage.GetStream(FileMode.Open, this.FileOpenAccess); } else { //Error. This package is created in InitForCreate while creating EncryptedPackageEnvelope. //If it does not exist, throw an error. throw new FileFormatException(SR.PackageNotFound); } } }
EmbedPackage(Stream packageStream) { StreamInfo siPackage = new StreamInfo(_root, PackageStreamName); Debug.Assert(!siPackage.InternalExists()); // // Create a stream to hold the document content. Create it in the // dataspace containing the RightsManagementEncryptionTransform. This // will cause the compound file code (specifically, the DataSpaceManager) // to create a RightsManagementEncryptionTransform object, and then // to trigger the TransformInitializationEvent, which will allow us to // retrieve the transform object. // _packageStream = siPackage.Create( FileMode.Create, _root.OpenAccess, _dataSpaceName ); if (packageStream != null) { //copy the stream PackagingUtilities.CopyStream(packageStream, _packageStream, Int64.MaxValue, /*bytes to copy*/ 4096 /*buffer size */); _package = Package.Open(_packageStream, FileMode.Open, this.FileOpenAccess); } else { // // Create the package on the package stream // _package = Package.Open(_packageStream, FileMode.Create, FileAccess.ReadWrite); _package.Flush(); _packageStream.Flush(); } }
/// <summary> /// Check if the stream exists. /// </summary> /// <param name="name">Name of stream</param> /// <returns>True if exists, False if not</returns> public bool StreamExists(string name) { CheckDisposedStatus(); bool streamExists = false; StreamInfo streamInfo = new StreamInfo(this, name); streamExists = streamInfo.InternalExists(); return streamExists; }
/// <summary> /// Deleted the stream with the passed name. /// </summary> /// <param name="name">Name of stream</param> public void DeleteStream(string name) { CheckDisposedStatus(); //check the arguments if( null == name ) throw new ArgumentNullException("name"); StreamInfo streamInfo = new StreamInfo(this, name); if (streamInfo.InternalExists()) { streamInfo.Delete(); } }
/// <summary> /// Returns the streaminfo by the passed name. /// </summary> /// <param name="name">Name of stream</param> /// <returns>Reference to the stream</returns> public StreamInfo GetStreamInfo(string name) { CheckDisposedStatus(); //check the arguments if( null == name ) throw new ArgumentNullException("name"); StreamInfo streamInfo = new StreamInfo(this, name); if (streamInfo.InternalExists()) { return streamInfo; } else { throw new IOException(SR.Get(SRID.StreamNotExist)); } }
/***********************************************************************/ // public Methods /// <summary> /// Creates "this" stream /// </summary> /// <param name="name">Name of stream</param> /// <param name="compressionOption">CompressionOptiont</param> /// <param name="encryptionOption">EncryptionOption</param> /// <returns>Reference to new stream</returns> public StreamInfo CreateStream( string name, CompressionOption compressionOption, EncryptionOption encryptionOption ) { CheckDisposedStatus(); //check the arguments if( null == name ) throw new ArgumentNullException("name"); // Stream names: we preserve casing, but do case-insensitive comparison (Native CompoundFile API behavior) if (((IEqualityComparer) CU.StringCaseInsensitiveComparer).Equals(name, EncryptedPackageEnvelope.PackageStreamName)) throw new ArgumentException(SR.Get(SRID.StreamNameNotValid,name)); //create a new streaminfo object StreamInfo streamInfo = new StreamInfo(this, name, compressionOption, encryptionOption); if (streamInfo.InternalExists()) { throw new IOException(SR.Get(SRID.StreamAlreadyExist)); } //Define the compression and encryption options in the dataspacemanager DataSpaceManager manager = Root.GetDataSpaceManager(); string dataSpaceLabel = null; if (manager != null) { //case : Compression option is set. Stream need to be compressed. Define compression transform. //At this time, we only treat CompressionOption - Normal and None. The rest are treated as Normal if (compressionOption != CompressionOption.NotCompressed) { //If it is not defined already, define it. if (!manager.TransformLabelIsDefined(sc_compressionTransformName)) manager.DefineTransform(CompressionTransform.ClassTransformIdentifier, sc_compressionTransformName); } //case : Encryption option is set. Stream need to be encrypted. Define encryption transform. if (encryptionOption == EncryptionOption.RightsManagement) { //If it not defined already, define it. if (!manager.TransformLabelIsDefined(EncryptedPackageEnvelope.EncryptionTransformName)) { //We really cannot define RM transform completely here because the transform initialization cannot be done here without publishlicense and cryptoprovider. //However it will always be defined because this method is accessed only through an EncryptedPackageEnvelope and RM transform is always defined in EncryptedPackageEnvelope.Create() throw new SystemException(SR.Get(SRID.RightsManagementEncryptionTransformNotFound)); } } //Now find the dataspace label that we need to define these transforms in. //CASE: When both CompressionOption and EncryptionOption are set if ( (compressionOption != CompressionOption.NotCompressed) && (encryptionOption == EncryptionOption.RightsManagement) ) { dataSpaceLabel = sc_dataspaceLabelRMEncryptionNormalCompression; if (!manager.DataSpaceIsDefined(dataSpaceLabel)) { string[] transformStack = new string[2]; //compress the data first. then encrypt it. This ordering will cause the content to be compressed, then encrypted, then written to the stream. transformStack[0] = EncryptedPackageEnvelope.EncryptionTransformName; transformStack[1] = sc_compressionTransformName; manager.DefineDataSpace(transformStack, dataSpaceLabel); } } //CASE : when CompressionOption alone is set else if ( (compressionOption != CompressionOption.NotCompressed) && (encryptionOption == EncryptionOption.None) ) { dataSpaceLabel = sc_dataspaceLabelNoEncryptionNormalCompression; if (!manager.DataSpaceIsDefined(dataSpaceLabel)) { string[] transformStack = new string[1]; transformStack[0] = sc_compressionTransformName; manager.DefineDataSpace(transformStack, dataSpaceLabel); } } //CASE : when EncryptionOption alone is set else if (encryptionOption == EncryptionOption.RightsManagement) { dataSpaceLabel = EncryptedPackageEnvelope.DataspaceLabelRMEncryptionNoCompression; if (!manager.DataSpaceIsDefined(dataSpaceLabel)) { string[] transformStack = new string[1]; transformStack[0] = EncryptedPackageEnvelope.EncryptionTransformName; manager.DefineDataSpace(transformStack, dataSpaceLabel); } } //All the other cases are not handled at this point. } //create the underlying stream if (null == dataSpaceLabel) streamInfo.Create(); //create the stream with default parameters else streamInfo.Create(dataSpaceLabel); //create the stream in the defined dataspace return streamInfo; }
/// <summary> /// Retrieve the package stream contained in the compound file. /// </summary> private void EnsurePackageStream() { if (_packageStream == null) { StreamInfo siPackage = new StreamInfo(_root, PackageStreamName); if (siPackage.InternalExists()) { // // Open the existing package stream with the same level of access // that the compound file is open. // _packageStream = siPackage.GetStream(FileMode.Open, this.FileOpenAccess); } else { //Error. This package is created in InitForCreate while creating EncryptedPackageEnvelope. //If it does not exist, throw an error. throw new FileFormatException(SR.Get(SRID.PackageNotFound)); } } }
InitForOpen() { StreamInfo siPackage = new StreamInfo(_root, PackageStreamName); if (!siPackage.InternalExists()) { throw new FileFormatException(SR.Get(SRID.PackageNotFound)); } //If the StreamInfo exists we go on to check if correct transform has been //applied to the Stream DataSpaceManager dsm = _root.GetDataSpaceManager(); List<IDataTransform> transforms = dsm.GetTransformsForStreamInfo(siPackage); RightsManagementEncryptionTransform rmet = null; foreach (IDataTransform dataTransform in transforms) { string id = dataTransform.TransformIdentifier as string; if (id != null && String.CompareOrdinal(id.ToUpperInvariant(), RightsManagementEncryptionTransform.ClassTransformIdentifier.ToUpperInvariant()) == 0) { // Do not allow more than one RM Transform if (rmet != null) { throw new FileFormatException(SR.Get(SRID.MultipleRightsManagementEncryptionTransformFound)); } rmet = dataTransform as RightsManagementEncryptionTransform; } } if (rmet == null) { throw new FileFormatException(SR.Get(SRID.RightsManagementEncryptionTransformNotFound)); } // // There is no reason to further push initialization of the Rights Management // data (parsing publish / use license). It will add unnecessary costs to the // scenarios where RM license are not relevant, for example indexing and // working with document properties // // // Make the rights management information stored in the compound file // available to the application through the RightsManagementInformation // property. // _rmi = new RightsManagementInformation(rmet); }
EmbedPackage(Stream packageStream) { StreamInfo siPackage = new StreamInfo(_root, PackageStreamName); Debug.Assert(!siPackage.InternalExists()); // // Create a stream to hold the document content. Create it in the // dataspace containing the RightsManagementEncryptionTransform. This // will cause the compound file code (specifically, the DataSpaceManager) // to create a RightsManagementEncryptionTransform object, and then // to trigger the TransformInitializationEvent, which will allow us to // retrieve the transform object. // _packageStream = siPackage.Create( FileMode.Create, _root.OpenAccess, _dataSpaceName ); if (packageStream != null) { //copy the stream PackagingUtilities.CopyStream(packageStream, _packageStream, Int64.MaxValue, /*bytes to copy*/ 4096 /*buffer size */); _package = Package.Open(_packageStream, FileMode.Open, this.FileOpenAccess); } else { // // Create the package on the package stream // _package = Package.Open(_packageStream, FileMode.Create, FileAccess.ReadWrite); _package.Flush(); _packageStream.Flush(); } }