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