| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use ascii; |
| 5 |
use bytes; |
| 6 |
use errors; |
| 7 |
use fmt; |
| 8 |
use io; |
| 9 |
use memio; |
| 10 |
use os; |
| 11 |
use strconv; |
| 12 |
use strings; |
| 13 |
|
| 14 |
export type encoder = struct { |
| 15 |
stream: io::stream, |
| 16 |
out: io::handle, |
| 17 |
err: (void | io::error), |
| 18 |
}; |
| 19 |
|
| 20 |
const encoder_vtable: io::vtable = io::vtable { |
| 21 |
writer = &encode_writer, |
| 22 |
... |
| 23 |
}; |
| 24 |
|
| 25 |
// Creates a stream that encodes writes as lowercase hexadecimal before writing |
| 26 |
// them to a secondary stream. Closing this stream will not close the underlying |
| 27 |
// stream. |
| 28 |
export fn newencoder(out: io::handle) encoder = { |
| 29 |
return encoder { |
| 30 |
stream = &encoder_vtable, |
| 31 |
out = out, |
| 32 |
err = void, |
| 33 |
}; |
| 34 |
}; |
| 35 |
|
| 36 |
fn encode_writer(s: *io::stream, in: const []u8) (size | io::error) = { |
| 37 |
const s = s: *encoder; |
| 38 |
match(s.err) { |
| 39 |
case let err: io::error => |
| 40 |
return err; |
| 41 |
case void => void; |
| 42 |
}; |
| 43 |
let z = 0z; |
| 44 |
for (let i = 0z; i < len(in); i += 1) { |
| 45 |
const r = strconv::u8tos(in[i], strconv::base::HEX_LOWER); |
| 46 |
if (len(r) == 1) { |
| 47 |
match(fmt::fprint(s.out, "0")) { |
| 48 |
case let b: size => |
| 49 |
z += b; |
| 50 |
case let err: io::error => |
| 51 |
s.err = err; |
| 52 |
return err; |
| 53 |
}; |
| 54 |
}; |
| 55 |
match(fmt::fprint(s.out, r)) { |
| 56 |
case let b: size => |
| 57 |
z += b; |
| 58 |
case let err: io::error => |
| 59 |
s.err = err; |
| 60 |
return err; |
| 61 |
}; |
| 62 |
}; |
| 63 |
return z; |
| 64 |
}; |
| 65 |
|
| 66 |
// Encodes a byte slice as a hexadecimal string and returns it. The caller must |
| 67 |
// free the return value. |
| 68 |
export fn encodestr(in: []u8) (str | nomem) = { |
| 69 |
const out = memio::dynamic(); |
| 70 |
const enc = newencoder(&out); |
| 71 |
match (io::writeall(&enc, in)) { |
| 72 |
case size => void; |
| 73 |
case nomem => |
| 74 |
io::close(&out)!; |
| 75 |
return nomem; |
| 76 |
case => |
| 77 |
abort(); // unreachable |
| 78 |
}; |
| 79 |
return memio::string(&out)!; |
| 80 |
}; |
| 81 |
|
| 82 |
@test fn encodestr() void = { |
| 83 |
let in: [_]u8 = [0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D]; |
| 84 |
let s = encodestr(in)!; |
| 85 |
defer free(s); |
| 86 |
assert(s == "cafebabedeadf00d"); |
| 87 |
}; |
| 88 |
|
| 89 |
// Encodes a byte slice as a hexadecimal string and writes it to an |
| 90 |
// [[io::handle]]. |
| 91 |
export fn encode(out: io::handle, in: []u8) (size | io::error) = { |
| 92 |
const enc = newencoder(out); |
| 93 |
return io::writeall(&enc, in); |
| 94 |
}; |
| 95 |
|
| 96 |
@test fn encode() void = { |
| 97 |
const in: [_]u8 = [0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D]; |
| 98 |
|
| 99 |
let out = memio::dynamic(); |
| 100 |
defer io::close(&out)!; |
| 101 |
|
| 102 |
encode(&out, in)!; |
| 103 |
assert(memio::string(&out)! == "cafebabedeadf00d"); |
| 104 |
}; |
| 105 |
|
| 106 |
export type decoder = struct { |
| 107 |
stream: io::stream, |
| 108 |
in: io::handle, |
| 109 |
state: (void | io::EOF | io::error), |
| 110 |
}; |
| 111 |
|
| 112 |
const decoder_vtable: io::vtable = io::vtable { |
| 113 |
reader = &decode_reader, |
| 114 |
... |
| 115 |
}; |
| 116 |
|
| 117 |
// Creates a stream that reads and decodes hexadecimal data from a secondary |
| 118 |
// stream. This stream does not need to be closed, and closing it will not |
| 119 |
// close the underlying stream. |
| 120 |
export fn newdecoder(in: io::handle) decoder = { |
| 121 |
return decoder { |
| 122 |
stream = &decoder_vtable, |
| 123 |
in = in, |
| 124 |
state = void, |
| 125 |
... |
| 126 |
}; |
| 127 |
}; |
| 128 |
|
| 129 |
fn decode_reader(s: *io::stream, out: []u8) (size | io::EOF | io::error) = { |
| 130 |
const s = s: *decoder; |
| 131 |
match(s.state) { |
| 132 |
case let err: (io::EOF | io::error) => |
| 133 |
return err; |
| 134 |
case void => void; |
| 135 |
}; |
| 136 |
static let buf: [os::BUFSZ]u8 = [0...]; |
| 137 |
let n = len(out) * 2; |
| 138 |
if (n > os::BUFSZ) { |
| 139 |
n = os::BUFSZ; |
| 140 |
}; |
| 141 |
let nr = 0z; |
| 142 |
for (nr < n) { |
| 143 |
match(io::read(s.in, buf[nr..n])) { |
| 144 |
case let n: size => |
| 145 |
nr += n; |
| 146 |
case io::EOF => |
| 147 |
s.state = io::EOF; |
| 148 |
break; |
| 149 |
case let err: io::error => |
| 150 |
s.state = err; |
| 151 |
return err; |
| 152 |
}; |
| 153 |
}; |
| 154 |
if (nr % 2 != 0) { |
| 155 |
s.state = errors::invalid; |
| 156 |
return errors::invalid; |
| 157 |
}; |
| 158 |
const l = nr / 2; |
| 159 |
for (let i = 0z; i < l; i += 1) { |
| 160 |
const oct = strings::fromutf8_unsafe(buf[i * 2..i * 2 + 2]); |
| 161 |
const u = match (strconv::stou8(oct, 16)) { |
| 162 |
case (strconv::invalid | strconv::overflow) => |
| 163 |
s.state = errors::invalid; |
| 164 |
return errors::invalid; |
| 165 |
case let u: u8 => |
| 166 |
yield u; |
| 167 |
}; |
| 168 |
out[i] = u; |
| 169 |
}; |
| 170 |
return l; |
| 171 |
}; |
| 172 |
|
| 173 |
// Decodes a string of hexadecimal bytes into a byte slice. The caller must free |
| 174 |
// the return value. |
| 175 |
export fn decodestr(s: str) ([]u8 | errors::invalid | nomem) = { |
| 176 |
let s = strings::toutf8(s); |
| 177 |
const in = memio::fixed(s); |
| 178 |
const decoder = newdecoder(&in); |
| 179 |
const out = memio::dynamic(); |
| 180 |
match(io::copy(&out, &decoder)) { |
| 181 |
case size => |
| 182 |
return memio::buffer(&out); |
| 183 |
case errors::invalid => |
| 184 |
io::close(&out)!; |
| 185 |
return errors::invalid; |
| 186 |
case nomem => |
| 187 |
io::close(&out)!; |
| 188 |
return nomem; |
| 189 |
case => |
| 190 |
abort(); // unreachable |
| 191 |
}; |
| 192 |
}; |
| 193 |
|
| 194 |
@test fn decode() void = { |
| 195 |
let s = decodestr("cafebabedeadf00d") as []u8; |
| 196 |
defer free(s); |
| 197 |
assert(bytes::equal(s, [0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D])); |
| 198 |
|
| 199 |
assert(decodestr("this is not hex") is errors::invalid); |
| 200 |
}; |
| 201 |
|
| 202 |
// Outputs a dump of hex data alongside the offset and an ASCII representation |
| 203 |
// (if applicable). |
| 204 |
// |
| 205 |
// Example output: |
| 206 |
// |
| 207 |
// 00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| |
| 208 |
// 00000010 03 00 3e 00 01 00 00 00 80 70 01 00 00 00 00 00 |..>......p......| |
| 209 |
// |
| 210 |
// If the addr parameter is provided, the address column will start from this |
| 211 |
// address (but the data slice will still be printed from index 0). |
| 212 |
export fn dump(out: io::handle, data: []u8, addr: u64 = 0) (void | io::error) = { |
| 213 |
let datalen = len(data): u64; |
| 214 |
|
| 215 |
for (let off = 0u64; off < datalen; off += 16) { |
| 216 |
fmt::fprintf(out, "{:.8x} ", addr + off)?; |
| 217 |
|
| 218 |
let toff = 0z; |
| 219 |
for (let i = 0u64; i < 16 && off + i < datalen; i += 1) { |
| 220 |
let val = data[off + i]; |
| 221 |
toff += fmt::fprintf(out, "{}{:.2x} ", |
| 222 |
if (i == 8) " " else "", val)?; |
| 223 |
}; |
| 224 |
|
| 225 |
// Align ASCII representation, max width of hex part (48) + |
| 226 |
// spacing around it |
| 227 |
for (toff < 50; toff += 1) { |
| 228 |
fmt::fprint(out, " ")?; |
| 229 |
}; |
| 230 |
|
| 231 |
fmt::fprint(out, "|")?; |
| 232 |
for (let i = 0u64; i < 16 && off + i < datalen; i += 1) { |
| 233 |
let r = data[off + i]: rune; |
| 234 |
|
| 235 |
fmt::fprint(out, if (ascii::isprint(r)) r else '.')?; |
| 236 |
}; |
| 237 |
fmt::fprint(out, "|\n")?; |
| 238 |
}; |
| 239 |
}; |
| 240 |
|
| 241 |
@test fn dump() void = { |
| 242 |
let in: [_]u8 = [ |
| 243 |
0x7F, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0xCA, 0xFE, |
| 244 |
0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D, 0xCE, 0xFE, 0xBA, 0xBE, |
| 245 |
0xDE, 0xAD, 0xF0, 0x0D |
| 246 |
]; |
| 247 |
|
| 248 |
let sink = memio::dynamic(); |
| 249 |
defer io::close(&sink)!; |
| 250 |
dump(&sink, in) as void; |
| 251 |
|
| 252 |
let s = memio::string(&sink)!; |
| 253 |
assert(s == |
| 254 |
"00000000 7f 45 4c 46 02 01 01 00 ca fe ba be de ad f0 0d |.ELF............|\n" |
| 255 |
"00000010 ce fe ba be de ad f0 0d |........|\n"); |
| 256 |
}; |