1 /* bytevector.h -*- mode:c; coding:utf-8; -*- 2 * 3 * Copyright (c) 2010-2021 Takashi Kato <ktakashi@ymail.com> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: $ 29 */ 30 #ifndef SAGITTARIUS_PRIVATE_BYTEVECTOR_H_ 31 #define SAGITTARIUS_PRIVATE_BYTEVECTOR_H_ 32 33 #include "sagittariusdefs.h" 34 35 SG_CLASS_DECL(Sg_ByteVectorClass); 36 #define SG_CLASS_BVECTOR (&Sg_ByteVectorClass) 37 38 struct SgByteVectorRec 39 { 40 SG_HEADER; 41 unsigned long literalp : 1; 42 long size : (SIZEOF_LONG*CHAR_BIT-1); 43 uint8_t *elements; 44 }; 45 46 #define SG_BVECTOR(obj) ((SgByteVector*)obj) 47 #define SG_BVECTORP(obj) SG_XTYPEP(obj, SG_CLASS_BVECTOR) 48 #define SG_BVECTOR_SIZE(obj) (SG_BVECTOR(obj)->size) 49 #define SG_BVECTOR_ELEMENTS(obj) (SG_BVECTOR(obj)->elements) 50 #define SG_BVECTOR_ELEMENT(obj, i) (SG_BVECTOR(obj)->elements[i]) 51 #define SG_BVECTOR_LITERALP(obj) (SG_BVECTOR(obj)->literalp) 52 53 #define SG_LITERAL_BVECTORP(obj) \ 54 (SG_BVECTORP(obj) && SG_BVECTOR(obj)->literalp) 55 #define SG_BVECTOR_SET_LITERAL(obj) \ 56 (SG_BVECTOR(obj)->literalp = TRUE) 57 58 /* utility macros */ 59 #define SG_IS_BYTE(v) (-128 <= v && v <= 127) 60 #define SG_IS_OCTET(v) (0 <= v && v <= 255) 61 62 #define SG_BVECTOR_IS_VALID_INDEX(bv, index) \ 63 (0 <= index && index < SG_BVECTOR_SIZE(bv)) 64 65 #ifdef HAVE_ALLOCA 66 #define SG_ALLOC_TEMP_BVECTOR(var, size) \ 67 do { \ 68 (var) = SG_BVECTOR(alloca(sizeof(SgByteVector))); \ 69 SG_SET_CLASS(var, SG_CLASS_BVECTOR); \ 70 SG_BVECTOR_SIZE(var) = size; \ 71 SG_BVECTOR_LITERALP(var) = FALSE; \ 72 SG_BVECTOR_ELEMENTS(var) = (uint8_t*)(alloca(size)); \ 73 } while (0) 74 #else 75 #define SG_ALLOC_TEMP_BVECTOR(var, size) \ 76 do { (var) = Sg_MakeByteVector(size, 0); } while (0) 77 #endif 78 79 #define SG_STATIC_BYTEVECTOR(size_, elements_) \ 80 { { SG_CLASS_STATIC_TAG(Sg_ByteVectorClass) }, TRUE, (size_), (elements_) } 81 82 SG_CDECL_BEGIN 83 84 SG_EXTERN SgObject Sg_MakeByteVector(long size, int fill); 85 86 SG_EXTERN SgObject Sg_MakeByteVectorFromU8Array(const uint8_t *buf, 87 long size); 88 89 SG_EXTERN SgObject Sg_NativeEndianness(); 90 SG_EXTERN int Sg_ByteVectorEqP(SgByteVector *bv1, SgByteVector *bv2); 91 SG_EXTERN SgObject Sg_ByteVectorCopy(SgByteVector *src, long start, long end); 92 SG_EXTERN void Sg_ByteVectorCopyX(SgByteVector *src, long srcStart, 93 SgByteVector *dst, long dstStart, 94 long size); 95 SG_EXTERN void Sg_ByteVectorFill(SgByteVector *bv, int value, 96 long start, long end); 97 SG_EXTERN SgObject Sg_ByteVectorReverseX(SgByteVector *bv, 98 long start, long end); 99 100 /* converter */ 101 SG_EXTERN SgObject Sg_ListToByteVector(SgObject lst, int bitCount, int signP); 102 SG_EXTERN SgObject Sg_ByteVectorToList(SgByteVector *bv, int bitCount, 103 int signP); 104 SG_EXTERN SgObject Sg_ByteVectorToString(SgByteVector *bv, 105 SgTranscoder *transcoder, 106 long start, long end); 107 SG_EXTERN SgObject Sg_StringToByteVector(SgString *s, 108 SgTranscoder *transcoder, 109 long start, long end); 110 /* u/s8 accessor */ 111 SG_EXTERN uint8_t Sg_ByteVectorU8Ref(SgByteVector *bv, long index); 112 SG_EXTERN void Sg_ByteVectorU8Set(SgByteVector *bv, long index, 113 uint8_t value); 114 SG_EXTERN int8_t Sg_ByteVectorS8Ref(SgByteVector *bv, long index); 115 SG_EXTERN void Sg_ByteVectorS8Set(SgByteVector *bv, long index, 116 int8_t value); 117 /* u/s16 accessor */ 118 SG_EXTERN uint16_t Sg_ByteVectorU16NativeRef(SgByteVector *bv, long index); 119 SG_EXTERN uint16_t Sg_ByteVectorU16LittleRef(SgByteVector *bv, long index); 120 SG_EXTERN uint16_t Sg_ByteVectorU16BigRef(SgByteVector *bv, long index); 121 SG_EXTERN void Sg_ByteVectorU16NativeSet(SgByteVector *bv, long index, 122 uint16_t value); 123 SG_EXTERN void Sg_ByteVectorU16LittleSet(SgByteVector *bv, long index, 124 uint16_t value); 125 SG_EXTERN void Sg_ByteVectorU16BigSet(SgByteVector *bv, long index, 126 uint16_t value); 127 SG_EXTERN int16_t Sg_ByteVectorS16NativeRef(SgByteVector *bv, long index); 128 SG_EXTERN int16_t Sg_ByteVectorS16LittleRef(SgByteVector *bv, long index); 129 SG_EXTERN int16_t Sg_ByteVectorS16BigRef(SgByteVector *bv, long index); 130 SG_EXTERN void Sg_ByteVectorS16NativeSet(SgByteVector *bv, long index, 131 int16_t value); 132 SG_EXTERN void Sg_ByteVectorS16LittleSet(SgByteVector *bv, long index, 133 int16_t value); 134 SG_EXTERN void Sg_ByteVectorS16BigSet(SgByteVector *bv, long index, 135 int16_t value); 136 /* u/s32 accessor */ 137 SG_EXTERN uint32_t Sg_ByteVectorU32NativeRef(SgByteVector *bv, long index); 138 SG_EXTERN uint32_t Sg_ByteVectorU32LittleRef(SgByteVector *bv, long index); 139 SG_EXTERN uint32_t Sg_ByteVectorU32BigRef(SgByteVector *bv, long index); 140 SG_EXTERN void Sg_ByteVectorU32NativeSet(SgByteVector *bv, long index, 141 uint32_t value); 142 SG_EXTERN void Sg_ByteVectorU32LittleSet(SgByteVector *bv, long index, 143 uint32_t value); 144 SG_EXTERN void Sg_ByteVectorU32BigSet(SgByteVector *bv, long index, 145 uint32_t value); 146 SG_EXTERN int32_t Sg_ByteVectorS32NativeRef(SgByteVector *bv, long index); 147 SG_EXTERN int32_t Sg_ByteVectorS32LittleRef(SgByteVector *bv, long index); 148 SG_EXTERN int32_t Sg_ByteVectorS32BigRef(SgByteVector *bv, long index); 149 SG_EXTERN void Sg_ByteVectorS32NativeSet(SgByteVector *bv, long index, 150 int32_t value); 151 SG_EXTERN void Sg_ByteVectorS32LittleSet(SgByteVector *bv, long index, 152 int32_t value); 153 SG_EXTERN void Sg_ByteVectorS32BigSet(SgByteVector *bv, long index, 154 int32_t value); 155 /* u/s64 accessor */ 156 SG_EXTERN uint64_t Sg_ByteVectorU64NativeRef(SgByteVector *bv, long index); 157 SG_EXTERN uint64_t Sg_ByteVectorU64LittleRef(SgByteVector *bv, long index); 158 SG_EXTERN uint64_t Sg_ByteVectorU64BigRef(SgByteVector *bv, long index); 159 SG_EXTERN void Sg_ByteVectorU64NativeSet(SgByteVector *bv, long index, 160 uint64_t value); 161 SG_EXTERN void Sg_ByteVectorU64LittleSet(SgByteVector *bv, long index, 162 uint64_t value); 163 SG_EXTERN void Sg_ByteVectorU64BigSet(SgByteVector *bv, long index, 164 uint64_t value); 165 SG_EXTERN int64_t Sg_ByteVectorS64NativeRef(SgByteVector *bv, long index); 166 SG_EXTERN int64_t Sg_ByteVectorS64LittleRef(SgByteVector *bv, long index); 167 SG_EXTERN int64_t Sg_ByteVectorS64BigRef(SgByteVector *bv, long index); 168 SG_EXTERN void Sg_ByteVectorS64NativeSet(SgByteVector *bv, long index, 169 int64_t value); 170 SG_EXTERN void Sg_ByteVectorS64LittleSet(SgByteVector *bv, long index, 171 int64_t value); 172 SG_EXTERN void Sg_ByteVectorS64BigSet(SgByteVector *bv, long index, 173 int64_t value); 174 /* float accessor */ 175 SG_EXTERN float Sg_ByteVectorIEEESingleNativeRef(SgByteVector *bv, 176 long index); 177 SG_EXTERN float Sg_ByteVectorIEEESingleLittleRef(SgByteVector *bv, 178 long index); 179 SG_EXTERN float Sg_ByteVectorIEEESingleBigRef(SgByteVector *bv, 180 long index); 181 SG_EXTERN void Sg_ByteVectorIEEESingleNativeSet(SgByteVector *bv, 182 long index, float value); 183 SG_EXTERN void Sg_ByteVectorIEEESingleLittleSet(SgByteVector *bv, 184 long index, float value); 185 SG_EXTERN void Sg_ByteVectorIEEESingleBigSet(SgByteVector *bv, 186 long index, float value); 187 /* double accessor */ 188 SG_EXTERN double Sg_ByteVectorIEEEDoubleNativeRef(SgByteVector *bv, 189 long index); 190 SG_EXTERN double Sg_ByteVectorIEEEDoubleLittleRef(SgByteVector *bv, 191 long index); 192 SG_EXTERN double Sg_ByteVectorIEEEDoubleBigRef(SgByteVector *bv, 193 long index); 194 SG_EXTERN void Sg_ByteVectorIEEEDoubleNativeSet(SgByteVector *bv, 195 long index, 196 double value); 197 SG_EXTERN void Sg_ByteVectorIEEEDoubleLittleSet(SgByteVector *bv, 198 long index, 199 double value); 200 SG_EXTERN void Sg_ByteVectorIEEEDoubleBigSet(SgByteVector *bv, 201 long index, double value); 202 203 /* utility */ 204 SG_EXTERN SgObject Sg_ByteVectorToIntegerBig(SgByteVector *bv, 205 long start, long end); 206 SG_EXTERN SgObject Sg_ByteVectorToIntegerSBig(SgByteVector *bv, 207 long start, long end); 208 SG_EXTERN SgObject Sg_IntegerToByteVectorBig(SgObject num, long size); 209 SG_EXTERN SgObject Sg_SIntegerToByteVectorBig(SgObject num, long size); 210 SG_EXTERN SgObject Sg_ByteVectorToIntegerLittle(SgByteVector *bv, 211 long start, long end); 212 SG_EXTERN SgObject Sg_ByteVectorToIntegerSLittle(SgByteVector *bv, 213 long start, long end); 214 SG_EXTERN SgObject Sg_IntegerToByteVectorLittle(SgObject num, long size); 215 SG_EXTERN SgObject Sg_SIntegerToByteVectorLittle(SgObject num, long size); 216 SG_EXTERN SgObject Sg_ByteVectorConcatenate(SgObject bvList); 217 218 #define Sg_ByteVectorToInteger Sg_ByteVectorToIntegerBig 219 #define Sg_IntegerToByteVector Sg_IntegerToByteVectorBig 220 #define Sg_ByteVectorToIntegerS Sg_ByteVectorToIntegerSBig 221 #define Sg_SIntegerToByteVector Sg_SIntegerToByteVectorBig 222 223 SG_EXTERN int Sg_ByteVectorCmp(SgByteVector *x, SgByteVector *y); 224 225 SG_CDECL_END 226 227 #endif /* SAGITTARIUS_BYTEVECTOR_H_ */ 228