Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / diskimage / include / bytepack.h
blobc5dc7494014ecd1d38950babc793938053680e40
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
27 #ifndef BYTEPACK_H
28 #define BYTEPACK_H
30 #ifndef EXEC_TYPES_H
31 #include <exec/types.h>
32 #endif
34 #include <string.h>
36 typedef struct {
37 UBYTE *start, *current, *end;
38 } BytePackBuffer;
40 static inline void BytePackInit (BytePackBuffer *b, APTR addr, ULONG len) {
41 b->start = b->current = addr;
42 b->end = (UBYTE *)addr + len;
45 static inline void BytePackWrite8 (BytePackBuffer *b, UBYTE val) {
46 if (b->current < b->end) {
47 *b->current++ = val;
51 static inline void BytePackWrite16MSB (BytePackBuffer *b, UWORD val) {
52 BytePackWrite8(b, val >> 8);
53 BytePackWrite8(b, val);
56 static inline void BytePackWrite24MSB (BytePackBuffer *b, ULONG val) {
57 BytePackWrite8(b, val >> 16);
58 BytePackWrite8(b, val >> 8);
59 BytePackWrite8(b, val);
61 static inline void BytePackWrite32MSB (BytePackBuffer *b, ULONG val) {
62 BytePackWrite8(b, val >> 24);
63 BytePackWrite8(b, val >> 16);
64 BytePackWrite8(b, val >> 8);
65 BytePackWrite8(b, val);
68 static inline void BytePackWrite16LSB (BytePackBuffer *b, UWORD val) {
69 BytePackWrite8(b, val);
70 BytePackWrite8(b, val >> 8);
73 static inline void BytePackWrite24LSB (BytePackBuffer *b, ULONG val) {
74 BytePackWrite8(b, val);
75 BytePackWrite8(b, val >> 8);
76 BytePackWrite8(b, val >> 16);
78 static inline void BytePackWrite32LSB (BytePackBuffer *b, ULONG val) {
79 BytePackWrite8(b, val);
80 BytePackWrite8(b, val >> 8);
81 BytePackWrite8(b, val >> 16);
82 BytePackWrite8(b, val >> 24);
85 static inline void BytePackWriteText (BytePackBuffer *b, CONST_STRPTR txt, ULONG len) {
86 ULONG space = b->end - b->current;
87 if (len > space) {
88 len = space;
90 if (len > 0) {
91 strncpy(b->current, txt, len);
92 b->current += len;
96 static inline ULONG BytePackBytesWritten (BytePackBuffer *b) {
97 return (ULONG)(b->current - b->start);
100 #endif