syslog-ng source
nvtable-serialize-endianutils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2015 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  * Copyright (c) 2012-2015 Viktor Juhasz <viktor.juhasz@balabit.com>
5  * Copyright (c) 2012-2013 Viktor Tusa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * As an additional exemption you are allowed to compile & link against the
22  * OpenSSL libraries as published by the OpenSSL project. See the file
23  * COPYING for details.
24  *
25  */
26 
27 #ifndef NVTABLE_SERIALIZE_ENDIANUTILS_H_
28 #define NVTABLE_SERIALIZE_ENDIANUTILS_H_
29 
30 static inline guint8 reverse(guint8 b)
31 {
32  b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
33  b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
34  b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
35  return b;
36 }
37 
38 static inline void
39 nv_table_swap_entry_flags(NVEntry *entry)
40 {
41  entry->flags = reverse(entry->flags);
42 }
43 
44 static inline void
45 nv_entry_swap_bytes(NVEntry *entry)
46 {
47  nv_table_swap_entry_flags(entry);
48  entry->alloc_len = GUINT32_SWAP_LE_BE(entry->alloc_len);
49 
50  if (!entry->indirect)
51  {
52  entry->vdirect.value_len = GUINT32_SWAP_LE_BE(entry->vdirect.value_len);
53  }
54  else
55  {
56  entry->vindirect.handle = GUINT32_SWAP_LE_BE(entry->vindirect.handle);
57  entry->vindirect.ofs = GUINT32_SWAP_LE_BE(entry->vindirect.ofs);
58  entry->vindirect.len = GUINT32_SWAP_LE_BE(entry->vindirect.len);
59  }
60 }
61 
62 static inline void
63 nv_table_dyn_value_swap_bytes(NVIndexEntry *self)
64 {
65  self->handle = GUINT32_SWAP_LE_BE(self->handle);
66  self->ofs = GUINT32_SWAP_LE_BE(self->handle);
67 };
68 
69 static inline void
70 nv_table_data_swap_bytes(NVTable *self)
71 {
72  NVIndexEntry *index_table;
73  NVEntry *entry;
74  gint i;
75 
76  for (i = 0; i < self->num_static_entries; i++)
77  {
78  entry = nv_table_get_entry_at_ofs(self, self->static_entries[i]);
79  if (!entry)
80  continue;
81  nv_entry_swap_bytes(entry);
82  }
83 
84  index_table = nv_table_get_index(self);
85  for (i = 0; i < self->index_size; i++)
86  {
87  entry = nv_table_get_entry_at_ofs(self, index_table[i].ofs);
88 
89  if (!entry)
90  continue;
91  nv_entry_swap_bytes(entry);
92  }
93 }
94 
95 static inline void
96 nv_table_struct_swap_bytes(NVTable *self)
97 {
98  guint16 i;
99  NVIndexEntry *index_table;
100 
101  self->size = GUINT16_SWAP_LE_BE(self->size);
102  self->used = GUINT16_SWAP_LE_BE(self->used);
103  self->index_size = GUINT16_SWAP_LE_BE(self->index_size);
104 
105  for (i = 0; i < self->num_static_entries; i++)
106  {
107  self->static_entries[i] = GUINT16_SWAP_LE_BE(self->static_entries[i]);
108  }
109  index_table = nv_table_get_index(self);
110 
111  for (i = 0; i < self->index_size; i++)
112  {
113  nv_table_dyn_value_swap_bytes(&index_table[i]);
114  }
115 }
116 
117 #endif /* NVTABLE_SERIALIZE_ENDIANUTILS_H_ */
#define self
Definition: rcptid.c:38