blob: 993efd8e6a1feeda37bbf28f09dd19b6e22eb178 [file] [log] [blame]
nxpandroidc7611652015-09-23 16:42:05 +05301/******************************************************************************
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
nxpandroidc7611652015-09-23 16:42:05 +053019/******************************************************************************
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 ******************************************************************************/
nxpandroidc7611652015-09-23 16:42:05 +053025#include "ndef_utils.h"
nxf24591c1cbeab2018-02-21 17:32:26 +053026#include <string.h>
nxpandroidc7611652015-09-23 16:42:05 +053027
28/*******************************************************************************
29**
30** Static Local Functions
31**
32*******************************************************************************/
33
nxpandroidc7611652015-09-23 16:42:05 +053034/*******************************************************************************
35**
36** Function shiftdown
37**
38** Description shift memory down (to make space to insert a record)
39**
40*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053041static void shiftdown(uint8_t* p_mem, uint32_t len, uint32_t shift_amount) {
nxf24591c1cbeab2018-02-21 17:32:26 +053042 uint8_t* ps = p_mem + len - 1;
43 uint8_t* pd = ps + shift_amount;
44 uint32_t xx;
nxpandroidc7611652015-09-23 16:42:05 +053045
nxpandroid8f6d0532017-07-12 18:25:30 +053046 for (xx = 0; xx < len; xx++) *pd-- = *ps--;
nxpandroidc7611652015-09-23 16:42:05 +053047}
48
49/*******************************************************************************
50**
51** Function shiftup
52**
53** Description shift memory up (to delete a record)
54**
55*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053056static void shiftup(uint8_t* p_dest, uint8_t* p_src, uint32_t len) {
nxf24591c1cbeab2018-02-21 17:32:26 +053057 uint8_t* ps = p_src;
58 uint8_t* pd = p_dest;
59 uint32_t xx;
nxpandroidc7611652015-09-23 16:42:05 +053060
nxpandroid8f6d0532017-07-12 18:25:30 +053061 for (xx = 0; xx < len; xx++) *pd++ = *ps++;
nxpandroidc7611652015-09-23 16:42:05 +053062}
63
64/*******************************************************************************
65**
66** Function NDEF_MsgValidate
67**
68** Description This function validates an NDEF message.
69**
nxf24591c1cbeab2018-02-21 17:32:26 +053070** Returns TRUE if all OK, or FALSE if the message is invalid.
nxpandroidc7611652015-09-23 16:42:05 +053071**
72*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053073tNDEF_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;
nxpandroidc7611652015-09-23 16:42:05 +053081
nxpandroid8f6d0532017-07-12 18:25:30 +053082 if ((p_msg == NULL) || (msg_len < 3)) return (NDEF_MSG_TOO_SHORT);
nxpandroidc7611652015-09-23 16:42:05 +053083
nxpandroid8f6d0532017-07-12 18:25:30 +053084 /* The first record must have the MB bit set */
85 if ((*p_msg & NDEF_MB_MASK) == 0) return (NDEF_MSG_NO_MSG_BEGIN);
nxpandroidc7611652015-09-23 16:42:05 +053086
nxpandroid8f6d0532017-07-12 18:25:30 +053087 /* The first record cannot be a chunk */
88 if ((*p_msg & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
89 return (NDEF_MSG_UNEXPECTED_CHUNK);
nxpandroidc7611652015-09-23 16:42:05 +053090
nxpandroid8f6d0532017-07-12 18:25:30 +053091 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);
nxpandroidc7611652015-09-23 16:42:05 +053094
nxpandroid8f6d0532017-07-12 18:25:30 +053095 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +053096
nxpandroid8f6d0532017-07-12 18:25:30 +053097 /* header should have a valid TNF */
98 if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_MASK)
99 return NDEF_MSG_INVALID_CHUNK;
nxpandroidc7611652015-09-23 16:42:05 +0530100
nxpandroid8f6d0532017-07-12 18:25:30 +0530101 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530104
nxpandroid8f6d0532017-07-12 18:25:30 +0530105 /* Type field length */
106 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530107
nxpandroid8f6d0532017-07-12 18:25:30 +0530108 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530113
nxpandroid8f6d0532017-07-12 18:25:30 +0530114 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530120
nxpandroid8f6d0532017-07-12 18:25:30 +0530121 BE_STREAM_TO_UINT32(payload_len, p_rec);
nxpandroidc7611652015-09-23 16:42:05 +0530122 }
123
nxpandroid8f6d0532017-07-12 18:25:30 +0530124 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530128
nxpandroid8f6d0532017-07-12 18:25:30 +0530129 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 }
nxpandroidc7611652015-09-23 16:42:05 +0530136
nxpandroid8f6d0532017-07-12 18:25:30 +0530137 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530213}
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530225int32_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;
nxpandroidc7611652015-09-23 16:42:05 +0530230
nxpandroid8f6d0532017-07-12 18:25:30 +0530231 for (count = 0;;) {
232 count++;
nxpandroidc7611652015-09-23 16:42:05 +0530233
nxpandroid8f6d0532017-07-12 18:25:30 +0530234 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530235
nxpandroid8f6d0532017-07-12 18:25:30 +0530236 if (rec_hdr & NDEF_ME_MASK) break;
nxpandroidc7611652015-09-23 16:42:05 +0530237
nxpandroid8f6d0532017-07-12 18:25:30 +0530238 /* Type field length */
239 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530240
nxpandroid8f6d0532017-07-12 18:25:30 +0530241 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530246
nxpandroid8f6d0532017-07-12 18:25:30 +0530247 /* ID field Length */
248 if (rec_hdr & NDEF_IL_MASK)
249 id_len = *p_rec++;
250 else
251 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530252
nxpandroid8f6d0532017-07-12 18:25:30 +0530253 /* Point to next record */
254 p_rec += (payload_len + type_len + id_len);
255 }
nxpandroidc7611652015-09-23 16:42:05 +0530256
nxpandroid8f6d0532017-07-12 18:25:30 +0530257 /* Return the number of records found */
258 return (count);
nxpandroidc7611652015-09-23 16:42:05 +0530259}
260
261/*******************************************************************************
262**
263** Function NDEF_MsgGetRecLength
264**
nxpandroid8f6d0532017-07-12 18:25:30 +0530265** Description This function returns length of the current record in the
266** given NDEF message.
nxpandroidc7611652015-09-23 16:42:05 +0530267**
268** Returns Length of record
269**
270*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530271uint32_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;
nxpandroidc7611652015-09-23 16:42:05 +0530275
nxpandroid8f6d0532017-07-12 18:25:30 +0530276 /* 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++;
nxpandroidc7611652015-09-23 16:42:05 +0530287 rec_len++;
nxpandroid8f6d0532017-07-12 18:25:30 +0530288 } else {
289 BE_STREAM_TO_UINT32(payload_len, p_cur_rec);
290 rec_len += 4;
291 }
nxpandroidc7611652015-09-23 16:42:05 +0530292
nxpandroid8f6d0532017-07-12 18:25:30 +0530293 /* ID field Length */
294 if (rec_hdr & NDEF_IL_MASK) {
295 id_len = *p_cur_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530296 rec_len++;
nxpandroid8f6d0532017-07-12 18:25:30 +0530297 } else
298 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530299
nxpandroid8f6d0532017-07-12 18:25:30 +0530300 /* Total length of record */
301 rec_len += (payload_len + type_len + id_len);
nxpandroidc7611652015-09-23 16:42:05 +0530302
nxpandroid8f6d0532017-07-12 18:25:30 +0530303 return (rec_len);
nxpandroidc7611652015-09-23 16:42:05 +0530304}
305
306/*******************************************************************************
307**
308** Function NDEF_MsgGetNextRec
309**
310** Description This function gets a pointer to the next record in the given
nxpandroid8f6d0532017-07-12 18:25:30 +0530311** NDEF message. If the current record pointer is NULL, a
312** pointer to the first record is returned.
nxpandroidc7611652015-09-23 16:42:05 +0530313**
314** Returns Pointer to the start of the record, or NULL if no more
315**
316*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530317uint8_t* NDEF_MsgGetNextRec(uint8_t* p_cur_rec) {
318 uint8_t rec_hdr, type_len, id_len;
319 uint32_t payload_len;
nxpandroidc7611652015-09-23 16:42:05 +0530320
nxpandroid8f6d0532017-07-12 18:25:30 +0530321 /* Get the current record's header */
322 rec_hdr = *p_cur_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530323
nxpandroid8f6d0532017-07-12 18:25:30 +0530324 /* If this is the last record, return NULL */
325 if (rec_hdr & NDEF_ME_MASK) return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530326
nxpandroid8f6d0532017-07-12 18:25:30 +0530327 /* Type field length */
328 type_len = *p_cur_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530329
nxpandroid8f6d0532017-07-12 18:25:30 +0530330 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530335
nxpandroid8f6d0532017-07-12 18:25:30 +0530336 /* ID field Length */
337 if (rec_hdr & NDEF_IL_MASK)
338 id_len = *p_cur_rec++;
339 else
340 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530341
nxpandroid8f6d0532017-07-12 18:25:30 +0530342 /* Point to next record */
343 p_cur_rec += (payload_len + type_len + id_len);
nxpandroidc7611652015-09-23 16:42:05 +0530344
nxpandroid8f6d0532017-07-12 18:25:30 +0530345 return (p_cur_rec);
nxpandroidc7611652015-09-23 16:42:05 +0530346}
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530358uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530363
nxpandroid8f6d0532017-07-12 18:25:30 +0530364 for (count = 0;; count++) {
365 if (count == index) return (p_rec);
nxpandroidc7611652015-09-23 16:42:05 +0530366
nxpandroid8f6d0532017-07-12 18:25:30 +0530367 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530368
nxpandroid8f6d0532017-07-12 18:25:30 +0530369 if (rec_hdr & NDEF_ME_MASK) return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530370
nxpandroid8f6d0532017-07-12 18:25:30 +0530371 /* Type field length */
372 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530373
nxpandroid8f6d0532017-07-12 18:25:30 +0530374 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530379
nxpandroid8f6d0532017-07-12 18:25:30 +0530380 /* ID field Length */
381 if (rec_hdr & NDEF_IL_MASK)
382 id_len = *p_rec++;
383 else
384 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530385
nxpandroid8f6d0532017-07-12 18:25:30 +0530386 /* Point to next record */
387 p_rec += (payload_len + type_len + id_len);
388 }
nxpandroidc7611652015-09-23 16:42:05 +0530389
nxpandroid8f6d0532017-07-12 18:25:30 +0530390 /* If here, there is no record of that index */
391 return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530392}
393
nxpandroidc7611652015-09-23 16:42:05 +0530394/*******************************************************************************
395**
396** Function NDEF_MsgGetLastRecInMsg
397**
398** Description This function gets a pointer to the last record in the
399** given NDEF message.
400**
nxpandroid8f6d0532017-07-12 18:25:30 +0530401** Returns Pointer to the start of the last record, or NULL if some
402** problem
nxpandroidc7611652015-09-23 16:42:05 +0530403**
404*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530405uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530410
nxpandroid8f6d0532017-07-12 18:25:30 +0530411 for (;;) {
412 pRecStart = p_rec;
413 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530414
nxpandroid8f6d0532017-07-12 18:25:30 +0530415 if (rec_hdr & NDEF_ME_MASK) break;
nxpandroidc7611652015-09-23 16:42:05 +0530416
nxpandroid8f6d0532017-07-12 18:25:30 +0530417 /* Type field length */
418 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530419
nxpandroid8f6d0532017-07-12 18:25:30 +0530420 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530425
nxpandroid8f6d0532017-07-12 18:25:30 +0530426 /* ID field Length */
427 if (rec_hdr & NDEF_IL_MASK)
428 id_len = *p_rec++;
429 else
430 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530431
nxpandroid8f6d0532017-07-12 18:25:30 +0530432 /* Point to next record */
433 p_rec += (payload_len + type_len + id_len);
434 }
nxpandroidc7611652015-09-23 16:42:05 +0530435
nxpandroid8f6d0532017-07-12 18:25:30 +0530436 return (pRecStart);
nxpandroidc7611652015-09-23 16:42:05 +0530437}
438
nxpandroidc7611652015-09-23 16:42:05 +0530439/*******************************************************************************
440**
441** Function NDEF_MsgGetFirstRecByType
442**
nxpandroid8f6d0532017-07-12 18:25:30 +0530443** Description This function gets a pointer to the first record with the
444** given record type in the given NDEF message.
nxpandroidc7611652015-09-23 16:42:05 +0530445**
446** Returns Pointer to the start of the record, or NULL
447**
448*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530449uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530455
nxpandroid8f6d0532017-07-12 18:25:30 +0530456 for (;;) {
457 pRecStart = p_rec;
nxpandroidc7611652015-09-23 16:42:05 +0530458
nxpandroid8f6d0532017-07-12 18:25:30 +0530459 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530460
nxpandroid8f6d0532017-07-12 18:25:30 +0530461 /* Type field length */
462 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530463
nxpandroid8f6d0532017-07-12 18:25:30 +0530464 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530469
nxpandroid8f6d0532017-07-12 18:25:30 +0530470 /* ID field Length */
471 if (rec_hdr & NDEF_IL_MASK)
472 id_len = *p_rec++;
473 else
474 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530475
nxpandroid8f6d0532017-07-12 18:25:30 +0530476 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530481
nxpandroid8f6d0532017-07-12 18:25:30 +0530482 /* If this was the last record, return NULL */
483 if (rec_hdr & NDEF_ME_MASK) return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530484
nxpandroid8f6d0532017-07-12 18:25:30 +0530485 /* Point to next record */
486 p_rec += (payload_len + type_len + id_len);
487 }
nxpandroidc7611652015-09-23 16:42:05 +0530488
nxpandroid8f6d0532017-07-12 18:25:30 +0530489 /* If here, there is no record of that type */
490 return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530491}
492
493/*******************************************************************************
494**
495** Function NDEF_MsgGetNextRecByType
496**
nxpandroid8f6d0532017-07-12 18:25:30 +0530497** Description This function gets a pointer to the next record with the
498** given record type in the given NDEF message.
nxpandroidc7611652015-09-23 16:42:05 +0530499**
500** Returns Pointer to the start of the record, or NULL
501**
502*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530503uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530509
nxpandroid8f6d0532017-07-12 18:25:30 +0530510 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530513
nxpandroid8f6d0532017-07-12 18:25:30 +0530514 for (;;) {
515 pRecStart = p_rec;
nxpandroidc7611652015-09-23 16:42:05 +0530516
nxpandroid8f6d0532017-07-12 18:25:30 +0530517 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530518
nxpandroid8f6d0532017-07-12 18:25:30 +0530519 /* Type field length */
520 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530521
nxpandroid8f6d0532017-07-12 18:25:30 +0530522 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530527
nxpandroid8f6d0532017-07-12 18:25:30 +0530528 /* ID field Length */
529 if (rec_hdr & NDEF_IL_MASK)
530 id_len = *p_rec++;
531 else
532 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530533
nxpandroid8f6d0532017-07-12 18:25:30 +0530534 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530539
nxpandroid8f6d0532017-07-12 18:25:30 +0530540 /* If this was the last record, return NULL */
541 if (rec_hdr & NDEF_ME_MASK) break;
nxpandroidc7611652015-09-23 16:42:05 +0530542
nxpandroid8f6d0532017-07-12 18:25:30 +0530543 /* Point to next record */
544 p_rec += (payload_len + type_len + id_len);
545 }
nxpandroidc7611652015-09-23 16:42:05 +0530546
nxpandroid8f6d0532017-07-12 18:25:30 +0530547 /* If here, there is no record of that type */
548 return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530549}
550
nxpandroidc7611652015-09-23 16:42:05 +0530551/*******************************************************************************
552**
553** Function NDEF_MsgGetFirstRecById
554**
nxpandroid8f6d0532017-07-12 18:25:30 +0530555** Description This function gets a pointer to the first record with the
556** given record id in the given NDEF message.
nxpandroidc7611652015-09-23 16:42:05 +0530557**
558** Returns Pointer to the start of the record, or NULL
559**
560*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530561uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530566
nxpandroid8f6d0532017-07-12 18:25:30 +0530567 for (;;) {
568 pRecStart = p_rec;
nxpandroidc7611652015-09-23 16:42:05 +0530569
nxpandroid8f6d0532017-07-12 18:25:30 +0530570 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530571
nxpandroid8f6d0532017-07-12 18:25:30 +0530572 /* Type field length */
573 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530574
nxpandroid8f6d0532017-07-12 18:25:30 +0530575 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530580
nxpandroid8f6d0532017-07-12 18:25:30 +0530581 /* ID field Length */
582 if (rec_hdr & NDEF_IL_MASK)
583 id_len = *p_rec++;
584 else
585 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530586
nxpandroid8f6d0532017-07-12 18:25:30 +0530587 /* At this point, p_rec points to the start of the type field. Skip it */
588 p_rec += type_len;
nxpandroidc7611652015-09-23 16:42:05 +0530589
nxpandroid8f6d0532017-07-12 18:25:30 +0530590 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530593
nxpandroid8f6d0532017-07-12 18:25:30 +0530594 /* If this was the last record, return NULL */
595 if (rec_hdr & NDEF_ME_MASK) return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530596
nxpandroid8f6d0532017-07-12 18:25:30 +0530597 /* Point to next record */
598 p_rec += (id_len + payload_len);
599 }
nxpandroidc7611652015-09-23 16:42:05 +0530600
nxpandroid8f6d0532017-07-12 18:25:30 +0530601 /* If here, there is no record of that ID */
602 return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530603}
604
605/*******************************************************************************
606**
607** Function NDEF_MsgGetNextRecById
608**
nxpandroid8f6d0532017-07-12 18:25:30 +0530609** Description This function gets a pointer to the next record with the
610** given record id in the given NDEF message.
nxpandroidc7611652015-09-23 16:42:05 +0530611**
612** Returns Pointer to the start of the record, or NULL
613**
614*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530615uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530621
nxpandroid8f6d0532017-07-12 18:25:30 +0530622 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530625
nxpandroid8f6d0532017-07-12 18:25:30 +0530626 for (;;) {
627 pRecStart = p_rec;
nxpandroidc7611652015-09-23 16:42:05 +0530628
nxpandroid8f6d0532017-07-12 18:25:30 +0530629 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530630
nxpandroid8f6d0532017-07-12 18:25:30 +0530631 /* Type field length */
632 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530633
nxpandroid8f6d0532017-07-12 18:25:30 +0530634 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530639
nxpandroid8f6d0532017-07-12 18:25:30 +0530640 /* ID field Length */
641 if (rec_hdr & NDEF_IL_MASK)
642 id_len = *p_rec++;
643 else
644 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530645
nxpandroid8f6d0532017-07-12 18:25:30 +0530646 /* At this point, p_rec points to the start of the type field. Skip it */
647 p_rec += type_len;
nxpandroidc7611652015-09-23 16:42:05 +0530648
nxpandroid8f6d0532017-07-12 18:25:30 +0530649 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530652
nxpandroid8f6d0532017-07-12 18:25:30 +0530653 /* If this was the last record, return NULL */
654 if (rec_hdr & NDEF_ME_MASK) break;
nxpandroidc7611652015-09-23 16:42:05 +0530655
nxpandroid8f6d0532017-07-12 18:25:30 +0530656 /* Point to next record */
657 p_rec += (id_len + payload_len);
658 }
nxpandroidc7611652015-09-23 16:42:05 +0530659
nxpandroid8f6d0532017-07-12 18:25:30 +0530660 /* If here, there is no record of that ID */
661 return (NULL);
nxpandroidc7611652015-09-23 16:42:05 +0530662}
663
664/*******************************************************************************
665**
666** Function NDEF_RecGetType
667**
nxpandroid8f6d0532017-07-12 18:25:30 +0530668** Description This function gets a pointer to the record type for the
669** given NDEF record.
nxpandroidc7611652015-09-23 16:42:05 +0530670**
671** Returns Pointer to Type (NULL if none). TNF and len are filled in.
672**
673*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530674uint8_t* NDEF_RecGetType(uint8_t* p_rec, uint8_t* p_tnf, uint8_t* p_type_len) {
675 uint8_t rec_hdr, type_len;
nxpandroidc7611652015-09-23 16:42:05 +0530676
nxpandroid8f6d0532017-07-12 18:25:30 +0530677 /* First byte is the record header */
678 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530679
nxpandroid8f6d0532017-07-12 18:25:30 +0530680 /* Next byte is the type field length */
681 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530682
nxpandroid8f6d0532017-07-12 18:25:30 +0530683 /* Skip the payload length */
684 if (rec_hdr & NDEF_SR_MASK)
685 p_rec += 1;
686 else
687 p_rec += 4;
nxpandroidc7611652015-09-23 16:42:05 +0530688
nxpandroid8f6d0532017-07-12 18:25:30 +0530689 /* Skip ID field Length, if present */
690 if (rec_hdr & NDEF_IL_MASK) p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530691
nxpandroid8f6d0532017-07-12 18:25:30 +0530692 /* At this point, p_rec points to the start of the type field. */
693 *p_type_len = type_len;
nxf24591c1cbeab2018-02-21 17:32:26 +0530694 *p_tnf = rec_hdr & NDEF_TNF_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530695
nxpandroid8f6d0532017-07-12 18:25:30 +0530696 if (type_len == 0)
697 return (NULL);
698 else
699 return (p_rec);
nxpandroidc7611652015-09-23 16:42:05 +0530700}
701
702/*******************************************************************************
703**
704** Function NDEF_RecGetId
705**
nxpandroid8f6d0532017-07-12 18:25:30 +0530706** Description This function gets a pointer to the record id for the given
707** NDEF record.
nxpandroidc7611652015-09-23 16:42:05 +0530708**
709** Returns Pointer to Id (NULL if none). ID Len is filled in.
710**
711*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530712uint8_t* NDEF_RecGetId(uint8_t* p_rec, uint8_t* p_id_len) {
713 uint8_t rec_hdr, type_len;
nxpandroidc7611652015-09-23 16:42:05 +0530714
nxpandroid8f6d0532017-07-12 18:25:30 +0530715 /* First byte is the record header */
716 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530717
nxpandroid8f6d0532017-07-12 18:25:30 +0530718 /* Next byte is the type field length */
719 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530720
nxpandroid8f6d0532017-07-12 18:25:30 +0530721 /* Skip the payload length */
722 if (rec_hdr & NDEF_SR_MASK)
723 p_rec++;
724 else
725 p_rec += 4;
nxpandroidc7611652015-09-23 16:42:05 +0530726
nxpandroid8f6d0532017-07-12 18:25:30 +0530727 /* ID field Length */
728 if (rec_hdr & NDEF_IL_MASK)
729 *p_id_len = *p_rec++;
730 else
731 *p_id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530732
nxpandroid8f6d0532017-07-12 18:25:30 +0530733 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530738}
739
nxpandroidc7611652015-09-23 16:42:05 +0530740/*******************************************************************************
741**
742** Function NDEF_RecGetPayload
743**
nxpandroid8f6d0532017-07-12 18:25:30 +0530744** Description This function gets a pointer to the payload for the given
745** NDEF record.
nxpandroidc7611652015-09-23 16:42:05 +0530746**
nxpandroid8f6d0532017-07-12 18:25:30 +0530747** Returns a pointer to the payload (or NULL none). Payload len filled
748** in.
nxpandroidc7611652015-09-23 16:42:05 +0530749**
750*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530751uint8_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;
nxpandroidc7611652015-09-23 16:42:05 +0530754
nxpandroid8f6d0532017-07-12 18:25:30 +0530755 /* First byte is the record header */
756 rec_hdr = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530757
nxpandroid8f6d0532017-07-12 18:25:30 +0530758 /* Next byte is the type field length */
759 type_len = *p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530760
nxpandroid8f6d0532017-07-12 18:25:30 +0530761 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530766
nxpandroid8f6d0532017-07-12 18:25:30 +0530767 *p_payload_len = payload_len;
nxpandroidc7611652015-09-23 16:42:05 +0530768
nxpandroid8f6d0532017-07-12 18:25:30 +0530769 /* ID field Length */
770 if (rec_hdr & NDEF_IL_MASK)
771 id_len = *p_rec++;
772 else
773 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530774
nxpandroid8f6d0532017-07-12 18:25:30 +0530775 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530781}
782
nxpandroidc7611652015-09-23 16:42:05 +0530783/*******************************************************************************
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530793void 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);
nxpandroidc7611652015-09-23 16:42:05 +0530796}
797
798/*******************************************************************************
799**
800** Function NDEF_MsgAddRec
801**
nxpandroid8f6d0532017-07-12 18:25:30 +0530802** Description This function adds an NDEF record to the end of an NDEF
803** message.
nxpandroidc7611652015-09-23 16:42:05 +0530804**
805** Returns OK, or error if the record did not fit
806** *p_cur_size is updated
807**
808*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530809extern 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;
nxpandroidc7611652015-09-23 16:42:05 +0530818
nxpandroid8f6d0532017-07-12 18:25:30 +0530819 if (tnf > NDEF_TNF_RESERVED) {
820 tnf = NDEF_TNF_UNKNOWN;
821 type_len = 0;
822 }
nxpandroidc7611652015-09-23 16:42:05 +0530823
nxpandroid8f6d0532017-07-12 18:25:30 +0530824 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +0530827
nxpandroid8f6d0532017-07-12 18:25:30 +0530828 if ((*p_cur_size + recSize) > max_size) return (NDEF_MSG_INSUFFICIENT_MEM);
nxpandroidc7611652015-09-23 16:42:05 +0530829
nxpandroid8f6d0532017-07-12 18:25:30 +0530830 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530837
nxpandroid8f6d0532017-07-12 18:25:30 +0530838 if (!pLast) return (NDEF_MSG_NO_MSG_END);
nxpandroidc7611652015-09-23 16:42:05 +0530839
nxpandroid8f6d0532017-07-12 18:25:30 +0530840 *pLast &= ~NDEF_ME_MASK;
841 *p_rec = tnf | NDEF_ME_MASK;
842 }
nxpandroidc7611652015-09-23 16:42:05 +0530843
nxpandroid8f6d0532017-07-12 18:25:30 +0530844 if (plen == 1) *p_rec |= NDEF_SR_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530845
nxpandroid8f6d0532017-07-12 18:25:30 +0530846 if (ilen != 0) *p_rec |= NDEF_IL_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530847
nxpandroid8f6d0532017-07-12 18:25:30 +0530848 p_rec++;
nxpandroidc7611652015-09-23 16:42:05 +0530849
nxpandroid8f6d0532017-07-12 18:25:30 +0530850 /* The next byte is the type field length */
851 *p_rec++ = type_len;
nxpandroidc7611652015-09-23 16:42:05 +0530852
nxpandroid8f6d0532017-07-12 18:25:30 +0530853 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530858
nxpandroid8f6d0532017-07-12 18:25:30 +0530859 /* ID field Length (optional) */
860 if (ilen > 0) *p_rec++ = id_len;
nxpandroidc7611652015-09-23 16:42:05 +0530861
nxpandroid8f6d0532017-07-12 18:25:30 +0530862 /* Next comes the type */
863 if (type_len) {
864 if (p_type) memcpy(p_rec, p_type, type_len);
nxpandroidc7611652015-09-23 16:42:05 +0530865
nxpandroid8f6d0532017-07-12 18:25:30 +0530866 p_rec += type_len;
867 }
nxpandroidc7611652015-09-23 16:42:05 +0530868
nxpandroid8f6d0532017-07-12 18:25:30 +0530869 /* Next comes the ID */
870 if (id_len) {
871 if (p_id) memcpy(p_rec, p_id, id_len);
nxpandroidc7611652015-09-23 16:42:05 +0530872
nxpandroid8f6d0532017-07-12 18:25:30 +0530873 p_rec += id_len;
874 }
nxpandroidc7611652015-09-23 16:42:05 +0530875
nxpandroid8f6d0532017-07-12 18:25:30 +0530876 /* And lastly the payload. If NULL, the app just wants to reserve memory */
877 if (p_payload) memcpy(p_rec, p_payload, payload_len);
nxpandroidc7611652015-09-23 16:42:05 +0530878
nxpandroid8f6d0532017-07-12 18:25:30 +0530879 *p_cur_size += recSize;
nxpandroidc7611652015-09-23 16:42:05 +0530880
nxpandroid8f6d0532017-07-12 18:25:30 +0530881 return (NDEF_OK);
nxpandroidc7611652015-09-23 16:42:05 +0530882}
883
884/*******************************************************************************
885**
nxpandroidc7611652015-09-23 16:42:05 +0530886** Function NDEF_MsgAppendPayload
887**
nxpandroid8f6d0532017-07-12 18:25:30 +0530888** Description This function appends extra payload to a specific record in
889** the given NDEF message
nxpandroidc7611652015-09-23 16:42:05 +0530890**
891** Returns OK, or error if the extra payload did not fit
892** *p_cur_size is updated
893**
894*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530895tNDEF_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;
nxf24591c1cbeab2018-02-21 17:32:26 +0530899 uint8_t *p_prev_pl, *pp;
nxpandroid8f6d0532017-07-12 18:25:30 +0530900 uint8_t incr_lenfld = 0;
901 uint8_t type_len, id_len;
nxpandroidc7611652015-09-23 16:42:05 +0530902
nxpandroid8f6d0532017-07-12 18:25:30 +0530903 /* Skip header */
904 pp = p_rec + 1;
nxpandroidc7611652015-09-23 16:42:05 +0530905
nxpandroid8f6d0532017-07-12 18:25:30 +0530906 /* Next byte is the type field length */
907 type_len = *pp++;
nxpandroidc7611652015-09-23 16:42:05 +0530908
nxpandroid8f6d0532017-07-12 18:25:30 +0530909 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530914
nxpandroid8f6d0532017-07-12 18:25:30 +0530915 /* ID field Length */
916 if (*p_rec & NDEF_IL_MASK)
917 id_len = *pp++;
918 else
919 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530920
nxpandroid8f6d0532017-07-12 18:25:30 +0530921 p_prev_pl = pp + type_len + id_len;
nxpandroidc7611652015-09-23 16:42:05 +0530922
nxpandroid8f6d0532017-07-12 18:25:30 +0530923 new_paylen = prev_paylen + add_pl_len;
nxpandroidc7611652015-09-23 16:42:05 +0530924
nxpandroid8f6d0532017-07-12 18:25:30 +0530925 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +0530930
nxpandroid8f6d0532017-07-12 18:25:30 +0530931 /* Check that it all fits */
932 if ((*p_cur_size + add_pl_len + incr_lenfld) > max_size)
933 return (NDEF_MSG_INSUFFICIENT_MEM);
nxpandroidc7611652015-09-23 16:42:05 +0530934
nxpandroid8f6d0532017-07-12 18:25:30 +0530935 /* Point to payload length field */
936 pp = p_rec + 2;
nxpandroidc7611652015-09-23 16:42:05 +0530937
nxpandroid8f6d0532017-07-12 18:25:30 +0530938 /* 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 }
nxpandroidc7611652015-09-23 16:42:05 +0530943
nxpandroid8f6d0532017-07-12 18:25:30 +0530944 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +0530950
nxpandroid8f6d0532017-07-12 18:25:30 +0530951 /* Point to the end of the previous payload */
952 pp = p_prev_pl + prev_paylen;
nxpandroidc7611652015-09-23 16:42:05 +0530953
nxpandroid8f6d0532017-07-12 18:25:30 +0530954 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530957
nxpandroid8f6d0532017-07-12 18:25:30 +0530958 /* Now copy in the additional payload data */
959 memcpy(pp, p_add_pl, add_pl_len);
nxpandroidc7611652015-09-23 16:42:05 +0530960
nxpandroid8f6d0532017-07-12 18:25:30 +0530961 *p_cur_size += add_pl_len + incr_lenfld;
nxpandroidc7611652015-09-23 16:42:05 +0530962
nxpandroid8f6d0532017-07-12 18:25:30 +0530963 return (NDEF_OK);
nxpandroidc7611652015-09-23 16:42:05 +0530964}
965
966/*******************************************************************************
967**
968** Function NDEF_MsgReplacePayload
969**
nxpandroid8f6d0532017-07-12 18:25:30 +0530970** Description This function replaces the payload of a specific record in
971** the given NDEF message
nxpandroidc7611652015-09-23 16:42:05 +0530972**
973** Returns OK, or error if the new payload did not fit
974** *p_cur_size is updated
975**
976*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530977tNDEF_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;
nxf24591c1cbeab2018-02-21 17:32:26 +0530981 uint8_t *p_prev_pl, *pp;
nxpandroid8f6d0532017-07-12 18:25:30 +0530982 uint32_t paylen_delta;
983 uint8_t type_len, id_len;
nxpandroidc7611652015-09-23 16:42:05 +0530984
nxpandroid8f6d0532017-07-12 18:25:30 +0530985 /* Skip header */
986 pp = p_rec + 1;
nxpandroidc7611652015-09-23 16:42:05 +0530987
nxpandroid8f6d0532017-07-12 18:25:30 +0530988 /* Next byte is the type field length */
989 type_len = *pp++;
nxpandroidc7611652015-09-23 16:42:05 +0530990
nxpandroid8f6d0532017-07-12 18:25:30 +0530991 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +0530996
nxpandroid8f6d0532017-07-12 18:25:30 +0530997 /* ID field Length */
998 if (*p_rec & NDEF_IL_MASK)
999 id_len = *pp++;
1000 else
1001 id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +05301002
nxpandroid8f6d0532017-07-12 18:25:30 +05301003 p_prev_pl = pp + type_len + id_len;
nxpandroidc7611652015-09-23 16:42:05 +05301004
nxpandroid8f6d0532017-07-12 18:25:30 +05301005 /* Point to payload length field again */
1006 pp = p_rec + 2;
nxpandroidc7611652015-09-23 16:42:05 +05301007
nxpandroid8f6d0532017-07-12 18:25:30 +05301008 if (new_pl_len > prev_paylen) {
1009 /* New payload is larger than the previous */
1010 paylen_delta = new_pl_len - prev_paylen;
nxpandroidc7611652015-09-23 16:42:05 +05301011
nxpandroid8f6d0532017-07-12 18:25:30 +05301012 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301017
nxpandroid8f6d0532017-07-12 18:25:30 +05301018 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);
nxpandroidc7611652015-09-23 16:42:05 +05301024
nxpandroid8f6d0532017-07-12 18:25:30 +05301025 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +05301030
nxpandroid8f6d0532017-07-12 18:25:30 +05301031 /* Point to the end of the previous payload */
1032 pp = p_prev_pl + prev_paylen;
nxpandroidc7611652015-09-23 16:42:05 +05301033
nxpandroid8f6d0532017-07-12 18:25:30 +05301034 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301037
nxpandroid8f6d0532017-07-12 18:25:30 +05301038 *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;
nxpandroidc7611652015-09-23 16:42:05 +05301042
nxpandroid8f6d0532017-07-12 18:25:30 +05301043 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +05301050 }
1051
nxpandroid8f6d0532017-07-12 18:25:30 +05301052 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +05301057
nxpandroid8f6d0532017-07-12 18:25:30 +05301058 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301073}
1074
1075/*******************************************************************************
1076**
1077** Function NDEF_MsgReplaceType
1078**
nxpandroid8f6d0532017-07-12 18:25:30 +05301079** Description This function replaces the type field of a specific record
1080** in the given NDEF message
nxpandroidc7611652015-09-23 16:42:05 +05301081**
1082** Returns OK, or error if the new type field did not fit
1083** *p_cur_size is updated
1084**
1085*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +05301086tNDEF_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;
nxf24591c1cbeab2018-02-21 17:32:26 +05301090 uint8_t *p_prev_type, prev_type_len;
nxpandroid8f6d0532017-07-12 18:25:30 +05301091 uint8_t* pp;
nxpandroidc7611652015-09-23 16:42:05 +05301092
nxpandroid8f6d0532017-07-12 18:25:30 +05301093 /* Skip header */
1094 pp = p_rec + 1;
nxpandroidc7611652015-09-23 16:42:05 +05301095
nxpandroid8f6d0532017-07-12 18:25:30 +05301096 /* Next byte is the type field length */
1097 prev_type_len = *pp++;
nxpandroidc7611652015-09-23 16:42:05 +05301098
nxpandroid8f6d0532017-07-12 18:25:30 +05301099 /* Skip the payload length */
1100 if (*p_rec & NDEF_SR_MASK)
1101 pp += 1;
1102 else
1103 pp += 4;
nxpandroidc7611652015-09-23 16:42:05 +05301104
nxpandroid8f6d0532017-07-12 18:25:30 +05301105 if (*p_rec & NDEF_IL_MASK) pp++;
nxpandroidc7611652015-09-23 16:42:05 +05301106
nxpandroid8f6d0532017-07-12 18:25:30 +05301107 /* Save pointer to the start of the type field */
1108 p_prev_type = pp;
nxpandroidc7611652015-09-23 16:42:05 +05301109
nxpandroid8f6d0532017-07-12 18:25:30 +05301110 if (new_type_len > prev_type_len) {
1111 /* New type is larger than the previous */
1112 typelen_delta = new_type_len - prev_type_len;
nxpandroidc7611652015-09-23 16:42:05 +05301113
nxpandroid8f6d0532017-07-12 18:25:30 +05301114 if ((*p_cur_size + typelen_delta) > max_size)
1115 return (NDEF_MSG_INSUFFICIENT_MEM);
nxpandroidc7611652015-09-23 16:42:05 +05301116
nxpandroid8f6d0532017-07-12 18:25:30 +05301117 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301121
nxpandroid8f6d0532017-07-12 18:25:30 +05301122 *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;
nxpandroidc7611652015-09-23 16:42:05 +05301126
nxpandroid8f6d0532017-07-12 18:25:30 +05301127 /* 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)));
nxpandroidc7611652015-09-23 16:42:05 +05301131
nxpandroid8f6d0532017-07-12 18:25:30 +05301132 *p_cur_size -= typelen_delta;
1133 }
nxpandroidc7611652015-09-23 16:42:05 +05301134
nxpandroid8f6d0532017-07-12 18:25:30 +05301135 /* Save in new type length */
1136 p_rec[1] = new_type_len;
nxpandroidc7611652015-09-23 16:42:05 +05301137
nxpandroid8f6d0532017-07-12 18:25:30 +05301138 /* Now copy in the new type field data */
1139 if (p_new_type) memcpy(p_prev_type, p_new_type, new_type_len);
nxpandroidc7611652015-09-23 16:42:05 +05301140
nxpandroid8f6d0532017-07-12 18:25:30 +05301141 return (NDEF_OK);
nxpandroidc7611652015-09-23 16:42:05 +05301142}
1143
1144/*******************************************************************************
1145**
1146** Function NDEF_MsgReplaceId
1147**
nxpandroid8f6d0532017-07-12 18:25:30 +05301148** Description This function replaces the ID field of a specific record in
1149** the given NDEF message
nxpandroidc7611652015-09-23 16:42:05 +05301150**
1151** Returns OK, or error if the new ID field did not fit
1152** *p_cur_size is updated
1153**
1154*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +05301155tNDEF_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;
nxf24591c1cbeab2018-02-21 17:32:26 +05301159 uint8_t *p_prev_id, *p_idlen_field;
nxpandroid8f6d0532017-07-12 18:25:30 +05301160 uint8_t prev_id_len, type_len;
1161 uint8_t* pp;
nxpandroidc7611652015-09-23 16:42:05 +05301162
nxpandroid8f6d0532017-07-12 18:25:30 +05301163 /* Skip header */
1164 pp = p_rec + 1;
nxpandroidc7611652015-09-23 16:42:05 +05301165
nxpandroid8f6d0532017-07-12 18:25:30 +05301166 /* Next byte is the type field length */
1167 type_len = *pp++;
nxpandroidc7611652015-09-23 16:42:05 +05301168
nxpandroid8f6d0532017-07-12 18:25:30 +05301169 /* Skip the payload length */
1170 if (*p_rec & NDEF_SR_MASK)
1171 pp += 1;
1172 else
1173 pp += 4;
nxpandroidc7611652015-09-23 16:42:05 +05301174
nxpandroid8f6d0532017-07-12 18:25:30 +05301175 p_idlen_field = pp;
nxpandroidc7611652015-09-23 16:42:05 +05301176
nxpandroid8f6d0532017-07-12 18:25:30 +05301177 if (*p_rec & NDEF_IL_MASK)
1178 prev_id_len = *pp++;
1179 else
1180 prev_id_len = 0;
nxpandroidc7611652015-09-23 16:42:05 +05301181
nxpandroid8f6d0532017-07-12 18:25:30 +05301182 /* Save pointer to the start of the ID field (right after the type field) */
1183 p_prev_id = pp + type_len;
nxpandroidc7611652015-09-23 16:42:05 +05301184
nxpandroid8f6d0532017-07-12 18:25:30 +05301185 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;
nxpandroidc7611652015-09-23 16:42:05 +05301188
nxpandroid8f6d0532017-07-12 18:25:30 +05301189 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301193
nxpandroid8f6d0532017-07-12 18:25:30 +05301194 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);
nxpandroidc7611652015-09-23 16:42:05 +05301201
nxpandroid8f6d0532017-07-12 18:25:30 +05301202 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301206
nxpandroid8f6d0532017-07-12 18:25:30 +05301207 *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;
nxpandroidc7611652015-09-23 16:42:05 +05301225 }
nxpandroid8f6d0532017-07-12 18:25:30 +05301226 }
nxpandroidc7611652015-09-23 16:42:05 +05301227
nxpandroid8f6d0532017-07-12 18:25:30 +05301228 /* Save in new ID length and data */
1229 if (new_id_len) {
1230 *p_idlen_field = new_id_len;
nxpandroidc7611652015-09-23 16:42:05 +05301231
nxpandroid8f6d0532017-07-12 18:25:30 +05301232 if (p_new_id) memcpy(p_prev_id, p_new_id, new_id_len);
1233 }
nxpandroidc7611652015-09-23 16:42:05 +05301234
nxpandroid8f6d0532017-07-12 18:25:30 +05301235 return (NDEF_OK);
nxpandroidc7611652015-09-23 16:42:05 +05301236}
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**
nxf24591c1cbeab2018-02-21 17:32:26 +05301245** Returns TRUE if OK, FALSE if the index was invalid
nxpandroidc7611652015-09-23 16:42:05 +05301246** *p_cur_size is updated
1247**
1248*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +05301249tNDEF_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);
nxf24591c1cbeab2018-02-21 17:32:26 +05301252 uint8_t *pNext, *pPrev;
nxpandroidc7611652015-09-23 16:42:05 +05301253
nxpandroid8f6d0532017-07-12 18:25:30 +05301254 if (!p_rec) return (NDEF_REC_NOT_FOUND);
nxpandroidc7611652015-09-23 16:42:05 +05301255
nxpandroid8f6d0532017-07-12 18:25:30 +05301256 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +05301262
nxpandroid8f6d0532017-07-12 18:25:30 +05301263 *p_cur_size -= (uint32_t)(pNext - p_msg);
nxpandroidc7611652015-09-23 16:42:05 +05301264
nxpandroid8f6d0532017-07-12 18:25:30 +05301265 shiftup(p_msg, pNext, *p_cur_size);
1266 } else
1267 *p_cur_size = 0; /* No more records, lenght must be zero */
nxpandroidc7611652015-09-23 16:42:05 +05301268
1269 return (NDEF_OK);
nxpandroid8f6d0532017-07-12 18:25:30 +05301270 }
nxpandroidc7611652015-09-23 16:42:05 +05301271
nxpandroid8f6d0532017-07-12 18:25:30 +05301272 /* 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}
nxpandroidc7611652015-09-23 16:42:05 +05301297
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +05301310tNDEF_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;
nxf24591c1cbeab2018-02-21 17:32:26 +05301315 uint8_t *p_type, *p_id, *p_pay;
nxpandroid8f6d0532017-07-12 18:25:30 +05301316 uint8_t type_len, id_len, tnf;
1317 uint32_t pay_len;
1318 tNDEF_STATUS status;
nxpandroidc7611652015-09-23 16:42:05 +05301319
nxpandroid8f6d0532017-07-12 18:25:30 +05301320 /* First, validate the source */
1321 status = NDEF_MsgValidate(p_src, src_len, true);
1322 if (status != NDEF_OK) return (status);
nxpandroidc7611652015-09-23 16:42:05 +05301323
nxpandroid8f6d0532017-07-12 18:25:30 +05301324 /* The output buffer must be at least as large as the input buffer */
1325 max_out_len = src_len;
nxpandroidc7611652015-09-23 16:42:05 +05301326
nxpandroid8f6d0532017-07-12 18:25:30 +05301327 /* Initialize output */
1328 NDEF_MsgInit(p_dest, max_out_len, &out_len);
nxpandroidc7611652015-09-23 16:42:05 +05301329
nxpandroid8f6d0532017-07-12 18:25:30 +05301330 p_rec = p_src;
nxpandroidc7611652015-09-23 16:42:05 +05301331
nxpandroid8f6d0532017-07-12 18:25:30 +05301332 /* 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);
nxpandroidc7611652015-09-23 16:42:05 +05301337
nxpandroid8f6d0532017-07-12 18:25:30 +05301338 /* 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;
nxpandroidc7611652015-09-23 16:42:05 +05301347
nxpandroid8f6d0532017-07-12 18:25:30 +05301348 status = NDEF_MsgAddRec(p_dest, max_out_len, &out_len, tnf, p_type,
1349 type_len, p_id, id_len, p_pay, pay_len);
nxpandroidc7611652015-09-23 16:42:05 +05301350 }
1351
nxpandroid8f6d0532017-07-12 18:25:30 +05301352 p_rec = NDEF_MsgGetNextRec(p_rec);
1353 }
nxpandroidc7611652015-09-23 16:42:05 +05301354
nxpandroid8f6d0532017-07-12 18:25:30 +05301355 *p_out_len = out_len;
1356
1357 return (status);
nxpandroidc7611652015-09-23 16:42:05 +05301358}