MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_cli_checksum_manifest_digest

Function cbm_cli_checksum_manifest_digest

src/cli/cli.c:7995–8078  ·  view source on GitHub ↗

Parse one downloaded checksum manifest without trusting line truncation or * substring matches. This non-header symbol is intentionally exercised by the * focused CLI regression tests. Duplicate entries are accepted only when they * name the exact artifact and normalize to the same SHA-256 digest. */

Source from the content-addressed store, hash-verified

7993 * focused CLI regression tests. Duplicate entries are accepted only when they
7994 * name the exact artifact and normalize to the same SHA-256 digest. */
7995int cbm_cli_checksum_manifest_digest(const char *manifest_path, const char *archive_name, char *out,
7996 size_t out_size) {
7997 if (out && out_size > 0) {
7998 out[0] = '\0';
7999 }
8000 if (!manifest_path || !archive_name || !archive_name[0] || !out || out_size < SHA256_BUF_SIZE ||
8001 strchr(archive_name, '\n') || strchr(archive_name, '\r')) {
8002 return CLI_ERR;
8003 }
8004
8005 FILE *fp = cbm_fopen(manifest_path, "rb");
8006 if (!fp) {
8007 return CLI_ERR;
8008 }
8009 unsigned char *manifest = malloc(CHECKSUM_MANIFEST_MAX_BYTES + 1U);
8010 if (!manifest) {
8011 (void)fclose(fp);
8012 return CLI_ERR;
8013 }
8014 size_t manifest_length = 0;
8015 bool read_ok = true;
8016 while (manifest_length <= CHECKSUM_MANIFEST_MAX_BYTES) {
8017 size_t capacity = CHECKSUM_MANIFEST_MAX_BYTES + 1U - manifest_length;
8018 size_t count = fread(manifest + manifest_length, 1, capacity, fp);
8019 manifest_length += count;
8020 if (ferror(fp)) {
8021 read_ok = false;
8022 break;
8023 }
8024 if (feof(fp)) {
8025 break;
8026 }
8027 if (count == 0) {
8028 read_ok = false;
8029 break;
8030 }
8031 }
8032 if (fclose(fp) != 0) {
8033 read_ok = false;
8034 }
8035 if (!read_ok || manifest_length == 0 || manifest_length > CHECKSUM_MANIFEST_MAX_BYTES ||
8036 memchr(manifest, '\0', manifest_length)) {
8037 free(manifest);
8038 return CLI_ERR;
8039 }
8040
8041 size_t archive_name_length = strlen(archive_name);
8042 char selected[SHA256_BUF_SIZE] = {0};
8043 bool found = false;
8044 const unsigned char *cursor = manifest;
8045 const unsigned char *end = manifest + manifest_length;
8046 while (cursor < end) {
8047 const unsigned char *newline = memchr(cursor, '\n', (size_t)(end - cursor));
8048 const unsigned char *line_end = newline ? newline : end;
8049 size_t line_length = (size_t)(line_end - cursor);
8050 if (line_length > 0 && cursor[line_length - 1U] == (unsigned char)'\r') {
8051 line_length--;
8052 }

Callers 2

verify_download_checksumFunction · 0.85
test_cli.cFile · 0.85

Calls 4

cbm_fopenFunction · 0.85
freadFunction · 0.85
cli_checksum_line_digestFunction · 0.85

Tested by

no test coverage detected