Backport from xs.

This commit is contained in:
default 2022-10-08 07:00:05 +02:00
parent 29a0b21929
commit 0f4779e2a0
6 changed files with 29 additions and 28 deletions

26
xs.h
View file

@ -41,7 +41,7 @@ void _xs_destroy(char **var);
#define xs_debug() raise(SIGTRAP) #define xs_debug() raise(SIGTRAP)
xstype xs_type(const char *data); xstype xs_type(const char *data);
int xs_size(const char *data); int xs_size(const char *data);
int xs_is_null(char *data); int xs_is_null(const char *data);
d_char *xs_dup(const char *data); d_char *xs_dup(const char *data);
void *xs_realloc(void *ptr, size_t size); void *xs_realloc(void *ptr, size_t size);
d_char *xs_expand(d_char *data, int offset, int size); d_char *xs_expand(d_char *data, int offset, int size);
@ -54,9 +54,9 @@ d_char *xs_str_new(const char *str);
d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto); d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto);
#define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto) #define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto)
d_char *xs_fmt(const char *fmt, ...); d_char *xs_fmt(const char *fmt, ...);
int xs_str_in(char *haystack, char *needle); int xs_str_in(const char *haystack, const char *needle);
int xs_startswith(char *str, char *prefix); int xs_startswith(const char *str, const char *prefix);
int xs_endswith(char *str, char *postfix); int xs_endswith(const char *str, const char *postfix);
d_char *xs_crop(d_char *str, int start, int end); d_char *xs_crop(d_char *str, int start, int end);
d_char *xs_strip(d_char *str); d_char *xs_strip(d_char *str);
d_char *xs_tolower(d_char *str); d_char *xs_tolower(d_char *str);
@ -79,8 +79,8 @@ d_char *xs_dict_del(d_char *dict, const char *key);
d_char *xs_dict_set(d_char *dict, const char *key, const char *data); d_char *xs_dict_set(d_char *dict, const char *key, const char *data);
d_char *xs_val_new(xstype t); d_char *xs_val_new(xstype t);
d_char *xs_number_new(double f); d_char *xs_number_new(double f);
double xs_number_get(char *v); double xs_number_get(const char *v);
char *xs_number_str(char *v); const char *xs_number_str(const char *v);
extern int _xs_debug; extern int _xs_debug;
@ -189,7 +189,7 @@ int xs_size(const char *data)
} }
int xs_is_null(char *data) int xs_is_null(const char *data)
/* checks for null */ /* checks for null */
{ {
return !!(data == NULL || xs_type(data) == XSTYPE_NULL); return !!(data == NULL || xs_type(data) == XSTYPE_NULL);
@ -323,7 +323,7 @@ d_char *xs_fmt(const char *fmt, ...)
} }
int xs_str_in(char *haystack, char *needle) int xs_str_in(const char *haystack, const char *needle)
/* finds needle in haystack and returns the offset or -1 */ /* finds needle in haystack and returns the offset or -1 */
{ {
char *s; char *s;
@ -336,14 +336,14 @@ int xs_str_in(char *haystack, char *needle)
} }
int xs_startswith(char *str, char *prefix) int xs_startswith(const char *str, const char *prefix)
/* returns true if str starts with prefix */ /* returns true if str starts with prefix */
{ {
return !!(xs_str_in(str, prefix) == 0); return !!(xs_str_in(str, prefix) == 0);
} }
int xs_endswith(char *str, char *postfix) int xs_endswith(const char *str, const char *postfix)
/* returns true if str ends with postfix */ /* returns true if str ends with postfix */
{ {
int ssz = strlen(str); int ssz = strlen(str);
@ -732,7 +732,7 @@ d_char *xs_number_new(double f)
} }
double xs_number_get(char *v) double xs_number_get(const char *v)
/* gets the number as a double */ /* gets the number as a double */
{ {
double f = 0.0; double f = 0.0;
@ -744,10 +744,10 @@ double xs_number_get(char *v)
} }
char *xs_number_str(char *v) const char *xs_number_str(const char *v)
/* gets the number as a string */ /* gets the number as a string */
{ {
char *p = NULL; const char *p = NULL;
if (v[0] == XSTYPE_NUMBER) if (v[0] == XSTYPE_NUMBER)
p = &v[1]; p = &v[1];

View file

@ -4,7 +4,7 @@
#define _XS_MIME #define _XS_MIME
char *xs_mime_by_ext(char *file); char *xs_mime_by_ext(const char *file);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
@ -27,7 +27,7 @@ struct _mime_info {
}; };
char *xs_mime_by_ext(char *file) char *xs_mime_by_ext(const char *file)
/* returns the MIME type by file extension */ /* returns the MIME type by file extension */
{ {
struct _mime_info *mi = mime_info; struct _mime_info *mi = mime_info;

View file

@ -9,10 +9,10 @@ d_char *xs_sha1_hex(const void *input, int size);
d_char *xs_sha256_hex(const void *input, int size); d_char *xs_sha256_hex(const void *input, int size);
d_char *xs_sha256_base64(const void *input, int size); d_char *xs_sha256_base64(const void *input, int size);
d_char *xs_rsa_genkey(int bits); d_char *xs_rsa_genkey(int bits);
d_char *xs_rsa_sign(char *secret, char *mem, int size); d_char *xs_rsa_sign(const char *secret, const char *mem, int size);
int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig); int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
d_char *xs_evp_sign(char *secret, char *mem, int size); d_char *xs_evp_sign(const char *secret, const char *mem, int size);
int xs_evp_verify(char *pubkey, char *mem, int size, char *b64sig); int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
@ -119,7 +119,7 @@ d_char *xs_rsa_genkey(int bits)
} }
d_char *xs_rsa_sign(char *secret, char *mem, int size) d_char *xs_rsa_sign(const char *secret, const char *mem, int size)
/* signs a memory block (secret is in PEM format) */ /* signs a memory block (secret is in PEM format) */
{ {
d_char *signature = NULL; d_char *signature = NULL;
@ -146,7 +146,7 @@ d_char *xs_rsa_sign(char *secret, char *mem, int size)
} }
int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig) int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig)
/* verifies a base64 block, returns non-zero on ok */ /* verifies a base64 block, returns non-zero on ok */
{ {
int r = 0; int r = 0;
@ -176,7 +176,7 @@ int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig)
} }
d_char *xs_evp_sign(char *secret, char *mem, int size) d_char *xs_evp_sign(const char *secret, const char *mem, int size)
/* signs a memory block (secret is in PEM format) */ /* signs a memory block (secret is in PEM format) */
{ {
d_char *signature = NULL; d_char *signature = NULL;
@ -217,7 +217,7 @@ d_char *xs_evp_sign(char *secret, char *mem, int size)
} }
int xs_evp_verify(char *pubkey, char *mem, int size, char *b64sig) int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig)
/* verifies a base64 block, returns non-zero on ok */ /* verifies a base64 block, returns non-zero on ok */
{ {
int r = 0; int r = 0;

View file

@ -13,7 +13,7 @@ typedef struct _xs_set {
xs_set *xs_set_new(int elems); xs_set *xs_set_new(int elems);
void xs_set_free(xs_set *s); void xs_set_free(xs_set *s);
int xs_set_add(xs_set *s, char *data); int xs_set_add(xs_set *s, const char *data);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
@ -40,7 +40,7 @@ void xs_set_free(xs_set *s)
} }
unsigned int _xs_set_hash(char *data, int size) unsigned int _xs_set_hash(const char *data, int size)
{ {
unsigned int hash = 0x666; unsigned int hash = 0x666;
int n; int n;
@ -54,7 +54,7 @@ unsigned int _xs_set_hash(char *data, int size)
} }
int xs_set_add(xs_set *s, char *data) int xs_set_add(xs_set *s, const char *data)
/* adds the data to the set */ /* adds the data to the set */
/* returns: 1 if added, 0 if already there, -1 if it's full */ /* returns: 1 if added, 0 if already there, -1 if it's full */
{ {

View file

@ -5,7 +5,7 @@
#define _XS_SOCKET_H #define _XS_SOCKET_H
int xs_socket_timeout(int s, double rto, double sto); int xs_socket_timeout(int s, double rto, double sto);
int xs_socket_server(char *addr, int port); int xs_socket_server(const char *addr, int port);
FILE *xs_socket_accept(int rs); FILE *xs_socket_accept(int rs);
@ -40,7 +40,7 @@ int xs_socket_timeout(int s, double rto, double sto)
} }
int xs_socket_server(char *addr, int port) int xs_socket_server(const char *addr, int port)
/* opens a server socket */ /* opens a server socket */
{ {
int rs = -1; int rs = -1;

1
xs_version.h Normal file
View file

@ -0,0 +1 @@
/* b6512569814e4b409191a24193abe54d00427df2 */