/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.poifs.crypt; import java.io.IOException; import java.nio.charset.Charset; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.poifs.crypt.standard.EncryptionRecord; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSWriterEvent; import org.apache.poi.poifs.filesystem.POIFSWriterListener; import org.apache.poi.util.LittleEndianByteArrayOutputStream; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutput; import org.apache.poi.util.StringUtil; public class DataSpaceMapUtils { public static void addDefaultDataSpace(DirectoryEntry dir) throws IOException { DataSpaceMapEntry dsme = new DataSpaceMapEntry( new int[]{ 0 } , new String[]{ Decryptor.DEFAULT_POIFS_ENTRY } , "StrongEncryptionDataSpace" ); DataSpaceMap dsm = new DataSpaceMap(new DataSpaceMapEntry[]{dsme}); createEncryptionEntry(dir, "\u0006DataSpaces/DataSpaceMap", dsm); DataSpaceDefinition dsd = new DataSpaceDefinition(new String[]{ "StrongEncryptionTransform" }); createEncryptionEntry(dir, "\u0006DataSpaces/DataSpaceInfo/StrongEncryptionDataSpace", dsd); TransformInfoHeader tih = new TransformInfoHeader( 1 , "{FF9A3F03-56EF-4613-BDD5-5A41C1D07246}" , "Microsoft.Container.EncryptionTransform" , 1, 0, 1, 0, 1, 0 ); IRMDSTransformInfo irm = new IRMDSTransformInfo(tih, 0, null); createEncryptionEntry(dir, "\u0006DataSpaces/TransformInfo/StrongEncryptionTransform/\u0006Primary", irm); DataSpaceVersionInfo dsvi = new DataSpaceVersionInfo("Microsoft.Container.DataSpaces", 1, 0, 1, 0, 1, 0); createEncryptionEntry(dir, "\u0006DataSpaces/Version", dsvi); } public static DocumentEntry createEncryptionEntry(DirectoryEntry dir, String path, EncryptionRecord out) throws IOException { String parts[] = path.split("/"); for (int i=0; i 0) { for (int i=0; i<(4-scratchedBytes); i++) { is.readByte(); } } return new String(data, 0, data.length, Charset.forName("UTF-8")); } public static void writeUtf8LPP4(LittleEndianOutput os, String str) { if (str == null || "".equals(str)) { os.writeInt(str == null ? 0 : 4); os.writeInt(0); } else { byte buf[] = str.getBytes(Charset.forName("UTF-8")); os.writeInt(buf.length); os.write(buf); int scratchBytes = buf.length%4; if (scratchBytes > 0) { for (int i=0; i<(4-scratchBytes); i++) { os.writeByte(0); } } } } }