odyssey/sources/read.c

59 lines
1.3 KiB
C
Raw Normal View History

2016-11-09 12:42:29 +00:00
/*
2017-07-06 11:44:19 +00:00
* Shapito.
2016-11-09 12:42:29 +00:00
*
* Protocol-level PostgreSQL client library.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
2017-07-06 11:29:52 +00:00
#include "sources/macro.h"
#include "sources/stream.h"
#include "sources/header.h"
#include "sources/key.h"
#include "sources/read.h"
2016-11-09 12:42:29 +00:00
int shapito_read_startup(uint32_t *len, char **data, uint32_t *size)
2016-11-09 13:08:10 +00:00
{
if (*size < sizeof(uint32_t))
return sizeof(uint32_t) - *size;
/* len */
uint32_t pos_size = *size;
char *pos = *data;
shapito_stream_read32(len, &pos, &pos_size);
2016-11-09 13:08:10 +00:00
uint32_t len_to_read;
len_to_read = *len - *size;
if (len_to_read > 0)
return len_to_read;
/* advance data stream */
*data += *len;
*size -= *len;
/* set actual data size */
*len -= sizeof(uint32_t);
return 0;
}
int shapito_read(uint32_t *len, char **data, uint32_t *size)
2016-11-09 12:42:29 +00:00
{
if (*size < sizeof(shapito_header_t))
return sizeof(shapito_header_t) - *size;
2016-11-09 12:42:29 +00:00
uint32_t pos_size = *size - sizeof(uint8_t);
char *pos = *data + sizeof(uint8_t);
2016-11-09 12:42:29 +00:00
/* type */
shapito_stream_read32(len, &pos, &pos_size);
2016-11-09 12:42:29 +00:00
uint32_t len_to_read;
len_to_read = (*len + sizeof(char)) - *size;
2016-11-09 12:42:29 +00:00
if (len_to_read > 0)
return len_to_read;
/* advance data stream */
*data += sizeof(uint8_t) + *len;
*size -= sizeof(uint8_t) + *len;
2016-11-09 13:08:10 +00:00
/* set actual data size */
2016-11-09 12:42:29 +00:00
*len -= sizeof(uint32_t);
return 0;
}