Add "encoding" for sequence and array of integers
[babeltrace.git] / formats / ctf / types / array.c
CommitLineData
d06d03db
MD
1/*
2 * Common Trace Format
3 *
4 * Array format access functions.
5 *
ccd7e1c8 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d06d03db 7 *
ccd7e1c8
MD
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:
d06d03db 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
d06d03db
MD
17 */
18
19#include <babeltrace/ctf/types.h>
20
81dee1bb 21int ctf_array_read(struct stream_pos *ppos, struct definition *definition)
d06d03db 22{
81dee1bb
MD
23 struct definition_array *array_definition =
24 container_of(definition, struct definition_array, p);
25 struct declaration_array *array_declaration =
26 array_definition->declaration;
27 struct declaration *elem = array_declaration->elem;
28 struct ctf_stream_pos *pos =
29 container_of(ppos, struct ctf_stream_pos, parent);
30
31 if (elem->id == CTF_TYPE_INTEGER) {
32 struct declaration_integer *integer_declaration =
33 container_of(elem, struct declaration_integer, p);
34
35 if (integer_declaration->encoding == CTF_STRING_UTF8
36 || integer_declaration->encoding == CTF_STRING_ASCII) {
37
38 if (integer_declaration->len == CHAR_BIT
39 && integer_declaration->p.alignment == CHAR_BIT) {
40
41 ctf_align_pos(pos, integer_declaration->p.alignment);
42 if (!ctf_pos_access_ok(pos, array_declaration->len * CHAR_BIT))
43 return -EFAULT;
44
45 g_string_assign(array_definition->string, "");
46 g_string_insert_len(array_definition->string,
47 0, (char *) ctf_get_pos_addr(pos),
48 array_declaration->len);
49 ctf_move_pos(pos, array_declaration->len * CHAR_BIT);
50 return 0;
51 }
52 }
53 }
54 return array_rw(ppos, definition);
55}
56
57int ctf_array_write(struct stream_pos *ppos, struct definition *definition)
58{
59 struct definition_array *array_definition =
60 container_of(definition, struct definition_array, p);
61 struct declaration_array *array_declaration =
62 array_definition->declaration;
63 struct declaration *elem = array_declaration->elem;
64 struct ctf_stream_pos *pos =
65 container_of(ppos, struct ctf_stream_pos, parent);
66
67 if (elem->id == CTF_TYPE_INTEGER) {
68 struct declaration_integer *integer_declaration =
69 container_of(elem, struct declaration_integer, p);
70
71 if (integer_declaration->encoding == CTF_STRING_UTF8
72 || integer_declaration->encoding == CTF_STRING_ASCII) {
73
74 if (integer_declaration->len == CHAR_BIT
75 && integer_declaration->p.alignment == CHAR_BIT) {
76
77 ctf_align_pos(pos, integer_declaration->p.alignment);
78 if (!ctf_pos_access_ok(pos, array_declaration->len * CHAR_BIT))
79 return -EFAULT;
80
81 memcpy((char *) ctf_get_pos_addr(pos),
82 array_definition->string->str,
83 array_declaration->len);
84 ctf_move_pos(pos, array_declaration->len * CHAR_BIT);
85 return 0;
86 }
87 }
88 }
89 return array_rw(ppos, definition);
d06d03db 90}
This page took 0.026176 seconds and 4 git commands to generate.