Repository: valkey-io/valkey
File: src/util.c (C)

Code before:
            if (strchr(buf, '.') != NULL) {
                char *p = buf + l - 1;
                while (*p == '0') {
                    p--;
                    l--;
                }
                if (*p == '.') l--;
            }
            if (l == 2 && buf[0] == '-' && buf[1] == '0') {
                buf[0] = '0';
                l = 1;
            }
            break;
        default: goto err; /* Invalid mode. */
        }
    }
    buf[l] = '\0';
    return l;
err:
    /* force add Null termination */
    if (len > 0) buf[0] = '\0';
    return 0;
}

/* Populate the provided seed array by hashing the provided string with SHA1
 * and copying the first 16 bytes of the digest into the seed buffer. */
void getHashSeedFromString(unsigned char *seed_array, size_t outlen, const char *value) {
    SHA256_CTX ctx;
    unsigned char digest[SHA256_BLOCK_SIZE];

    sha256_init(&ctx);
    sha256_update(&ctx, (const BYTE *)value, strlen(value));
    sha256_final(&ctx, digest);

    if (outlen > SHA256_BLOCK_SIZE) outlen = SHA256_BLOCK_SIZE;
    memcpy(seed_array, digest, outlen);
}


/* Parses a version string on the form "major.minor.patch" and returns an
 * integer on the form 0xMMmmpp. Returns -1 on parse error. */
int version2num(const char *version) {
    int v = 0, part = 0, numdots = 0;
    const char *p = version;
    do {
        if (*p >= '0' && *p <= '9') {
            part = part * 10 + (unsigned)(*p - '0');
            if (part > 255) return -1;
        } else if (*p == '.') {
            if (++numdots > 2) return -1;
            v = (v << 8) | part;

Review comment (suggestion):
To match the implementation below...

```suggestion
/* Populate the provided seed array by hashing the provided string with SHA256
 * and copying the first outlen bytes of the digest into the seed buffer. */
```

Code after:
            if (strchr(buf, '.') != NULL) {
                char *p = buf + l - 1;
                while (*p == '0') {
                    p--;
                    l--;
                }
                if (*p == '.') l--;
            }
            if (l == 2 && buf[0] == '-' && buf[1] == '0') {
                buf[0] = '0';
                l = 1;
            }
            break;
        default: goto err; /* Invalid mode. */
        }
    }
    buf[l] = '\0';
    return l;
err:
    /* force add Null termination */
    if (len > 0) buf[0] = '\0';
    return 0;
}

/* Populate the provided seed array by hashing the provided string with SHA256
 * and copying the first outlen bytes of the digest into the seed buffer. */
void getHashSeedFromString(unsigned char *seed_array, size_t outlen, const char *value) {
    SHA256_CTX ctx;
    unsigned char digest[SHA256_BLOCK_SIZE];

    sha256_init(&ctx);
    sha256_update(&ctx, (const BYTE *)value, strlen(value));
    sha256_final(&ctx, digest);

    if (outlen > SHA256_BLOCK_SIZE) outlen = SHA256_BLOCK_SIZE;
    memcpy(seed_array, digest, outlen);
}


/* Parses a version string on the form "major.minor.patch" and returns an
 * integer on the form 0xMMmmpp. Returns -1 on parse error. */
int version2num(const char *version) {
    int v = 0, part = 0, numdots = 0;
    const char *p = version;
    do {
        if (*p >= '0' && *p <= '9') {
            part = part * 10 + (unsigned)(*p - '0');
            if (part > 255) return -1;
        } else if (*p == '.') {
            if (++numdots > 2) return -1;
            v = (v << 8) | part;