/* 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 #include #include #include #include #include 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); return EXIT_SUCCESS; }