2#include "main-header.h"
3#include <platform-header.h>
5#define CNWDEBUGCOM(a, ...) noop()
18int command_add_encoded_arg_with_pos(
struct command *com,
byte *buf,
int bufsize,
int *pos) {
22 if ((*pos) > (bufsize - 3)) {
23 CNWDEBUGCOM(
"cannot decode out an argument at position %i with bufsize %i\n", *pos, bufsize);
26 byte fieldnum = buf[(*pos)];
27 byte fieldtype = buf[(*pos) + 1];
28 byte arraysize = buf[(*pos) + 2];
29 int len = fieldlen(fieldtype, arraysize);
36 nbuf = (
byte *)com->argbuf;
43 CNWDEBUGCOM(
" Copying bytes from pos %i - %i bytes of argument fieldnum=%i,type=%i, writing to combuf %i\n", *pos, len, fieldnum, fieldtype, (nbuf - (uint8_t *)&com->argbuf));
46 nbuf[npos++] = fieldnum;
47 nbuf[npos++] = fieldtype;
50 if (fieldtype & 0x80) {
51 nbuf[npos++] = arraysize;
54 if ((len + (*pos) + 2) > bufsize) {
55 CNWDEBUGCOM(
"Len: %i, pos: %i, (len+pos+2: %i), bufsize: %i, writing to %i\n", len, *pos, (*pos) + len + 2, bufsize, (nbuf - (uint8_t *)&com->argbuf));
61 for (j = 0; j < len; j++) {
62 nbuf[npos++] = buf[j + (*pos) + 2 + header];
65 if (fieldtype & 0x80) {
69 *pos = (*pos) + len + 2;
77int get_uint64(
byte *buf,
int bufsize,
int *pos, uint64_t *res) {
78 if (((*pos) + 8) > bufsize) {
84 for (i = 0; i < 8; i++) {
85 uint64_t v = ((uint64_t)buf[i + (*pos)]);
94int get_uint32(
byte *buf,
int bufsize,
int *pos, uint32_t *res) {
95 if (((*pos) + 4) > bufsize) {
101 for (i = 0; i < 4; i++) {
102 uint32_t v = ((uint32_t)buf[i + (*pos)]);
111int get_uint16(
byte *buf,
int bufsize,
int *pos, uint16_t *res) {
112 if (((*pos) + 2) > bufsize) {
118 for (i = 0; i < 2; i++) {
119 uint32_t v = ((uint32_t)buf[i + (*pos)]);
130int get_uint8(
byte *buf,
int bufsize,
int *pos, uint8_t *res) {
131 if (((*pos)) >= bufsize) {
132 CNWDEBUGCOM(
"Pos: %i, bufsize: %i\n", *pos, bufsize);
143int decode(
byte *buf,
int bufsize,
struct command *com) {
150 CNWDEBUGCOM(
"-------- Decoding --------\n");
157 if ((e = get_uint8(buf, bufsize, &pos, &res8)) != 0) {
161 CNWDEBUGCOM(
"not a binary encoding 0x%x\n", res8);
164 if ((e = get_uint8(buf, bufsize, &pos, &res8)) != 0) {
168 CNWDEBUGCOM(
"Incompatible version 0x%x\n", res8);
174 if ((e = get_uint64(buf, bufsize, &pos, &res)) != 0) {
179 if ((e = get_uint64(buf, bufsize, &pos, &res)) != 0) {
184 if ((e = get_uint64(buf, bufsize, &pos, &res)) != 0) {
189 if ((e = get_uint32(buf, bufsize, &pos, &res32)) != 0) {
192 com->
index = (int)res32;
195 if ((e = get_uint8(buf, bufsize, &pos, &res8)) != 0) {
200 if ((e = get_uint8(buf, bufsize, &pos, &res8)) != 0) {
205 if ((e = get_uint8(buf, bufsize, &pos, &res8)) != 0) {
211 CNWDEBUGCOM(
"Command type: %i\n", com->
com);
212 CNWDEBUGCOM(
"Sender: %p\n", com->
sender);
213 CNWDEBUGCOM(
"Index: %p\n", com->
index);
214 CNWDEBUGCOM(
"Args: %i\n", args);
218 if (com->
sender > 0xFFFFFFFF) {
225 for (i = 0; i < args; i++) {
244 e = command_add_encoded_arg_with_pos(com, buf, bufsize, &pos);
int get_arg_size_inmem(const struct command *com, int index)
given an argument by index [0..n], will returns the number in bytes this argument takes up in memory....
const byte * get_arg_new(const struct command *com, int index)
given an argument by index[0..n], will returns a pointer to it. This will include the fieldnumber and...