refactor: change char_u to uint8_t or char in sha256.c/undo.c (#21914)

Use uint8_t for bytes in hashes as it doesn't make sense for them to be signed.
This commit is contained in:
zeertzjq
2023-01-21 08:58:57 +08:00
committed by GitHub
parent efe5ce6fa8
commit 1c16d0994d
5 changed files with 44 additions and 44 deletions

View File

@@ -5972,18 +5972,18 @@ static void ex_undo(exarg_T *eap)
static void ex_wundo(exarg_T *eap)
{
char hash[UNDO_HASH_SIZE];
uint8_t hash[UNDO_HASH_SIZE];
u_compute_hash(curbuf, (char_u *)hash);
u_write_undo(eap->arg, eap->forceit, curbuf, (char_u *)hash);
u_compute_hash(curbuf, hash);
u_write_undo(eap->arg, eap->forceit, curbuf, hash);
}
static void ex_rundo(exarg_T *eap)
{
char hash[UNDO_HASH_SIZE];
uint8_t hash[UNDO_HASH_SIZE];
u_compute_hash(curbuf, (char_u *)hash);
u_read_undo(eap->arg, (char_u *)hash, NULL);
u_compute_hash(curbuf, hash);
u_read_undo(eap->arg, hash, NULL);
}
/// ":redo".

View File

@@ -1554,7 +1554,7 @@ rewind_retry:
break;
}
if (read_undo_file) {
sha256_update(&sha_ctx, (char_u *)line_start, (size_t)len);
sha256_update(&sha_ctx, (uint8_t *)line_start, (size_t)len);
}
lnum++;
if (--read_count == 0) {
@@ -1610,7 +1610,7 @@ rewind_retry:
break;
}
if (read_undo_file) {
sha256_update(&sha_ctx, (char_u *)line_start, (size_t)len);
sha256_update(&sha_ctx, (uint8_t *)line_start, (size_t)len);
}
lnum++;
if (--read_count == 0) {
@@ -1667,7 +1667,7 @@ failed:
error = true;
} else {
if (read_undo_file) {
sha256_update(&sha_ctx, (char_u *)line_start, (size_t)len);
sha256_update(&sha_ctx, (uint8_t *)line_start, (size_t)len);
}
read_no_eol_lnum = ++lnum;
}
@@ -3196,7 +3196,7 @@ restore_backup:
// Keep it fast!
ptr = ml_get_buf(buf, lnum, false) - 1;
if (write_undo_file) {
sha256_update(&sha_ctx, (char_u *)ptr + 1, (uint32_t)(strlen(ptr + 1) + 1));
sha256_update(&sha_ctx, (uint8_t *)ptr + 1, (uint32_t)(strlen(ptr + 1) + 1));
}
while ((c = *++ptr) != NUL) {
if (c == NL) {
@@ -3605,10 +3605,10 @@ nofail:
// When writing the whole file and 'undofile' is set, also write the undo
// file.
if (retval == OK && write_undo_file) {
char hash[UNDO_HASH_SIZE];
uint8_t hash[UNDO_HASH_SIZE];
sha256_finish(&sha_ctx, (char_u *)hash);
u_write_undo(NULL, false, buf, (char_u *)hash);
sha256_finish(&sha_ctx, hash);
u_write_undo(NULL, false, buf, hash);
}
if (!should_abort(retval)) {

View File

@@ -32,10 +32,10 @@
}
#define PUT_UINT32(n, b, i) { \
(b)[(i)] = (char_u)((n) >> 24); \
(b)[(i) + 1] = (char_u)((n) >> 16); \
(b)[(i) + 2] = (char_u)((n) >> 8); \
(b)[(i) + 3] = (char_u)((n)); \
(b)[(i)] = (uint8_t)((n) >> 24); \
(b)[(i) + 1] = (uint8_t)((n) >> 16); \
(b)[(i) + 2] = (uint8_t)((n) >> 8); \
(b)[(i) + 3] = (uint8_t)((n)); \
}
void sha256_start(context_sha256_T *ctx)
@@ -53,7 +53,7 @@ void sha256_start(context_sha256_T *ctx)
ctx->state[7] = 0x5BE0CD19;
}
static void sha256_process(context_sha256_T *ctx, const char_u data[SHA256_BUFFER_SIZE])
static void sha256_process(context_sha256_T *ctx, const uint8_t data[SHA256_BUFFER_SIZE])
{
uint32_t temp1, temp2, W[SHA256_BUFFER_SIZE];
uint32_t A, B, C, D, E, F, G, H;
@@ -180,7 +180,7 @@ static void sha256_process(context_sha256_T *ctx, const char_u data[SHA256_BUFFE
ctx->state[7] += H;
}
void sha256_update(context_sha256_T *ctx, const char_u *input, size_t length)
void sha256_update(context_sha256_T *ctx, const uint8_t *input, size_t length)
{
if (length == 0) {
return;
@@ -198,7 +198,7 @@ void sha256_update(context_sha256_T *ctx, const char_u *input, size_t length)
size_t fill = SHA256_BUFFER_SIZE - left;
if (left && (length >= fill)) {
memcpy((void *)(ctx->buffer + left), (void *)input, fill);
memcpy(ctx->buffer + left, input, fill);
sha256_process(ctx, ctx->buffer);
length -= fill;
input += fill;
@@ -212,22 +212,22 @@ void sha256_update(context_sha256_T *ctx, const char_u *input, size_t length)
}
if (length) {
memcpy((void *)(ctx->buffer + left), (void *)input, length);
memcpy(ctx->buffer + left, input, length);
}
}
static char_u sha256_padding[SHA256_BUFFER_SIZE] = {
static uint8_t sha256_padding[SHA256_BUFFER_SIZE] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
void sha256_finish(context_sha256_T *ctx, char_u digest[SHA256_SUM_SIZE])
void sha256_finish(context_sha256_T *ctx, uint8_t digest[SHA256_SUM_SIZE])
{
uint32_t last, padn;
uint32_t high, low;
char_u msglen[8];
uint8_t msglen[8];
high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
low = (ctx->total[0] << 3);
@@ -265,7 +265,7 @@ void sha256_finish(context_sha256_T *ctx, char_u digest[SHA256_SUM_SIZE])
const char *sha256_bytes(const uint8_t *restrict buf, size_t buf_len, const uint8_t *restrict salt,
size_t salt_len)
{
char_u sha256sum[SHA256_SUM_SIZE];
uint8_t sha256sum[SHA256_SUM_SIZE];
static char hexit[SHA256_BUFFER_SIZE + 1]; // buf size + NULL
context_sha256_T ctx;
@@ -309,8 +309,8 @@ bool sha256_self_test(void)
{
char output[SHA256_BUFFER_SIZE + 1]; // buf size + NULL
context_sha256_T ctx;
char_u buf[1000];
char_u sha256sum[SHA256_SUM_SIZE];
uint8_t buf[1000];
uint8_t sha256sum[SHA256_SUM_SIZE];
const char *hexit;
static bool sha256_self_tested = false;

View File

@@ -12,7 +12,7 @@
typedef struct {
uint32_t total[2];
uint32_t state[8];
char_u buffer[SHA256_BUFFER_SIZE];
uint8_t buffer[SHA256_BUFFER_SIZE];
} context_sha256_T;
#ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@@ -631,13 +631,13 @@ static char e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
/// @param[in] buf The buffer used to compute the hash
/// @param[in] hash Array of size UNDO_HASH_SIZE in which to store the value of
/// the hash
void u_compute_hash(buf_T *buf, char_u *hash)
void u_compute_hash(buf_T *buf, uint8_t *hash)
{
context_sha256_T ctx;
sha256_start(&ctx);
for (linenr_T lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++) {
char *p = ml_get_buf(buf, lnum, false);
sha256_update(&ctx, (char_u *)p, (uint32_t)(strlen(p) + 1));
sha256_update(&ctx, (uint8_t *)p, strlen(p) + 1);
}
sha256_finish(&ctx, hash);
}
@@ -765,7 +765,7 @@ static void u_free_uhp(u_header_T *uhp)
/// @param hash The hash of the buffer contents
//
/// @returns false in case of an error.
static bool serialize_header(bufinfo_T *bi, char_u *hash)
static bool serialize_header(bufinfo_T *bi, uint8_t *hash)
FUNC_ATTR_NONNULL_ALL
{
buf_T *buf = bi->bi_buf;
@@ -788,7 +788,7 @@ static bool serialize_header(bufinfo_T *bi, char_u *hash)
undo_write_bytes(bi, (uintmax_t)buf->b_ml.ml_line_count, 4);
size_t len = buf->b_u_line_ptr ? strlen(buf->b_u_line_ptr) : 0;
undo_write_bytes(bi, len, 4);
if (len > 0 && !undo_write(bi, (char_u *)buf->b_u_line_ptr, len)) {
if (len > 0 && !undo_write(bi, (uint8_t *)buf->b_u_line_ptr, len)) {
return false;
}
undo_write_bytes(bi, (uintmax_t)buf->b_u_line_lnum, 4);
@@ -1053,7 +1053,7 @@ static bool serialize_uep(bufinfo_T *bi, u_entry_T *uep)
if (!undo_write_bytes(bi, len, 4)) {
return false;
}
if (len > 0 && !undo_write(bi, (char_u *)uep->ue_array[i], len)) {
if (len > 0 && !undo_write(bi, (uint8_t *)uep->ue_array[i], len)) {
return false;
}
}
@@ -1072,18 +1072,18 @@ static u_entry_T *unserialize_uep(bufinfo_T *bi, bool *error, const char *file_n
uep->ue_lcount = undo_read_4c(bi);
uep->ue_size = undo_read_4c(bi);
char_u **array = NULL;
char **array = NULL;
if (uep->ue_size > 0) {
if ((size_t)uep->ue_size < SIZE_MAX / sizeof(char *)) { // -V547
array = xmalloc(sizeof(char *) * (size_t)uep->ue_size);
memset(array, 0, sizeof(char *) * (size_t)uep->ue_size);
}
}
uep->ue_array = (char **)array;
uep->ue_array = array;
for (size_t i = 0; i < (size_t)uep->ue_size; i++) {
int line_len = undo_read_4c(bi);
char_u *line;
char *line;
if (line_len >= 0) {
line = undo_read_string(bi, (size_t)line_len);
} else {
@@ -1150,7 +1150,7 @@ static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
/// @param[in] buf Buffer for which undo file is written.
/// @param[in] hash Hash value of the buffer text. Must have #UNDO_HASH_SIZE
/// size.
void u_write_undo(const char *const name, const bool forceit, buf_T *const buf, char_u *const hash)
void u_write_undo(const char *const name, const bool forceit, buf_T *const buf, uint8_t *const hash)
FUNC_ATTR_NONNULL_ARG(3, 4)
{
char *file_name;
@@ -1359,7 +1359,7 @@ theend:
/// a bit more verbose.
/// Otherwise use curbuf->b_ffname to generate the undo file name.
/// "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
void u_read_undo(char *name, const char_u *hash, const char *orig_name FUNC_ATTR_UNUSED)
void u_read_undo(char *name, const uint8_t *hash, const char *orig_name FUNC_ATTR_UNUSED)
FUNC_ATTR_NONNULL_ARG(2)
{
u_header_T **uhp_table = NULL;
@@ -1426,8 +1426,8 @@ void u_read_undo(char *name, const char_u *hash, const char *orig_name FUNC_ATTR
goto error;
}
char read_hash[UNDO_HASH_SIZE];
if (!undo_read(&bi, (char_u *)read_hash, UNDO_HASH_SIZE)) {
uint8_t read_hash[UNDO_HASH_SIZE];
if (!undo_read(&bi, read_hash, UNDO_HASH_SIZE)) {
corruption_error("hash", file_name);
goto error;
}
@@ -1453,7 +1453,7 @@ void u_read_undo(char *name, const char_u *hash, const char *orig_name FUNC_ATTR
}
if (str_len > 0) {
line_ptr = (char *)undo_read_string(&bi, (size_t)str_len);
line_ptr = undo_read_string(&bi, (size_t)str_len);
}
linenr_T line_lnum = (linenr_T)undo_read_4c(&bi);
colnr_T line_colnr = (colnr_T)undo_read_4c(&bi);
@@ -1743,10 +1743,10 @@ static bool undo_read(bufinfo_T *bi, uint8_t *buffer, size_t size)
/// @param len can be zero to allocate an empty line.
///
/// @returns a pointer to allocated memory or NULL in case of an error.
static uint8_t *undo_read_string(bufinfo_T *bi, size_t len)
static char *undo_read_string(bufinfo_T *bi, size_t len)
{
uint8_t *ptr = xmallocz(len);
if (len > 0 && !undo_read(bi, ptr, len)) {
char *ptr = xmallocz(len);
if (len > 0 && !undo_read(bi, (uint8_t *)ptr, len)) {
xfree(ptr);
return NULL;
}