Fix babeltrace-log incorrect timestamp type
[babeltrace.git] / formats / ctf / types / string.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Strings read/write functions.
5 *
ccd7e1c8 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
de0ba614 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
6dc2ca62
MD
17 */
18
847bf71a 19#include <babeltrace/babeltrace.h>
d79865b9 20#include <babeltrace/ctf/types.h>
a3dbc794 21#include <limits.h> /* C99 limits */
6dc2ca62
MD
22#include <string.h>
23
c5e74408 24int ctf_string_read(struct stream_pos *ppos, struct definition *definition)
a52d7f6a 25{
d11e9c49
MD
26 struct definition_string *string_definition =
27 container_of(definition, struct definition_string, p);
28 const struct declaration_string *string_declaration =
29 string_definition->declaration;
30 struct ctf_stream_pos *pos = ctf_pos(ppos);
a52d7f6a 31 size_t len;
c5e74408 32 ssize_t max_len;
47e0f2e2 33 char *srcaddr;
a52d7f6a 34
d11e9c49 35 ctf_align_pos(pos, string_declaration->p.alignment);
c5e74408 36
d11e9c49 37 srcaddr = ctf_get_pos_addr(pos);
670977d3 38 if (pos->offset == EOF)
df752f35 39 return -EFAULT;
c5e74408
MD
40 /* Not counting \0 */
41 max_len = pos->packet_size - pos->offset - 1;
42 if (max_len < 0)
43 return -EFAULT;
44 len = strnlen(srcaddr, max_len) + 1; /* Add \0 */
45 /* Truncated string, unexpected. Trace probably corrupted. */
46 if (srcaddr[len - 1] != '\0')
47 return -EFAULT;
48
d11e9c49
MD
49 if (string_definition->alloc_len < len) {
50 string_definition->value =
51 g_realloc(string_definition->value, len);
52 string_definition->alloc_len = len;
53 }
847bf71a 54 printf_debug("CTF string read %s\n", srcaddr);
d11e9c49
MD
55 memcpy(string_definition->value, srcaddr, len);
56 string_definition->len = len;
847bf71a 57 ctf_move_pos(pos, len * CHAR_BIT);
c5e74408 58 return 0;
6dc2ca62 59}
a52d7f6a 60
c5e74408 61int ctf_string_write(struct stream_pos *ppos,
d11e9c49 62 struct definition *definition)
a52d7f6a 63{
d11e9c49
MD
64 struct definition_string *string_definition =
65 container_of(definition, struct definition_string, p);
66 const struct declaration_string *string_declaration =
67 string_definition->declaration;
68 struct ctf_stream_pos *pos = ctf_pos(ppos);
a52d7f6a 69 size_t len;
47e0f2e2 70 char *destaddr;
a52d7f6a 71
d11e9c49
MD
72 ctf_align_pos(pos, string_declaration->p.alignment);
73 assert(string_definition->value != NULL);
74 len = string_definition->len;
c5e74408
MD
75
76 if (!ctf_pos_access_ok(pos, len))
77 return -EFAULT;
78
d11e9c49 79 if (pos->dummy)
a52d7f6a 80 goto end;
d11e9c49 81 destaddr = ctf_get_pos_addr(pos);
8563e754 82 memcpy(destaddr, string_definition->value, len);
a52d7f6a 83end:
847bf71a 84 ctf_move_pos(pos, len * CHAR_BIT);
c5e74408 85 return 0;
a52d7f6a 86}
This page took 0.027594 seconds and 4 git commands to generate.