Java Code Examples for org.apache.poi.util.LittleEndian#putUInt()

The following examples show how to use org.apache.poi.util.LittleEndian#putUInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Property.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
     * Writes the property to an output stream.
     *
     * @param out The output stream to write to.
     * @param codepage The codepage to use for writing non-wide strings
     * @return the number of bytes written to the stream
     *
     * @exception IOException if an I/O error occurs
     * @exception WritingNotSupportedException if a variant type is to be
     * written that is not yet supported
     */
    public int write(final OutputStream out, final int codepage)
    throws IOException, WritingNotSupportedException {
        int length = 0;
        long variantType = getType();

        /* Ensure that wide strings are written if the codepage is Unicode. */
//        if (codepage == CodePageUtil.CP_UNICODE && variantType == Variant.VT_LPSTR) {
//            variantType = Variant.VT_LPWSTR;
//        }

        if (variantType == Variant.VT_LPSTR && codepage != CodePageUtil.CP_UTF16) {
            String csStr = CodePageUtil.codepageToEncoding(codepage > 0 ? codepage : Property.DEFAULT_CODEPAGE);
            if (!Charset.forName(csStr).newEncoder().canEncode((String)value)) {
                variantType = Variant.VT_LPWSTR;
            }
        }

        LittleEndian.putUInt(variantType, out);
        length += LittleEndianConsts.INT_SIZE;
        length += VariantSupport.write(out, variantType, getValue(), codepage);
        return length;
    }
 
Example 2
Source File: BinaryRC4Decryptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected static Cipher initCipherForBlock(Cipher cipher, int block,
    EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode)
throws GeneralSecurityException {
    EncryptionVerifier ver = encryptionInfo.getVerifier();
    HashAlgorithm hashAlgo = ver.getHashAlgorithm();
    byte blockKey[] = new byte[4];
    LittleEndian.putUInt(blockKey, 0, block);
    byte encKey[] = CryptoFunctions.generateKey(skey.getEncoded(), hashAlgo, blockKey, 16);
    SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm());
    if (cipher == null) {
        EncryptionHeader em = encryptionInfo.getHeader();
        cipher = CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), null, null, encryptMode);
    } else {
        cipher.init(encryptMode, key);
    }
    return cipher;
}
 
Example 3
Source File: CryptoAPIDecryptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected static Cipher initCipherForBlock(Cipher cipher, int block,
    EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode)
throws GeneralSecurityException {
    EncryptionVerifier ver = encryptionInfo.getVerifier();
    HashAlgorithm hashAlgo = ver.getHashAlgorithm();
    byte blockKey[] = new byte[4];
    LittleEndian.putUInt(blockKey, 0, block);
    MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);
    hashAlg.update(skey.getEncoded());
    byte encKey[] = hashAlg.digest(blockKey);
    EncryptionHeader header = encryptionInfo.getHeader();
    int keyBits = header.getKeySize();
    encKey = CryptoFunctions.getBlock0(encKey, keyBits / 8);
    if (keyBits == 40) {
        encKey = CryptoFunctions.getBlock0(encKey, 16);
    }
    SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm());
    if (cipher == null) {
        cipher = CryptoFunctions.getCipher(key, header.getCipherAlgorithm(), null, null, encryptMode);
    } else {
        cipher.init(encryptMode, key);
    }
    return cipher;
}
 
Example 4
Source File: TypeWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Writes an unsigned four-byte value to an output stream.</p>
 *
 * @param out The stream to write to.
 * @param n The value to write.
 * @return The number of bytes that have been written to the output stream.
 * @exception IOException if an I/O error occurs
 */
public static int writeUIntToStream( final OutputStream out, final long n )
        throws IOException
{
    long high = n & 0xFFFFFFFF00000000L;
    if ( high != 0 && high != 0xFFFFFFFF00000000L ) {
        throw new IllegalPropertySetDataException( "Value " + n
                + " cannot be represented by 4 bytes." );
    }
    LittleEndian.putUInt( n, out );
    return LittleEndianConsts.INT_SIZE;
}
 
Example 5
Source File: CodePageString.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
int write( OutputStream out ) throws IOException {
    LittleEndian.putUInt( _value.length, out );
    out.write( _value );
    return LittleEndianConsts.INT_SIZE + _value.length;
}
 
Example 6
Source File: Section.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes this section into an output stream.<p>
 *
 * Internally this is done by writing into three byte array output
 * streams: one for the properties, one for the property list and one for
 * the section as such. The two former are appended to the latter when they
 * have received all their data.
 *
 * @param out The stream to write into.
 *
 * @return The number of bytes written, i.e. the section's size.
 * @exception IOException if an I/O error occurs
 * @exception WritingNotSupportedException if HPSF does not yet support
 * writing a property's variant type.
 */
public int write(final OutputStream out) throws WritingNotSupportedException, IOException {
    /* Check whether we have already generated the bytes making out the
     * section. */
    if (sectionBytes.size() > 0) {
        sectionBytes.writeTo(out);
        return sectionBytes.size();
    }

    /* Writing the section's dictionary it tricky. If there is a dictionary
     * (property 0) the codepage property (property 1) must be set, too. */
    int codepage = getCodepage();
    if (codepage == -1) {
        String msg =
            "The codepage property is not set although a dictionary is present. "+
            "Defaulting to ISO-8859-1.";
        LOG.log(POILogger.WARN, msg);
        codepage = Property.DEFAULT_CODEPAGE;
    }

    /* The properties are written to this stream. */
    final ByteArrayOutputStream propertyStream = new ByteArrayOutputStream();

    /* The property list is established here. After each property that has
     * been written to "propertyStream", a property list entry is written to
     * "propertyListStream". */
    final ByteArrayOutputStream propertyListStream = new ByteArrayOutputStream();

    /* Maintain the current position in the list. */
    int position = 0;

    /* Increase the position variable by the size of the property list so
     * that it points behind the property list and to the beginning of the
     * properties themselves. */
    position += 2 * LittleEndianConsts.INT_SIZE + getPropertyCount() * 2 * LittleEndianConsts.INT_SIZE;

    /* Write the properties and the property list into their respective
     * streams: */
    for (Property p : properties.values()) {
        final long id = p.getID();

        /* Write the property list entry. */
        LittleEndian.putUInt(id, propertyListStream);
        LittleEndian.putUInt(position, propertyListStream);

        /* If the property ID is not equal 0 we write the property and all
         * is fine. However, if it equals 0 we have to write the section's
         * dictionary which has an implicit type only and an explicit
         * value. */
        if (id != 0) {
            /* Write the property and update the position to the next
             * property. */
            position += p.write(propertyStream, codepage);
        } else {
            if (codepage == -1) {
                throw new IllegalPropertySetDataException("Codepage (property 1) is undefined.");
            }
            position += writeDictionary(propertyStream, codepage);
        }
    }

    /* Write the section: */
    int streamLength = LittleEndianConsts.INT_SIZE * 2 + propertyListStream.size() + propertyStream.size();

    /* Write the section's length: */
    LittleEndian.putInt(streamLength, out);

    /* Write the section's number of properties: */
    LittleEndian.putInt(getPropertyCount(), out);

    /* Write the property list: */
    propertyListStream.writeTo(out);

    /* Write the properties: */
    propertyStream.writeTo(out);

    return streamLength;
}
 
Example 7
Source File: UnicodeString.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
int write( OutputStream out ) throws IOException {
    LittleEndian.putUInt( _value.length / 2, out );
    out.write( _value );
    return LittleEndianConsts.INT_SIZE + _value.length;
}
 
Example 8
Source File: CryptoAPIEncryptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Encrypt the Document-/SummaryInformation and other optionally streams.
 * Opposed to other crypto modes, cryptoapi is record based and can't be used
 * to stream-encrypt a whole file
 * 
 * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>
 */
public void setSummaryEntries(DirectoryNode dir, String encryptedStream, NPOIFSFileSystem entries)
throws IOException, GeneralSecurityException {
    CryptoAPIDocumentOutputStream bos = new CryptoAPIDocumentOutputStream(this); // NOSONAR
    byte buf[] = new byte[8];
    
    bos.write(buf, 0, 8); // skip header
    List<StreamDescriptorEntry> descList = new ArrayList<StreamDescriptorEntry>();

    int block = 0;
    for (Entry entry : entries.getRoot()) {
        if (entry.isDirectoryEntry()) {
            continue;
        }
        StreamDescriptorEntry descEntry = new StreamDescriptorEntry();
        descEntry.block = block;
        descEntry.streamOffset = bos.size();
        descEntry.streamName = entry.getName();
        descEntry.flags = StreamDescriptorEntry.flagStream.setValue(0, 1);
        descEntry.reserved2 = 0;
        
        bos.setBlock(block);
        DocumentInputStream dis = dir.createDocumentInputStream(entry);
        IOUtils.copy(dis, bos);
        dis.close();
        
        descEntry.streamSize = bos.size() - descEntry.streamOffset;
        descList.add(descEntry);
        
        block++;
    }
    
    int streamDescriptorArrayOffset = bos.size();
    
    bos.setBlock(0);
    LittleEndian.putUInt(buf, 0, descList.size());
    bos.write(buf, 0, 4);
    
    for (StreamDescriptorEntry sde : descList) {
        LittleEndian.putUInt(buf, 0, sde.streamOffset);
        bos.write(buf, 0, 4);
        LittleEndian.putUInt(buf, 0, sde.streamSize);
        bos.write(buf, 0, 4);
        LittleEndian.putUShort(buf, 0, sde.block);
        bos.write(buf, 0, 2);
        LittleEndian.putUByte(buf, 0, (short)sde.streamName.length());
        bos.write(buf, 0, 1);
        LittleEndian.putUByte(buf, 0, (short)sde.flags);
        bos.write(buf, 0, 1);
        LittleEndian.putUInt(buf, 0, sde.reserved2);
        bos.write(buf, 0, 4);
        byte nameBytes[] = StringUtil.getToUnicodeLE(sde.streamName);
        bos.write(nameBytes, 0, nameBytes.length);
        LittleEndian.putShort(buf, 0, (short)0); // null-termination
        bos.write(buf, 0, 2);
    }
    
    int savedSize = bos.size();
    int streamDescriptorArraySize = savedSize - streamDescriptorArrayOffset;
    LittleEndian.putUInt(buf, 0, streamDescriptorArrayOffset);
    LittleEndian.putUInt(buf, 4, streamDescriptorArraySize);

    bos.reset();
    bos.setBlock(0);
    bos.write(buf, 0, 8);
    bos.setSize(savedSize);
    
    dir.createDocument(encryptedStream, new ByteArrayInputStream(bos.getBuf(), 0, savedSize));
}