nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2010-2014 Broadcom Corporation |
| 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 | ******************************************************************************/ |
| 18 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 19 | /****************************************************************************** |
| 20 | * |
| 21 | * This file contains source code for some utility functions to help parse |
| 22 | * and build NFC Data Exchange Format (NDEF) messages |
| 23 | * |
| 24 | ******************************************************************************/ |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 25 | #include "ndef_utils.h" |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 26 | #include <string.h> |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 27 | |
| 28 | /******************************************************************************* |
| 29 | ** |
| 30 | ** Static Local Functions |
| 31 | ** |
| 32 | *******************************************************************************/ |
| 33 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 34 | /******************************************************************************* |
| 35 | ** |
| 36 | ** Function shiftdown |
| 37 | ** |
| 38 | ** Description shift memory down (to make space to insert a record) |
| 39 | ** |
| 40 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 41 | static void shiftdown(uint8_t* p_mem, uint32_t len, uint32_t shift_amount) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 42 | uint8_t* ps = p_mem + len - 1; |
| 43 | uint8_t* pd = ps + shift_amount; |
| 44 | uint32_t xx; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 45 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 46 | for (xx = 0; xx < len; xx++) *pd-- = *ps--; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /******************************************************************************* |
| 50 | ** |
| 51 | ** Function shiftup |
| 52 | ** |
| 53 | ** Description shift memory up (to delete a record) |
| 54 | ** |
| 55 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 56 | static void shiftup(uint8_t* p_dest, uint8_t* p_src, uint32_t len) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 57 | uint8_t* ps = p_src; |
| 58 | uint8_t* pd = p_dest; |
| 59 | uint32_t xx; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 60 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 61 | for (xx = 0; xx < len; xx++) *pd++ = *ps++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /******************************************************************************* |
| 65 | ** |
| 66 | ** Function NDEF_MsgValidate |
| 67 | ** |
| 68 | ** Description This function validates an NDEF message. |
| 69 | ** |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 70 | ** Returns TRUE if all OK, or FALSE if the message is invalid. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 71 | ** |
| 72 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 73 | tNDEF_STATUS NDEF_MsgValidate(uint8_t* p_msg, uint32_t msg_len, |
| 74 | bool b_allow_chunks) { |
| 75 | uint8_t* p_rec = p_msg; |
| 76 | uint8_t* p_end = p_msg + msg_len; |
| 77 | uint8_t rec_hdr = 0, type_len, id_len; |
| 78 | int count; |
| 79 | uint32_t payload_len; |
| 80 | bool bInChunk = false; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 81 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 82 | if ((p_msg == NULL) || (msg_len < 3)) return (NDEF_MSG_TOO_SHORT); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 83 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 84 | /* The first record must have the MB bit set */ |
| 85 | if ((*p_msg & NDEF_MB_MASK) == 0) return (NDEF_MSG_NO_MSG_BEGIN); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 86 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 87 | /* The first record cannot be a chunk */ |
| 88 | if ((*p_msg & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED) |
| 89 | return (NDEF_MSG_UNEXPECTED_CHUNK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 90 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 91 | for (count = 0; p_rec < p_end; count++) { |
| 92 | /* if less than short record header */ |
| 93 | if (p_rec + 3 > p_end) return (NDEF_MSG_TOO_SHORT); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 94 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 95 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 96 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 97 | /* header should have a valid TNF */ |
| 98 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_MASK) |
| 99 | return NDEF_MSG_INVALID_CHUNK; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 100 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 101 | /* The second and all subsequent records must NOT have the MB bit set */ |
| 102 | if ((count > 0) && (rec_hdr & NDEF_MB_MASK)) |
| 103 | return (NDEF_MSG_EXTRA_MSG_BEGIN); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 104 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 105 | /* Type field length */ |
| 106 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 107 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 108 | /* If the record is chunked, first record must contain the type unless |
| 109 | * it's Type Name Format is Unknown */ |
| 110 | if ((rec_hdr & NDEF_CF_MASK) && (rec_hdr & NDEF_MB_MASK) && type_len == 0 && |
| 111 | (rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNKNOWN) |
| 112 | return (NDEF_MSG_INVALID_CHUNK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 113 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 114 | /* Payload length - can be 1 or 4 bytes */ |
| 115 | if (rec_hdr & NDEF_SR_MASK) |
| 116 | payload_len = *p_rec++; |
| 117 | else { |
| 118 | /* if less than 4 bytes payload length */ |
| 119 | if (p_rec + 4 > p_end) return (NDEF_MSG_TOO_SHORT); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 120 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 121 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 122 | } |
| 123 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 124 | /* ID field Length */ |
| 125 | if (rec_hdr & NDEF_IL_MASK) { |
| 126 | /* if less than 1 byte ID field length */ |
| 127 | if (p_rec + 1 > p_end) return (NDEF_MSG_TOO_SHORT); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 128 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 129 | id_len = *p_rec++; |
| 130 | } else { |
| 131 | id_len = 0; |
| 132 | /* Empty record must have the id_len */ |
| 133 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_EMPTY) |
| 134 | return (NDEF_MSG_INVALID_EMPTY_REC); |
| 135 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 136 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 137 | /* A chunk must have type "unchanged", and no type or ID fields */ |
| 138 | if (rec_hdr & NDEF_CF_MASK) { |
| 139 | if (!b_allow_chunks) return (NDEF_MSG_UNEXPECTED_CHUNK); |
| 140 | |
| 141 | /* Inside a chunk, the type must be unchanged and no type or ID field i |
| 142 | * sallowed */ |
| 143 | if (bInChunk) { |
| 144 | if ((type_len != 0) || (id_len != 0) || |
| 145 | ((rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNCHANGED)) |
| 146 | return (NDEF_MSG_INVALID_CHUNK); |
| 147 | } else { |
| 148 | /* First record of a chunk must NOT have type "unchanged" */ |
| 149 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED) |
| 150 | return (NDEF_MSG_INVALID_CHUNK); |
| 151 | |
| 152 | bInChunk = true; |
| 153 | } |
| 154 | } else { |
| 155 | /* This may be the last guy in a chunk. */ |
| 156 | if (bInChunk) { |
| 157 | if ((type_len != 0) || (id_len != 0) || |
| 158 | ((rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNCHANGED)) |
| 159 | return (NDEF_MSG_INVALID_CHUNK); |
| 160 | |
| 161 | bInChunk = false; |
| 162 | } else { |
| 163 | /* If not in a chunk, the record must NOT have type "unchanged" */ |
| 164 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED) |
| 165 | return (NDEF_MSG_INVALID_CHUNK); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* An empty record must NOT have a type, ID or payload */ |
| 170 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_EMPTY) { |
| 171 | if ((type_len != 0) || (id_len != 0) || (payload_len != 0)) |
| 172 | return (NDEF_MSG_INVALID_EMPTY_REC); |
| 173 | } |
| 174 | |
| 175 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNKNOWN) { |
| 176 | if (type_len != 0) return (NDEF_MSG_LENGTH_MISMATCH); |
| 177 | } |
| 178 | |
| 179 | /* External type should have non-zero type length */ |
| 180 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_EXT) { |
| 181 | if (type_len == 0) return (NDEF_MSG_LENGTH_MISMATCH); |
| 182 | } |
| 183 | |
| 184 | /* External type and Well Known types should have valid characters |
| 185 | in the TYPE field */ |
| 186 | if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_EXT || |
| 187 | (rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_WKT) { |
| 188 | uint8_t* p_rec_type = p_rec; |
| 189 | if ((p_rec_type + type_len) > p_end) return (NDEF_MSG_TOO_SHORT); |
| 190 | |
| 191 | for (int type_index = 0; type_index < type_len; type_index++) { |
| 192 | if (p_rec_type[type_index] < NDEF_RTD_VALID_START || |
| 193 | p_rec_type[type_index] > NDEF_RTD_VALID_END) |
| 194 | return (NDEF_MSG_INVALID_TYPE); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* Point to next record */ |
| 199 | p_rec += (payload_len + type_len + id_len); |
| 200 | |
| 201 | if (rec_hdr & NDEF_ME_MASK) break; |
| 202 | |
| 203 | rec_hdr = 0; |
| 204 | } |
| 205 | |
| 206 | /* The last record should have the ME bit set */ |
| 207 | if ((rec_hdr & NDEF_ME_MASK) == 0) return (NDEF_MSG_NO_MSG_END); |
| 208 | |
| 209 | /* p_rec should equal p_end if all the length fields were correct */ |
| 210 | if (p_rec != p_end) return (NDEF_MSG_LENGTH_MISMATCH); |
| 211 | |
| 212 | return (NDEF_OK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /******************************************************************************* |
| 216 | ** |
| 217 | ** Function NDEF_MsgGetNumRecs |
| 218 | ** |
| 219 | ** Description This function gets the number of records in the given NDEF |
| 220 | ** message. |
| 221 | ** |
| 222 | ** Returns The record count, or 0 if the message is invalid. |
| 223 | ** |
| 224 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 225 | int32_t NDEF_MsgGetNumRecs(uint8_t* p_msg) { |
| 226 | uint8_t* p_rec = p_msg; |
| 227 | uint8_t rec_hdr, type_len, id_len; |
| 228 | int count; |
| 229 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 230 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 231 | for (count = 0;;) { |
| 232 | count++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 233 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 234 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 235 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 236 | if (rec_hdr & NDEF_ME_MASK) break; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 237 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 238 | /* Type field length */ |
| 239 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 240 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 241 | /* Payload length - can be 1 or 4 bytes */ |
| 242 | if (rec_hdr & NDEF_SR_MASK) |
| 243 | payload_len = *p_rec++; |
| 244 | else |
| 245 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 246 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 247 | /* ID field Length */ |
| 248 | if (rec_hdr & NDEF_IL_MASK) |
| 249 | id_len = *p_rec++; |
| 250 | else |
| 251 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 252 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 253 | /* Point to next record */ |
| 254 | p_rec += (payload_len + type_len + id_len); |
| 255 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 256 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 257 | /* Return the number of records found */ |
| 258 | return (count); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /******************************************************************************* |
| 262 | ** |
| 263 | ** Function NDEF_MsgGetRecLength |
| 264 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 265 | ** Description This function returns length of the current record in the |
| 266 | ** given NDEF message. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 267 | ** |
| 268 | ** Returns Length of record |
| 269 | ** |
| 270 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 271 | uint32_t NDEF_MsgGetRecLength(uint8_t* p_cur_rec) { |
| 272 | uint8_t rec_hdr, type_len, id_len; |
| 273 | uint32_t rec_len = 0; |
| 274 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 275 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 276 | /* Get the current record's header */ |
| 277 | rec_hdr = *p_cur_rec++; |
| 278 | rec_len++; |
| 279 | |
| 280 | /* Type field length */ |
| 281 | type_len = *p_cur_rec++; |
| 282 | rec_len++; |
| 283 | |
| 284 | /* Payload length - can be 1 or 4 bytes */ |
| 285 | if (rec_hdr & NDEF_SR_MASK) { |
| 286 | payload_len = *p_cur_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 287 | rec_len++; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 288 | } else { |
| 289 | BE_STREAM_TO_UINT32(payload_len, p_cur_rec); |
| 290 | rec_len += 4; |
| 291 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 292 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 293 | /* ID field Length */ |
| 294 | if (rec_hdr & NDEF_IL_MASK) { |
| 295 | id_len = *p_cur_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 296 | rec_len++; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 297 | } else |
| 298 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 299 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 300 | /* Total length of record */ |
| 301 | rec_len += (payload_len + type_len + id_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 302 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 303 | return (rec_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /******************************************************************************* |
| 307 | ** |
| 308 | ** Function NDEF_MsgGetNextRec |
| 309 | ** |
| 310 | ** Description This function gets a pointer to the next record in the given |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 311 | ** NDEF message. If the current record pointer is NULL, a |
| 312 | ** pointer to the first record is returned. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 313 | ** |
| 314 | ** Returns Pointer to the start of the record, or NULL if no more |
| 315 | ** |
| 316 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 317 | uint8_t* NDEF_MsgGetNextRec(uint8_t* p_cur_rec) { |
| 318 | uint8_t rec_hdr, type_len, id_len; |
| 319 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 320 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 321 | /* Get the current record's header */ |
| 322 | rec_hdr = *p_cur_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 323 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 324 | /* If this is the last record, return NULL */ |
| 325 | if (rec_hdr & NDEF_ME_MASK) return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 326 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 327 | /* Type field length */ |
| 328 | type_len = *p_cur_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 329 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 330 | /* Payload length - can be 1 or 4 bytes */ |
| 331 | if (rec_hdr & NDEF_SR_MASK) |
| 332 | payload_len = *p_cur_rec++; |
| 333 | else |
| 334 | BE_STREAM_TO_UINT32(payload_len, p_cur_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 335 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 336 | /* ID field Length */ |
| 337 | if (rec_hdr & NDEF_IL_MASK) |
| 338 | id_len = *p_cur_rec++; |
| 339 | else |
| 340 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 341 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 342 | /* Point to next record */ |
| 343 | p_cur_rec += (payload_len + type_len + id_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 344 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 345 | return (p_cur_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /******************************************************************************* |
| 349 | ** |
| 350 | ** Function NDEF_MsgGetRecByIndex |
| 351 | ** |
| 352 | ** Description This function gets a pointer to the record with the given |
| 353 | ** index (0-based index) in the given NDEF message. |
| 354 | ** |
| 355 | ** Returns Pointer to the start of the record, or NULL |
| 356 | ** |
| 357 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 358 | uint8_t* NDEF_MsgGetRecByIndex(uint8_t* p_msg, int32_t index) { |
| 359 | uint8_t* p_rec = p_msg; |
| 360 | uint8_t rec_hdr, type_len, id_len; |
| 361 | int32_t count; |
| 362 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 363 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 364 | for (count = 0;; count++) { |
| 365 | if (count == index) return (p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 366 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 367 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 368 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 369 | if (rec_hdr & NDEF_ME_MASK) return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 370 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 371 | /* Type field length */ |
| 372 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 373 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 374 | /* Payload length - can be 1 or 4 bytes */ |
| 375 | if (rec_hdr & NDEF_SR_MASK) |
| 376 | payload_len = *p_rec++; |
| 377 | else |
| 378 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 379 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 380 | /* ID field Length */ |
| 381 | if (rec_hdr & NDEF_IL_MASK) |
| 382 | id_len = *p_rec++; |
| 383 | else |
| 384 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 385 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 386 | /* Point to next record */ |
| 387 | p_rec += (payload_len + type_len + id_len); |
| 388 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 389 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 390 | /* If here, there is no record of that index */ |
| 391 | return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 392 | } |
| 393 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 394 | /******************************************************************************* |
| 395 | ** |
| 396 | ** Function NDEF_MsgGetLastRecInMsg |
| 397 | ** |
| 398 | ** Description This function gets a pointer to the last record in the |
| 399 | ** given NDEF message. |
| 400 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 401 | ** Returns Pointer to the start of the last record, or NULL if some |
| 402 | ** problem |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 403 | ** |
| 404 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 405 | uint8_t* NDEF_MsgGetLastRecInMsg(uint8_t* p_msg) { |
| 406 | uint8_t* p_rec = p_msg; |
| 407 | uint8_t* pRecStart; |
| 408 | uint8_t rec_hdr, type_len, id_len; |
| 409 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 410 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 411 | for (;;) { |
| 412 | pRecStart = p_rec; |
| 413 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 414 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 415 | if (rec_hdr & NDEF_ME_MASK) break; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 416 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 417 | /* Type field length */ |
| 418 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 419 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 420 | /* Payload length - can be 1 or 4 bytes */ |
| 421 | if (rec_hdr & NDEF_SR_MASK) |
| 422 | payload_len = *p_rec++; |
| 423 | else |
| 424 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 425 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 426 | /* ID field Length */ |
| 427 | if (rec_hdr & NDEF_IL_MASK) |
| 428 | id_len = *p_rec++; |
| 429 | else |
| 430 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 431 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 432 | /* Point to next record */ |
| 433 | p_rec += (payload_len + type_len + id_len); |
| 434 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 435 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 436 | return (pRecStart); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 437 | } |
| 438 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 439 | /******************************************************************************* |
| 440 | ** |
| 441 | ** Function NDEF_MsgGetFirstRecByType |
| 442 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 443 | ** Description This function gets a pointer to the first record with the |
| 444 | ** given record type in the given NDEF message. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 445 | ** |
| 446 | ** Returns Pointer to the start of the record, or NULL |
| 447 | ** |
| 448 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 449 | uint8_t* NDEF_MsgGetFirstRecByType(uint8_t* p_msg, uint8_t tnf, uint8_t* p_type, |
| 450 | uint8_t tlen) { |
| 451 | uint8_t* p_rec = p_msg; |
| 452 | uint8_t* pRecStart; |
| 453 | uint8_t rec_hdr, type_len, id_len; |
| 454 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 455 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 456 | for (;;) { |
| 457 | pRecStart = p_rec; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 458 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 459 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 460 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 461 | /* Type field length */ |
| 462 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 463 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 464 | /* Payload length - can be 1 or 4 bytes */ |
| 465 | if (rec_hdr & NDEF_SR_MASK) |
| 466 | payload_len = *p_rec++; |
| 467 | else |
| 468 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 469 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 470 | /* ID field Length */ |
| 471 | if (rec_hdr & NDEF_IL_MASK) |
| 472 | id_len = *p_rec++; |
| 473 | else |
| 474 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 475 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 476 | /* At this point, p_rec points to the start of the type field. We need to */ |
| 477 | /* compare the type of the type, the length of the type and the data */ |
| 478 | if (((rec_hdr & NDEF_TNF_MASK) == tnf) && (type_len == tlen) && |
| 479 | (!memcmp(p_rec, p_type, tlen))) |
| 480 | return (pRecStart); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 481 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 482 | /* If this was the last record, return NULL */ |
| 483 | if (rec_hdr & NDEF_ME_MASK) return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 484 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 485 | /* Point to next record */ |
| 486 | p_rec += (payload_len + type_len + id_len); |
| 487 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 488 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 489 | /* If here, there is no record of that type */ |
| 490 | return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /******************************************************************************* |
| 494 | ** |
| 495 | ** Function NDEF_MsgGetNextRecByType |
| 496 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 497 | ** Description This function gets a pointer to the next record with the |
| 498 | ** given record type in the given NDEF message. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 499 | ** |
| 500 | ** Returns Pointer to the start of the record, or NULL |
| 501 | ** |
| 502 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 503 | uint8_t* NDEF_MsgGetNextRecByType(uint8_t* p_cur_rec, uint8_t tnf, |
| 504 | uint8_t* p_type, uint8_t tlen) { |
| 505 | uint8_t* p_rec; |
| 506 | uint8_t* pRecStart; |
| 507 | uint8_t rec_hdr, type_len, id_len; |
| 508 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 509 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 510 | /* If this is the last record in the message, return NULL */ |
| 511 | p_rec = NDEF_MsgGetNextRec(p_cur_rec); |
| 512 | if (p_rec == NULL) return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 513 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 514 | for (;;) { |
| 515 | pRecStart = p_rec; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 516 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 517 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 518 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 519 | /* Type field length */ |
| 520 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 521 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 522 | /* Payload length - can be 1 or 4 bytes */ |
| 523 | if (rec_hdr & NDEF_SR_MASK) |
| 524 | payload_len = *p_rec++; |
| 525 | else |
| 526 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 527 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 528 | /* ID field Length */ |
| 529 | if (rec_hdr & NDEF_IL_MASK) |
| 530 | id_len = *p_rec++; |
| 531 | else |
| 532 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 533 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 534 | /* At this point, p_rec points to the start of the type field. We need to */ |
| 535 | /* compare the type of the type, the length of the type and the data */ |
| 536 | if (((rec_hdr & NDEF_TNF_MASK) == tnf) && (type_len == tlen) && |
| 537 | (!memcmp(p_rec, p_type, tlen))) |
| 538 | return (pRecStart); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 539 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 540 | /* If this was the last record, return NULL */ |
| 541 | if (rec_hdr & NDEF_ME_MASK) break; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 542 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 543 | /* Point to next record */ |
| 544 | p_rec += (payload_len + type_len + id_len); |
| 545 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 546 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 547 | /* If here, there is no record of that type */ |
| 548 | return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 549 | } |
| 550 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 551 | /******************************************************************************* |
| 552 | ** |
| 553 | ** Function NDEF_MsgGetFirstRecById |
| 554 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 555 | ** Description This function gets a pointer to the first record with the |
| 556 | ** given record id in the given NDEF message. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 557 | ** |
| 558 | ** Returns Pointer to the start of the record, or NULL |
| 559 | ** |
| 560 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 561 | uint8_t* NDEF_MsgGetFirstRecById(uint8_t* p_msg, uint8_t* p_id, uint8_t ilen) { |
| 562 | uint8_t* p_rec = p_msg; |
| 563 | uint8_t* pRecStart; |
| 564 | uint8_t rec_hdr, type_len, id_len; |
| 565 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 566 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 567 | for (;;) { |
| 568 | pRecStart = p_rec; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 569 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 570 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 571 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 572 | /* Type field length */ |
| 573 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 574 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 575 | /* Payload length - can be 1 or 4 bytes */ |
| 576 | if (rec_hdr & NDEF_SR_MASK) |
| 577 | payload_len = *p_rec++; |
| 578 | else |
| 579 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 580 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 581 | /* ID field Length */ |
| 582 | if (rec_hdr & NDEF_IL_MASK) |
| 583 | id_len = *p_rec++; |
| 584 | else |
| 585 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 586 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 587 | /* At this point, p_rec points to the start of the type field. Skip it */ |
| 588 | p_rec += type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 589 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 590 | /* At this point, p_rec points to the start of the ID field. Compare length |
| 591 | * and data */ |
| 592 | if ((id_len == ilen) && (!memcmp(p_rec, p_id, ilen))) return (pRecStart); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 593 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 594 | /* If this was the last record, return NULL */ |
| 595 | if (rec_hdr & NDEF_ME_MASK) return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 596 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 597 | /* Point to next record */ |
| 598 | p_rec += (id_len + payload_len); |
| 599 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 600 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 601 | /* If here, there is no record of that ID */ |
| 602 | return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /******************************************************************************* |
| 606 | ** |
| 607 | ** Function NDEF_MsgGetNextRecById |
| 608 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 609 | ** Description This function gets a pointer to the next record with the |
| 610 | ** given record id in the given NDEF message. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 611 | ** |
| 612 | ** Returns Pointer to the start of the record, or NULL |
| 613 | ** |
| 614 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 615 | uint8_t* NDEF_MsgGetNextRecById(uint8_t* p_cur_rec, uint8_t* p_id, |
| 616 | uint8_t ilen) { |
| 617 | uint8_t* p_rec; |
| 618 | uint8_t* pRecStart; |
| 619 | uint8_t rec_hdr, type_len, id_len; |
| 620 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 621 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 622 | /* If this is the last record in the message, return NULL */ |
| 623 | p_rec = NDEF_MsgGetNextRec(p_cur_rec); |
| 624 | if (p_rec == NULL) return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 625 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 626 | for (;;) { |
| 627 | pRecStart = p_rec; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 628 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 629 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 630 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 631 | /* Type field length */ |
| 632 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 633 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 634 | /* Payload length - can be 1 or 4 bytes */ |
| 635 | if (rec_hdr & NDEF_SR_MASK) |
| 636 | payload_len = *p_rec++; |
| 637 | else |
| 638 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 639 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 640 | /* ID field Length */ |
| 641 | if (rec_hdr & NDEF_IL_MASK) |
| 642 | id_len = *p_rec++; |
| 643 | else |
| 644 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 645 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 646 | /* At this point, p_rec points to the start of the type field. Skip it */ |
| 647 | p_rec += type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 648 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 649 | /* At this point, p_rec points to the start of the ID field. Compare length |
| 650 | * and data */ |
| 651 | if ((id_len == ilen) && (!memcmp(p_rec, p_id, ilen))) return (pRecStart); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 652 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 653 | /* If this was the last record, return NULL */ |
| 654 | if (rec_hdr & NDEF_ME_MASK) break; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 655 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 656 | /* Point to next record */ |
| 657 | p_rec += (id_len + payload_len); |
| 658 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 659 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 660 | /* If here, there is no record of that ID */ |
| 661 | return (NULL); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /******************************************************************************* |
| 665 | ** |
| 666 | ** Function NDEF_RecGetType |
| 667 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 668 | ** Description This function gets a pointer to the record type for the |
| 669 | ** given NDEF record. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 670 | ** |
| 671 | ** Returns Pointer to Type (NULL if none). TNF and len are filled in. |
| 672 | ** |
| 673 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 674 | uint8_t* NDEF_RecGetType(uint8_t* p_rec, uint8_t* p_tnf, uint8_t* p_type_len) { |
| 675 | uint8_t rec_hdr, type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 676 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 677 | /* First byte is the record header */ |
| 678 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 679 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 680 | /* Next byte is the type field length */ |
| 681 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 682 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 683 | /* Skip the payload length */ |
| 684 | if (rec_hdr & NDEF_SR_MASK) |
| 685 | p_rec += 1; |
| 686 | else |
| 687 | p_rec += 4; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 688 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 689 | /* Skip ID field Length, if present */ |
| 690 | if (rec_hdr & NDEF_IL_MASK) p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 691 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 692 | /* At this point, p_rec points to the start of the type field. */ |
| 693 | *p_type_len = type_len; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 694 | *p_tnf = rec_hdr & NDEF_TNF_MASK; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 695 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 696 | if (type_len == 0) |
| 697 | return (NULL); |
| 698 | else |
| 699 | return (p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /******************************************************************************* |
| 703 | ** |
| 704 | ** Function NDEF_RecGetId |
| 705 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 706 | ** Description This function gets a pointer to the record id for the given |
| 707 | ** NDEF record. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 708 | ** |
| 709 | ** Returns Pointer to Id (NULL if none). ID Len is filled in. |
| 710 | ** |
| 711 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 712 | uint8_t* NDEF_RecGetId(uint8_t* p_rec, uint8_t* p_id_len) { |
| 713 | uint8_t rec_hdr, type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 714 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 715 | /* First byte is the record header */ |
| 716 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 717 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 718 | /* Next byte is the type field length */ |
| 719 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 720 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 721 | /* Skip the payload length */ |
| 722 | if (rec_hdr & NDEF_SR_MASK) |
| 723 | p_rec++; |
| 724 | else |
| 725 | p_rec += 4; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 726 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 727 | /* ID field Length */ |
| 728 | if (rec_hdr & NDEF_IL_MASK) |
| 729 | *p_id_len = *p_rec++; |
| 730 | else |
| 731 | *p_id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 732 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 733 | /* p_rec now points to the start of the type field. The ID field follows it */ |
| 734 | if (*p_id_len == 0) |
| 735 | return (NULL); |
| 736 | else |
| 737 | return (p_rec + type_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 738 | } |
| 739 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 740 | /******************************************************************************* |
| 741 | ** |
| 742 | ** Function NDEF_RecGetPayload |
| 743 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 744 | ** Description This function gets a pointer to the payload for the given |
| 745 | ** NDEF record. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 746 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 747 | ** Returns a pointer to the payload (or NULL none). Payload len filled |
| 748 | ** in. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 749 | ** |
| 750 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 751 | uint8_t* NDEF_RecGetPayload(uint8_t* p_rec, uint32_t* p_payload_len) { |
| 752 | uint8_t rec_hdr, type_len, id_len; |
| 753 | uint32_t payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 754 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 755 | /* First byte is the record header */ |
| 756 | rec_hdr = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 757 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 758 | /* Next byte is the type field length */ |
| 759 | type_len = *p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 760 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 761 | /* Next is the payload length (1 or 4 bytes) */ |
| 762 | if (rec_hdr & NDEF_SR_MASK) |
| 763 | payload_len = *p_rec++; |
| 764 | else |
| 765 | BE_STREAM_TO_UINT32(payload_len, p_rec); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 766 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 767 | *p_payload_len = payload_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 768 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 769 | /* ID field Length */ |
| 770 | if (rec_hdr & NDEF_IL_MASK) |
| 771 | id_len = *p_rec++; |
| 772 | else |
| 773 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 774 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 775 | /* p_rec now points to the start of the type field. The ID field follows it, |
| 776 | * then the payload */ |
| 777 | if (payload_len == 0) |
| 778 | return (NULL); |
| 779 | else |
| 780 | return (p_rec + type_len + id_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 781 | } |
| 782 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 783 | /******************************************************************************* |
| 784 | ** |
| 785 | ** Function NDEF_MsgInit |
| 786 | ** |
| 787 | ** Description This function initializes an NDEF message. |
| 788 | ** |
| 789 | ** Returns void |
| 790 | ** *p_cur_size is initialized to 0 |
| 791 | ** |
| 792 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 793 | void NDEF_MsgInit(uint8_t* p_msg, uint32_t max_size, uint32_t* p_cur_size) { |
| 794 | *p_cur_size = 0; |
| 795 | memset(p_msg, 0, max_size); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /******************************************************************************* |
| 799 | ** |
| 800 | ** Function NDEF_MsgAddRec |
| 801 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 802 | ** Description This function adds an NDEF record to the end of an NDEF |
| 803 | ** message. |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 804 | ** |
| 805 | ** Returns OK, or error if the record did not fit |
| 806 | ** *p_cur_size is updated |
| 807 | ** |
| 808 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 809 | extern tNDEF_STATUS NDEF_MsgAddRec(uint8_t* p_msg, uint32_t max_size, |
| 810 | uint32_t* p_cur_size, uint8_t tnf, |
| 811 | uint8_t* p_type, uint8_t type_len, |
| 812 | uint8_t* p_id, uint8_t id_len, |
| 813 | uint8_t* p_payload, uint32_t payload_len) { |
| 814 | uint8_t* p_rec = p_msg + *p_cur_size; |
| 815 | uint32_t recSize; |
| 816 | int plen = (payload_len < 256) ? 1 : 4; |
| 817 | int ilen = (id_len == 0) ? 0 : 1; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 818 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 819 | if (tnf > NDEF_TNF_RESERVED) { |
| 820 | tnf = NDEF_TNF_UNKNOWN; |
| 821 | type_len = 0; |
| 822 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 823 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 824 | /* First, make sure the record will fit. we need at least 2 bytes for header |
| 825 | * and type length */ |
| 826 | recSize = payload_len + 2 + type_len + plen + ilen + id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 827 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 828 | if ((*p_cur_size + recSize) > max_size) return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 829 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 830 | /* Construct the record header. For the first record, set both begin and end |
| 831 | * bits */ |
| 832 | if (*p_cur_size == 0) |
| 833 | *p_rec = tnf | NDEF_MB_MASK | NDEF_ME_MASK; |
| 834 | else { |
| 835 | /* Find the previous last and clear his 'Message End' bit */ |
| 836 | uint8_t* pLast = NDEF_MsgGetLastRecInMsg(p_msg); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 837 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 838 | if (!pLast) return (NDEF_MSG_NO_MSG_END); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 839 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 840 | *pLast &= ~NDEF_ME_MASK; |
| 841 | *p_rec = tnf | NDEF_ME_MASK; |
| 842 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 843 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 844 | if (plen == 1) *p_rec |= NDEF_SR_MASK; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 845 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 846 | if (ilen != 0) *p_rec |= NDEF_IL_MASK; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 847 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 848 | p_rec++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 849 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 850 | /* The next byte is the type field length */ |
| 851 | *p_rec++ = type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 852 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 853 | /* Payload length - can be 1 or 4 bytes */ |
| 854 | if (plen == 1) |
| 855 | *p_rec++ = (uint8_t)payload_len; |
| 856 | else |
| 857 | UINT32_TO_BE_STREAM(p_rec, payload_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 858 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 859 | /* ID field Length (optional) */ |
| 860 | if (ilen > 0) *p_rec++ = id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 861 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 862 | /* Next comes the type */ |
| 863 | if (type_len) { |
| 864 | if (p_type) memcpy(p_rec, p_type, type_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 865 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 866 | p_rec += type_len; |
| 867 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 868 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 869 | /* Next comes the ID */ |
| 870 | if (id_len) { |
| 871 | if (p_id) memcpy(p_rec, p_id, id_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 872 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 873 | p_rec += id_len; |
| 874 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 875 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 876 | /* And lastly the payload. If NULL, the app just wants to reserve memory */ |
| 877 | if (p_payload) memcpy(p_rec, p_payload, payload_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 878 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 879 | *p_cur_size += recSize; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 880 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 881 | return (NDEF_OK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /******************************************************************************* |
| 885 | ** |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 886 | ** Function NDEF_MsgAppendPayload |
| 887 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 888 | ** Description This function appends extra payload to a specific record in |
| 889 | ** the given NDEF message |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 890 | ** |
| 891 | ** Returns OK, or error if the extra payload did not fit |
| 892 | ** *p_cur_size is updated |
| 893 | ** |
| 894 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 895 | tNDEF_STATUS NDEF_MsgAppendPayload(uint8_t* p_msg, uint32_t max_size, |
| 896 | uint32_t* p_cur_size, uint8_t* p_rec, |
| 897 | uint8_t* p_add_pl, uint32_t add_pl_len) { |
| 898 | uint32_t prev_paylen, new_paylen; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 899 | uint8_t *p_prev_pl, *pp; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 900 | uint8_t incr_lenfld = 0; |
| 901 | uint8_t type_len, id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 902 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 903 | /* Skip header */ |
| 904 | pp = p_rec + 1; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 905 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 906 | /* Next byte is the type field length */ |
| 907 | type_len = *pp++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 908 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 909 | /* Next is the payload length (1 or 4 bytes) */ |
| 910 | if (*p_rec & NDEF_SR_MASK) |
| 911 | prev_paylen = *pp++; |
| 912 | else |
| 913 | BE_STREAM_TO_UINT32(prev_paylen, pp); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 914 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 915 | /* ID field Length */ |
| 916 | if (*p_rec & NDEF_IL_MASK) |
| 917 | id_len = *pp++; |
| 918 | else |
| 919 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 920 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 921 | p_prev_pl = pp + type_len + id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 922 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 923 | new_paylen = prev_paylen + add_pl_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 924 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 925 | /* Previous payload may be < 256, and this addition may make it larger than |
| 926 | * 256 */ |
| 927 | /* If that were to happen, the payload length field goes from 1 byte to 4 |
| 928 | * bytes */ |
| 929 | if ((prev_paylen < 256) && (new_paylen > 255)) incr_lenfld = 3; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 930 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 931 | /* Check that it all fits */ |
| 932 | if ((*p_cur_size + add_pl_len + incr_lenfld) > max_size) |
| 933 | return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 934 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 935 | /* Point to payload length field */ |
| 936 | pp = p_rec + 2; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 937 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 938 | /* If we need to increase the length field from 1 to 4 bytes, do it first */ |
| 939 | if (incr_lenfld) { |
| 940 | shiftdown(pp + 1, (uint32_t)(*p_cur_size - (pp - p_msg) - 1), 3); |
| 941 | p_prev_pl += 3; |
| 942 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 943 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 944 | /* Store in the new length */ |
| 945 | if (new_paylen > 255) { |
| 946 | *p_rec &= ~NDEF_SR_MASK; |
| 947 | UINT32_TO_BE_STREAM(pp, new_paylen); |
| 948 | } else |
| 949 | *pp = (uint8_t)new_paylen; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 950 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 951 | /* Point to the end of the previous payload */ |
| 952 | pp = p_prev_pl + prev_paylen; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 953 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 954 | /* If we are not the last record, make space for the extra payload */ |
| 955 | if ((*p_rec & NDEF_ME_MASK) == 0) |
| 956 | shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), add_pl_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 957 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 958 | /* Now copy in the additional payload data */ |
| 959 | memcpy(pp, p_add_pl, add_pl_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 960 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 961 | *p_cur_size += add_pl_len + incr_lenfld; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 962 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 963 | return (NDEF_OK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /******************************************************************************* |
| 967 | ** |
| 968 | ** Function NDEF_MsgReplacePayload |
| 969 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 970 | ** Description This function replaces the payload of a specific record in |
| 971 | ** the given NDEF message |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 972 | ** |
| 973 | ** Returns OK, or error if the new payload did not fit |
| 974 | ** *p_cur_size is updated |
| 975 | ** |
| 976 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 977 | tNDEF_STATUS NDEF_MsgReplacePayload(uint8_t* p_msg, uint32_t max_size, |
| 978 | uint32_t* p_cur_size, uint8_t* p_rec, |
| 979 | uint8_t* p_new_pl, uint32_t new_pl_len) { |
| 980 | uint32_t prev_paylen; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 981 | uint8_t *p_prev_pl, *pp; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 982 | uint32_t paylen_delta; |
| 983 | uint8_t type_len, id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 984 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 985 | /* Skip header */ |
| 986 | pp = p_rec + 1; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 987 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 988 | /* Next byte is the type field length */ |
| 989 | type_len = *pp++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 990 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 991 | /* Next is the payload length (1 or 4 bytes) */ |
| 992 | if (*p_rec & NDEF_SR_MASK) |
| 993 | prev_paylen = *pp++; |
| 994 | else |
| 995 | BE_STREAM_TO_UINT32(prev_paylen, pp); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 996 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 997 | /* ID field Length */ |
| 998 | if (*p_rec & NDEF_IL_MASK) |
| 999 | id_len = *pp++; |
| 1000 | else |
| 1001 | id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1002 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1003 | p_prev_pl = pp + type_len + id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1004 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1005 | /* Point to payload length field again */ |
| 1006 | pp = p_rec + 2; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1007 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1008 | if (new_pl_len > prev_paylen) { |
| 1009 | /* New payload is larger than the previous */ |
| 1010 | paylen_delta = new_pl_len - prev_paylen; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1011 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1012 | /* If the previous payload length was < 256, and new is > 255 */ |
| 1013 | /* the payload length field goes from 1 byte to 4 bytes */ |
| 1014 | if ((prev_paylen < 256) && (new_pl_len > 255)) { |
| 1015 | if ((*p_cur_size + paylen_delta + 3) > max_size) |
| 1016 | return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1017 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1018 | shiftdown(pp + 1, (uint32_t)(*p_cur_size - (pp - p_msg) - 1), 3); |
| 1019 | p_prev_pl += 3; |
| 1020 | *p_cur_size += 3; |
| 1021 | *p_rec &= ~NDEF_SR_MASK; |
| 1022 | } else if ((*p_cur_size + paylen_delta) > max_size) |
| 1023 | return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1024 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1025 | /* Store in the new length */ |
| 1026 | if (new_pl_len > 255) { |
| 1027 | UINT32_TO_BE_STREAM(pp, new_pl_len); |
| 1028 | } else |
| 1029 | *pp = (uint8_t)new_pl_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1030 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1031 | /* Point to the end of the previous payload */ |
| 1032 | pp = p_prev_pl + prev_paylen; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1033 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1034 | /* If we are not the last record, make space for the extra payload */ |
| 1035 | if ((*p_rec & NDEF_ME_MASK) == 0) |
| 1036 | shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), paylen_delta); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1037 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1038 | *p_cur_size += paylen_delta; |
| 1039 | } else if (new_pl_len < prev_paylen) { |
| 1040 | /* New payload is smaller than the previous */ |
| 1041 | paylen_delta = prev_paylen - new_pl_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1042 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1043 | /* If the previous payload was > 256, and new is less than 256 */ |
| 1044 | /* the payload length field goes from 4 bytes to 1 byte */ |
| 1045 | if ((prev_paylen > 255) && (new_pl_len < 256)) { |
| 1046 | shiftup(pp + 1, pp + 4, (uint32_t)(*p_cur_size - (pp - p_msg) - 3)); |
| 1047 | p_prev_pl -= 3; |
| 1048 | *p_cur_size -= 3; |
| 1049 | *p_rec |= NDEF_SR_MASK; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1050 | } |
| 1051 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1052 | /* Store in the new length */ |
| 1053 | if (new_pl_len > 255) { |
| 1054 | UINT32_TO_BE_STREAM(pp, new_pl_len); |
| 1055 | } else |
| 1056 | *pp = (uint8_t)new_pl_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1057 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1058 | /* Point to the end of the previous payload */ |
| 1059 | pp = p_prev_pl + prev_paylen; |
| 1060 | |
| 1061 | /* If we are not the last record, remove the extra space from the previous |
| 1062 | * payload */ |
| 1063 | if ((*p_rec & NDEF_ME_MASK) == 0) |
| 1064 | shiftup(pp - paylen_delta, pp, (uint32_t)(*p_cur_size - (pp - p_msg))); |
| 1065 | |
| 1066 | *p_cur_size -= paylen_delta; |
| 1067 | } |
| 1068 | |
| 1069 | /* Now copy in the new payload data */ |
| 1070 | if (p_new_pl) memcpy(p_prev_pl, p_new_pl, new_pl_len); |
| 1071 | |
| 1072 | return (NDEF_OK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /******************************************************************************* |
| 1076 | ** |
| 1077 | ** Function NDEF_MsgReplaceType |
| 1078 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1079 | ** Description This function replaces the type field of a specific record |
| 1080 | ** in the given NDEF message |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1081 | ** |
| 1082 | ** Returns OK, or error if the new type field did not fit |
| 1083 | ** *p_cur_size is updated |
| 1084 | ** |
| 1085 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1086 | tNDEF_STATUS NDEF_MsgReplaceType(uint8_t* p_msg, uint32_t max_size, |
| 1087 | uint32_t* p_cur_size, uint8_t* p_rec, |
| 1088 | uint8_t* p_new_type, uint8_t new_type_len) { |
| 1089 | uint8_t typelen_delta; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 1090 | uint8_t *p_prev_type, prev_type_len; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1091 | uint8_t* pp; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1092 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1093 | /* Skip header */ |
| 1094 | pp = p_rec + 1; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1095 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1096 | /* Next byte is the type field length */ |
| 1097 | prev_type_len = *pp++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1098 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1099 | /* Skip the payload length */ |
| 1100 | if (*p_rec & NDEF_SR_MASK) |
| 1101 | pp += 1; |
| 1102 | else |
| 1103 | pp += 4; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1104 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1105 | if (*p_rec & NDEF_IL_MASK) pp++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1106 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1107 | /* Save pointer to the start of the type field */ |
| 1108 | p_prev_type = pp; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1109 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1110 | if (new_type_len > prev_type_len) { |
| 1111 | /* New type is larger than the previous */ |
| 1112 | typelen_delta = new_type_len - prev_type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1113 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1114 | if ((*p_cur_size + typelen_delta) > max_size) |
| 1115 | return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1116 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1117 | /* Point to the end of the previous type, and make space for the extra data |
| 1118 | */ |
| 1119 | pp = p_prev_type + prev_type_len; |
| 1120 | shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), typelen_delta); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1121 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1122 | *p_cur_size += typelen_delta; |
| 1123 | } else if (new_type_len < prev_type_len) { |
| 1124 | /* New type field is smaller than the previous */ |
| 1125 | typelen_delta = prev_type_len - new_type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1126 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1127 | /* Point to the end of the previous type, and shift up to fill the the |
| 1128 | * unused space */ |
| 1129 | pp = p_prev_type + prev_type_len; |
| 1130 | shiftup(pp - typelen_delta, pp, (uint32_t)(*p_cur_size - (pp - p_msg))); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1131 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1132 | *p_cur_size -= typelen_delta; |
| 1133 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1134 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1135 | /* Save in new type length */ |
| 1136 | p_rec[1] = new_type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1137 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1138 | /* Now copy in the new type field data */ |
| 1139 | if (p_new_type) memcpy(p_prev_type, p_new_type, new_type_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1140 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1141 | return (NDEF_OK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /******************************************************************************* |
| 1145 | ** |
| 1146 | ** Function NDEF_MsgReplaceId |
| 1147 | ** |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1148 | ** Description This function replaces the ID field of a specific record in |
| 1149 | ** the given NDEF message |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1150 | ** |
| 1151 | ** Returns OK, or error if the new ID field did not fit |
| 1152 | ** *p_cur_size is updated |
| 1153 | ** |
| 1154 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1155 | tNDEF_STATUS NDEF_MsgReplaceId(uint8_t* p_msg, uint32_t max_size, |
| 1156 | uint32_t* p_cur_size, uint8_t* p_rec, |
| 1157 | uint8_t* p_new_id, uint8_t new_id_len) { |
| 1158 | uint8_t idlen_delta; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 1159 | uint8_t *p_prev_id, *p_idlen_field; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1160 | uint8_t prev_id_len, type_len; |
| 1161 | uint8_t* pp; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1162 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1163 | /* Skip header */ |
| 1164 | pp = p_rec + 1; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1165 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1166 | /* Next byte is the type field length */ |
| 1167 | type_len = *pp++; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1168 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1169 | /* Skip the payload length */ |
| 1170 | if (*p_rec & NDEF_SR_MASK) |
| 1171 | pp += 1; |
| 1172 | else |
| 1173 | pp += 4; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1174 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1175 | p_idlen_field = pp; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1176 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1177 | if (*p_rec & NDEF_IL_MASK) |
| 1178 | prev_id_len = *pp++; |
| 1179 | else |
| 1180 | prev_id_len = 0; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1181 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1182 | /* Save pointer to the start of the ID field (right after the type field) */ |
| 1183 | p_prev_id = pp + type_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1184 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1185 | if (new_id_len > prev_id_len) { |
| 1186 | /* New ID field is larger than the previous */ |
| 1187 | idlen_delta = new_id_len - prev_id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1188 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1189 | /* If the previous ID length was 0, we need to add a 1-byte ID length */ |
| 1190 | if (prev_id_len == 0) { |
| 1191 | if ((*p_cur_size + idlen_delta + 1) > max_size) |
| 1192 | return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1193 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1194 | shiftdown(p_idlen_field, |
| 1195 | (uint32_t)(*p_cur_size - (p_idlen_field - p_msg)), 1); |
| 1196 | p_prev_id += 1; |
| 1197 | *p_cur_size += 1; |
| 1198 | *p_rec |= NDEF_IL_MASK; |
| 1199 | } else if ((*p_cur_size + idlen_delta) > max_size) |
| 1200 | return (NDEF_MSG_INSUFFICIENT_MEM); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1201 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1202 | /* Point to the end of the previous ID field, and make space for the extra |
| 1203 | * data */ |
| 1204 | pp = p_prev_id + prev_id_len; |
| 1205 | shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), idlen_delta); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1206 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1207 | *p_cur_size += idlen_delta; |
| 1208 | } else if (new_id_len < prev_id_len) { |
| 1209 | /* New ID field is smaller than the previous */ |
| 1210 | idlen_delta = prev_id_len - new_id_len; |
| 1211 | |
| 1212 | /* Point to the end of the previous ID, and shift up to fill the the unused |
| 1213 | * space */ |
| 1214 | pp = p_prev_id + prev_id_len; |
| 1215 | shiftup(pp - idlen_delta, pp, (uint32_t)(*p_cur_size - (pp - p_msg))); |
| 1216 | |
| 1217 | *p_cur_size -= idlen_delta; |
| 1218 | |
| 1219 | /* If removing the ID, make sure that length field is also removed */ |
| 1220 | if (new_id_len == 0) { |
| 1221 | shiftup(p_idlen_field, p_idlen_field + 1, |
| 1222 | (uint32_t)(*p_cur_size - (p_idlen_field - p_msg - (uint32_t)1))); |
| 1223 | *p_rec &= ~NDEF_IL_MASK; |
| 1224 | *p_cur_size -= 1; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1225 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1226 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1227 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1228 | /* Save in new ID length and data */ |
| 1229 | if (new_id_len) { |
| 1230 | *p_idlen_field = new_id_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1231 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1232 | if (p_new_id) memcpy(p_prev_id, p_new_id, new_id_len); |
| 1233 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1234 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1235 | return (NDEF_OK); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /******************************************************************************* |
| 1239 | ** |
| 1240 | ** Function NDEF_MsgRemoveRec |
| 1241 | ** |
| 1242 | ** Description This function removes the record at the given |
| 1243 | ** index in the given NDEF message. |
| 1244 | ** |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 1245 | ** Returns TRUE if OK, FALSE if the index was invalid |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1246 | ** *p_cur_size is updated |
| 1247 | ** |
| 1248 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1249 | tNDEF_STATUS NDEF_MsgRemoveRec(uint8_t* p_msg, uint32_t* p_cur_size, |
| 1250 | int32_t index) { |
| 1251 | uint8_t* p_rec = NDEF_MsgGetRecByIndex(p_msg, index); |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 1252 | uint8_t *pNext, *pPrev; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1253 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1254 | if (!p_rec) return (NDEF_REC_NOT_FOUND); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1255 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1256 | /* If this is the first record in the message... */ |
| 1257 | if (*p_rec & NDEF_MB_MASK) { |
| 1258 | /* Find the second record (if any) and set his 'Message Begin' bit */ |
| 1259 | pNext = NDEF_MsgGetRecByIndex(p_msg, 1); |
| 1260 | if (pNext != NULL) { |
| 1261 | *pNext |= NDEF_MB_MASK; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1262 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1263 | *p_cur_size -= (uint32_t)(pNext - p_msg); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1264 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1265 | shiftup(p_msg, pNext, *p_cur_size); |
| 1266 | } else |
| 1267 | *p_cur_size = 0; /* No more records, lenght must be zero */ |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1268 | |
| 1269 | return (NDEF_OK); |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1270 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1271 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1272 | /* If this is the last record in the message... */ |
| 1273 | if (*p_rec & NDEF_ME_MASK) { |
| 1274 | if (index > 0) { |
| 1275 | /* Find the previous record and set his 'Message End' bit */ |
| 1276 | pPrev = NDEF_MsgGetRecByIndex(p_msg, index - 1); |
| 1277 | if (pPrev == NULL) return false; |
| 1278 | |
| 1279 | *pPrev |= NDEF_ME_MASK; |
| 1280 | } |
| 1281 | *p_cur_size = (uint32_t)(p_rec - p_msg); |
| 1282 | |
| 1283 | return (NDEF_OK); |
| 1284 | } |
| 1285 | |
| 1286 | /* Not the first or the last... get the address of the next record */ |
| 1287 | pNext = NDEF_MsgGetNextRec(p_rec); |
| 1288 | if (pNext == NULL) return false; |
| 1289 | |
| 1290 | /* We are removing p_rec, so shift from pNext to the end */ |
| 1291 | shiftup(p_rec, pNext, (uint32_t)(*p_cur_size - (pNext - p_msg))); |
| 1292 | |
| 1293 | *p_cur_size -= (uint32_t)(pNext - p_rec); |
| 1294 | |
| 1295 | return (NDEF_OK); |
| 1296 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1297 | |
| 1298 | /******************************************************************************* |
| 1299 | ** |
| 1300 | ** Function NDEF_MsgCopyAndDechunk |
| 1301 | ** |
| 1302 | ** Description This function copies and de-chunks an NDEF message. |
| 1303 | ** It is assumed that the destination is at least as large |
| 1304 | ** as the source, since the source may not actually contain |
| 1305 | ** any chunks. |
| 1306 | ** |
| 1307 | ** Returns The output byte count |
| 1308 | ** |
| 1309 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1310 | tNDEF_STATUS NDEF_MsgCopyAndDechunk(uint8_t* p_src, uint32_t src_len, |
| 1311 | uint8_t* p_dest, uint32_t* p_out_len) { |
| 1312 | uint32_t out_len, max_out_len; |
| 1313 | uint8_t* p_rec; |
| 1314 | uint8_t* p_prev_rec = p_dest; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 1315 | uint8_t *p_type, *p_id, *p_pay; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1316 | uint8_t type_len, id_len, tnf; |
| 1317 | uint32_t pay_len; |
| 1318 | tNDEF_STATUS status; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1319 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1320 | /* First, validate the source */ |
| 1321 | status = NDEF_MsgValidate(p_src, src_len, true); |
| 1322 | if (status != NDEF_OK) return (status); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1323 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1324 | /* The output buffer must be at least as large as the input buffer */ |
| 1325 | max_out_len = src_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1326 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1327 | /* Initialize output */ |
| 1328 | NDEF_MsgInit(p_dest, max_out_len, &out_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1329 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1330 | p_rec = p_src; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1331 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1332 | /* Now, copy record by record */ |
| 1333 | while ((p_rec != NULL) && (status == NDEF_OK)) { |
| 1334 | p_type = NDEF_RecGetType(p_rec, &tnf, &type_len); |
| 1335 | p_id = NDEF_RecGetId(p_rec, &id_len); |
| 1336 | p_pay = NDEF_RecGetPayload(p_rec, &pay_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1337 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1338 | /* If this is the continuation of a chunk, append the payload to the |
| 1339 | * previous */ |
| 1340 | if (tnf == NDEF_TNF_UNCHANGED) { |
| 1341 | if (p_pay) { |
| 1342 | status = NDEF_MsgAppendPayload(p_dest, max_out_len, &out_len, |
| 1343 | p_prev_rec, p_pay, pay_len); |
| 1344 | } |
| 1345 | } else { |
| 1346 | p_prev_rec = p_dest + out_len; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1347 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1348 | status = NDEF_MsgAddRec(p_dest, max_out_len, &out_len, tnf, p_type, |
| 1349 | type_len, p_id, id_len, p_pay, pay_len); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1350 | } |
| 1351 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1352 | p_rec = NDEF_MsgGetNextRec(p_rec); |
| 1353 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1354 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 1355 | *p_out_len = out_len; |
| 1356 | |
| 1357 | return (status); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 1358 | } |