e5b428cc57571b05283bb7a8b1dc5b2b13bdc73b
[babeltrace.git] / formats / ctf / types / array.c
1 /*
2 * Common Trace Format
3 *
4 * Array format access functions.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
21 #include <babeltrace/ctf/types.h>
22
23 int ctf_array_read(struct stream_pos *ppos, struct definition *definition)
24 {
25 struct definition_array *array_definition =
26 container_of(definition, struct definition_array, p);
27 struct declaration_array *array_declaration =
28 array_definition->declaration;
29 struct declaration *elem = array_declaration->elem;
30 struct ctf_stream_pos *pos =
31 container_of(ppos, struct ctf_stream_pos, parent);
32
33 if (elem->id == CTF_TYPE_INTEGER) {
34 struct declaration_integer *integer_declaration =
35 container_of(elem, struct declaration_integer, p);
36
37 if (integer_declaration->encoding == CTF_STRING_UTF8
38 || integer_declaration->encoding == CTF_STRING_ASCII) {
39
40 if (integer_declaration->len == CHAR_BIT
41 && integer_declaration->p.alignment == CHAR_BIT) {
42
43 ctf_align_pos(pos, integer_declaration->p.alignment);
44 if (!ctf_pos_access_ok(pos, array_declaration->len * CHAR_BIT))
45 return -EFAULT;
46
47 g_string_assign(array_definition->string, "");
48 g_string_insert_len(array_definition->string,
49 0, (char *) ctf_get_pos_addr(pos),
50 array_declaration->len);
51 ctf_move_pos(pos, array_declaration->len * CHAR_BIT);
52 return 0;
53 }
54 }
55 }
56 return array_rw(ppos, definition);
57 }
58
59 int ctf_array_write(struct stream_pos *ppos, struct definition *definition)
60 {
61 struct definition_array *array_definition =
62 container_of(definition, struct definition_array, p);
63 struct declaration_array *array_declaration =
64 array_definition->declaration;
65 struct declaration *elem = array_declaration->elem;
66 struct ctf_stream_pos *pos =
67 container_of(ppos, struct ctf_stream_pos, parent);
68
69 if (elem->id == CTF_TYPE_INTEGER) {
70 struct declaration_integer *integer_declaration =
71 container_of(elem, struct declaration_integer, p);
72
73 if (integer_declaration->encoding == CTF_STRING_UTF8
74 || integer_declaration->encoding == CTF_STRING_ASCII) {
75
76 if (integer_declaration->len == CHAR_BIT
77 && integer_declaration->p.alignment == CHAR_BIT) {
78
79 ctf_align_pos(pos, integer_declaration->p.alignment);
80 if (!ctf_pos_access_ok(pos, array_declaration->len * CHAR_BIT))
81 return -EFAULT;
82
83 memcpy((char *) ctf_get_pos_addr(pos),
84 array_definition->string->str,
85 array_declaration->len);
86 ctf_move_pos(pos, array_declaration->len * CHAR_BIT);
87 return 0;
88 }
89 }
90 }
91 return array_rw(ppos, definition);
92 }
This page took 0.055861 seconds and 3 git commands to generate.