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