diff options
| author | jakob <jakob@memeware.net> | 2017-02-19 10:57:47 -0500 |
|---|---|---|
| committer | jakob <jakob@memeware.net> | 2017-02-19 10:57:47 -0500 |
| commit | 32fb7b11d83138cc288d62c1f1674d95cc94e8de (patch) | |
| tree | 290c5a27b58e24bb82bd46decba0aa5e1282e003 /src/io.c | |
| parent | 1fc6cc323dbaa5387d84630615c0d4cab0c1c5ba (diff) | |
Implemented table linked list, decompression and encryption. Tests have been improved.
Diffstat (limited to 'src/io.c')
| -rw-r--r-- | src/io.c | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -62,10 +62,37 @@ void stream_write(struct stream *s, void *src, size_t n) { ptrdiff_t dist = (uintptr_t) s->_cur - (uintptr_t) s->_start; switch(s->_loc) { case HEAP: - s->_start = realloc(s->_start, s->len * 2); + s->len *= 2; + s->_start = realloc(s->_start, s->len); s->_cur = s->_start + dist; } } memcpy(s->_cur, src, n); s->_cur += n; } + + +/* Applies an initial and primary key to the given stream, effectively + encrypting or decrypting it. */ +void stream_xor(struct stream *s, uint8_t initial, uint8_t primary) { + *s->_start ^= initial; + for (char *p = s->_start; p < s->_start + s->len; *p++ ^= primary); +} + + +/* Obtains the current value of the stream's position indicator. */ +size_t stream_tell(struct stream *s) { + return s->_cur - s->_start; +} + + +/* Sets the stream's position indicator to the given `pos`. */ +void stream_seek(struct stream *s, size_t pos) { + s->_cur = s->_start + pos; +} + + +/* Sets the stream's position indicator to the beginning. */ +void stream_rewind(struct stream *s) { + s->_cur = s->_start; +} |