int nvram_get_file(const char *key, const char *fname, int max) { int n; char *p; char *b; int r; r = 0; p = nvram_safe_get(key); n = strlen(p); if (n <= max) { if ((b = malloc(base64_decoded_len(n) + 128)) != NULL) { n = base64_decode(p, b, n); if (n > 0) r = (f_write(fname, b, n, 0, 0644) == n); free(b); } } return r; /* char b[2048]; int n; char *p; p = nvram_safe_get(key); n = strlen(p); if (n <= max) { n = base64_decode(p, b, n); if (n > 0) return (f_write(fname, b, n, 0, 0700) == n); } return 0; */ }
static char* test_encode_and_decode_base64() { load_histograms(); uint8_t* buffer = NULL; uint8_t* decoded = NULL; char* encoded = NULL; size_t len = 0; int rc = 0; rc = hdr_encode_compressed(cor_histogram, &buffer, &len); mu_assert("Did not encode", validate_return_code(rc)); size_t encoded_len = base64_encoded_len(len); size_t decoded_len = base64_decoded_len(encoded_len); encoded = calloc(encoded_len + 1, sizeof(char)); decoded = calloc(decoded_len, sizeof(uint8_t)); base64_encode(buffer, len, encoded, encoded_len); base64_decode(encoded, encoded_len, decoded, decoded_len); mu_assert("Should be same", memcmp(buffer, decoded, len) == 0); return 0; }
static void load(long uptime) { int i; long t; char *bi, *bo; int n; char hgz[256]; unsigned char mac[6]; strlcpy(save_path, nvram_safe_get("rstats_path"), sizeof(save_path) - 32); if (((n = strlen(save_path)) > 0) && (save_path[n - 1] == '/')) { ether_atoe(nvram_safe_get("et0macaddr"), mac); sprintf(save_path + n, "ddwrt_rstats_%02x%02x%02x%02x%02x%02x.gz", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } if (f_read("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime)) != sizeof(save_utime)) { save_utime = 0; } t = uptime + get_stime(); if ((save_utime < uptime) || (save_utime > t)) save_utime = t; _dprintf("uptime = %dm, save_utime = %dm\n", uptime / 60, save_utime / 60); speed_count = decomp(speed_fn, speed, sizeof(speed[0]), MAX_SPEED_IF); _dprintf("load speed_count = %d\n", speed_count); if ((load_history()) || (save_path[0] == 0)) return; sprintf(hgz, "%s.gz", history_fn); if (strcmp(save_path, "*nvram") == 0) { if (!wait_action_idle(60)) exit(0); bi = nvram_safe_get("rstats_data"); if ((n = strlen(bi)) > 0) { if ((bo = malloc(base64_decoded_len(n))) != NULL) { n = base64_decode(bi, bo, n); f_write(hgz, bo, n, 0, 0); free(bo); _dprintf("from nvram = %d\n", n); } } } else { for (i = 30; i > 0; --i) { if (!wait_action_idle(60)) exit(0); _dprintf("copy %s to %s\n", save_path, hgz); if (eval("cp", save_path, hgz) == 0) break; // not ready... sleep(3); } if (i == 0) { save_utime = uptime + 60; _dprintf("forcing early save on next\n"); } } load_history(); }