Skip to content

Commit

Permalink
Merge pull request #748 from robertdavidgraham/integration
Browse files Browse the repository at this point in the history
extract
  • Loading branch information
robertdavidgraham committed Dec 3, 2023
2 parents 082c8ed + 3ac323a commit dfd2001
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/util-extract.c
@@ -0,0 +1,73 @@
#include "util-extract.h"


unsigned char
e_next_byte(struct ebuf_t *ebuf) {
if (ebuf->offset + 1 > ebuf->max)
return -1;

return ebuf->buf[ebuf->offset++];
}

unsigned short
e_next_short16(struct ebuf_t *ebuf, int endian) {
const unsigned char *buf = ebuf->buf;
size_t offset = ebuf->offset;
unsigned short result;

if (ebuf->offset + 2 > ebuf->max)
return -1;

if (endian == EBUF_BE) {
result = buf[offset+0]<<8 | buf[offset+1];
} else {
result = buf[offset+1]<<8 | buf[offset+0];
}
ebuf->offset += 2;
return result;
}
unsigned e_next_int32(struct ebuf_t *ebuf, int endian) {
const unsigned char *buf = ebuf->buf;
size_t offset = ebuf->offset;
unsigned result;

if (ebuf->offset + 4 > ebuf->max)
return -1;

if (endian == EBUF_BE) {
result = buf[offset+0]<<24 | buf[offset+1] << 16
| buf[offset+2]<<8 | buf[offset+3] << 0;
} else {
result = buf[offset+3]<<24 | buf[offset+2] << 16
| buf[offset+1]<<8 | buf[offset+0] << 0;
}
ebuf->offset += 4;
return result;
}
unsigned long long
e_next_long64(struct ebuf_t *ebuf, int endian) {
const unsigned char *buf = ebuf->buf;
size_t offset = ebuf->offset;
unsigned long long hi;
unsigned long long lo;

if (ebuf->offset + 8 > ebuf->max)
return -1ll;

if (endian == EBUF_BE) {
hi = buf[offset+0]<<24 | buf[offset+1] << 16
| buf[offset+2]<<8 | buf[offset+3] << 0;
lo = buf[offset+4]<<24 | buf[offset+5] << 16
| buf[offset+6]<<8 | buf[offset+7] << 0;
} else {
lo = buf[offset+3]<<24 | buf[offset+2] << 16
| buf[offset+1]<<8 | buf[offset+0] << 0;
hi = buf[offset+7]<<24 | buf[offset+6] << 16
| buf[offset+5]<<8 | buf[offset+4] << 0;
}
ebuf->offset += 8;
return hi<<32ull | lo;

}


22 changes: 22 additions & 0 deletions src/util-extract.h
@@ -0,0 +1,22 @@
#ifndef UTIL_EXTRACT_H
#define UTIL_EXTRACT_H
#include <stdio.h>

struct ebuf_t {
const unsigned char *buf;
size_t offset;
size_t max;
};

enum {
EBUF_BE,
EBUG_LE,
};

unsigned char e_next_byte(struct ebuf_t *ebuf);
unsigned short e_next_short16(struct ebuf_t *ebuf, int endian);
unsigned e_next_int32(struct ebuf_t *ebuf, int endian);
unsigned long long e_next_long64(struct ebuf_t *ebuf, int endian);


#endif

0 comments on commit dfd2001

Please sign in to comment.