The previous code works ok but it can be lead to the reader
misunderstand its behavior.
To make it clear that error case is handled, some codes are rearranged.
Also, to clarify the error handling and flow of the code, switch
statement is replaced to if statement.
Change-Id: I65de34aab245c20ba3b6869cea1931edf99594d5
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
// to not use too many syscalls.
unsigned char buf[1 << 16];
- int r_read = read(dest_fd, buf, read_bytes < sizeof(buf) ? read_bytes : sizeof(buf));
- read_bytes -= r_read;
- switch (r_read) {
- case -1:
+ ssize_t r_read = read(dest_fd, buf, read_bytes < sizeof(buf) ? read_bytes : sizeof(buf));
+
+ if (r_read < 0)
return -1;
- case 0:
- // We read all the data. Let's check the results!
- // ... wait, there is no two-layer (switch and while) break in C.
- // Ugh, we have to goto instead.
- goto while_done;
- }
+
+ if (r_read == 0)
+ break;
+
+ read_bytes -= r_read;
SHA1Update(&context, buf, r_read);
}
-while_done:;
SHA1Final(sha1, &context);
return 0;