GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
port_init.c
Go to the documentation of this file.
1/*!
2 \file diglib/port_init.c
3
4 \brief Vector library - portability (lower level functions)
5
6 Lower level functions for reading/writing/manipulating vectors.
7
8 This code is a quick hack to allow the writing of portable
9 binary data files.
10 The approach is to take known values and compare them against
11 the current machine's internal representation. A cross reference
12 table is then built, and then all file reads and writes must go
13 through these routines to correct the numbers if need be.
14
15 As long as the byte switching is symmetrical, the conversion routines
16 will work both directions.
17
18 The integer test patterns are quite simple, and their choice was
19 arbitrary, but the float and double valued were more critical.
20
21 I did not have a specification for IEEE to go by, so it is possible
22 that I have missed something. My criteria were:
23
24 First, true IEEE numbers had to be chosen to avoid getting an FPE.
25 Second, every byte in the test pattern had to be unique. And
26 finally, the number had to not be sensitive to rounding by the
27 specific hardware implementation.
28
29 By experimentation it was found that the number 1.3333 met
30 all these criteria for both floats and doubles
31
32 See the discourse at the end of this file for more information
33
34 The 3.0 dig, and dig_plus files are inherently non-portable. This
35 can be seen in moving files between a SUN 386i and other SUN machines.
36 The recommended way to transport files was always to convert to ASCII
37 (b.a.vect) and copy the ASCII files: dig_ascii and dig_att to the
38 destination machine.
39
40 The problem lies in the way that different architectures internally
41 represent data. If a number is internally store as 0x01020304 on
42 a 680x0 family machine, the same number will be stored as
43 0x04030201 on an 80386 class machine.
44
45 The CERL port of GRASS to the Compaq 386 already has code to deal
46 with this incompatibility. This code converts all files that are written
47 out to conform to the 680x0 standard. These binary files can then be
48 shared between machines without conversion.
49 This code is designed to work with the majority of computers in use
50 today that fit the following requirements:
51 byte == 8 bits
52 int == 4 bytes
53 long == 4 bytes
54 double == IEEE standard 64 bit
55 float == IEEE standard 32 bit
56
57 bytes can be swapped around in any reasonable way, but bits within each
58 byte must be maintained in normal high to low ordering: 76543210
59 is this a problem?
60
61 If this ability is desired on a SUN 386i, for example, you simply
62 define the compiler flag CERL_PORTABLE in the src/CMD/makehead file
63 and recompile all of the mapdev programs.
64 needs update, makehead/mapdev no longer exist
65
66 Binary DLG files are NOT supported by this code, and will continue to
67 be non-portable between different architectures.
68 applies to the files coor/topo/cidx, needs testing
69
70 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
71 SPDX-License-Identifier: GPL-2.0-or-later
72
73 \author Original author CERL, probably Dave Gerdes
74 \author Update to GRASS 5.7 Radim Blazek
75 */
76
77#include <stdio.h>
78#include <sys/types.h>
79#include <grass/vector.h>
80#include <grass/glocale.h>
81
82#define TEST_PATTERN 1.3333
83#ifdef HAVE_LONG_LONG_INT
84#define LONG_LONG_TEST 0x0102030405060708LL
85#endif
86#define LONG_TEST 0x01020304
87#define INT_TEST 0x01020304
88#define SHORT_TEST 0x0102
89
90static double u_d = TEST_PATTERN;
91static float u_f = TEST_PATTERN;
92off_t u_o; /* depends on sizeof(off_t) */
93static long u_l = LONG_TEST;
94static int u_i = INT_TEST;
95static short u_s = SHORT_TEST;
96
97/* dbl_cmpr holds the bytes of an IEEE representation of TEST_PATTERN */
98static const unsigned char dbl_cmpr[] = {0x3f, 0xf5, 0x55, 0x32,
99 0x61, 0x7c, 0x1b, 0xda};
100/* flt_cmpr holds the bytes of an IEEE representation of TEST_PATTERN */
101static const unsigned char flt_cmpr[] = {0x3f, 0xaa, 0xa9, 0x93};
102static const unsigned char off_t_cmpr[] = {0x01, 0x02, 0x03, 0x04,
103 0x05, 0x06, 0x07, 0x08};
104static const unsigned char lng_cmpr[] = {0x01, 0x02, 0x03, 0x04};
105static const unsigned char int_cmpr[] = {0x01, 0x02, 0x03, 0x04};
106static const unsigned char shrt_cmpr[] = {0x01, 0x02};
107
108/* Find native sizes */
109int nat_dbl = sizeof(double);
110int nat_flt = sizeof(float);
111int nat_off_t = sizeof(off_t);
112int nat_lng = sizeof(long);
113int nat_int = sizeof(int);
114int nat_shrt = sizeof(short);
115
122
123unsigned char dbl_cnvrt[sizeof(double)];
124unsigned char flt_cnvrt[sizeof(float)];
125unsigned char off_t_cnvrt[sizeof(off_t)];
126unsigned char lng_cnvrt[sizeof(long)];
127unsigned char int_cnvrt[sizeof(int)];
128unsigned char shrt_cnvrt[sizeof(short)];
129
130/*
131 * match search_value against each char in basis.
132 * return offset or -1 if not found
133 */
134static int find_offset(const unsigned char *basis, unsigned char search_value,
135 int size)
136{
137 int i;
138
139 for (i = 0; i < size; i++)
140 if (basis[i] == search_value)
141 return (i);
142
143 return (-1);
144}
145
146static int find_offsets(const void *pattern, unsigned char *cnvrt,
147 const unsigned char *cmpr, int port_size, int nat_size,
148 const char *typename)
149{
150 int big, ltl;
151 int i;
152
153 for (i = 0; i < port_size; i++) {
154 int off = find_offset(pattern, cmpr[i], nat_size);
155
156 if (off < 0)
157 G_fatal_error(_("Unable to find '%x' in %s"), cmpr[i], typename);
158
159 cnvrt[i] = off;
160 }
161
162 big = ltl = 1;
163
164 for (i = 0; i < port_size; i++) {
165 if (cnvrt[i] != (nat_size - port_size + i))
166 big = 0; /* isn't big endian */
167 if (cnvrt[i] != (port_size - 1 - i))
168 ltl = 0; /* isn't little endian */
169 }
170
171 if (big)
172 return ENDIAN_BIG;
173
174 if (ltl)
175 return ENDIAN_LITTLE;
176
177 return ENDIAN_OTHER;
178}
179
180/*!
181 \brief Initialize Port_info structures
182 */
183void port_init(void)
184{
185 static int done;
186
187 if (done)
188 return;
189
190 done = 1;
191
192 /* Following code checks only if all assumptions are fulfilled */
193 /* Check sizes */
194 if (nat_dbl != PORT_DOUBLE)
195 G_fatal_error("sizeof(double) != %d", PORT_DOUBLE);
196 if (nat_flt != PORT_FLOAT)
197 G_fatal_error("sizeof(float) != %d", PORT_DOUBLE);
198 /* off_t size is variable, depending on the vector size and LFS support */
199 if (nat_lng < PORT_LONG)
200 G_fatal_error("sizeof(long) < %d", PORT_LONG);
201 if (nat_int < PORT_INT)
202 G_fatal_error("sizeof(int) < %d", PORT_INT);
203 if (nat_shrt < PORT_SHORT)
204 G_fatal_error("sizeof(short) < %d", PORT_SHORT);
205
206 /* Find for each byte in big endian test pattern (*_cmpr)
207 * offset of corresponding byte in machine native order.
208 * Look if native byte order is little or big or some other (pdp)
209 * endian.
210 */
211
212 if (nat_off_t == 8)
213#ifdef HAVE_LONG_LONG_INT
215#else
216 G_fatal_error("Internal error: can't construct an off_t literal");
217#endif
218 else
220
221 dbl_order =
222 find_offsets(&u_d, dbl_cnvrt, dbl_cmpr, PORT_DOUBLE, nat_dbl, "double");
223 flt_order =
224 find_offsets(&u_f, flt_cnvrt, flt_cmpr, PORT_FLOAT, nat_flt, "float");
225 off_t_order = find_offsets(&u_o, off_t_cnvrt, off_t_cmpr, nat_off_t,
226 nat_off_t, "off_t");
227 lng_order =
228 find_offsets(&u_l, lng_cnvrt, lng_cmpr, PORT_LONG, nat_lng, "long");
229 int_order =
230 find_offsets(&u_i, int_cnvrt, int_cmpr, PORT_INT, nat_int, "int");
231 shrt_order = find_offsets(&u_s, shrt_cnvrt, shrt_cmpr, PORT_SHORT, nat_shrt,
232 "short");
233}
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define PORT_LONG
Definition dig_defines.h:47
#define PORT_SHORT
Definition dig_defines.h:49
#define PORT_DOUBLE
Sizes of types used in portable format (different names used in Vlib/ and diglib/ for the same thing)
Definition dig_defines.h:45
#define PORT_FLOAT
Definition dig_defines.h:46
#define PORT_INT
Definition dig_defines.h:48
#define ENDIAN_OTHER
Definition gis.h:414
#define ENDIAN_LITTLE
Endian check.
Definition gis.h:412
#define ENDIAN_BIG
Definition gis.h:413
#define _(str)
Definition glocale.h:10
unsigned char flt_cnvrt[sizeof(float)]
Definition port_init.c:124
off_t u_o
Definition port_init.c:92
int nat_lng
Definition port_init.c:112
int shrt_order
Definition port_init.c:121
#define LONG_TEST
Definition port_init.c:86
unsigned char dbl_cnvrt[sizeof(double)]
Definition port_init.c:123
int flt_order
Definition port_init.c:117
int dbl_order
Definition port_init.c:116
int int_order
Definition port_init.c:120
int nat_dbl
Definition port_init.c:109
#define TEST_PATTERN
Definition port_init.c:82
unsigned char lng_cnvrt[sizeof(long)]
Definition port_init.c:126
int off_t_order
Definition port_init.c:118
int lng_order
Definition port_init.c:119
unsigned char off_t_cnvrt[sizeof(off_t)]
Definition port_init.c:125
#define INT_TEST
Definition port_init.c:87
int nat_off_t
Definition port_init.c:111
#define LONG_LONG_TEST
Definition port_init.c:84
int nat_shrt
Definition port_init.c:114
int nat_int
Definition port_init.c:113
#define SHORT_TEST
Definition port_init.c:88
unsigned char shrt_cnvrt[sizeof(short)]
Definition port_init.c:128
void port_init(void)
Initialize Port_info structures.
Definition port_init.c:183
unsigned char int_cnvrt[sizeof(int)]
Definition port_init.c:127
int nat_flt
Definition port_init.c:110