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