# HG changeset patch # User Paul Boddie # Date 1615671513 -3600 # Node ID 54dcc50bf1930ff7717ad60d619feb9ac34b09a9 # Parent 14888a561fb945c98ac84f035815e1d525e48e93 Added convenience functions to access strings in mapped memory. Currently, the opener context duplicates the string retrieval function internally. diff -r 14888a561fb9 -r 54dcc50bf193 dstest_host_client.cc --- a/dstest_host_client.cc Sat Mar 13 17:49:23 2021 +0100 +++ b/dstest_host_client.cc Sat Mar 13 22:38:33 2021 +0100 @@ -66,7 +66,7 @@ /* Write the filename. */ - strcpy(context.memory, filename); + file_string_set(&context, filename, 0); /* Invoke the open method to receive the file reference. */ diff -r 14888a561fb9 -r 54dcc50bf193 file.cc --- a/file.cc Sat Mar 13 17:49:23 2021 +0100 +++ b/file.cc Sat Mar 13 22:38:33 2021 +0100 @@ -22,6 +22,8 @@ #include #include +#include + #include "dataspace_client.h" #include "opener_client.h" #include "opener_context_client.h" @@ -76,6 +78,17 @@ file->ref = L4_INVALID_CAP; } +/* Open a file using the given structure and context. */ + +long file_open(file_t *file, file_t *context) +{ + client_OpenerContext openercontext(context->ref); + file_init(file); + return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); +} + + + /* Map a region of the given file to a memory region. */ long file_mmap(file_t *file, offset_t position, offset_t length) @@ -86,16 +99,7 @@ if (err) return err; - return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory); -} - -/* Open a file using the given structure and context. */ - -long file_open(file_t *file, file_t *context) -{ - client_OpenerContext openercontext(context->ref); - file_init(file); - return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); + return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); } /* Return the amount of data in the mapped region for the given file. */ @@ -113,4 +117,50 @@ return file->end_pos - file->start_pos; } + + +/* Get a pointer to any terminated string at the given offset or NULL if the + data from offset is not terminated. */ + +char *file_string_get(file_t *file, offset_t offset) +{ + offset_t limit = file_span(file) - offset; + + if (strnlen(file->memory + offset, limit) < limit) + return file->memory + offset; + else + return NULL; +} + +/* Copy a string to the mapped region at the given offset, returning the number + of copied characters excluding the zero terminator. */ + +offset_t file_string_set(file_t *file, const char *data, offset_t offset) +{ + offset_t i, pos, limit = file_span(file) - 1; + + /* Do not attempt to copy data with an invalid offset. */ + + if (offset > limit) + return 0; + + /* Copy the data to the given offset, stopping before the final byte of the + region. */ + + for (i = 0, pos = offset; pos < limit; i++, pos++) + { + file->memory[pos] = data[i]; + + /* Terminator written, can return immediately. */ + + if (!data[i]) + return pos - offset - 1; + } + + /* Terminate the incomplete string at the end of the region. */ + + file->memory[limit] = '\0'; + return limit - offset; +} + // vim: tabstop=2 expandtab shiftwidth=2 diff -r 14888a561fb9 -r 54dcc50bf193 file.h --- a/file.h Sat Mar 13 17:49:23 2021 +0100 +++ b/file.h Sat Mar 13 22:38:33 2021 +0100 @@ -65,6 +65,11 @@ offset_t file_populated_span(file_t *file); offset_t file_span(file_t *file); +/* Convenience functions. */ + +char *file_string_get(file_t *file, offset_t offset); +offset_t file_string_set(file_t *file, const char *data, offset_t offset); + EXTERN_C_END // vim: tabstop=2 expandtab shiftwidth=2