| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package com.google.android.mms.pdu; |
| 19 |
|
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.ContentResolver; |
| 22 |
import android.content.Context; |
| 23 |
import android.text.TextUtils; |
| 24 |
|
| 25 |
import java.io.ByteArrayOutputStream; |
| 26 |
import java.io.FileNotFoundException; |
| 27 |
import java.io.IOException; |
| 28 |
import java.io.InputStream; |
| 29 |
import java.util.Arrays; |
| 30 |
import java.util.HashMap; |
| 31 |
|
| 32 |
public class PduComposer { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static private final int PDU_PHONE_NUMBER_ADDRESS_TYPE = 1; |
| 37 |
static private final int PDU_EMAIL_ADDRESS_TYPE = 2; |
| 38 |
static private final int PDU_IPV4_ADDRESS_TYPE = 3; |
| 39 |
static private final int PDU_IPV6_ADDRESS_TYPE = 4; |
| 40 |
static private final int PDU_UNKNOWN_ADDRESS_TYPE = 5; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static final String REGEXP_PHONE_NUMBER_ADDRESS_TYPE = "\\+?[0-9|\\.|\\-]+"; |
| 46 |
static final String REGEXP_EMAIL_ADDRESS_TYPE = "[a-zA-Z| ]*\\<{0,1}[a-zA-Z| ]+@{1}" + |
| 47 |
"[a-zA-Z| ]+\\.{1}[a-zA-Z| ]+\\>{0,1}"; |
| 48 |
static final String REGEXP_IPV6_ADDRESS_TYPE = |
| 49 |
"[a-fA-F]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}" + |
| 50 |
"[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}" + |
| 51 |
"[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}"; |
| 52 |
static final String REGEXP_IPV4_ADDRESS_TYPE = "[0-9]{1,3}\\.{1}[0-9]{1,3}\\.{1}" + |
| 53 |
"[0-9]{1,3}\\.{1}[0-9]{1,3}"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static final String STRING_PHONE_NUMBER_ADDRESS_TYPE = "/TYPE=PLMN"; |
| 59 |
static final String STRING_IPV4_ADDRESS_TYPE = "/TYPE=IPV4"; |
| 60 |
static final String STRING_IPV6_ADDRESS_TYPE = "/TYPE=IPV6"; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static private final int PDU_COMPOSE_SUCCESS = 0; |
| 66 |
static private final int PDU_COMPOSE_CONTENT_ERROR = 1; |
| 67 |
static private final int PDU_COMPOSE_FIELD_NOT_SET = 2; |
| 68 |
static private final int PDU_COMPOSE_FIELD_NOT_SUPPORTED = 3; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static private final int QUOTED_STRING_FLAG = 34; |
| 74 |
static private final int END_STRING_FLAG = 0; |
| 75 |
static private final int LENGTH_QUOTE = 31; |
| 76 |
static private final int TEXT_MAX = 127; |
| 77 |
static private final int SHORT_INTEGER_MAX = 127; |
| 78 |
static private final int LONG_INTEGER_LENGTH_MAX = 8; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static private final int PDU_COMPOSER_BLOCK_SIZE = 1024; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@UnsupportedAppUsage |
| 89 |
protected ByteArrayOutputStream mMessage = null; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
@UnsupportedAppUsage |
| 95 |
private GenericPdu mPdu = null; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@UnsupportedAppUsage |
| 101 |
protected int mPosition = 0; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@UnsupportedAppUsage |
| 107 |
private BufferStack mStack = null; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@UnsupportedAppUsage |
| 113 |
private final ContentResolver mResolver; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@UnsupportedAppUsage |
| 119 |
private PduHeaders mPduHeader = null; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@UnsupportedAppUsage |
| 125 |
private static HashMap<String, Integer> mContentTypeMap = null; |
| 126 |
|
| 127 |
static { |
| 128 |
mContentTypeMap = new HashMap<String, Integer>(); |
| 129 |
|
| 130 |
int i; |
| 131 |
for (i = 0; i < PduContentTypes.contentTypes.length; i++) { |
| 132 |
mContentTypeMap.put(PduContentTypes.contentTypes[i], i); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@UnsupportedAppUsage |
| 143 |
public PduComposer(Context context, GenericPdu pdu) { |
| 144 |
mPdu = pdu; |
| 145 |
mResolver = context.getContentResolver(); |
| 146 |
mPduHeader = pdu.getPduHeaders(); |
| 147 |
mStack = new BufferStack(); |
| 148 |
mMessage = new ByteArrayOutputStream(); |
| 149 |
mPosition = 0; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@UnsupportedAppUsage |
| 160 |
public byte[] make() { |
| 161 |
|
| 162 |
int type = mPdu.getMessageType(); |
| 163 |
|
| 164 |
|
| 165 |
switch (type) { |
| 166 |
case PduHeaders.MESSAGE_TYPE_SEND_REQ: |
| 167 |
case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF: |
| 168 |
if (makeSendRetrievePdu(type) != PDU_COMPOSE_SUCCESS) { |
| 169 |
return null; |
| 170 |
} |
| 171 |
break; |
| 172 |
case PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND: |
| 173 |
if (makeNotifyResp() != PDU_COMPOSE_SUCCESS) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
break; |
| 177 |
case PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND: |
| 178 |
if (makeAckInd() != PDU_COMPOSE_SUCCESS) { |
| 179 |
return null; |
| 180 |
} |
| 181 |
break; |
| 182 |
case PduHeaders.MESSAGE_TYPE_READ_REC_IND: |
| 183 |
if (makeReadRecInd() != PDU_COMPOSE_SUCCESS) { |
| 184 |
return null; |
| 185 |
} |
| 186 |
break; |
| 187 |
default: |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
return mMessage.toByteArray(); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@UnsupportedAppUsage |
| 198 |
protected void arraycopy(byte[] buf, int pos, int length) { |
| 199 |
mMessage.write(buf, pos, length); |
| 200 |
mPosition = mPosition + length; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
protected void append(int value) { |
| 207 |
mMessage.write(value); |
| 208 |
mPosition ++; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@UnsupportedAppUsage |
| 217 |
protected void appendShortInteger(int value) { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
append((value | 0x80) & 0xff); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
@UnsupportedAppUsage |
| 238 |
protected void appendOctet(int number) { |
| 239 |
append(number); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
protected void appendShortLength(int value) { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
append(value); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
@UnsupportedAppUsage |
| 261 |
protected void appendLongInteger(long longInt) { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
int size; |
| 272 |
long temp = longInt; |
| 273 |
|
| 274 |
|
| 275 |
for(size = 0; (temp != 0) && (size < LONG_INTEGER_LENGTH_MAX); size++) { |
| 276 |
temp = (temp >>> 8); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
appendShortLength(size); |
| 281 |
|
| 282 |
|
| 283 |
int i; |
| 284 |
int shift = (size -1) * 8; |
| 285 |
|
| 286 |
for (i = 0; i < size; i++) { |
| 287 |
append((int)((longInt >>> shift) & 0xff)); |
| 288 |
shift = shift - 8; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
@UnsupportedAppUsage |
| 298 |
protected void appendTextString(byte[] text) { |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if (((text[0])&0xff) > TEXT_MAX) { |
| 307 |
append(TEXT_MAX); |
| 308 |
} |
| 309 |
|
| 310 |
arraycopy(text, 0, text.length); |
| 311 |
append(0); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
@UnsupportedAppUsage |
| 320 |
protected void appendTextString(String str) { |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
appendTextString(str.getBytes()); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@UnsupportedAppUsage |
| 337 |
protected void appendEncodedString(EncodedStringValue enStr) { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
assert(enStr != null); |
| 343 |
|
| 344 |
int charset = enStr.getCharacterSet(); |
| 345 |
byte[] textString = enStr.getTextString(); |
| 346 |
if (null == textString) { |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
mStack.newbuf(); |
| 356 |
PositionMarker start = mStack.mark(); |
| 357 |
|
| 358 |
appendShortInteger(charset); |
| 359 |
appendTextString(textString); |
| 360 |
|
| 361 |
int len = start.getLength(); |
| 362 |
mStack.pop(); |
| 363 |
appendValueLength(len); |
| 364 |
mStack.copy(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
@UnsupportedAppUsage |
| 373 |
protected void appendUintvarInteger(long value) { |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
int i; |
| 383 |
long max = SHORT_INTEGER_MAX; |
| 384 |
|
| 385 |
for (i = 0; i < 5; i++) { |
| 386 |
if (value < max) { |
| 387 |
break; |
| 388 |
} |
| 389 |
|
| 390 |
max = (max << 7) | 0x7fl; |
| 391 |
} |
| 392 |
|
| 393 |
while(i > 0) { |
| 394 |
long temp = value >>> (i * 7); |
| 395 |
temp = temp & 0x7f; |
| 396 |
|
| 397 |
append((int)((temp | 0x80) & 0xff)); |
| 398 |
|
| 399 |
i--; |
| 400 |
} |
| 401 |
|
| 402 |
append((int)(value & 0x7f)); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
protected void appendDateValue(long date) { |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
appendLongInteger(date); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
@UnsupportedAppUsage |
| 424 |
protected void appendValueLength(long value) { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if (value < LENGTH_QUOTE) { |
| 434 |
appendShortLength((int) value); |
| 435 |
return; |
| 436 |
} |
| 437 |
|
| 438 |
append(LENGTH_QUOTE); |
| 439 |
appendUintvarInteger(value); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
@UnsupportedAppUsage |
| 448 |
protected void appendQuotedString(byte[] text) { |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
append(QUOTED_STRING_FLAG); |
| 456 |
arraycopy(text, 0, text.length); |
| 457 |
append(END_STRING_FLAG); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
@UnsupportedAppUsage |
| 466 |
protected void appendQuotedString(String str) { |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
appendQuotedString(str.getBytes()); |
| 474 |
} |
| 475 |
|
| 476 |
private EncodedStringValue appendAddressType(EncodedStringValue address) { |
| 477 |
EncodedStringValue temp = null; |
| 478 |
|
| 479 |
try { |
| 480 |
int addressType = checkAddressType(address.getString()); |
| 481 |
temp = EncodedStringValue.copy(address); |
| 482 |
if (PDU_PHONE_NUMBER_ADDRESS_TYPE == addressType) { |
| 483 |
|
| 484 |
temp.appendTextString(STRING_PHONE_NUMBER_ADDRESS_TYPE.getBytes()); |
| 485 |
} else if (PDU_IPV4_ADDRESS_TYPE == addressType) { |
| 486 |
|
| 487 |
temp.appendTextString(STRING_IPV4_ADDRESS_TYPE.getBytes()); |
| 488 |
} else if (PDU_IPV6_ADDRESS_TYPE == addressType) { |
| 489 |
|
| 490 |
temp.appendTextString(STRING_IPV6_ADDRESS_TYPE.getBytes()); |
| 491 |
} |
| 492 |
} catch (NullPointerException e) { |
| 493 |
return null; |
| 494 |
} |
| 495 |
|
| 496 |
return temp; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
@UnsupportedAppUsage |
| 503 |
private int appendHeader(int field) { |
| 504 |
switch (field) { |
| 505 |
case PduHeaders.MMS_VERSION: |
| 506 |
appendOctet(field); |
| 507 |
|
| 508 |
int version = mPduHeader.getOctet(field); |
| 509 |
if (0 == version) { |
| 510 |
appendShortInteger(PduHeaders.CURRENT_MMS_VERSION); |
| 511 |
} else { |
| 512 |
appendShortInteger(version); |
| 513 |
} |
| 514 |
|
| 515 |
break; |
| 516 |
|
| 517 |
case PduHeaders.MESSAGE_ID: |
| 518 |
case PduHeaders.TRANSACTION_ID: |
| 519 |
byte[] textString = mPduHeader.getTextString(field); |
| 520 |
if (null == textString) { |
| 521 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 522 |
} |
| 523 |
|
| 524 |
appendOctet(field); |
| 525 |
appendTextString(textString); |
| 526 |
break; |
| 527 |
|
| 528 |
case PduHeaders.TO: |
| 529 |
case PduHeaders.BCC: |
| 530 |
case PduHeaders.CC: |
| 531 |
EncodedStringValue[] addr = mPduHeader.getEncodedStringValues(field); |
| 532 |
|
| 533 |
if (null == addr) { |
| 534 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 535 |
} |
| 536 |
|
| 537 |
EncodedStringValue temp; |
| 538 |
for (int i = 0; i < addr.length; i++) { |
| 539 |
temp = appendAddressType(addr[i]); |
| 540 |
if (temp == null) { |
| 541 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 542 |
} |
| 543 |
|
| 544 |
appendOctet(field); |
| 545 |
appendEncodedString(temp); |
| 546 |
} |
| 547 |
break; |
| 548 |
|
| 549 |
case PduHeaders.FROM: |
| 550 |
|
| 551 |
appendOctet(field); |
| 552 |
|
| 553 |
EncodedStringValue from = mPduHeader.getEncodedStringValue(field); |
| 554 |
if ((from == null) |
| 555 |
|| TextUtils.isEmpty(from.getString()) |
| 556 |
|| new String(from.getTextString()).equals( |
| 557 |
PduHeaders.FROM_INSERT_ADDRESS_TOKEN_STR)) { |
| 558 |
|
| 559 |
append(1); |
| 560 |
|
| 561 |
append(PduHeaders.FROM_INSERT_ADDRESS_TOKEN); |
| 562 |
} else { |
| 563 |
mStack.newbuf(); |
| 564 |
PositionMarker fstart = mStack.mark(); |
| 565 |
|
| 566 |
|
| 567 |
append(PduHeaders.FROM_ADDRESS_PRESENT_TOKEN); |
| 568 |
|
| 569 |
temp = appendAddressType(from); |
| 570 |
if (temp == null) { |
| 571 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 572 |
} |
| 573 |
|
| 574 |
appendEncodedString(temp); |
| 575 |
|
| 576 |
int flen = fstart.getLength(); |
| 577 |
mStack.pop(); |
| 578 |
appendValueLength(flen); |
| 579 |
mStack.copy(); |
| 580 |
} |
| 581 |
break; |
| 582 |
|
| 583 |
case PduHeaders.READ_STATUS: |
| 584 |
case PduHeaders.STATUS: |
| 585 |
case PduHeaders.REPORT_ALLOWED: |
| 586 |
case PduHeaders.PRIORITY: |
| 587 |
case PduHeaders.DELIVERY_REPORT: |
| 588 |
case PduHeaders.READ_REPORT: |
| 589 |
case PduHeaders.RETRIEVE_STATUS: |
| 590 |
int octet = mPduHeader.getOctet(field); |
| 591 |
if (0 == octet) { |
| 592 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 593 |
} |
| 594 |
|
| 595 |
appendOctet(field); |
| 596 |
appendOctet(octet); |
| 597 |
break; |
| 598 |
|
| 599 |
case PduHeaders.DATE: |
| 600 |
long date = mPduHeader.getLongInteger(field); |
| 601 |
if (-1 == date) { |
| 602 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 603 |
} |
| 604 |
|
| 605 |
appendOctet(field); |
| 606 |
appendDateValue(date); |
| 607 |
break; |
| 608 |
|
| 609 |
case PduHeaders.SUBJECT: |
| 610 |
case PduHeaders.RETRIEVE_TEXT: |
| 611 |
EncodedStringValue enString = |
| 612 |
mPduHeader.getEncodedStringValue(field); |
| 613 |
if (null == enString) { |
| 614 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 615 |
} |
| 616 |
|
| 617 |
appendOctet(field); |
| 618 |
appendEncodedString(enString); |
| 619 |
break; |
| 620 |
|
| 621 |
case PduHeaders.MESSAGE_CLASS: |
| 622 |
byte[] messageClass = mPduHeader.getTextString(field); |
| 623 |
if (null == messageClass) { |
| 624 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 625 |
} |
| 626 |
|
| 627 |
appendOctet(field); |
| 628 |
if (Arrays.equals(messageClass, |
| 629 |
PduHeaders.MESSAGE_CLASS_ADVERTISEMENT_STR.getBytes())) { |
| 630 |
appendOctet(PduHeaders.MESSAGE_CLASS_ADVERTISEMENT); |
| 631 |
} else if (Arrays.equals(messageClass, |
| 632 |
PduHeaders.MESSAGE_CLASS_AUTO_STR.getBytes())) { |
| 633 |
appendOctet(PduHeaders.MESSAGE_CLASS_AUTO); |
| 634 |
} else if (Arrays.equals(messageClass, |
| 635 |
PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes())) { |
| 636 |
appendOctet(PduHeaders.MESSAGE_CLASS_PERSONAL); |
| 637 |
} else if (Arrays.equals(messageClass, |
| 638 |
PduHeaders.MESSAGE_CLASS_INFORMATIONAL_STR.getBytes())) { |
| 639 |
appendOctet(PduHeaders.MESSAGE_CLASS_INFORMATIONAL); |
| 640 |
} else { |
| 641 |
appendTextString(messageClass); |
| 642 |
} |
| 643 |
break; |
| 644 |
|
| 645 |
case PduHeaders.EXPIRY: |
| 646 |
long expiry = mPduHeader.getLongInteger(field); |
| 647 |
if (-1 == expiry) { |
| 648 |
return PDU_COMPOSE_FIELD_NOT_SET; |
| 649 |
} |
| 650 |
|
| 651 |
appendOctet(field); |
| 652 |
|
| 653 |
mStack.newbuf(); |
| 654 |
PositionMarker expiryStart = mStack.mark(); |
| 655 |
|
| 656 |
append(PduHeaders.VALUE_RELATIVE_TOKEN); |
| 657 |
appendLongInteger(expiry); |
| 658 |
|
| 659 |
int expiryLength = expiryStart.getLength(); |
| 660 |
mStack.pop(); |
| 661 |
appendValueLength(expiryLength); |
| 662 |
mStack.copy(); |
| 663 |
break; |
| 664 |
|
| 665 |
default: |
| 666 |
return PDU_COMPOSE_FIELD_NOT_SUPPORTED; |
| 667 |
} |
| 668 |
|
| 669 |
return PDU_COMPOSE_SUCCESS; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
private int makeReadRecInd() { |
| 676 |
if (mMessage == null) { |
| 677 |
mMessage = new ByteArrayOutputStream(); |
| 678 |
mPosition = 0; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
appendOctet(PduHeaders.MESSAGE_TYPE); |
| 683 |
appendOctet(PduHeaders.MESSAGE_TYPE_READ_REC_IND); |
| 684 |
|
| 685 |
|
| 686 |
if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) { |
| 687 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
if (appendHeader(PduHeaders.MESSAGE_ID) != PDU_COMPOSE_SUCCESS) { |
| 692 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
if (appendHeader(PduHeaders.TO) != PDU_COMPOSE_SUCCESS) { |
| 697 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
if (appendHeader(PduHeaders.FROM) != PDU_COMPOSE_SUCCESS) { |
| 702 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
appendHeader(PduHeaders.DATE); |
| 707 |
|
| 708 |
|
| 709 |
if (appendHeader(PduHeaders.READ_STATUS) != PDU_COMPOSE_SUCCESS) { |
| 710 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
return PDU_COMPOSE_SUCCESS; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
private int makeNotifyResp() { |
| 724 |
if (mMessage == null) { |
| 725 |
mMessage = new ByteArrayOutputStream(); |
| 726 |
mPosition = 0; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
appendOctet(PduHeaders.MESSAGE_TYPE); |
| 731 |
appendOctet(PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND); |
| 732 |
|
| 733 |
|
| 734 |
if (appendHeader(PduHeaders.TRANSACTION_ID) != PDU_COMPOSE_SUCCESS) { |
| 735 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) { |
| 740 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
if (appendHeader(PduHeaders.STATUS) != PDU_COMPOSE_SUCCESS) { |
| 745 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
appendHeader(PduHeaders.REPORT_ALLOWED); |
| 750 |
|
| 751 |
return PDU_COMPOSE_SUCCESS; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
private int makeAckInd() { |
| 758 |
if (mMessage == null) { |
| 759 |
mMessage = new ByteArrayOutputStream(); |
| 760 |
mPosition = 0; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
appendOctet(PduHeaders.MESSAGE_TYPE); |
| 765 |
appendOctet(PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND); |
| 766 |
|
| 767 |
|
| 768 |
if (appendHeader(PduHeaders.TRANSACTION_ID) != PDU_COMPOSE_SUCCESS) { |
| 769 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) { |
| 774 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
appendHeader(PduHeaders.REPORT_ALLOWED); |
| 779 |
|
| 780 |
return PDU_COMPOSE_SUCCESS; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
private int makeSendRetrievePdu(int type) { |
| 787 |
if (mMessage == null) { |
| 788 |
mMessage = new ByteArrayOutputStream(); |
| 789 |
mPosition = 0; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
appendOctet(PduHeaders.MESSAGE_TYPE); |
| 794 |
appendOctet(type); |
| 795 |
|
| 796 |
|
| 797 |
appendOctet(PduHeaders.TRANSACTION_ID); |
| 798 |
|
| 799 |
byte[] trid = mPduHeader.getTextString(PduHeaders.TRANSACTION_ID); |
| 800 |
if (trid == null) { |
| 801 |
|
| 802 |
throw new IllegalArgumentException("Transaction-ID is null."); |
| 803 |
} |
| 804 |
appendTextString(trid); |
| 805 |
|
| 806 |
|
| 807 |
if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) { |
| 808 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
appendHeader(PduHeaders.DATE); |
| 813 |
|
| 814 |
|
| 815 |
if (appendHeader(PduHeaders.FROM) != PDU_COMPOSE_SUCCESS) { |
| 816 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 817 |
} |
| 818 |
|
| 819 |
boolean recipient = false; |
| 820 |
|
| 821 |
|
| 822 |
if (appendHeader(PduHeaders.TO) != PDU_COMPOSE_CONTENT_ERROR) { |
| 823 |
recipient = true; |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
if (appendHeader(PduHeaders.CC) != PDU_COMPOSE_CONTENT_ERROR) { |
| 828 |
recipient = true; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
if (appendHeader(PduHeaders.BCC) != PDU_COMPOSE_CONTENT_ERROR) { |
| 833 |
recipient = true; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
if (false == recipient) { |
| 838 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
appendHeader(PduHeaders.SUBJECT); |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
appendHeader(PduHeaders.MESSAGE_CLASS); |
| 847 |
|
| 848 |
|
| 849 |
appendHeader(PduHeaders.EXPIRY); |
| 850 |
|
| 851 |
|
| 852 |
appendHeader(PduHeaders.PRIORITY); |
| 853 |
|
| 854 |
|
| 855 |
appendHeader(PduHeaders.DELIVERY_REPORT); |
| 856 |
|
| 857 |
|
| 858 |
appendHeader(PduHeaders.READ_REPORT); |
| 859 |
|
| 860 |
if (type == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) { |
| 861 |
|
| 862 |
appendHeader(PduHeaders.RETRIEVE_STATUS); |
| 863 |
|
| 864 |
appendHeader(PduHeaders.RETRIEVE_TEXT); |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
appendOctet(PduHeaders.CONTENT_TYPE); |
| 870 |
|
| 871 |
|
| 872 |
return makeMessageBody(type); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
private int makeMessageBody(int type) { |
| 879 |
|
| 880 |
mStack.newbuf(); |
| 881 |
|
| 882 |
PositionMarker ctStart = mStack.mark(); |
| 883 |
|
| 884 |
|
| 885 |
String contentType = new String(mPduHeader.getTextString(PduHeaders.CONTENT_TYPE)); |
| 886 |
Integer contentTypeIdentifier = mContentTypeMap.get(contentType); |
| 887 |
if (contentTypeIdentifier == null) { |
| 888 |
|
| 889 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 890 |
} |
| 891 |
|
| 892 |
appendShortInteger(contentTypeIdentifier.intValue()); |
| 893 |
|
| 894 |
|
| 895 |
PduBody body; |
| 896 |
if (type == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) { |
| 897 |
body = ((RetrieveConf) mPdu).getBody(); |
| 898 |
} else { |
| 899 |
body = ((SendReq) mPdu).getBody(); |
| 900 |
} |
| 901 |
if (null == body || body.getPartsNum() == 0) { |
| 902 |
|
| 903 |
appendUintvarInteger(0); |
| 904 |
mStack.pop(); |
| 905 |
mStack.copy(); |
| 906 |
return PDU_COMPOSE_SUCCESS; |
| 907 |
} |
| 908 |
|
| 909 |
PduPart part; |
| 910 |
try { |
| 911 |
part = body.getPart(0); |
| 912 |
|
| 913 |
byte[] start = part.getContentId(); |
| 914 |
if (start != null) { |
| 915 |
appendOctet(PduPart.P_DEP_START); |
| 916 |
if (('<' == start[0]) && ('>' == start[start.length - 1])) { |
| 917 |
appendTextString(start); |
| 918 |
} else { |
| 919 |
appendTextString("<" + new String(start) + ">"); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
appendOctet(PduPart.P_CT_MR_TYPE); |
| 925 |
appendTextString(part.getContentType()); |
| 926 |
} |
| 927 |
catch (ArrayIndexOutOfBoundsException e){ |
| 928 |
e.printStackTrace(); |
| 929 |
} |
| 930 |
|
| 931 |
int ctLength = ctStart.getLength(); |
| 932 |
mStack.pop(); |
| 933 |
appendValueLength(ctLength); |
| 934 |
mStack.copy(); |
| 935 |
|
| 936 |
|
| 937 |
int partNum = body.getPartsNum(); |
| 938 |
appendUintvarInteger(partNum); |
| 939 |
for (int i = 0; i < partNum; i++) { |
| 940 |
part = body.getPart(i); |
| 941 |
mStack.newbuf(); |
| 942 |
PositionMarker attachment = mStack.mark(); |
| 943 |
|
| 944 |
mStack.newbuf(); |
| 945 |
PositionMarker contentTypeBegin = mStack.mark(); |
| 946 |
|
| 947 |
byte[] partContentType = part.getContentType(); |
| 948 |
|
| 949 |
if (partContentType == null) { |
| 950 |
|
| 951 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
Integer partContentTypeIdentifier = |
| 956 |
mContentTypeMap.get(new String(partContentType)); |
| 957 |
if (partContentTypeIdentifier == null) { |
| 958 |
appendTextString(partContentType); |
| 959 |
} else { |
| 960 |
appendShortInteger(partContentTypeIdentifier.intValue()); |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
byte[] name = part.getName(); |
| 968 |
|
| 969 |
if (null == name) { |
| 970 |
name = part.getFilename(); |
| 971 |
|
| 972 |
if (null == name) { |
| 973 |
name = part.getContentLocation(); |
| 974 |
|
| 975 |
if (null == name) { |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
appendOctet(PduPart.P_DEP_NAME); |
| 984 |
appendTextString(name); |
| 985 |
|
| 986 |
|
| 987 |
int charset = part.getCharset(); |
| 988 |
if (charset != 0) { |
| 989 |
appendOctet(PduPart.P_CHARSET); |
| 990 |
appendShortInteger(charset); |
| 991 |
} |
| 992 |
|
| 993 |
int contentTypeLength = contentTypeBegin.getLength(); |
| 994 |
mStack.pop(); |
| 995 |
appendValueLength(contentTypeLength); |
| 996 |
mStack.copy(); |
| 997 |
|
| 998 |
|
| 999 |
byte[] contentId = part.getContentId(); |
| 1000 |
|
| 1001 |
if (null != contentId) { |
| 1002 |
appendOctet(PduPart.P_CONTENT_ID); |
| 1003 |
if (('<' == contentId[0]) && ('>' == contentId[contentId.length - 1])) { |
| 1004 |
appendQuotedString(contentId); |
| 1005 |
} else { |
| 1006 |
appendQuotedString("<" + new String(contentId) + ">"); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
byte[] contentLocation = part.getContentLocation(); |
| 1012 |
if (null != contentLocation) { |
| 1013 |
appendOctet(PduPart.P_CONTENT_LOCATION); |
| 1014 |
appendTextString(contentLocation); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
int headerLength = attachment.getLength(); |
| 1019 |
|
| 1020 |
int dataLength = 0; |
| 1021 |
byte[] partData = part.getData(); |
| 1022 |
|
| 1023 |
if (partData != null) { |
| 1024 |
arraycopy(partData, 0, partData.length); |
| 1025 |
dataLength = partData.length; |
| 1026 |
} else { |
| 1027 |
InputStream cr = null; |
| 1028 |
try { |
| 1029 |
byte[] buffer = new byte[PDU_COMPOSER_BLOCK_SIZE]; |
| 1030 |
cr = mResolver.openInputStream(part.getDataUri()); |
| 1031 |
int len = 0; |
| 1032 |
while ((len = cr.read(buffer)) != -1) { |
| 1033 |
mMessage.write(buffer, 0, len); |
| 1034 |
mPosition += len; |
| 1035 |
dataLength += len; |
| 1036 |
} |
| 1037 |
} catch (FileNotFoundException e) { |
| 1038 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 1039 |
} catch (IOException e) { |
| 1040 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 1041 |
} catch (RuntimeException e) { |
| 1042 |
return PDU_COMPOSE_CONTENT_ERROR; |
| 1043 |
} finally { |
| 1044 |
if (cr != null) { |
| 1045 |
try { |
| 1046 |
cr.close(); |
| 1047 |
} catch (IOException e) { |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
if (dataLength != (attachment.getLength() - headerLength)) { |
| 1054 |
throw new RuntimeException("BUG: Length correctness check failed"); |
| 1055 |
} |
| 1056 |
|
| 1057 |
mStack.pop(); |
| 1058 |
appendUintvarInteger(headerLength); |
| 1059 |
appendUintvarInteger(dataLength); |
| 1060 |
mStack.copy(); |
| 1061 |
} |
| 1062 |
|
| 1063 |
return PDU_COMPOSE_SUCCESS; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
static private class LengthRecordNode { |
| 1070 |
ByteArrayOutputStream currentMessage = null; |
| 1071 |
public int currentPosition = 0; |
| 1072 |
|
| 1073 |
public LengthRecordNode next = null; |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
private class PositionMarker { |
| 1080 |
private int c_pos; |
| 1081 |
private int currentStackSize; |
| 1082 |
|
| 1083 |
@UnsupportedAppUsage |
| 1084 |
int getLength() { |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
if (currentStackSize != mStack.stackSize) { |
| 1089 |
throw new RuntimeException("BUG: Invalid call to getLength()"); |
| 1090 |
} |
| 1091 |
|
| 1092 |
return mPosition - c_pos; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
private class BufferStack { |
| 1102 |
private LengthRecordNode stack = null; |
| 1103 |
private LengthRecordNode toCopy = null; |
| 1104 |
|
| 1105 |
int stackSize = 0; |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
@UnsupportedAppUsage |
| 1111 |
void newbuf() { |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
if (toCopy != null) { |
| 1116 |
throw new RuntimeException("BUG: Invalid newbuf() before copy()"); |
| 1117 |
} |
| 1118 |
|
| 1119 |
LengthRecordNode temp = new LengthRecordNode(); |
| 1120 |
|
| 1121 |
temp.currentMessage = mMessage; |
| 1122 |
temp.currentPosition = mPosition; |
| 1123 |
|
| 1124 |
temp.next = stack; |
| 1125 |
stack = temp; |
| 1126 |
|
| 1127 |
stackSize = stackSize + 1; |
| 1128 |
|
| 1129 |
mMessage = new ByteArrayOutputStream(); |
| 1130 |
mPosition = 0; |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
@UnsupportedAppUsage |
| 1137 |
void pop() { |
| 1138 |
ByteArrayOutputStream currentMessage = mMessage; |
| 1139 |
int currentPosition = mPosition; |
| 1140 |
|
| 1141 |
mMessage = stack.currentMessage; |
| 1142 |
mPosition = stack.currentPosition; |
| 1143 |
|
| 1144 |
toCopy = stack; |
| 1145 |
|
| 1146 |
|
| 1147 |
stack = stack.next; |
| 1148 |
stackSize = stackSize - 1; |
| 1149 |
|
| 1150 |
toCopy.currentMessage = currentMessage; |
| 1151 |
toCopy.currentPosition = currentPosition; |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
@UnsupportedAppUsage |
| 1158 |
void copy() { |
| 1159 |
arraycopy(toCopy.currentMessage.toByteArray(), 0, |
| 1160 |
toCopy.currentPosition); |
| 1161 |
|
| 1162 |
toCopy = null; |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
@UnsupportedAppUsage |
| 1169 |
PositionMarker mark() { |
| 1170 |
PositionMarker m = new PositionMarker(); |
| 1171 |
|
| 1172 |
m.c_pos = mPosition; |
| 1173 |
m.currentStackSize = stackSize; |
| 1174 |
|
| 1175 |
return m; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
protected static int checkAddressType(String address) { |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
if (null == address) { |
| 1210 |
return PDU_UNKNOWN_ADDRESS_TYPE; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (address.matches(REGEXP_IPV4_ADDRESS_TYPE)) { |
| 1214 |
|
| 1215 |
return PDU_IPV4_ADDRESS_TYPE; |
| 1216 |
}else if (address.matches(REGEXP_PHONE_NUMBER_ADDRESS_TYPE)) { |
| 1217 |
|
| 1218 |
return PDU_PHONE_NUMBER_ADDRESS_TYPE; |
| 1219 |
} else if (address.matches(REGEXP_EMAIL_ADDRESS_TYPE)) { |
| 1220 |
|
| 1221 |
return PDU_EMAIL_ADDRESS_TYPE; |
| 1222 |
} else if (address.matches(REGEXP_IPV6_ADDRESS_TYPE)) { |
| 1223 |
|
| 1224 |
return PDU_IPV6_ADDRESS_TYPE; |
| 1225 |
} else { |
| 1226 |
|
| 1227 |
return PDU_UNKNOWN_ADDRESS_TYPE; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
} |