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