|
@@ -5,23 +5,26 @@
|
|
|
#include <stdint.h>
|
|
#include <stdint.h>
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
|
|
+#include <zlib.h>
|
|
|
|
|
|
|
|
#define MIN_MSG_SIZE (sizeof(uint64_t) + 1)
|
|
#define MIN_MSG_SIZE (sizeof(uint64_t) + 1)
|
|
|
#define MAX_MSG_SIZE (100 * 1024 * 1024)
|
|
#define MAX_MSG_SIZE (100 * 1024 * 1024)
|
|
|
|
|
|
|
|
-#define ERR_MORE_MSG -1
|
|
|
|
|
-#define ERR_DONE 0
|
|
|
|
|
-#define ERR_READ_MSG_SIZE 1
|
|
|
|
|
-#define ERR_MSG_SIZE 2
|
|
|
|
|
-#define ERR_OPEN_FILE 3
|
|
|
|
|
-#define ERR_DATA 4
|
|
|
|
|
-#define ERR_IO_OPEN 5
|
|
|
|
|
-#define ERR_IO_SEEK 6
|
|
|
|
|
-#define ERR_IO_WRITE 7
|
|
|
|
|
-#define ERR_IO_CLOSE 8
|
|
|
|
|
-#define ERR_NO_MEM 9
|
|
|
|
|
-#define ERR_INVALID_ARGS 10
|
|
|
|
|
-#define ERR_READ_MSG_ID 11
|
|
|
|
|
|
|
+#define ERR_MORE_MSG -1
|
|
|
|
|
+#define ERR_DONE 0
|
|
|
|
|
+#define ERR_READ_MSG_SIZE 1
|
|
|
|
|
+#define ERR_MSG_SIZE 2
|
|
|
|
|
+#define ERR_OPEN_FILE 3
|
|
|
|
|
+#define ERR_DATA 4
|
|
|
|
|
+#define ERR_IO_OPEN 5
|
|
|
|
|
+#define ERR_IO_SEEK 6
|
|
|
|
|
+#define ERR_IO_WRITE 7
|
|
|
|
|
+#define ERR_IO_CLOSE 8
|
|
|
|
|
+#define ERR_NO_MEM 9
|
|
|
|
|
+#define ERR_INVALID_ARGS 10
|
|
|
|
|
+#define ERR_READ_MSG_ID 11
|
|
|
|
|
+#define ERR_MSG_SIZE_INFLATED 12
|
|
|
|
|
+#define ERR_ZLIB 13
|
|
|
|
|
|
|
|
int write_msg_id(uint32_t msg_id)
|
|
int write_msg_id(uint32_t msg_id)
|
|
|
{
|
|
{
|
|
@@ -33,6 +36,28 @@ int write_msg_id(uint32_t msg_id)
|
|
|
return ERR_DONE;
|
|
return ERR_DONE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+int inflate_buf(uint32_t msg_size, void* buf, uint32_t msg_size_inflated,
|
|
|
|
|
+ void* inflated_buf)
|
|
|
|
|
+{
|
|
|
|
|
+ z_stream strm;
|
|
|
|
|
+ memset(&strm, 0, sizeof(z_stream));
|
|
|
|
|
+ int ret = inflateInit(&strm);
|
|
|
|
|
+ if (ret != Z_OK)
|
|
|
|
|
+ return ERR_ZLIB;
|
|
|
|
|
+
|
|
|
|
|
+ strm.avail_in = msg_size;
|
|
|
|
|
+ strm.next_in = buf;
|
|
|
|
|
+ strm.avail_out = msg_size_inflated;
|
|
|
|
|
+ strm.next_out = inflated_buf;
|
|
|
|
|
+
|
|
|
|
|
+ ret = inflate(&strm, Z_FINISH);
|
|
|
|
|
+ if(ret != Z_STREAM_END)
|
|
|
|
|
+ return ERR_ZLIB;
|
|
|
|
|
+
|
|
|
|
|
+ inflateEnd(&strm);
|
|
|
|
|
+ return ERR_DONE;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
int handle_msg(FILE* input_stream)
|
|
int handle_msg(FILE* input_stream)
|
|
|
{
|
|
{
|
|
|
uint32_t msg_id = 0;
|
|
uint32_t msg_id = 0;
|
|
@@ -54,6 +79,14 @@ int handle_msg(FILE* input_stream)
|
|
|
if (msg_size < MIN_MSG_SIZE || msg_size > MAX_MSG_SIZE)
|
|
if (msg_size < MIN_MSG_SIZE || msg_size > MAX_MSG_SIZE)
|
|
|
return ERR_MSG_SIZE;
|
|
return ERR_MSG_SIZE;
|
|
|
|
|
|
|
|
|
|
+ uint32_t msg_size_inflated = 0;
|
|
|
|
|
+ c = fread(&msg_size_inflated, 1, sizeof(uint32_t), input_stream);
|
|
|
|
|
+ if (c != sizeof(uint32_t))
|
|
|
|
|
+ return ERR_MSG_SIZE_INFLATED;
|
|
|
|
|
+ if (msg_size_inflated != 0 && (msg_size_inflated < MIN_MSG_SIZE ||
|
|
|
|
|
+ msg_size_inflated > MAX_MSG_SIZE))
|
|
|
|
|
+ return ERR_MSG_SIZE_INFLATED;
|
|
|
|
|
+
|
|
|
unsigned char* buf = (unsigned char*)malloc(msg_size);
|
|
unsigned char* buf = (unsigned char*)malloc(msg_size);
|
|
|
if (!buf)
|
|
if (!buf)
|
|
|
return ERR_NO_MEM;
|
|
return ERR_NO_MEM;
|
|
@@ -62,6 +95,24 @@ int handle_msg(FILE* input_stream)
|
|
|
if (c != msg_size)
|
|
if (c != msg_size)
|
|
|
return ERR_IO_OPEN;
|
|
return ERR_IO_OPEN;
|
|
|
|
|
|
|
|
|
|
+ if(msg_size_inflated)
|
|
|
|
|
+ {
|
|
|
|
|
+ unsigned char* inflated_buf = (unsigned char*)malloc(msg_size_inflated);
|
|
|
|
|
+ if (!inflated_buf)
|
|
|
|
|
+ return ERR_NO_MEM;
|
|
|
|
|
+
|
|
|
|
|
+ int err = inflate_buf(msg_size, buf, msg_size_inflated, inflated_buf);
|
|
|
|
|
+ if(err != ERR_DONE)
|
|
|
|
|
+ {
|
|
|
|
|
+ free(inflated_buf);
|
|
|
|
|
+ return err;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ free(buf);
|
|
|
|
|
+ buf = inflated_buf;
|
|
|
|
|
+ msg_size = msg_size_inflated;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
char* path = (char*)buf;
|
|
char* path = (char*)buf;
|
|
|
// strlen is unsafe
|
|
// strlen is unsafe
|
|
|
unsigned char* data = (unsigned char*)memchr(path, '\0', msg_size);
|
|
unsigned char* data = (unsigned char*)memchr(path, '\0', msg_size);
|