e2b41dd361724bbe45d64c382bcefb8bc2b2899d
[pspp-builds.git] / src / libpspp / integer-format.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <libpspp/integer-format.h>
22
23 #include <assert.h>
24
25 /* Returns true if FORMAT is a valid integer format. */
26 static inline bool
27 is_integer_format (enum integer_format format)
28 {
29   return (format == INTEGER_MSB_FIRST
30           || format == INTEGER_LSB_FIRST
31           || format == INTEGER_VAX);
32 }
33
34 /* Converts the CNT bytes in INTEGER from SRC integer_format to DST
35    integer_format. */
36 void
37 integer_convert (enum integer_format src, const void *from,
38                  enum integer_format dst, void *to,
39                  size_t cnt)
40 {
41   if (src != dst)
42     integer_put (integer_get (src, from, cnt), dst, to, cnt);
43   else if (from != to)
44     memcpy (to, from, cnt);
45 }
46
47 /* Returns the value of the CNT-byte integer at FROM, which is in
48    the given FORMAT. */
49 uint64_t
50 integer_get (enum integer_format format, const void *from_, size_t cnt)
51 {
52   const uint8_t *from = from_;
53   uint64_t value = 0;
54   size_t i;
55
56   assert (is_integer_format (format));
57   assert (cnt < 8);
58
59   switch (format)
60     {
61     case INTEGER_MSB_FIRST:
62       for (i = 0; i < cnt; i++)
63         value = (value << 8) | from[i];
64       break;
65     case INTEGER_LSB_FIRST:
66       for (i = 0; i < cnt; i++)
67         value = (value << 8) | from[cnt - i - 1];
68       break;
69     case INTEGER_VAX:
70       for (i = 0; i < (cnt & ~1); i++)
71         value = (value << 8) | from[i ^ 1];
72       if (cnt & 1)
73         value = (value << 8) | from[cnt - 1];
74       break;
75     }
76
77   return value;
78 }
79
80 /* Stores VALUE as a CNT-byte integer at TO, in the given
81    FORMAT. */
82 void
83 integer_put (uint64_t value, enum integer_format format, void *to_, size_t cnt)
84 {
85   uint8_t *to = to_;
86   size_t i;
87
88   assert (is_integer_format (format));
89   assert (cnt < 8);
90
91   value <<= 8 * (8 - cnt);
92
93   switch (format)
94     {
95     case INTEGER_MSB_FIRST:
96       for (i = 0; i < cnt; i++)
97         {
98           to[i] = value >> 56;
99           value <<= 8;
100         }
101       break;
102     case INTEGER_LSB_FIRST:
103       for (i = 0; i < cnt; i++)
104         {
105           to[cnt - i - 1] = value >> 56;
106           value <<= 8;
107         }
108       break;
109     case INTEGER_VAX:
110       for (i = 0; i < (cnt & ~1); i++)
111         {
112           to[i ^ 1] = value >> 56;
113           value <<= 8;
114         }
115       if (cnt & 1)
116         to[cnt - 1] = value >> 56;
117       break;
118     }
119 }
120
121 /* Returns true if bytes with index IDX1 and IDX2 in VALUE differ
122    in value. */
123 static inline bool
124 bytes_differ (uint64_t value, unsigned int idx1, unsigned int idx2)
125 {
126   uint8_t byte1 = value >> (idx1 * 8);
127   uint8_t byte2 = value >> (idx2 * 8);
128   return byte1 != byte2;
129 }
130
131 /* Attempts to identify the integer format in which the LENGTH
132    bytes in INTEGER represent the given EXPECTED_VALUE.  Returns
133    true if successful, false otherwise.  On success, stores the
134    format in *FORMAT. */
135 bool
136 integer_identify (uint64_t expected_value, const void *integer, size_t length,
137                   enum integer_format *format)
138 {
139   /* Odd-length integers are confusing. */
140   assert (length % 2 == 0);
141
142   /* LENGTH must be greater than 2 because VAX format is
143      equivalent to little-endian for 2-byte integers. */
144   assert (length > 2);
145
146   /* EXPECTED_VALUE must contain different byte values, because
147      otherwise all formats are identical. */
148   assert (bytes_differ (expected_value, 0, 1)
149           || bytes_differ (expected_value, 0, 2)
150           || bytes_differ (expected_value, 0, 3)
151           || (length > 4
152               && (bytes_differ (expected_value, 0, 4)
153                   || bytes_differ (expected_value, 0, 5)))
154           || (length > 6
155               && (bytes_differ (expected_value, 0, 6)
156                   || bytes_differ (expected_value, 0, 7))));
157
158   if (integer_get (INTEGER_MSB_FIRST, integer, length) == expected_value)
159     *format = INTEGER_MSB_FIRST;
160   else if (integer_get (INTEGER_LSB_FIRST, integer, length) == expected_value)
161     *format = INTEGER_LSB_FIRST;
162   else if (integer_get (INTEGER_VAX, integer, length) == expected_value)
163     *format = INTEGER_VAX;
164   else
165     return false;
166
167   return true;
168 }