diff --git a/Makefile b/Makefile index 62beea0..ae3b231 100644 --- a/Makefile +++ b/Makefile @@ -27,24 +27,23 @@ install: mkdir -p -m 755 $(PREFIX_MAN)/man8 install -m 644 doc/snac.8 $(PREFIX_MAN)/man8/snac.8 -activitypub.o: activitypub.c xs.h xs_encdec.h xs_json.h xs_curl.h \ - xs_mime.h xs_openssl.h xs_regex.h xs_time.h xs_set.h snac.h +activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \ + xs_openssl.h xs_regex.h xs_time.h xs_set.h snac.h data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h xs_set.h \ xs_time.h snac.h format.o: format.c xs.h xs_regex.h snac.h -html.o: html.c xs.h xs_io.h xs_encdec.h xs_json.h xs_regex.h xs_set.h \ - xs_openssl.h xs_time.h xs_mime.h snac.h -http.o: http.c xs.h xs_io.h xs_encdec.h xs_openssl.h xs_curl.h xs_time.h \ - xs_json.h snac.h -httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ - xs_httpd.h xs_mime.h xs_time.h snac.h -main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h -mastoapi.o: mastoapi.c xs.h xs_encdec.h xs_openssl.h xs_json.h xs_io.h \ - xs_time.h xs_glob.h snac.h -snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ +html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ + xs_time.h xs_mime.h snac.h +http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ + snac.h +httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_httpd.h xs_mime.h \ + xs_time.h snac.h +main.o: main.c xs.h xs_io.h xs_json.h snac.h +mastoapi.o: mastoapi.c xs.h xs_openssl.h xs_json.h xs_io.h xs_time.h \ + xs_glob.h xs_set.h snac.h +snac.o: snac.c xs.h xs_io.h xs_unicode.h xs_json.h xs_curl.h xs_openssl.h \ xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ snac.h upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h -utils.o: utils.c xs.h xs_io.h xs_encdec.h xs_json.h xs_time.h \ - xs_openssl.h snac.h -webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h +utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h +webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h snac.h diff --git a/activitypub.c b/activitypub.c index 13415df..13e17b3 100644 --- a/activitypub.c +++ b/activitypub.c @@ -2,7 +2,6 @@ /* copyright (c) 2022 - 2023 grunfink / MIT license */ #include "xs.h" -#include "xs_encdec.h" #include "xs_json.h" #include "xs_curl.h" #include "xs_mime.h" diff --git a/html.c b/html.c index 861ac8b..4e3f235 100644 --- a/html.c +++ b/html.c @@ -3,7 +3,6 @@ #include "xs.h" #include "xs_io.h" -#include "xs_encdec.h" #include "xs_json.h" #include "xs_regex.h" #include "xs_set.h" diff --git a/http.c b/http.c index 58d188c..75769f2 100644 --- a/http.c +++ b/http.c @@ -3,7 +3,6 @@ #include "xs.h" #include "xs_io.h" -#include "xs_encdec.h" #include "xs_openssl.h" #include "xs_curl.h" #include "xs_time.h" diff --git a/httpd.c b/httpd.c index 841ad4c..09b8d41 100644 --- a/httpd.c +++ b/httpd.c @@ -3,12 +3,12 @@ #include "xs.h" #include "xs_io.h" -#include "xs_encdec.h" #include "xs_json.h" #include "xs_socket.h" #include "xs_httpd.h" #include "xs_mime.h" #include "xs_time.h" +#include "xs_openssl.h" #include "snac.h" diff --git a/main.c b/main.c index 322aa67..1462c64 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,6 @@ #include "xs.h" #include "xs_io.h" -#include "xs_encdec.h" #include "xs_json.h" #include "snac.h" diff --git a/mastoapi.c b/mastoapi.c index cb0ba91..f5701d9 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -4,7 +4,6 @@ #ifndef NO_MASTODON_API #include "xs.h" -#include "xs_encdec.h" #include "xs_openssl.h" #include "xs_json.h" #include "xs_io.h" diff --git a/snac.c b/snac.c index 8005546..48353f1 100644 --- a/snac.c +++ b/snac.c @@ -7,7 +7,6 @@ #include "xs_io.h" #include "xs_unicode.h" #include "xs_json.h" -#include "xs_encdec.h" #include "xs_curl.h" #include "xs_openssl.h" #include "xs_socket.h" diff --git a/utils.c b/utils.c index 189bd18..8594d84 100644 --- a/utils.c +++ b/utils.c @@ -3,7 +3,6 @@ #include "xs.h" #include "xs_io.h" -#include "xs_encdec.h" #include "xs_json.h" #include "xs_time.h" #include "xs_openssl.h" diff --git a/webfinger.c b/webfinger.c index 3471b49..ca4ed71 100644 --- a/webfinger.c +++ b/webfinger.c @@ -2,7 +2,6 @@ /* copyright (c) 2022 - 2023 grunfink / MIT license */ #include "xs.h" -#include "xs_encdec.h" #include "xs_json.h" #include "xs_curl.h" diff --git a/xs_encdec.h b/xs_encdec.h deleted file mode 100644 index 14cb36e..0000000 --- a/xs_encdec.h +++ /dev/null @@ -1,155 +0,0 @@ -/* copyright (c) 2022 - 2023 grunfink / MIT license */ - -#ifndef _XS_ENCDEC_H - -#define _XS_ENCDEC_H - - xs_str *xs_base64_enc(const xs_val *data, int sz); - xs_val *xs_base64_dec(const xs_str *data, int *size); - int xs_is_base64(const char *str); - - -#ifdef XS_IMPLEMENTATION - -/** base64 */ - -static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/="; - -xs_str *xs_base64_enc_tbl(const xs_val *data, int sz, const char *b64_tbl) -/* encodes data to base64 using a table */ -{ - xs_str *s; - unsigned char *p; - char *i; - int bsz, n; - - bsz = ((sz + 3 - 1) / 3) * 4; - i = s = xs_realloc(NULL, _xs_blk_size(bsz + 1)); - p = (unsigned char *)data; - - for (n = 0; n < sz; n += 3) { - int l = sz - n; - - if (l == 1) { - *i++ = b64_tbl[(p[n] >> 2) & 0x3f]; - *i++ = b64_tbl[(p[n] << 4) & 0x3f]; - *i++ = '='; - *i++ = '='; - } - else - if (l == 2) { - *i++ = b64_tbl[(p[n] >> 2) & 0x3f]; - *i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f]; - *i++ = b64_tbl[(p[n + 1] << 2) & 0x3f]; - *i++ = '='; - } - else { - *i++ = b64_tbl[(p[n] >> 2) & 0x3f]; - *i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f]; - *i++ = b64_tbl[(p[n + 1] << 2 | p[n + 2] >> 6) & 0x3f]; - *i++ = b64_tbl[(p[n + 2]) & 0x3f]; - } - } - - *i = '\0'; - - return s; -} - - -xs_str *xs_base64_enc(const xs_val *data, int sz) -/* encodes data to base64 */ -{ - return xs_base64_enc_tbl(data, sz, xs_b64_tbl); -} - - -xs_val *xs_base64_dec_tbl(const xs_str *data, int *size, const char *b64_tbl) -/* decodes data from base64 using a table */ -{ - xs_val *s = NULL; - int sz = 0; - char *p; - - p = (char *)data; - - /* size of data must be a multiple of 4 */ - if (strlen(p) % 4) - return NULL; - - for (p = (char *)data; *p; p += 4) { - int cs[4]; - int n; - unsigned char tmp[3]; - - for (n = 0; n < 4; n++) { - char *ss = strchr(b64_tbl, p[n]); - - if (ss == NULL) { - /* not a base64 char */ - return xs_free(s); - } - - cs[n] = ss - b64_tbl; - } - - n = 0; - - /* first byte */ - tmp[n++] = cs[0] << 2 | ((cs[1] >> 4) & 0x0f); - - /* second byte */ - if (cs[2] != 64) - tmp[n++] = cs[1] << 4 | ((cs[2] >> 2) & 0x3f); - - /* third byte */ - if (cs[3] != 64) - tmp[n++] = cs[2] << 6 | (cs[3] & 0x3f); - - /* must be done manually because data can be pure binary */ - s = xs_realloc(s, _xs_blk_size(sz + n)); - memcpy(s + sz, tmp, n); - sz += n; - } - - /* asciiz it to use it as a string */ - s = xs_realloc(s, _xs_blk_size(sz + 1)); - s[sz] = '\0'; - - *size = sz; - - return s; -} - - -xs_val *xs_base64_dec(const xs_str *data, int *size) -/* decodes data from base64 */ -{ - return xs_base64_dec_tbl(data, size, xs_b64_tbl); -} - - -int xs_is_base64_tbl(const char *str, const char *b64_tbl) -/* returns 1 if str is a base64 string, with table */ -{ - while (*str) { - if (strchr(b64_tbl, *str++) == NULL) - return 0; - } - - return 1; -} - - -int xs_is_base64(const char *str) -/* returns 1 if str is a base64 string */ -{ - return xs_is_base64_tbl(str, xs_b64_tbl); -} - - -#endif /* XS_IMPLEMENTATION */ - -#endif /* _XS_ENCDEC_H */ diff --git a/xs_openssl.h b/xs_openssl.h index 4a86046..d1a52f0 100644 --- a/xs_openssl.h +++ b/xs_openssl.h @@ -6,7 +6,15 @@ xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex); +#ifndef _XS_MD5_H #define xs_md5_hex(input, size) _xs_digest(input, size, "md5", 1) +#endif /* XS_MD5_H */ + +#ifndef _XS_BASE64_H +xs_str *xs_base64_enc(const xs_val *data, int sz); +xs_val *xs_base64_dec(const xs_str *data, int *size); +#endif /* XS_BASE64_H */ + #define xs_sha1_hex(input, size) _xs_digest(input, size, "sha1", 1) #define xs_sha256_hex(input, size) _xs_digest(input, size, "sha256", 1) #define xs_sha256_base64(input, size) _xs_digest(input, size, "sha256", 0) @@ -22,7 +30,9 @@ int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64 #include "openssl/pem.h" #include "openssl/evp.h" -#if 0 + +#ifndef _XS_BASE64_H + xs_str *xs_base64_enc(const xs_val *data, int sz) /* encodes data to base64 */ { @@ -77,7 +87,8 @@ xs_val *xs_base64_dec(const xs_str *data, int *size) return s; } -#endif + +#endif /* _XS_BASE64_H */ xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex) diff --git a/xs_version.h b/xs_version.h index e3b333f..099bc71 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* e0835629880a2846ad69c02a63a9209d5dd34945 */ +/* 1948fa3c5f0df994170cd38b9144b99734b071e6 */