iodine 0.4.19 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of iodine might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/CHANGELOG.md +22 -0
- data/LIMITS.md +19 -9
- data/README.md +92 -77
- data/SPEC-PubSub-Draft.md +113 -0
- data/SPEC-Websocket-Draft.md +127 -143
- data/bin/http-hello +0 -1
- data/bin/raw-rbhttp +1 -1
- data/bin/raw_broadcast +8 -10
- data/bin/updated api +2 -2
- data/bin/ws-broadcast +2 -4
- data/bin/ws-echo +2 -2
- data/examples/config.ru +13 -13
- data/examples/echo.ru +5 -6
- data/examples/hello.ru +2 -3
- data/examples/info.md +316 -0
- data/examples/pubsub_engine.ru +81 -0
- data/examples/redis.ru +9 -9
- data/examples/shootout.ru +45 -11
- data/ext/iodine/defer.c +194 -297
- data/ext/iodine/defer.h +61 -53
- data/ext/iodine/evio.c +0 -260
- data/ext/iodine/evio.h +50 -22
- data/ext/iodine/evio_callbacks.c +26 -0
- data/ext/iodine/evio_epoll.c +251 -0
- data/ext/iodine/evio_kqueue.c +193 -0
- data/ext/iodine/extconf.rb +1 -1
- data/ext/iodine/facil.c +1420 -542
- data/ext/iodine/facil.h +151 -64
- data/ext/iodine/fio_ary.h +418 -0
- data/ext/iodine/{base64.c → fio_base64.c} +33 -24
- data/ext/iodine/{base64.h → fio_base64.h} +6 -7
- data/ext/iodine/{fio_cli_helper.c → fio_cli.c} +77 -58
- data/ext/iodine/{fio_cli_helper.h → fio_cli.h} +9 -4
- data/ext/iodine/fio_hashmap.h +759 -0
- data/ext/iodine/fio_json_parser.h +651 -0
- data/ext/iodine/fio_llist.h +257 -0
- data/ext/iodine/fio_mem.c +672 -0
- data/ext/iodine/fio_mem.h +140 -0
- data/ext/iodine/fio_random.c +248 -0
- data/ext/iodine/{random.h → fio_random.h} +11 -14
- data/ext/iodine/{sha1.c → fio_sha1.c} +28 -24
- data/ext/iodine/{sha1.h → fio_sha1.h} +38 -16
- data/ext/iodine/{sha2.c → fio_sha2.c} +66 -49
- data/ext/iodine/{sha2.h → fio_sha2.h} +57 -26
- data/ext/iodine/{fiobj_internal.c → fio_siphash.c} +9 -90
- data/ext/iodine/fio_siphash.h +18 -0
- data/ext/iodine/fio_tmpfile.h +38 -0
- data/ext/iodine/fiobj.h +24 -7
- data/ext/iodine/fiobj4sock.h +23 -0
- data/ext/iodine/fiobj_ary.c +143 -226
- data/ext/iodine/fiobj_ary.h +17 -16
- data/ext/iodine/fiobj_data.c +1160 -0
- data/ext/iodine/fiobj_data.h +164 -0
- data/ext/iodine/fiobj_hash.c +298 -406
- data/ext/iodine/fiobj_hash.h +101 -54
- data/ext/iodine/fiobj_json.c +478 -601
- data/ext/iodine/fiobj_json.h +34 -9
- data/ext/iodine/fiobj_numbers.c +383 -51
- data/ext/iodine/fiobj_numbers.h +87 -11
- data/ext/iodine/fiobj_str.c +423 -184
- data/ext/iodine/fiobj_str.h +81 -32
- data/ext/iodine/fiobject.c +273 -522
- data/ext/iodine/fiobject.h +477 -112
- data/ext/iodine/http.c +2243 -83
- data/ext/iodine/http.h +842 -121
- data/ext/iodine/http1.c +810 -385
- data/ext/iodine/http1.h +16 -39
- data/ext/iodine/http1_parser.c +146 -74
- data/ext/iodine/http1_parser.h +15 -4
- data/ext/iodine/http_internal.c +1258 -0
- data/ext/iodine/http_internal.h +226 -0
- data/ext/iodine/http_mime_parser.h +341 -0
- data/ext/iodine/iodine.c +86 -68
- data/ext/iodine/iodine.h +26 -11
- data/ext/iodine/iodine_helpers.c +8 -7
- data/ext/iodine/iodine_http.c +487 -324
- data/ext/iodine/iodine_json.c +304 -0
- data/ext/iodine/iodine_json.h +6 -0
- data/ext/iodine/iodine_protocol.c +107 -45
- data/ext/iodine/iodine_pubsub.c +526 -225
- data/ext/iodine/iodine_pubsub.h +10 -0
- data/ext/iodine/iodine_websockets.c +268 -510
- data/ext/iodine/iodine_websockets.h +2 -4
- data/ext/iodine/pubsub.c +726 -432
- data/ext/iodine/pubsub.h +85 -103
- data/ext/iodine/rb-call.c +4 -4
- data/ext/iodine/rb-defer.c +46 -22
- data/ext/iodine/rb-fiobj2rb.h +117 -0
- data/ext/iodine/rb-rack-io.c +73 -238
- data/ext/iodine/rb-rack-io.h +2 -2
- data/ext/iodine/rb-registry.c +35 -93
- data/ext/iodine/rb-registry.h +1 -0
- data/ext/iodine/redis_engine.c +742 -304
- data/ext/iodine/redis_engine.h +42 -39
- data/ext/iodine/resp_parser.h +311 -0
- data/ext/iodine/sock.c +627 -490
- data/ext/iodine/sock.h +345 -297
- data/ext/iodine/spnlock.inc +15 -4
- data/ext/iodine/websocket_parser.h +16 -20
- data/ext/iodine/websockets.c +188 -257
- data/ext/iodine/websockets.h +24 -133
- data/lib/iodine.rb +52 -7
- data/lib/iodine/cli.rb +6 -24
- data/lib/iodine/json.rb +40 -0
- data/lib/iodine/version.rb +1 -1
- data/lib/iodine/websocket.rb +5 -3
- data/lib/rack/handler/iodine.rb +58 -13
- metadata +38 -48
- data/bin/ws-shootout +0 -107
- data/examples/broadcast.ru +0 -56
- data/ext/iodine/bscrypt-common.h +0 -116
- data/ext/iodine/bscrypt.h +0 -49
- data/ext/iodine/fio2resp.c +0 -60
- data/ext/iodine/fio2resp.h +0 -51
- data/ext/iodine/fio_dict.c +0 -446
- data/ext/iodine/fio_dict.h +0 -99
- data/ext/iodine/fio_hash_table.h +0 -370
- data/ext/iodine/fio_list.h +0 -111
- data/ext/iodine/fiobj_internal.h +0 -280
- data/ext/iodine/fiobj_primitives.c +0 -131
- data/ext/iodine/fiobj_primitives.h +0 -55
- data/ext/iodine/fiobj_sym.c +0 -135
- data/ext/iodine/fiobj_sym.h +0 -60
- data/ext/iodine/hex.c +0 -124
- data/ext/iodine/hex.h +0 -70
- data/ext/iodine/http1_request.c +0 -81
- data/ext/iodine/http1_request.h +0 -58
- data/ext/iodine/http1_response.c +0 -417
- data/ext/iodine/http1_response.h +0 -95
- data/ext/iodine/http_request.c +0 -111
- data/ext/iodine/http_request.h +0 -102
- data/ext/iodine/http_response.c +0 -1703
- data/ext/iodine/http_response.h +0 -250
- data/ext/iodine/misc.c +0 -182
- data/ext/iodine/misc.h +0 -74
- data/ext/iodine/random.c +0 -208
- data/ext/iodine/redis_connection.c +0 -278
- data/ext/iodine/redis_connection.h +0 -86
- data/ext/iodine/resp.c +0 -842
- data/ext/iodine/resp.h +0 -261
- data/ext/iodine/siphash.c +0 -154
- data/ext/iodine/siphash.h +0 -22
- data/ext/iodine/xor-crypt.c +0 -193
- data/ext/iodine/xor-crypt.h +0 -107
data/ext/iodine/http_response.h
DELETED
@@ -1,250 +0,0 @@
|
|
1
|
-
#ifndef H_HTTP_RESPONSE_H
|
2
|
-
#define H_HTTP_RESPONSE_H
|
3
|
-
/*
|
4
|
-
Copyright: Boaz Segev, 2016-2017
|
5
|
-
License: MIT
|
6
|
-
|
7
|
-
Feel free to copy, use and enjoy according to the license provided.
|
8
|
-
*/
|
9
|
-
#include "http_request.h"
|
10
|
-
#include <stdio.h>
|
11
|
-
#include <time.h>
|
12
|
-
|
13
|
-
/* support C++ */
|
14
|
-
#ifdef __cplusplus
|
15
|
-
extern "C" {
|
16
|
-
#endif
|
17
|
-
|
18
|
-
typedef struct {
|
19
|
-
/** The protocol version family (HTTP/1.1 / HTTP/2 etc'). */
|
20
|
-
enum HTTP_VERSION http_version;
|
21
|
-
/** Will be set to TRUE (1) once the headers were sent. */
|
22
|
-
unsigned headers_sent : 1;
|
23
|
-
/** Set to true when the "Date" header is written to the buffer. */
|
24
|
-
unsigned date_written : 1;
|
25
|
-
/** Set to true when the "Connection" header is written to the buffer. */
|
26
|
-
unsigned connection_written : 1;
|
27
|
-
/** Set to true when the "Content-Length" header is written to the buffer. */
|
28
|
-
unsigned content_length_written : 1;
|
29
|
-
/** Set to true in order to close the connection once the response was sent.
|
30
|
-
*/
|
31
|
-
unsigned should_close : 1;
|
32
|
-
/** Internally used by the logging API. */
|
33
|
-
unsigned logged : 1;
|
34
|
-
/** Set this value to TRUE to indicate the request pointer should be freed. */
|
35
|
-
unsigned request_dupped : 1;
|
36
|
-
/** The response status */
|
37
|
-
uint16_t status;
|
38
|
-
/** The socket UUID for the response. */
|
39
|
-
intptr_t fd;
|
40
|
-
/** The originating request. */
|
41
|
-
http_request_s *request;
|
42
|
-
/** The body's response length.
|
43
|
-
*
|
44
|
-
* If this isn't set manually, the first call to `http_response_write_body`
|
45
|
-
* (and friends) will set the length to the length being written (which might
|
46
|
-
* be less then the total data sent, if the sending is fragmented).
|
47
|
-
*
|
48
|
-
* The value to -1 to prevents `http_response_s` from sending the
|
49
|
-
* `Content-Length` header.
|
50
|
-
*/
|
51
|
-
ssize_t content_length;
|
52
|
-
/** The HTTP Date for the response (in seconds since epoche).
|
53
|
-
*
|
54
|
-
* Defaults to now (approximately, not exactly, uses cached time data).
|
55
|
-
*
|
56
|
-
* The date will be automatically formatted to match the HTTP protocol
|
57
|
-
* specifications.
|
58
|
-
*
|
59
|
-
* It is better to avoid setting the "Date" header manualy.
|
60
|
-
*/
|
61
|
-
time_t date;
|
62
|
-
/** The HTTP Last-Modified date for the response (in seconds since epoche).
|
63
|
-
*
|
64
|
-
* Defaults to now (approximately, not exactly, uses cached time data).
|
65
|
-
*
|
66
|
-
* The date will be automatically formatted to match the HTTP protocol
|
67
|
-
* specifications.
|
68
|
-
*
|
69
|
-
* It is better to avoid setting the "Last-Modified" header manualy.
|
70
|
-
*/
|
71
|
-
time_t last_modified;
|
72
|
-
/**
|
73
|
-
Internally used by the logging API.
|
74
|
-
*/
|
75
|
-
clock_t clock_start;
|
76
|
-
} http_response_s;
|
77
|
-
|
78
|
-
/**
|
79
|
-
The struct HttpCookie is a helper for seting cookie data.
|
80
|
-
|
81
|
-
This struct is used together with the `http_response_set_cookie`. i.e.:
|
82
|
-
|
83
|
-
http_response_set_cookie(response,
|
84
|
-
.name = "my_cookie",
|
85
|
-
.value = "data" );
|
86
|
-
|
87
|
-
*/
|
88
|
-
typedef struct {
|
89
|
-
/** The cookie's name (key). */
|
90
|
-
char *name;
|
91
|
-
/** The cookie's value (leave blank to delete cookie). */
|
92
|
-
char *value;
|
93
|
-
/** The cookie's domain (optional). */
|
94
|
-
char *domain;
|
95
|
-
/** The cookie's path (optional). */
|
96
|
-
char *path;
|
97
|
-
/** The cookie name's size in bytes or a terminating NULL will be assumed.*/
|
98
|
-
size_t name_len;
|
99
|
-
/** The cookie value's size in bytes or a terminating NULL will be assumed.*/
|
100
|
-
size_t value_len;
|
101
|
-
/** The cookie domain's size in bytes or a terminating NULL will be assumed.*/
|
102
|
-
size_t domain_len;
|
103
|
-
/** The cookie path's size in bytes or a terminating NULL will be assumed.*/
|
104
|
-
size_t path_len;
|
105
|
-
/** Max Age (how long should the cookie persist), in seconds (0 == session).*/
|
106
|
-
int max_age;
|
107
|
-
/** Limit cookie to secure connections.*/
|
108
|
-
unsigned secure : 1;
|
109
|
-
/** Limit cookie to HTTP (intended to prevent javascript access/hijacking).*/
|
110
|
-
unsigned http_only : 1;
|
111
|
-
} http_cookie_s;
|
112
|
-
|
113
|
-
/* *****************************************************************************
|
114
|
-
Initialization
|
115
|
-
***************************************************************************** */
|
116
|
-
|
117
|
-
/** Creates / allocates a protocol version's response object. */
|
118
|
-
http_response_s *http_response_create(http_request_s *request);
|
119
|
-
/** Destroys the response object. No data is sent.*/
|
120
|
-
void http_response_destroy(http_response_s *);
|
121
|
-
/** Sends the data and destroys the response object.*/
|
122
|
-
void http_response_finish(http_response_s *);
|
123
|
-
|
124
|
-
/* *****************************************************************************
|
125
|
-
Writing data to the response object
|
126
|
-
***************************************************************************** */
|
127
|
-
|
128
|
-
/**
|
129
|
-
Writes a header to the response. This function writes only the requested
|
130
|
-
number of bytes from the header name and the requested number of bytes from
|
131
|
-
the header value. It can be used even when the header name and value don't
|
132
|
-
contain NULL terminating bytes by passing the `.name_len` or `.value_len` data
|
133
|
-
in the `http_headers_s` structure.
|
134
|
-
|
135
|
-
If the header buffer is full or the headers were already sent (new headers
|
136
|
-
cannot be sent), the function will return -1.
|
137
|
-
|
138
|
-
On success, the function returns 0.
|
139
|
-
*/
|
140
|
-
int http_response_write_header_fn(http_response_s *, http_header_s header);
|
141
|
-
#define http_response_write_header(response, ...) \
|
142
|
-
http_response_write_header_fn(response, (http_header_s){__VA_ARGS__})
|
143
|
-
|
144
|
-
/**
|
145
|
-
Set / Delete a cookie using this helper function.
|
146
|
-
|
147
|
-
To set a cookie, use (in this example, a session cookie):
|
148
|
-
|
149
|
-
http_response_set_cookie(response,
|
150
|
-
.name = "my_cookie",
|
151
|
-
.value = "data");
|
152
|
-
|
153
|
-
To delete a cookie, use:
|
154
|
-
|
155
|
-
http_response_set_cookie(response,
|
156
|
-
.name = "my_cookie",
|
157
|
-
.value = NULL);
|
158
|
-
|
159
|
-
This function writes a cookie header to the response. Only the requested
|
160
|
-
number of bytes from the cookie value and name are written (if none are
|
161
|
-
provided, a terminating NULL byte is assumed).
|
162
|
-
|
163
|
-
Both the name and the value of the cookie are checked for validity (legal
|
164
|
-
characters), but other properties aren't reviewed (domain/path) - please make
|
165
|
-
sure to use only valid data, as HTTP imposes restrictions on these things.
|
166
|
-
|
167
|
-
If the header buffer is full or the headers were already sent (new headers
|
168
|
-
cannot be sent), the function will return -1.
|
169
|
-
|
170
|
-
On success, the function returns 0.
|
171
|
-
*/
|
172
|
-
int http_response_set_cookie(http_response_s *, http_cookie_s);
|
173
|
-
#define http_response_set_cookie(response, ...) \
|
174
|
-
http_response_set_cookie(response, (http_cookie_s){__VA_ARGS__})
|
175
|
-
|
176
|
-
/**
|
177
|
-
Sends the headers (if they weren't previously sent) and writes the data to the
|
178
|
-
underlying socket.
|
179
|
-
|
180
|
-
The body will be copied to the server's outgoing buffer.
|
181
|
-
|
182
|
-
If the connection was already closed, the function will return -1. On success,
|
183
|
-
the function returns 0.
|
184
|
-
*/
|
185
|
-
int http_response_write_body(http_response_s *, const char *body,
|
186
|
-
size_t length);
|
187
|
-
|
188
|
-
/**
|
189
|
-
Sends the headers (if they weren't previously sent) and writes the data to the
|
190
|
-
underlying socket.
|
191
|
-
|
192
|
-
The server's outgoing buffer will take ownership of the file and close it
|
193
|
-
using `close` once the data was sent.
|
194
|
-
|
195
|
-
If the connection was already closed, the function will return -1. On success,
|
196
|
-
the function returns 0. The file is alsways closed by the function.
|
197
|
-
|
198
|
-
If should be possible to destroy the response object and send an error response
|
199
|
-
if an error is detected. The function will avoid sending any data before it
|
200
|
-
knows the likelyhood of error is small enough.
|
201
|
-
*/
|
202
|
-
int http_response_sendfile(http_response_s *, int source_fd, off_t offset,
|
203
|
-
size_t length);
|
204
|
-
/**
|
205
|
-
Attempts to send the file requested using an **optional** response object (if no
|
206
|
-
response object is pointed to, a temporary response object will be created).
|
207
|
-
|
208
|
-
If a `file_path_unsafe` is provided, it will be appended to the `file_path_safe`
|
209
|
-
(if any) and URL decoded before attempting to locate and open the file. Any
|
210
|
-
insecure path manipulations in the `file_path_unsafe` (i.e. `..` or `//`) will
|
211
|
-
cause the function to fail.
|
212
|
-
|
213
|
-
`file_path_unsafe` MUST begine with a `/`, or it will be appended to
|
214
|
-
`file_path_safe` as part of the last folder's name. if `file_path_safe` ends
|
215
|
-
with a `/`, it will be trancated.
|
216
|
-
|
217
|
-
If the `log` flag is set, response logging will be performed.
|
218
|
-
|
219
|
-
If the path ends with a backslash ('/'), the string `"index.html"` will be
|
220
|
-
appended (a default file name for when serving a folder). No automatic folder
|
221
|
-
indexing is supported.
|
222
|
-
|
223
|
-
This function will honor Ranged requests by setting the byte range
|
224
|
-
appropriately.
|
225
|
-
|
226
|
-
On failure, the function will return -1 (no response will be sent).
|
227
|
-
|
228
|
-
On success, the function returns 0.
|
229
|
-
*/
|
230
|
-
int http_response_sendfile2(http_response_s *response, http_request_s *request,
|
231
|
-
const char *file_path_safe, size_t path_safe_len,
|
232
|
-
const char *file_path_unsafe,
|
233
|
-
size_t path_unsafe_len, uint8_t log);
|
234
|
-
/* *****************************************************************************
|
235
|
-
Helpers and common tasks
|
236
|
-
***************************************************************************** */
|
237
|
-
|
238
|
-
/** Gets a response status, as a string. */
|
239
|
-
const char *http_response_status_str(uint16_t status);
|
240
|
-
/** Gets the mime-type string (C string) associated with the file extension. */
|
241
|
-
const char *http_response_ext2mime(const char *ext);
|
242
|
-
|
243
|
-
/** Starts counting miliseconds for log results. */
|
244
|
-
void http_response_log_start(http_response_s *);
|
245
|
-
|
246
|
-
#ifdef __cplusplus
|
247
|
-
} /* extern "C" */
|
248
|
-
#endif
|
249
|
-
|
250
|
-
#endif
|
data/ext/iodine/misc.c
DELETED
@@ -1,182 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
Copyright: Boaz segev, 2016-2017
|
3
|
-
License: MIT except for any non-public-domain algorithms (none that I'm aware
|
4
|
-
of), which might be subject to their own licenses.
|
5
|
-
|
6
|
-
Feel free to copy, use and enjoy in accordance with to the license(s).
|
7
|
-
*/
|
8
|
-
#ifndef _GNU_SOURCE
|
9
|
-
#define _GNU_SOURCE
|
10
|
-
#endif
|
11
|
-
#include "misc.h"
|
12
|
-
/* ***************************************************************************
|
13
|
-
Other helper functions
|
14
|
-
*/
|
15
|
-
|
16
|
-
#ifdef HAS_UNIX_FEATURES
|
17
|
-
#include <errno.h>
|
18
|
-
#include <fcntl.h>
|
19
|
-
#include <limits.h>
|
20
|
-
#include <sys/stat.h>
|
21
|
-
#include <sys/types.h>
|
22
|
-
#include <unistd.h>
|
23
|
-
|
24
|
-
/**
|
25
|
-
Allocates memory and dumps the whole file into the memory allocated.
|
26
|
-
|
27
|
-
Remember to call `free` when done.
|
28
|
-
|
29
|
-
Returns the number of bytes allocated. On error, returns 0 and sets the
|
30
|
-
container pointer to NULL.
|
31
|
-
|
32
|
-
This function has some Unix specific properties that resolve links and user
|
33
|
-
folder referencing.
|
34
|
-
*/
|
35
|
-
fdump_s *bscrypt_fdump(const char *file_path, size_t size_limit) {
|
36
|
-
struct stat f_data;
|
37
|
-
int file = -1;
|
38
|
-
fdump_s *container = NULL;
|
39
|
-
size_t file_path_len;
|
40
|
-
if (file_path == NULL || (file_path_len = strlen(file_path)) == 0 ||
|
41
|
-
file_path_len > PATH_MAX)
|
42
|
-
return NULL;
|
43
|
-
|
44
|
-
char real_public_path[PATH_MAX + 1];
|
45
|
-
real_public_path[PATH_MAX] = 0;
|
46
|
-
if (file_path[0] == '~' && getenv("HOME") && file_path_len <= PATH_MAX) {
|
47
|
-
strcpy(real_public_path, getenv("HOME"));
|
48
|
-
memcpy(real_public_path + strlen(real_public_path), file_path + 1,
|
49
|
-
file_path_len);
|
50
|
-
file_path = real_public_path;
|
51
|
-
}
|
52
|
-
|
53
|
-
if (stat(file_path, &f_data))
|
54
|
-
goto error;
|
55
|
-
if (size_limit == 0 || (size_t)f_data.st_size < size_limit)
|
56
|
-
size_limit = f_data.st_size;
|
57
|
-
container = malloc(size_limit + sizeof(fdump_s) + 1);
|
58
|
-
if (!container)
|
59
|
-
goto error;
|
60
|
-
file = open(file_path, O_RDONLY);
|
61
|
-
if (file < 0)
|
62
|
-
goto error;
|
63
|
-
if (read(file, container->data, size_limit) != (ssize_t)size_limit)
|
64
|
-
goto error;
|
65
|
-
close(file);
|
66
|
-
container->length = size_limit;
|
67
|
-
container->data[size_limit] = 0;
|
68
|
-
return container;
|
69
|
-
error:
|
70
|
-
if (container)
|
71
|
-
free(container), (container = NULL);
|
72
|
-
if (file >= 0)
|
73
|
-
close(file);
|
74
|
-
return 0;
|
75
|
-
}
|
76
|
-
|
77
|
-
#endif /* HAS_UNIX_FEATURES */
|
78
|
-
|
79
|
-
/**
|
80
|
-
A faster (yet less localized) alternative to `gmtime_r`.
|
81
|
-
|
82
|
-
See the libc `gmtime_r` documentation for details.
|
83
|
-
|
84
|
-
Falls back to `gmtime_r` for dates before epoch.
|
85
|
-
*/
|
86
|
-
struct tm *bscrypt_gmtime(const time_t *timer, struct tm *tmbuf) {
|
87
|
-
// static char* DAYS[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
88
|
-
// static char * Months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
89
|
-
// "Jul",
|
90
|
-
// "Aug", "Sep", "Oct", "Nov", "Dec"};
|
91
|
-
static uint8_t month_len[] = {
|
92
|
-
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, // nonleap year
|
93
|
-
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 // leap year
|
94
|
-
};
|
95
|
-
if (*timer < 0)
|
96
|
-
return gmtime_r(timer, tmbuf);
|
97
|
-
ssize_t tmp;
|
98
|
-
tmbuf->tm_gmtoff = 0;
|
99
|
-
tmbuf->tm_zone = "UTC";
|
100
|
-
tmbuf->tm_isdst = 0;
|
101
|
-
tmbuf->tm_year = 70; // tm_year == The number of years since 1900
|
102
|
-
tmbuf->tm_mon = 0;
|
103
|
-
// for seconds up to weekdays, we build up, as small values clean up larger
|
104
|
-
// values.
|
105
|
-
tmp = ((ssize_t)*timer);
|
106
|
-
tmbuf->tm_sec = tmp % 60;
|
107
|
-
tmp = tmp / 60;
|
108
|
-
tmbuf->tm_min = tmp % 60;
|
109
|
-
tmp = tmp / 60;
|
110
|
-
tmbuf->tm_hour = tmp % 24;
|
111
|
-
tmp = tmp / 24;
|
112
|
-
// day of epoch was a thursday. Add + 3 so sunday == 0...
|
113
|
-
tmbuf->tm_wday = (tmp + 3) % 7;
|
114
|
-
// tmp == number of days since epoch
|
115
|
-
#define DAYS_PER_400_YEARS ((400 * 365) + 97)
|
116
|
-
while (tmp >= DAYS_PER_400_YEARS) {
|
117
|
-
tmbuf->tm_year += 400;
|
118
|
-
tmp -= DAYS_PER_400_YEARS;
|
119
|
-
}
|
120
|
-
#undef DAYS_PER_400_YEARS
|
121
|
-
#define DAYS_PER_100_YEARS ((100 * 365) + 24)
|
122
|
-
while (tmp >= DAYS_PER_100_YEARS) {
|
123
|
-
tmbuf->tm_year += 100;
|
124
|
-
tmp -= DAYS_PER_100_YEARS;
|
125
|
-
if (((tmbuf->tm_year / 100) & 3) ==
|
126
|
-
0) // leap century divisable by 400 => add leap
|
127
|
-
--tmp;
|
128
|
-
}
|
129
|
-
#undef DAYS_PER_100_YEARS
|
130
|
-
#define DAYS_PER_32_YEARS ((32 * 365) + 8)
|
131
|
-
while (tmp >= DAYS_PER_32_YEARS) {
|
132
|
-
tmbuf->tm_year += 32;
|
133
|
-
tmp -= DAYS_PER_32_YEARS;
|
134
|
-
}
|
135
|
-
#undef DAYS_PER_32_YEARS
|
136
|
-
#define DAYS_PER_8_YEARS ((8 * 365) + 2)
|
137
|
-
while (tmp >= DAYS_PER_8_YEARS) {
|
138
|
-
tmbuf->tm_year += 8;
|
139
|
-
tmp -= DAYS_PER_8_YEARS;
|
140
|
-
}
|
141
|
-
#undef DAYS_PER_8_YEARS
|
142
|
-
#define DAYS_PER_4_YEARS ((4 * 365) + 1)
|
143
|
-
while (tmp >= DAYS_PER_4_YEARS) {
|
144
|
-
tmbuf->tm_year += 4;
|
145
|
-
tmp -= DAYS_PER_4_YEARS;
|
146
|
-
}
|
147
|
-
#undef DAYS_PER_4_YEARS
|
148
|
-
while (tmp >= 365) {
|
149
|
-
tmbuf->tm_year += 1;
|
150
|
-
tmp -= 365;
|
151
|
-
if ((tmbuf->tm_year & 3) == 0) { // leap year
|
152
|
-
if (tmp > 0) {
|
153
|
-
--tmp;
|
154
|
-
continue;
|
155
|
-
} else {
|
156
|
-
tmp += 365;
|
157
|
-
--tmbuf->tm_year;
|
158
|
-
break;
|
159
|
-
}
|
160
|
-
}
|
161
|
-
}
|
162
|
-
tmbuf->tm_yday = tmp;
|
163
|
-
if ((tmbuf->tm_year & 3) == 1) {
|
164
|
-
// regular year
|
165
|
-
for (size_t i = 0; i < 12; i++) {
|
166
|
-
if (tmp < month_len[i])
|
167
|
-
break;
|
168
|
-
tmp -= month_len[i];
|
169
|
-
++tmbuf->tm_mon;
|
170
|
-
}
|
171
|
-
} else {
|
172
|
-
// leap year
|
173
|
-
for (size_t i = 12; i < 24; i++) {
|
174
|
-
if (tmp < month_len[i])
|
175
|
-
break;
|
176
|
-
tmp -= month_len[i];
|
177
|
-
++tmbuf->tm_mon;
|
178
|
-
}
|
179
|
-
}
|
180
|
-
tmbuf->tm_mday = tmp;
|
181
|
-
return tmbuf;
|
182
|
-
}
|
data/ext/iodine/misc.h
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
Copyright: Boaz segev, 2016-2017
|
3
|
-
License: MIT except for any non-public-domain algorithms (none that I'm aware
|
4
|
-
of), which might be subject to their own licenses.
|
5
|
-
|
6
|
-
Feel free to copy, use and enjoy in accordance with to the license(s).
|
7
|
-
*/
|
8
|
-
#ifndef bscrypt_MISC_H
|
9
|
-
#define bscrypt_MISC_H
|
10
|
-
#include "bscrypt-common.h"
|
11
|
-
#include <time.h>
|
12
|
-
/* *****************************************************************************
|
13
|
-
C++ extern
|
14
|
-
*/
|
15
|
-
#if defined(__cplusplus)
|
16
|
-
extern "C" {
|
17
|
-
#endif
|
18
|
-
|
19
|
-
/* ***************************************************************************
|
20
|
-
Miscellaneous helper functions
|
21
|
-
|
22
|
-
i.e. file content dumping and GMT time alternative to `gmtime_r`.
|
23
|
-
*/
|
24
|
-
|
25
|
-
#ifdef HAS_UNIX_FEATURES
|
26
|
-
|
27
|
-
/**
|
28
|
-
File dump data.
|
29
|
-
|
30
|
-
This struct (or, a pointer to this struct) is returned
|
31
|
-
by the `bscrypt.fdump`
|
32
|
-
function on success.
|
33
|
-
|
34
|
-
To free the pointer returned, simply call `free`.
|
35
|
-
*/
|
36
|
-
typedef struct {
|
37
|
-
size_t length;
|
38
|
-
char data[];
|
39
|
-
} fdump_s;
|
40
|
-
|
41
|
-
/**
|
42
|
-
Allocates memory and dumps the whole file into the memory allocated.
|
43
|
-
|
44
|
-
!!!: Remember to call `free` when done.
|
45
|
-
|
46
|
-
Returns the number of bytes allocated.
|
47
|
-
|
48
|
-
On error, returns 0 and sets the container pointer to NULL.
|
49
|
-
|
50
|
-
This function has some Unix specific properties that resolve links and user
|
51
|
-
folder referencing.
|
52
|
-
*/
|
53
|
-
fdump_s *bscrypt_fdump(const char *file_path, size_t size_limit);
|
54
|
-
|
55
|
-
#endif /* HAS_UNIX_FEATURES */
|
56
|
-
|
57
|
-
/**
|
58
|
-
A faster (yet less localized) alternative to
|
59
|
-
`gmtime_r`.
|
60
|
-
|
61
|
-
See the libc `gmtime_r` documentation for details.
|
62
|
-
|
63
|
-
Falls back to `gmtime_r` for dates before epoch.
|
64
|
-
*/
|
65
|
-
struct tm *bscrypt_gmtime(const time_t *timer, struct tm *tmbuf);
|
66
|
-
|
67
|
-
/* *****************************************************************************
|
68
|
-
C++ extern finish
|
69
|
-
*/
|
70
|
-
#if defined(__cplusplus)
|
71
|
-
}
|
72
|
-
#endif
|
73
|
-
|
74
|
-
#endif
|