Skip to content

Commit

Permalink
Update code for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Huitema authored and Christian Huitema committed Apr 12, 2024
1 parent 42f766a commit 396dded
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/mbedtls_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,24 +616,28 @@ int ptls_mbedtls_load_file(char const * file_name, unsigned char ** buf, size_t
F = fopen(file_name, "rb");
#endif

if (F != NULL) {
if (F == NULL) {
ret = PTLS_ERROR_NOT_AVAILABLE;
}
else {
long sz;
fseek(F, 0, SEEK_END);
sz = ftell(F);
if (sz > 0) {
if (sz <= 0) {
ret = PTLS_ERROR_NOT_AVAILABLE;
}
else {
*buf = (unsigned char *)malloc(sz + 1);
if (*buf == NULL){
ret = PTLS_ERROR_NO_MEMORY;
}
else {
size_t nb_read = 0;
*n = sz + 1;
*n = (size_t)sz + 1;

fseek(F, 0, SEEK_SET);
while(nb_read < sz){
unsigned char * position = *buf;
position += nb_read;
size_t bytes_read = fread(position, 1, sz - nb_read, F);
while(nb_read < (size_t)sz){
size_t bytes_read = fread((*buf) + nb_read, 1, sz - nb_read, F);
if (bytes_read > 0){
nb_read += bytes_read;
} else {
Expand All @@ -644,14 +648,8 @@ int ptls_mbedtls_load_file(char const * file_name, unsigned char ** buf, size_t
}
}
}
else {
ret = PTLS_ERROR_NOT_AVAILABLE;
}
(void)fclose(F);
}
else {
ret = PTLS_ERROR_NOT_AVAILABLE;
}
if (ret == 0){
(*buf)[(*n) - 1] = 0;
}
Expand Down

0 comments on commit 396dded

Please sign in to comment.