mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-03 09:23:23 +08:00
c11e5a9383
Fixes: mongoose/mongoose.c:180:8: warning: no previous prototype for ‘mg_dns_parse_name’ [-Wmissing-prototypes] 180 | size_t mg_dns_parse_name(const uint8_t *s, size_t n, size_t ofs, char *dst, | ^~~~~~~~~~~~~~~~~ mongoose/mongoose.c:306:6: warning: no previous prototype for ‘mg_dns_send’ [-Wmissing-prototypes] 306 | void mg_dns_send(struct mg_connection *c, const struct mg_str *name, | ^~~~~~~~~~~ mongoose/mongoose.c:925:6: warning: no previous prototype for ‘mg_http_parse_headers’ [-Wmissing-prototypes] 925 | void mg_http_parse_headers(const char *s, const char *end, | ^~~~~~~~~~~~~~~~~~~~~ mongoose/mongoose.c:1125:7: warning: no previous prototype for ‘mg_http_etag’ [-Wmissing-prototypes] 1125 | char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) { | ^~~~~~~~~~~~ mongoose/mongoose.c:2578:6: warning: no previous prototype for ‘mg_sha1_transform’ [-Wmissing-prototypes] 2578 | void mg_sha1_transform(uint32_t state[5], const unsigned char buffer[64]) { | ^~~~~~~~~~~~~~~~~ mongoose/mongoose.c:2976:8: warning: no previous prototype for ‘mg_open_listener’ [-Wmissing-prototypes] 2976 | SOCKET mg_open_listener(const char *url, struct mg_addr *addr) { | ^~~~~~~~~~~~~~~~ Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
85 lines
2.0 KiB
C
85 lines
2.0 KiB
C
#include "base64.h"
|
|
#include <string.h>
|
|
|
|
static int mg_b64idx(int c) {
|
|
if (c < 26) {
|
|
return c + 'A';
|
|
} else if (c < 52) {
|
|
return c - 26 + 'a';
|
|
} else if (c < 62) {
|
|
return c - 52 + '0';
|
|
} else {
|
|
return c == 62 ? '+' : '/';
|
|
}
|
|
}
|
|
|
|
static int mg_b64rev(int c) {
|
|
if (c >= 'A' && c <= 'Z') {
|
|
return c - 'A';
|
|
} else if (c >= 'a' && c <= 'z') {
|
|
return c + 26 - 'a';
|
|
} else if (c >= '0' && c <= '9') {
|
|
return c + 52 - '0';
|
|
} else if (c == '+') {
|
|
return 62;
|
|
} else if (c == '/') {
|
|
return 63;
|
|
} else if (c == '=') {
|
|
return 64;
|
|
} else {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
int mg_base64_update(unsigned char ch, char *to, int n) {
|
|
int rem = (n & 3) % 3;
|
|
if (rem == 0) {
|
|
to[n] = (char) mg_b64idx(ch >> 2);
|
|
to[++n] = (char) ((ch & 3) << 4);
|
|
} else if (rem == 1) {
|
|
to[n] = (char) mg_b64idx(to[n] | (ch >> 4));
|
|
to[++n] = (char) ((ch & 15) << 2);
|
|
} else {
|
|
to[n] = (char) mg_b64idx(to[n] | (ch >> 6));
|
|
to[++n] = (char) mg_b64idx(ch & 63);
|
|
n++;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
int mg_base64_final(char *to, int n) {
|
|
int saved = n;
|
|
// printf("---[%.*s]\n", n, to);
|
|
if (n & 3) n = mg_base64_update(0, to, n);
|
|
if ((saved & 3) == 2) n--;
|
|
// printf(" %d[%.*s]\n", n, n, to);
|
|
while (n & 3) to[n++] = '=';
|
|
to[n] = '\0';
|
|
return n;
|
|
}
|
|
|
|
int mg_base64_encode(const unsigned char *p, int n, char *to) {
|
|
int i, len = 0;
|
|
for (i = 0; i < n; i++) len = mg_base64_update(p[i], to, len);
|
|
len = mg_base64_final(to, len);
|
|
return len;
|
|
}
|
|
|
|
int mg_base64_decode(const char *src, int n, char *dst) {
|
|
const char *end = src + n;
|
|
int len = 0;
|
|
while (src + 3 < end) {
|
|
int a = mg_b64rev(src[0]), b = mg_b64rev(src[1]), c = mg_b64rev(src[2]),
|
|
d = mg_b64rev(src[3]);
|
|
if (a == 64 || a < 0 || b == 64 || b < 0 || c < 0 || d < 0) return 0;
|
|
dst[len++] = (char) ((a << 2) | (b >> 4));
|
|
if (src[2] != '=') {
|
|
dst[len++] = (char) ((b << 4) | (c >> 2));
|
|
if (src[3] != '=') dst[len++] = (char) ((c << 6) | d);
|
|
}
|
|
src += 4;
|
|
}
|
|
dst[len] = '\0';
|
|
return len;
|
|
}
|