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