4ab305249b7d096c50cc9cdf2db1b011010067f5
[babeltrace.git] / formats / ctf / types / string.c
1 /*
2 * Common Trace Format
3 *
4 * Strings read/write functions.
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/babeltrace.h>
20 #include <babeltrace/ctf/types.h>
21 #include <limits.h> /* C99 limits */
22 #include <string.h>
23
24 int ctf_string_read(struct stream_pos *ppos, struct definition *definition)
25 {
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);
31 size_t len;
32 ssize_t max_len;
33 char *srcaddr;
34
35 ctf_align_pos(pos, string_declaration->p.alignment);
36
37 srcaddr = ctf_get_pos_addr(pos);
38 /* Not counting \0 */
39 max_len = pos->packet_size - pos->offset - 1;
40 if (max_len < 0)
41 return -EFAULT;
42 len = strnlen(srcaddr, max_len) + 1; /* Add \0 */
43 /* Truncated string, unexpected. Trace probably corrupted. */
44 if (srcaddr[len - 1] != '\0')
45 return -EFAULT;
46
47 if (string_definition->alloc_len < len) {
48 string_definition->value =
49 g_realloc(string_definition->value, len);
50 string_definition->alloc_len = len;
51 }
52 printf_debug("CTF string read %s\n", srcaddr);
53 memcpy(string_definition->value, srcaddr, len);
54 string_definition->len = len;
55 ctf_move_pos(pos, len * CHAR_BIT);
56 return 0;
57 }
58
59 int ctf_string_write(struct stream_pos *ppos,
60 struct definition *definition)
61 {
62 struct definition_string *string_definition =
63 container_of(definition, struct definition_string, p);
64 const struct declaration_string *string_declaration =
65 string_definition->declaration;
66 struct ctf_stream_pos *pos = ctf_pos(ppos);
67 size_t len;
68 char *destaddr;
69
70 ctf_align_pos(pos, string_declaration->p.alignment);
71 assert(string_definition->value != NULL);
72 len = string_definition->len;
73
74 if (!ctf_pos_access_ok(pos, len))
75 return -EFAULT;
76
77 if (pos->dummy)
78 goto end;
79 destaddr = ctf_get_pos_addr(pos);
80 memcpy(destaddr, string_definition->value, len);
81 end:
82 ctf_move_pos(pos, len * CHAR_BIT);
83 return 0;
84 }
This page took 0.029815 seconds and 3 git commands to generate.