Compile fixes.
[SquirrelJME.git] / nanocoat / lib / base / streamInflate.c
blobb35db9d0b57fd01e0b3d6e31c539fca3e8b62250
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "sjme/stream.h"
13 #include "sjme/alloc.h"
14 #include "sjme/util.h"
15 #include "sjme/inflate.h"
17 /**
18 * Inflation initializer state.
20 * @since 2024/08/30
22 typedef struct sjme_stream_inflateInit
24 /** First handle. */
25 sjme_pointer handle;
26 } sjme_stream_inflateInit;
28 static sjme_errorCode sjme_stream_inputInflateClose(
29 sjme_attrInNotNull sjme_stream_input stream,
30 sjme_attrInNotNull sjme_stream_implState* inImplState)
32 sjme_errorCode error;
33 sjme_inflate* state;
35 if (stream == NULL || inImplState == NULL)
36 return SJME_ERROR_NULL_ARGUMENTS;
38 /* Do nothing if already closed. */
39 state = inImplState->handle;
40 if (state == NULL)
41 return SJME_ERROR_NONE;
43 /* Destroy it. */
44 if (sjme_error_is(error = sjme_inflate_destroy(state)))
45 return sjme_error_default(error);
47 /* The state is now not valid. */
48 inImplState->handle = NULL;
50 /* Success! */
51 return SJME_ERROR_NONE;
54 static sjme_errorCode sjme_stream_inputInflateInit(
55 sjme_attrInNotNull sjme_stream_input stream,
56 sjme_attrInNotNull sjme_stream_implState* inImplState,
57 sjme_attrInNullable sjme_pointer data)
59 sjme_stream_inflateInit* init;
61 init = data;
62 if (stream == NULL || inImplState == NULL || init == NULL)
63 return SJME_ERROR_NULL_ARGUMENTS;
65 /* Set data. */
66 inImplState->handle = init->handle;
68 /* Success! */
69 return SJME_ERROR_NONE;
72 static sjme_errorCode sjme_stream_inputInflateRead(
73 sjme_attrInNotNull sjme_stream_input stream,
74 sjme_attrInNotNull sjme_stream_implState* inImplState,
75 sjme_attrOutNotNull sjme_attrOutNegativeOnePositive sjme_jint* readCount,
76 sjme_attrOutNotNullBuf(length) sjme_pointer dest,
77 sjme_attrInPositive sjme_jint length)
79 sjme_errorCode error;
80 sjme_inflate* inState;
81 sjme_jint count;
83 if (stream == NULL || inImplState == NULL || readCount == NULL ||
84 dest == NULL)
85 return SJME_ERROR_NULL_ARGUMENTS;
87 if (length < 0)
88 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
90 /* Recover state, fail if closed. */
91 inState = inImplState->handle;
92 if (inState == NULL)
93 return SJME_ERROR_ILLEGAL_STATE;
95 /* Request inflated data. */
96 count = INT32_MAX;
97 if (sjme_error_is(error = sjme_inflate_inflate(inState,
98 &count, dest, length)) || count == INT32_MAX)
99 return sjme_error_default(error);
101 /* Success! */
102 *readCount = count;
103 return SJME_ERROR_NONE;
106 /** Input deflate functions. */
107 static const sjme_stream_inputFunctions sjme_stream_inputInflateFunctions =
109 .close = sjme_stream_inputInflateClose,
110 .init = sjme_stream_inputInflateInit,
111 .read = sjme_stream_inputInflateRead,
114 sjme_errorCode sjme_stream_inputOpenInflate(
115 sjme_attrInNotNull sjme_alloc_pool* inPool,
116 sjme_attrOutNotNull sjme_stream_input* outStream,
117 sjme_attrInNotNull sjme_stream_input inCompressed)
119 sjme_errorCode error;
120 sjme_stream_input result;
121 sjme_stream_inflateInit init;
122 sjme_inflate* state;
124 if (inPool == NULL || outStream == NULL || inCompressed == NULL)
125 return SJME_ERROR_NULL_ARGUMENTS;
127 /* Setup decompression state data. */
128 state = NULL;
129 if (sjme_error_is(error = sjme_inflate_new(inPool,
130 &state, inCompressed)) || state == NULL)
131 goto fail_inflateNew;
133 /* Set initialization data. */
134 memset(&init, 0, sizeof(init));
135 init.handle = state;
137 /* Setup sub-stream. */
138 result = NULL;
139 if (sjme_error_is(error = sjme_stream_inputOpen(inPool,
140 &result, &sjme_stream_inputInflateFunctions,
141 &init, NULL)) || result == NULL)
142 goto fail_open;
144 /* Success! */
145 *outStream = result;
146 return SJME_ERROR_NONE;
148 fail_open:
149 if (result != NULL)
150 sjme_closeable_close(SJME_AS_CLOSEABLE(result));
152 fail_inflateNew:
153 if (state != NULL)
154 sjme_inflate_destroy(state);
156 return sjme_error_default(error);