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

The following examples show how to use org.apache.poi.util.LittleEndian#putUShort() . 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: ImageHeaderWMF.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void write(OutputStream out) throws IOException {
    byte[] header = new byte[22];
    int pos = 0;
    LittleEndian.putInt(header, pos, APMHEADER_KEY); pos += LittleEndian.INT_SIZE; //header key
    LittleEndian.putUShort(header, pos, 0); pos += LittleEndian.SHORT_SIZE; //hmf
    LittleEndian.putUShort(header, pos, left); pos += LittleEndian.SHORT_SIZE; //left
    LittleEndian.putUShort(header, pos, top); pos += LittleEndian.SHORT_SIZE; //top
    LittleEndian.putUShort(header, pos, right); pos += LittleEndian.SHORT_SIZE; //right
    LittleEndian.putUShort(header, pos, bottom); pos += LittleEndian.SHORT_SIZE; //bottom
    LittleEndian.putUShort(header, pos, inch); pos += LittleEndian.SHORT_SIZE; //inch
    LittleEndian.putInt(header, pos, 0); pos += LittleEndian.INT_SIZE;  //reserved

    checksum = getChecksum();
    LittleEndian.putUShort(header, pos, checksum);

    out.write(header);
}
 
Example 2
Source File: TypeWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Writes an unsigned two-byte value to an output stream.</p>
 *
 * @param out The stream to write to
 * @param n The value to write
 * @exception IOException if an I/O error occurs
 */
public static void writeUShortToStream( final OutputStream out, final int n )
        throws IOException
{
    int high = n & 0xFFFF0000;
    if ( high != 0 ) {
        throw new IllegalPropertySetDataException( "Value " + n
                + " cannot be represented by 2 bytes." );
    }
    LittleEndian.putUShort( n, out );
}
 
Example 3
Source File: XOREncryptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void confirmPassword(String password) {
    int keyComp      = CryptoFunctions.createXorKey1(password);
    int verifierComp = CryptoFunctions.createXorVerifier1(password);
    byte xorArray[]  = CryptoFunctions.createXorArray1(password);
    
    byte shortBuf[] = new byte[2];
    XOREncryptionVerifier ver = (XOREncryptionVerifier)getEncryptionInfo().getVerifier();
    LittleEndian.putUShort(shortBuf, 0, keyComp);
    ver.setEncryptedKey(shortBuf);
    LittleEndian.putUShort(shortBuf, 0, verifierComp);
    ver.setEncryptedVerifier(shortBuf);
    setSecretKey(new SecretKeySpec(xorArray, "XOR"));
}
 
Example 4
Source File: WriteAccessRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public WriteAccessRecord(RecordInputStream in) {
	if (in.remaining() > DATA_SIZE) {
		throw new RecordFormatException("Expected data size (" + DATA_SIZE + ") but got ("
				+ in.remaining() + ")");
	}
	// The string is always 112 characters (padded with spaces), therefore
	// this record can not be continued.

	int nChars = in.readUShort();
	int is16BitFlag = in.readUByte();
	if (nChars > DATA_SIZE || (is16BitFlag & 0xFE) != 0) {
		// String header looks wrong (probably missing)
		// OOO doc says this is optional anyway.
		// reconstruct data
		byte[] data = new byte[3 + in.remaining()];
		LittleEndian.putUShort(data, 0, nChars);
		LittleEndian.putByte(data, 2, is16BitFlag);
		in.readFully(data, 3, data.length-3);
		String rawValue = new String(data, StringUtil.UTF8);
		setUsername(rawValue.trim());
		return;
	}

	String rawText;
	if ((is16BitFlag & 0x01) == 0x00) {
		rawText = StringUtil.readCompressedUnicode(in, nChars);
	} else {
		rawText = StringUtil.readUnicodeLE(in, nChars);
	}
	field_1_username = rawText.trim();

	// consume padding
	int padSize = in.remaining();
	while (padSize > 0) {
		// in some cases this seems to be garbage (non spaces)
		in.readUByte();
		padSize--;
	}
}
 
Example 5
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));
}