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

The following examples show how to use org.apache.poi.util.LittleEndian#putShort() . 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: EscherDgRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
    public int serialize( int offset, byte[] data, EscherSerializationListener listener )
    {
        listener.beforeRecordSerialize( offset, getRecordId(), this );

        LittleEndian.putShort( data, offset, getOptions() );
        LittleEndian.putShort( data, offset + 2, getRecordId() );
        LittleEndian.putInt( data, offset + 4, 8 );
        LittleEndian.putInt( data, offset + 8, field_1_numShapes );
        LittleEndian.putInt( data, offset + 12, field_2_lastMSOSPID );
//        System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length );
//        int pos = offset + 8 + 18 + remainingData.length;

        listener.afterRecordSerialize( offset + 16, getRecordId(), getRecordSize(), this );
        return getRecordSize();
    }
 
Example 2
Source File: AbstractEscherHolderRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int serialize(int offset, byte[] data)
{
    LittleEndian.putShort( data, 0 + offset, getSid() );
    LittleEndian.putShort( data, 2 + offset, (short) ( getRecordSize() - 4 ) );
    byte[] rawData = getRawData();
    if ( escherRecords.size() == 0 && rawData != null )
    {
        LittleEndian.putShort(data, 0 + offset, getSid());
        LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
        System.arraycopy( rawData, 0, data, 4 + offset, rawData.length);
        return rawData.length + 4;
    }
    LittleEndian.putShort(data, 0 + offset, getSid());
    LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));

    int pos = offset + 4;
    for (EscherRecord r : escherRecords) {
        pos += r.serialize( pos, data, new NullEscherSerializationListener() );
    }
    return getRecordSize();
}
 
Example 3
Source File: EscherSpRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
     * This method serializes this escher record into a byte array.
     *
     * @param offset   The offset into <code>data</code> to start writing the record data to.
     * @param data     The byte array to serialize to.
     * @param listener A listener to retrieve start and end callbacks.  Use a <code>NullEscherSerailizationListener</code> to ignore these events.
     * @return The number of bytes written.
     *
     * @see NullEscherSerializationListener
     */
    @Override
    public int serialize( int offset, byte[] data, EscherSerializationListener listener )
    {
        listener.beforeRecordSerialize( offset, getRecordId(), this );
        LittleEndian.putShort( data, offset, getOptions() );
        LittleEndian.putShort( data, offset + 2, getRecordId() );
        int remainingBytes = 8;
        LittleEndian.putInt( data, offset + 4, remainingBytes );
        LittleEndian.putInt( data, offset + 8, field_1_shapeId );
        LittleEndian.putInt( data, offset + 12, field_2_flags );
//        System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length );
//        int pos = offset + 8 + 18 + remainingData.length;
        listener.afterRecordSerialize( offset + getRecordSize(), getRecordId(), getRecordSize(), this );
        return 8 + 8;
    }
 
Example 4
Source File: EscherSpgrRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
    public int serialize( int offset, byte[] data, EscherSerializationListener listener )
    {
        listener.beforeRecordSerialize( offset, getRecordId(), this );
        LittleEndian.putShort( data, offset, getOptions() );
        LittleEndian.putShort( data, offset + 2, getRecordId() );
        int remainingBytes = 16;
        LittleEndian.putInt( data, offset + 4, remainingBytes );
        LittleEndian.putInt( data, offset + 8, field_1_rectX1 );
        LittleEndian.putInt( data, offset + 12, field_2_rectY1 );
        LittleEndian.putInt( data, offset + 16, field_3_rectX2 );
        LittleEndian.putInt( data, offset + 20, field_4_rectY2 );
//        System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length );
//        int pos = offset + 8 + 18 + remainingData.length;
        listener.afterRecordSerialize( offset + getRecordSize(), getRecordId(), offset + getRecordSize(), this );
        return 8 + 16;
    }
 
Example 5
Source File: EscherSplitMenuColorsRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
    public int serialize( int offset, byte[] data, EscherSerializationListener listener ) {
//        int field_2_numIdClusters = field_5_fileIdClusters.length + 1;
        listener.beforeRecordSerialize( offset, getRecordId(), this );

        int pos = offset;
        LittleEndian.putShort( data, pos, getOptions() );     pos += 2;
        LittleEndian.putShort( data, pos, getRecordId() );    pos += 2;
        int remainingBytes =  getRecordSize() - 8;

        LittleEndian.putInt( data, pos, remainingBytes );          pos += 4;
        LittleEndian.putInt( data, pos, field_1_color1 );          pos += 4;
        LittleEndian.putInt( data, pos, field_2_color2 );          pos += 4;
        LittleEndian.putInt( data, pos, field_3_color3 );          pos += 4;
        LittleEndian.putInt( data, pos, field_4_color4 );          pos += 4;
        listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
        return getRecordSize();
    }
 
Example 6
Source File: EscherTextboxRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int serialize( int offset, byte[] data, EscherSerializationListener listener )
{
    listener.beforeRecordSerialize( offset, getRecordId(), this );

    LittleEndian.putShort(data, offset, getOptions());
    LittleEndian.putShort(data, offset+2, getRecordId());
    int remainingBytes = thedata.length;
    LittleEndian.putInt(data, offset+4, remainingBytes);
    System.arraycopy(thedata, 0, data, offset+8, thedata.length);
    int pos = offset+8+thedata.length;

    listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
    int size = pos - offset;
    if (size != getRecordSize()) {
        throw new RecordFormatException(size + " bytes written but getRecordSize() reports " + getRecordSize());
    }
    return size;
}
 
Example 7
Source File: EscherBitmapBlip.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int serialize( int offset, byte[] data, EscherSerializationListener listener ) {
    listener.beforeRecordSerialize(offset, getRecordId(), this);

    LittleEndian.putShort( data, offset, getOptions() );
    LittleEndian.putShort( data, offset + 2, getRecordId() );
    LittleEndian.putInt( data, offset + 4, getRecordSize() - HEADER_SIZE );
    int pos = offset + HEADER_SIZE;

    System.arraycopy( field_1_UID, 0, data, pos, 16 );
    data[pos + 16] = field_2_marker;
    byte pd[] = getPicturedata();
    System.arraycopy( pd, 0, data, pos + 17, pd.length );

    listener.afterRecordSerialize(offset + getRecordSize(), getRecordId(), getRecordSize(), this);
    return HEADER_SIZE + 16 + 1 + pd.length;
}
 
Example 8
Source File: EscherArrayProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void setSizeOfElements(int sizeOfElements) {
    LittleEndian.putShort( getComplexData(), 4, (short) sizeOfElements );

    int expectedArraySize = getNumberOfElementsInArray() * getActualSizeOfElements(getSizeOfElements()) + FIXED_SIZE;
    if (expectedArraySize != getComplexData().length) {
        // Keep just the first 6 bytes.  The rest is no good to us anyway.
        byte[] newArray = new byte[expectedArraySize];
        System.arraycopy( getComplexData(), 0, newArray, 0, 6 );
        setComplexData(newArray);
    }
}
 
Example 9
Source File: EscherComplexProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Serializes the simple part of this property.  i.e. the first 6 bytes.
 */
@Override
public int serializeSimplePart(byte[] data, int pos) {
    LittleEndian.putShort(data, pos, getId());
    LittleEndian.putInt(data, pos + 2, _complexData.length);
    return 6;
}
 
Example 10
Source File: EscherArrayProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void setNumberOfElementsInArray(int numberOfElements) {
    int expectedArraySize = numberOfElements * getActualSizeOfElements(getSizeOfElements()) + FIXED_SIZE;
    if (expectedArraySize != getComplexData().length) {
        byte[] newArray = new byte[expectedArraySize];
        System.arraycopy(getComplexData(), 0, newArray, 0, getComplexData().length);
        setComplexData(newArray);
    }
    LittleEndian.putShort(getComplexData(), 0, (short) numberOfElements);
}
 
Example 11
Source File: EscherMetafileBlip.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
    listener.beforeRecordSerialize(offset, getRecordId(), this);

    int pos = offset;
    LittleEndian.putShort( data, pos, getOptions() ); pos += 2;
    LittleEndian.putShort( data, pos, getRecordId() ); pos += 2;
    LittleEndian.putInt( data, pos, getRecordSize() - HEADER_SIZE ); pos += 4;

    System.arraycopy( field_1_UID, 0, data, pos, field_1_UID.length ); pos += field_1_UID.length;
    if ((getOptions() ^ getSignature()) == 0x10) {
        System.arraycopy( field_2_UID, 0, data, pos, field_2_UID.length ); pos += field_2_UID.length;
    }
    LittleEndian.putInt( data, pos, field_2_cb ); pos += 4;
    LittleEndian.putInt( data, pos, field_3_rcBounds_x1 ); pos += 4;
    LittleEndian.putInt( data, pos, field_3_rcBounds_y1 ); pos += 4;
    LittleEndian.putInt( data, pos, field_3_rcBounds_x2 ); pos += 4;
    LittleEndian.putInt( data, pos, field_3_rcBounds_y2 ); pos += 4;
    LittleEndian.putInt( data, pos, field_4_ptSize_w ); pos += 4;
    LittleEndian.putInt( data, pos, field_4_ptSize_h ); pos += 4;
    LittleEndian.putInt( data, pos, field_5_cbSave ); pos += 4;
    data[pos] = field_6_fCompression; pos++;
    data[pos] = field_7_fFilter; pos++;

    System.arraycopy( raw_pictureData, 0, data, pos, raw_pictureData.length ); pos += raw_pictureData.length;
    if(remainingData != null) {
        System.arraycopy( remainingData, 0, data, pos, remainingData.length ); pos += remainingData.length;
    }

    listener.afterRecordSerialize(offset + getRecordSize(), getRecordId(), getRecordSize(), this);
    return getRecordSize();
}
 
Example 12
Source File: EscherBSERecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
    listener.beforeRecordSerialize( offset, getRecordId(), this );

    if (_remainingData == null) {
        _remainingData = new byte[0];
    }

    LittleEndian.putShort( data, offset, getOptions() );
    LittleEndian.putShort( data, offset + 2, getRecordId() );
    int blipSize = field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize();
    int remainingBytes = _remainingData.length + 36 + blipSize;
    LittleEndian.putInt( data, offset + 4, remainingBytes );

    data[offset + 8] = field_1_blipTypeWin32;
    data[offset + 9] = field_2_blipTypeMacOS;
    System.arraycopy(field_3_uid, 0, data, offset + 10, 16);
    LittleEndian.putShort( data, offset + 26, field_4_tag );
    LittleEndian.putInt( data, offset + 28, field_5_size );
    LittleEndian.putInt( data, offset + 32, field_6_ref );
    LittleEndian.putInt( data, offset + 36, field_7_offset );
    data[offset + 40] = field_8_usage;
    data[offset + 41] = field_9_name;
    data[offset + 42] = field_10_unused2;
    data[offset + 43] = field_11_unused3;
    int bytesWritten = 0;
    if (field_12_blipRecord != null) {
        bytesWritten = field_12_blipRecord.serialize( offset + 44, data, new NullEscherSerializationListener() );
    }
    System.arraycopy( _remainingData, 0, data, offset + 44 + bytesWritten, _remainingData.length );
    int pos = offset + 8 + 36 + _remainingData.length + bytesWritten;

    listener.afterRecordSerialize(pos, getRecordId(), pos - offset, this);
    return pos - offset;
}
 
Example 13
Source File: EscherArrayProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Serializes the simple part of this property.  ie the first 6 bytes.
 *
 * Needs special code to handle the case when the size doesn't
 *  include the size of the header block
 */
@Override
public int serializeSimplePart(byte[] data, int pos) {
    LittleEndian.putShort(data, pos, getId());
    int recordSize = getComplexData().length;
    if(!sizeIncludesHeaderSize) {
        recordSize -= 6;
    }
    LittleEndian.putInt(data, pos + 2, recordSize);
    return 6;
}
 
Example 14
Source File: EscherBlipRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
    listener.beforeRecordSerialize(offset, getRecordId(), this);

    LittleEndian.putShort( data, offset, getOptions() );
    LittleEndian.putShort( data, offset + 2, getRecordId() );

    System.arraycopy( field_pictureData, 0, data, offset + 4, field_pictureData.length );

    listener.afterRecordSerialize(offset + 4 + field_pictureData.length, getRecordId(), field_pictureData.length + 4, this);
    return field_pictureData.length + 4;
}
 
Example 15
Source File: EscherChildAnchorRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
    listener.beforeRecordSerialize( offset, getRecordId(), this );
    int pos = offset;
    LittleEndian.putShort( data, pos, getOptions() );          pos += 2;
    LittleEndian.putShort( data, pos, getRecordId() );         pos += 2;
    LittleEndian.putInt( data, pos, getRecordSize()-8 );       pos += 4;
    LittleEndian.putInt( data, pos, field_1_dx1 );             pos += 4;
    LittleEndian.putInt( data, pos, field_2_dy1 );             pos += 4;
    LittleEndian.putInt( data, pos, field_3_dx2 );             pos += 4;
    LittleEndian.putInt( data, pos, field_4_dy2 );             pos += 4;

    listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
    return pos - offset;
}
 
Example 16
Source File: DrawingGroupRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void writeContinueHeader( byte[] data, int offset, int sizeExcludingHeader )
{
    LittleEndian.putShort(data, 0 + offset, ContinueRecord.sid);
    LittleEndian.putShort(data, 2 + offset, (short) sizeExcludingHeader);
}
 
Example 17
Source File: DrawingGroupRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void writeHeader( byte[] data, int offset, int sizeExcludingHeader )
{
    LittleEndian.putShort(data, 0 + offset, getSid());
    LittleEndian.putShort(data, 2 + offset, (short) sizeExcludingHeader);
}
 
Example 18
Source File: CryptoFunctions.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method generates the xor verifier for word documents &lt; 2007 (method 2).
 * Its output will be used as password input for the newer word generations which
 * utilize a real hashing algorithm like sha1.
 * 
 * @param password the password
 * @return the hashed password
 * 
 * @see <a href="http://msdn.microsoft.com/en-us/library/dd905229.aspx">2.3.7.4 Binary Document Password Verifier Derivation Method 2</a>
 * @see <a href="http://blogs.msdn.com/b/vsod/archive/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0.aspx">How to set the editing restrictions in Word using Open XML SDK 2.0</a>
 * @see <a href="http://www.aspose.com/blogs/aspose-blogs/vladimir-averkin/archive/2007/08/20/funny-how-the-new-powerful-cryptography-implemented-in-word-2007-turns-it-into-a-perfect-tool-for-document-password-removal.html">Funny: How the new powerful cryptography implemented in Word 2007 turns it into a perfect tool for document password removal.</a>
 */
public static int createXorVerifier2(String password) {
    //Array to hold Key Values
    byte[] generatedKey = new byte[4];

    //Maximum length of the password is 15 chars.
    final int maxPasswordLength = 15; 
    
    if (!"".equals(password)) {
        // Truncate the password to 15 characters
        password = password.substring(0, Math.min(password.length(), maxPasswordLength));

        byte[] arrByteChars = toAnsiPassword(password);
        
        // Compute the high-order word of the new key:

        // --> Initialize from the initial code array (see below), depending on the passwords length. 
        int highOrderWord = INITIAL_CODE_ARRAY[arrByteChars.length - 1];

        // --> For each character in the password:
        //      --> For every bit in the character, starting with the least significant and progressing to (but excluding) 
        //          the most significant, if the bit is set, XOR the keys high-order word with the corresponding word from 
        //          the Encryption Matrix
        for (int i = 0; i < arrByteChars.length; i++) {
            int tmp = maxPasswordLength - arrByteChars.length + i;
            for (int intBit = 0; intBit < 7; intBit++) {
                if ((arrByteChars[i] & (0x0001 << intBit)) != 0) {
                    highOrderWord ^= ENCRYPTION_MATRIX[tmp][intBit];
                }
            }
        }
        
        // Compute the low-order word of the new key:
        int verifier = createXorVerifier1(password);

        // The byte order of the result shall be reversed [password "Example": 0x64CEED7E becomes 7EEDCE64],
        // and that value shall be hashed as defined by the attribute values.
        
        LittleEndian.putShort(generatedKey, 0, (short)verifier);
        LittleEndian.putShort(generatedKey, 2, (short)highOrderWord);
    }
    
    return LittleEndian.getInt(generatedKey);
}
 
Example 19
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));
}
 
Example 20
Source File: TypeWriter.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Writes a two-byte value (short) 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.
 * @exception IOException if an I/O error occurs
 */
public static int writeToStream( final OutputStream out, final short n )
        throws IOException
{
    LittleEndian.putShort( out, n ); // FIXME: unsigned
    return LittleEndianConsts.SHORT_SIZE;
}