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; }
/** * Build OCSP URI string * * @v ocsp OCSP check * @ret rc Return status code */ static int ocsp_uri_string ( struct ocsp_check *ocsp ) { struct x509_ocsp_responder *responder = &ocsp->cert->extensions.auth_info.ocsp; char *base64; char *sep; size_t base64_len; size_t uri_len; size_t len; int rc; /* Sanity check */ if ( ! responder->uri.len ) { DBGC ( ocsp, "OCSP %p \"%s\" has no OCSP URI\n", ocsp, x509_name ( ocsp->cert ) ); rc = -ENOTTY; goto err_no_uri; } /* Calculate base64-encoded request length */ base64_len = ( base64_encoded_len ( ocsp->request.builder.len ) + 1 /* NUL */ ); /* Allocate and construct the base64-encoded request */ base64 = malloc ( base64_len ); if ( ! base64 ) { rc = -ENOMEM; goto err_alloc_base64; } base64_encode ( ocsp->request.builder.data, ocsp->request.builder.len, base64, base64_len ); /* Calculate URI-encoded base64-encoded request length */ uri_len = ( uri_encode ( URI_PATH, base64, ( base64_len - 1 /* NUL */ ), NULL, 0 ) + 1 /* NUL */ ); /* Allocate and construct the URI string */ len = ( responder->uri.len + 1 /* possible "/" */ + uri_len ); ocsp->uri_string = zalloc ( len ); if ( ! ocsp->uri_string ) { rc = -ENOMEM; goto err_alloc_uri; } memcpy ( ocsp->uri_string, responder->uri.data, responder->uri.len ); sep = &ocsp->uri_string[ responder->uri.len - 1 ]; if ( *sep != '/' ) *(++sep) = '/'; uri_encode ( URI_PATH, base64, base64_len, ( sep + 1 ), uri_len ); DBGC2 ( ocsp, "OCSP %p \"%s\" URI is %s\n", ocsp, x509_name ( ocsp->cert ), ocsp->uri_string ); /* Success */ rc = 0; err_alloc_uri: free ( base64 ); err_alloc_base64: err_no_uri: return rc; }
int nvram_set_file(const char *key, const char *fname, int max) { char *in; char *out; long len; int n; int r; if ((len = f_size(fname)) > max) return 0; max = (int)len; r = 0; if (f_read_alloc(fname, &in, max) == max) { if ((out = malloc(base64_encoded_len(max) + 128)) != NULL) { n = base64_encode(in, out, max); out[n] = 0; nvram_set(key, out); free(out); r = 1; } free(in); } return r; /* char a[2048]; char b[4096]; int n; if (((n = f_read(fname, &a, sizeof(a))) > 0) && (n <= max)) { n = base64_encode(a, b, n); b[n] = 0; nvram_set(key, b); return 1; } return 0; */ }
static void save(int quick) { int i; char *bi, *bo; int n; int b; char hgz[256]; char tmp[256]; char bak[256]; char bkp[256]; time_t now; struct tm *tms; static int lastbak = -1; //_dprintf("%s: quick=%d\n", __FUNCTION__, quick); f_write("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime), 0, 0); comp(speed_fn, speed, sizeof(speed[0]) * speed_count); /* if ((now = time(0)) < Y2K) { _dprintf("%s: time not set\n", __FUNCTION__); return; } */ comp(history_fn, &history, sizeof(history)); //_dprintf("%s: write source=%s\n", __FUNCTION__, save_path); f_write_string(source_fn, save_path, 0, 0); if (quick) { return; } sprintf(hgz, "%s.gz", history_fn); if (strcmp(save_path, "*nvram") == 0) { if (!wait_action_idle(10)) { _dprintf("%s: busy, not saving\n", __FUNCTION__); return; } if ((n = f_read_alloc(hgz, &bi, 20 * 1024)) > 0) { if ((bo = malloc(base64_encoded_len(n) + 1)) != NULL) { n = base64_encode(bi, bo, n); bo[n] = 0; nvram_set("rstats_data", bo); if (!nvram_match("debug_nocommit", "1")) nvram_commit(); _dprintf("%s: nvram commit\n", __FUNCTION__); free(bo); } } free(bi); } else if (save_path[0] != 0) { strcpy(tmp, save_path); strcat(tmp, ".tmp"); for (i = 15; i > 0; --i) { if (!wait_action_idle(10)) { _dprintf("%s: busy, not saving\n", __FUNCTION__); } else { _dprintf("%s: cp %s %s\n", __FUNCTION__, hgz, tmp); if (eval("cp", hgz, tmp) == 0) { _dprintf("%s: copy ok\n", __FUNCTION__); if (!nvram_match("rstats_bak", "0")) { now = time(0); tms = localtime(&now); if (lastbak != tms->tm_yday) { strcpy(bak, save_path); n = strlen(bak); if ((n > 3) && (strcmp(bak + (n - 3), ".gz") == 0)) n -= 3; strcpy(bkp, bak); for (b = HI_BACK-1; b > 0; --b) { sprintf(bkp + n, "_%d.bak", b + 1); sprintf(bak + n, "_%d.bak", b); rename(bak, bkp); } if (eval("cp", "-p", save_path, bak) == 0) lastbak = tms->tm_yday; } } _dprintf("%s: rename %s %s\n", __FUNCTION__, tmp, save_path); if (rename(tmp, save_path) == 0) { _dprintf("%s: rename ok\n", __FUNCTION__); break; } } } // might not be ready sleep(3); if (gotterm) break; } } }
static void save(int exiting) { int i; time_t now; char *bi, *bo; int n; char hgz[256]; char tmp[128]; _dprintf("save\n"); f_write("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime), 0, 0); comp(speed_fn, speed, sizeof(speed[0]) * speed_count); if ((now = time(0)) < Y2K) { _dprintf("time not set\n"); return; } comp(history_fn, &history, sizeof(history)); if (exiting) { _dprintf("exiting=1\n"); return; } _dprintf("save ext\n"); sprintf(hgz, "%s.gz", history_fn); if (strcmp(save_path, "*nvram") == 0) { if (!wait_action_idle(10)) { _dprintf("busy, not saving\n"); return; } if ((n = f_read_alloc(hgz, &bi, 20 * 1024)) > 0) { if ((bo = malloc(base64_encoded_len(n) + 1)) != NULL) { n = base64_encode(bi, bo, n); bo[n] = 0; _dprintf("rstats_data=%s\n", bo); nvram_set("rstats_data", bo); if (!nvram_match("debug_nocommit", "1")) nvram_commit(); free(bo); } } free(bi); } else if (save_path[0] != 0) { strcpy(tmp, save_path); strcat(tmp, "_tmp"); for (i = 15; i > 0; --i) { if (!wait_action_idle(10)) { _dprintf("busy, not saving\n"); } else { _dprintf("copy %s to %s\n", hgz, save_path); if (eval("cp", hgz, tmp) == 0) { _dprintf("copy ok\n"); if (rename(tmp, save_path) == 0) { _dprintf("rename ok\n"); break; } } } // might not be ready sleep(3); } } }