portable.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 <sys/types.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/Vect.h>
  23. extern void port_init(void);
  24. extern int nat_dbl;
  25. extern int nat_flt;
  26. extern int nat_lng;
  27. extern int nat_off_t;
  28. extern int nat_int;
  29. extern int nat_shrt;
  30. extern int dbl_order;
  31. extern int flt_order;
  32. extern int off_t_order;
  33. extern int lng_order;
  34. extern int int_order;
  35. extern int shrt_order;
  36. extern unsigned char dbl_cnvrt[sizeof(double)];
  37. extern unsigned char flt_cnvrt[sizeof(float)];
  38. extern unsigned char off_t_cnvrt[sizeof(off_t)];
  39. extern unsigned char lng_cnvrt[sizeof(long)];
  40. extern unsigned char int_cnvrt[sizeof(int)];
  41. extern unsigned char shrt_cnvrt[sizeof(short)];
  42. struct Port_info *Cur_Head;
  43. static char *buffer = NULL;
  44. static int buf_alloced = 0;
  45. static int buf_alloc(int needed)
  46. {
  47. char *p;
  48. int cnt;
  49. if (needed <= buf_alloced)
  50. return (0);
  51. cnt = buf_alloced;
  52. p = dig__alloc_space(needed, &cnt, 100, buffer, 1);
  53. if (p == NULL)
  54. return (dig_out_of_memory());
  55. buffer = p;
  56. buf_alloced = cnt;
  57. return (0);
  58. }
  59. /***************************** READ ************************************/
  60. /* These are the routines to read from the Portable Vector Format.
  61. These routines must handle any type size conversions between the
  62. portable format and the native machine.
  63. Return: 0 error
  64. 1 OK
  65. */
  66. /* read doubles from the PVF file */
  67. int dig__fread_port_D(double *buf, int cnt, GVFILE * fp)
  68. {
  69. int i, j, ret;
  70. unsigned char *c1, *c2;
  71. if (Cur_Head->dbl_quick) {
  72. ret = dig_fread(buf, PORT_DOUBLE, cnt, fp);
  73. if (ret != cnt)
  74. return 0;
  75. }
  76. else {
  77. /* read into buffer */
  78. buf_alloc(cnt * PORT_DOUBLE);
  79. ret = dig_fread(buffer, PORT_DOUBLE, cnt, fp);
  80. if (ret != cnt)
  81. return 0;
  82. /* read from buffer in changed order */
  83. c1 = (unsigned char *)buffer;
  84. c2 = (unsigned char *)buf;
  85. for (i = 0; i < cnt; i++) {
  86. for (j = 0; j < PORT_DOUBLE; j++) {
  87. c2[Cur_Head->dbl_cnvrt[j]] = c1[j];
  88. }
  89. c1 += PORT_DOUBLE;
  90. c2 += sizeof(double);
  91. }
  92. }
  93. return 1;
  94. }
  95. /* read floats from the PVF file */
  96. int dig__fread_port_F(float *buf, int cnt, GVFILE * fp)
  97. {
  98. int i, j, ret;
  99. unsigned char *c1, *c2;
  100. if (Cur_Head->flt_quick) {
  101. ret = dig_fread(buf, PORT_FLOAT, cnt, fp);
  102. if (ret != cnt)
  103. return 0;
  104. }
  105. else {
  106. /* read into buffer */
  107. buf_alloc(cnt * PORT_FLOAT);
  108. ret = dig_fread(buffer, PORT_FLOAT, cnt, fp);
  109. if (ret != cnt)
  110. return 0;
  111. /* read from buffer in changed order */
  112. c1 = (unsigned char *)buffer;
  113. c2 = (unsigned char *)buf;
  114. for (i = 0; i < cnt; i++) {
  115. for (j = 0; j < PORT_FLOAT; j++) {
  116. c2[Cur_Head->flt_cnvrt[j]] = c1[j];
  117. }
  118. c1 += PORT_FLOAT;
  119. c2 += sizeof(float);
  120. }
  121. }
  122. return 1;
  123. }
  124. /* read off_ts from the PVF file */
  125. int dig__fread_port_O(off_t *buf, int cnt, GVFILE * fp, int port_off_t_size)
  126. {
  127. int i, j, ret;
  128. unsigned char *c1, *c2;
  129. if (Cur_Head->off_t_quick) {
  130. if (nat_off_t == port_off_t_size) {
  131. ret = dig_fread(buf, port_off_t_size, cnt, fp);
  132. if (ret != cnt)
  133. return 0;
  134. }
  135. else if (nat_off_t > port_off_t_size) {
  136. /* read into buffer */
  137. buf_alloc(cnt * port_off_t_size);
  138. ret = dig_fread(buffer, port_off_t_size, cnt, fp);
  139. if (ret != cnt)
  140. return 0;
  141. /* set buffer to zero (positive numbers) */
  142. memset(buf, 0, cnt * sizeof(off_t));
  143. /* read from buffer in changed order */
  144. c1 = (unsigned char *)buffer;
  145. if (off_t_order == ENDIAN_LITTLE)
  146. c2 = (unsigned char *)buf;
  147. else
  148. c2 = (unsigned char *)buf + nat_off_t - port_off_t_size;
  149. for (i = 0; i < cnt; i++) {
  150. /* set to FF if the value is negative */
  151. if (off_t_order == ENDIAN_LITTLE) {
  152. if (c1[port_off_t_size - 1] & 0x80)
  153. memset(c2, 0xff, sizeof(off_t));
  154. }
  155. else {
  156. if (c1[0] & 0x80)
  157. memset(c2, 0xff, sizeof(off_t));
  158. }
  159. memcpy(c2, c1, port_off_t_size);
  160. c1 += port_off_t_size;
  161. c2 += sizeof(off_t);
  162. }
  163. }
  164. else if (nat_off_t < port_off_t_size) {
  165. /* should never happen */
  166. G_fatal_error("Vector exceeds supported file size limit");
  167. }
  168. }
  169. else {
  170. if (nat_off_t >= port_off_t_size) {
  171. /* read into buffer */
  172. buf_alloc(cnt * port_off_t_size);
  173. ret = dig_fread(buffer, port_off_t_size, cnt, fp);
  174. if (ret != cnt)
  175. return 0;
  176. /* set buffer to zero (positive numbers) */
  177. memset(buf, 0, cnt * sizeof(off_t));
  178. /* read from buffer in changed order */
  179. c1 = (unsigned char *)buffer;
  180. c2 = (unsigned char *)buf;
  181. for (i = 0; i < cnt; i++) {
  182. /* set to FF if the value is negative */
  183. if (Cur_Head->byte_order == ENDIAN_LITTLE) {
  184. if (c1[port_off_t_size - 1] & 0x80)
  185. memset(c2, 0xff, sizeof(off_t));
  186. }
  187. else {
  188. if (c1[0] & 0x80)
  189. memset(c2, 0xff, sizeof(off_t));
  190. }
  191. for (j = 0; j < port_off_t_size; j++)
  192. c2[Cur_Head->off_t_cnvrt[j]] = c1[j];
  193. c1 += port_off_t_size;
  194. c2 += sizeof(off_t);
  195. }
  196. }
  197. else if (nat_off_t < port_off_t_size) {
  198. /* should never happen */
  199. G_fatal_error("Vector exceeds supported file size limit");
  200. }
  201. }
  202. return 1;
  203. }
  204. /* read longs from the PVF file */
  205. int dig__fread_port_L(long *buf, int cnt, GVFILE * fp)
  206. {
  207. int i, j, ret;
  208. unsigned char *c1, *c2;
  209. if (Cur_Head->lng_quick) {
  210. if (nat_lng == PORT_LONG) {
  211. ret = dig_fread(buf, PORT_LONG, cnt, fp);
  212. if (ret != cnt)
  213. return 0;
  214. }
  215. else {
  216. /* read into buffer */
  217. buf_alloc(cnt * PORT_LONG);
  218. ret = dig_fread(buffer, PORT_LONG, cnt, fp);
  219. if (ret != cnt)
  220. return 0;
  221. /* set buffer to zero (positive numbers) */
  222. memset(buf, 0, cnt * sizeof(long));
  223. /* read from buffer in changed order */
  224. c1 = (unsigned char *)buffer;
  225. if (lng_order == ENDIAN_LITTLE)
  226. c2 = (unsigned char *)buf;
  227. else
  228. c2 = (unsigned char *)buf + nat_lng - PORT_LONG;
  229. for (i = 0; i < cnt; i++) {
  230. /* set to FF if the value is negative */
  231. if (lng_order == ENDIAN_LITTLE) {
  232. if (c1[PORT_LONG - 1] & 0x80)
  233. memset(c2, 0xff, sizeof(long));
  234. }
  235. else {
  236. if (c1[0] & 0x80)
  237. memset(c2, 0xff, sizeof(long));
  238. }
  239. memcpy(c2, c1, PORT_LONG);
  240. c1 += PORT_LONG;
  241. c2 += sizeof(long);
  242. }
  243. }
  244. }
  245. else {
  246. /* read into buffer */
  247. buf_alloc(cnt * PORT_LONG);
  248. ret = dig_fread(buffer, PORT_LONG, cnt, fp);
  249. if (ret != cnt)
  250. return 0;
  251. /* set buffer to zero (positive numbers) */
  252. memset(buf, 0, cnt * sizeof(long));
  253. /* read from buffer in changed order */
  254. c1 = (unsigned char *)buffer;
  255. c2 = (unsigned char *)buf;
  256. for (i = 0; i < cnt; i++) {
  257. /* set to FF if the value is negative */
  258. if (Cur_Head->byte_order == ENDIAN_LITTLE) {
  259. if (c1[PORT_LONG - 1] & 0x80)
  260. memset(c2, 0xff, sizeof(long));
  261. }
  262. else {
  263. if (c1[0] & 0x80)
  264. memset(c2, 0xff, sizeof(long));
  265. }
  266. for (j = 0; j < PORT_LONG; j++)
  267. c2[Cur_Head->lng_cnvrt[j]] = c1[j];
  268. c1 += PORT_LONG;
  269. c2 += sizeof(long);
  270. }
  271. }
  272. return 1;
  273. }
  274. /* read ints from the PVF file */
  275. int dig__fread_port_I(int *buf, int cnt, GVFILE * fp)
  276. {
  277. int i, j, ret;
  278. unsigned char *c1, *c2;
  279. if (Cur_Head->int_quick) {
  280. if (nat_int == PORT_INT) {
  281. ret = dig_fread(buf, PORT_INT, cnt, fp);
  282. if (ret != cnt)
  283. return 0;
  284. }
  285. else {
  286. /* read into buffer */
  287. buf_alloc(cnt * PORT_INT);
  288. ret = dig_fread(buffer, PORT_INT, cnt, fp);
  289. if (ret != cnt)
  290. return 0;
  291. /* set buffer to zero (positive numbers) */
  292. memset(buf, 0, cnt * sizeof(int));
  293. /* read from buffer in changed order */
  294. c1 = (unsigned char *)buffer;
  295. if (int_order == ENDIAN_LITTLE)
  296. c2 = (unsigned char *)buf;
  297. else
  298. c2 = (unsigned char *)buf + nat_int - PORT_INT;
  299. for (i = 0; i < cnt; i++) {
  300. /* set to FF if the value is negative */
  301. if (int_order == ENDIAN_LITTLE) {
  302. if (c1[PORT_INT - 1] & 0x80)
  303. memset(c2, 0xff, sizeof(int));
  304. }
  305. else {
  306. if (c1[0] & 0x80)
  307. memset(c2, 0xff, sizeof(int));
  308. }
  309. memcpy(c2, c1, PORT_INT);
  310. c1 += PORT_INT;
  311. c2 += sizeof(int);
  312. }
  313. }
  314. }
  315. else {
  316. /* read into buffer */
  317. buf_alloc(cnt * PORT_INT);
  318. ret = dig_fread(buffer, PORT_INT, cnt, fp);
  319. if (ret != cnt)
  320. return 0;
  321. /* set buffer to zero (positive numbers) */
  322. memset(buf, 0, cnt * sizeof(int));
  323. /* read from buffer in changed order */
  324. c1 = (unsigned char *)buffer;
  325. c2 = (unsigned char *)buf;
  326. for (i = 0; i < cnt; i++) {
  327. /* set to FF if the value is negative */
  328. if (Cur_Head->byte_order == ENDIAN_LITTLE) {
  329. if (c1[PORT_INT - 1] & 0x80)
  330. memset(c2, 0xff, sizeof(int));
  331. }
  332. else {
  333. if (c1[0] & 0x80)
  334. memset(c2, 0xff, sizeof(int));
  335. }
  336. for (j = 0; j < PORT_INT; j++)
  337. c2[Cur_Head->int_cnvrt[j]] = c1[j];
  338. c1 += PORT_INT;
  339. c2 += sizeof(int);
  340. }
  341. }
  342. return 1;
  343. }
  344. /* read shorts from the PVF file */
  345. int dig__fread_port_S(short *buf, int cnt, GVFILE * fp)
  346. {
  347. int i, j, ret;
  348. unsigned char *c1, *c2;
  349. if (Cur_Head->shrt_quick) {
  350. if (nat_shrt == PORT_SHORT) {
  351. ret = dig_fread(buf, PORT_SHORT, cnt, fp);
  352. if (ret != cnt)
  353. return 0;
  354. }
  355. else {
  356. /* read into buffer */
  357. buf_alloc(cnt * PORT_SHORT);
  358. if (0 >= (ret = dig_fread(buffer, PORT_SHORT, cnt, fp)))
  359. if (ret != cnt)
  360. return 0;
  361. /* set buffer to zero (positive numbers) */
  362. memset(buf, 0, cnt * sizeof(short));
  363. /* read from buffer in changed order */
  364. c1 = (unsigned char *)buffer;
  365. if (shrt_order == ENDIAN_LITTLE)
  366. c2 = (unsigned char *)buf;
  367. else
  368. c2 = (unsigned char *)buf + nat_shrt - PORT_SHORT;
  369. for (i = 0; i < cnt; i++) {
  370. /* set to FF if the value is negative */
  371. if (shrt_order == ENDIAN_LITTLE) {
  372. if (c1[PORT_SHORT - 1] & 0x80)
  373. memset(c2, 0xff, sizeof(short));
  374. }
  375. else {
  376. if (c1[0] & 0x80)
  377. memset(c2, 0xff, sizeof(short));
  378. }
  379. memcpy(c2, c1, PORT_SHORT);
  380. c1 += PORT_SHORT;
  381. c2 += sizeof(short);
  382. }
  383. }
  384. }
  385. else {
  386. /* read into buffer */
  387. buf_alloc(cnt * PORT_SHORT);
  388. ret = dig_fread(buffer, PORT_SHORT, cnt, fp);
  389. if (ret != cnt)
  390. return 0;
  391. /* set buffer to zero (positive numbers) */
  392. memset(buf, 0, cnt * sizeof(short));
  393. /* read from buffer in changed order */
  394. c1 = (unsigned char *)buffer;
  395. c2 = (unsigned char *)buf;
  396. for (i = 0; i < cnt; i++) {
  397. /* set to FF if the value is negative */
  398. if (Cur_Head->byte_order == ENDIAN_LITTLE) {
  399. if (c1[PORT_SHORT - 1] & 0x80)
  400. memset(c2, 0xff, sizeof(short));
  401. }
  402. else {
  403. if (c1[0] & 0x80)
  404. memset(c2, 0xff, sizeof(short));
  405. }
  406. for (j = 0; j < PORT_SHORT; j++)
  407. c2[Cur_Head->shrt_cnvrt[j]] = c1[j];
  408. c1 += PORT_SHORT;
  409. c2 += sizeof(short);
  410. }
  411. }
  412. return 1;
  413. }
  414. /* read chars from the PVF file */
  415. int dig__fread_port_C(char *buf, int cnt, GVFILE * fp)
  416. {
  417. int ret;
  418. ret = dig_fread(buf, PORT_CHAR, cnt, fp);
  419. if (ret != cnt)
  420. return 0;
  421. return 1;
  422. }
  423. /* read plus_t from the PVF file */
  424. /* plus_t is defined as int so we only retype pointer and use int function */
  425. int dig__fread_port_P(plus_t * buf, int cnt, GVFILE * fp)
  426. {
  427. int *ibuf;
  428. ibuf = (int *)buf;
  429. return (dig__fread_port_I(ibuf, cnt, fp));
  430. }
  431. /***************************** WRITE ************************************/
  432. int dig__fwrite_port_D(double *buf, /* DOUBLE */
  433. int cnt, GVFILE * fp)
  434. {
  435. int i, j;
  436. unsigned char *c1, *c2;
  437. if (Cur_Head->dbl_quick) {
  438. if (dig_fwrite(buf, PORT_DOUBLE, cnt, fp) == cnt)
  439. return 1;
  440. }
  441. else {
  442. buf_alloc(cnt * PORT_DOUBLE);
  443. c1 = (unsigned char *)buf;
  444. c2 = (unsigned char *)buffer;
  445. for (i = 0; i < cnt; i++) {
  446. for (j = 0; j < PORT_DOUBLE; j++)
  447. c2[j] = c1[Cur_Head->dbl_cnvrt[j]];
  448. c1 += sizeof(double);
  449. c2 += PORT_DOUBLE;
  450. }
  451. if (dig_fwrite(buffer, PORT_DOUBLE, cnt, fp) == cnt)
  452. return 1;
  453. }
  454. return 0;
  455. }
  456. int dig__fwrite_port_F(float *buf, /* FLOAT */
  457. int cnt, GVFILE * fp)
  458. {
  459. int i, j;
  460. unsigned char *c1, *c2;
  461. if (Cur_Head->flt_quick) {
  462. if (dig_fwrite(buf, PORT_FLOAT, cnt, fp) == cnt)
  463. return 1;
  464. }
  465. else {
  466. buf_alloc(cnt * PORT_FLOAT);
  467. c1 = (unsigned char *)buf;
  468. c2 = (unsigned char *)buffer;
  469. for (i = 0; i < cnt; i++) {
  470. for (j = 0; j < PORT_FLOAT; j++)
  471. c2[j] = c1[Cur_Head->flt_cnvrt[j]];
  472. c1 += sizeof(float);
  473. c2 += PORT_FLOAT;
  474. }
  475. if (dig_fwrite(buffer, PORT_FLOAT, cnt, fp) == cnt)
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. int dig__fwrite_port_O(off_t *buf, /* OFF_T */
  481. int cnt, GVFILE * fp, int port_off_t_size)
  482. {
  483. int i, j;
  484. unsigned char *c1, *c2;
  485. if (Cur_Head->off_t_quick) {
  486. if (nat_off_t == port_off_t_size) {
  487. if (dig_fwrite(buf, port_off_t_size, cnt, fp) == cnt)
  488. return 1;
  489. }
  490. else if (nat_off_t > port_off_t_size) {
  491. buf_alloc(cnt * port_off_t_size);
  492. if (off_t_order == ENDIAN_LITTLE)
  493. c1 = (unsigned char *)buf;
  494. else
  495. c1 = (unsigned char *)buf + nat_off_t - port_off_t_size;
  496. c2 = (unsigned char *)buffer;
  497. for (i = 0; i < cnt; i++) {
  498. memcpy(c2, c1, port_off_t_size);
  499. c1 += port_off_t_size;
  500. c2 += sizeof(off_t);
  501. }
  502. if (dig_fwrite(buffer, port_off_t_size, cnt, fp) == cnt)
  503. return 1;
  504. }
  505. else if (nat_off_t < port_off_t_size) {
  506. /* should never happen */
  507. G_fatal_error("Vector exceeds supported file size limit");
  508. }
  509. }
  510. else {
  511. if (nat_off_t >= port_off_t_size) {
  512. buf_alloc(cnt * port_off_t_size);
  513. c1 = (unsigned char *)buf;
  514. c2 = (unsigned char *)buffer;
  515. for (i = 0; i < cnt; i++) {
  516. for (j = 0; j < port_off_t_size; j++)
  517. c2[j] = c1[Cur_Head->off_t_cnvrt[j]];
  518. c1 += sizeof(off_t);
  519. c2 += port_off_t_size;
  520. }
  521. if (dig_fwrite(buffer, port_off_t_size, cnt, fp) == cnt)
  522. return 1;
  523. }
  524. else if (nat_off_t < port_off_t_size) {
  525. /* should never happen */
  526. G_fatal_error("Vector exceeds supported file size limit");
  527. }
  528. }
  529. return 0;
  530. }
  531. int dig__fwrite_port_L(long *buf, /* LONG */
  532. int cnt, GVFILE * fp)
  533. {
  534. int i, j;
  535. unsigned char *c1, *c2;
  536. if (Cur_Head->lng_quick) {
  537. if (nat_lng == PORT_LONG) {
  538. if (dig_fwrite(buf, PORT_LONG, cnt, fp) == cnt)
  539. return 1;
  540. }
  541. else {
  542. buf_alloc(cnt * PORT_LONG);
  543. if (lng_order == ENDIAN_LITTLE)
  544. c1 = (unsigned char *)buf;
  545. else
  546. c1 = (unsigned char *)buf + nat_lng - PORT_LONG;
  547. c2 = (unsigned char *)buffer;
  548. for (i = 0; i < cnt; i++) {
  549. memcpy(c2, c1, PORT_LONG);
  550. c1 += PORT_LONG;
  551. c2 += sizeof(long);
  552. }
  553. if (dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt)
  554. return 1;
  555. }
  556. }
  557. else {
  558. buf_alloc(cnt * PORT_LONG);
  559. c1 = (unsigned char *)buf;
  560. c2 = (unsigned char *)buffer;
  561. for (i = 0; i < cnt; i++) {
  562. for (j = 0; j < PORT_LONG; j++)
  563. c2[j] = c1[Cur_Head->lng_cnvrt[j]];
  564. c1 += sizeof(long);
  565. c2 += PORT_LONG;
  566. }
  567. if (dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt)
  568. return 1;
  569. }
  570. return 0;
  571. }
  572. int dig__fwrite_port_I(int *buf, /* INT */
  573. int cnt, GVFILE * fp)
  574. {
  575. int i, j;
  576. unsigned char *c1, *c2;
  577. if (Cur_Head->int_quick) {
  578. if (nat_int == PORT_INT) {
  579. if (dig_fwrite(buf, PORT_INT, cnt, fp) == cnt)
  580. return 1;
  581. }
  582. else {
  583. buf_alloc(cnt * PORT_INT);
  584. if (int_order == ENDIAN_LITTLE)
  585. c1 = (unsigned char *)buf;
  586. else
  587. c1 = (unsigned char *)buf + nat_int - PORT_INT;
  588. c2 = (unsigned char *)buffer;
  589. for (i = 0; i < cnt; i++) {
  590. memcpy(c2, c1, PORT_INT);
  591. c1 += PORT_INT;
  592. c2 += sizeof(int);
  593. }
  594. if (dig_fwrite(buffer, PORT_INT, cnt, fp) == cnt)
  595. return 1;
  596. }
  597. }
  598. else {
  599. buf_alloc(cnt * PORT_INT);
  600. c1 = (unsigned char *)buf;
  601. c2 = (unsigned char *)buffer;
  602. for (i = 0; i < cnt; i++) {
  603. for (j = 0; j < PORT_INT; j++)
  604. c2[j] = c1[Cur_Head->int_cnvrt[j]];
  605. c1 += sizeof(int);
  606. c2 += PORT_INT;
  607. }
  608. if (dig_fwrite(buffer, PORT_INT, cnt, fp) == cnt)
  609. return 1;
  610. }
  611. return 0;
  612. }
  613. int dig__fwrite_port_S(short *buf, /* SHORT */
  614. int cnt, GVFILE * fp)
  615. {
  616. int i, j;
  617. unsigned char *c1, *c2;
  618. if (Cur_Head->shrt_quick) {
  619. if (nat_shrt == PORT_SHORT) {
  620. if (dig_fwrite(buf, PORT_SHORT, cnt, fp) == cnt)
  621. return 1;
  622. }
  623. else {
  624. buf_alloc(cnt * PORT_SHORT);
  625. if (shrt_order == ENDIAN_LITTLE)
  626. c1 = (unsigned char *)buf;
  627. else
  628. c1 = (unsigned char *)buf + nat_shrt - PORT_SHORT;
  629. c2 = (unsigned char *)buffer;
  630. for (i = 0; i < cnt; i++) {
  631. memcpy(c2, c1, PORT_SHORT);
  632. c1 += PORT_SHORT;
  633. c2 += sizeof(short);
  634. }
  635. if (dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt)
  636. return 1;
  637. }
  638. }
  639. else {
  640. buf_alloc(cnt * PORT_SHORT);
  641. c1 = (unsigned char *)buf;
  642. c2 = (unsigned char *)buffer;
  643. for (i = 0; i < cnt; i++) {
  644. for (j = 0; j < PORT_SHORT; j++)
  645. c2[j] = c1[Cur_Head->shrt_cnvrt[j]];
  646. c1 += sizeof(short);
  647. c2 += PORT_SHORT;
  648. }
  649. if (dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt)
  650. return 1;
  651. }
  652. return 0;
  653. }
  654. /* plus_t is defined as int so we only retype pointer and use int function */
  655. int dig__fwrite_port_P(plus_t * buf, /* PLUS_T->INT */
  656. int cnt, GVFILE * fp)
  657. {
  658. return (dig__fwrite_port_I((int *)buf, cnt, fp));
  659. }
  660. int dig__fwrite_port_C(char *buf, /* CHAR */
  661. int cnt, GVFILE * fp)
  662. {
  663. if (dig_fwrite(buf, PORT_CHAR, cnt, fp) == cnt)
  664. return 1;
  665. return 0;
  666. }
  667. /* set portable info structure to byte order of file */
  668. void dig_init_portable(struct Port_info *port, int byte_order)
  669. {
  670. int i;
  671. port_init();
  672. port->byte_order = byte_order;
  673. /* double */
  674. if (port->byte_order == dbl_order)
  675. port->dbl_quick = TRUE;
  676. else
  677. port->dbl_quick = FALSE;
  678. for (i = 0; i < PORT_DOUBLE; i++) {
  679. if (port->byte_order == ENDIAN_BIG)
  680. port->dbl_cnvrt[i] = dbl_cnvrt[i];
  681. else
  682. port->dbl_cnvrt[i] = dbl_cnvrt[PORT_DOUBLE - i - 1];
  683. }
  684. /* float */
  685. if (port->byte_order == flt_order)
  686. port->flt_quick = TRUE;
  687. else
  688. port->flt_quick = FALSE;
  689. for (i = 0; i < PORT_FLOAT; i++) {
  690. if (port->byte_order == ENDIAN_BIG)
  691. port->flt_cnvrt[i] = flt_cnvrt[i];
  692. else
  693. port->flt_cnvrt[i] = flt_cnvrt[PORT_FLOAT - i - 1];
  694. }
  695. /* long */
  696. if (port->byte_order == lng_order)
  697. port->lng_quick = TRUE;
  698. else
  699. port->lng_quick = FALSE;
  700. for (i = 0; i < PORT_LONG; i++) {
  701. if (port->byte_order == ENDIAN_BIG)
  702. port->lng_cnvrt[i] = lng_cnvrt[i];
  703. else
  704. port->lng_cnvrt[i] = lng_cnvrt[PORT_LONG - i - 1];
  705. }
  706. /* int */
  707. if (port->byte_order == int_order)
  708. port->int_quick = TRUE;
  709. else
  710. port->int_quick = FALSE;
  711. for (i = 0; i < PORT_INT; i++) {
  712. if (port->byte_order == ENDIAN_BIG)
  713. port->int_cnvrt[i] = int_cnvrt[i];
  714. else
  715. port->int_cnvrt[i] = int_cnvrt[PORT_INT - i - 1];
  716. }
  717. /* short */
  718. if (port->byte_order == shrt_order)
  719. port->shrt_quick = TRUE;
  720. else
  721. port->shrt_quick = FALSE;
  722. for (i = 0; i < PORT_SHORT; i++) {
  723. if (port->byte_order == ENDIAN_BIG)
  724. port->shrt_cnvrt[i] = shrt_cnvrt[i];
  725. else
  726. port->shrt_cnvrt[i] = shrt_cnvrt[PORT_SHORT - i - 1];
  727. }
  728. /* off_t */
  729. if (port->byte_order == off_t_order)
  730. port->off_t_quick = TRUE;
  731. else
  732. port->off_t_quick = FALSE;
  733. for (i = 0; i < nat_off_t; i++) {
  734. if (port->byte_order == ENDIAN_BIG)
  735. port->off_t_cnvrt[i] = off_t_cnvrt[i];
  736. else
  737. port->off_t_cnvrt[i] = off_t_cnvrt[nat_off_t - i - 1];
  738. }
  739. return;
  740. }
  741. /* set current portable info */
  742. int dig_set_cur_port(struct Port_info *port)
  743. {
  744. Cur_Head = port;
  745. return 0;
  746. }
  747. int dig__byte_order_out()
  748. {
  749. if (dbl_order == ENDIAN_LITTLE)
  750. return (ENDIAN_LITTLE);
  751. else
  752. return (ENDIAN_BIG);
  753. }