Compute the SHA-256 of a file in-process (no external hashing tool — those * differ per OS, may be absent, and mis-quote paths under cmd.exe). Writes a * 64-char hex digest + NUL to out. Returns 0 on success. Not static: * exercised directly by the self-update checksum regression test. */
| 2974 | * 64-char hex digest + NUL to out. Returns 0 on success. Not static: |
| 2975 | * exercised directly by the self-update checksum regression test. */ |
| 2976 | int cbm_cli_sha256_file(const char *path, char *out, size_t out_size) { |
| 2977 | if (out_size < SHA256_BUF_SIZE) { |
| 2978 | return CLI_ERR; |
| 2979 | } |
| 2980 | FILE *fp = cbm_fopen(path, "rb"); |
| 2981 | if (!fp) { |
| 2982 | return CLI_ERR; |
| 2983 | } |
| 2984 | cbm_sha256_ctx ctx; |
| 2985 | cbm_sha256_init(&ctx); |
| 2986 | unsigned char buf[CLI_BUF_1K]; |
| 2987 | size_t n; |
| 2988 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 2989 | cbm_sha256_update(&ctx, buf, n); |
| 2990 | } |
| 2991 | int read_err = ferror(fp); |
| 2992 | fclose(fp); |
| 2993 | if (read_err) { |
| 2994 | return CLI_ERR; |
| 2995 | } |
| 2996 | uint8_t digest[CBM_SHA256_DIGEST_LEN]; |
| 2997 | cbm_sha256_final(&ctx, digest); |
| 2998 | static const char hex[] = "0123456789abcdef"; |
| 2999 | for (int i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { |
| 3000 | out[i * 2] = hex[digest[i] >> 4]; |
| 3001 | out[i * 2 + 1] = hex[digest[i] & 0x0f]; |
| 3002 | } |
| 3003 | out[SHA256_HEX_LEN] = '\0'; |
| 3004 | return 0; |
| 3005 | } |
| 3006 | |
| 3007 | /* ── Download helper (shell-free curl via exec) ───────────────── */ |
| 3008 |