1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 21:48:13 +00:00
trezor-firmware/micropython/bootloader/protobuf.c

74 lines
1.6 KiB
C
Raw Normal View History

2017-04-15 15:44:19 +00:00
#include <string.h>
#include "protobuf.h"
void pb_start(PB_CTX *ctx, uint16_t msg_id)
{
memset(ctx->buf, 0, sizeof(ctx->buf));
ctx->buf[0] = '?';
ctx->buf[1] = '#';
ctx->buf[2] = '#';
ctx->buf[3] = (msg_id >> 8) & 0xFF;
ctx->buf[4] = msg_id & 0xFF;
2017-04-16 15:37:39 +00:00
ctx->pos = 9;
ctx->len = 0;
2017-04-15 15:44:19 +00:00
}
2017-04-16 15:37:39 +00:00
void pb_end(PB_CTX *ctx)
2017-04-15 15:44:19 +00:00
{
2017-04-16 15:37:39 +00:00
ctx->buf[5] = (ctx->len >> 24) & 0xFF;
ctx->buf[6] = (ctx->len >> 16) & 0xFF;
ctx->buf[7] = (ctx->len >> 8) & 0xFF;
ctx->buf[8] = ctx->len & 0xFF;
// align to 64 bytes
ctx->pos += (-ctx->pos) & 63;
2017-04-15 15:44:19 +00:00
}
2017-04-16 15:37:39 +00:00
inline static void pb_append(PB_CTX *ctx, uint8_t b)
2017-04-15 15:44:19 +00:00
{
2017-04-16 15:37:39 +00:00
ctx->buf[ctx->pos] = b;
ctx->pos++;
if (ctx->pos % 64 == 0) {
ctx->buf[ctx->pos] = '?';
ctx->pos++;
}
ctx->len++;
2017-04-15 15:44:19 +00:00
}
2017-04-16 15:37:39 +00:00
static void pb_varint(PB_CTX *ctx, uint32_t val)
2017-04-15 15:44:19 +00:00
{
for (;;) {
if (val < 0x80) {
2017-04-16 15:37:39 +00:00
pb_append(ctx, val & 0x7F);
2017-04-15 15:44:19 +00:00
break;
} else {
2017-04-16 15:37:39 +00:00
pb_append(ctx, (val & 0x7F) | 0x80);
2017-04-15 15:44:19 +00:00
val >>= 7;
}
}
}
2017-04-16 15:37:39 +00:00
void pb_add_bool(PB_CTX *ctx, uint32_t field_number, bool val)
{
field_number = (field_number << 3) | 0;
pb_varint(ctx, field_number);
pb_append(ctx, val);
}
void pb_add_string(PB_CTX *ctx, uint32_t field_number, const char *val)
{
field_number = (field_number << 3) | 2;
pb_varint(ctx, field_number);
size_t len = strlen(val);
pb_varint(ctx, len);
for (size_t i = 0; i < len; i++) {
pb_append(ctx, val[i]);
}
}
void pb_add_varint(PB_CTX *ctx, uint32_t field_number, uint32_t val)
{
field_number = (field_number << 3) | 0;
pb_varint(ctx, field_number);
pb_varint(ctx, val);
}