- Remove the suggestion of null terimination of the custom_fread custom_fwrite functions
- Update the tests
This commit is contained in:
2023-12-06 14:58:41 +01:00
parent fc997e1aac
commit 0023659552
2 changed files with 22 additions and 10 deletions

View File

@@ -73,17 +73,17 @@ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
* auto rolling over the offset
* if the bytes to read is bigger than the remaining bytes
* 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[in] bytes The number of bytes to read
* @param[in,out] handle Custom file handles
*/
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
if (handle->offset + bytes >= handle->len) {
bytes = handle->len - handle->offset - 1;
if (handle->offset + bytes > handle->len) {
bytes = handle->len - handle->offset;
}
memcpy(buf, handle->data + handle->offset, bytes);
handle->offset += bytes;
((char*)buf)[bytes] = '\0';
if (handle->offset > handle->len) {
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
* 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 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
*/
size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) {
if (handle->offset + bytes >= handle->len) {
bytes = handle->len - handle->offset - 1;
if (handle->offset + bytes > handle->len) {
bytes = handle->len - handle->offset;
}
memcpy(handle->data + handle->offset, buf, bytes);
handle->offset += bytes;