Parsee/tools/b64.c
2024-09-12 20:11:54 +02:00

55 lines
1.3 KiB
C

/* b64.c - Generates a C string symbol stored as Base64
* ============================================================
* Under CC0, as its a rather useful example of a KappaChat tool.
* See LICENSE for more information about Parsee's licensing. */
#include <Cytoplasm/HashMap.h>
#include <Cytoplasm/Stream.h>
#include <Cytoplasm/Base64.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Array.h>
#include <stdlib.h>
int
Main(Array *args, HashMap *env)
{
char *file = ArrayGet(args, 1);
char *symbol = ArrayGet(args, 2);
char *out = ArrayGet(args, 3);
Stream *inStream;
uint8_t *bytes;
size_t len;
int o;
char *b64;
Stream *outStream;
if (!file || !symbol || !out)
{
return EXIT_FAILURE;
}
inStream = StreamOpen(file, "rb");
bytes = NULL;
len = 0;
while ((o = StreamGetc(inStream)) != EOF)
{
bytes = Realloc(bytes, len + 1);
bytes[len++] = o;
}
StreamClose(inStream);
b64 = Base64Encode((const char *) bytes, len);
Free(bytes);
outStream = StreamOpen(out, "w");
StreamPrintf(outStream, "const char %s[] = ", symbol);
StreamPrintf(outStream, "\"%s\";\n", b64);
Free(b64);
StreamFlush(outStream);
StreamClose(outStream);
(void) env;
return EXIT_SUCCESS;
}