tftp
- Remove the suggestion of null terimination of the custom_fread custom_fwrite functions - Update the tests
This commit is contained in:
@@ -73,17 +73,17 @@ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
|
|||||||
* auto rolling over the offset
|
* auto rolling over the offset
|
||||||
* if the bytes to read is bigger than the remaining bytes
|
* if the bytes to read is bigger than the remaining bytes
|
||||||
* it will read the remaining bytes and set the bytes to 0
|
* it will read the remaining bytes and set the bytes to 0
|
||||||
|
* this function does not handle null termination
|
||||||
* @param[out] buf The buffer to write the data to
|
* @param[out] buf The buffer to write the data to
|
||||||
* @param[in] bytes The number of bytes to read
|
* @param[in] bytes The number of bytes to read
|
||||||
* @param[in,out] handle Custom file handles
|
* @param[in,out] handle Custom file handles
|
||||||
*/
|
*/
|
||||||
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
||||||
if (handle->offset + bytes >= handle->len) {
|
if (handle->offset + bytes > handle->len) {
|
||||||
bytes = handle->len - handle->offset - 1;
|
bytes = handle->len - handle->offset;
|
||||||
}
|
}
|
||||||
memcpy(buf, handle->data + handle->offset, bytes);
|
memcpy(buf, handle->data + handle->offset, bytes);
|
||||||
handle->offset += bytes;
|
handle->offset += bytes;
|
||||||
((char*)buf)[bytes] = '\0';
|
|
||||||
if (handle->offset > handle->len) {
|
if (handle->offset > handle->len) {
|
||||||
bytes = 0;
|
bytes = 0;
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,12 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief tftp custom file functions to write the data
|
* @brief tftp custom file functions to write the data
|
||||||
* auto rolling over the offset
|
* auto rolling over the offset
|
||||||
|
* if the bytes to write is bigger than the remaining bytes
|
||||||
|
* it will write the remaining bytes and set the bytes to 0
|
||||||
|
* this function does not handle null termination
|
||||||
|
* if you want to write a null termination you have to add it to the data
|
||||||
|
* and set the len to the new length before calling this function
|
||||||
*
|
*
|
||||||
* @param buf The buffer to write the data to
|
* @param buf The buffer to write the data to
|
||||||
* @param bytes The number of bytes to write
|
* @param bytes The number of bytes to write
|
||||||
@@ -100,8 +105,8 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
|||||||
* @return The number of bytes written
|
* @return The number of bytes written
|
||||||
*/
|
*/
|
||||||
size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
||||||
if (handle->offset + bytes >= handle->len) {
|
if (handle->offset + bytes > handle->len) {
|
||||||
bytes = handle->len - handle->offset - 1;
|
bytes = handle->len - handle->offset;
|
||||||
}
|
}
|
||||||
memcpy(handle->data + handle->offset, buf, bytes);
|
memcpy(handle->data + handle->offset, buf, bytes);
|
||||||
handle->offset += bytes;
|
handle->offset += bytes;
|
||||||
|
|||||||
@@ -77,20 +77,27 @@ TEST(TFTP, custom_fwrite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(TFTP, custom_fread_fwrite_suggested_by_Lorenz) {
|
TEST(TFTP, custom_fread_fwrite_suggested_by_Lorenz) {
|
||||||
|
char buf[21];
|
||||||
write_file.data = (char*)malloc(21 * sizeof(char));
|
write_file.data = (char*)malloc(21 * sizeof(char));
|
||||||
write_file.len = 21;
|
write_file.len = 21;
|
||||||
write_file.offset = 0;
|
write_file.offset = 0;
|
||||||
tftp_custom_fwrite("1234567890123456789", 19, &write_file);
|
tftp_custom_fwrite("1234567890123456789", 19, &write_file);
|
||||||
|
|
||||||
tftp_custom_fwrite("0987654321", 10, &write_file);
|
tftp_custom_fwrite("0987654321", 11, &write_file);
|
||||||
EXPECT_EQ(write_file.offset, 20);
|
// the above function does not write the null terminator
|
||||||
|
EXPECT_EQ(write_file.offset, 21);
|
||||||
EXPECT_EQ(write_file.len, 21);
|
EXPECT_EQ(write_file.len, 21);
|
||||||
EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 21), 0);
|
EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 20), 0);
|
||||||
|
|
||||||
|
tftp_custom_fseek(&write_file, 0, SEEK_SET);
|
||||||
|
tftp_custom_fread(buf, 20, &write_file);
|
||||||
|
// the above function does not write the null terminator
|
||||||
|
buf[20] = '\0';
|
||||||
|
EXPECT_EQ(memcmp(buf, "12345678901234567890", 21), 0);
|
||||||
free(write_file.data);
|
free(write_file.data);
|
||||||
write_file.data = NULL;
|
write_file.data = NULL;
|
||||||
write_file.len = 0;
|
write_file.len = 0;
|
||||||
|
|
||||||
char buf[21];
|
|
||||||
file.offset = 9;
|
file.offset = 9;
|
||||||
tftp_custom_fread(buf, 20, &file);
|
tftp_custom_fread(buf, 20, &file);
|
||||||
EXPECT_EQ(memcmp(buf, "0", 2), 0);
|
EXPECT_EQ(memcmp(buf, "0", 2), 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user