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.hwpf.sprm;
019:
020: import java.util.List;
021:
022: import org.apache.poi.util.LittleEndian;
023:
024: public class SprmUtils {
025: public SprmUtils() {
026: }
027:
028: public static byte[] shortArrayToByteArray(short[] convert) {
029: byte[] buf = new byte[convert.length * LittleEndian.SHORT_SIZE];
030:
031: for (int x = 0; x < convert.length; x++) {
032: LittleEndian.putShort(buf, x * LittleEndian.SHORT_SIZE,
033: convert[x]);
034: }
035:
036: return buf;
037: }
038:
039: public static int addSpecialSprm(short instruction,
040: byte[] varParam, List list) {
041: byte[] sprm = new byte[varParam.length + 4];
042: System.arraycopy(varParam, 0, sprm, 4, varParam.length);
043: LittleEndian.putShort(sprm, instruction);
044: LittleEndian.putShort(sprm, 2, (short) (varParam.length + 1));
045: list.add(sprm);
046: return sprm.length;
047: }
048:
049: public static int addSprm(short instruction, int param,
050: byte[] varParam, List list) {
051: int type = (instruction & 0xe000) >> 13;
052:
053: byte[] sprm = null;
054: switch (type) {
055: case 0:
056: case 1:
057: sprm = new byte[3];
058: sprm[2] = (byte) param;
059: break;
060: case 2:
061: sprm = new byte[4];
062: LittleEndian.putShort(sprm, 2, (short) param);
063: break;
064: case 3:
065: sprm = new byte[6];
066: LittleEndian.putInt(sprm, 2, param);
067: break;
068: case 4:
069: case 5:
070: sprm = new byte[4];
071: LittleEndian.putShort(sprm, 2, (short) param);
072: break;
073: case 6:
074: sprm = new byte[3 + varParam.length];
075: sprm[2] = (byte) varParam.length;
076: System.arraycopy(varParam, 0, sprm, 3, varParam.length);
077: break;
078: case 7:
079: sprm = new byte[5];
080: // this is a three byte int so it has to be handled special
081: byte[] temp = new byte[4];
082: LittleEndian.putInt(temp, 0, param);
083: System.arraycopy(temp, 0, sprm, 2, 3);
084: break;
085: default:
086: //should never happen
087: break;
088: }
089: LittleEndian.putShort(sprm, 0, instruction);
090: list.add(sprm);
091: return sprm.length;
092: }
093:
094: public static byte[] getGrpprl(List sprmList, int size) {
095: // spit out the final grpprl
096: byte[] grpprl = new byte[size];
097: int listSize = sprmList.size() - 1;
098: int index = 0;
099: for (; listSize >= 0; listSize--) {
100: byte[] sprm = (byte[]) sprmList.remove(0);
101: System.arraycopy(sprm, 0, grpprl, index, sprm.length);
102: index += sprm.length;
103: }
104:
105: return grpprl;
106:
107: }
108:
109: public static int convertBrcToInt(short[] brc) {
110: byte[] buf = new byte[4];
111: LittleEndian.putShort(buf, brc[0]);
112: LittleEndian.putShort(buf, LittleEndian.SHORT_SIZE, brc[1]);
113: return LittleEndian.getInt(buf);
114: }
115: }
|