Skip to content

Commit dfd2001

Browse files
Merge pull request #748 from robertdavidgraham/integration
extract
2 parents 082c8ed + 3ac323a commit dfd2001

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/util-extract.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "util-extract.h"
2+
3+
4+
unsigned char
5+
e_next_byte(struct ebuf_t *ebuf) {
6+
if (ebuf->offset + 1 > ebuf->max)
7+
return -1;
8+
9+
return ebuf->buf[ebuf->offset++];
10+
}
11+
12+
unsigned short
13+
e_next_short16(struct ebuf_t *ebuf, int endian) {
14+
const unsigned char *buf = ebuf->buf;
15+
size_t offset = ebuf->offset;
16+
unsigned short result;
17+
18+
if (ebuf->offset + 2 > ebuf->max)
19+
return -1;
20+
21+
if (endian == EBUF_BE) {
22+
result = buf[offset+0]<<8 | buf[offset+1];
23+
} else {
24+
result = buf[offset+1]<<8 | buf[offset+0];
25+
}
26+
ebuf->offset += 2;
27+
return result;
28+
}
29+
unsigned e_next_int32(struct ebuf_t *ebuf, int endian) {
30+
const unsigned char *buf = ebuf->buf;
31+
size_t offset = ebuf->offset;
32+
unsigned result;
33+
34+
if (ebuf->offset + 4 > ebuf->max)
35+
return -1;
36+
37+
if (endian == EBUF_BE) {
38+
result = buf[offset+0]<<24 | buf[offset+1] << 16
39+
| buf[offset+2]<<8 | buf[offset+3] << 0;
40+
} else {
41+
result = buf[offset+3]<<24 | buf[offset+2] << 16
42+
| buf[offset+1]<<8 | buf[offset+0] << 0;
43+
}
44+
ebuf->offset += 4;
45+
return result;
46+
}
47+
unsigned long long
48+
e_next_long64(struct ebuf_t *ebuf, int endian) {
49+
const unsigned char *buf = ebuf->buf;
50+
size_t offset = ebuf->offset;
51+
unsigned long long hi;
52+
unsigned long long lo;
53+
54+
if (ebuf->offset + 8 > ebuf->max)
55+
return -1ll;
56+
57+
if (endian == EBUF_BE) {
58+
hi = buf[offset+0]<<24 | buf[offset+1] << 16
59+
| buf[offset+2]<<8 | buf[offset+3] << 0;
60+
lo = buf[offset+4]<<24 | buf[offset+5] << 16
61+
| buf[offset+6]<<8 | buf[offset+7] << 0;
62+
} else {
63+
lo = buf[offset+3]<<24 | buf[offset+2] << 16
64+
| buf[offset+1]<<8 | buf[offset+0] << 0;
65+
hi = buf[offset+7]<<24 | buf[offset+6] << 16
66+
| buf[offset+5]<<8 | buf[offset+4] << 0;
67+
}
68+
ebuf->offset += 8;
69+
return hi<<32ull | lo;
70+
71+
}
72+
73+

src/util-extract.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef UTIL_EXTRACT_H
2+
#define UTIL_EXTRACT_H
3+
#include <stdio.h>
4+
5+
struct ebuf_t {
6+
const unsigned char *buf;
7+
size_t offset;
8+
size_t max;
9+
};
10+
11+
enum {
12+
EBUF_BE,
13+
EBUG_LE,
14+
};
15+
16+
unsigned char e_next_byte(struct ebuf_t *ebuf);
17+
unsigned short e_next_short16(struct ebuf_t *ebuf, int endian);
18+
unsigned e_next_int32(struct ebuf_t *ebuf, int endian);
19+
unsigned long long e_next_long64(struct ebuf_t *ebuf, int endian);
20+
21+
22+
#endif

0 commit comments

Comments
 (0)