| 828 | // *********************************************************************** |
| 829 | |
| 830 | int Deserialize(lua_State* L) { |
| 831 | u64 len; |
| 832 | const char* str = luaL_checklstring(L, -1, &len); |
| 833 | |
| 834 | // find metadata if possible |
| 835 | char* start = (char*)str; |
| 836 | if (strncmp(str, "--[[poly,", 9) == 0) { |
| 837 | while (true) { |
| 838 | if (strncmp(start, "]]", 2) == 0) { |
| 839 | start += 2; |
| 840 | break; |
| 841 | } |
| 842 | start++; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | i64 metaDataLen = start - str; |
| 847 | i64 dataLen = len - metaDataLen; |
| 848 | |
| 849 | String metaData; |
| 850 | metaData.pData = (char*)str + 9; // 9 is the length of '--[[poly,' |
| 851 | metaData.length = metaDataLen - 9; |
| 852 | |
| 853 | String decoded; |
| 854 | |
| 855 | if (strncmp(start, "b64:", 4) == 0) { |
| 856 | // base64 |
| 857 | String base64; |
| 858 | base64.pData = (char*)start + 4; |
| 859 | base64.length = dataLen - 4; |
| 860 | |
| 861 | decoded = DecodeBase64(g_pArenaFrame, base64); |
| 862 | } else { |
| 863 | decoded.pData = (char*)start; |
| 864 | decoded.length = dataLen; |
| 865 | } |
| 866 | |
| 867 | if ((u8)decoded.pData[0] == 0xBD) { |
| 868 | // binary file |
| 869 | CborParserState state; |
| 870 | state.len = decoded.length - 1; |
| 871 | state.pData = (u8*)decoded.pData+1; |
| 872 | state.pEnd = state.pData + state.len; |
| 873 | state.pCurrent = state.pData; |
| 874 | |
| 875 | ParseCborValue(L, state); |
| 876 | } |
| 877 | else if ((u8)decoded.pData[0] == 0xBC) { |
| 878 | // compressed file |
| 879 | i32 compressedSize; |
| 880 | i32 originalSize; |
| 881 | u8* pData; |
| 882 | |
| 883 | MemcpyLE((u8*)&compressedSize, (u8*)decoded.pData+1, 4); |
| 884 | MemcpyLE((u8*)&originalSize, (u8*)decoded.pData+5, 4); |
| 885 | pData = (u8*)decoded.pData+9; |
| 886 | |
| 887 | u8* pDecompressedData = New(g_pArenaFrame, u8, originalSize); |
no test coverage detected