mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-22 05:15:04 +00:00
Backport from xs.
This commit is contained in:
parent
17842c2a79
commit
a4cf695e60
5 changed files with 31 additions and 27 deletions
47
xs.h
47
xs.h
|
@ -43,6 +43,7 @@ xstype xs_type(const char *data);
|
|||
int xs_size(const char *data);
|
||||
int xs_is_null(char *data);
|
||||
d_char *xs_dup(const char *data);
|
||||
void *xs_realloc(void *ptr, size_t size);
|
||||
d_char *xs_expand(d_char *data, int offset, int size);
|
||||
d_char *xs_collapse(d_char *data, int offset, int size);
|
||||
d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size);
|
||||
|
@ -198,7 +199,7 @@ d_char *xs_dup(const char *data)
|
|||
/* creates a duplicate of data */
|
||||
{
|
||||
int sz = xs_size(data);
|
||||
d_char *s = malloc(_xs_blk_size(sz));
|
||||
d_char *s = xs_realloc(NULL, _xs_blk_size(sz));
|
||||
|
||||
memcpy(s, data, sz);
|
||||
|
||||
|
@ -206,29 +207,31 @@ d_char *xs_dup(const char *data)
|
|||
}
|
||||
|
||||
|
||||
void *xs_realloc(void *ptr, size_t size)
|
||||
{
|
||||
d_char *ndata = realloc(ptr, size);
|
||||
|
||||
if (ndata == NULL) {
|
||||
fprintf(stderr, "**OUT OF MEMORY**\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
return ndata;
|
||||
}
|
||||
|
||||
|
||||
d_char *xs_expand(d_char *data, int offset, int size)
|
||||
/* opens a hole in data */
|
||||
{
|
||||
int sz = xs_size(data);
|
||||
int n;
|
||||
|
||||
/* open room */
|
||||
if (sz == 0 || _xs_blk_size(sz) != _xs_blk_size(sz + size)) {
|
||||
d_char *ndata;
|
||||
|
||||
ndata = realloc(data, _xs_blk_size(sz + size));
|
||||
|
||||
if (ndata == NULL) {
|
||||
fprintf(stderr, "**OUT OF MEMORY**");
|
||||
abort();
|
||||
}
|
||||
else
|
||||
data = ndata;
|
||||
}
|
||||
if (sz == 0 || _xs_blk_size(sz) != _xs_blk_size(sz + size))
|
||||
data = xs_realloc(data, _xs_blk_size(sz + size));
|
||||
|
||||
/* move up the rest of the data */
|
||||
for (n = sz + size - 1; n >= offset + size; n--)
|
||||
data[n] = data[n - size];
|
||||
if (data != NULL)
|
||||
memmove(data + offset + size, data + offset, sz - offset);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
@ -250,7 +253,7 @@ d_char *xs_collapse(d_char *data, int offset, int size)
|
|||
for (n = offset; n < sz; n++)
|
||||
data[n] = data[n + size];
|
||||
|
||||
return realloc(data, _xs_blk_size(sz));
|
||||
return xs_realloc(data, _xs_blk_size(sz));
|
||||
}
|
||||
|
||||
|
||||
|
@ -311,7 +314,7 @@ d_char *xs_fmt(const char *fmt, ...)
|
|||
s = calloc(n, 1);
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = vsnprintf(s, n, fmt, ap);
|
||||
vsnprintf(s, n, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
|
@ -398,7 +401,7 @@ d_char *xs_list_new(void)
|
|||
{
|
||||
d_char *list;
|
||||
|
||||
list = malloc(_xs_blk_size(2));
|
||||
list = xs_realloc(NULL, _xs_blk_size(2));
|
||||
list[0] = XSTYPE_LIST;
|
||||
list[1] = XSTYPE_EOL;
|
||||
|
||||
|
@ -573,7 +576,7 @@ d_char *xs_dict_new(void)
|
|||
{
|
||||
d_char *dict;
|
||||
|
||||
dict = malloc(_xs_blk_size(2));
|
||||
dict = xs_realloc(NULL, _xs_blk_size(2));
|
||||
dict[0] = XSTYPE_DICT;
|
||||
dict[1] = XSTYPE_EOD;
|
||||
|
||||
|
@ -688,7 +691,7 @@ d_char *xs_dict_set(d_char *dict, const char *key, const char *data)
|
|||
d_char *xs_val_new(xstype t)
|
||||
/* adds a new special value */
|
||||
{
|
||||
d_char *v = malloc(_xs_blk_size(1));
|
||||
d_char *v = xs_realloc(NULL, _xs_blk_size(1));
|
||||
|
||||
v[0] = t;
|
||||
|
||||
|
@ -699,7 +702,7 @@ d_char *xs_val_new(xstype t)
|
|||
d_char *xs_number_new(float f)
|
||||
/* adds a new number value */
|
||||
{
|
||||
d_char *v = malloc(_xs_blk_size(1 + sizeof(float)));
|
||||
d_char *v = xs_realloc(NULL, _xs_blk_size(1 + sizeof(float)));
|
||||
|
||||
v[0] = XSTYPE_NUMBER;
|
||||
memcpy(&v[1], &f, sizeof(float));
|
||||
|
|
|
@ -508,7 +508,7 @@ d_char *xs_json_loads(const char *json)
|
|||
d_char *v = NULL;
|
||||
js_type t;
|
||||
|
||||
_xs_json_loads_lexer(&json, &t);
|
||||
free(_xs_json_loads_lexer(&json, &t));
|
||||
|
||||
if (t == JS_OBRACK)
|
||||
v = _xs_json_loads_array(&json, &t);
|
||||
|
|
|
@ -133,7 +133,7 @@ d_char *xs_rsa_sign(char *secret, char *mem, int size)
|
|||
rsa = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL);
|
||||
|
||||
/* alloc space */
|
||||
sig = malloc(RSA_size(rsa));
|
||||
sig = xs_realloc(NULL, RSA_size(rsa));
|
||||
|
||||
if (RSA_sign(NID_sha256, (unsigned char *)mem, size, sig, &sig_len, rsa) == 1)
|
||||
signature = xs_base64_enc((char *)sig, sig_len);
|
||||
|
@ -200,7 +200,7 @@ d_char *xs_evp_sign(char *secret, char *mem, int size)
|
|||
mdctx = EVP_MD_CTX_new();
|
||||
|
||||
sig_len = EVP_PKEY_size(pkey);
|
||||
sig = malloc(sig_len);
|
||||
sig = xs_realloc(NULL, sig_len);
|
||||
|
||||
EVP_SignInit(mdctx, md);
|
||||
EVP_SignUpdate(mdctx, mem, size);
|
||||
|
|
|
@ -60,9 +60,10 @@ d_char *xs_regex_match_n(const char *str, const char *rx, int count)
|
|||
int n = 0;
|
||||
|
||||
/* split */
|
||||
p = split = xs_regex_split_n(str, rx, count);
|
||||
split = xs_regex_split_n(str, rx, count);
|
||||
|
||||
/* now iterate to get only the 'separators' (odd ones) */
|
||||
p = split;
|
||||
while (xs_list_iter(&p, &v)) {
|
||||
if (n & 0x1)
|
||||
list = xs_list_append(list, v);
|
||||
|
|
2
xs_set.h
2
xs_set.h
|
@ -8,7 +8,7 @@ typedef struct _xs_set {
|
|||
int elems; /* number of hash entries */
|
||||
int used; /* number of used hash entries */
|
||||
d_char *list; /* list of stored data */
|
||||
int hash[0]; /* hashed offsets */
|
||||
int hash[]; /* hashed offsets */
|
||||
} xs_set;
|
||||
|
||||
xs_set *xs_set_new(int elems);
|
||||
|
|
Loading…
Reference in a new issue