2023-12-14 23:08:16 +00:00
|
|
|
#include "stream.h"
|
|
|
|
#include "lib.h"
|
|
|
|
|
2023-12-15 00:38:55 +00:00
|
|
|
#include <errno.h>
|
2023-12-14 23:08:16 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
static uint64_t longswap(uint64_t ll) {
|
|
|
|
if (htons(20) == 20)
|
|
|
|
return ll;
|
|
|
|
|
|
|
|
union { uint64_t ll; uint8_t c[8]; } out = {0};
|
|
|
|
union { uint64_t ll; uint8_t c[8]; } in = {ll};
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
out.c[7-i] = in.c[i];
|
|
|
|
|
|
|
|
return out.ll;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stream_t stream_open(const char *path, const char* mode) {
|
2023-12-14 23:08:16 +00:00
|
|
|
stream_t stream;
|
|
|
|
|
|
|
|
if (strcmp("-", path) == 0) {
|
2023-12-16 04:02:45 +00:00
|
|
|
if (*mode == 'r')
|
|
|
|
stream.__file = stdin;
|
|
|
|
else
|
|
|
|
stream.__file = stdout;
|
2023-12-14 23:08:16 +00:00
|
|
|
stream.__alloc = false;
|
2023-12-16 04:02:45 +00:00
|
|
|
return stream;
|
2023-12-14 23:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.__file = fopen(path, mode);
|
|
|
|
stream.__alloc = true;
|
|
|
|
|
|
|
|
if (stream.__file == NULL) {
|
2023-12-16 04:02:45 +00:00
|
|
|
perror_and_die("cannot open '%s'", path);
|
2023-12-14 23:08:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stream_close(stream_t *stream) {
|
|
|
|
if (stream->__alloc)
|
|
|
|
fclose(stream->__file);
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_read(const stream_t *stream, void *res, size_t amount) {
|
2023-12-14 23:08:16 +00:00
|
|
|
size_t read;
|
|
|
|
read = fread(res, 1, amount, stream->__file);
|
|
|
|
|
|
|
|
if (read == 0) {
|
2023-12-15 00:38:55 +00:00
|
|
|
if (feof(stream->__file) || errno == 0)
|
2023-12-14 23:08:16 +00:00
|
|
|
return false;
|
|
|
|
else
|
2023-12-16 04:02:45 +00:00
|
|
|
perror_and_die("cannot read from stream");
|
2023-12-14 23:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_read_i8(const stream_t *stream, int8_t *res) {
|
2023-12-14 23:08:16 +00:00
|
|
|
if (stream_read(stream, res, 1) == false)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_read_i16(const stream_t *stream, int16_t *res) {
|
2023-12-14 23:08:16 +00:00
|
|
|
if (stream_read(stream, res, 2) == false)
|
|
|
|
return false;
|
|
|
|
*res = ntohs(*res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_read_i32(const stream_t *stream, int32_t *res) {
|
2023-12-14 23:08:16 +00:00
|
|
|
if (stream_read(stream, res, 4) == false)
|
|
|
|
return false;
|
|
|
|
*res = ntohl(*res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_read_i64(const stream_t *stream, int64_t *res) {
|
|
|
|
if (stream_read(stream, res, 8) == false)
|
|
|
|
return false;
|
|
|
|
*res = longswap(*res);
|
|
|
|
return true;
|
|
|
|
}
|
2023-12-14 23:08:16 +00:00
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_read_u16(const stream_t *stream, uint16_t *res) {
|
|
|
|
if (stream_read(stream, res, 2) == false)
|
|
|
|
return false;
|
|
|
|
*res = ntohs(*res);
|
|
|
|
return true;
|
|
|
|
}
|
2023-12-14 23:08:16 +00:00
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_write(const stream_t *stream, const void *buf, size_t amount) {
|
|
|
|
size_t wrote;
|
|
|
|
wrote = fwrite(buf, 1, amount, stream->__file);
|
2023-12-14 23:08:16 +00:00
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
if (wrote == 0)
|
|
|
|
perror_and_die("cannot write to stream");
|
|
|
|
|
|
|
|
if (wrote < amount)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2023-12-14 23:08:16 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_write_i8(const stream_t *stream, int8_t b) {
|
|
|
|
if (stream_write(stream, &b, 1) == false)
|
2023-12-14 23:08:16 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_write_i16(const stream_t *stream, int16_t s) {
|
|
|
|
s = htons(s);
|
|
|
|
if (stream_write(stream, &s, 2) == false)
|
2023-12-14 23:08:16 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 04:02:45 +00:00
|
|
|
bool stream_write_i32(const stream_t *stream, int32_t i) {
|
|
|
|
i = htonl(i);
|
|
|
|
if (stream_write(stream, &i, 4) == false)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stream_write_i64(const stream_t *stream, int64_t l) {
|
|
|
|
l = longswap(l);
|
|
|
|
if (stream_write(stream, &l, 8) == false)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stream_write_u16(const stream_t *stream, uint16_t s) {
|
|
|
|
s = htons(s);
|
|
|
|
if (stream_write(stream, &s, 2) == false)
|
2023-12-14 23:08:16 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|