| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* strm_binary.d - Byte encoding/decoding for streams |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* Copyright (c) 2025 Daniel Kochmanski |
| 11 |
* |
| 12 |
* See file 'LICENSE' for the copyright details. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <ecl/ecl.h> |
| 17 |
#include <ecl/internal.h> |
| 18 |
|
| 19 |
/* Binary operators */ |
| 20 |
|
| 21 |
cl_object |
| 22 |
ecl_binary_read_byte(cl_object strm) |
| 23 |
{ |
| 24 |
cl_index (*read_byte8)(cl_object, unsigned char *, cl_index); |
| 25 |
unsigned char *buf = strm->stream.byte_buffer; |
| 26 |
cl_index nbytes; |
| 27 |
strm->stream.last_char = EOF; |
| 28 |
strm->stream.last_byte = OBJNULL; |
| 29 |
read_byte8 = strm->stream.ops->read_byte8; |
| 30 |
nbytes = strm->stream.byte_size/8; |
| 31 |
if (read_byte8(strm, buf, nbytes) < nbytes) |
| 32 |
return OBJNULL; |
| 33 |
return strm->stream.byte_decoder(strm, buf); |
| 34 |
} |
| 35 |
|
| 36 |
void |
| 37 |
ecl_binary_write_byte(cl_object strm, cl_object byte) |
| 38 |
{ |
| 39 |
cl_index (*write_byte8)(cl_object strm, unsigned char *c, cl_index n); |
| 40 |
cl_index nbytes = strm->stream.byte_size/8; |
| 41 |
unsigned char *buf = strm->stream.byte_buffer; |
| 42 |
write_byte8 = strm->stream.ops->write_byte8; |
| 43 |
strm->stream.byte_encoder(strm, buf, byte); |
| 44 |
write_byte8(strm, buf, nbytes); |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
ecl_binary_unread_byte(cl_object strm, cl_object byte) |
| 49 |
{ |
| 50 |
unlikely_if (strm->stream.last_char != EOF |
| 51 |
|| strm->stream.last_byte != OBJNULL) { |
| 52 |
ecl_unread_twice(strm); |
| 53 |
} |
| 54 |
strm->stream.last_byte = byte; |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* 8-bit unsigned |
| 59 |
*/ |
| 60 |
|
| 61 |
cl_object |
| 62 |
ecl_binary_u8_decoder(cl_object strm, unsigned char *buf) |
| 63 |
{ |
| 64 |
unsigned char c = buf[0]; |
| 65 |
return ecl_make_fixnum(c); |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
ecl_binary_u8_encoder(cl_object strm, unsigned char *buf, cl_object byte) |
| 70 |
{ |
| 71 |
unsigned char c = ecl_to_uint8_t(byte); |
| 72 |
buf[0] = c; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* 8-bit signed |
| 77 |
*/ |
| 78 |
cl_object |
| 79 |
ecl_binary_s8_decoder(cl_object strm, unsigned char *buf) |
| 80 |
{ |
| 81 |
signed char c = (signed char)buf[0]; |
| 82 |
return ecl_make_fixnum(c); |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
ecl_binary_s8_encoder(cl_object strm, unsigned char *buf, cl_object byte) |
| 87 |
{ |
| 88 |
signed char c = ecl_to_int8_t(byte); |
| 89 |
buf[0] = (unsigned char)c; |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* Big Endian |
| 94 |
*/ |
| 95 |
cl_object |
| 96 |
ecl_binary_be_decoder(cl_object strm, unsigned char *buf) |
| 97 |
{ |
| 98 |
cl_index idx, ndx = strm->stream.byte_size/8; |
| 99 |
cl_object output = OBJNULL; |
| 100 |
cl_object offset = ecl_make_fixnum(8); |
| 101 |
unsigned char c; |
| 102 |
for (idx=0; idx<ndx; idx++) { |
| 103 |
c = buf[idx]; |
| 104 |
if (output) { |
| 105 |
output = cl_logior(2, ecl_make_fixnum(c), cl_ash(output, offset)); |
| 106 |
} else { |
| 107 |
output = (strm->stream.flags & ECL_STREAM_SIGNED_BYTES) |
| 108 |
? ecl_make_fixnum((signed char)c) |
| 109 |
: ecl_make_fixnum((unsigned char)c); |
| 110 |
} |
| 111 |
} |
| 112 |
return output; |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
ecl_binary_be_encoder(cl_object strm, unsigned char *buf, cl_object byte) |
| 117 |
{ |
| 118 |
cl_index idx, ndx = strm->stream.byte_size/8; |
| 119 |
cl_object offset = ecl_make_fixnum(-8); |
| 120 |
cl_object mask = ecl_make_fixnum(0xFF); |
| 121 |
for (idx=0; idx<ndx; idx++) { |
| 122 |
cl_object b = cl_logand(2, byte, mask); |
| 123 |
buf[ndx-idx-1] = (unsigned char)ecl_fixnum(b); |
| 124 |
byte = cl_ash(byte, offset); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* Little Endian |
| 130 |
*/ |
| 131 |
cl_object |
| 132 |
ecl_binary_le_decoder(cl_object strm, unsigned char *buf) |
| 133 |
{ |
| 134 |
cl_index idx, ndx = strm->stream.byte_size/8; |
| 135 |
cl_object output = OBJNULL; |
| 136 |
cl_object offset = ecl_make_fixnum(8); |
| 137 |
unsigned char c; |
| 138 |
for (idx=0; idx<ndx; idx++) { |
| 139 |
c = buf[ndx-idx-1]; |
| 140 |
if (output) { |
| 141 |
output = cl_logior(2, ecl_make_fixnum(c), cl_ash(output, offset)); |
| 142 |
} else { |
| 143 |
output = (strm->stream.flags & ECL_STREAM_SIGNED_BYTES) |
| 144 |
? ecl_make_fixnum((signed char)c) |
| 145 |
: ecl_make_fixnum((unsigned char)c); |
| 146 |
} |
| 147 |
} |
| 148 |
return output; |
| 149 |
} |
| 150 |
|
| 151 |
void |
| 152 |
ecl_binary_le_encoder(cl_object strm, unsigned char *buf, cl_object byte) |
| 153 |
{ |
| 154 |
cl_index idx, ndx = strm->stream.byte_size/8; |
| 155 |
cl_object offset = ecl_make_fixnum(-8); |
| 156 |
cl_object mask = ecl_make_fixnum(0xFF); |
| 157 |
for (idx=0; idx<ndx; idx++) { |
| 158 |
cl_object b = cl_logand(2, byte, mask); |
| 159 |
buf[idx] = (unsigned char)ecl_fixnum(b); |
| 160 |
byte = cl_ash(byte, offset); |
| 161 |
} |
| 162 |
} |