PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ws/com/google/android/mms/pdu/PduComposer.java

https://gitlab.com/adam.lukaitis/TextSecure
Java | 1184 lines | 636 code | 184 blank | 364 comment | 163 complexity | 64f4cbb2121d38ba2976093e235c2957 MD5 | raw file
  1. /*
  2. * Copyright (C) 2007-2008 Esmertec AG.
  3. * Copyright (C) 2007-2008 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package ws.com.google.android.mms.pdu;
  18. import android.content.ContentResolver;
  19. import android.content.Context;
  20. import android.text.TextUtils;
  21. import android.util.Log;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.util.Arrays;
  27. import java.util.HashMap;
  28. public class PduComposer {
  29. /**
  30. * Address type.
  31. */
  32. static private final int PDU_PHONE_NUMBER_ADDRESS_TYPE = 1;
  33. static private final int PDU_EMAIL_ADDRESS_TYPE = 2;
  34. static private final int PDU_IPV4_ADDRESS_TYPE = 3;
  35. static private final int PDU_IPV6_ADDRESS_TYPE = 4;
  36. static private final int PDU_UNKNOWN_ADDRESS_TYPE = 5;
  37. /**
  38. * Address regular expression string.
  39. */
  40. static final String REGEXP_PHONE_NUMBER_ADDRESS_TYPE = "\\+?[0-9|\\.|\\-]+";
  41. static final String REGEXP_EMAIL_ADDRESS_TYPE = "[a-zA-Z| ]*\\<{0,1}[a-zA-Z| ]+@{1}" +
  42. "[a-zA-Z| ]+\\.{1}[a-zA-Z| ]+\\>{0,1}";
  43. static final String REGEXP_IPV6_ADDRESS_TYPE =
  44. "[a-fA-F]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}" +
  45. "[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}" +
  46. "[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}";
  47. static final String REGEXP_IPV4_ADDRESS_TYPE = "[0-9]{1,3}\\.{1}[0-9]{1,3}\\.{1}" +
  48. "[0-9]{1,3}\\.{1}[0-9]{1,3}";
  49. /**
  50. * The postfix strings of address.
  51. */
  52. static final String STRING_PHONE_NUMBER_ADDRESS_TYPE = "/TYPE=PLMN";
  53. static final String STRING_IPV4_ADDRESS_TYPE = "/TYPE=IPV4";
  54. static final String STRING_IPV6_ADDRESS_TYPE = "/TYPE=IPV6";
  55. /**
  56. * Error values.
  57. */
  58. static private final int PDU_COMPOSE_SUCCESS = 0;
  59. static private final int PDU_COMPOSE_CONTENT_ERROR = 1;
  60. static private final int PDU_COMPOSE_FIELD_NOT_SET = 2;
  61. static private final int PDU_COMPOSE_FIELD_NOT_SUPPORTED = 3;
  62. /**
  63. * WAP values defined in WSP spec.
  64. */
  65. static private final int QUOTED_STRING_FLAG = 34;
  66. static private final int END_STRING_FLAG = 0;
  67. static private final int LENGTH_QUOTE = 31;
  68. static private final int TEXT_MAX = 127;
  69. static private final int SHORT_INTEGER_MAX = 127;
  70. static private final int LONG_INTEGER_LENGTH_MAX = 8;
  71. /**
  72. * Block size when read data from InputStream.
  73. */
  74. static private final int PDU_COMPOSER_BLOCK_SIZE = 1024;
  75. /**
  76. * The output message.
  77. */
  78. protected ByteArrayOutputStream mMessage = null;
  79. /**
  80. * The PDU.
  81. */
  82. private GenericPdu mPdu = null;
  83. /**
  84. * Current visiting position of the mMessage.
  85. */
  86. protected int mPosition = 0;
  87. /**
  88. * Message compose buffer stack.
  89. */
  90. private BufferStack mStack = null;
  91. /**
  92. * Content resolver.
  93. */
  94. private final ContentResolver mResolver;
  95. /**
  96. * Header of this pdu.
  97. */
  98. private PduHeaders mPduHeader = null;
  99. /**
  100. * Map of all content type
  101. */
  102. private static HashMap<String, Integer> mContentTypeMap = null;
  103. static {
  104. mContentTypeMap = new HashMap<String, Integer>();
  105. int i;
  106. for (i = 0; i < PduContentTypes.contentTypes.length; i++) {
  107. mContentTypeMap.put(PduContentTypes.contentTypes[i], i);
  108. }
  109. }
  110. /**
  111. * Constructor.
  112. *
  113. * @param context the context
  114. * @param pdu the pdu to be composed
  115. */
  116. public PduComposer(Context context, GenericPdu pdu) {
  117. mPdu = pdu;
  118. mResolver = context.getContentResolver();
  119. mPduHeader = pdu.getPduHeaders();
  120. mStack = new BufferStack();
  121. mMessage = new ByteArrayOutputStream();
  122. mPosition = 0;
  123. }
  124. /**
  125. * Make the message. No need to check whether mandatory fields are set,
  126. * because the constructors of outgoing pdus are taking care of this.
  127. *
  128. * @return OutputStream of maked message. Return null if
  129. * the PDU is invalid.
  130. */
  131. public byte[] make() {
  132. // Get Message-type.
  133. int type = mPdu.getMessageType();
  134. /* make the message */
  135. switch (type) {
  136. case PduHeaders.MESSAGE_TYPE_SEND_REQ:
  137. if (makeSendReqPdu() != PDU_COMPOSE_SUCCESS) {
  138. return null;
  139. }
  140. break;
  141. case PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND:
  142. if (makeNotifyResp() != PDU_COMPOSE_SUCCESS) {
  143. return null;
  144. }
  145. break;
  146. case PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND:
  147. if (makeAckInd() != PDU_COMPOSE_SUCCESS) {
  148. return null;
  149. }
  150. break;
  151. case PduHeaders.MESSAGE_TYPE_READ_REC_IND:
  152. if (makeReadRecInd() != PDU_COMPOSE_SUCCESS) {
  153. return null;
  154. }
  155. break;
  156. default:
  157. return null;
  158. }
  159. Log.w("PduComposer", "Returning: " + mMessage.size() + " bytes...");
  160. return mMessage.toByteArray();
  161. }
  162. /**
  163. * Copy buf to mMessage.
  164. */
  165. protected void arraycopy(byte[] buf, int pos, int length) {
  166. mMessage.write(buf, pos, length);
  167. mPosition = mPosition + length;
  168. }
  169. /**
  170. * Append a byte to mMessage.
  171. */
  172. protected void append(int value) {
  173. mMessage.write(value);
  174. mPosition ++;
  175. }
  176. /**
  177. * Append short integer value to mMessage.
  178. * This implementation doesn't check the validity of parameter, since it
  179. * assumes that the values are validated in the GenericPdu setter methods.
  180. */
  181. protected void appendShortInteger(int value) {
  182. /*
  183. * From WAP-230-WSP-20010705-a:
  184. * Short-integer = OCTET
  185. * ; Integers in range 0-127 shall be encoded as a one octet value
  186. * ; with the most significant bit set to one (1xxx xxxx) and with
  187. * ; the value in the remaining least significant bits.
  188. * In our implementation, only low 7 bits are stored and otherwise
  189. * bits are ignored.
  190. */
  191. append((value | 0x80) & 0xff);
  192. }
  193. /**
  194. * Append an octet number between 128 and 255 into mMessage.
  195. * NOTE:
  196. * A value between 0 and 127 should be appended by using appendShortInteger.
  197. * This implementation doesn't check the validity of parameter, since it
  198. * assumes that the values are validated in the GenericPdu setter methods.
  199. */
  200. protected void appendOctet(int number) {
  201. append(number);
  202. }
  203. /**
  204. * Append a short length into mMessage.
  205. * This implementation doesn't check the validity of parameter, since it
  206. * assumes that the values are validated in the GenericPdu setter methods.
  207. */
  208. protected void appendShortLength(int value) {
  209. /*
  210. * From WAP-230-WSP-20010705-a:
  211. * Short-length = <Any octet 0-30>
  212. */
  213. append(value);
  214. }
  215. /**
  216. * Append long integer into mMessage. it's used for really long integers.
  217. * This implementation doesn't check the validity of parameter, since it
  218. * assumes that the values are validated in the GenericPdu setter methods.
  219. */
  220. protected void appendLongInteger(long longInt) {
  221. /*
  222. * From WAP-230-WSP-20010705-a:
  223. * Long-integer = Short-length Multi-octet-integer
  224. * ; The Short-length indicates the length of the Multi-octet-integer
  225. * Multi-octet-integer = 1*30 OCTET
  226. * ; The content octets shall be an unsigned integer value with the
  227. * ; most significant octet encoded first (big-endian representation).
  228. * ; The minimum number of octets must be used to encode the value.
  229. */
  230. int size;
  231. long temp = longInt;
  232. // Count the length of the long integer.
  233. for(size = 0; (temp != 0) && (size < LONG_INTEGER_LENGTH_MAX); size++) {
  234. temp = (temp >>> 8);
  235. }
  236. // Set Length.
  237. appendShortLength(size);
  238. // Count and set the long integer.
  239. int i;
  240. int shift = (size -1) * 8;
  241. for (i = 0; i < size; i++) {
  242. append((int)((longInt >>> shift) & 0xff));
  243. shift = shift - 8;
  244. }
  245. }
  246. /**
  247. * Append text string into mMessage.
  248. * This implementation doesn't check the validity of parameter, since it
  249. * assumes that the values are validated in the GenericPdu setter methods.
  250. */
  251. protected void appendTextString(byte[] text) {
  252. /*
  253. * From WAP-230-WSP-20010705-a:
  254. * Text-string = [Quote] *TEXT End-of-string
  255. * ; If the first character in the TEXT is in the range of 128-255,
  256. * ; a Quote character must precede it. Otherwise the Quote character
  257. * ;must be omitted. The Quote is not part of the contents.
  258. */
  259. if (((text[0])&0xff) > TEXT_MAX) { // No need to check for <= 255
  260. append(TEXT_MAX);
  261. }
  262. arraycopy(text, 0, text.length);
  263. append(0);
  264. }
  265. /**
  266. * Append text string into mMessage.
  267. * This implementation doesn't check the validity of parameter, since it
  268. * assumes that the values are validated in the GenericPdu setter methods.
  269. */
  270. protected void appendTextString(String str) {
  271. /*
  272. * From WAP-230-WSP-20010705-a:
  273. * Text-string = [Quote] *TEXT End-of-string
  274. * ; If the first character in the TEXT is in the range of 128-255,
  275. * ; a Quote character must precede it. Otherwise the Quote character
  276. * ;must be omitted. The Quote is not part of the contents.
  277. */
  278. appendTextString(str.getBytes());
  279. }
  280. /**
  281. * Append encoded string value to mMessage.
  282. * This implementation doesn't check the validity of parameter, since it
  283. * assumes that the values are validated in the GenericPdu setter methods.
  284. */
  285. protected void appendEncodedString(EncodedStringValue enStr) {
  286. /*
  287. * From OMA-TS-MMS-ENC-V1_3-20050927-C:
  288. * Encoded-string-value = Text-string | Value-length Char-set Text-string
  289. */
  290. assert(enStr != null);
  291. int charset = enStr.getCharacterSet();
  292. byte[] textString = enStr.getTextString();
  293. if (null == textString) {
  294. return;
  295. }
  296. /*
  297. * In the implementation of EncodedStringValue, the charset field will
  298. * never be 0. It will always be composed as
  299. * Encoded-string-value = Value-length Char-set Text-string
  300. */
  301. mStack.newbuf();
  302. PositionMarker start = mStack.mark();
  303. appendShortInteger(charset);
  304. appendTextString(textString);
  305. int len = start.getLength();
  306. mStack.pop();
  307. appendValueLength(len);
  308. mStack.copy();
  309. }
  310. /**
  311. * Append uintvar integer into mMessage.
  312. * This implementation doesn't check the validity of parameter, since it
  313. * assumes that the values are validated in the GenericPdu setter methods.
  314. */
  315. protected void appendUintvarInteger(long value) {
  316. /*
  317. * From WAP-230-WSP-20010705-a:
  318. * To encode a large unsigned integer, split it into 7-bit fragments
  319. * and place them in the payloads of multiple octets. The most significant
  320. * bits are placed in the first octets with the least significant bits
  321. * ending up in the last octet. All octets MUST set the Continue bit to 1
  322. * except the last octet, which MUST set the Continue bit to 0.
  323. */
  324. int i;
  325. long max = SHORT_INTEGER_MAX;
  326. for (i = 0; i < 5; i++) {
  327. if (value < max) {
  328. break;
  329. }
  330. max = (max << 7) | 0x7fl;
  331. }
  332. while(i > 0) {
  333. long temp = value >>> (i * 7);
  334. temp = temp & 0x7f;
  335. append((int)((temp | 0x80) & 0xff));
  336. i--;
  337. }
  338. append((int)(value & 0x7f));
  339. }
  340. /**
  341. * Append date value into mMessage.
  342. * This implementation doesn't check the validity of parameter, since it
  343. * assumes that the values are validated in the GenericPdu setter methods.
  344. */
  345. protected void appendDateValue(long date) {
  346. /*
  347. * From OMA-TS-MMS-ENC-V1_3-20050927-C:
  348. * Date-value = Long-integer
  349. */
  350. appendLongInteger(date);
  351. }
  352. /**
  353. * Append value length to mMessage.
  354. * This implementation doesn't check the validity of parameter, since it
  355. * assumes that the values are validated in the GenericPdu setter methods.
  356. */
  357. protected void appendValueLength(long value) {
  358. /*
  359. * From WAP-230-WSP-20010705-a:
  360. * Value-length = Short-length | (Length-quote Length)
  361. * ; Value length is used to indicate the length of the value to follow
  362. * Short-length = <Any octet 0-30>
  363. * Length-quote = <Octet 31>
  364. * Length = Uintvar-integer
  365. */
  366. if (value < LENGTH_QUOTE) {
  367. appendShortLength((int) value);
  368. return;
  369. }
  370. append(LENGTH_QUOTE);
  371. appendUintvarInteger(value);
  372. }
  373. /**
  374. * Append quoted string to mMessage.
  375. * This implementation doesn't check the validity of parameter, since it
  376. * assumes that the values are validated in the GenericPdu setter methods.
  377. */
  378. protected void appendQuotedString(byte[] text) {
  379. /*
  380. * From WAP-230-WSP-20010705-a:
  381. * Quoted-string = <Octet 34> *TEXT End-of-string
  382. * ;The TEXT encodes an RFC2616 Quoted-string with the enclosing
  383. * ;quotation-marks <"> removed.
  384. */
  385. append(QUOTED_STRING_FLAG);
  386. arraycopy(text, 0, text.length);
  387. append(END_STRING_FLAG);
  388. }
  389. /**
  390. * Append quoted string to mMessage.
  391. * This implementation doesn't check the validity of parameter, since it
  392. * assumes that the values are validated in the GenericPdu setter methods.
  393. */
  394. protected void appendQuotedString(String str) {
  395. /*
  396. * From WAP-230-WSP-20010705-a:
  397. * Quoted-string = <Octet 34> *TEXT End-of-string
  398. * ;The TEXT encodes an RFC2616 Quoted-string with the enclosing
  399. * ;quotation-marks <"> removed.
  400. */
  401. appendQuotedString(str.getBytes());
  402. }
  403. private EncodedStringValue appendAddressType(EncodedStringValue address) {
  404. EncodedStringValue temp = null;
  405. try {
  406. int addressType = checkAddressType(address.getString());
  407. temp = EncodedStringValue.copy(address);
  408. if (PDU_PHONE_NUMBER_ADDRESS_TYPE == addressType) {
  409. // Phone number.
  410. temp.appendTextString(STRING_PHONE_NUMBER_ADDRESS_TYPE.getBytes());
  411. } else if (PDU_IPV4_ADDRESS_TYPE == addressType) {
  412. // Ipv4 address.
  413. temp.appendTextString(STRING_IPV4_ADDRESS_TYPE.getBytes());
  414. } else if (PDU_IPV6_ADDRESS_TYPE == addressType) {
  415. // Ipv6 address.
  416. temp.appendTextString(STRING_IPV6_ADDRESS_TYPE.getBytes());
  417. }
  418. } catch (NullPointerException e) {
  419. return null;
  420. }
  421. return temp;
  422. }
  423. /**
  424. * Append header to mMessage.
  425. */
  426. private int appendHeader(int field) {
  427. switch (field) {
  428. case PduHeaders.MMS_VERSION:
  429. appendOctet(field);
  430. int version = mPduHeader.getOctet(field);
  431. if (0 == version) {
  432. appendShortInteger(PduHeaders.CURRENT_MMS_VERSION);
  433. } else {
  434. appendShortInteger(version);
  435. }
  436. break;
  437. case PduHeaders.MESSAGE_ID:
  438. case PduHeaders.TRANSACTION_ID:
  439. byte[] textString = mPduHeader.getTextString(field);
  440. if (null == textString) {
  441. return PDU_COMPOSE_FIELD_NOT_SET;
  442. }
  443. appendOctet(field);
  444. appendTextString(textString);
  445. break;
  446. case PduHeaders.TO:
  447. case PduHeaders.BCC:
  448. case PduHeaders.CC:
  449. EncodedStringValue[] addr = mPduHeader.getEncodedStringValues(field);
  450. if (null == addr) {
  451. return PDU_COMPOSE_FIELD_NOT_SET;
  452. }
  453. EncodedStringValue temp;
  454. for (int i = 0; i < addr.length; i++) {
  455. temp = appendAddressType(addr[i]);
  456. if (temp == null) {
  457. return PDU_COMPOSE_CONTENT_ERROR;
  458. }
  459. appendOctet(field);
  460. appendEncodedString(temp);
  461. }
  462. break;
  463. case PduHeaders.FROM:
  464. // Value-length (Address-present-token Encoded-string-value | Insert-address-token)
  465. appendOctet(field);
  466. EncodedStringValue from = mPduHeader.getEncodedStringValue(field);
  467. if ((from == null)
  468. || TextUtils.isEmpty(from.getString())
  469. || new String(from.getTextString()).equals(
  470. PduHeaders.FROM_INSERT_ADDRESS_TOKEN_STR)) {
  471. // Length of from = 1
  472. append(1);
  473. // Insert-address-token = <Octet 129>
  474. append(PduHeaders.FROM_INSERT_ADDRESS_TOKEN);
  475. } else {
  476. mStack.newbuf();
  477. PositionMarker fstart = mStack.mark();
  478. // Address-present-token = <Octet 128>
  479. append(PduHeaders.FROM_ADDRESS_PRESENT_TOKEN);
  480. temp = appendAddressType(from);
  481. if (temp == null) {
  482. return PDU_COMPOSE_CONTENT_ERROR;
  483. }
  484. appendEncodedString(temp);
  485. int flen = fstart.getLength();
  486. mStack.pop();
  487. appendValueLength(flen);
  488. mStack.copy();
  489. }
  490. break;
  491. case PduHeaders.READ_STATUS:
  492. case PduHeaders.STATUS:
  493. case PduHeaders.REPORT_ALLOWED:
  494. case PduHeaders.PRIORITY:
  495. case PduHeaders.DELIVERY_REPORT:
  496. case PduHeaders.READ_REPORT:
  497. int octet = mPduHeader.getOctet(field);
  498. if (0 == octet) {
  499. return PDU_COMPOSE_FIELD_NOT_SET;
  500. }
  501. appendOctet(field);
  502. appendOctet(octet);
  503. break;
  504. case PduHeaders.DATE:
  505. long date = mPduHeader.getLongInteger(field);
  506. if (-1 == date) {
  507. return PDU_COMPOSE_FIELD_NOT_SET;
  508. }
  509. appendOctet(field);
  510. appendDateValue(date);
  511. break;
  512. case PduHeaders.SUBJECT:
  513. EncodedStringValue enString =
  514. mPduHeader.getEncodedStringValue(field);
  515. if (null == enString) {
  516. return PDU_COMPOSE_FIELD_NOT_SET;
  517. }
  518. appendOctet(field);
  519. appendEncodedString(enString);
  520. break;
  521. case PduHeaders.MESSAGE_CLASS:
  522. byte[] messageClass = mPduHeader.getTextString(field);
  523. if (null == messageClass) {
  524. return PDU_COMPOSE_FIELD_NOT_SET;
  525. }
  526. appendOctet(field);
  527. if (Arrays.equals(messageClass,
  528. PduHeaders.MESSAGE_CLASS_ADVERTISEMENT_STR.getBytes())) {
  529. appendOctet(PduHeaders.MESSAGE_CLASS_ADVERTISEMENT);
  530. } else if (Arrays.equals(messageClass,
  531. PduHeaders.MESSAGE_CLASS_AUTO_STR.getBytes())) {
  532. appendOctet(PduHeaders.MESSAGE_CLASS_AUTO);
  533. } else if (Arrays.equals(messageClass,
  534. PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes())) {
  535. appendOctet(PduHeaders.MESSAGE_CLASS_PERSONAL);
  536. } else if (Arrays.equals(messageClass,
  537. PduHeaders.MESSAGE_CLASS_INFORMATIONAL_STR.getBytes())) {
  538. appendOctet(PduHeaders.MESSAGE_CLASS_INFORMATIONAL);
  539. } else {
  540. appendTextString(messageClass);
  541. }
  542. break;
  543. case PduHeaders.EXPIRY:
  544. long expiry = mPduHeader.getLongInteger(field);
  545. if (-1 == expiry) {
  546. return PDU_COMPOSE_FIELD_NOT_SET;
  547. }
  548. appendOctet(field);
  549. mStack.newbuf();
  550. PositionMarker expiryStart = mStack.mark();
  551. append(PduHeaders.VALUE_RELATIVE_TOKEN);
  552. appendLongInteger(expiry);
  553. int expiryLength = expiryStart.getLength();
  554. mStack.pop();
  555. appendValueLength(expiryLength);
  556. mStack.copy();
  557. break;
  558. default:
  559. return PDU_COMPOSE_FIELD_NOT_SUPPORTED;
  560. }
  561. return PDU_COMPOSE_SUCCESS;
  562. }
  563. /**
  564. * Make ReadRec.Ind.
  565. */
  566. private int makeReadRecInd() {
  567. if (mMessage == null) {
  568. mMessage = new ByteArrayOutputStream();
  569. mPosition = 0;
  570. }
  571. // X-Mms-Message-Type
  572. appendOctet(PduHeaders.MESSAGE_TYPE);
  573. appendOctet(PduHeaders.MESSAGE_TYPE_READ_REC_IND);
  574. // X-Mms-MMS-Version
  575. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  576. return PDU_COMPOSE_CONTENT_ERROR;
  577. }
  578. // Message-ID
  579. if (appendHeader(PduHeaders.MESSAGE_ID) != PDU_COMPOSE_SUCCESS) {
  580. return PDU_COMPOSE_CONTENT_ERROR;
  581. }
  582. // To
  583. if (appendHeader(PduHeaders.TO) != PDU_COMPOSE_SUCCESS) {
  584. return PDU_COMPOSE_CONTENT_ERROR;
  585. }
  586. // From
  587. if (appendHeader(PduHeaders.FROM) != PDU_COMPOSE_SUCCESS) {
  588. return PDU_COMPOSE_CONTENT_ERROR;
  589. }
  590. // Date Optional
  591. appendHeader(PduHeaders.DATE);
  592. // X-Mms-Read-Status
  593. if (appendHeader(PduHeaders.READ_STATUS) != PDU_COMPOSE_SUCCESS) {
  594. return PDU_COMPOSE_CONTENT_ERROR;
  595. }
  596. // X-Mms-Applic-ID Optional(not support)
  597. // X-Mms-Reply-Applic-ID Optional(not support)
  598. // X-Mms-Aux-Applic-Info Optional(not support)
  599. return PDU_COMPOSE_SUCCESS;
  600. }
  601. /**
  602. * Make NotifyResp.Ind.
  603. */
  604. private int makeNotifyResp() {
  605. if (mMessage == null) {
  606. mMessage = new ByteArrayOutputStream();
  607. mPosition = 0;
  608. }
  609. // X-Mms-Message-Type
  610. appendOctet(PduHeaders.MESSAGE_TYPE);
  611. appendOctet(PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND);
  612. // X-Mms-Transaction-ID
  613. if (appendHeader(PduHeaders.TRANSACTION_ID) != PDU_COMPOSE_SUCCESS) {
  614. return PDU_COMPOSE_CONTENT_ERROR;
  615. }
  616. // X-Mms-MMS-Version
  617. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  618. return PDU_COMPOSE_CONTENT_ERROR;
  619. }
  620. // X-Mms-Status
  621. if (appendHeader(PduHeaders.STATUS) != PDU_COMPOSE_SUCCESS) {
  622. return PDU_COMPOSE_CONTENT_ERROR;
  623. }
  624. // X-Mms-Report-Allowed Optional (not support)
  625. return PDU_COMPOSE_SUCCESS;
  626. }
  627. /**
  628. * Make Acknowledge.Ind.
  629. */
  630. private int makeAckInd() {
  631. if (mMessage == null) {
  632. mMessage = new ByteArrayOutputStream();
  633. mPosition = 0;
  634. }
  635. // X-Mms-Message-Type
  636. appendOctet(PduHeaders.MESSAGE_TYPE);
  637. appendOctet(PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND);
  638. // X-Mms-Transaction-ID
  639. if (appendHeader(PduHeaders.TRANSACTION_ID) != PDU_COMPOSE_SUCCESS) {
  640. return PDU_COMPOSE_CONTENT_ERROR;
  641. }
  642. // X-Mms-MMS-Version
  643. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  644. return PDU_COMPOSE_CONTENT_ERROR;
  645. }
  646. // X-Mms-Report-Allowed Optional
  647. appendHeader(PduHeaders.REPORT_ALLOWED);
  648. return PDU_COMPOSE_SUCCESS;
  649. }
  650. /**
  651. * Make Send.req.
  652. */
  653. private int makeSendReqPdu() {
  654. Log.w("PduComposer", "Making send request...");
  655. if (mMessage == null) {
  656. mMessage = new ByteArrayOutputStream();
  657. mPosition = 0;
  658. }
  659. // X-Mms-Message-Type
  660. appendOctet(PduHeaders.MESSAGE_TYPE);
  661. appendOctet(PduHeaders.MESSAGE_TYPE_SEND_REQ);
  662. // X-Mms-Transaction-ID
  663. appendOctet(PduHeaders.TRANSACTION_ID);
  664. byte[] trid = mPduHeader.getTextString(PduHeaders.TRANSACTION_ID);
  665. if (trid == null) {
  666. // Transaction-ID should be set(by Transaction) before make().
  667. throw new IllegalArgumentException("Transaction-ID is null.");
  668. }
  669. appendTextString(trid);
  670. // X-Mms-MMS-Version
  671. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  672. return PDU_COMPOSE_CONTENT_ERROR;
  673. }
  674. // Date Date-value Optional.
  675. appendHeader(PduHeaders.DATE);
  676. // From
  677. if (appendHeader(PduHeaders.FROM) != PDU_COMPOSE_SUCCESS) {
  678. return PDU_COMPOSE_CONTENT_ERROR;
  679. }
  680. boolean recipient = false;
  681. // To
  682. if (appendHeader(PduHeaders.TO) != PDU_COMPOSE_CONTENT_ERROR) {
  683. recipient = true;
  684. }
  685. // Cc
  686. if (appendHeader(PduHeaders.CC) != PDU_COMPOSE_CONTENT_ERROR) {
  687. recipient = true;
  688. }
  689. // Bcc
  690. if (appendHeader(PduHeaders.BCC) != PDU_COMPOSE_CONTENT_ERROR) {
  691. recipient = true;
  692. }
  693. // Need at least one of "cc", "bcc" and "to".
  694. if (false == recipient) {
  695. return PDU_COMPOSE_CONTENT_ERROR;
  696. }
  697. // Subject Optional
  698. appendHeader(PduHeaders.SUBJECT);
  699. // X-Mms-Message-Class Optional
  700. // Message-class-value = Class-identifier | Token-text
  701. appendHeader(PduHeaders.MESSAGE_CLASS);
  702. // X-Mms-Expiry Optional
  703. appendHeader(PduHeaders.EXPIRY);
  704. // X-Mms-Priority Optional
  705. appendHeader(PduHeaders.PRIORITY);
  706. // X-Mms-Delivery-Report Optional
  707. appendHeader(PduHeaders.DELIVERY_REPORT);
  708. // X-Mms-Read-Report Optional
  709. appendHeader(PduHeaders.READ_REPORT);
  710. // Content-Type
  711. appendOctet(PduHeaders.CONTENT_TYPE);
  712. // Message body
  713. return makeMessageBody();
  714. }
  715. /**
  716. * Make message body.
  717. */
  718. private int makeMessageBody() {
  719. Log.w("PduComposer", "Making message body...");
  720. // 1. add body informations
  721. mStack.newbuf(); // Switching buffer because we need to
  722. PositionMarker ctStart = mStack.mark();
  723. // This contentTypeIdentifier should be used for type of attachment...
  724. String contentType = new String(mPduHeader.getTextString(PduHeaders.CONTENT_TYPE));
  725. Integer contentTypeIdentifier = mContentTypeMap.get(contentType);
  726. if (contentTypeIdentifier == null) {
  727. // content type is mandatory
  728. return PDU_COMPOSE_CONTENT_ERROR;
  729. }
  730. appendShortInteger(contentTypeIdentifier.intValue());
  731. // content-type parameter: start
  732. PduBody body = ((SendReq) mPdu).getBody();
  733. if (null == body || body.getPartsNum() == 0) {
  734. // empty message
  735. appendUintvarInteger(0);
  736. mStack.pop();
  737. mStack.copy();
  738. return PDU_COMPOSE_SUCCESS;
  739. }
  740. PduPart part;
  741. try {
  742. part = body.getPart(0);
  743. byte[] start = part.getContentId();
  744. if (start != null) {
  745. appendOctet(PduPart.P_DEP_START);
  746. if (('<' == start[0]) && ('>' == start[start.length - 1])) {
  747. appendTextString(start);
  748. } else {
  749. appendTextString("<" + new String(start) + ">");
  750. }
  751. }
  752. // content-type parameter: type
  753. appendOctet(PduPart.P_CT_MR_TYPE);
  754. appendTextString(part.getContentType());
  755. }
  756. catch (ArrayIndexOutOfBoundsException e){
  757. e.printStackTrace();
  758. }
  759. int ctLength = ctStart.getLength();
  760. mStack.pop();
  761. appendValueLength(ctLength);
  762. mStack.copy();
  763. // 3. add content
  764. int partNum = body.getPartsNum();
  765. appendUintvarInteger(partNum);
  766. for (int i = 0; i < partNum; i++) {
  767. part = body.getPart(i);
  768. mStack.newbuf(); // Leaving space for header lengh and data length
  769. PositionMarker attachment = mStack.mark();
  770. mStack.newbuf(); // Leaving space for Content-Type length
  771. PositionMarker contentTypeBegin = mStack.mark();
  772. byte[] partContentType = part.getContentType();
  773. if (partContentType == null) {
  774. // content type is mandatory
  775. return PDU_COMPOSE_CONTENT_ERROR;
  776. }
  777. // content-type value
  778. Integer partContentTypeIdentifier =
  779. mContentTypeMap.get(new String(partContentType));
  780. if (partContentTypeIdentifier == null) {
  781. appendTextString(partContentType);
  782. } else {
  783. appendShortInteger(partContentTypeIdentifier.intValue());
  784. }
  785. /* Content-type parameter : name.
  786. * The value of name, filename, content-location is the same.
  787. * Just one of them is enough for this PDU.
  788. */
  789. byte[] name = part.getName();
  790. if (null == name) {
  791. name = part.getFilename();
  792. if (null == name) {
  793. name = part.getContentLocation();
  794. if (null == name) {
  795. /* at lease one of name, filename, Content-location
  796. * should be available.
  797. */
  798. return PDU_COMPOSE_CONTENT_ERROR;
  799. }
  800. }
  801. }
  802. appendOctet(PduPart.P_DEP_NAME);
  803. appendTextString(name);
  804. // content-type parameter : charset
  805. int charset = part.getCharset();
  806. if (charset != 0) {
  807. appendOctet(PduPart.P_CHARSET);
  808. appendShortInteger(charset);
  809. }
  810. int contentTypeLength = contentTypeBegin.getLength();
  811. mStack.pop();
  812. appendValueLength(contentTypeLength);
  813. mStack.copy();
  814. // content id
  815. byte[] contentId = part.getContentId();
  816. if (null != contentId) {
  817. appendOctet(PduPart.P_CONTENT_ID);
  818. if (('<' == contentId[0]) && ('>' == contentId[contentId.length - 1])) {
  819. appendQuotedString(contentId);
  820. } else {
  821. appendQuotedString("<" + new String(contentId) + ">");
  822. }
  823. }
  824. // content-location
  825. byte[] contentLocation = part.getContentLocation();
  826. if (null != contentLocation) {
  827. appendOctet(PduPart.P_CONTENT_LOCATION);
  828. appendTextString(contentLocation);
  829. }
  830. // content
  831. int headerLength = attachment.getLength();
  832. int dataLength = 0; // Just for safety...
  833. byte[] partData = part.getData();
  834. if (partData != null) {
  835. arraycopy(partData, 0, partData.length);
  836. dataLength = partData.length;
  837. } else {
  838. InputStream cr;
  839. try {
  840. byte[] buffer = new byte[PDU_COMPOSER_BLOCK_SIZE];
  841. cr = mResolver.openInputStream(part.getDataUri());
  842. int len = 0;
  843. while ((len = cr.read(buffer)) != -1) {
  844. mMessage.write(buffer, 0, len);
  845. mPosition += len;
  846. dataLength += len;
  847. }
  848. } catch (FileNotFoundException e) {
  849. return PDU_COMPOSE_CONTENT_ERROR;
  850. } catch (IOException e) {
  851. return PDU_COMPOSE_CONTENT_ERROR;
  852. } catch (RuntimeException e) {
  853. return PDU_COMPOSE_CONTENT_ERROR;
  854. }
  855. }
  856. if (dataLength != (attachment.getLength() - headerLength)) {
  857. throw new RuntimeException("BUG: Length sanity check failed");
  858. }
  859. mStack.pop();
  860. appendUintvarInteger(headerLength);
  861. appendUintvarInteger(dataLength);
  862. mStack.copy();
  863. }
  864. return PDU_COMPOSE_SUCCESS;
  865. }
  866. /**
  867. * Record current message informations.
  868. */
  869. static private class LengthRecordNode {
  870. ByteArrayOutputStream currentMessage = null;
  871. public int currentPosition = 0;
  872. public LengthRecordNode next = null;
  873. }
  874. /**
  875. * Mark current message position and stact size.
  876. */
  877. private class PositionMarker {
  878. private int c_pos; // Current position
  879. private int currentStackSize; // Current stack size
  880. int getLength() {
  881. // If these assert fails, likely that you are finding the
  882. // size of buffer that is deep in BufferStack you can only
  883. // find the length of the buffer that is on top
  884. if (currentStackSize != mStack.stackSize) {
  885. throw new RuntimeException("BUG: Invalid call to getLength()");
  886. }
  887. return mPosition - c_pos;
  888. }
  889. }
  890. /**
  891. * This implementation can be OPTIMIZED to use only
  892. * 2 buffers. This optimization involves changing BufferStack
  893. * only... Its usage (interface) will not change.
  894. */
  895. private class BufferStack {
  896. private LengthRecordNode stack = null;
  897. private LengthRecordNode toCopy = null;
  898. int stackSize = 0;
  899. /**
  900. * Create a new message buffer and push it into the stack.
  901. */
  902. void newbuf() {
  903. // You can't create a new buff when toCopy != null
  904. // That is after calling pop() and before calling copy()
  905. // If you do, it is a bug
  906. if (toCopy != null) {
  907. throw new RuntimeException("BUG: Invalid newbuf() before copy()");
  908. }
  909. LengthRecordNode temp = new LengthRecordNode();
  910. temp.currentMessage = mMessage;
  911. temp.currentPosition = mPosition;
  912. temp.next = stack;
  913. stack = temp;
  914. stackSize = stackSize + 1;
  915. mMessage = new ByteArrayOutputStream();
  916. mPosition = 0;
  917. }
  918. /**
  919. * Pop the message before and record current message in the stack.
  920. */
  921. void pop() {
  922. ByteArrayOutputStream currentMessage = mMessage;
  923. int currentPosition = mPosition;
  924. mMessage = stack.currentMessage;
  925. mPosition = stack.currentPosition;
  926. toCopy = stack;
  927. // Re using the top element of the stack to avoid memory allocation
  928. stack = stack.next;
  929. stackSize = stackSize - 1;
  930. toCopy.currentMessage = currentMessage;
  931. toCopy.currentPosition = currentPosition;
  932. }
  933. /**
  934. * Append current message to the message before.
  935. */
  936. void copy() {
  937. arraycopy(toCopy.currentMessage.toByteArray(), 0,
  938. toCopy.currentPosition);
  939. toCopy = null;
  940. }
  941. /**
  942. * Mark current message position
  943. */
  944. PositionMarker mark() {
  945. PositionMarker m = new PositionMarker();
  946. m.c_pos = mPosition;
  947. m.currentStackSize = stackSize;
  948. return m;
  949. }
  950. }
  951. /**
  952. * Check address type.
  953. *
  954. * @param address address string without the postfix stinng type,
  955. * such as "/TYPE=PLMN", "/TYPE=IPv6" and "/TYPE=IPv4"
  956. * @return PDU_PHONE_NUMBER_ADDRESS_TYPE if it is phone number,
  957. * PDU_EMAIL_ADDRESS_TYPE if it is email address,
  958. * PDU_IPV4_ADDRESS_TYPE if it is ipv4 address,
  959. * PDU_IPV6_ADDRESS_TYPE if it is ipv6 address,
  960. * PDU_UNKNOWN_ADDRESS_TYPE if it is unknown.
  961. */
  962. protected static int checkAddressType(String address) {
  963. /**
  964. * From OMA-TS-MMS-ENC-V1_3-20050927-C.pdf, section 8.
  965. * address = ( e-mail / device-address / alphanum-shortcode / num-shortcode)
  966. * e-mail = mailbox; to the definition of mailbox as described in
  967. * section 3.4 of [RFC2822], but excluding the
  968. * obsolete definitions as indicated by the "obs-" prefix.
  969. * device-address = ( global-phone-number "/TYPE=PLMN" )
  970. * / ( ipv4 "/TYPE=IPv4" ) / ( ipv6 "/TYPE=IPv6" )
  971. * / ( escaped-value "/TYPE=" address-type )
  972. *
  973. * global-phone-number = ["+"] 1*( DIGIT / written-sep )
  974. * written-sep =("-"/".")
  975. *
  976. * ipv4 = 1*3DIGIT 3( "." 1*3DIGIT ) ; IPv4 address value
  977. *
  978. * ipv6 = 4HEXDIG 7( ":" 4HEXDIG ) ; IPv6 address per RFC 2373
  979. */
  980. if (null == address) {
  981. return PDU_UNKNOWN_ADDRESS_TYPE;
  982. }
  983. if (address.matches(REGEXP_IPV4_ADDRESS_TYPE)) {
  984. // Ipv4 address.
  985. return PDU_IPV4_ADDRESS_TYPE;
  986. }else if (address.matches(REGEXP_PHONE_NUMBER_ADDRESS_TYPE)) {
  987. // Phone number.
  988. return PDU_PHONE_NUMBER_ADDRESS_TYPE;
  989. } else if (address.matches(REGEXP_EMAIL_ADDRESS_TYPE)) {
  990. // Email address.
  991. return PDU_EMAIL_ADDRESS_TYPE;
  992. } else if (address.matches(REGEXP_IPV6_ADDRESS_TYPE)) {
  993. // Ipv6 address.
  994. return PDU_IPV6_ADDRESS_TYPE;
  995. } else {
  996. // Unknown address.
  997. return PDU_UNKNOWN_ADDRESS_TYPE;
  998. }
  999. }
  1000. }