You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MAPIAttribute.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hmef.attribute;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import org.apache.poi.hmef.Attachment;
  23. import org.apache.poi.hmef.HMEFMessage;
  24. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  25. import org.apache.poi.hsmf.datatypes.Types;
  26. import org.apache.poi.hsmf.datatypes.Types.MAPIType;
  27. import org.apache.poi.util.HexDump;
  28. import org.apache.poi.util.IOUtils;
  29. import org.apache.poi.util.LittleEndian;
  30. import org.apache.poi.util.StringUtil;
  31. /**
  32. * A pure-MAPI attribute which applies to a {@link HMEFMessage}
  33. * or one of its {@link Attachment}s.
  34. */
  35. public class MAPIAttribute {
  36. //arbitrarily selected; may need to increase
  37. private static final int DEFAULT_MAX_RECORD_LENGTH = 1_000_000;
  38. private static int MAX_RECORD_LENGTH = 1_000_000;
  39. private final MAPIProperty property;
  40. private final int type;
  41. private final byte[] data;
  42. /**
  43. * @param length the max record length allowed for MAPIAttribute
  44. */
  45. public static void setMaxRecordLength(int length) {
  46. MAX_RECORD_LENGTH = length;
  47. }
  48. /**
  49. * @return the max record length allowed for MAPIAttribute
  50. */
  51. public static int getMaxRecordLength() {
  52. return MAX_RECORD_LENGTH;
  53. }
  54. /**
  55. * Constructs a single new attribute from
  56. * the contents of the stream
  57. */
  58. public MAPIAttribute(MAPIProperty property, int type, byte[] data) {
  59. this.property = property;
  60. this.type = type;
  61. this.data = data.clone();
  62. }
  63. public MAPIProperty getProperty() {
  64. return property;
  65. }
  66. public int getType() {
  67. return type;
  68. }
  69. public byte[] getData() {
  70. return data;
  71. }
  72. public String toString() {
  73. String hex;
  74. if(data.length <= 16) {
  75. hex = HexDump.toHex(data);
  76. } else {
  77. byte[] d = Arrays.copyOf(data, 16);
  78. hex = HexDump.toHex(d);
  79. hex = hex.substring(0, hex.length()-1) + ", ....]";
  80. }
  81. return property + " " + hex;
  82. }
  83. /**
  84. * Parses a MAPI Properties TNEF Attribute, and returns
  85. * the list of MAPI Attributes contained within it
  86. */
  87. public static List<MAPIAttribute> create(TNEFAttribute parent) throws IOException {
  88. if(parent.getProperty() == TNEFProperty.ID_MAPIPROPERTIES) {
  89. // Regular MAPI Properties, normally on the message
  90. }
  91. else if(parent.getProperty() == TNEFProperty.ID_ATTACHMENT) {
  92. // MAPI Properties for an attachment
  93. }
  94. else {
  95. // Something else, oh dear...
  96. throw new IllegalArgumentException(
  97. "Can only create from a MAPIProperty attribute, " +
  98. "instead received a " + parent.getProperty() + " one"
  99. );
  100. }
  101. ByteArrayInputStream inp = new ByteArrayInputStream(parent.getData());
  102. // First up, get the number of attributes
  103. int count = LittleEndian.readInt(inp);
  104. List<MAPIAttribute> attrs = new ArrayList<>();
  105. // Now, read each one in in turn
  106. for(int i=0; i<count; i++) {
  107. int typeAndMV = LittleEndian.readUShort(inp);
  108. int id = LittleEndian.readUShort(inp);
  109. // Is it either Multi-Valued or Variable-Length?
  110. boolean isMV = false;
  111. boolean isVL = false;
  112. int typeId = typeAndMV;
  113. if( (typeAndMV & Types.MULTIVALUED_FLAG) != 0 ) {
  114. isMV = true;
  115. typeId -= Types.MULTIVALUED_FLAG;
  116. }
  117. if(typeId == Types.ASCII_STRING.getId() || typeId == Types.UNICODE_STRING.getId() ||
  118. typeId == Types.BINARY.getId() || typeId == Types.DIRECTORY.getId()) {
  119. isVL = true;
  120. }
  121. // Turn the type ID into a strongly typed thing
  122. MAPIType type = Types.getById(typeId);
  123. if (type == null) {
  124. type = Types.createCustom(typeId);
  125. }
  126. // If it's a named property, rather than a standard
  127. // MAPI property, grab the details of it
  128. MAPIProperty prop = MAPIProperty.get(id);
  129. if(id >= 0x8000 && id <= 0xFFFF) {
  130. byte[] guid = new byte[16];
  131. if (IOUtils.readFully(inp, guid) < 0) {
  132. throw new IOException("Not enough data to read guid");
  133. }
  134. int mptype = LittleEndian.readInt(inp);
  135. // Get the name of it
  136. String name;
  137. if(mptype == 0) {
  138. // It's based on a normal one
  139. int mpid = LittleEndian.readInt(inp);
  140. MAPIProperty base = MAPIProperty.get(mpid);
  141. name = base.name;
  142. } else {
  143. // Custom name was stored
  144. int mplen = LittleEndian.readInt(inp);
  145. byte[] mpdata = IOUtils.safelyAllocate(mplen, MAX_RECORD_LENGTH);
  146. if (IOUtils.readFully(inp, mpdata) < 0) {
  147. throw new IOException("Not enough data to read " + mplen + " bytes for attribute name");
  148. }
  149. name = StringUtil.getFromUnicodeLE(mpdata, 0, (mplen/2)-1);
  150. skipToBoundary(mplen, inp);
  151. }
  152. // Now create
  153. prop = MAPIProperty.createCustom(id, type, name);
  154. }
  155. if(prop == MAPIProperty.UNKNOWN) {
  156. prop = MAPIProperty.createCustom(id, type, "(unknown " + Integer.toHexString(id) + ")");
  157. }
  158. // Now read in the value(s)
  159. int values = 1;
  160. if(isMV || isVL) {
  161. values = LittleEndian.readInt(inp);
  162. }
  163. if (type == Types.NULL && values > 1) {
  164. throw new IOException("Placeholder/NULL arrays aren't supported.");
  165. }
  166. for(int j=0; j<values; j++) {
  167. int len = getLength(type, inp);
  168. byte[] data = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH);
  169. if (IOUtils.readFully(inp, data) < 0) {
  170. throw new IOException("Not enough data to read " + len + " bytes of attribute value");
  171. }
  172. skipToBoundary(len, inp);
  173. // Create
  174. MAPIAttribute attr;
  175. if(type == Types.UNICODE_STRING || type == Types.ASCII_STRING) {
  176. attr = new MAPIStringAttribute(prop, typeId, data);
  177. } else if(type == Types.APP_TIME || type == Types.TIME) {
  178. attr = new MAPIDateAttribute(prop, typeId, data);
  179. } else if(id == MAPIProperty.RTF_COMPRESSED.id) {
  180. attr = new MAPIRtfAttribute(prop, typeId, data);
  181. } else {
  182. attr = new MAPIAttribute(prop, typeId, data);
  183. }
  184. attrs.add(attr);
  185. }
  186. }
  187. // All done
  188. return attrs;
  189. }
  190. private static int getLength(MAPIType type, InputStream inp) throws IOException {
  191. if (type.isFixedLength()) {
  192. return type.getLength();
  193. }
  194. if (type == Types.ASCII_STRING ||
  195. type == Types.UNICODE_STRING ||
  196. type == Types.DIRECTORY ||
  197. type == Types.BINARY) {
  198. // Need to read the length, as it varies
  199. return LittleEndian.readInt(inp);
  200. } else {
  201. throw new IllegalArgumentException("Unknown type " + type);
  202. }
  203. }
  204. private static void skipToBoundary(int length, InputStream inp) throws IOException {
  205. // Data is always padded out to a 4 byte boundary
  206. if(length % 4 != 0) {
  207. int toSkip = 4 - (length % 4);
  208. long skipped = IOUtils.skipFully(inp, toSkip);
  209. if (skipped != toSkip) {
  210. throw new IOException("tried to skip "+toSkip +" but only skipped:"+skipped);
  211. }
  212. }
  213. }
  214. }