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. */
| 6661 | * 64-char hex digest + NUL to out. Returns 0 on success. Not static: |
| 6662 | * exercised directly by the self-update checksum regression test. */ |
| 6663 | int cbm_cli_sha256_file(const char *path, char *out, size_t out_size) { |
| 6664 | if (out_size < SHA256_BUF_SIZE) { |
| 6665 | return CLI_ERR; |
| 6666 | } |
| 6667 | FILE *fp = cbm_fopen(path, "rb"); |
| 6668 | if (!fp) { |
| 6669 | return CLI_ERR; |
| 6670 | } |
| 6671 | cbm_sha256_ctx ctx; |
| 6672 | cbm_sha256_init(&ctx); |
| 6673 | unsigned char buf[CLI_BUF_1K]; |
| 6674 | size_t n; |
| 6675 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 6676 | cbm_sha256_update(&ctx, buf, n); |
| 6677 | } |
| 6678 | int read_err = ferror(fp); |
| 6679 | fclose(fp); |
| 6680 | if (read_err) { |
| 6681 | return CLI_ERR; |
| 6682 | } |
| 6683 | uint8_t digest[CBM_SHA256_DIGEST_LEN]; |
| 6684 | cbm_sha256_final(&ctx, digest); |
| 6685 | static const char hex[] = "0123456789abcdef"; |
| 6686 | for (int i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { |
| 6687 | out[i * 2] = hex[digest[i] >> 4]; |
| 6688 | out[i * 2 + 1] = hex[digest[i] & 0x0f]; |
| 6689 | } |
| 6690 | out[SHA256_HEX_LEN] = '\0'; |
| 6691 | return 0; |
| 6692 | } |
| 6693 | |
| 6694 | static int cli_checksum_hex_nibble(unsigned char value) { |
| 6695 | if (value >= (unsigned char)'0' && value <= (unsigned char)'9') { |