summaryrefslogtreecommitdiffstats
path: root/src/shared
diff options
context:
space:
mode:
authorManuel Bentele2021-03-12 14:52:47 +0100
committerManuel Bentele2021-03-12 14:52:47 +0100
commit52a4ac3ede9b5bbc8015105d6fd5524c70e9bef5 (patch)
treec07cf0ae3d41709e0d1048b8324ee642c5e5cde3 /src/shared
parent[BUILD] Build picohttpparser as independent library (diff)
downloaddnbd3-52a4ac3ede9b5bbc8015105d6fd5524c70e9bef5.tar.gz
dnbd3-52a4ac3ede9b5bbc8015105d6fd5524c70e9bef5.tar.xz
dnbd3-52a4ac3ede9b5bbc8015105d6fd5524c70e9bef5.zip
[KERNEL] Refactor code to satisfy Linux kernel code style
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/serialize.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/shared/serialize.c b/src/shared/serialize.c
index 648b719..1f7cddd 100644
--- a/src/shared/serialize.c
+++ b/src/shared/serialize.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
#include <dnbd3/shared/serialize.h>
#include <dnbd3/types.h>
@@ -15,14 +16,17 @@ void serializer_reset_write(serialized_buffer_t *buffer)
uint8_t serializer_get_uint8(serialized_buffer_t *buffer)
{
- if (buffer->buffer_pointer + 1 > buffer->buffer_end) return 0;
+ if (buffer->buffer_pointer + 1 > buffer->buffer_end)
+ return 0;
return (uint8_t)*buffer->buffer_pointer++;
}
uint16_t serializer_get_uint16(serialized_buffer_t *buffer)
{
uint16_t ret;
- if (buffer->buffer_pointer + 2 > buffer->buffer_end) return 0;
+
+ if (buffer->buffer_pointer + 2 > buffer->buffer_end)
+ return 0;
memcpy(&ret, buffer->buffer_pointer, 2);
buffer->buffer_pointer += 2;
return net_order_16(ret);
@@ -31,7 +35,9 @@ uint16_t serializer_get_uint16(serialized_buffer_t *buffer)
uint64_t serializer_get_uint64(serialized_buffer_t *buffer)
{
uint64_t ret;
- if (buffer->buffer_pointer + 8 > buffer->buffer_end) return 0;
+
+ if (buffer->buffer_pointer + 8 > buffer->buffer_end)
+ return 0;
memcpy(&ret, buffer->buffer_pointer, 8);
buffer->buffer_pointer += 8;
return net_order_64(ret);
@@ -40,22 +46,29 @@ uint64_t serializer_get_uint64(serialized_buffer_t *buffer)
char *serializer_get_string(serialized_buffer_t *buffer)
{
char *ptr = buffer->buffer_pointer, *start = buffer->buffer_pointer;
- if (ptr >= buffer->buffer_end) return NULL;
- while (ptr < buffer->buffer_end && *ptr) ++ptr;
- if (*ptr) return NULL; // String did not terminate within buffer (possibly corrupted/malicious packet)
+
+ if (ptr >= buffer->buffer_end)
+ return NULL;
+ while (ptr < buffer->buffer_end && *ptr)
+ ++ptr;
+ // String did not terminate within buffer (possibly corrupted/malicious packet)
+ if (*ptr)
+ return NULL;
buffer->buffer_pointer = ptr + 1;
return start;
}
void serializer_put_uint8(serialized_buffer_t *buffer, uint8_t value)
{
- if (buffer->buffer_pointer + 1 > buffer->buffer_end) return;
+ if (buffer->buffer_pointer + 1 > buffer->buffer_end)
+ return;
*buffer->buffer_pointer++ = (char)value;
}
void serializer_put_uint16(serialized_buffer_t *buffer, uint16_t value)
{
- if (buffer->buffer_pointer + 2 > buffer->buffer_end) return;
+ if (buffer->buffer_pointer + 2 > buffer->buffer_end)
+ return;
value = net_order_16(value);
memcpy(buffer->buffer_pointer, &value, 2);
buffer->buffer_pointer += 2;
@@ -63,7 +76,8 @@ void serializer_put_uint16(serialized_buffer_t *buffer, uint16_t value)
void serializer_put_uint64(serialized_buffer_t *buffer, uint64_t value)
{
- if (buffer->buffer_pointer + 8 > buffer->buffer_end) return;
+ if (buffer->buffer_pointer + 8 > buffer->buffer_end)
+ return;
value = net_order_64(value);
memcpy(buffer->buffer_pointer, &value, 8);
buffer->buffer_pointer += 8;
@@ -72,12 +86,14 @@ void serializer_put_uint64(serialized_buffer_t *buffer, uint64_t value)
void serializer_put_string(serialized_buffer_t *buffer, const char *value)
{
const size_t len = strlen(value) + 1;
- if (buffer->buffer_pointer + len > buffer->buffer_end) return;
+
+ if (buffer->buffer_pointer + len > buffer->buffer_end)
+ return;
memcpy(buffer->buffer_pointer, value, len);
buffer->buffer_pointer += len;
}
uint32_t serializer_get_written_length(serialized_buffer_t *buffer)
{
- return (uint32_t)( buffer->buffer_pointer - buffer->buffer );
+ return (uint32_t)(buffer->buffer_pointer - buffer->buffer);
}