Skip to content

C source cleanup #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
target = hs100
objects = comms.o handlers.o hs100.o escape.o

CFLAGS = -std=gnu99 -Wall -Werror
CFLAGS = -std=c99 -Wall -Werror
# for those who like their warnings turned up to 11
CFLAGS += -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wshadow -pedantic
CFLAGS += -Wpointer-arith -Wcast-align -Wcast-qual -Wwrite-strings -Wundef

# also passes cppcheck --std=c99 --enable=all --suppress=missingInclude *.c

MACHINE := $(shell $(CC) -dumpmachine)
$(info MACHINE="$(MACHINE)")
Expand Down
20 changes: 10 additions & 10 deletions comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include "comms.h"

bool hs100_encrypt(uint8_t *d, const uint8_t *s, size_t len)
static bool hs100_encrypt(uint8_t *d, const uint8_t *s, size_t len)
{
uint8_t key, temp;
uint8_t key;
size_t i;

if (d == NULL)
Expand All @@ -31,16 +31,16 @@ bool hs100_encrypt(uint8_t *d, const uint8_t *s, size_t len)

key = 0xab;
for (i = 0; i < len; i++) {
temp = key ^ s[i];
uint8_t temp = key ^ s[i];
key = temp;
d[i] = temp;
}
return true;
}

bool hs100_decrypt(uint8_t *d, const uint8_t *s, size_t len)
static bool hs100_decrypt(uint8_t *d, const uint8_t *s, size_t len)
{
uint8_t key, temp;
uint8_t key;
size_t i;

if (d == NULL)
Expand All @@ -52,14 +52,14 @@ bool hs100_decrypt(uint8_t *d, const uint8_t *s, size_t len)

key = 0xab;
for (i = 0; i < len; i++) {
temp = key ^ s[i];
uint8_t temp = key ^ s[i];
key = s[i];
d[i] = temp;
}
return true;
}

uint8_t *hs100_encode(size_t *outlen, const char *srcmsg)
static uint8_t *hs100_encode(size_t *outlen, const char *srcmsg)
{
size_t srcmsg_len;
uint8_t *d;
Expand All @@ -73,7 +73,7 @@ uint8_t *hs100_encode(size_t *outlen, const char *srcmsg)
d = calloc(1, *outlen);
if (d == NULL)
return NULL;
if (!hs100_encrypt(d + 4, (uint8_t *) srcmsg, srcmsg_len)) {
if (!hs100_encrypt(d + 4, (const uint8_t *) srcmsg, srcmsg_len)) {
free(d);
return NULL;
}
Expand All @@ -83,7 +83,7 @@ uint8_t *hs100_encode(size_t *outlen, const char *srcmsg)
return d;
}

char *hs100_decode(const uint8_t *s, size_t s_len)
static char *hs100_decode(const uint8_t *s, size_t s_len)
{
uint32_t in_s_len;
char *outbuf;
Expand Down Expand Up @@ -182,7 +182,7 @@ char *hs100_send(const char *servaddr, const char *msg)
}
msglen = ntohl(msglen) + 4;
recvbuf = calloc(1, (size_t) msglen);
recvsize = recv(sock, recvbuf, msglen, MSG_WAITALL);
(void) recv(sock, recvbuf, msglen, MSG_WAITALL);
#ifdef _WIN32
closesocket(sock);
#else
Expand Down
1 change: 1 addition & 0 deletions handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include "comms.h"
#include "escape.h"
#include "handlers.h"

char *handler_associate(int argc, char *argv[])
{
Expand Down
5 changes: 5 additions & 0 deletions handlers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
char *handler_associate(int argc, char *argv[]);
char *handler_set_server(int argc, char *argv[]);
char *handler_set_relay_state(int argc, char *argv[]);
char *handler_get_realtime(int argc, char *argv[]);
17 changes: 6 additions & 11 deletions hs100.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
#include <string.h>
#include "version.h"
#include "comms.h"

// handlers for more complicated commands
extern char *handler_associate(int argc, char *argv[]);
extern char *handler_set_server(int argc, char *argv[]);
extern char *handler_set_relay_state(int argc, char *argv[]);
extern char *handler_get_realtime(int argc, char *argv[]);
#include "handlers.h"

struct cmd_s {
char *command;
char *help;
char *json;
const char *command;
const char *help;
const char *json;
char *(*handler) (int argc, char *argv[]);
};
struct cmd_s cmds[] = {
Expand Down Expand Up @@ -73,7 +68,7 @@ struct cmd_s cmds[] = {
},
};

struct cmd_s *get_cmd_from_name(char *needle)
static struct cmd_s *get_cmd_from_name(char *needle)
{
int cmds_index = 0;
while (cmds[cmds_index].command) {
Expand All @@ -84,7 +79,7 @@ struct cmd_s *get_cmd_from_name(char *needle)
return NULL;
}

void print_usage()
static void print_usage(void)
{
fprintf(stderr, "hs100 version " VERSION_STRING
", Copyright (C) 2018-2019 Jason Benaim.\n"
Expand Down