001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021: import org.apache.poi.util.StringUtil;
022:
023: import java.io.IOException;
024: import java.io.OutputStream;
025:
026: /**
027: * A Document Encryption Atom (type 12052). Holds information
028: * on the Encryption of a Document
029: *
030: * @author Nick Burch
031: */
032:
033: public class DocumentEncryptionAtom extends RecordAtom {
034: private byte[] _header;
035: private static long _type = 12052l;
036:
037: private byte[] data;
038: private String encryptionProviderName;
039:
040: /**
041: * For the Document Encryption Atom
042: */
043: protected DocumentEncryptionAtom(byte[] source, int start, int len) {
044: // Get the header
045: _header = new byte[8];
046: System.arraycopy(source, start, _header, 0, 8);
047:
048: // Grab everything else, for now
049: data = new byte[len - 8];
050: System.arraycopy(source, start + 8, data, 0, len - 8);
051:
052: // Grab the provider, from byte 8+44 onwards
053: // It's a null terminated Little Endian String
054: int endPos = -1;
055: int pos = start + 8 + 44;
056: while (pos < (start + len) && endPos < 0) {
057: if (source[pos] == 0 && source[pos + 1] == 0) {
058: // Hit the end
059: endPos = pos;
060: }
061: pos += 2;
062: }
063: pos = start + 8 + 44;
064: int stringLen = (endPos - pos) / 2;
065: encryptionProviderName = StringUtil.getFromUnicodeLE(source,
066: pos, stringLen);
067: }
068:
069: /**
070: * Return the length of the encryption key, in bits
071: */
072: public int getKeyLength() {
073: return data[28];
074: }
075:
076: /**
077: * Return the name of the encryption provider used
078: */
079: public String getEncryptionProviderName() {
080: return encryptionProviderName;
081: }
082:
083: /**
084: * We are of type 12052
085: */
086: public long getRecordType() {
087: return _type;
088: }
089:
090: /**
091: * Write the contents of the record back, so it can be written
092: * to disk
093: */
094: public void writeOut(OutputStream out) throws IOException {
095: // Header
096: out.write(_header);
097:
098: // Data
099: out.write(data);
100: }
101: }
|