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. */
| 6711 | * 64-char hex digest + NUL to out. Returns 0 on success. Not static: |
| 6712 | * exercised directly by the self-update checksum regression test. */ |
| 6713 | int cbm_cli_sha256_file(const char *path, char *out, size_t out_size) { |
| 6714 | if (out_size < SHA256_BUF_SIZE) { |
| 6715 | return CLI_ERR; |
| 6716 | } |
| 6717 | FILE *fp = cbm_fopen(path, "rb"); |
| 6718 | if (!fp) { |
| 6719 | return CLI_ERR; |
| 6720 | } |
| 6721 | cbm_sha256_ctx ctx; |
| 6722 | cbm_sha256_init(&ctx); |
| 6723 | unsigned char buf[CLI_BUF_1K]; |
| 6724 | size_t n; |
| 6725 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 6726 | cbm_sha256_update(&ctx, buf, n); |
| 6727 | } |
| 6728 | int read_err = ferror(fp); |
| 6729 | fclose(fp); |
| 6730 | if (read_err) { |
| 6731 | return CLI_ERR; |
| 6732 | } |
| 6733 | uint8_t digest[CBM_SHA256_DIGEST_LEN]; |
| 6734 | cbm_sha256_final(&ctx, digest); |
| 6735 | static const char hex[] = "0123456789abcdef"; |
| 6736 | for (int i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { |
| 6737 | out[i * 2] = hex[digest[i] >> 4]; |
| 6738 | out[i * 2 + 1] = hex[digest[i] & 0x0f]; |
| 6739 | } |
| 6740 | out[SHA256_HEX_LEN] = '\0'; |
| 6741 | return 0; |
| 6742 | } |
| 6743 | |
| 6744 | static int cli_checksum_hex_nibble(unsigned char value) { |
| 6745 | if (value >= (unsigned char)'0' && value <= (unsigned char)'9') { |