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. */
| 7896 | * 64-char hex digest + NUL to out. Returns 0 on success. Not static: |
| 7897 | * exercised directly by the self-update checksum regression test. */ |
| 7898 | int cbm_cli_sha256_file(const char *path, char *out, size_t out_size) { |
| 7899 | if (out_size < SHA256_BUF_SIZE) { |
| 7900 | return CLI_ERR; |
| 7901 | } |
| 7902 | FILE *fp = cbm_fopen(path, "rb"); |
| 7903 | if (!fp) { |
| 7904 | return CLI_ERR; |
| 7905 | } |
| 7906 | cbm_sha256_ctx ctx; |
| 7907 | cbm_sha256_init(&ctx); |
| 7908 | unsigned char buf[CLI_BUF_1K]; |
| 7909 | size_t n; |
| 7910 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 7911 | cbm_sha256_update(&ctx, buf, n); |
| 7912 | } |
| 7913 | int read_err = ferror(fp); |
| 7914 | fclose(fp); |
| 7915 | if (read_err) { |
| 7916 | return CLI_ERR; |
| 7917 | } |
| 7918 | uint8_t digest[CBM_SHA256_DIGEST_LEN]; |
| 7919 | cbm_sha256_final(&ctx, digest); |
| 7920 | static const char hex[] = "0123456789abcdef"; |
| 7921 | for (int i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { |
| 7922 | out[i * 2] = hex[digest[i] >> 4]; |
| 7923 | out[i * 2 + 1] = hex[digest[i] & 0x0f]; |
| 7924 | } |
| 7925 | out[SHA256_HEX_LEN] = '\0'; |
| 7926 | return 0; |
| 7927 | } |
| 7928 | |
| 7929 | static int cli_checksum_hex_nibble(unsigned char value) { |
| 7930 | if (value >= (unsigned char)'0' && value <= (unsigned char)'9') { |