rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* Copyright Statement: |
| 2 | * |
| 3 | * This software/firmware and related documentation ("MediaTek Software") are |
| 4 | * protected under relevant copyright laws. The information contained herein |
| 5 | * is confidential and proprietary to MediaTek Inc. and/or its licensors. |
| 6 | * Without the prior written permission of MediaTek inc. and/or its licensors, |
| 7 | * any reproduction, modification, use or disclosure of MediaTek Software, |
| 8 | * and information contained herein, in whole or in part, shall be strictly prohibited. |
| 9 | */ |
| 10 | /* MediaTek Inc. (C) 2010. All rights reserved. |
| 11 | * |
| 12 | * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 13 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 14 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON |
| 15 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 18 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 19 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 20 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH |
| 21 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES |
| 22 | * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES |
| 23 | * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK |
| 24 | * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR |
| 25 | * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND |
| 26 | * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 27 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 28 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO |
| 29 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 30 | * |
| 31 | * The following software/firmware and/or related documentation ("MediaTek Software") |
| 32 | * have been modified by MediaTek Inc. All revisions are subject to any receiver's |
| 33 | * applicable license agreements with MediaTek Inc. |
| 34 | */ |
| 35 | #include <memory> |
| 36 | #include <vector> |
| 37 | #include <string> |
| 38 | using namespace std; |
| 39 | |
| 40 | #include "SmsHeader.h" |
| 41 | #include "HexDump.h" |
| 42 | |
| 43 | SmsHeader::SmsHeader() { |
| 44 | // TODO Auto-generated constructor stub |
| 45 | |
| 46 | } |
| 47 | |
| 48 | SmsHeader::~SmsHeader() { |
| 49 | // TODO Auto-generated destructor stub |
| 50 | } |
| 51 | |
| 52 | std::shared_ptr<SmsHeader> SmsHeader::fromByteArray(std::vector<uint8_t> v) { |
| 53 | //ByteArrayInputStream inStream = new ByteArrayInputStream(data); |
| 54 | //uint8_t* temp = data; |
| 55 | std::shared_ptr<SmsHeader> smsHeader = std::make_shared<SmsHeader>(); |
| 56 | auto beg = v.begin(); |
| 57 | while (beg < v.end() ) { |
| 58 | /** |
| 59 | * NOTE: as defined in the spec, ConcatRef and PortAddr |
| 60 | * fields should not reoccur, but if they do the last |
| 61 | * occurrence is to be used. Also, for ConcatRef |
| 62 | * elements, if the count is zero, sequence is zero, or |
| 63 | * sequence is larger than count, the entire element is to |
| 64 | * be ignored. |
| 65 | */ |
| 66 | int id = (*(beg++)); |
| 67 | int len = (*(beg++)); |
| 68 | std::shared_ptr<ConcatRef> concatRef; |
| 69 | std::shared_ptr<PortAddrs> portAddrs; |
| 70 | switch (id) { |
| 71 | case ELT_ID_CONCATENATED_8_BIT_REFERENCE: |
| 72 | { |
| 73 | concatRef = make_shared<ConcatRef>(); |
| 74 | concatRef->refNumber = (*(beg++)); |
| 75 | concatRef->msgCount = (*(beg++)); |
| 76 | concatRef->seqNumber = (*(beg++)); |
| 77 | concatRef->isEightBits = true; |
| 78 | if (concatRef->msgCount != 0 && concatRef->seqNumber != 0 |
| 79 | && concatRef->seqNumber <= concatRef->msgCount) { |
| 80 | smsHeader->concatRef = concatRef; |
| 81 | } |
| 82 | break; |
| 83 | } |
| 84 | case ELT_ID_CONCATENATED_16_BIT_REFERENCE: |
| 85 | concatRef = make_shared<ConcatRef>(); |
| 86 | concatRef->refNumber = ((*(beg++)) << 8) | (*(beg++)); |
| 87 | concatRef->msgCount = (*(beg++)); |
| 88 | concatRef->seqNumber = (*(beg++)); |
| 89 | concatRef->isEightBits = false; |
| 90 | if (concatRef->msgCount != 0 && concatRef->seqNumber != 0 |
| 91 | && concatRef->seqNumber <= concatRef->msgCount) { |
| 92 | smsHeader->concatRef = concatRef; |
| 93 | } |
| 94 | break; |
| 95 | case ELT_ID_APPLICATION_PORT_ADDRESSING_8_BIT: |
| 96 | portAddrs = make_shared<PortAddrs>(); |
| 97 | portAddrs->destPort = (*(beg++)); |
| 98 | portAddrs->origPort = (*(beg++)); |
| 99 | portAddrs->areEightBits = true; |
| 100 | smsHeader->portAddrs = portAddrs; |
| 101 | break; |
| 102 | case ELT_ID_APPLICATION_PORT_ADDRESSING_16_BIT: |
| 103 | portAddrs = make_shared<PortAddrs>(); |
| 104 | portAddrs->destPort = ((*(beg++)) << 8) | (*(beg++)); |
| 105 | portAddrs->origPort = ((*(beg++)) << 8) | (*(beg++)); |
| 106 | portAddrs->areEightBits = false; |
| 107 | smsHeader->portAddrs = portAddrs; |
| 108 | break; |
| 109 | case ELT_ID_NATIONAL_LANGUAGE_SINGLE_SHIFT: |
| 110 | smsHeader->languageShiftTable = (*(beg++)); |
| 111 | break; |
| 112 | case ELT_ID_NATIONAL_LANGUAGE_LOCKING_SHIFT: |
| 113 | smsHeader->languageTable = (*(beg++)); |
| 114 | break; |
| 115 | case ELT_ID_SPECIAL_SMS_MESSAGE_INDICATION: { |
| 116 | auto specialSmsMsg = make_shared<SpecialSmsMsg>(); |
| 117 | specialSmsMsg->msgIndType = (*(beg++)); |
| 118 | specialSmsMsg->msgCount = (*(beg++)); |
| 119 | smsHeader->specialSmsMsgList.push_back(specialSmsMsg); |
| 120 | break; |
| 121 | } |
| 122 | default: |
| 123 | auto miscElt = make_shared<MiscElt>(); |
| 124 | miscElt->id = id; |
| 125 | //miscElt->data.insert(miscElt->data.end(), beg, v.end()); |
| 126 | smsHeader->miscEltList.push_back(miscElt); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | return smsHeader; |
| 131 | } |
| 132 | |
| 133 | std::vector<uint8_t> SmsHeader::toByteArray( |
| 134 | std::shared_ptr<SmsHeader> smsHeader) { |
| 135 | std::vector<uint8_t> temp; |
| 136 | temp.clear(); |
| 137 | if ((smsHeader->portAddrs == nullptr) && (smsHeader->concatRef == nullptr) |
| 138 | && (smsHeader->specialSmsMsgList.empty()) |
| 139 | && (smsHeader->miscEltList.empty()) |
| 140 | && (smsHeader->languageShiftTable == 0) |
| 141 | && (smsHeader->languageTable == 0)) { |
| 142 | return temp; |
| 143 | } |
| 144 | |
| 145 | auto concatRef = smsHeader->concatRef; |
| 146 | if (concatRef != nullptr) { |
| 147 | if (concatRef->isEightBits) { |
| 148 | temp.push_back(ELT_ID_CONCATENATED_8_BIT_REFERENCE); |
| 149 | temp.push_back(3); |
| 150 | temp.push_back(concatRef->refNumber); |
| 151 | } else { |
| 152 | temp.push_back(ELT_ID_CONCATENATED_16_BIT_REFERENCE); |
| 153 | temp.push_back(4); |
| 154 | temp.push_back(concatRef->refNumber >> 8); |
| 155 | temp.push_back(concatRef->refNumber & 0x00FF); |
| 156 | } |
| 157 | temp.push_back(concatRef->msgCount); |
| 158 | temp.push_back(concatRef->seqNumber); |
| 159 | } |
| 160 | auto portAddrs = smsHeader->portAddrs; |
| 161 | if (portAddrs != nullptr) { |
| 162 | if (portAddrs->areEightBits) { |
| 163 | temp.push_back(ELT_ID_APPLICATION_PORT_ADDRESSING_8_BIT); |
| 164 | temp.push_back(2); |
| 165 | temp.push_back(portAddrs->destPort); |
| 166 | temp.push_back(portAddrs->origPort); |
| 167 | } else { |
| 168 | temp.push_back(ELT_ID_APPLICATION_PORT_ADDRESSING_16_BIT); |
| 169 | temp.push_back(4); |
| 170 | temp.push_back(portAddrs->destPort >> 8); |
| 171 | temp.push_back(portAddrs->destPort & 0x00FF); |
| 172 | temp.push_back(portAddrs->origPort >> 8); |
| 173 | temp.push_back(portAddrs->origPort & 0x00FF); |
| 174 | } |
| 175 | } |
| 176 | if (smsHeader->languageShiftTable != 0) { |
| 177 | temp.push_back(ELT_ID_NATIONAL_LANGUAGE_SINGLE_SHIFT); |
| 178 | temp.push_back(1); |
| 179 | temp.push_back(smsHeader->languageShiftTable); |
| 180 | } |
| 181 | if (smsHeader->languageTable != 0) { |
| 182 | temp.push_back(ELT_ID_NATIONAL_LANGUAGE_LOCKING_SHIFT); |
| 183 | temp.push_back(1); |
| 184 | temp.push_back(smsHeader->languageTable); |
| 185 | } |
| 186 | for (auto specialSmsMsg : smsHeader->specialSmsMsgList) { |
| 187 | temp.push_back(ELT_ID_SPECIAL_SMS_MESSAGE_INDICATION); |
| 188 | temp.push_back(2); |
| 189 | temp.push_back(specialSmsMsg->msgIndType & 0xFF); |
| 190 | temp.push_back(specialSmsMsg->msgCount & 0xFF); |
| 191 | } |
| 192 | for (auto miscElt : smsHeader->miscEltList) { |
| 193 | temp.push_back(miscElt->id); |
| 194 | temp.push_back(miscElt->data.size()); |
| 195 | temp.insert(temp.end(), miscElt->data.begin(), miscElt->data.end()); |
| 196 | } |
| 197 | return temp; |
| 198 | } |
| 199 | |
| 200 | std::string SmsHeader::toString() { |
| 201 | string builder; |
| 202 | builder.append("UserDataHeader "); |
| 203 | builder.append("{ ConcatRef "); |
| 204 | if (concatRef == nullptr) { |
| 205 | builder.append("unset"); |
| 206 | } else { |
| 207 | builder.append("{ refNumber=" + std::to_string(concatRef->refNumber)); |
| 208 | builder.append(", msgCount=" + std::to_string(concatRef->msgCount)); |
| 209 | builder.append(", seqNumber=" + std::to_string(concatRef->seqNumber)); |
| 210 | builder.append(", isEightBits=" + std::to_string(concatRef->isEightBits)); |
| 211 | builder.append(" }"); |
| 212 | } |
| 213 | builder.append(", PortAddrs "); |
| 214 | if (portAddrs == nullptr) { |
| 215 | builder.append("unset"); |
| 216 | } else { |
| 217 | builder.append("{ destPort=" + std::to_string(portAddrs->destPort)); |
| 218 | builder.append(", origPort=" + std::to_string(portAddrs->origPort)); |
| 219 | builder.append(", areEightBits=" + std::to_string(portAddrs->areEightBits)); |
| 220 | builder.append(" }"); |
| 221 | } |
| 222 | if (languageShiftTable != 0) { |
| 223 | builder.append( |
| 224 | ", languageShiftTable=" + std::to_string(languageShiftTable)); |
| 225 | } |
| 226 | if (languageTable != 0) { |
| 227 | builder.append(", languageTable=" + std::to_string(languageTable)); |
| 228 | } |
| 229 | for (auto specialSmsMsg : specialSmsMsgList) { |
| 230 | builder.append(", SpecialSmsMsg "); |
| 231 | builder.append("{ msgIndType=" + std::to_string(specialSmsMsg->msgIndType)); |
| 232 | builder.append(", msgCount=" + std::to_string(specialSmsMsg->msgCount)); |
| 233 | builder.append(" }"); |
| 234 | } |
| 235 | for (auto miscElt : miscEltList) { |
| 236 | builder.append(", MiscElt "); |
| 237 | builder.append("{ id=" + std::to_string(miscElt->id)); |
| 238 | builder.append(", length=" + std::to_string((int) miscElt->data.size())); |
| 239 | builder.append(", data=" + HexDump::toHexString(miscElt->data)); |
| 240 | builder.append(" }"); |
| 241 | } |
| 242 | builder.append(" }"); |
| 243 | return builder; |
| 244 | } |