78 } |
78 } |
79 return result; |
79 return result; |
80 } |
80 } |
81 |
81 |
82 // TODO: Test with empty map |
82 // TODO: Test with empty map |
83 uint8_t *flib_netmsg_to_drawnmapdata(size_t *outlen, char *netmsg) { |
83 int flib_netmsg_to_drawnmapdata(char *netmsg, uint8_t** outbuf, size_t *outlen) { |
84 uint8_t *result = NULL; |
84 int result = -1; |
85 |
85 |
86 // First step: base64 decoding |
86 // First step: base64 decoding |
87 char *base64decout = NULL; |
87 char *base64decout = NULL; |
88 size_t base64declen; |
88 size_t base64declen; |
89 bool ok = base64_decode_alloc(netmsg, strlen(netmsg), &base64decout, &base64declen); |
89 bool ok = base64_decode_alloc(netmsg, strlen(netmsg), &base64decout, &base64declen); |
90 if(ok && base64declen>3) { |
90 if(ok && base64declen>3) { |
91 // Second step: unzip with the QCompress header. That header is just a big-endian |
91 // Second step: unzip with the QCompress header. That header is just a big-endian |
92 // uint32 indicating the length of the uncompressed data. |
92 // uint32 indicating the length of the uncompressed data. |
|
93 uint8_t *ubyteBuf = (uint8_t*)base64decout; |
93 uint32_t unzipLen = |
94 uint32_t unzipLen = |
94 (((uint32_t)base64decout[0])<<24) |
95 (((uint32_t)ubyteBuf[0])<<24) |
95 + (((uint32_t)base64decout[1])<<16) |
96 + (((uint32_t)ubyteBuf[1])<<16) |
96 + (((uint32_t)base64decout[2])<<8) |
97 + (((uint32_t)ubyteBuf[2])<<8) |
97 + base64decout[3]; |
98 + ubyteBuf[3]; |
98 uint8_t *out = flib_malloc(unzipLen); |
99 if(unzipLen==0) { |
99 if(out) { |
100 *outbuf = NULL; |
100 uLongf actualUnzipLen = unzipLen; |
101 *outlen = 0; |
101 int resultcode = uncompress(out, &actualUnzipLen, (Bytef*)(base64decout+4), base64declen-4); |
102 result = 0; |
102 if(resultcode == Z_OK) { |
103 } else { |
103 result = out; |
104 uint8_t *out = flib_malloc(unzipLen); |
104 *outlen = actualUnzipLen; |
105 if(out) { |
105 out = NULL; |
106 uLongf actualUnzipLen = unzipLen; |
106 } else { |
107 int resultcode = uncompress(out, &actualUnzipLen, (Bytef*)(base64decout+4), base64declen-4); |
107 flib_log_e("Uncompressing drawn map failed. Code: %i", resultcode); |
108 if(resultcode == Z_OK) { |
|
109 *outbuf = out; |
|
110 *outlen = actualUnzipLen; |
|
111 out = NULL; |
|
112 result = 0; |
|
113 } else { |
|
114 flib_log_e("Uncompressing drawn map failed. Code: %i", resultcode); |
|
115 } |
108 } |
116 } |
|
117 free(out); |
109 } |
118 } |
110 free(out); |
|
111 } else { |
119 } else { |
112 flib_log_e("base64 decoding of drawn map failed."); |
120 flib_log_e("base64 decoding of drawn map failed."); |
113 } |
121 } |
114 free(base64decout); |
122 free(base64decout); |
115 return result; |
123 return result; |