This commit is contained in:
Alberto Fustinoni 2017-06-20 13:43:20 +09:00
parent b065b0619c
commit f4808e1b74
7 changed files with 14 additions and 1119 deletions

View File

@ -1,7 +1,7 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (memalign.c).
* The following license statement only applies to this file (boolean.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
@ -20,45 +20,20 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#ifndef __LIBRETRO_SDK_BOOLEAN_H
#define __LIBRETRO_SDK_BOOLEAN_H
#include <memalign.h>
#ifndef __cplusplus
void *memalign_alloc(size_t boundary, size_t size)
{
void **place = NULL;
uintptr_t addr = 0;
void *ptr = (void*)malloc(boundary + size + sizeof(uintptr_t));
if (!ptr)
return NULL;
addr = ((uintptr_t)ptr + sizeof(uintptr_t) + boundary)
& ~(boundary - 1);
place = (void**)addr;
place[-1] = ptr;
return (void*)addr;
}
void memalign_free(void *ptr)
{
void **p = NULL;
if (!ptr)
return;
p = (void**)ptr;
free(p[-1]);
}
void *memalign_alloc_aligned(size_t size)
{
#if defined(__x86_64__) || defined(__LP64) || defined(__IA64__) || defined(_M_X64) || defined(_WIN64)
return memalign_alloc(64, size);
#elif defined(__i386__) || defined(__i486__) || defined(__i686__) || defined(GEKKO)
return memalign_alloc(32, size);
#if defined(_MSC_VER) && !defined(SN_TARGET_PS3)
/* Hack applied for MSVC when compiling in C89 mode as it isn't C99 compliant. */
#define bool unsigned char
#define true 1
#define false 0
#else
return memalign_alloc(32, size);
#include <stdbool.h>
#endif
#endif
#endif
}

View File

@ -1,268 +0,0 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (interface_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h>
#include <streams/memory_stream.h>
struct intfstream_internal
{
enum intfstream_type type;
struct
{
RFILE *fp;
} file;
struct
{
struct
{
uint8_t *data;
unsigned size;
} buf;
memstream_t *fp;
bool writable;
} memory;
};
bool intfstream_resize(intfstream_internal_t *intf, intfstream_info_t *info)
{
if (!intf || !info)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
break;
case INTFSTREAM_MEMORY:
intf->memory.buf.data = info->memory.buf.data;
intf->memory.buf.size = info->memory.buf.size;
memstream_set_buffer(intf->memory.buf.data,
intf->memory.buf.size);
break;
}
return true;
}
bool intfstream_open(intfstream_internal_t *intf, const char *path,
unsigned mode, ssize_t len)
{
if (!intf)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
intf->file.fp = filestream_open(path, mode, len);
if (!intf->file.fp)
return false;
break;
case INTFSTREAM_MEMORY:
intf->memory.fp = memstream_open(intf->memory.writable);
if (!intf->memory.fp)
return false;
break;
}
return true;
}
int intfstream_close(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_close(intf->file.fp);
case INTFSTREAM_MEMORY:
memstream_close(intf->memory.fp);
return 0;
}
return -1;
}
void *intfstream_init(intfstream_info_t *info)
{
intfstream_internal_t *intf = NULL;
if (!info)
goto error;
intf = (intfstream_internal_t*)calloc(1, sizeof(*intf));
if (!intf)
goto error;
intf->type = info->type;
switch (intf->type)
{
case INTFSTREAM_FILE:
break;
case INTFSTREAM_MEMORY:
intf->memory.writable = info->memory.writable;
if (!intfstream_resize(intf, info))
goto error;
break;
}
return intf;
error:
if (intf)
free(intf);
return NULL;
}
int intfstream_seek(intfstream_internal_t *intf, int offset, int whence)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return (int)filestream_seek(intf->file.fp, (int)offset, whence);
case INTFSTREAM_MEMORY:
return (int)memstream_seek(intf->memory.fp, offset, whence);
}
return -1;
}
ssize_t intfstream_read(intfstream_internal_t *intf, void *s, size_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_read(intf->file.fp, s, len);
case INTFSTREAM_MEMORY:
return memstream_read(intf->memory.fp, s, len);
}
return 0;
}
ssize_t intfstream_write(intfstream_internal_t *intf,
const void *s, size_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_write(intf->file.fp, s, len);
case INTFSTREAM_MEMORY:
return memstream_write(intf->memory.fp, s, len);
}
return 0;
}
char *intfstream_gets(intfstream_internal_t *intf,
char *buffer, size_t len)
{
if (!intf)
return NULL;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_gets(intf->file.fp, buffer, len);
case INTFSTREAM_MEMORY:
return memstream_gets(intf->memory.fp, buffer, len);
}
return NULL;
}
int intfstream_getc(intfstream_internal_t *intf)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_getc(intf->file.fp);
case INTFSTREAM_MEMORY:
return memstream_getc(intf->memory.fp);
}
return 0;
}
int intfstream_tell(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return (int)filestream_tell(intf->file.fp);
case INTFSTREAM_MEMORY:
return (int)memstream_pos(intf->memory.fp);
}
return -1;
}
void intfstream_rewind(intfstream_internal_t *intf)
{
switch (intf->type)
{
case INTFSTREAM_FILE:
filestream_rewind(intf->file.fp);
break;
case INTFSTREAM_MEMORY:
memstream_rewind(intf->memory.fp);
break;
}
}
void intfstream_putc(intfstream_internal_t *intf, int c)
{
if (!intf)
return;
switch (intf->type)
{
case INTFSTREAM_FILE:
filestream_putc(intf->file.fp, c);
break;
case INTFSTREAM_MEMORY:
memstream_putc(intf->memory.fp, c);
break;
}
}

View File

@ -1,190 +0,0 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (memory_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <streams/memory_stream.h>
static uint8_t* g_buffer = NULL;
static size_t g_size = 0;
static size_t last_file_size = 0;
struct memstream
{
uint8_t *buf;
size_t size;
size_t ptr;
size_t max_ptr;
unsigned writing;
};
static void memstream_update_pos(memstream_t *stream)
{
if (stream && stream->ptr > stream->max_ptr)
stream->max_ptr = stream->ptr;
}
void memstream_set_buffer(uint8_t *buffer, size_t size)
{
g_buffer = buffer;
g_size = size;
}
size_t memstream_get_last_size(void)
{
return last_file_size;
}
static void memstream_init(memstream_t *stream,
uint8_t *buffer, size_t max_size, unsigned writing)
{
if (!stream)
return;
stream->buf = buffer;
stream->size = max_size;
stream->ptr = 0;
stream->max_ptr = 0;
stream->writing = writing;
}
memstream_t *memstream_open(unsigned writing)
{
memstream_t *stream;
if (!g_buffer || !g_size)
return NULL;
stream = (memstream_t*)calloc(1, sizeof(*stream));
memstream_init(stream, g_buffer, g_size, writing);
g_buffer = NULL;
g_size = 0;
return stream;
}
void memstream_close(memstream_t *stream)
{
if (!stream)
return;
last_file_size = stream->writing ? stream->max_ptr : stream->size;
free(stream);
}
size_t memstream_read(memstream_t *stream, void *data, size_t bytes)
{
size_t avail = 0;
if (!stream)
return 0;
avail = stream->size - stream->ptr;
if (bytes > avail)
bytes = avail;
memcpy(data, stream->buf + stream->ptr, bytes);
stream->ptr += bytes;
memstream_update_pos(stream);
return bytes;
}
size_t memstream_write(memstream_t *stream, const void *data, size_t bytes)
{
size_t avail = 0;
if (!stream)
return 0;
avail = stream->size - stream->ptr;
if (bytes > avail)
bytes = avail;
memcpy(stream->buf + stream->ptr, data, bytes);
stream->ptr += bytes;
memstream_update_pos(stream);
return bytes;
}
int memstream_seek(memstream_t *stream, int offset, int whence)
{
size_t ptr;
switch (whence)
{
case SEEK_SET:
ptr = offset;
break;
case SEEK_CUR:
ptr = stream->ptr + offset;
break;
case SEEK_END:
ptr = (stream->writing ? stream->max_ptr : stream->size) + offset;
break;
default:
return -1;
}
if (ptr <= stream->size)
{
stream->ptr = ptr;
return 0;
}
return -1;
}
void memstream_rewind(memstream_t *stream)
{
memstream_seek(stream, 0L, SEEK_SET);
}
size_t memstream_pos(memstream_t *stream)
{
return stream->ptr;
}
char *memstream_gets(memstream_t *stream, char *buffer, size_t len)
{
return NULL;
}
int memstream_getc(memstream_t *stream)
{
int ret = 0;
if (stream->ptr >= stream->size)
return EOF;
ret = stream->buf[stream->ptr++];
memstream_update_pos(stream);
return ret;
}
void memstream_putc(memstream_t *stream, int c)
{
if (stream->ptr < stream->size)
stream->buf[stream->ptr++] = c;
memstream_update_pos(stream);
}

View File

@ -1,151 +0,0 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (stdin_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#ifdef _WIN32
#ifndef _XBOX
#include <windows.h>
#endif
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <boolean.h>
#include <streams/stdin_stream.h>
#if (defined(_WIN32) && defined(_XBOX)) || defined(__CELLOS_LV2__)
size_t read_stdin(char *buf, size_t size)
{
/* Not implemented. */
return 0;
}
#elif defined(_WIN32)
size_t read_stdin(char *buf, size_t size)
{
DWORD i;
DWORD has_read = 0;
DWORD avail = 0;
bool echo = false;
HANDLE hnd = GetStdHandle(STD_INPUT_HANDLE);
if (hnd == INVALID_HANDLE_VALUE)
return 0;
/* Check first if we're a pipe
* (not console). */
/* If not a pipe, check if we're running in a console. */
if (!PeekNamedPipe(hnd, NULL, 0, NULL, &avail, NULL))
{
INPUT_RECORD recs[256];
bool has_key = false;
DWORD mode = 0;
DWORD has_read = 0;
if (!GetConsoleMode(hnd, &mode))
return 0;
if ((mode & (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT))
&& !SetConsoleMode(hnd,
mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)))
return 0;
/* Win32, Y U NO SANE NONBLOCK READ!? */
if (!PeekConsoleInput(hnd, recs,
sizeof(recs) / sizeof(recs[0]), &has_read))
return 0;
for (i = 0; i < has_read; i++)
{
/* Very crude, but should get the job done. */
if (recs[i].EventType == KEY_EVENT &&
recs[i].Event.KeyEvent.bKeyDown &&
(isgraph(recs[i].Event.KeyEvent.wVirtualKeyCode) ||
recs[i].Event.KeyEvent.wVirtualKeyCode == VK_RETURN))
{
has_key = true;
echo = true;
avail = size;
break;
}
}
if (!has_key)
{
FlushConsoleInputBuffer(hnd);
return 0;
}
}
if (!avail)
return 0;
if (avail > size)
avail = size;
if (!ReadFile(hnd, buf, avail, &has_read, NULL))
return 0;
for (i = 0; i < has_read; i++)
if (buf[i] == '\r')
buf[i] = '\n';
/* Console won't echo for us while in non-line mode,
* so do it manually ... */
if (echo)
{
HANDLE hnd_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (hnd_out != INVALID_HANDLE_VALUE)
{
DWORD has_written;
WriteConsole(hnd_out, buf, has_read, &has_written, NULL);
}
}
return has_read;
}
#else
size_t read_stdin(char *buf, size_t size)
{
size_t has_read = 0;
while (size)
{
ssize_t ret = read(STDIN_FILENO, buf, size);
if (ret <= 0)
break;
buf += ret;
has_read += ret;
size -= ret;
}
return has_read;
}
#endif

View File

@ -1,95 +0,0 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <streams/trans_stream.h>
/**
* trans_stream_trans_full:
* @data : (optional) existing stream data, or a target
* for the new stream data to be saved
* @in : input data
* @in_size : input size
* @out : output data
* @out_size : output size
* @error : (optional) output for error code
*
* Perform a full transcoding from a source to a destination.
*/
bool trans_stream_trans_full(
struct trans_stream_backend *backend, void **data,
const uint8_t *in, uint32_t in_size,
uint8_t *out, uint32_t out_size,
enum trans_stream_error *error)
{
void *rdata;
bool ret;
uint32_t rd, wn;
if (data && *data)
{
rdata = *data;
}
else
{
rdata = backend->stream_new();
if (!rdata)
{
if (error)
*error = TRANS_STREAM_ERROR_ALLOCATION_FAILURE;
return false;
}
}
backend->set_in(rdata, in, in_size);
backend->set_out(rdata, out, out_size);
ret = backend->trans(rdata, true, &rd, &wn, error);
if (data)
*data = rdata;
else
backend->stream_free(rdata);
return ret;
}
const struct trans_stream_backend* trans_stream_get_zlib_deflate_backend(void)
{
#if HAVE_ZLIB
return &zlib_deflate_backend;
#else
return NULL;
#endif
}
const struct trans_stream_backend* trans_stream_get_zlib_inflate_backend(void)
{
#if HAVE_ZLIB
return &zlib_inflate_backend;
#else
return NULL;
#endif
}
const struct trans_stream_backend* trans_stream_get_pipe_backend(void)
{
return &pipe_backend;
}

View File

@ -1,95 +0,0 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream_pipe.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <streams/trans_stream.h>
struct pipe_trans_stream
{
const uint8_t *in;
uint8_t *out;
uint32_t in_size, out_size;
};
static void *pipe_stream_new(void)
{
return (struct pipe_trans_stream*)calloc(1, sizeof(struct pipe_trans_stream));
}
static void pipe_stream_free(void *data)
{
free(data);
}
static void pipe_set_in(void *data, const uint8_t *in, uint32_t in_size)
{
struct pipe_trans_stream *p = (struct pipe_trans_stream *) data;
p->in = in;
p->in_size = in_size;
}
static void pipe_set_out(void *data, uint8_t *out, uint32_t out_size)
{
struct pipe_trans_stream *p = (struct pipe_trans_stream *) data;
p->out = out;
p->out_size = out_size;
}
static bool pipe_trans(
void *data, bool flush,
uint32_t *rd, uint32_t *wn,
enum trans_stream_error *error)
{
struct pipe_trans_stream *p = (struct pipe_trans_stream *) data;
if (p->out_size < p->in_size)
{
memcpy(p->out, p->in, p->out_size);
*rd = *wn = p->out_size;
p->in += p->out_size;
p->out += p->out_size;
*error = TRANS_STREAM_ERROR_BUFFER_FULL;
return false;
}
else
{
memcpy(p->out, p->in, p->in_size);
*rd = *wn = p->in_size;
p->in += p->in_size;
p->out += p->in_size;
*error = TRANS_STREAM_ERROR_NONE;
return true;
}
}
const struct trans_stream_backend pipe_backend = {
"pipe",
&pipe_backend,
pipe_stream_new,
pipe_stream_free,
NULL,
pipe_set_in,
pipe_set_out,
pipe_trans
};

View File

@ -1,281 +0,0 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream_zlib.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <compat/zlib.h>
#include <string/stdstring.h>
#include <streams/trans_stream.h>
struct zlib_trans_stream
{
z_stream z;
int ex; /* window_bits or level */
bool inited;
};
static void *zlib_deflate_stream_new(void)
{
struct zlib_trans_stream *ret = (struct zlib_trans_stream*)calloc(1, sizeof(struct zlib_trans_stream));
if (ret)
ret->ex = 9;
return (void *) ret;
}
static void *zlib_inflate_stream_new(void)
{
struct zlib_trans_stream *ret = (struct zlib_trans_stream*)calloc(1, sizeof(struct zlib_trans_stream));
if (ret)
ret->ex = MAX_WBITS;
return (void *) ret;
}
static void zlib_deflate_stream_free(void *data)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (z->inited)
deflateEnd(&z->z);
free(z);
}
static void zlib_inflate_stream_free(void *data)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (!z)
return;
if (z->inited)
inflateEnd(&z->z);
free(z);
}
static bool zlib_deflate_define(void *data, const char *prop, uint32_t val)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (string_is_equal_fast(prop, "level", 5))
{
z->ex = (int) val;
return true;
}
return false;
}
static bool zlib_inflate_define(void *data, const char *prop, uint32_t val)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (string_is_equal_fast(prop, "window_bits", 11))
{
z->ex = (int) val;
return true;
}
return false;
}
static void zlib_deflate_set_in(void *data, const uint8_t *in, uint32_t in_size)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (!z)
return;
z->z.next_in = (uint8_t *) in;
z->z.avail_in = in_size;
if (!z->inited)
{
deflateInit(&z->z, z->ex);
z->inited = true;
}
}
static void zlib_inflate_set_in(void *data, const uint8_t *in, uint32_t in_size)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (!z)
return;
z->z.next_in = (uint8_t *) in;
z->z.avail_in = in_size;
if (!z->inited)
{
inflateInit2(&z->z, z->ex);
z->inited = true;
}
}
static void zlib_set_out(void *data, uint8_t *out, uint32_t out_size)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (!z)
return;
z->z.next_out = out;
z->z.avail_out = out_size;
}
static bool zlib_deflate_trans(
void *data, bool flush,
uint32_t *rd, uint32_t *wn,
enum trans_stream_error *error)
{
int zret;
bool ret;
uint32_t pre_avail_in, pre_avail_out;
struct zlib_trans_stream *zt = (struct zlib_trans_stream *) data;
z_stream *z = &zt->z;
if (!zt->inited)
{
deflateInit(z, zt->ex);
zt->inited = true;
}
pre_avail_in = z->avail_in;
pre_avail_out = z->avail_out;
zret = deflate(z, flush ? Z_FINISH : Z_NO_FLUSH);
if (zret == Z_OK)
{
if (error)
*error = TRANS_STREAM_ERROR_AGAIN;
}
else if (zret == Z_STREAM_END)
{
if (error)
*error = TRANS_STREAM_ERROR_NONE;
}
else
{
if (error)
*error = TRANS_STREAM_ERROR_OTHER;
return false;
}
ret = true;
if (z->avail_out == 0)
{
/* Filled buffer, maybe an error */
if (z->avail_in != 0)
{
ret = false;
if (error)
*error = TRANS_STREAM_ERROR_BUFFER_FULL;
}
}
*rd = pre_avail_in - z->avail_in;
*wn = pre_avail_out - z->avail_out;
if (flush && zret == Z_STREAM_END)
{
deflateEnd(z);
zt->inited = false;
}
return ret;
}
static bool zlib_inflate_trans(
void *data, bool flush,
uint32_t *rd, uint32_t *wn,
enum trans_stream_error *error)
{
int zret;
bool ret;
uint32_t pre_avail_in, pre_avail_out;
struct zlib_trans_stream *zt = (struct zlib_trans_stream *) data;
z_stream *z = &zt->z;
if (!zt->inited)
{
inflateInit2(z, zt->ex);
zt->inited = true;
}
pre_avail_in = z->avail_in;
pre_avail_out = z->avail_out;
zret = inflate(z, flush ? Z_FINISH : Z_NO_FLUSH);
if (zret == Z_OK)
{
if (error)
*error = TRANS_STREAM_ERROR_AGAIN;
}
else if (zret == Z_STREAM_END)
{
if (error)
*error = TRANS_STREAM_ERROR_NONE;
}
else
{
if (error)
*error = TRANS_STREAM_ERROR_OTHER;
return false;
}
ret = true;
if (z->avail_out == 0)
{
/* Filled buffer, maybe an error */
if (z->avail_in != 0)
{
ret = false;
if (error)
*error = TRANS_STREAM_ERROR_BUFFER_FULL;
}
}
*rd = pre_avail_in - z->avail_in;
*wn = pre_avail_out - z->avail_out;
if (flush && zret == Z_STREAM_END)
{
inflateEnd(z);
zt->inited = false;
}
return ret;
}
const struct trans_stream_backend zlib_deflate_backend = {
"zlib_deflate",
&zlib_inflate_backend,
zlib_deflate_stream_new,
zlib_deflate_stream_free,
zlib_deflate_define,
zlib_deflate_set_in,
zlib_set_out,
zlib_deflate_trans
};
const struct trans_stream_backend zlib_inflate_backend = {
"zlib_inflate",
&zlib_deflate_backend,
zlib_inflate_stream_new,
zlib_inflate_stream_free,
zlib_inflate_define,
zlib_inflate_set_in,
zlib_set_out,
zlib_inflate_trans
};