/
pack.h
55 lines (44 loc) · 1.68 KB
/
pack.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
#ifndef PACK_H
#define PACK_H
/*
* dependent headers
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
/*
* packing declarations
*/
void pack_int(void **ptr, uint8_t *byte, uint8_t *off, int val, unsigned int width);
void pack_uint(void **ptr, uint8_t *byte, uint8_t *off, unsigned int val, unsigned int width);
void pack_flush(void **ptr, uint8_t *byte, uint8_t *off);
void fpack_int(FILE *file, uint8_t *byte, uint8_t *off, int val, unsigned int width);
void fpack_uint(FILE *file, uint8_t *byte, uint8_t *off, unsigned int val, unsigned int width);
void fpack_flush(FILE *file, uint8_t *byte, uint8_t *off);
int unpack_int(void **ptr, uint8_t *byte, uint8_t *off, unsigned int width);
unsigned int unpack_uint(void **ptr, uint8_t *byte, uint8_t *off, unsigned int width);
void unpack_align(void **ptr, uint8_t *byte, uint8_t *off);
int funpack_int(FILE *file, uint8_t *byte, uint8_t *off, unsigned int width);
unsigned int funpack_uint(FILE *file, uint8_t *byte, uint8_t *off, unsigned int width);
void funpack_align(FILE *file, uint8_t *byte, uint8_t *off);
/**
* Unsigned extend a value.
* @val: The value to be extended.
* @bits: The number of bits to extend.
*/
static inline unsigned int pack_uext(unsigned int val, int bits)
{
assert((bits > 0) && (bits < (8 * sizeof(unsigned int))));
return (val << ((8 * sizeof(unsigned int)) - bits)) >> ((8 * sizeof(unsigned int)) - bits);
}
/**
* Sign extend a value.
* @val: The value to be extended.
* @bits: The number of bits to extend.
*/
static inline int pack_sext(int val, int bits)
{
assert((bits > 0) && (bits < (8 * sizeof(int))));
return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
}
#endif