File: mupip_endiancvt.c

package info (click to toggle)
fis-gtm 7.1-006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,908 kB
  • sloc: ansic: 344,906; asm: 5,184; csh: 4,859; sh: 2,000; awk: 294; makefile: 73; sed: 13
file content (1870 lines) | stat: -rw-r--r-- 68,460 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
/****************************************************************
 *								*
 * Copyright (c) 2006-2024 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"

#include "gtm_fcntl.h"
#include "gtm_unistd.h"
#include "gtm_socket.h"
#include "gtm_inet.h"
#include "gtm_stdio.h"
#include "gtm_string.h"
#include "gtm_sem.h"

#include <sys/wait.h>
#include <stddef.h>
#include <errno.h>
#include "gtm_time.h"
#ifdef __MVS__
#include "gtm_zos_io.h"
#include <sys/time.h>
#endif

#include "gdsroot.h"
#include "v15_gdsroot.h"	/* for v15_trans_num */
#include "gtm_facility.h"
#include "fileinfo.h"
#include "gdsbt.h"
#include "gdsfhead.h"
#include "v6_gdsfhead.h"
#include "db_header_conversion.h"
#include "gdsblk.h"
#include "filestruct.h"
#include "jnl.h"
#include "gdsbml.h"
#include "cli.h"
#include "iosp.h"
#include "copy.h"
#include "error.h"
#include "gtmio.h"
#include "iotimer.h"
#include "gt_timer.h"
#include "stp_parms.h"
#include "gtm_stat.h"
#include "eintr_wrappers.h"
#include "util.h"
#include "gtm_caseconv.h"
#include "io.h"
#include "is_proc_alive.h"
#include "mu_rndwn_file.h"
#include "mupip_exit.h"
#include "mu_gv_cur_reg_init.h"
#include "mupip_endiancvt.h"
#include "gtmmsg.h"
#include "wcs_sleep.h"
#include "gvcst_lbm_check.h"		/* gvcst_blk_ever_allocated */
#include "shmpool.h"
#include "min_max.h"
#include "spec_type.h"			/* collation info */
#include "anticipatory_freeze.h"
#include "gtmcrypt.h"
#include "db_ipcs_reset.h"
#include "db_write_eof_block.h"

GBLREF	gd_region		*gv_cur_region;
GBLREF	mstr			pvt_crypt_buf;

LITREF	char			*gtm_dbversion_table[];

error_def(ERR_BADTAG);
error_def(ERR_DBRDONLY);
error_def(ERR_ENDIANCVT);
error_def(ERR_IOEOF);
error_def(ERR_MUNOACTION);
error_def(ERR_MUNOFINISH);
error_def(ERR_MUPCLIERR);
error_def(ERR_MUSTANDALONE);
error_def(ERR_NOENDIANCVT);
error_def(ERR_TEXT);

#define ABANDONED_KILLS		"abandoned kills present"
#define DBCREATE		"database creation in progress"
#define DBCORRUPT		"the database is corrupted"
#define GTCMSERVERACTIVE	"a GT.CM server accessing the database"
#define KILLINPROG		"kills in progress"
#define NOTCURRDBFORMAT		"database format is not the current version"
#define NOTCURRMDBFORMAT	"minor database format is not the current version"
#define NOTFULLYUPGRADED	"some blocks are not upgraded to the current version"
#define	RECOVINTRPT		"recovery was interrupted"

#define MAX_CONF_RESPONSE	30

/* No journal pool for endiancvt, so ignore csa everywhere. Saves a bunch of CSA_ARG(NULL)s. */
#define GTM_PUTMSG_CSA(...)	gtm_putmsg_csa(CSA_ARG(NULL) __VA_ARGS__)

typedef struct
{	/* adapted from dbcertify.h */
	unsigned int	top;		/* Offset to top of the key */
	unsigned int	end;		/* End of the current key */
	unsigned int	gvn_len;	/* Length of key */
	unsigned char	*key;		/* Pointer to the key */
} end_gv_key;

typedef struct
{
	int			db_fd;
	int			outdb_fd;		/* FD_INVALID if inplace */
	boolean_t		inplace;		/* update in place */
	boolean_t		endian_native;		/* original database */
	block_id		tot_blks;
	int			bsize;			/* GDS block size */
	int4			startvbn;		/* in DISK_BLOCK_SIZE units */
	block_id		last_blk_cvt;		/* highest block converted so far not lbm */
	char			*database_fn;
	int			database_fn_len;
	uint4			is_encrypted;
	block_id		encryption_hash_cutoff;
	boolean_t		non_null_iv;
	trans_num		encryption_hash2_start_tn;
	enc_handles		encr_handles;
	struct	/* used by find_dtblk related routines */
	{
		char		*buff;
		char		*dtrbuff;	/* keep the DT root */
		block_id	blkid;
		boolean_t	native;		/* records are native endian */
		boolean_t	dtrnative;	/* records in dtrbuff are native endian */
		int		count;		/* number of times needed */
	}	dtblk;
} endian_info;

void		endian_header(sgmnt_data *new, sgmnt_data *old, boolean_t new_is_native);
void		v6_endian_header(v6_sgmnt_data *new, v6_sgmnt_data *old, boolean_t new_is_native);
int4		endian_process(endian_info *info, sgmnt_data *new_data, sgmnt_data *old_data, boolean_t override_specified);
void		endian_cvt_blk_hdr(blk_hdr_ptr_t blkhdr, boolean_t new_is_native, boolean_t make_empty);
void		endian_cvt_blk_recs(endian_info *info, char *new_block, blk_hdr_ptr_t blkhdr, block_id blknum);
char		*endian_read_dbblk(endian_info *info, block_id blk_to_get);
void		endian_find_key(endian_info *info, end_gv_key *gv_key, char *rec_p, int rec_len,
		                int blk_levl, boolean_t long_blk_id);
boolean_t	endian_match_key(end_gv_key *gv_key1, int blk_levl, end_gv_key *key2);
block_id	endian_find_dtblk(endian_info *info, end_gv_key *gv_key);

/* If we acquired standalone access, we need to release it before we exit. Ideally, mupip_exit_handler should take care of doing
 * it. But, since we free up memory allocated to gv_cur_region before exiting out of this module. An alternative would be to free
 * gv_cur_region AFTER db_ipcs_reset in mupip_exit_handler but since various code paths set gv_cur_region, the implication of such
 * a change is not clear at this writing.
 */
#define DO_STANDALONE_CLNUP_IF_NEEDED(ENDIAN_NATIVE)			\
{									\
	if (ENDIAN_NATIVE)						\
	{	/* release standalone access */				\
		assert(FILE_INFO(gv_cur_region)->grabbed_access_sem);	\
		db_ipcs_reset(gv_cur_region);				\
		mu_gv_cur_reg_free();					\
	}								\
}

void mupip_endiancvt(void)
{
	block_id		blk_num;
	boolean_t		outdb_specified, endian_native, swap_boolean, got_standalone, override_specified, swap_boolean2;
	char			db_name[MAX_FN_LEN + 1], *t_name;
	char			outdb[MAX_FN_LEN + 1], conf_buff[MAX_CONF_RESPONSE + 1], *response;
	char			*errptr, *check_error, *mastermap;
	char			*from_endian, *to_endian;
	endian_info		info;
	endian32_struct		endian_check;
	enum db_ver		swap_dbver;
	enum mdb_ver		swap_mdbver;
	int			rc;
	int			i, db_fd, outdb_fd, mastermap_size;
	int			gtmcrypt_errno;
	int4			status, save_errno;
	sgmnt_data		*old_data, *new_data;
	trans_num		curr_tn;
	uint4			swap_uint4;
	uint4			cli_status;
	unsigned short		n_len, outdb_len, t_len;
	ZOS_ONLY(int		realfiletag;)
	DCL_THREADGBL_ACCESS;

	SETUP_THREADGBL_ACCESS;
	if (CLI_PRESENT == (cli_status = cli_present("OUTDB")))
	{
		outdb_specified = TRUE;
		outdb_len = SIZEOF(outdb) - 1;
		if (!cli_get_str("OUTDB", outdb, &outdb_len))
			mupip_exit(ERR_MUPCLIERR);
	} else
		outdb_specified = FALSE;
	n_len = SIZEOF(db_name) - 1;
	if (cli_get_str("DATABASE", db_name, &n_len) == FALSE)
		mupip_exit(ERR_MUPCLIERR);

	OPENFILE(db_name, (!outdb_specified ? O_RDWR : O_RDONLY), db_fd);
	if (FD_INVALID == db_fd)
	{
		save_errno = errno;
		util_out_print("Error accessing database file !AD. Aborting endiancvt.", TRUE, n_len, db_name);
		errptr = (char *)STRERROR(save_errno);
		util_out_print("open : !AZ", TRUE, errptr);
		mupip_exit(save_errno);
	}
#	ifdef __MVS__
	if (-1 == gtm_zos_tag_to_policy(db_fd, TAG_BINARY, &realfiletag))
		TAG_POLICY_GTM_PUTMSG(db_name, realfiletag, TAG_BINARY, errno);
#	endif
	old_data = (sgmnt_data *)malloc(SIZEOF(sgmnt_data));
	LSEEKREAD(db_fd, 0, old_data, SIZEOF(sgmnt_data), save_errno);
	if (0 == memcmp(old_data, V6_GDS_LABEL, GDS_LABEL_SZ - 1))
		db_header_upconv(old_data);
	if (0 != save_errno)
	{
		free(old_data);
		CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
		util_out_print("Error reading database file !AD header. Aborting endiancvt.", TRUE, n_len, db_name);
		if (-1 != save_errno)
		{
			errptr = (char *)STRERROR(save_errno);
			util_out_print("read : !AZ", TRUE, errptr);
			mupip_exit(save_errno);
		} else
			mupip_exit(ERR_IOEOF);
	}
	if (MEMCMP_LIT(&old_data->label[0], GDS_LABEL) && MEMCMP_LIT(&old_data->label[0], V6_GDS_LABEL))
	{
		util_out_print("Database file !AD has an unrecognizable format", TRUE, n_len, db_name);
		free(old_data);
		CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
		mupip_exit(ERR_MUNOACTION);
	}
	override_specified = (CLI_PRESENT == (cli_status = cli_present("OVERRIDE")));
	check_error = NULL;
	endian_check.word32 = (uint4)old_data->minor_dbver;
#ifdef BIGENDIAN
	if (!endian_check.shorts.big_endian)
#else
	if (!endian_check.shorts.little_endian)
#endif
	{
		endian_native = FALSE;		/* nobody can be using the db */
		/* do checks after swapping fields */
		assert(SIZEOF(int4) == SIZEOF(old_data->desired_db_format));
		/* If OVERRIDE is specified, skip checking for those fields that are not critical for the integrity of the db.
		 * Any field that indicates the db is potentially damaged cannot be overridden.
		 */
		if (!override_specified)
		{
			swap_mdbver = (enum mdb_ver)GTM_BYTESWAP_32(old_data->minor_dbver);
			if ((GDSMVCURR != swap_mdbver) && (BLK_ID_32_MVER != swap_mdbver))
			{
				check_error = NOTCURRMDBFORMAT;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			swap_uint4 = GTM_BYTESWAP_32(old_data->kill_in_prog);
			if (0 != swap_uint4)
			{
				check_error = KILLINPROG;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			swap_uint4 = GTM_BYTESWAP_32(old_data->abandoned_kills);
			if (0 != swap_uint4)
			{
				check_error = ABANDONED_KILLS;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			swap_uint4 = GTM_BYTESWAP_32(old_data->rc_srv_cnt);
			if (0 != swap_uint4)
			{
				check_error = GTCMSERVERACTIVE;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			swap_boolean = GTM_BYTESWAP_32(old_data->createcomplete);
			swap_boolean = ((GTM_BYTESWAP_32(old_data->last_mdb_ver) >= GDSMV71003)
					|| (GTM_BYTESWAP_32(old_data->minor_dbver) >= GDSMV71003)) ?
				!swap_boolean : GTM_BYTESWAP_32(old_data->createinprogress);
			if (swap_boolean)
			{
				check_error = DBCREATE;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
		}
		swap_dbver = (enum db_ver)GTM_BYTESWAP_32(old_data->desired_db_format);
		if ((0 >= swap_dbver) || (GDSV7 < swap_dbver))
		{
			check_error = NOTCURRDBFORMAT;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		assert(SIZEOF(int4) == SIZEOF(old_data->fully_upgraded));
		swap_boolean = GTM_BYTESWAP_32(old_data->fully_upgraded);
		if (!swap_boolean)
		{
			check_error = NOTFULLYUPGRADED;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		swap_uint4 = GTM_BYTESWAP_32(old_data->recov_interrupted);
		if (0 != swap_uint4)
		{
			check_error = RECOVINTRPT;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		swap_uint4 = GTM_BYTESWAP_32(old_data->file_corrupt);
		if (0 != swap_uint4)
		{
			check_error = DBCORRUPT;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
	} else
	{
		endian_native = TRUE;		/* need to get standalone */
		mu_gv_cur_reg_init();
		strcpy((char *)gv_cur_region->dyn.addr->fname, db_name);
		gv_cur_region->dyn.addr->fname_len = n_len;
		got_standalone = STANDALONE(gv_cur_region);
		if (FALSE == got_standalone)
		{
			mu_gv_cur_reg_free();
			free(old_data);
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			GTM_PUTMSG_CSA(VARLSTCNT(4) MAKE_MSG_TYPE(ERR_MUSTANDALONE, ERROR), 2, n_len, db_name);
			mupip_exit(ERR_MUNOACTION);
		}
		/* Now that we got standalone access, re-read uptodate fileheader in case it changed in between.
		 * And redo the checks that we did before getting standalone access to make sure nothing changed.
		 */
		LSEEKREAD(db_fd, 0, old_data, SIZEOF(sgmnt_data), save_errno);
		if (0 == memcmp(old_data, V6_GDS_LABEL, GDS_LABEL_SZ - 1))
			db_header_upconv(old_data);
		if (0 != save_errno)
		{
			assert(FALSE);
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			free(old_data);
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			util_out_print("Error reading database file !AD header. Aborting endiancvt.", TRUE, n_len, db_name);
			if (-1 != save_errno)
			{
				errptr = (char *)STRERROR(save_errno);
				util_out_print("read : !AZ", TRUE, errptr);
				mupip_exit(save_errno);
			} else
				mupip_exit(ERR_IOEOF);
		}
		if (MEMCMP_LIT(&old_data->label[0], GDS_LABEL) && MEMCMP_LIT(&old_data->label[0], V6_GDS_LABEL))
		{
			assert(FALSE);
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			util_out_print("Database file !AD has an unrecognizable format2", TRUE, n_len, db_name);
			free(old_data);
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			mupip_exit(ERR_MUNOACTION);
		}
		if (endian_check.word32 != (uint4)old_data->minor_dbver)
		{	/* file header endianness changed between the two LSEEKREADs. abort endiancvt */
			assert(FALSE);
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			util_out_print("Database file !AD endianness changed during endiancvt. Aborting", TRUE, n_len, db_name);
			free(old_data);
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			mupip_exit(ERR_MUNOACTION);
		}
		/* Now that the redone checks are okay, go ahead with the rest of the endian conversion */
		if (gv_cur_region->read_only && !outdb_specified)
		{
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			free(old_data);
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			GTM_PUTMSG_CSA(VARLSTCNT(4) ERR_DBRDONLY, 2, n_len, db_name);
			mupip_exit(ERR_MUNOACTION);
		}
		if (!override_specified)
		{
			if ((GDSMVCURR != old_data->minor_dbver) && (BLK_ID_32_MVER != old_data->minor_dbver))
			{
				check_error = NOTCURRMDBFORMAT;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			if (0 != old_data->kill_in_prog)
			{
				check_error = KILLINPROG;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			if (0 != old_data->abandoned_kills)
			{
				check_error = ABANDONED_KILLS;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			if (0 != old_data->rc_srv_cnt)
			{
				check_error = GTCMSERVERACTIVE;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
			if (CREATE_IN_PROGRESS(old_data))
			{
				check_error = DBCREATE;
				GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
			}
		}
		if ((0 >= old_data->desired_db_format) || (GDSV7 < old_data->desired_db_format))
		{
			check_error = NOTCURRDBFORMAT;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		if (!old_data->fully_upgraded)
		{
			check_error = NOTFULLYUPGRADED;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		if (0 != old_data->recov_interrupted)
		{
			check_error = RECOVINTRPT;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		if (0 != old_data->file_corrupt)
		{
			check_error = DBCORRUPT;
			GTM_PUTMSG_CSA(VARLSTCNT(6) ERR_NOENDIANCVT, 4, n_len, db_name, LEN_AND_STR(check_error));
		}
		if (!check_error && !outdb_specified)
		{
			if (JNL_ENABLED(old_data))
			{
				/* report what we are doing ERR_JNLSTATE */
				util_out_print("!_Journaling was enabled - now closed",TRUE);
				old_data->jnl_state = jnl_closed;
			}
			if (REPL_ENABLED(old_data))
			{
				/* report what we are doing ERR_REPLSTATE */
				util_out_print("!_Replication was enabled - now closed",TRUE);
				old_data->repl_state = repl_closed;
			}
		}
	}
	if (check_error)
	{
		DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
		free(old_data);
		CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
		mupip_exit(ERR_MUNOACTION);
	}
	info.is_encrypted = endian_native ? old_data->is_encrypted : GTM_BYTESWAP_32(old_data->is_encrypted);
	info.encryption_hash_cutoff = endian_native
		? old_data->encryption_hash_cutoff
		: BLK_ID_BYTESWAP(old_data->encryption_hash_cutoff);
	if (USES_ANY_KEY(&info))
	{	/* Database is encrypted. Initialize encryption and setup the keys to be used in later encryption/decryption */
		info.database_fn = &db_name[0];
		info.database_fn_len = n_len;
		INIT_PROC_ENCRYPTION(gtmcrypt_errno);
		if (0 == gtmcrypt_errno)
		{
			info.encr_handles.encr_key_handle = GTMCRYPT_INVALID_KEY_HANDLE;
			info.encr_handles.encr_key_handle2 = GTMCRYPT_INVALID_KEY_HANDLE;
			if (IS_ENCRYPTED(info.is_encrypted))
			{
				GTMCRYPT_INIT_BOTH_CIPHER_CONTEXTS(NULL, old_data->encryption_hash,
						info.database_fn_len, info.database_fn,
						info.encr_handles.encr_key_handle, gtmcrypt_errno);
			}
			if ((0 == gtmcrypt_errno) && USES_NEW_KEY(&info))
			{
				GTMCRYPT_INIT_BOTH_CIPHER_CONTEXTS(NULL, old_data->encryption_hash2,
						info.database_fn_len, info.database_fn,
						info.encr_handles.encr_key_handle2, gtmcrypt_errno);
			}
		}
		if (0 != gtmcrypt_errno)
		{
			GTMCRYPT_REPORT_ERROR(gtmcrypt_errno, gtm_putmsg, n_len, db_name);
			mupip_exit(gtmcrypt_errno);
		}
		REALLOC_CRYPTBUF_IF_NEEDED(SIZEOF(blk_hdr));
	}
	from_endian = endian_check.shorts.big_endian ? "BIG" : "LITTLE";
	to_endian = endian_check.shorts.big_endian ? "LITTLE" : "BIG";
	util_out_print("Converting database file !AD from !AZ endian to !AZ endian on a !AZ endian system", TRUE,
		n_len, db_name, from_endian, to_endian, ENDIANTHIS);
	if (!outdb_specified)
	{
		util_out_print("Converting in place - database will be damaged by an abnormal termination", TRUE);
		util_out_print("You must have a backup before proceeding", TRUE);
	} else
		util_out_print("Converting to new file !AD", TRUE, outdb_len, outdb);
	util_out_print("Proceed [yes/no] ?", TRUE);
	response = util_input(conf_buff, MAX_CONF_RESPONSE, stdin, TRUE);
	if (NULL == response || ('Y' != conf_buff[0] && 'y' != conf_buff[0]))
	{
		DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
		free(old_data);
		CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
		mupip_exit(ERR_MUNOACTION);
	}
	new_data = (sgmnt_data *)malloc(SIZEOF(sgmnt_data));
	memcpy(new_data, old_data, SIZEOF(sgmnt_data));
	new_data->file_corrupt = endian_native ? GTM_BYTESWAP_32(TRUE) : TRUE;
	info.db_fd = db_fd;
	info.inplace = !outdb_specified;
	info.endian_native = info.dtblk.native = info.dtblk.dtrnative = endian_native;
	info.tot_blks = info.last_blk_cvt = info.bsize = info.startvbn = 0;
	info.dtblk.buff = info.dtblk.dtrbuff = NULL;
	info.dtblk.blkid = -1;			/* invalid block number */
	info.dtblk.count = 0;
	if (0 == memcmp(new_data->label, V6_GDS_LABEL, GDS_LABEL_SZ - 1))
	{	/* convert V6 file header fields */
		v6_endian_header((v6_sgmnt_data_ptr_t)new_data, (v6_sgmnt_data_ptr_t)old_data, !endian_native);
	} else
		endian_header(new_data, old_data, !endian_native); /* convert file header fields */
	if (outdb_specified)
	{
		OPENFILE3(outdb, O_RDWR | O_CREAT | O_EXCL, 0666, outdb_fd);
		if (FD_INVALID == outdb_fd)
		{	/* error */
			save_errno = errno;
			util_out_print("Error creating converted databasae file !AD.  Aborting endiancvt.", TRUE, outdb_len, outdb);
			errptr = (char *)STRERROR(save_errno);
			util_out_print("open : !AZ", TRUE, errptr);
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			free(new_data);
			free(old_data);
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			mupip_exit(save_errno);
		}
#		ifdef __MVS__
		if (-1 == gtm_zos_set_tag(outdb_fd, TAG_BINARY, TAG_NOTTEXT, TAG_DONTFORCE, &realfiletag))
			TAG_POLICY_GTM_PUTMSG(outdb, realfiletag, TAG_BINARY, errno);
#		endif
		new_data->file_corrupt = endian_native ? GTM_BYTESWAP_32(TRUE) : TRUE;
		LSEEKWRITE(outdb_fd, 0, new_data, SIZEOF(sgmnt_data), save_errno);
		if (0 != save_errno)
		{
			free(new_data);
			free(old_data);
			CLOSEFILE_RESET(outdb_fd, rc);	/* resets "outdb_fd" to FD_INVALID */
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			util_out_print("Error writing converted database file !AD header.  Aborting endiancvt.", TRUE,
				outdb_len, outdb);
			if (-1 != save_errno)
			{
				errptr = (char *)STRERROR(save_errno);
				util_out_print("write : !AZ", TRUE, errptr);
				mupip_exit(save_errno);
			} else
			{
				util_out_print("write : unexpected error", TRUE);
				mupip_exit(ERR_MUNOFINISH);
			}
		}
		/* read master bit map from old file */
		mastermap_size = endian_native ? old_data->master_map_len : new_data->master_map_len;
		mastermap = malloc(mastermap_size);
		LSEEKREAD(db_fd, SGMNT_HDR_LEN, mastermap, mastermap_size, save_errno);
		if (0 != save_errno)
		{
			free(new_data);
			free(old_data);
			CLOSEFILE_RESET(outdb_fd, rc);	/* resets "outdb_fd" to FD_INVALID */
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			util_out_print("Error reading database file !AD master map.  Aborting endiancvt.", TRUE,
				n_len, db_name);
			if (-1 != save_errno)
			{
				errptr = (char *)STRERROR(save_errno);
				util_out_print("read : !AZ", TRUE, errptr);
				mupip_exit(save_errno);
			} else
				mupip_exit(ERR_IOEOF);
		}
		/* write master bit map to new file */
		LSEEKWRITE(outdb_fd, SGMNT_HDR_LEN, mastermap, mastermap_size, save_errno);
		if (0 != save_errno)
		{
			free(new_data);
			free(old_data);
			CLOSEFILE_RESET(outdb_fd, rc);	/* resets "outdb_fd" to FD_INVALID */
			CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
			DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
			util_out_print("Error writing converted database file !AD master map.  Aborting endiancvt.", TRUE,
				outdb_len, outdb);
			if (-1 != save_errno)
			{
				errptr = (char *)STRERROR(save_errno);
				util_out_print("write : !AZ", TRUE, errptr);
				mupip_exit(save_errno);
			} else
			{
				util_out_print("write : unexpected error", TRUE);
				mupip_exit(ERR_MUNOFINISH);
			}
		}
	} else
		outdb_fd = FD_INVALID;
	info.outdb_fd = outdb_fd;
	status = endian_process(&info, new_data, old_data, override_specified);
	if (0 != status)
	{
		/* db_ipcs_reset in the macro below works even with the now converted opposite endian header since it just sets
		 * csd->s{e|h}mid to INVALIDS{E|H}MID and zeroes s{e|h}m_ctime.
		 */
		DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
		free(new_data);
		free(old_data);
		CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
		if (outdb_specified)
			CLOSEFILE_RESET(outdb_fd, rc);	/* resets "outdb_fd" to FD_INVALID */
		mupip_exit(ERR_MUNOFINISH);	/* endian_process issued specific message */
	}
	new_data->file_corrupt = endian_native ? GTM_BYTESWAP_32(FALSE) : FALSE;
	if (outdb_specified)
	{
		LSEEKWRITE(outdb_fd, 0, new_data, SIZEOF(sgmnt_data), save_errno);
	} else
	{
		DB_LSEEKWRITE((sgmnt_addrs *)NULL, ((unix_db_info *)NULL), (char *)NULL, db_fd, 0,
				new_data, SIZEOF(sgmnt_data), save_errno);
	}
	if (0 != save_errno)
	{
		free(new_data);
		free(old_data);
		if (outdb_specified)
			CLOSEFILE_RESET(outdb_fd, rc);	/* resets "outdb_fd" to FD_INVALID */
		CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
		DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
		util_out_print("Error writing!AZ database file !AD header.  Aborting endiancvt.", TRUE,
			outdb_specified ? "new" : "", outdb_specified ? outdb_len : n_len, outdb_specified ? outdb : db_name);
		if (-1 != save_errno)
		{
			errptr = (char *)STRERROR(save_errno);
			util_out_print("write : !AZ", TRUE, errptr);
			mupip_exit(save_errno);
		} else
		{
			util_out_print("write : unexpected error", TRUE);
			mupip_exit(ERR_MUNOFINISH);
		}
	}
	DO_STANDALONE_CLNUP_IF_NEEDED(endian_native);
	free(new_data);
	free(old_data);
	if (outdb_specified)
	{
		GTM_FSYNC(outdb_fd, rc);
	} else
		GTM_DB_FSYNC((sgmnt_addrs *)NULL, db_fd, rc);
	if (-1 == rc)
	{
		save_errno = errno;
		assert(FALSE);
		if (-1 != save_errno)
		{
			errptr = (char *)STRERROR(save_errno);
			util_out_print("fsync : !AZ : !AZ", TRUE, (outdb_specified ? "outdb_fd" : "db_fd"), errptr);
			mupip_exit(save_errno);
		} else
		{
			util_out_print("fsync : !AZ : unexpected error", TRUE, (outdb_specified ? "outdb_fd" : "db_fd"));
			mupip_exit(ERR_MUNOFINISH);
		}
	}
	CLOSEFILE_RESET(db_fd, rc);	/* resets "db_fd" to FD_INVALID */
	if (outdb_specified)
		CLOSEFILE_RESET(outdb_fd, rc);	/* resets "outdb_fd" to FD_INVALID */
	/* Display success message only after all data has been synced to disk and the file descriptors closed */
	GTM_PUTMSG_CSA(VARLSTCNT(7) ERR_ENDIANCVT, 5, n_len, db_name, from_endian, to_endian, ENDIANTHIS);
	mupip_exit(SS_NORMAL);
}

#define SWAP_SD4(FIELD)					\
{							\
	assert(SIZEOF(int4) == SIZEOF(old->FIELD));	\
	new->FIELD = GTM_BYTESWAP_32(old->FIELD);	\
}
#define SWAP_SD4_CAST(FIELD, castType)				\
{								\
	assert(SIZEOF(int4) == SIZEOF(old->FIELD));		\
	new->FIELD = (castType)GTM_BYTESWAP_32(old->FIELD);	\
}
#define SWAP_SD8(FIELD)					\
{							\
	assert(SIZEOF(gtm_int8) == SIZEOF(old->FIELD));	\
	new->FIELD = GTM_BYTESWAP_64(old->FIELD);	\
}

void endian_header(sgmnt_data *new, sgmnt_data *old, boolean_t new_is_native)
{
	int	idx;
	time_t	ctime;

	/************* MOSTLY STATIC DATABASE STATE FIELDS **************************/
	SWAP_SD4(blk_size);
	SWAP_SD4(v6_master_map_len);
	SWAP_SD4(bplmap);
	SWAP_SD4(v6_start_vbn);
	SWAP_SD4_CAST(acc_meth, enum db_acc_method);
	SWAP_SD4(max_bts);
	SWAP_SD4(n_bts);
	SWAP_SD4(bt_buckets);
	SWAP_SD4(reserved_bytes);
	SWAP_SD4(max_rec_size);
	SWAP_SD4(max_key_size);
	SWAP_SD4(lock_space_size);
	SWAP_SD4(extension_size);
	SWAP_SD4(def_coll);
	SWAP_SD4(def_coll_ver);
	SWAP_SD4(write_fullblk);
	SWAP_SD4(statsdb_allocation);
	SWAP_SD4(std_null_coll);
	SWAP_SD4(null_subs);
	SWAP_SD4(free_space);
	SWAP_SD4(mutex_spin_parms.mutex_hard_spin_count);	/* gdsbt.h */
	SWAP_SD4(mutex_spin_parms.mutex_sleep_spin_count);
	SWAP_SD4(mutex_spin_parms.mutex_spin_sleep_mask);
	SWAP_SD4(mutex_spin_parms.mutex_que_entry_space_size);
	SWAP_SD4(max_update_array_size);
	SWAP_SD4(max_non_bm_update_array_size);
	/* SWAP_SD4(file_corrupt); is set in main routine	*/
	SWAP_SD4_CAST(minor_dbver, enum mdb_ver);
	SWAP_SD4(jnl_checksum);
	SWAP_SD4(wcs_phase2_commit_wait_spincnt);
	SWAP_SD4_CAST(last_mdb_ver, enum mdb_ver);
	/************* FIELDS SET AT CREATION TIME ********************************/
	new->createcomplete = TRUE;
	assert(!old->createinprogress);
	assert(SIZEOF(int4) == SIZEOF(old->creation_time4));
	time(&ctime);
	assert(SIZEOF(ctime) >= SIZEOF(int4));
	new->creation_time4 = (int4)ctime;/* Take only lower order 4-bytes of current time */
	if (!new_is_native)
		SWAP_SD4(creation_time4);
	/************* FIELDS USED BY TN WARN PROCESSING *************************/
	SWAP_SD8(max_tn);
	SWAP_SD8(max_tn_warn);
	/************* FIELDS SET BY MUPIP BACKUP/REORG *************************/
	SWAP_SD8(last_inc_backup);
	SWAP_SD8(last_com_backup);
	SWAP_SD8(last_rec_backup);
	SWAP_SD4(v6_last_inc_bkup_last_blk);
	SWAP_SD4(v6_last_com_bkup_last_blk);
	SWAP_SD4(v6_last_rec_bkup_last_blk);
	SWAP_SD4(v6_reorg_restart_block);
	SWAP_SD8(last_start_backup);
	/************* FIELDS SET WHEN DB IS OPEN ********************************/
	new->image_count = 0;		/* should be zero when db is not open so reset it unconditionally */
	new->freeze = 0;		/* should be zero when db is not open so reset it unconditionally */
	SWAP_SD4(kill_in_prog);
	SWAP_SD4(abandoned_kills);
	/************* FIELDS USED IN V4 <==> V5 COMPATIBILITY MODE ****************/
	SWAP_SD8(tn_upgrd_blks_0);
	SWAP_SD8(desired_db_format_tn);
	SWAP_SD8(reorg_db_fmt_start_tn);
	SWAP_SD4(v6_reorg_upgrd_dwngrd_restart_block);
	SWAP_SD4(v6_blks_to_upgrd);
	SWAP_SD4(v6_blks_to_upgrd_subzero_error);
	SWAP_SD4_CAST(desired_db_format, enum db_ver);
	SWAP_SD4(fully_upgraded);	/* should be TRUE */
	assert(new->fully_upgraded);
	/* Since the source database is fully upgraded and since all RECYCLED blocks will be marked as FREE, we are guaranteed
	 * there are NO V4 format block that is too full to be upgraded to V5 format (i.e. will cause DYNUPGRDFAIL error).
	 */
	new->db_got_to_v5_once = TRUE;	/* should be TRUE */
	new->opened_by_gtmv53 = TRUE;	/* should be TRUE */
	/************* FIELDS RELATED TO DB TRANSACTION HISTORY *****************************/
	SWAP_SD8(trans_hist.curr_tn);
	SWAP_SD8(trans_hist.early_tn);
	SWAP_SD8(trans_hist.last_mm_sync);
	SWAP_SD8(trans_hist.mm_tn);
	SWAP_SD4(trans_hist.lock_sequence);
	SWAP_SD4(trans_hist.ccp_jnl_filesize);
	SWAP_SD8(trans_hist.total_blks);
	SWAP_SD8(trans_hist.free_blocks);
	/************* FIELDS RELATED TO WRITE CACHE FLUSHING *******************************/
	SWAP_SD4(flush_time[0]);
	SWAP_SD4(flush_time[1]);
	SWAP_SD4(flush_trigger);
	SWAP_SD4(n_wrt_per_flu);
	SWAP_SD4(wait_disk_space);
	SWAP_SD4(defer_time);
	SWAP_SD4(mumps_can_bypass);
	SWAP_SD4(epoch_taper);
	SWAP_SD4(epoch_taper_time_pct);
	SWAP_SD4(epoch_taper_jnl_pct);
	SWAP_SD4(asyncio);
	/************* FIELDS Used for update process performance improvement. Some may go away in later releases ********/
	SWAP_SD4(reserved_for_upd);
	SWAP_SD4(avg_blks_per_100gbl);
	SWAP_SD4(pre_read_trigger_factor);
	SWAP_SD4(writer_trigger_factor);
	/************* FIELDS USED ONLY BY UNIX ********************************/
	/* Solaris complains about swapping -1
	 * assert(INVALID_SEMID == GTM_BYTESWAP_32(INVALID_SEMID));
	 * assert(INVALID_SHMID == GTM_BYTESWAP_32(INVALID_SHMID));
	 */
	assert(-1 == INVALID_SEMID);
	assert(-1 == INVALID_SHMID);
	if (new_is_native)
	{	/* Since we have standalone access, reset volatile fields in the database file header */
		new->semid = INVALID_SEMID;
		new->shmid = INVALID_SHMID;
		new->gt_sem_ctime.ctime = 0;
		new->gt_shm_ctime.ctime = 0;
		memset(new->machine_name, 0, MAX_MCNAMELEN);
	}
	/************* ACCOUNTING INFORMATION ********************************/
	/************* CCP/RC RELATED FIELDS (CCP STUFF IS NOT USED CURRENTLY BY GT.M) *************/
	SWAP_SD4(staleness[0]);
	SWAP_SD4(staleness[1]);
	SWAP_SD4(ccp_tick_interval[0]);
	SWAP_SD4(ccp_tick_interval[1]);
	SWAP_SD4(ccp_quantum_interval[0]);
	SWAP_SD4(ccp_quantum_interval[1]);
	SWAP_SD4(ccp_response_interval[0]);
	SWAP_SD4(ccp_response_interval[1]);
	SWAP_SD4(ccp_jnl_before);
	SWAP_SD4(clustered);
	SWAP_SD4(unbacked_cache);
	/* RC server related fields sb zero when not active	*/
	SWAP_SD4(rc_srv_cnt);
	SWAP_SD4(dsid);
	SWAP_SD4(rc_node);
	/************* REPLICATION RELATED FIELDS ****************/
	SWAP_SD8(reg_seqno);
	SWAP_SD8(pre_multisite_resync_seqno);
	/* Note some of the following names were added or renamed in V5.1 but should be of no issue for V5.0 builds
	 * since we will be swapping unused fields.
	*/
	SWAP_SD8(zqgblmod_tn);
	SWAP_SD8(zqgblmod_seqno);
	new->repl_state = repl_closed;
	if (!new_is_native)
		SWAP_SD4(repl_state);
	SWAP_SD4(multi_site_open);
	/************* TP RELATED FIELDS ********************/
	for (idx = 0; idx < ARRAYSIZE(old->tp_cdb_sc_blkmod); idx++)
		new->tp_cdb_sc_blkmod[idx] = 0;
	/************* JOURNALLING RELATED FIELDS ****************/
	SWAP_SD4(jnl_alq);
	SWAP_SD4(jnl_deq);
	SWAP_SD4(jnl_buffer_size);
	SWAP_SD4(jnl_before_image);
	new->jnl_state = jnl_closed;
	if (!new_is_native)
		SWAP_SD4(jnl_state);
	SWAP_SD4(jnl_file_len);
	SWAP_SD4(autoswitchlimit);
	SWAP_SD4(epoch_interval);
	SWAP_SD4(alignsize);
	SWAP_SD4(jnl_sync_io);
	SWAP_SD4(yield_lmt);
	SWAP_SD4(turn_around_point);
	SWAP_SD8(jnl_eovtn);
	/************* INTERRUPTED RECOVERY RELATED FIELDS ****************/
	SWAP_SD8(intrpt_recov_resync_seqno);
	SWAP_SD4(intrpt_recov_tp_resolve_time);
	SWAP_SD4(recov_interrupted);
	SWAP_SD4(intrpt_recov_jnl_state);
	SWAP_SD4(intrpt_recov_repl_state);
	/************* TRUNCATE RELATED FIELDS ****************/
	SWAP_SD4(v6_before_trunc_total_blks);
	SWAP_SD4(v6_after_trunc_total_blks);
	SWAP_SD4(v6_before_trunc_free_blocks);
	/************* POTENTIALLY LARGE CHARACTER ARRAYS **************/
	/************* ENCRYPTION-RELATED FIELDS **************/
	SWAP_SD4(non_null_iv);
	SWAP_SD4(v6_encryption_hash_cutoff);
	SWAP_SD8(encryption_hash2_start_tn);
	/************* BG_TRC_REC RELATED FIELDS ***********/
	/* Convert "bg_trc_rec_cntr_filler" and "bg_trc_rec_tn_filler" */
#	define TAB_BG_TRC_REC(A,B)	new->B##_cntr = (bg_trc_rec_cntr) 0; new->B##_tn = (bg_trc_rec_tn) 0;
#	include "tab_bg_trc_rec.h"
#	undef TAB_BG_TRC_REC
	/************* DB_CSH_ACCT_REC RELATED FIELDS ***********/
	/* Convert "db_csh_acct_rec_filler_4k" */
#	define TAB_DB_CSH_ACCT_REC(A,B,C)	new->A.cumul_count = new->A.curr_count = 0;
#	include "tab_db_csh_acct_rec.h"
#	undef TAB_DB_CSH_ACCT_REC
	/************* GVSTATS_REC RELATED FIELDS ***********/
#	define TAB_GVSTATS_REC(COUNTER,TEXT1,TEXT2)	SWAP_SD8(gvstats_rec.COUNTER);
#	include "tab_gvstats_rec.h"
#	undef TAB_GVSTATS_REC
	/************* FIELDS EXTENDED IN V7 HEADER ********************************/
	SWAP_SD8(master_map_len);
	SWAP_SD8(start_vbn);
	SWAP_SD8(last_inc_bkup_last_blk);
	SWAP_SD8(last_com_bkup_last_blk);
	SWAP_SD8(last_rec_bkup_last_blk);
	SWAP_SD8(reorg_restart_block);
	SWAP_SD8(reorg_upgrd_dwngrd_restart_block);
	SWAP_SD8(blks_to_upgrd);
	SWAP_SD8(blks_to_upgrd_subzero_error);
	SWAP_SD8(before_trunc_total_blks);
	SWAP_SD8(after_trunc_total_blks);
	SWAP_SD8(before_trunc_free_blocks);
	SWAP_SD8(encryption_hash_cutoff);
	/************* INTERRUPTED RECOVERY RELATED FIELDS continued ****************/
	for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
		SWAP_SD8(intrpt_recov_resync_strm_seqno[idx]);
	/************* DB CREATION AND UPGRADE CERTIFICATION FIELDS ***********/
	SWAP_SD4_CAST(creation_db_ver, enum db_ver);
	SWAP_SD4_CAST(creation_mdb_ver, enum mdb_ver);
	SWAP_SD4_CAST(certified_for_upgrade_to, enum db_ver);
	/************* SECSHR_DB_CLNUP RELATED FIELDS (now moved to node_local) ***********/
	/* "secshr_ops_index_filler" and "secshr_ops_array_filler" need not be converted (they are fillers) */
	/********************************************************/
	/* "next_upgrd_warn" is unused and need not be converted */
	SWAP_SD4(is_encrypted);
	SWAP_SD4(db_trigger_cycle);
	/************* SUPPLEMENTARY REPLICATION INSTANCE RELATED FIELDS ****************/
	for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
	{
		SWAP_SD8(strm_reg_seqno[idx]);
		SWAP_SD8(save_strm_reg_seqno[idx]);
	}
	/************* MISCELLANEOUS FIELDS ****************/
	SWAP_SD4(freeze_on_fail);
	SWAP_SD4(span_node_absent);
	SWAP_SD4(maxkeysz_assured);
	SWAP_SD4(hasht_upgrade_needed);
	SWAP_SD4(defer_allocate);
	SWAP_SD4(problksplit);
	SWAP_SD4(nobitmap_prepin);
}

void	v6_endian_header(v6_sgmnt_data *new, v6_sgmnt_data *old, boolean_t new_is_native)
{
	int	idx;
	time_t	ctime;

	/* convert the header back down to V6 format  */
	db_header_dwnconv((sgmnt_data_ptr_t)old);
	db_header_dwnconv((sgmnt_data_ptr_t)new);

	/************* MOSTLY STATIC DATABASE STATE FIELDS **************************/
	SWAP_SD4(blk_size);
	SWAP_SD4(master_map_len);
	SWAP_SD4(bplmap);
	SWAP_SD4(start_vbn);
	SWAP_SD4_CAST(acc_meth, enum db_acc_method);
	SWAP_SD4(max_bts);
	SWAP_SD4(n_bts);
	SWAP_SD4(bt_buckets);
	SWAP_SD4(reserved_bytes);
	SWAP_SD4(max_rec_size);
	SWAP_SD4(max_key_size);
	SWAP_SD4(lock_space_size);
	SWAP_SD4(extension_size);
	SWAP_SD4(def_coll);
	SWAP_SD4(def_coll_ver);
	SWAP_SD4(write_fullblk);
	SWAP_SD4(statsdb_allocation);
	SWAP_SD4(std_null_coll);
	SWAP_SD4(null_subs);
	SWAP_SD4(free_space);
	SWAP_SD4(mutex_spin_parms.mutex_hard_spin_count);	/* gdsbt.h */
	SWAP_SD4(mutex_spin_parms.mutex_sleep_spin_count);
	SWAP_SD4(mutex_spin_parms.mutex_spin_sleep_mask);
	SWAP_SD4(mutex_spin_parms.mutex_que_entry_space_size);
	SWAP_SD4(max_update_array_size);
	SWAP_SD4(max_non_bm_update_array_size);
	/* SWAP_SD4(file_corrupt); is set in main routine	*/
	SWAP_SD4_CAST(minor_dbver, enum mdb_ver);
	SWAP_SD4(jnl_checksum);
	SWAP_SD4(wcs_phase2_commit_wait_spincnt);
	SWAP_SD4_CAST(last_mdb_ver, enum mdb_ver);
	/************* FIELDS SET AT CREATION TIME ********************************/
	new->createinprogress = FALSE;
	assert(SIZEOF(int4) == SIZEOF(old->creation_time4));
	time(&ctime);
	assert(SIZEOF(ctime) >= SIZEOF(int4));
	new->creation_time4 = (int4)ctime;/* Take only lower order 4-bytes of current time */
	if (!new_is_native)
		SWAP_SD4(creation_time4);
	/************* FIELDS USED BY TN WARN PROCESSING *************************/
	SWAP_SD8(max_tn);
	SWAP_SD8(max_tn_warn);
	/************* FIELDS SET BY MUPIP BACKUP/REORG *************************/
	SWAP_SD8(last_inc_backup);
	SWAP_SD8(last_com_backup);
	SWAP_SD8(last_rec_backup);
	SWAP_SD4(last_inc_bkup_last_blk);
	SWAP_SD4(last_com_bkup_last_blk);
	SWAP_SD4(last_rec_bkup_last_blk);
	SWAP_SD4(reorg_restart_block);
	/* Filler to align text with the V7 version */
	/************* FIELDS SET WHEN DB IS OPEN ********************************/
	new->image_count = 0;		/* should be zero when db is not open so reset it unconditionally */
	new->freeze = 0;		/* should be zero when db is not open so reset it unconditionally */
	SWAP_SD4(kill_in_prog);
	SWAP_SD4(abandoned_kills);
	/************* FIELDS USED IN V4 <==> V5 COMPATIBILITY MODE ****************/
	SWAP_SD8(tn_upgrd_blks_0);
	SWAP_SD8(desired_db_format_tn);
	SWAP_SD8(reorg_db_fmt_start_tn);
	SWAP_SD4(reorg_upgrd_dwngrd_restart_block);
	SWAP_SD4(blks_to_upgrd);
	SWAP_SD4(blks_to_upgrd_subzero_error);
	SWAP_SD4_CAST(desired_db_format, enum db_ver);
	SWAP_SD4(fully_upgraded);	/* should be TRUE */
	assert(new->fully_upgraded);
	/* Since the source database is fully upgraded and since all RECYCLED blocks will be marked as FREE, we are guaranteed
	 * there are NO V4 format block that is too full to be upgraded to V5 format (i.e. will cause DYNUPGRDFAIL error).
	 */
	new->db_got_to_v5_once = TRUE;	/* should be TRUE */
	new->opened_by_gtmv53 = TRUE;	/* should be TRUE */
	/************* FIELDS RELATED TO DB TRANSACTION HISTORY *****************************/
	SWAP_SD8(trans_hist.curr_tn);
	SWAP_SD8(trans_hist.early_tn);
	SWAP_SD8(trans_hist.last_mm_sync);
	SWAP_SD8(trans_hist.mm_tn);
	SWAP_SD4(trans_hist.lock_sequence);
	SWAP_SD4(trans_hist.ccp_jnl_filesize);
	SWAP_SD4(trans_hist.total_blks);
	SWAP_SD4(trans_hist.free_blocks);
	/************* FIELDS RELATED TO WRITE CACHE FLUSHING *******************************/
	SWAP_SD4(flush_time[0]);
	SWAP_SD4(flush_time[1]);
	SWAP_SD4(flush_trigger);
	SWAP_SD4(n_wrt_per_flu);
	SWAP_SD4(wait_disk_space);
	SWAP_SD4(defer_time);
	SWAP_SD4(mumps_can_bypass);
	SWAP_SD4(epoch_taper);
	SWAP_SD4(epoch_taper_time_pct);
	SWAP_SD4(epoch_taper_jnl_pct);
	SWAP_SD4(asyncio);
	/************* FIELDS Used for update process performance improvement. Some may go away in later releases ********/
	SWAP_SD4(reserved_for_upd);
	SWAP_SD4(avg_blks_per_100gbl);
	SWAP_SD4(pre_read_trigger_factor);
	SWAP_SD4(writer_trigger_factor);
	/************* FIELDS USED ONLY BY UNIX ********************************/
	/* Solaris complains about swapping -1
	 * assert(INVALID_SEMID == GTM_BYTESWAP_32(INVALID_SEMID));
	 * assert(INVALID_SHMID == GTM_BYTESWAP_32(INVALID_SHMID));
	 */
	assert(-1 == INVALID_SEMID);
	assert(-1 == INVALID_SHMID);
	if (new_is_native)
	{	/* Since we have standalone access, reset volatile fields in the database file header */
		new->semid = INVALID_SEMID;
		new->shmid = INVALID_SHMID;
		new->gt_sem_ctime.ctime = 0;
		new->gt_shm_ctime.ctime = 0;
		memset(new->machine_name, 0, MAX_MCNAMELEN);
	}
	/************* ACCOUNTING INFORMATION ********************************/
	/************* CCP/RC RELATED FIELDS (CCP STUFF IS NOT USED CURRENTLY BY GT.M) *************/
	SWAP_SD4(staleness[0]);
	SWAP_SD4(staleness[1]);
	SWAP_SD4(ccp_tick_interval[0]);
	SWAP_SD4(ccp_tick_interval[1]);
	SWAP_SD4(ccp_quantum_interval[0]);
	SWAP_SD4(ccp_quantum_interval[1]);
	SWAP_SD4(ccp_response_interval[0]);
	SWAP_SD4(ccp_response_interval[1]);
	SWAP_SD4(ccp_jnl_before);
	SWAP_SD4(clustered);
	SWAP_SD4(unbacked_cache);
	/* RC server related fields sb zero when not active	*/
	SWAP_SD4(rc_srv_cnt);
	SWAP_SD4(dsid);
	SWAP_SD4(rc_node);
	/************* REPLICATION RELATED FIELDS ****************/
	SWAP_SD8(reg_seqno);
	SWAP_SD8(pre_multisite_resync_seqno);
	/* Note some of the following names were added or renamed in V5.1 but should be of no issue for V5.0 builds
	 * since we will be swapping unused fields.
	*/
	SWAP_SD8(zqgblmod_tn);
	SWAP_SD8(zqgblmod_seqno);
	new->repl_state = repl_closed;
	if (!new_is_native)
		SWAP_SD4(repl_state);
	SWAP_SD4(multi_site_open);
	/************* TP RELATED FIELDS ********************/
	for (idx = 0; idx < ARRAYSIZE(old->tp_cdb_sc_blkmod); idx++)
		new->tp_cdb_sc_blkmod[idx] = 0;
	/************* JOURNALLING RELATED FIELDS ****************/
	SWAP_SD4(jnl_alq);
	SWAP_SD4(jnl_deq);
	SWAP_SD4(jnl_buffer_size);
	SWAP_SD4(jnl_before_image);
	new->jnl_state = jnl_closed;
	if (!new_is_native)
		SWAP_SD4(jnl_state);
	SWAP_SD4(jnl_file_len);
	SWAP_SD4(autoswitchlimit);
	SWAP_SD4(epoch_interval);
	SWAP_SD4(alignsize);
	SWAP_SD4(jnl_sync_io);
	SWAP_SD4(yield_lmt);
	SWAP_SD4(turn_around_point);
	SWAP_SD8(jnl_eovtn);
	/************* INTERRUPTED RECOVERY RELATED FIELDS ****************/
	SWAP_SD8(intrpt_recov_resync_seqno);
	SWAP_SD4(intrpt_recov_tp_resolve_time);
	SWAP_SD4(recov_interrupted);
	SWAP_SD4(intrpt_recov_jnl_state);
	SWAP_SD4(intrpt_recov_repl_state);
	/************* TRUNCATE RELATED FIELDS ****************/
	SWAP_SD4(before_trunc_total_blks);
	SWAP_SD4(after_trunc_total_blks);
	SWAP_SD4(before_trunc_free_blocks);
	/************* POTENTIALLY LARGE CHARACTER ARRAYS **************/
	/************* ENCRYPTION-RELATED FIELDS **************/
	SWAP_SD4(non_null_iv);
	SWAP_SD4(encryption_hash_cutoff);
	SWAP_SD8(encryption_hash2_start_tn);
	/************* BG_TRC_REC RELATED FIELDS ***********/
	/* Convert "bg_trc_rec_cntr_filler" and "bg_trc_rec_tn_filler" */
#	define TAB_BG_TRC_REC(A,B)	new->B##_cntr = (bg_trc_rec_cntr) 0; new->B##_tn = (bg_trc_rec_tn) 0;
#	include "tab_bg_trc_rec.h"
#	undef TAB_BG_TRC_REC
	/************* DB_CSH_ACCT_REC RELATED FIELDS ***********/
	/* Convert "db_csh_acct_rec_filler_4k" */
#	define TAB_DB_CSH_ACCT_REC(A,B,C)	new->A.cumul_count = new->A.curr_count = 0;
#	include "tab_db_csh_acct_rec.h"
#	undef TAB_DB_CSH_ACCT_REC
	/************* GVSTATS_REC RELATED FIELDS ***********/
#	define TAB_GVSTATS_REC(COUNTER,TEXT1,TEXT2)	SWAP_SD8(gvstats_rec.COUNTER);
#	include "tab_gvstats_rec.h"
#	undef TAB_GVSTATS_REC
	/* Filler
	 * space
	 * for
	 * alignment
	 * with
	 * the
	 * V7
	 * version
	 * of
	 * this
	 * function
	 *
	 *
	 */
	/************* INTERRUPTED RECOVERY RELATED FIELDS continued ****************/
	for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
		SWAP_SD8(intrpt_recov_resync_strm_seqno[idx]);
	/************* DB CREATION AND UPGRADE CERTIFICATION FIELDS ***********/
	SWAP_SD4_CAST(creation_db_ver, enum db_ver);
	SWAP_SD4_CAST(creation_mdb_ver, enum mdb_ver);
	SWAP_SD4_CAST(certified_for_upgrade_to, enum db_ver);
	/************* SECSHR_DB_CLNUP RELATED FIELDS (now moved to node_local) ***********/
	/* "secshr_ops_index_filler" and "secshr_ops_array_filler" need not be converted (they are fillers) */
	/********************************************************/
	/* "next_upgrd_warn" is unused and need not be converted */
	SWAP_SD4(is_encrypted);
	SWAP_SD4(db_trigger_cycle);
	/************* SUPPLEMENTARY REPLICATION INSTANCE RELATED FIELDS ****************/
	for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
	{
		SWAP_SD8(strm_reg_seqno[idx]);
		SWAP_SD8(save_strm_reg_seqno[idx]);
	}
	/************* MISCELLANEOUS FIELDS ****************/
	SWAP_SD4(freeze_on_fail);
	SWAP_SD4(span_node_absent);
	SWAP_SD4(maxkeysz_assured);
	SWAP_SD4(hasht_upgrade_needed);
	SWAP_SD4(defer_allocate);
	SWAP_SD4(problksplit);
	SWAP_SD4(nobitmap_prepin);

	/* convert the header back up to V7 format  */
	db_header_upconv((sgmnt_data_ptr_t)old);
	db_header_upconv((sgmnt_data_ptr_t)new);
}

int4	endian_process(endian_info *info, sgmnt_data *new_data, sgmnt_data *old_data, boolean_t override_specified)
{	/* returns 0 for success
	   This routine based on mubinccpy and dbcertify_scan_phase
	*/
	blk_hdr_ptr_t	bp_new, bp_native, bp_old;
	block_id	blk_num, totblks, last_blk_written, mm_offset,
			lbm_done, busy_done, recycled_done, free_done, lbmap_cnt;
	boolean_t	new_is_native;
	boolean_t	blk_needs_encryption, non_null_iv, use_old_key, use_new_key;
	char		*blk_buff[2], *lbmap_buff[2], *errptr;
	int		save_errno, bsize, lbm_status;
	int		buff_native, buff_old, buff_new;
	int		lm_offset;
	int		crypt_blk_size, gtmcrypt_errno;
	int4		startvbn;
	int4		bplmap;
	off_t		dbptr;
	trans_num	encryption_hash2_start_tn;
	DCL_THREADGBL_ACCESS;

	SETUP_THREADGBL_ACCESS;
	if (info->endian_native)
	{	/* use fields from old header */
		bplmap = old_data->bplmap;
		totblks = old_data->trans_hist.total_blks;
		lbmap_cnt = (totblks + bplmap - 1) / bplmap;
		bsize = old_data->blk_size;
		startvbn = old_data->start_vbn;
		non_null_iv = old_data->non_null_iv;
		encryption_hash2_start_tn = old_data->encryption_hash2_start_tn;
		buff_native = buff_old = 0;
		new_is_native = FALSE;
		buff_new = 1;
	} else
	{	/* use swapped fields from new header */
		bplmap = new_data->bplmap;
		totblks = new_data->trans_hist.total_blks;
		lbmap_cnt = (totblks + bplmap - 1) / bplmap;
		bsize = new_data->blk_size;
		startvbn = new_data->start_vbn;
		non_null_iv = new_data->non_null_iv;
		encryption_hash2_start_tn = new_data->encryption_hash2_start_tn;
		buff_native = buff_new = 1;
		new_is_native = TRUE;
		buff_old = 0;
	}
	/* Asserting that the database is either not using any encryption or has set the database_fn_len field for future uses. */
	assert((0 != info->database_fn_len) || !USES_ANY_KEY(info));
	dbptr = (off_t)BLK_ZERO_OFF(startvbn);
	info->tot_blks = totblks;
	info->bsize = bsize;
	info->startvbn = startvbn;
	info->non_null_iv = non_null_iv;
	info->encryption_hash2_start_tn = encryption_hash2_start_tn;
	blk_buff[0] = malloc(bsize);
	blk_buff[1] = malloc(bsize);
	lbmap_buff[0] = malloc(bsize);
	lbmap_buff[1] = malloc(bsize);
	blk_num = last_blk_written = lbm_done = busy_done = recycled_done = free_done = 0;
	for (mm_offset = 0; (mm_offset < lbmap_cnt) && (blk_num < totblks); ++mm_offset)
	{	/* for each local bit map */
		assert(0 == (blk_num % bplmap));	/* check proper local bit map alignment */
		LSEEKREAD(info->db_fd, dbptr, lbmap_buff[buff_old], bsize, save_errno);
		if (0 != save_errno)
		{
			free(blk_buff[0]);
			free(lbmap_buff[0]);
			free(blk_buff[1]);
			free(lbmap_buff[1]);
			errptr = (char *)STRERROR(save_errno);
			util_out_print("Error reading local bit map block !UL : !AZ", TRUE, blk_num, errptr);
			return save_errno;
		}
		memcpy(lbmap_buff[buff_new], lbmap_buff[buff_old], bsize);
		endian_cvt_blk_hdr((blk_hdr_ptr_t)lbmap_buff[buff_new], new_is_native, FALSE);
		assert(LCL_MAP_LEVL == ((blk_hdr_ptr_t)lbmap_buff[buff_native])->levl);
		/* set all recycled bits to free to avoid trouble if pre GDSV6 */
		/* lm_offset 0 is the local bit map itself */
		for (lm_offset = 1; lm_offset < bplmap && (blk_num + lm_offset) < totblks; lm_offset++)
		{
			GET_BM_STATUS(lbmap_buff[buff_new], lm_offset, lbm_status);
			if (BLK_RECYCLED == lbm_status)
			{
				SET_BM_STATUS(lbmap_buff[buff_new], lm_offset, BLK_FREE);
				recycled_done++;
			} else if (BLK_FREE == lbm_status)
				free_done++;		/* count before changing recycled to free */
			else
				assertpro(BLK_MAPINVALID != lbm_status);
		}
		if (info->inplace)
		{
			DB_LSEEKWRITE(NULL, ((unix_db_info *)NULL), NULL, info->db_fd, dbptr,
							lbmap_buff[buff_new], bsize, save_errno);
		} else
			LSEEKWRITE(info->outdb_fd, dbptr, lbmap_buff[buff_new], bsize, save_errno);
		if (0 != save_errno)
		{
			free(blk_buff[0]);
			free(lbmap_buff[0]);
			free(blk_buff[1]);
			free(lbmap_buff[1]);
			errptr = (char *)STRERROR(save_errno);
			util_out_print("Error writing local bit map block !UL : !AZ", TRUE, blk_num, errptr);
			return save_errno;
		}
		last_blk_written = blk_num;
		lbm_done++;
		/* lm_offset 0 is the local bit map itself */
		for (lm_offset = 1, dbptr += bsize, blk_num++;
			(blk_num < totblks) && (lm_offset < bplmap);
			lm_offset++, dbptr += bsize, blk_num++)
		{	/* for each local bit map entry - there will only be busy or free blocks in the (new) database */
			GET_BM_STATUS(lbmap_buff[buff_new], lm_offset, lbm_status);
			if (BLK_BUSY == lbm_status)
			{
				LSEEKREAD(info->db_fd, dbptr, blk_buff[buff_old], bsize, save_errno);
				if (0 != save_errno)
				{
					free(blk_buff[0]);
					free(lbmap_buff[0]);
					free(blk_buff[1]);
					free(lbmap_buff[1]);
					errptr = (char *)STRERROR(save_errno);
					util_out_print("Error reading block !UL : !AZ", TRUE, blk_num, errptr);
					return save_errno;
				}
				memcpy(blk_buff[buff_new], blk_buff[buff_old], bsize);
				bp_new = (blk_hdr_ptr_t)blk_buff[buff_new];
				bp_old = (blk_hdr_ptr_t)blk_buff[buff_old];
				bp_native = (blk_hdr_ptr_t)blk_buff[buff_native];
				if (new_is_native)
					endian_cvt_blk_hdr(bp_new, new_is_native, BLK_RECYCLED == lbm_status);
				assert((bp_new->bsiz <= bsize) && (bp_new->bsiz >= SIZEOF(*bp_new)));
				crypt_blk_size = MIN(bsize, bp_new->bsiz) - (SIZEOF(*bp_new));
				blk_needs_encryption = BLK_NEEDS_ENCRYPTION(bp_new->levl, crypt_blk_size);
				if (blk_needs_encryption)
				{
					use_new_key = NEEDS_NEW_KEY(info, bp_new->tn);
					use_old_key = IS_ENCRYPTED(info->is_encrypted);
					if (use_new_key || use_old_key)
					{
						ASSERT_ENCRYPTION_INITIALIZED;
						/* We are doing decryption in place on blk_buff[bp_new] because a) the original data
						 * from blk_buff[bp_old] has already been copied to it, and b) blk_buff[bp_new] is
						 * where we want the decrypted data to be. Note, however, that we are using the
						 * block header from the old buffer as IV because the block header from the new
						 * buffer might have already been endian-converted and thus become incompatible with
						 * the encrypted data we are operating on.
						 */
						if (use_new_key)
						{
							GTMCRYPT_DECRYPT(NULL, TRUE, info->encr_handles.encr_key_handle2,
									(char *)(bp_new + 1), crypt_blk_size, NULL,
									bp_old, SIZEOF(blk_hdr), gtmcrypt_errno);
						} else
						{
							GTMCRYPT_DECRYPT(NULL, non_null_iv, info->encr_handles.encr_key_handle,
									(char *)(bp_new + 1), crypt_blk_size, NULL,
									bp_old, SIZEOF(blk_hdr), gtmcrypt_errno);
						}
						if (0 != gtmcrypt_errno)
						{
							GTMCRYPT_REPORT_ERROR(gtmcrypt_errno, gtm_putmsg,
									info->database_fn_len, info->database_fn);
							return gtmcrypt_errno;
						}
					}
				}
				if (!new_is_native)
					endian_cvt_blk_hdr(bp_new, new_is_native, BLK_RECYCLED == lbm_status);
				endian_cvt_blk_recs(info, (char *)bp_new, bp_native, blk_num);
				if (blk_needs_encryption && (use_new_key || use_old_key))
				{	/* Now that we have both the block header and the records in the right endianness,
					 * both stored in blk_buff[bp_new], we proceed to reencrypt the data in place.
					 */
					if (use_new_key)
					{
						GTMCRYPT_ENCRYPT(NULL, TRUE, info->encr_handles.encr_key_handle2,
							(char *)(bp_new + 1), crypt_blk_size, NULL,
							bp_new, SIZEOF(blk_hdr), gtmcrypt_errno);
					} else
					{
						GTMCRYPT_ENCRYPT(NULL, non_null_iv, info->encr_handles.encr_key_handle,
							(char *)(bp_new + 1), crypt_blk_size, NULL,
							bp_new, SIZEOF(blk_hdr), gtmcrypt_errno);
					}
					if (0 != gtmcrypt_errno)
					{
						GTMCRYPT_REPORT_ERROR(gtmcrypt_errno, gtm_putmsg,
								info->database_fn_len, info->database_fn);
						return gtmcrypt_errno;
					}
				}
				if (info->inplace)
				{
					DB_LSEEKWRITE(NULL, ((unix_db_info *)NULL), NULL, info->db_fd,
								dbptr, blk_buff[buff_new], bsize, save_errno);
				} else
					LSEEKWRITE(info->outdb_fd, dbptr, blk_buff[buff_new], bsize, save_errno);
				if (0 != save_errno)
				{
					free(blk_buff[0]);
					free(lbmap_buff[0]);
					free(blk_buff[1]);
					free(lbmap_buff[1]);
					errptr = (char *)STRERROR(save_errno);
					util_out_print("Error writing block !UL : !AZ", TRUE, blk_num, errptr);
					return save_errno;
				}
				last_blk_written = info->last_blk_cvt = blk_num;
				if (BLK_BUSY == lbm_status)
					busy_done++;
			}
		}
	}
	/* If MUPIP ENDIANCVT -OVERRIDE is specified and source db minor_dbver is not same as that of the endiancvt,
	 * then skip the EOF block writing as otherwise we could end up creating a db with dbver pointing to a
	 * pre-V63001 GT.M version but with a GDS-block EOF block (instead of a 512-byte EOF block which is the
	 * norm pre-V63001) effectively creating a db reflecting bits and pieces of multiple GT.M versions.
	 */
	if ((last_blk_written < totblks) && (!override_specified || (GDSMVCURR == old_data->minor_dbver)))
	{	/* need to create last disk block */
		dbptr = ((off_t)BLK_ZERO_OFF(startvbn) + (off_t)totblks * bsize);
		save_errno = db_write_eof_block(NULL, info->inplace ? info->db_fd : info->outdb_fd, bsize, dbptr,
												&(TREF(dio_buff)));
		if (0 != save_errno)
		{
			free(blk_buff[0]);
			free(lbmap_buff[0]);
			free(blk_buff[1]);
			free(lbmap_buff[1]);
			errptr = (char *)STRERROR(save_errno);
			util_out_print("Error writing last block : !AZ", TRUE, errptr);
			return save_errno;
		}
	}
	free(lbmap_buff[0]);
	free(lbmap_buff[1]);
	free(blk_buff[0]);
	free(blk_buff[1]);
	if (NULL != info->dtblk.buff)
	{
		free(info->dtblk.buff);
		info->dtblk.buff = NULL;
		info->dtblk.blkid = -1;
	}
	if (NULL != info->dtblk.dtrbuff)
	{
		free(info->dtblk.dtrbuff);
		info->dtblk.dtrbuff = NULL;
	}
	return 0;
}

void endian_cvt_blk_hdr(blk_hdr_ptr_t blkhdr, boolean_t new_is_native, boolean_t make_empty)
{	/* convert fields in block header */
	uint4		v15bsiz, v15levl, bsiz;
	v15_trans_num	v15tn;
	trans_num	tn;
	unsigned short	bver;

	v15bsiz = blkhdr->bver;
	blkhdr->bver = GTM_BYTESWAP_16(blkhdr->bver);
	if (new_is_native)
		v15bsiz = blkhdr->bver;		/* now it is native endian */
	if (SIZEOF(v15_blk_hdr) <= v15bsiz)
	{	/* old format block so it must be recycled and not upgraded */
		assert(FALSE);		/* should have been changed to FREE  above */
		assert(make_empty);
		v15levl = ((v15_blk_hdr *)blkhdr)->levl;
		v15tn = ((v15_blk_hdr *)blkhdr)->tn;
		assert(SIZEOF(char) == SIZEOF(blkhdr->levl));	/* no need to swap */
		blkhdr->levl = v15levl;
		bver = GDSV6;
		bsiz = SIZEOF(v15_blk_hdr);
		if (!new_is_native)
		{
			tn = ((v15_blk_hdr *)blkhdr)->tn;	/* expand while native; */
			blkhdr->tn = GTM_BYTESWAP_64(tn);
			bsiz = GTM_BYTESWAP_32(bsiz);
			bver = GTM_BYTESWAP_16(bver);
		} else
		{
			v15tn = GTM_BYTESWAP_32(v15tn);
			blkhdr->tn = v15tn;			/* expand while native */
		}
		blkhdr->bver = bver;
		blkhdr->bsiz = bsiz;
		return;
	}
	assert(SIZEOF(char) == SIZEOF(blkhdr->levl));	/* no need to swap */
	if (make_empty)
		blkhdr->bsiz = new_is_native ? SIZEOF(blk_hdr) : GTM_BYTESWAP_32(SIZEOF(blk_hdr));
	else
		blkhdr->bsiz = GTM_BYTESWAP_32(blkhdr->bsiz);
	blkhdr->tn = GTM_BYTESWAP_64(blkhdr->tn);
	return;
}

char *endian_read_dbblk(endian_info *info, block_id blk_to_get)
{
	blk_hdr_ptr_t	bp;
	boolean_t	use_old_key, use_new_key;
	boolean_t	blk_is_native;
	char		*buff;
	char		*inbuf;
	int		save_errno;
	int		gtmcrypt_errno;
	int		req_dec_blk_size;
	off_t		blkoff;

	if (DIR_ROOT == blk_to_get)
	{
		if (NULL == info->dtblk.dtrbuff)
		{	/* need to really get it */
			info->dtblk.dtrbuff = malloc(info->bsize);
			buff = info->dtblk.dtrbuff;
		} else	/* already have it */
			return info->dtblk.dtrbuff;
	} else
	{
		if (NULL == info->dtblk.buff)
		{
			info->dtblk.buff = malloc(info->bsize);
			info->dtblk.blkid = -1;		/* invalid */
		} else if (blk_to_get == info->dtblk.blkid)
			return info->dtblk.buff;	/* already have it */
		buff = info->dtblk.buff;
	}
	blkoff = ((off_t)BLK_ZERO_OFF(info->startvbn) + (off_t)blk_to_get * info->bsize);
	LSEEKREAD(info->db_fd, blkoff, buff, info->bsize, save_errno);
	if (0 != save_errno)
	{
		return NULL;
	}
	if (info->inplace && info->last_blk_cvt >= blk_to_get)
		blk_is_native = !info->endian_native;	/* already converted */
	else
		blk_is_native = info->endian_native;	/* still original endian */
	/* We need to save a copy of the block header before it is endian-converted because after the conversion it will become
	 * incompatible with the encrypted data.
	 */
	use_new_key = USES_NEW_KEY(info);
	use_old_key = IS_ENCRYPTED(info->is_encrypted);
	if (use_new_key || use_old_key)
		memcpy(pvt_crypt_buf.addr, buff, SIZEOF(blk_hdr));
	if (!blk_is_native)
		endian_cvt_blk_hdr((blk_hdr_ptr_t)buff, TRUE, FALSE);
	use_new_key = use_new_key && (((blk_hdr_ptr_t)buff)->tn >= info->encryption_hash2_start_tn);
	if (use_new_key || use_old_key)
	{
		bp = (blk_hdr_ptr_t)buff;
		assert((bp->bsiz <= info->bsize) && (bp->bsiz >= SIZEOF(*bp)));
		req_dec_blk_size = MIN(info->bsize, bp->bsiz) - (SIZEOF(*bp));
		if (BLK_NEEDS_ENCRYPTION(bp->levl, req_dec_blk_size))
		{
			ASSERT_ENCRYPTION_INITIALIZED;
			inbuf = (char *)(bp + 1);
			/* For IV use the block header we copied prior to endian conversion. */
			if (use_new_key)
			{
				GTMCRYPT_DECRYPT(NULL, TRUE, info->encr_handles.encr_key_handle2, inbuf, req_dec_blk_size, NULL,
						pvt_crypt_buf.addr, SIZEOF(blk_hdr), gtmcrypt_errno);
			} else
			{
				GTMCRYPT_DECRYPT(NULL, info->non_null_iv, info->encr_handles.encr_key_handle, inbuf,
						req_dec_blk_size, NULL, pvt_crypt_buf.addr, SIZEOF(blk_hdr), gtmcrypt_errno);
			}
			if (0 != gtmcrypt_errno)
			{
				GTMCRYPT_REPORT_ERROR(gtmcrypt_errno, gtm_putmsg, info->database_fn, info->database_fn_len);
				return NULL;
			}
		}
	}
	if (DIR_ROOT == blk_to_get)
		info->dtblk.dtrnative = blk_is_native;
	else
	{
		info->dtblk.blkid = blk_to_get;
		info->dtblk.native = blk_is_native;
	}
	return buff;
}

void endian_find_key(endian_info *info, end_gv_key *targ_gv_key, char *rec_p, int rec_len, int blk_levl, boolean_t long_blk_id)
{	/* find the key for the record and set targ_gv_key */
	char		*rec_key;
	int		cmpc;
	int		tmp_cmpc;
	unsigned char	*targ_key;

	if (bstar_rec_size(long_blk_id) == rec_len && 0 < blk_levl)
	{	/* no key for star key records */
		targ_gv_key->end = 0;
		return;
	}
	cmpc = EVAL_CMPC((rec_hdr_ptr_t)rec_p);
	targ_key = targ_gv_key->key + cmpc;
	rec_key = rec_p + SIZEOF(rec_hdr);
	while (TRUE)
	{
		for (; *rec_key; ++targ_key, ++rec_key)
			*targ_key = *rec_key;
		if (0 == *(rec_key + 1))
		{	/* end of key since two nulls */
			*targ_key++ = 0;	/* end the target key */
			*targ_key = 0;		/* with two as well */
			targ_gv_key->end = (unsigned int)(targ_key - targ_gv_key->key);
			break;
		}
		/* Else, copy subscript separator char and keep scanning */
		*targ_key++ = *rec_key++;
		assert((rec_key - rec_p) < rec_len);
	}
	assert(cmpc <= targ_gv_key->end);
	return;
}

boolean_t	endian_match_key(end_gv_key *gv_key1, int blk_levl, end_gv_key *gv_key2)
{
	int	key1_len, key2_len, key_len;
	unsigned char	*key1, *key2;

	key1 = gv_key1->key;
	key2 = gv_key2->key;
	key1_len = gv_key1->end + 1;
	if (1 == key1_len && 0 < blk_levl)
		return TRUE;	/* a star key record is greater than the second key */
	assert(1 < key1_len);
	key2_len = gv_key2->end + 1;
	assert(1 < key2_len);		/* should never look for star key */
	key_len = MIN(key1_len, key2_len);
	for (; key_len; key1++, key2++, key_len--)
	{
		if (*key1 != *key2)
			break;
	}
	if ((0 == key_len && key1_len >= key2_len) || (0 != key_len && *key1 > *key2))
		return TRUE;
	return FALSE;
}

/* find the directory tree leaf block for a key to check if a level zero
   block is in the DT or GVT.  The need for this should be rare so little
   attempt is made at efficiency other than caching the DT root block
   since it is the start of all searches.
   */
block_id	endian_find_dtblk(endian_info *info, end_gv_key *gv_key)
{
	block_id	blk_to_get, blk_ptr;
	block_id_32	temp_32_id;
	boolean_t	blk_is_native, long_blk_id;
	char		*buff, *blk_top, *rec_p;
	end_gv_key	found_gv_key;
	int		save_errno, rec_len, blk_levl, ptroffset;
	int		tmp_cmpc;
	unsigned char	found_gv_key_buff[MAX_KEY_SZ + 1];
	unsigned short	us_rec_len;

	info->dtblk.count++;
	found_gv_key.key = found_gv_key_buff;
	found_gv_key.end = found_gv_key.top = found_gv_key.gvn_len = 0;
	blk_to_get = DIR_ROOT;
	buff = endian_read_dbblk(info, blk_to_get);	/* will use dtrbuff after first time */
	if (!buff)
		return -1;
	blk_is_native = info->dtblk.dtrnative;
	while (TRUE)
	{
		blk_top = buff + ((blk_hdr_ptr_t)buff)->bsiz;
		blk_levl = ((blk_hdr_ptr_t)buff)->levl;
		long_blk_id = IS_64_BLK_ID(buff);
		rec_p = buff + SIZEOF(blk_hdr);
		while (rec_p < blk_top)
		{
			GET_USHORT(us_rec_len, &((rec_hdr *)rec_p)->rsiz);
			if (!blk_is_native)
				us_rec_len = GTM_BYTESWAP_16(us_rec_len);
			rec_len = us_rec_len;
			if (0 >= rec_len)
				return -1;
			if ((0 != blk_levl) && (bstar_rec_size(long_blk_id) == rec_len))
			{	/* down to the next level */
				READ_BLK_ID(long_blk_id, &blk_ptr, (sm_uc_ptr_t)(rec_p + SIZEOF(rec_hdr)));
				if (!blk_is_native)
				{
					if(long_blk_id)
						blk_ptr = BLK_ID_64_BYTESWAP(blk_ptr);
					else
					{
						assert(blk_ptr == (block_id_32)blk_ptr);
						temp_32_id = (block_id_32)blk_ptr;
						temp_32_id = BLK_ID_32_BYTESWAP(temp_32_id);
						blk_ptr = temp_32_id;
					}
					assert(0 <= blk_ptr);
				}
				if (blk_ptr > info->tot_blks)
					return -1;	/* past end of database */
				blk_to_get = blk_ptr;
				break;
			}
			endian_find_key(info, &found_gv_key, rec_p, rec_len, blk_levl, long_blk_id);
			if (endian_match_key(&found_gv_key, blk_levl, gv_key))
			{
				if (0 == blk_levl)
					return blk_to_get;	/* found dtleaf block we are looking for */
				ptroffset = found_gv_key.end - EVAL_CMPC((rec_hdr *)rec_p) + 1;
				READ_BLK_ID(long_blk_id, &blk_ptr, (sm_uc_ptr_t)(rec_p + SIZEOF(rec_hdr) + ptroffset));
				if (!blk_is_native)
				{
					if(long_blk_id)
						blk_ptr = BLK_ID_64_BYTESWAP(blk_ptr);
					else
					{
						assert(blk_ptr == (block_id_32)blk_ptr);
						temp_32_id = (block_id_32)blk_ptr;
						temp_32_id = BLK_ID_32_BYTESWAP(temp_32_id);
						blk_ptr = temp_32_id;
					}
					assert(0 <= blk_ptr);
				}
				if (blk_ptr > info->tot_blks)
					return -1;	/* past end of database */
				blk_to_get = blk_ptr;
				break;
			}
			rec_p = rec_p + rec_len;
		}
		if (0 == blk_levl)
			return -1;		/* we didn't find what should have been there */
		buff = endian_read_dbblk(info, blk_to_get);
		if (!buff)
			return -1;
		blk_is_native = info->dtblk.native;
	}
}

void endian_cvt_blk_recs(endian_info *info, char *new_block, blk_hdr_ptr_t blkhdr, block_id blknum)
{	/* convert records in new_block, could be data, index, or directory
	   use converted header fields from blkhdr which is in native format */
	block_id	ptr2blk, ptr2blk_swap, dtblk;
	block_id_32	temp_32_id;
	boolean_t	new_is_native, have_dt_blk;
	boolean_t	have_gvtleaf;
	boolean_t	long_blk_id;
	end_gv_key	gv_key;
	int		rec1_len, rec1_gvn_len, rec2_cmpc, rec2_len;
	int		tmp_cmpc;
	int		blk_levl;
	int		blk_id_sz;
	unsigned short	us_rec_len, us_rec_len_swap;
	unsigned char	*rec1_ptr, *rec2_ptr, *blk_top, *key_top;

	if (SIZEOF(v15_blk_hdr) <= blkhdr->bver)
		return;		/* pre V5 version so ignore records */
	new_is_native = !info->endian_native;
	blk_levl = blkhdr->levl;
	long_blk_id = IS_64_BLK_ID(blkhdr);
	blk_id_sz = SIZEOF_BLK_ID(long_blk_id);
	blk_top = (unsigned char *)new_block + blkhdr->bsiz;
	rec1_ptr = (unsigned char *)new_block + SIZEOF(blk_hdr);
	GET_USHORT(us_rec_len, &((rec_hdr *)rec1_ptr)->rsiz);
	rec1_len = new_is_native ? GTM_BYTESWAP_16(us_rec_len) : us_rec_len;
	/* May not need this whole thing, just do what dump_record does
	   and check if block_id follows keys - but what if data is 4 chars */
	/* need to check there really is a 2nd record */
	rec2_ptr = rec1_ptr + rec1_len;
	if (rec2_ptr < blk_top)
		rec2_cmpc = EVAL_CMPC((rec_hdr *)rec2_ptr);
	else
		rec2_cmpc = -1;		/* no second record */

	/* Determine type of block (DT lvl 0, DT lvl !0, GVT lvl 0, GVT lvl !0)

	    Rules for checking (from dbcertify_scan_phase.c):

	    1) If compression count of 2nd record is zero, it *must* be a directory tree block. This is a fast path
	       check to avoid doing the strlen in the second check.

	    2) If compression count of second record is less than or equal to the length of the global variable name,
	       then this must be a directory tree block. The reason this check works is a GVT index or data block
	       would have same GVN in the 2nd record as the first so the compression count would be a minimum of
	       (length(GVN) + 1). The "+ 1" is for the terminating null of the GVN.

	    dbcertify only cares about too full blocks so the above rules may not apply in all other cases.
	    endian cvt only care about index (levl > 0), dtleaf, or gvtleaf.
	*/
	have_dt_blk = FALSE;
	if (0 == rec2_cmpc)
		have_dt_blk = TRUE;
	else
	{
		rec1_gvn_len = STRLEN((char *)rec1_ptr + SIZEOF(rec_hdr));
		if (-1 != rec2_cmpc && rec2_cmpc <= rec1_gvn_len)
			have_dt_blk = TRUE;
	}
	if (have_dt_blk)
		have_gvtleaf = FALSE;			/* Could be dtleaf, dtindex, or dtroot but not gvtleaf */
	else if (-1 != rec2_cmpc)
	{	/* more than one record */
		if (0 == blk_levl)
			have_gvtleaf = TRUE;		/* gdsblk_gvtleaf only sure if more than one record */
		else	/* ambiguous whether gvtroot or gvtindex */
			have_gvtleaf = FALSE;
	} else if (blk_levl)
		have_gvtleaf = FALSE;			/* gdsblk_gvtindex at least not leaf */
	else
	{	/* only one record and level is 0 */
		/* if subscripts at level 0, it must be gvtleaf */
		/* find key_top to check if it has four/eight bytes (depends on V6 or V7 block)
		 * of data which look like a valid pointer */
		/* note dtleaf may have collation info after the pointer */
		for (key_top = rec1_ptr + SIZEOF(rec_hdr); *key_top && key_top < (rec1_ptr + rec1_len); key_top++)
			;
		if (*++key_top)
			have_gvtleaf = TRUE;		/* gdsblk_gvtleaf subscript so must be */
		else if (blk_id_sz <= ((rec1_ptr + rec1_len) - ++key_top) &&
			 (blk_id_sz + MAX_SPEC_TYPE_LEN) >= ((rec1_ptr + rec1_len) - key_top))
		{	/* record value long enough for block_id but not longer than block_id plus collation information */
			READ_BLK_ID(long_blk_id, &ptr2blk, key_top);
			if (new_is_native)
			{
				if(long_blk_id)
					ptr2blk = BLK_ID_64_BYTESWAP(ptr2blk);
				else
				{
					assert(ptr2blk == (block_id_32)ptr2blk);
					temp_32_id = (block_id_32)ptr2blk;
					temp_32_id = BLK_ID_32_BYTESWAP(temp_32_id);
					ptr2blk = temp_32_id;
				}
				assert(0 <= ptr2blk);
			}
			if (ptr2blk <= info->tot_blks)
			{	/* might be a pointer so need to check the hard way */
				gv_key.key = rec1_ptr + SIZEOF(rec_hdr);
				gv_key.top = gv_key.end = gv_key.gvn_len = (unsigned int)(key_top - gv_key.key - 1);
				dtblk = endian_find_dtblk(info, &gv_key);
				if (dtblk != blknum)
					have_gvtleaf = TRUE;	/* blknum is not DT level 0 */
				else
					have_gvtleaf = FALSE;	/* DT level 0 has pointers */
			} else	/* points after last block so not a block_id */
				have_gvtleaf = TRUE;		/* gdsblk_gvtleaf should be data */
		} else
			have_gvtleaf = TRUE;		/* gdsblk_gvtleaf too short for pointer or too long with collation info */
	}
	for (; rec1_ptr < blk_top; rec1_ptr += rec1_len)
	{
		GET_USHORT(us_rec_len, &((rec_hdr *)rec1_ptr)->rsiz);
		us_rec_len_swap = GTM_BYTESWAP_16(us_rec_len);
		PUT_USHORT(&((rec_hdr *)rec1_ptr)->rsiz, us_rec_len_swap);
		rec1_len = new_is_native ? us_rec_len_swap : us_rec_len;
		/* leave cmpc and cmpc2 bytes alone */
		if (!have_gvtleaf)
		{	/* fix up pointers as well */
			key_top = rec1_ptr + SIZEOF(rec_hdr);
			if (bstar_rec_size(long_blk_id) != rec1_len || 0 == blk_levl)
			{	/* find pointer after subscripts */
				for ( ; key_top < (rec1_ptr + rec1_len); )
					if (!*key_top++ && !*key_top++)
						break;		/* 2 nulls is end of subscripts */
			} else
				assert((key_top + blk_id_sz == blk_top) || blk_levl);	/* must be last if not leaf */
			assert((key_top + blk_id_sz) <= (rec1_ptr + rec1_len));
			if(long_blk_id)
			{
				GET_BLK_ID_64(ptr2blk, key_top);
				ptr2blk_swap = BLK_ID_64_BYTESWAP(ptr2blk);
				PUT_BLK_ID_64(key_top, ptr2blk_swap);
			} else
			{
				GET_BLK_ID_32(ptr2blk, key_top);
				assert(ptr2blk == (block_id_32)ptr2blk);
				temp_32_id = (block_id_32)ptr2blk;
				temp_32_id = BLK_ID_32_BYTESWAP(temp_32_id);
				ptr2blk_swap = temp_32_id;
				PUT_BLK_ID_32(key_top, ptr2blk_swap);
			}
#ifdef DEBUG
			if (new_is_native)
				ptr2blk = ptr2blk_swap;
			assert(0 <= ptr2blk);
			assert(ptr2blk <= info->tot_blks);
#endif
		}
	}
}