| 623 | |
| 624 | |
| 625 | static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) { |
| 626 | int8_t mode = MODE_BYTE; |
| 627 | |
| 628 | if (isNumeric((char*)text, length)) { |
| 629 | mode = MODE_NUMERIC; |
| 630 | bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4); |
| 631 | bb_appendBits(dataCodewords, length, getModeBits(version, MODE_NUMERIC)); |
| 632 | |
| 633 | uint16_t accumData = 0; |
| 634 | uint8_t accumCount = 0; |
| 635 | for (uint16_t i = 0; i < length; i++) { |
| 636 | accumData = accumData * 10 + ((char)(text[i]) - '0'); |
| 637 | accumCount++; |
| 638 | if (accumCount == 3) { |
| 639 | bb_appendBits(dataCodewords, accumData, 10); |
| 640 | accumData = 0; |
| 641 | accumCount = 0; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // 1 or 2 digits remaining |
| 646 | if (accumCount > 0) { |
| 647 | bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1); |
| 648 | } |
| 649 | |
| 650 | } else if (isAlphanumeric((char*)text, length)) { |
| 651 | mode = MODE_ALPHANUMERIC; |
| 652 | bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4); |
| 653 | bb_appendBits(dataCodewords, length, getModeBits(version, MODE_ALPHANUMERIC)); |
| 654 | |
| 655 | uint16_t accumData = 0; |
| 656 | uint8_t accumCount = 0; |
| 657 | for (uint16_t i = 0; i < length; i++) { |
| 658 | accumData = accumData * 45 + getAlphanumeric((char)(text[i])); |
| 659 | accumCount++; |
| 660 | if (accumCount == 2) { |
| 661 | bb_appendBits(dataCodewords, accumData, 11); |
| 662 | accumData = 0; |
| 663 | accumCount = 0; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // 1 character remaining |
| 668 | if (accumCount > 0) { |
| 669 | bb_appendBits(dataCodewords, accumData, 6); |
| 670 | } |
| 671 | |
| 672 | } else { |
| 673 | bb_appendBits(dataCodewords, 1 << MODE_BYTE, 4); |
| 674 | bb_appendBits(dataCodewords, length, getModeBits(version, MODE_BYTE)); |
| 675 | for (uint16_t i = 0; i < length; i++) { |
| 676 | bb_appendBits(dataCodewords, (char)(text[i]), 8); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | //bb_setBits(dataCodewords, length, 4, getModeBits(version, mode)); |
| 681 | |
| 682 | return mode; |
no test coverage detected