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. */
| 6854 | * 64-char hex digest + NUL to out. Returns 0 on success. Not static: |
| 6855 | * exercised directly by the self-update checksum regression test. */ |
| 6856 | int cbm_cli_sha256_file(const char *path, char *out, size_t out_size) { |
| 6857 | if (out_size < SHA256_BUF_SIZE) { |
| 6858 | return CLI_ERR; |
| 6859 | } |
| 6860 | FILE *fp = cbm_fopen(path, "rb"); |
| 6861 | if (!fp) { |
| 6862 | return CLI_ERR; |
| 6863 | } |
| 6864 | cbm_sha256_ctx ctx; |
| 6865 | cbm_sha256_init(&ctx); |
| 6866 | unsigned char buf[CLI_BUF_1K]; |
| 6867 | size_t n; |
| 6868 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 6869 | cbm_sha256_update(&ctx, buf, n); |
| 6870 | } |
| 6871 | int read_err = ferror(fp); |
| 6872 | fclose(fp); |
| 6873 | if (read_err) { |
| 6874 | return CLI_ERR; |
| 6875 | } |
| 6876 | uint8_t digest[CBM_SHA256_DIGEST_LEN]; |
| 6877 | cbm_sha256_final(&ctx, digest); |
| 6878 | static const char hex[] = "0123456789abcdef"; |
| 6879 | for (int i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { |
| 6880 | out[i * 2] = hex[digest[i] >> 4]; |
| 6881 | out[i * 2 + 1] = hex[digest[i] & 0x0f]; |
| 6882 | } |
| 6883 | out[SHA256_HEX_LEN] = '\0'; |
| 6884 | return 0; |
| 6885 | } |
| 6886 | |
| 6887 | static int cli_checksum_hex_nibble(unsigned char value) { |
| 6888 | if (value >= (unsigned char)'0' && value <= (unsigned char)'9') { |