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