-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuffer.h
95 lines (92 loc) · 2.9 KB
/
buffer.h
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
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#include <string>
#define srs_assert(expression) assert(expression)
using namespace std;
/**
* bytes utility, used to:
* convert basic types to bytes,
* build basic types from bytes.
* @remark the buffer never mange the bytes, user must manage it.
*/
class SrsBuffer
{
private:
// current position at bytes.
char* p;
// the bytes data for buffer to read or write.
char* bytes;
// the total number of bytes.
int nb_bytes;
public:
// Create buffer with data b and size nn.
// @remark User must free the data b.
SrsBuffer(char* b, int nn);
~SrsBuffer();
public:
// Copy the object, keep position of buffer.
SrsBuffer* copy();
// Get the data and head of buffer.
// current-bytes = head() = data() + pos()
char* data();
char* head();
// Get the total size of buffer.
// left-bytes = size() - pos()
int size();
void set_size(int v);
// Get the current buffer position.
int pos();
// Left bytes in buffer, total size() minus the current pos().
int left();
// Whether buffer is empty.
bool empty();
// Whether buffer is able to supply required size of bytes.
// @remark User should check buffer by require then do read/write.
// @remark Assert the required_size is not negative.
bool require(int required_size);
public:
// Skip some size.
// @param size can be any value. positive to forward; negative to backward.
// @remark to skip(pos()) to reset buffer.
// @remark assert initialized, the data() not NULL.
void skip(int size);
public:
// Read 1bytes char from buffer.
int8_t read_1bytes();
// Read 2bytes int from buffer.
int16_t read_2bytes();
int16_t read_le2bytes();
// Read 3bytes int from buffer.
int32_t read_3bytes();
int32_t read_le3bytes();
// Read 4bytes int from buffer.
int32_t read_4bytes();
int32_t read_le4bytes();
// Read 8bytes int from buffer.
int64_t read_8bytes();
int64_t read_le8bytes();
// Read string from buffer, length specifies by param len.
std::string read_string(int len);
// Read bytes from buffer, length specifies by param len.
void read_bytes(char* data, int size);
public:
// Write 1bytes char to buffer.
void write_1bytes(int8_t value);
// Write 2bytes int to buffer.
void write_2bytes(int16_t value);
void write_le2bytes(int16_t value);
// Write 4bytes int to buffer.
void write_4bytes(int32_t value);
void write_le4bytes(int32_t value);
// Write 3bytes int to buffer.
void write_3bytes(int32_t value);
void write_le3bytes(int32_t value);
// Write 8bytes int to buffer.
void write_8bytes(int64_t value);
void write_le8bytes(int64_t value);
// Write string to buffer
void write_string(std::string value);
// Write bytes to buffer
void write_bytes(char* data, int size);
};