| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <assert.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "scare.h" |
| 31 |
#include "sxprotos.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
typedef struct |
| 40 |
{ |
| 41 |
sc_byte *data; |
| 42 |
sc_int length; |
| 43 |
sc_bool is_open; |
| 44 |
sc_bool is_writable; |
| 45 |
} sx_scr_stream_t; |
| 46 |
static sx_scr_stream_t scr_serialization_stream = {NULL, 0, FALSE, FALSE}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
void * |
| 61 |
file_open_file_callback (sc_bool is_save) |
| 62 |
{ |
| 63 |
sx_scr_stream_t *const stream = &scr_serialization_stream; |
| 64 |
|
| 65 |
|
| 66 |
if (stream->is_open) |
| 67 |
{ |
| 68 |
scr_test_failed ("File open error: %s", |
| 69 |
"stream is in use (script limitation)"); |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
else if (is_save && stream->data) |
| 73 |
{ |
| 74 |
scr_test_failed ("File open error: %s", |
| 75 |
"stream has not been read (script limitation)"); |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if (is_save) |
| 84 |
{ |
| 85 |
stream->data = NULL; |
| 86 |
stream->length = 0; |
| 87 |
} |
| 88 |
else if (!stream->data) |
| 89 |
return NULL; |
| 90 |
|
| 91 |
stream->is_open = TRUE; |
| 92 |
stream->is_writable = is_save; |
| 93 |
return stream; |
| 94 |
} |
| 95 |
|
| 96 |
sc_int |
| 97 |
file_read_file_callback (void *opaque, sc_byte *buffer, sc_int length) |
| 98 |
{ |
| 99 |
sx_scr_stream_t *const stream = opaque; |
| 100 |
sc_int bytes; |
| 101 |
assert (opaque && buffer && length > 0); |
| 102 |
|
| 103 |
|
| 104 |
if (stream != &scr_serialization_stream) |
| 105 |
{ |
| 106 |
scr_test_failed ("File read error: %s", "stream is invalid"); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
else if (!stream->is_open) |
| 110 |
{ |
| 111 |
scr_test_failed ("File read error: %s", "stream is not open"); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
else if (stream->is_writable) |
| 115 |
{ |
| 116 |
scr_test_failed ("File read error: %s", "stream is not open for read"); |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
bytes = (stream->length < length) ? stream->length : length; |
| 122 |
memcpy (buffer, stream->data, bytes); |
| 123 |
memmove (stream->data, stream->data + bytes, stream->length - bytes); |
| 124 |
stream->length -= bytes; |
| 125 |
return bytes; |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
file_write_file_callback (void *opaque, const sc_byte *buffer, sc_int length) |
| 130 |
{ |
| 131 |
sx_scr_stream_t *const stream = opaque; |
| 132 |
assert (opaque && buffer && length > 0); |
| 133 |
|
| 134 |
|
| 135 |
if (stream != &scr_serialization_stream) |
| 136 |
{ |
| 137 |
scr_test_failed ("File write error: %s", "stream is invalid"); |
| 138 |
return; |
| 139 |
} |
| 140 |
else if (!stream->is_open) |
| 141 |
{ |
| 142 |
scr_test_failed ("File write error: %s", "stream is not open"); |
| 143 |
return; |
| 144 |
} |
| 145 |
else if (!stream->is_writable) |
| 146 |
{ |
| 147 |
scr_test_failed ("File write error: %s", "stream is not open for write"); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
stream->data = sx_realloc (stream->data, stream->length + length); |
| 153 |
memcpy (stream->data + stream->length, buffer, length); |
| 154 |
stream->length += length; |
| 155 |
} |
| 156 |
|
| 157 |
void |
| 158 |
file_close_file_callback (void *opaque) |
| 159 |
{ |
| 160 |
sx_scr_stream_t *const stream = opaque; |
| 161 |
assert (opaque); |
| 162 |
|
| 163 |
|
| 164 |
if (stream != &scr_serialization_stream) |
| 165 |
{ |
| 166 |
scr_test_failed ("File close error: %s", "stream is invalid"); |
| 167 |
return; |
| 168 |
} |
| 169 |
else if (!stream->is_open) |
| 170 |
{ |
| 171 |
scr_test_failed ("File close error: %s", "stream is not open"); |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (!stream->is_writable) |
| 180 |
{ |
| 181 |
sx_free (stream->data); |
| 182 |
stream->data = NULL; |
| 183 |
stream->length = 0; |
| 184 |
} |
| 185 |
stream->is_writable = FALSE; |
| 186 |
stream->is_open = FALSE; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
void |
| 196 |
file_cleanup (void) |
| 197 |
{ |
| 198 |
sx_scr_stream_t *const stream = &scr_serialization_stream; |
| 199 |
|
| 200 |
sx_free (stream->data); |
| 201 |
stream->data = NULL; |
| 202 |
stream->length = 0; |
| 203 |
stream->is_writable = FALSE; |
| 204 |
stream->is_open = FALSE; |
| 205 |
} |