| 1 |
/* |
| 2 |
* Common Trace Format |
| 3 |
* |
| 4 |
* Strings read/write functions. |
| 5 |
* |
| 6 |
* Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 |
* |
| 8 |
* Author: Mathieu Desnoyers <[email protected]> |
| 9 |
* |
| 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: |
| 16 |
* |
| 17 |
* The above copyright notice and this permission notice shall be included in |
| 18 |
* all copies or substantial portions of the Software. |
| 19 |
* |
| 20 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
* SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <babeltrace/babeltrace-internal.h> |
| 30 |
#include <babeltrace/ctf/types.h> |
| 31 |
#include <limits.h> /* C99 limits */ |
| 32 |
#include <babeltrace/compat/string.h> |
| 33 |
|
| 34 |
int ctf_string_read(struct bt_stream_pos *ppos, struct bt_definition *definition) |
| 35 |
{ |
| 36 |
struct definition_string *string_definition = |
| 37 |
container_of(definition, struct definition_string, p); |
| 38 |
const struct declaration_string *string_declaration = |
| 39 |
string_definition->declaration; |
| 40 |
struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 41 |
size_t len; |
| 42 |
ssize_t max_len_bits; |
| 43 |
char *srcaddr; |
| 44 |
|
| 45 |
if (!ctf_align_pos(pos, string_declaration->p.alignment)) |
| 46 |
return -EFAULT; |
| 47 |
|
| 48 |
srcaddr = ctf_get_pos_addr(pos); |
| 49 |
if (pos->offset == EOF) |
| 50 |
return -EFAULT; |
| 51 |
/* Not counting \0. Counting in bits. */ |
| 52 |
max_len_bits = pos->packet_size - pos->offset - CHAR_BIT; |
| 53 |
if (max_len_bits < 0) |
| 54 |
return -EFAULT; |
| 55 |
/* Add \0, counting in bytes. */ |
| 56 |
len = bt_strnlen(srcaddr, (size_t) max_len_bits / CHAR_BIT) + 1; |
| 57 |
/* Truncated string, unexpected. Trace probably corrupted. */ |
| 58 |
if (srcaddr[len - 1] != '\0') |
| 59 |
return -EFAULT; |
| 60 |
|
| 61 |
if (string_definition->alloc_len < len) { |
| 62 |
string_definition->value = |
| 63 |
g_realloc(string_definition->value, len); |
| 64 |
string_definition->alloc_len = len; |
| 65 |
} |
| 66 |
printf_debug("CTF string read %s\n", srcaddr); |
| 67 |
memcpy(string_definition->value, srcaddr, len); |
| 68 |
string_definition->len = len; |
| 69 |
if (!ctf_move_pos(pos, len * CHAR_BIT)) |
| 70 |
return -EFAULT; |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
int ctf_string_write(struct bt_stream_pos *ppos, |
| 75 |
struct bt_definition *definition) |
| 76 |
{ |
| 77 |
struct definition_string *string_definition = |
| 78 |
container_of(definition, struct definition_string, p); |
| 79 |
const struct declaration_string *string_declaration = |
| 80 |
string_definition->declaration; |
| 81 |
struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 82 |
size_t len; |
| 83 |
char *destaddr; |
| 84 |
|
| 85 |
if (!ctf_align_pos(pos, string_declaration->p.alignment)) |
| 86 |
return -EFAULT; |
| 87 |
assert(string_definition->value != NULL); |
| 88 |
len = string_definition->len; |
| 89 |
|
| 90 |
if (!ctf_pos_access_ok(pos, len)) |
| 91 |
return -EFAULT; |
| 92 |
|
| 93 |
if (pos->dummy) |
| 94 |
goto end; |
| 95 |
destaddr = ctf_get_pos_addr(pos); |
| 96 |
memcpy(destaddr, string_definition->value, len); |
| 97 |
end: |
| 98 |
if (!ctf_move_pos(pos, len * CHAR_BIT)) |
| 99 |
return -EFAULT; |
| 100 |
return 0; |
| 101 |
} |