MCPcopy Create free account
hub / github.com/DavidColson/Polybox / ParseCborValue

Function ParseCborValue

source/serialization.cpp:676–826  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

674// ***********************************************************************
675
676bool ParseCborValue(lua_State* L, CborParserState& state, i32 maxItems = -1) {
677 i32 items = 0;
678 while ((maxItems == -1 || items < maxItems) && state.pCurrent < state.pEnd) {
679 u8 val = *state.pCurrent;
680 items++;
681
682 // get major type and additional information
683 u8 majorType = val >> 5; // top 3 bits
684 u8 additionalInfo = val & 0x1fu; // bottom 5 bits
685
686 u8 followingBytes = 1u << (additionalInfo - 24);
687
688 if (additionalInfo < 24) {
689 followingBytes = 0;
690 }
691 else if (additionalInfo == 31 && majorType == 7) {
692 // stop code of invalid info
693 state.pCurrent++;
694 return true;
695 }
696
697 state.pCurrent++;
698
699 switch(majorType) {
700 case 0:{ // positive int
701 if (followingBytes == 0) {
702 lua_pushnumber(L, additionalInfo);
703 } else {
704 i32 result = 0;
705 MemcpyBE((u8*)&result, (u8*)state.pCurrent, followingBytes);
706 lua_pushnumber(L, result);
707 state.pCurrent += followingBytes;
708 }
709 break;
710 }
711 case 1: // negative int
712 if (followingBytes == 0) {
713 lua_pushnumber(L, -1-(i32)additionalInfo);
714 } else {
715 i32 result = 0;
716 MemcpyBE((u8*)&result, (u8*)state.pCurrent, followingBytes);
717 lua_pushnumber(L, -1-result);
718 state.pCurrent += followingBytes;
719 }
720 break;
721 case 2: { // userdata
722 i32 dataLen = 0;
723 if (followingBytes == 0) {
724 dataLen = additionalInfo;
725 } else {
726 MemcpyBE((u8*)&dataLen, (u8*)state.pCurrent, followingBytes);
727 state.pCurrent += followingBytes;
728 }
729
730 i32 userdataWidth = 0;
731 i32 userdataHeight = 0;
732 Type userdataType = Type::Int32;
733 MemcpyLE((u8*)&userdataWidth, (u8*)state.pCurrent, 4);

Callers 1

DeserializeFunction · 0.85

Calls 4

MemcpyBEFunction · 0.85
MemcpyLEFunction · 0.85
AllocUserDataFunction · 0.85
GetUserDataSizeFunction · 0.85

Tested by

no test coverage detected