Java Code Examples for org.apache.poi.util.StringUtil#readUnicodeLE()

The following examples show how to use org.apache.poi.util.StringUtil#readUnicodeLE() . 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: DataSpaceMapUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static String readUnicodeLPP4(LittleEndianInput is) {
    int length = is.readInt();
    if (length%2 != 0) {
        throw new EncryptedDocumentException(
            "UNICODE-LP-P4 structure is a multiple of 4 bytes. "
            + "If Padding is present, it MUST be exactly 2 bytes long");
    }
    
    String result = StringUtil.readUnicodeLE(is, length/2);
    if (length%4==2) {
        // Padding (variable): A set of bytes that MUST be of the correct size such that the size of the 
        // UNICODE-LP-P4 structure is a multiple of 4 bytes. If Padding is present, it MUST be exactly 
        // 2 bytes long, and each byte MUST be 0x00.            
        is.readShort();
    }
    
    return result;
}
 
Example 2
Source File: NoteRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Read the record data from the supplied <code>RecordInputStream</code>
 * 
 * @param in the RecordInputStream to read from
 */
public NoteRecord(RecordInputStream in) {
	field_1_row = in.readUShort();
	field_2_col = in.readShort();
	field_3_flags = in.readShort();
	field_4_shapeid = in.readUShort();
	int length = in.readShort();
	field_5_hasMultibyte = in.readByte() != 0x00;
	if (field_5_hasMultibyte) {
		field_6_author = StringUtil.readUnicodeLE(in, length);
	} else {
		field_6_author = StringUtil.readCompressedUnicode(in, length);
	}
		if (in.available() == 1) {
		field_7_padding = Byte.valueOf(in.readByte());
	} else if (in.available() == 2 && length == 0) {
	    // If there's no author, may be double padded
           field_7_padding = Byte.valueOf(in.readByte());
           in.readByte();
		}
}
 
Example 3
Source File: NameCommentRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param ris the RecordInputstream to read the record from
 */
public NameCommentRecord(final RecordInputStream ris) {
  final LittleEndianInput in = ris;
  field_1_record_type = in.readShort();
  field_2_frt_cell_ref_flag = in.readShort();
  field_3_reserved = in.readLong();
  final int field_4_name_length = in.readShort();
  final int field_5_comment_length = in.readShort();

  if (in.readByte() == 0) {
      field_6_name_text = StringUtil.readCompressedUnicode(in, field_4_name_length);
  } else {
      field_6_name_text = StringUtil.readUnicodeLE(in, field_4_name_length);
  }
  if (in.readByte() == 0) {
      field_7_comment_text = StringUtil.readCompressedUnicode(in, field_5_comment_length);
  } else {
      field_7_comment_text = StringUtil.readUnicodeLE(in, field_5_comment_length);
  }    
}
 
Example 4
Source File: StringPtg.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** Create a StringPtg from a stream */
public StringPtg(LittleEndianInput in)  {
	int nChars = in.readUByte(); // Note - nChars is 8-bit
	_is16bitUnicode = (in.readByte() & 0x01) != 0;
	if (_is16bitUnicode) {
		field_3_string = StringUtil.readUnicodeLE(in, nChars);
	} else {
		field_3_string = StringUtil.readCompressedUnicode(in, nChars);
	}
}
 
Example 5
Source File: StyleRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public StyleRecord(RecordInputStream in) {
	field_1_xf_index = in.readShort();
	if (isBuiltin()) {
		field_2_builtin_style	   = in.readByte();
		field_3_outline_style_level = in.readByte();
	} else {
		int field_2_name_length = in.readShort();

		if(in.remaining() < 1) {
			// Some files from Crystal Reports lack the is16BitUnicode byte
			//  the remaining fields, which is naughty
			if (field_2_name_length != 0) {
				throw new RecordFormatException("Ran out of data reading style record");
			}
			// guess this is OK if the string length is zero
			field_4_name = "";
		} else {

			field_3_stringHasMultibyte = in.readByte() != 0x00;
			if (field_3_stringHasMultibyte) {
				field_4_name = StringUtil.readUnicodeLE(in, field_2_name_length);
			} else {
				field_4_name = StringUtil.readCompressedUnicode(in, field_2_name_length);
			}
		}
	}
}
 
Example 6
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 7
Source File: EmbeddedObjectRefSubRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public EmbeddedObjectRefSubRecord(LittleEndianInput in, int size) {

		// Much guess-work going on here due to lack of any documentation.
		// See similar source code in OOO:
		// http://svn.services.openoffice.org/ooo/trunk/sc/source/filter/excel/xiescher.cxx
		// 1223 void XclImpOleObj::ReadPictFmla( XclImpStream& rStrm, sal_uInt16 nRecSize )

		int streamIdOffset = in.readShort(); // OOO calls this 'nFmlaLen'
		int remaining = size - LittleEndian.SHORT_SIZE;

		int dataLenAfterFormula = remaining - streamIdOffset;
		int formulaSize = in.readUShort();
		remaining -= LittleEndian.SHORT_SIZE;
		field_1_unknown_int = in.readInt();
		remaining -= LittleEndian.INT_SIZE;
		byte[] formulaRawBytes = readRawData(in, formulaSize);
		remaining -= formulaSize;
		field_2_refPtg = readRefPtg(formulaRawBytes);
		if (field_2_refPtg == null) {
			// common case
			// field_2_n16 seems to be 5 here
			// The formula almost looks like tTbl but the row/column values seem like garbage.
			field_2_unknownFormulaData = formulaRawBytes;
		} else {
			field_2_unknownFormulaData = null;
		}

		int stringByteCount;
		if (remaining >= dataLenAfterFormula + 3) {
			int tag = in.readByte();
			stringByteCount = LittleEndian.BYTE_SIZE;
			if (tag != 0x03) {
				throw new RecordFormatException("Expected byte 0x03 here");
			}
			int nChars = in.readUShort();
			stringByteCount += LittleEndian.SHORT_SIZE;
			if (nChars > 0) {
				 // OOO: the 4th way Xcl stores a unicode string: not even a Grbit byte present if length 0
				field_3_unicode_flag = ( in.readByte() & 0x01 ) != 0;
				stringByteCount += LittleEndian.BYTE_SIZE;
				if (field_3_unicode_flag) {
					field_4_ole_classname = StringUtil.readUnicodeLE(in, nChars);
					stringByteCount += nChars * 2;
				} else {
					field_4_ole_classname = StringUtil.readCompressedUnicode(in, nChars);
					stringByteCount += nChars;
				}
			} else {
				field_4_ole_classname = "";
			}
		} else {
			field_4_ole_classname = null;
			stringByteCount = 0;
		}
		remaining -= stringByteCount;
		// Pad to next 2-byte boundary
		if (((stringByteCount + formulaSize) % 2) != 0) {
			int b = in.readByte();
			remaining -= LittleEndian.BYTE_SIZE;
			if (field_2_refPtg != null && field_4_ole_classname == null) {
				field_4_unknownByte = Byte.valueOf((byte)b);
			}
		}
		int nUnexpectedPadding = remaining - dataLenAfterFormula;

		if (nUnexpectedPadding > 0) {
			logger.log( POILogger.ERROR, "Discarding " + nUnexpectedPadding + " unexpected padding bytes ");
			readRawData(in, nUnexpectedPadding);
			remaining-=nUnexpectedPadding;
		}

		// Fetch the stream ID
		if (dataLenAfterFormula >= 4) {
			field_5_stream_id = Integer.valueOf(in.readInt());
			remaining -= LittleEndian.INT_SIZE;
		} else {
			field_5_stream_id = null;
		}
		field_6_unknown = readRawData(in, remaining);
	}
 
Example 8
Source File: UnicodeString.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected ExtRst(LittleEndianInput in, int expectedLength) {
   reserved = in.readShort();
   
   // Old style detection (Reserved = 0xFF)
   if(reserved == -1) {
      populateEmpty();
      return;
   }
   
   // Spot corrupt records
   if(reserved != 1) {
      _logger.log(POILogger.WARN, "Warning - ExtRst has wrong magic marker, expecting 1 but found " + reserved + " - ignoring");
      // Grab all the remaining data, and ignore it
      for(int i=0; i<expectedLength-2; i++) {
         in.readByte();
      }
      // And make us be empty
      populateEmpty();
      return;
   }

   // Carry on reading in as normal
   short stringDataSize = in.readShort();
   
   formattingFontIndex = in.readShort();
   formattingOptions   = in.readShort();
   
   // RPHSSub
   numberOfRuns = in.readUShort();
   short length1 = in.readShort();
   // No really. Someone clearly forgot to read
   //  the docs on their datastructure...
   short length2 = in.readShort();
   // And sometimes they write out garbage :(
   if(length1 == 0 && length2 > 0) {
      length2 = 0;
   }
   if(length1 != length2) {
      throw new IllegalStateException(
            "The two length fields of the Phonetic Text don't agree! " +
            length1 + " vs " + length2
      );
   }
   phoneticText = StringUtil.readUnicodeLE(in, length1);
   
   int runData = stringDataSize - 4 - 6 - (2*phoneticText.length());
   int numRuns = (runData / 6);
   phRuns = new PhRun[numRuns];
   for(int i=0; i<phRuns.length; i++) {
      phRuns[i] = new PhRun(in);
   }

   int extraDataLength = runData - (numRuns*6);
   if(extraDataLength < 0) {
 	 _logger.log( POILogger.WARN, "Warning - ExtRst overran by " + (0-extraDataLength) + " bytes");
      extraDataLength = 0;
   }
   extraData = new byte[extraDataLength];
   for(int i=0; i<extraData.length; i++) {
      extraData[i] = in.readByte();
   }
}