(c-include "stdint.h") (define-c-int-type int8_t) (define-c-int-type int16_t) (define-c-int-type int32_t) (define-c-int-type int64_t) (define-c-int-type uint16_t) (define-c-int-type uint32_t) (define-c-int-type uint64_t) (c-declare " static int16_t sexp_swap_s16(int16_t n) { return (n << 8) | ((n >> 8) & 0xFF); } static uint16_t sexp_swap_u16(uint16_t n) { return (n >> 8) | ((n & 0xFF) << 8); } static int32_t sexp_swap_s32(int32_t n) { n = ((n << 8) & 0xFF00FF00) | ((n >> 8) & 0xFF00FF); return (n << 16) | ((n >> 16) & 0xFFFF); } static uint32_t sexp_swap_u32(uint32_t n) { return ((n>>24)&0xff) | ((n<<8)&0xff0000) | ((n>>8)&0xff00) | ((n<<24)&0xff000000); } static int64_t sexp_swap_s64(int64_t n) { n = ((n << 8) & 0xFF00FF00FF00FF00ULL) | ((n >> 8) & 0x00FF00FF00FF00FFULL); n = ((n << 16) & 0xFFFF0000FFFF0000ULL) | ((n >> 16) & 0x0000FFFF0000FFFFULL); return (n << 32) | ((n >> 32) & 0xFFFFFFFFULL); } static uint64_t sexp_swap_u64(uint64_t n) { n = ((n << 8) & 0xFF00FF00FF00FF00ULL) | ((n >> 8) & 0x00FF00FF00FF00FFULL); n = ((n << 16) & 0xFFFF0000FFFF0000ULL ) | ((n >> 16) & 0x0000FFFF0000FFFFULL); return (n << 32) | (n >> 32); } static float sexp_swap_float(const float x) { float y; const uint32_t* xs = (const uint32_t*) &x; uint32_t* ys = (uint32_t*) &y; *ys = sexp_swap_u32(*xs); return y; } static double sexp_swap_double(const double x) { double y; const uint64_t* xs = (const uint64_t*) &x; uint64_t* ys = (uint64_t*) &y; *ys = sexp_swap_u64(*xs); return y; } sexp_sint_t decode_utf8(unsigned char* p, int ch_len) { if (ch_len <= 1) return *p; else if (ch_len == 2) return ((p[0]&0x3F)<<6) + (p[1]&0x3F); else if (ch_len == 3) return ((p[0]&0x1F)<<12) + ((p[1]&0x3F)<<6) + (p[2]&0x3F); else return ((p[0]&0x0F)<<18) + ((p[1]&0x3F)<<12) + ((p[2]&0x3F)<<6) + (p[3]&0x3F); } sexp str2utf16(sexp ctx, char* s, int len, sexp endianness) { unsigned char *p = (unsigned char*) s, *q; uint16_t *utf16, hi, lo; sexp_sint_t utf16_len, ch_len, ch, i; sexp res; q = p + len; for (utf16_len=0; p> 10) + ((ch) >> 10)); lo = (0xDC00 + ((ch) & 0x3FF)); (*utf16++) = hi; (*utf16++) = lo; } else { (*utf16++) = (uint16_t)ch; } p += ch_len; } if (endianness != sexp_global(ctx, SEXP_G_ENDIANNESS)) { utf16 = (uint16_t*)sexp_bytes_data(res); for (i=0; i>18)); *p++ = (0x80 + ((c>>12)&0x3F)); *p++ = (0x80 + ((c>>6)&0x3F)); *p = (0x80 + (c&0x3F)); break; case 3: *p++ = (0xE0 + ((c)>>12)); *p++ = (0x80 + ((c>>6)&0x3F)); *p = (0x80 + (c&0x3F)); break; case 2: *p++ = (0xC0 + ((c)>>6)); *p = (0x80 + (c&0x3F)); break; default: *p = c; break; } } sexp utf16_2_str(sexp ctx, char* bv, int len, sexp endianness, int endianness_mandatory) { int swap = endianness != sexp_global(ctx, SEXP_G_ENDIANNESS); uint16_t ch, ch2; sexp_sint_t i, ch_len, utf8_len=0, start=0; sexp res; unsigned char* dst; if (!endianness_mandatory && len>1) { ch = *(uint16_t*)(bv); if (ch == 0xFFFE) { swap = 1; start = 2; } else if (ch == 0xFEFF) { start = 2; } } for (i=start; i+13) { ch = *(uint32_t*)(bv); if (ch == 0xFFFE0000) { swap = 1; start = 4; } else if (ch == 0xFEFF) { start = 4; } } for (i=start; i+3utf16 "str2utf16") ((value ctx sexp) string (value (string-size arg1) int) (default (native-endianness) sexp))) (define-c sexp (%string->utf32 "str2utf32") ((value ctx sexp) string (value (string-size arg1) int) (value (string-length arg1) int) (default (native-endianness) sexp))) (define-c sexp (%utf16->string "utf16_2_str") ((value ctx sexp) bytevector (value (bytevector-length arg1) int) sexp (default SEXP_FALSE boolean))) (define-c sexp (%utf32->string "utf32_2_str") ((value ctx sexp) bytevector (value (bytevector-length arg1) int) sexp (default SEXP_FALSE boolean)))