port_init.c 7.6 KB

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