Skip to content

Commit

Permalink
Move simple datatype reading functions to own file.
Browse files Browse the repository at this point in the history
This facilitates easier testing.
  • Loading branch information
ralight committed Oct 3, 2018
1 parent 1488992 commit 99a1c0e
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 157 deletions.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(C_SRC
mqtt_protocol.h
net_mosq.c net_mosq.h
options.c
packet_datatypes.c
packet_mosq.c packet_mosq.h
read_handle.c read_handle.h
send_connect.c
Expand Down
4 changes: 4 additions & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ MOSQ_OBJS=mosquitto.o \
messages_mosq.o \
net_mosq.o \
options.o \
packet_datatypes.o \
packet_mosq.o \
read_handle.o \
send_connect.o \
Expand Down Expand Up @@ -136,6 +137,9 @@ net_mosq.o : net_mosq.c net_mosq.h
options.o : options.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

packet_datatypes.o : packet_datatypes.c packet_mosq.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

packet_mosq.o : packet_mosq.c packet_mosq.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

Expand Down
202 changes: 202 additions & 0 deletions lib/packet_datatypes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
Copyright (c) 2009-2018 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http:https://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http:https://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/

#include "config.h"

#include <assert.h>
#include <errno.h>
#include <string.h>

#ifdef WITH_BROKER
# include "mosquitto_broker_internal.h"
# ifdef WITH_WEBSOCKETS
# include <libwebsockets.h>
# endif
#else
# include "read_handle.h"
#endif

#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "net_mosq.h"
#include "packet_mosq.h"
#include "read_handle.h"
#ifdef WITH_BROKER
# include "sys_tree.h"
#else
# define G_BYTES_RECEIVED_INC(A)
# define G_BYTES_SENT_INC(A)
# define G_MSGS_SENT_INC(A)
# define G_PUB_MSGS_SENT_INC(A)
#endif


int packet__read_byte(struct mosquitto__packet *packet, uint8_t *byte)
{
assert(packet);
if(packet->pos+1 > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

*byte = packet->payload[packet->pos];
packet->pos++;

return MOSQ_ERR_SUCCESS;
}


void packet__write_byte(struct mosquitto__packet *packet, uint8_t byte)
{
assert(packet);
assert(packet->pos+1 <= packet->packet_length);

packet->payload[packet->pos] = byte;
packet->pos++;
}


int packet__read_bytes(struct mosquitto__packet *packet, void *bytes, uint32_t count)
{
assert(packet);
if(packet->pos+count > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

memcpy(bytes, &(packet->payload[packet->pos]), count);
packet->pos += count;

return MOSQ_ERR_SUCCESS;
}


void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, uint32_t count)
{
assert(packet);
assert(packet->pos+count <= packet->packet_length);

memcpy(&(packet->payload[packet->pos]), bytes, count);
packet->pos += count;
}


int packet__read_string(struct mosquitto__packet *packet, char **str, int *length)
{
uint16_t slen;
int rc;

assert(packet);
rc = packet__read_uint16(packet, &slen);
if(rc) return rc;

if(packet->pos+slen > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

*str = mosquitto__malloc(slen+1);
if(*str){
memcpy(*str, &(packet->payload[packet->pos]), slen);
(*str)[slen] = '\0';
packet->pos += slen;
}else{
return MOSQ_ERR_NOMEM;
}

*length = slen;
return MOSQ_ERR_SUCCESS;
}


void packet__write_string(struct mosquitto__packet *packet, const char *str, uint16_t length)
{
assert(packet);
packet__write_uint16(packet, length);
packet__write_bytes(packet, str, length);
}


int packet__read_uint16(struct mosquitto__packet *packet, uint16_t *word)
{
uint8_t msb, lsb;

assert(packet);
if(packet->pos+2 > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

msb = packet->payload[packet->pos];
packet->pos++;
lsb = packet->payload[packet->pos];
packet->pos++;

*word = (msb<<8) + lsb;

return MOSQ_ERR_SUCCESS;
}


void packet__write_uint16(struct mosquitto__packet *packet, uint16_t word)
{
packet__write_byte(packet, MOSQ_MSB(word));
packet__write_byte(packet, MOSQ_LSB(word));
}


int packet__read_uint32(struct mosquitto__packet *packet, uint32_t *word)
{
uint32_t val = 0;
int i;

assert(packet);
if(packet->pos+4 > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

for(i=0; i<4; i++){
val = (val << 8) + packet->payload[packet->pos];
packet->pos++;
}

*word = val;

return MOSQ_ERR_SUCCESS;
}


void packet__write_uint32(struct mosquitto__packet *packet, uint32_t word)
{
packet__write_byte(packet, (word & 0xFF000000) >> 24);
packet__write_byte(packet, (word & 0x00FF0000) >> 16);
packet__write_byte(packet, (word & 0x0000FF00) >> 8);
packet__write_byte(packet, (word & 0x000000FF));
}


int packet__read_varint(struct mosquitto__packet *packet, int32_t *word, uint8_t *bytes)
{
int i;
int remaining_mult = 1;
uint8_t byte;

*word = 0;
if(bytes) (*bytes) = 0;

for(i=0; i<4; i++){
if(packet->pos < packet->remaining_length){
if(bytes) (*bytes)++;
byte = packet->payload[packet->pos];
*word += (byte & 127) * remaining_mult;
remaining_mult *= 128;
packet->pos++;
if((byte & 128) == 0){
return MOSQ_ERR_SUCCESS;
}
}else{
return MOSQ_ERR_PROTOCOL;
}
}
return MOSQ_ERR_PROTOCOL;
}

157 changes: 0 additions & 157 deletions lib/packet_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,163 +150,6 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet)
}


int packet__read_byte(struct mosquitto__packet *packet, uint8_t *byte)
{
assert(packet);
if(packet->pos+1 > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

*byte = packet->payload[packet->pos];
packet->pos++;

return MOSQ_ERR_SUCCESS;
}


void packet__write_byte(struct mosquitto__packet *packet, uint8_t byte)
{
assert(packet);
assert(packet->pos+1 <= packet->packet_length);

packet->payload[packet->pos] = byte;
packet->pos++;
}


int packet__read_bytes(struct mosquitto__packet *packet, void *bytes, uint32_t count)
{
assert(packet);
if(packet->pos+count > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

memcpy(bytes, &(packet->payload[packet->pos]), count);
packet->pos += count;

return MOSQ_ERR_SUCCESS;
}


void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, uint32_t count)
{
assert(packet);
assert(packet->pos+count <= packet->packet_length);

memcpy(&(packet->payload[packet->pos]), bytes, count);
packet->pos += count;
}


int packet__read_string(struct mosquitto__packet *packet, char **str, int *length)
{
uint16_t slen;
int rc;

assert(packet);
rc = packet__read_uint16(packet, &slen);
if(rc) return rc;

if(packet->pos+slen > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

*str = mosquitto__malloc(slen+1);
if(*str){
memcpy(*str, &(packet->payload[packet->pos]), slen);
(*str)[slen] = '\0';
packet->pos += slen;
}else{
return MOSQ_ERR_NOMEM;
}

*length = slen;
return MOSQ_ERR_SUCCESS;
}


void packet__write_string(struct mosquitto__packet *packet, const char *str, uint16_t length)
{
assert(packet);
packet__write_uint16(packet, length);
packet__write_bytes(packet, str, length);
}


int packet__read_uint16(struct mosquitto__packet *packet, uint16_t *word)
{
uint8_t msb, lsb;

assert(packet);
if(packet->pos+2 > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

msb = packet->payload[packet->pos];
packet->pos++;
lsb = packet->payload[packet->pos];
packet->pos++;

*word = (msb<<8) + lsb;

return MOSQ_ERR_SUCCESS;
}


void packet__write_uint16(struct mosquitto__packet *packet, uint16_t word)
{
packet__write_byte(packet, MOSQ_MSB(word));
packet__write_byte(packet, MOSQ_LSB(word));
}


int packet__read_uint32(struct mosquitto__packet *packet, uint32_t *word)
{
uint32_t val = 0;
int i;

assert(packet);
if(packet->pos+4 > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

for(i=0; i<4; i++){
val = (val << 8) + packet->payload[packet->pos];
packet->pos++;
}

*word = val;

return MOSQ_ERR_SUCCESS;
}


void packet__write_uint32(struct mosquitto__packet *packet, uint32_t word)
{
packet__write_byte(packet, (word & 0xFF000000) >> 24);
packet__write_byte(packet, (word & 0x00FF0000) >> 16);
packet__write_byte(packet, (word & 0x0000FF00) >> 8);
packet__write_byte(packet, (word & 0x000000FF));
}


int packet__read_varint(struct mosquitto__packet *packet, int32_t *word, uint8_t *bytes)
{
int i;
int remaining_mult = 1;
uint8_t byte;

*word = 0;
if(bytes) (*bytes) = 0;

for(i=0; i<4; i++){
if(packet->pos < packet->remaining_length){
if(bytes) (*bytes)++;
byte = packet->payload[packet->pos];
*word += (byte & 127) * remaining_mult;
remaining_mult *= 128;
packet->pos++;
if((byte & 128) == 0){
return MOSQ_ERR_SUCCESS;
}
}else{
return MOSQ_ERR_PROTOCOL;
}
}
return MOSQ_ERR_PROTOCOL;
}


int packet__write(struct mosquitto *mosq)
{
ssize_t write_length;
Expand Down
Loading

0 comments on commit 99a1c0e

Please sign in to comment.