| 1 | /* |
| 2 | * Common Trace Format |
| 3 | * |
| 4 | * Sequence format access 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/ctf/types.h> |
| 20 | |
| 21 | int ctf_sequence_read(struct stream_pos *ppos, struct definition *definition) |
| 22 | { |
| 23 | struct definition_sequence *sequence_definition = |
| 24 | container_of(definition, struct definition_sequence, p); |
| 25 | struct declaration_sequence *sequence_declaration = |
| 26 | sequence_definition->declaration; |
| 27 | struct declaration *elem = sequence_declaration->elem; |
| 28 | struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 29 | |
| 30 | if (elem->id == CTF_TYPE_INTEGER) { |
| 31 | struct declaration_integer *integer_declaration = |
| 32 | container_of(elem, struct declaration_integer, p); |
| 33 | |
| 34 | if (integer_declaration->encoding == CTF_STRING_UTF8 |
| 35 | || integer_declaration->encoding == CTF_STRING_ASCII) { |
| 36 | |
| 37 | if (integer_declaration->len == CHAR_BIT |
| 38 | && integer_declaration->p.alignment == CHAR_BIT) { |
| 39 | uint64_t len = sequence_len(sequence_definition); |
| 40 | |
| 41 | ctf_align_pos(pos, integer_declaration->p.alignment); |
| 42 | if (!ctf_pos_access_ok(pos, len * CHAR_BIT)) |
| 43 | return -EFAULT; |
| 44 | |
| 45 | g_string_assign(sequence_definition->string, ""); |
| 46 | g_string_insert_len(sequence_definition->string, |
| 47 | 0, (char *) ctf_get_pos_addr(pos), len); |
| 48 | ctf_move_pos(pos, len * CHAR_BIT); |
| 49 | return 0; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return sequence_rw(ppos, definition); |
| 54 | } |
| 55 | |
| 56 | int ctf_sequence_write(struct stream_pos *ppos, struct definition *definition) |
| 57 | { |
| 58 | struct definition_sequence *sequence_definition = |
| 59 | container_of(definition, struct definition_sequence, p); |
| 60 | struct declaration_sequence *sequence_declaration = |
| 61 | sequence_definition->declaration; |
| 62 | struct declaration *elem = sequence_declaration->elem; |
| 63 | struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 64 | |
| 65 | if (elem->id == CTF_TYPE_INTEGER) { |
| 66 | struct declaration_integer *integer_declaration = |
| 67 | container_of(elem, struct declaration_integer, p); |
| 68 | |
| 69 | if (integer_declaration->encoding == CTF_STRING_UTF8 |
| 70 | || integer_declaration->encoding == CTF_STRING_ASCII) { |
| 71 | |
| 72 | if (integer_declaration->len == CHAR_BIT |
| 73 | && integer_declaration->p.alignment == CHAR_BIT) { |
| 74 | uint64_t len = sequence_len(sequence_definition); |
| 75 | |
| 76 | ctf_align_pos(pos, integer_declaration->p.alignment); |
| 77 | if (!ctf_pos_access_ok(pos, len * CHAR_BIT)) |
| 78 | return -EFAULT; |
| 79 | |
| 80 | memcpy((char *) ctf_get_pos_addr(pos), |
| 81 | sequence_definition->string->str, len); |
| 82 | ctf_move_pos(pos, len * CHAR_BIT); |
| 83 | return 0; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return sequence_rw(ppos, definition); |
| 88 | } |