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 register uint8_t* ps = p_mem + len - 1;
43 register uint8_t* pd = ps + shift_amount;
44 register 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 register uint8_t* ps = p_src;
58 register uint8_t* pd = p_dest;
59 register 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_MsgInsertRec
887 **
888 ** Description This function inserts a record at a specific index into the
889 ** given NDEF message
890 **
891 ** Returns OK, or error if the record did not fit
892 ** *p_cur_size is updated
893 **
894 *******************************************************************************/
NDEF_MsgInsertRec(uint8_t * p_msg,uint32_t max_size,uint32_t * p_cur_size,int32_t index,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)895 extern tNDEF_STATUS NDEF_MsgInsertRec(uint8_t* p_msg, uint32_t max_size,
896 uint32_t* p_cur_size, int32_t index,
897 uint8_t tnf, uint8_t* p_type,
898 uint8_t type_len, uint8_t* p_id,
899 uint8_t id_len, uint8_t* p_payload,
900 uint32_t payload_len) {
901 uint8_t* p_rec;
902 uint32_t recSize;
903 int32_t plen = (payload_len < 256) ? 1 : 4;
904 int32_t ilen = (id_len == 0) ? 0 : 1;
905
906 /* First, make sure the record will fit. we need at least 2 bytes for header
907 * and type length */
908 recSize = payload_len + 2 + type_len + plen + ilen + id_len;
909
910 if ((*p_cur_size + recSize) > max_size) return (NDEF_MSG_INSUFFICIENT_MEM);
911
912 /* See where the new record goes. If at the end, call the 'AddRec' function */
913 if ((index >= NDEF_MsgGetNumRecs(p_msg)) ||
914 ((p_rec = NDEF_MsgGetRecByIndex(p_msg, index)) == NULL)) {
915 return NDEF_MsgAddRec(p_msg, max_size, p_cur_size, tnf, p_type, type_len,
916 p_id, id_len, p_payload, payload_len);
917 }
918
919 /* If we are inserting at the beginning, remove the MB bit from the current
920 * first */
921 if (index == 0) *p_msg &= ~NDEF_MB_MASK;
922
923 /* Make space for the new record */
924 shiftdown(p_rec, (uint32_t)(*p_cur_size - (p_rec - p_msg)), recSize);
925
926 /* If adding at the beginning, set begin bit */
927 if (index == 0)
928 *p_rec = tnf | NDEF_MB_MASK;
929 else
930 *p_rec = tnf;
931
932 if (plen == 1) *p_rec |= NDEF_SR_MASK;
933
934 if (ilen != 0) *p_rec |= NDEF_IL_MASK;
935
936 p_rec++;
937
938 /* The next byte is the type field length */
939 *p_rec++ = type_len;
940
941 /* Payload length - can be 1 or 4 bytes */
942 if (plen == 1)
943 *p_rec++ = (uint8_t)payload_len;
944 else
945 UINT32_TO_BE_STREAM(p_rec, payload_len);
946
947 /* ID field Length (optional) */
948 if (ilen != 0) *p_rec++ = id_len;
949
950 /* Next comes the type */
951 if (type_len) {
952 if (p_type) memcpy(p_rec, p_type, type_len);
953
954 p_rec += type_len;
955 }
956
957 /* Next comes the ID */
958 if (ilen != 0) {
959 if (p_id) memcpy(p_rec, p_id, id_len);
960
961 p_rec += id_len;
962 }
963
964 /* And lastly the payload. If NULL, the app just wants to reserve memory */
965 if (p_payload) memcpy(p_rec, p_payload, payload_len);
966
967 *p_cur_size += recSize;
968
969 return (NDEF_OK);
970 }
971
972 /*******************************************************************************
973 **
974 ** Function NDEF_MsgAppendRec
975 **
976 ** Description This function adds NDEF records to the end of an NDEF
977 ** message.
978 **
979 ** Returns OK, or error if the record did not fit
980 ** *p_cur_size is updated
981 **
982 *******************************************************************************/
NDEF_MsgAppendRec(uint8_t * p_msg,uint32_t max_size,uint32_t * p_cur_size,uint8_t * p_new_rec,uint32_t new_rec_len)983 extern tNDEF_STATUS NDEF_MsgAppendRec(uint8_t* p_msg, uint32_t max_size,
984 uint32_t* p_cur_size, uint8_t* p_new_rec,
985 uint32_t new_rec_len) {
986 uint8_t* p_rec;
987 tNDEF_STATUS status;
988
989 /* First, validate new records */
990 status = NDEF_MsgValidate(p_new_rec, new_rec_len, false);
991 if (status != NDEF_OK) return (status);
992
993 /* First, make sure the record will fit */
994 if ((*p_cur_size + new_rec_len) > max_size)
995 return (NDEF_MSG_INSUFFICIENT_MEM);
996
997 /* Find where to copy new record */
998 if (*p_cur_size == 0)
999 p_rec = p_msg;
1000 else {
1001 /* Find the previous last and clear his 'Message End' bit */
1002 uint8_t* pLast = NDEF_MsgGetLastRecInMsg(p_msg);
1003
1004 if (!pLast) return (NDEF_MSG_NO_MSG_END);
1005
1006 *pLast &= ~NDEF_ME_MASK;
1007 p_rec = p_msg + *p_cur_size;
1008
1009 /* clear 'Message Begin' bit of new record */
1010 *p_new_rec &= ~NDEF_MB_MASK;
1011 }
1012
1013 /* append new records */
1014 memcpy(p_rec, p_new_rec, new_rec_len);
1015
1016 *p_cur_size += new_rec_len;
1017
1018 return (NDEF_OK);
1019 }
1020
1021 /*******************************************************************************
1022 **
1023 ** Function NDEF_MsgAppendPayload
1024 **
1025 ** Description This function appends extra payload to a specific record in
1026 ** the given NDEF message
1027 **
1028 ** Returns OK, or error if the extra payload did not fit
1029 ** *p_cur_size is updated
1030 **
1031 *******************************************************************************/
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)1032 tNDEF_STATUS NDEF_MsgAppendPayload(uint8_t* p_msg, uint32_t max_size,
1033 uint32_t* p_cur_size, uint8_t* p_rec,
1034 uint8_t* p_add_pl, uint32_t add_pl_len) {
1035 uint32_t prev_paylen, new_paylen;
1036 uint8_t *p_prev_pl, *pp;
1037 uint8_t incr_lenfld = 0;
1038 uint8_t type_len, id_len;
1039
1040 /* Skip header */
1041 pp = p_rec + 1;
1042
1043 /* Next byte is the type field length */
1044 type_len = *pp++;
1045
1046 /* Next is the payload length (1 or 4 bytes) */
1047 if (*p_rec & NDEF_SR_MASK)
1048 prev_paylen = *pp++;
1049 else
1050 BE_STREAM_TO_UINT32(prev_paylen, pp);
1051
1052 /* ID field Length */
1053 if (*p_rec & NDEF_IL_MASK)
1054 id_len = *pp++;
1055 else
1056 id_len = 0;
1057
1058 p_prev_pl = pp + type_len + id_len;
1059
1060 new_paylen = prev_paylen + add_pl_len;
1061
1062 /* Previous payload may be < 256, and this addition may make it larger than
1063 * 256 */
1064 /* If that were to happen, the payload length field goes from 1 byte to 4
1065 * bytes */
1066 if ((prev_paylen < 256) && (new_paylen > 255)) incr_lenfld = 3;
1067
1068 /* Check that it all fits */
1069 if ((*p_cur_size + add_pl_len + incr_lenfld) > max_size)
1070 return (NDEF_MSG_INSUFFICIENT_MEM);
1071
1072 /* Point to payload length field */
1073 pp = p_rec + 2;
1074
1075 /* If we need to increase the length field from 1 to 4 bytes, do it first */
1076 if (incr_lenfld) {
1077 shiftdown(pp + 1, (uint32_t)(*p_cur_size - (pp - p_msg) - 1), 3);
1078 p_prev_pl += 3;
1079 }
1080
1081 /* Store in the new length */
1082 if (new_paylen > 255) {
1083 *p_rec &= ~NDEF_SR_MASK;
1084 UINT32_TO_BE_STREAM(pp, new_paylen);
1085 } else
1086 *pp = (uint8_t)new_paylen;
1087
1088 /* Point to the end of the previous payload */
1089 pp = p_prev_pl + prev_paylen;
1090
1091 /* If we are not the last record, make space for the extra payload */
1092 if ((*p_rec & NDEF_ME_MASK) == 0)
1093 shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), add_pl_len);
1094
1095 /* Now copy in the additional payload data */
1096 memcpy(pp, p_add_pl, add_pl_len);
1097
1098 *p_cur_size += add_pl_len + incr_lenfld;
1099
1100 return (NDEF_OK);
1101 }
1102
1103 /*******************************************************************************
1104 **
1105 ** Function NDEF_MsgReplacePayload
1106 **
1107 ** Description This function replaces the payload of a specific record in
1108 ** the given NDEF message
1109 **
1110 ** Returns OK, or error if the new payload did not fit
1111 ** *p_cur_size is updated
1112 **
1113 *******************************************************************************/
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)1114 tNDEF_STATUS NDEF_MsgReplacePayload(uint8_t* p_msg, uint32_t max_size,
1115 uint32_t* p_cur_size, uint8_t* p_rec,
1116 uint8_t* p_new_pl, uint32_t new_pl_len) {
1117 uint32_t prev_paylen;
1118 uint8_t *p_prev_pl, *pp;
1119 uint32_t paylen_delta;
1120 uint8_t type_len, id_len;
1121
1122 /* Skip header */
1123 pp = p_rec + 1;
1124
1125 /* Next byte is the type field length */
1126 type_len = *pp++;
1127
1128 /* Next is the payload length (1 or 4 bytes) */
1129 if (*p_rec & NDEF_SR_MASK)
1130 prev_paylen = *pp++;
1131 else
1132 BE_STREAM_TO_UINT32(prev_paylen, pp);
1133
1134 /* ID field Length */
1135 if (*p_rec & NDEF_IL_MASK)
1136 id_len = *pp++;
1137 else
1138 id_len = 0;
1139
1140 p_prev_pl = pp + type_len + id_len;
1141
1142 /* Point to payload length field again */
1143 pp = p_rec + 2;
1144
1145 if (new_pl_len > prev_paylen) {
1146 /* New payload is larger than the previous */
1147 paylen_delta = new_pl_len - prev_paylen;
1148
1149 /* If the previous payload length was < 256, and new is > 255 */
1150 /* the payload length field goes from 1 byte to 4 bytes */
1151 if ((prev_paylen < 256) && (new_pl_len > 255)) {
1152 if ((*p_cur_size + paylen_delta + 3) > max_size)
1153 return (NDEF_MSG_INSUFFICIENT_MEM);
1154
1155 shiftdown(pp + 1, (uint32_t)(*p_cur_size - (pp - p_msg) - 1), 3);
1156 p_prev_pl += 3;
1157 *p_cur_size += 3;
1158 *p_rec &= ~NDEF_SR_MASK;
1159 } else if ((*p_cur_size + paylen_delta) > max_size)
1160 return (NDEF_MSG_INSUFFICIENT_MEM);
1161
1162 /* Store in the new length */
1163 if (new_pl_len > 255) {
1164 UINT32_TO_BE_STREAM(pp, new_pl_len);
1165 } else
1166 *pp = (uint8_t)new_pl_len;
1167
1168 /* Point to the end of the previous payload */
1169 pp = p_prev_pl + prev_paylen;
1170
1171 /* If we are not the last record, make space for the extra payload */
1172 if ((*p_rec & NDEF_ME_MASK) == 0)
1173 shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), paylen_delta);
1174
1175 *p_cur_size += paylen_delta;
1176 } else if (new_pl_len < prev_paylen) {
1177 /* New payload is smaller than the previous */
1178 paylen_delta = prev_paylen - new_pl_len;
1179
1180 /* If the previous payload was > 256, and new is less than 256 */
1181 /* the payload length field goes from 4 bytes to 1 byte */
1182 if ((prev_paylen > 255) && (new_pl_len < 256)) {
1183 shiftup(pp + 1, pp + 4, (uint32_t)(*p_cur_size - (pp - p_msg) - 3));
1184 p_prev_pl -= 3;
1185 *p_cur_size -= 3;
1186 *p_rec |= NDEF_SR_MASK;
1187 }
1188
1189 /* Store in the new length */
1190 if (new_pl_len > 255) {
1191 UINT32_TO_BE_STREAM(pp, new_pl_len);
1192 } else
1193 *pp = (uint8_t)new_pl_len;
1194
1195 /* Point to the end of the previous payload */
1196 pp = p_prev_pl + prev_paylen;
1197
1198 /* If we are not the last record, remove the extra space from the previous
1199 * payload */
1200 if ((*p_rec & NDEF_ME_MASK) == 0)
1201 shiftup(pp - paylen_delta, pp, (uint32_t)(*p_cur_size - (pp - p_msg)));
1202
1203 *p_cur_size -= paylen_delta;
1204 }
1205
1206 /* Now copy in the new payload data */
1207 if (p_new_pl) memcpy(p_prev_pl, p_new_pl, new_pl_len);
1208
1209 return (NDEF_OK);
1210 }
1211
1212 /*******************************************************************************
1213 **
1214 ** Function NDEF_MsgReplaceType
1215 **
1216 ** Description This function replaces the type field of a specific record
1217 ** in the given NDEF message
1218 **
1219 ** Returns OK, or error if the new type field did not fit
1220 ** *p_cur_size is updated
1221 **
1222 *******************************************************************************/
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)1223 tNDEF_STATUS NDEF_MsgReplaceType(uint8_t* p_msg, uint32_t max_size,
1224 uint32_t* p_cur_size, uint8_t* p_rec,
1225 uint8_t* p_new_type, uint8_t new_type_len) {
1226 uint8_t typelen_delta;
1227 uint8_t *p_prev_type, prev_type_len;
1228 uint8_t* pp;
1229
1230 /* Skip header */
1231 pp = p_rec + 1;
1232
1233 /* Next byte is the type field length */
1234 prev_type_len = *pp++;
1235
1236 /* Skip the payload length */
1237 if (*p_rec & NDEF_SR_MASK)
1238 pp += 1;
1239 else
1240 pp += 4;
1241
1242 if (*p_rec & NDEF_IL_MASK) pp++;
1243
1244 /* Save pointer to the start of the type field */
1245 p_prev_type = pp;
1246
1247 if (new_type_len > prev_type_len) {
1248 /* New type is larger than the previous */
1249 typelen_delta = new_type_len - prev_type_len;
1250
1251 if ((*p_cur_size + typelen_delta) > max_size)
1252 return (NDEF_MSG_INSUFFICIENT_MEM);
1253
1254 /* Point to the end of the previous type, and make space for the extra data
1255 */
1256 pp = p_prev_type + prev_type_len;
1257 shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), typelen_delta);
1258
1259 *p_cur_size += typelen_delta;
1260 } else if (new_type_len < prev_type_len) {
1261 /* New type field is smaller than the previous */
1262 typelen_delta = prev_type_len - new_type_len;
1263
1264 /* Point to the end of the previous type, and shift up to fill the the
1265 * unused space */
1266 pp = p_prev_type + prev_type_len;
1267 shiftup(pp - typelen_delta, pp, (uint32_t)(*p_cur_size - (pp - p_msg)));
1268
1269 *p_cur_size -= typelen_delta;
1270 }
1271
1272 /* Save in new type length */
1273 p_rec[1] = new_type_len;
1274
1275 /* Now copy in the new type field data */
1276 if (p_new_type) memcpy(p_prev_type, p_new_type, new_type_len);
1277
1278 return (NDEF_OK);
1279 }
1280
1281 /*******************************************************************************
1282 **
1283 ** Function NDEF_MsgReplaceId
1284 **
1285 ** Description This function replaces the ID field of a specific record in
1286 ** the given NDEF message
1287 **
1288 ** Returns OK, or error if the new ID field did not fit
1289 ** *p_cur_size is updated
1290 **
1291 *******************************************************************************/
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)1292 tNDEF_STATUS NDEF_MsgReplaceId(uint8_t* p_msg, uint32_t max_size,
1293 uint32_t* p_cur_size, uint8_t* p_rec,
1294 uint8_t* p_new_id, uint8_t new_id_len) {
1295 uint8_t idlen_delta;
1296 uint8_t *p_prev_id, *p_idlen_field;
1297 uint8_t prev_id_len, type_len;
1298 uint8_t* pp;
1299
1300 /* Skip header */
1301 pp = p_rec + 1;
1302
1303 /* Next byte is the type field length */
1304 type_len = *pp++;
1305
1306 /* Skip the payload length */
1307 if (*p_rec & NDEF_SR_MASK)
1308 pp += 1;
1309 else
1310 pp += 4;
1311
1312 p_idlen_field = pp;
1313
1314 if (*p_rec & NDEF_IL_MASK)
1315 prev_id_len = *pp++;
1316 else
1317 prev_id_len = 0;
1318
1319 /* Save pointer to the start of the ID field (right after the type field) */
1320 p_prev_id = pp + type_len;
1321
1322 if (new_id_len > prev_id_len) {
1323 /* New ID field is larger than the previous */
1324 idlen_delta = new_id_len - prev_id_len;
1325
1326 /* If the previous ID length was 0, we need to add a 1-byte ID length */
1327 if (prev_id_len == 0) {
1328 if ((*p_cur_size + idlen_delta + 1) > max_size)
1329 return (NDEF_MSG_INSUFFICIENT_MEM);
1330
1331 shiftdown(p_idlen_field,
1332 (uint32_t)(*p_cur_size - (p_idlen_field - p_msg)), 1);
1333 p_prev_id += 1;
1334 *p_cur_size += 1;
1335 *p_rec |= NDEF_IL_MASK;
1336 } else if ((*p_cur_size + idlen_delta) > max_size)
1337 return (NDEF_MSG_INSUFFICIENT_MEM);
1338
1339 /* Point to the end of the previous ID field, and make space for the extra
1340 * data */
1341 pp = p_prev_id + prev_id_len;
1342 shiftdown(pp, (uint32_t)(*p_cur_size - (pp - p_msg)), idlen_delta);
1343
1344 *p_cur_size += idlen_delta;
1345 } else if (new_id_len < prev_id_len) {
1346 /* New ID field is smaller than the previous */
1347 idlen_delta = prev_id_len - new_id_len;
1348
1349 /* Point to the end of the previous ID, and shift up to fill the the unused
1350 * space */
1351 pp = p_prev_id + prev_id_len;
1352 shiftup(pp - idlen_delta, pp, (uint32_t)(*p_cur_size - (pp - p_msg)));
1353
1354 *p_cur_size -= idlen_delta;
1355
1356 /* If removing the ID, make sure that length field is also removed */
1357 if (new_id_len == 0) {
1358 shiftup(p_idlen_field, p_idlen_field + 1,
1359 (uint32_t)(*p_cur_size - (p_idlen_field - p_msg - (uint32_t)1)));
1360 *p_rec &= ~NDEF_IL_MASK;
1361 *p_cur_size -= 1;
1362 }
1363 }
1364
1365 /* Save in new ID length and data */
1366 if (new_id_len) {
1367 *p_idlen_field = new_id_len;
1368
1369 if (p_new_id) memcpy(p_prev_id, p_new_id, new_id_len);
1370 }
1371
1372 return (NDEF_OK);
1373 }
1374
1375 /*******************************************************************************
1376 **
1377 ** Function NDEF_MsgRemoveRec
1378 **
1379 ** Description This function removes the record at the given
1380 ** index in the given NDEF message.
1381 **
1382 ** Returns TRUE if OK, FALSE if the index was invalid
1383 ** *p_cur_size is updated
1384 **
1385 *******************************************************************************/
NDEF_MsgRemoveRec(uint8_t * p_msg,uint32_t * p_cur_size,int32_t index)1386 tNDEF_STATUS NDEF_MsgRemoveRec(uint8_t* p_msg, uint32_t* p_cur_size,
1387 int32_t index) {
1388 uint8_t* p_rec = NDEF_MsgGetRecByIndex(p_msg, index);
1389 uint8_t *pNext, *pPrev;
1390
1391 if (!p_rec) return (NDEF_REC_NOT_FOUND);
1392
1393 /* If this is the first record in the message... */
1394 if (*p_rec & NDEF_MB_MASK) {
1395 /* Find the second record (if any) and set his 'Message Begin' bit */
1396 pNext = NDEF_MsgGetRecByIndex(p_msg, 1);
1397 if (pNext != NULL) {
1398 *pNext |= NDEF_MB_MASK;
1399
1400 *p_cur_size -= (uint32_t)(pNext - p_msg);
1401
1402 shiftup(p_msg, pNext, *p_cur_size);
1403 } else
1404 *p_cur_size = 0; /* No more records, lenght must be zero */
1405
1406 return (NDEF_OK);
1407 }
1408
1409 /* If this is the last record in the message... */
1410 if (*p_rec & NDEF_ME_MASK) {
1411 if (index > 0) {
1412 /* Find the previous record and set his 'Message End' bit */
1413 pPrev = NDEF_MsgGetRecByIndex(p_msg, index - 1);
1414 if (pPrev == NULL) return false;
1415
1416 *pPrev |= NDEF_ME_MASK;
1417 }
1418 *p_cur_size = (uint32_t)(p_rec - p_msg);
1419
1420 return (NDEF_OK);
1421 }
1422
1423 /* Not the first or the last... get the address of the next record */
1424 pNext = NDEF_MsgGetNextRec(p_rec);
1425 if (pNext == NULL) return false;
1426
1427 /* We are removing p_rec, so shift from pNext to the end */
1428 shiftup(p_rec, pNext, (uint32_t)(*p_cur_size - (pNext - p_msg)));
1429
1430 *p_cur_size -= (uint32_t)(pNext - p_rec);
1431
1432 return (NDEF_OK);
1433 }
1434
1435 /*******************************************************************************
1436 **
1437 ** Function NDEF_MsgCopyAndDechunk
1438 **
1439 ** Description This function copies and de-chunks an NDEF message.
1440 ** It is assumed that the destination is at least as large
1441 ** as the source, since the source may not actually contain
1442 ** any chunks.
1443 **
1444 ** Returns The output byte count
1445 **
1446 *******************************************************************************/
NDEF_MsgCopyAndDechunk(uint8_t * p_src,uint32_t src_len,uint8_t * p_dest,uint32_t * p_out_len)1447 tNDEF_STATUS NDEF_MsgCopyAndDechunk(uint8_t* p_src, uint32_t src_len,
1448 uint8_t* p_dest, uint32_t* p_out_len) {
1449 uint32_t out_len, max_out_len;
1450 uint8_t* p_rec;
1451 uint8_t* p_prev_rec = p_dest;
1452 uint8_t *p_type, *p_id, *p_pay;
1453 uint8_t type_len, id_len, tnf;
1454 uint32_t pay_len;
1455 tNDEF_STATUS status;
1456
1457 /* First, validate the source */
1458 status = NDEF_MsgValidate(p_src, src_len, true);
1459 if (status != NDEF_OK) return (status);
1460
1461 /* The output buffer must be at least as large as the input buffer */
1462 max_out_len = src_len;
1463
1464 /* Initialize output */
1465 NDEF_MsgInit(p_dest, max_out_len, &out_len);
1466
1467 p_rec = p_src;
1468
1469 /* Now, copy record by record */
1470 while ((p_rec != NULL) && (status == NDEF_OK)) {
1471 p_type = NDEF_RecGetType(p_rec, &tnf, &type_len);
1472 p_id = NDEF_RecGetId(p_rec, &id_len);
1473 p_pay = NDEF_RecGetPayload(p_rec, &pay_len);
1474
1475 /* If this is the continuation of a chunk, append the payload to the
1476 * previous */
1477 if (tnf == NDEF_TNF_UNCHANGED) {
1478 if (p_pay) {
1479 status = NDEF_MsgAppendPayload(p_dest, max_out_len, &out_len,
1480 p_prev_rec, p_pay, pay_len);
1481 }
1482 } else {
1483 p_prev_rec = p_dest + out_len;
1484
1485 status = NDEF_MsgAddRec(p_dest, max_out_len, &out_len, tnf, p_type,
1486 type_len, p_id, id_len, p_pay, pay_len);
1487 }
1488
1489 p_rec = NDEF_MsgGetNextRec(p_rec);
1490 }
1491
1492 *p_out_len = out_len;
1493
1494 return (status);
1495 }
1496