-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.c
More file actions
32 lines (26 loc) · 708 Bytes
/
encoder.c
File metadata and controls
32 lines (26 loc) · 708 Bytes
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
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "encoder.h"
#include "util.h"
#include "varint.h"
static inline bool encode_overflows(struct encoder *encoder, int len)
{
return encoder->cursor + len >= encoder->buf + encoder->buflen;
}
void writebuf(struct encoder *encoder, const unsigned char *data, int len)
{
if (encode_overflows(encoder, len))
fail(4, "buffer overrun");
memcpy(encoder->cursor, data, len);
encoder->cursor += len;
}
void writebuf_varint(struct encoder *encoder, uint64_t n)
{
int len = varint_length(n);
if (encode_overflows(encoder, len))
fail(4, "buffer overrun");
varint_write(encoder->cursor, n);
encoder->cursor += len;
}