-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVoteEncodingText.c
More file actions
executable file
·95 lines (86 loc) · 2.47 KB
/
VoteEncodingText.c
File metadata and controls
executable file
·95 lines (86 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Created by tw on 2018/3/2.
//
/**
* Routines for Text encoding of vote messages.
* Wire Format:
* "Voting <v|i> [R] <candidate ID> <count>"
*
*/
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "VoteEncoding.h"
#include "VoteProtocol.h"
static const char *MAGIC = "Voting";
static const char *VOTESTR = "v";
static const char *INQSTR = "s";
static const char *RESPONSESTR = "R";
static const char *DELIMSTR = " ";
enum {
BASE = 10
};
/**
* Encode voting message info as text string.
* WARNING: Message will be silently truncate if buffer if too small
* Invariants (e.g 0 <=candidate <= 1000) not checked.
*
*/
size_t Encode(const VoteInfo *v, uint8_t *outBuf, const size_t bufSize) {
uint8_t *bufPtr = outBuf;
long size = (size_t) bufSize;
int rv = snprintf((char *)bufPtr, size, "%s %c %s %d", MAGIC, (v->isInquiry ? 'i':'v'), (v->isResponse ? "R" : ""), v->condidate);
bufPtr += rv;
size -= rv;
if (v->isResponse) {
rv = snprintf((char *) bufPtr, size, " %llu", v->count);
bufPtr += rv;
}
return (size_t) (bufPtr - outBuf);
}
/**
* Extract message information form given buffer.
* Note:modifies input buffer.
*/
bool Decode(uint8_t *inBuf, const size_t mSize, VoteInfo *v) {
char *token;
token = strtok((char *)inBuf, DELIMSTR);
//Check for maginc
if (token == NULL || strcmp(token, MAGIC) != 0)
return false;
//Get vote/inquiry indicator
token = strtok(NULL, DELIMSTR);
if (token == NULL)
return false;
if (strcmp(token, VOTESTR) == 0)
v->isInquiry = false;
else if (strcmp(token, INQSTR) == 0)
v->isInquiry = true;
else
return false;
//Next token is either Response flag or candidate ID
token = strtok(NULL, DELIMSTR);
if (token == NULL)
return false;
if (strcmp(token, RESPONSESTR) == 0) {
v->isResponse = true;
token = strtok(NULL, DELIMSTR);
if (token == NULL)
return false;
} else {
v->isResponse = false;
}
//Get candidate
v->condidate = atoi(token);
if (v->isResponse) {
token = strtok(NULL, DELIMSTR);
if (token == NULL)
return false;
v->count = strtoll(token, NULL, BASE);
} else {
v->count = 0L;
}
return true;
}