-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathADF_internals.c
More file actions
9005 lines (7866 loc) · 301 KB
/
ADF_internals.c
File metadata and controls
9005 lines (7866 loc) · 301 KB
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
/* created by combine 2.0 */
/* file ADFI_AAA_var.c */
/***
File: ADF_internals.c
----------------------------------------------------------------------
BOEING
----------------------------------------------------------------------
Project: CGNS
Author: Tom Dickens 234-1024 [email protected]
Date: 3/2/1995
Purpose: Provide the underlying support for the ADF-Core.
----------------------------------------------------------------------
----------------------------------------------------------------------
Notes: Integer numbers are stored on disk as ASCII-hex numbers.
2 bytes gives a number from 0 to 255,
4 bytes 0 to 65,535,
8 bytes 0 to 4,294,967,295,
and 12 bytes from 0 to 281,474,976,710,655.
Pointers are 12 bytes.
8 bytes pointing to a 4096-byte chunk on disk,
and 4 bytes is an offset into that chunk.
This gives a maximum file size of 17,592,186,048,512 bytes (17.5 Tera bytes).
----------------------------------------------------------------------
The tables below detail the format of the information which
makes up the ADF file.
There are 7 different, unique types of data "chunks" used.
Three of these are of fixed length, and the other four are
variable in length.
With the exception of numeric data (user's data), all information
in an ADF file is written in ASCII.
Uniquely-defined boundary-tags are used to surround all "chunks"
of information. These tags are checked to confirm "chunk" type
and also to ensure data integrity.
----------------------------------------------------------------------
186 Physical disk-First block
bytes start end description range / format
32 0 31 "what" description "@(#)ADF Database Version AXXxxx>"
4 32 35 "AdF0" boundary tag Tag
28 36 63 Creation date/time "Wed Apr 19 09:33:25 1995 "
4 64 67 "AdF1" boundary tag Tag
28 68 95 Modification date/time "Wed Apr 19 09:33:29 1995 "
4 96 99 "AdF2" boundary tag Tag
1 100 100 Numeric format ['B', 'L', 'C', 'N']
1 101 101 Duplicate of numeric format ['B', 'L', 'C', 'N']
4 102 105 "AdF3" boundary tag Tag
2 106 107 sizeof( char ) 0 to 255
2 108 109 sizeof( short ) 0 to 255
2 110 111 sizeof( int ) 0 to 255
2 112 113 sizeof( long ) 0 to 255
2 114 115 sizeof( float ) 0 to 255
2 116 117 sizeof( double ) 0 to 255
2 118 119 sizeof( char * ) 0 to 255
2 120 121 sizeof( short * ) 0 to 255
2 122 123 sizeof( int *) 0 to 255
2 124 125 sizeof( long * ) 0 to 255
2 126 127 sizeof( float *) 0 to 255
2 128 129 sizeof( double *) 0 to 255
4 130 133 "AdF4" boundary tag Tag
12 134 145 Root-node header pointer Disk chunk, chunk offset.
12 146 157 End-of-File pointer Disk chunk, chunk offset.
12 158 169 Free-Chunk table pointer Disk chunk, chunk offset.
12 170 181 Extra pointer Disk chunk, chunk offset.
4 182 185 "AdF5" boundary tag Tag
80 Free-Chunk table
bytes start end description range / format
4 0 3 "fCbt" boundary tag Tag
12 4 15 First small block pointer Disk chunk, chunk offset.
12 16 27 Last small block pointer Disk chunk, chunk offset.
12 28 39 First medium block pointer Disk chunk, chunk offset.
12 40 51 Last medium block pointer Disk chunk, chunk offset.
12 52 63 First large block pointer Disk chunk, chunk offset.
12 64 75 Last large block pointer Disk chunk, chunk offset.
4 76 79 "fcte" boundarg tag Tag
Variable: min 32 Free Chunk
bytes start end description range / format
4 0 3 "FreE" boundary tag Tag
12 4 15 Pointer to End-of-Chunk-Tag
12 16 27 Pointer to Next-Chunk in list
0 28 - more free space
4 28 31 "EndC" boundarg tag Tag
Note: There can occur other free space "gas" in the file which are smaller
than the 32-bytes needed to have tags and pointers. The convention
in these cases is to just fill the entire free space with the letter
z, lower-case.
246 Node header
bytes start end description range / format
4 0 3 "NoDe" boundary tag Tag
32 4 35 Name Text: Blank filled
32 36 67 Label Text: Blank filled
8 68 75 Number of sub-nodes 0 to 4,294,967,295
8 76 83 Entries for sub-nodes 0 to 4,294,967,295
12 84 95 Pointer to sub-node table Disk chunk, chunk offset.
32 96 127 Data-type Text: Blank filled
2 128 129 Number of dimensions 0 to 12
8 130 137 Dimension value 0 0 to 4,294,967,295
8 138 145 Dimension value 1 0 to 4,294,967,295
8 146 153 Dimension value 2 0 to 4,294,967,295
8 154 161 Dimension value 3 0 to 4,294,967,295
8 162 169 Dimension value 4 0 to 4,294,967,295
8 170 177 Dimension value 5 0 to 4,294,967,295
8 178 185 Dimension value 6 0 to 4,294,967,295
8 186 193 Dimension value 7 0 to 4,294,967,295
8 194 201 Dimension value 8 0 to 4,294,967,295
8 202 209 Dimension value 9 0 to 4,294,967,295
8 210 217 Dimension value 10 0 to 4,294,967,295
8 218 225 Dimension value 11 0 to 4,294,967,295
4 226 229 Number of data chunks 0 to 65,535
12 230 241 Pointer to data chunk (or table) Disk chunk, chunk offset.
4 242 245 "TaiL" boundary tag Tag
Variable: min 64 Sub-node table
bytes start end description range / format
4 0 3 "SNTb" boundary tag Tag
12 4 15 Pointer to End-of-Table-Tag
32 16 47 Child's name Text: Blank filled
12 48 59 Pointer to child Disk chunk, chunk offset.
32 - - Child's name Text: Blank filled
12 - - Pointer to child Disk chunk, chunk offset.
32 - - Child's name Text: Blank filled
12 - - Pointer to child Disk chunk, chunk offset.
32 - - Child's name Text: Blank filled
12 - - Pointer to child Disk chunk, chunk offset.
32 - - Child's name Text: Blank filled
12 - - Pointer to child Disk chunk, chunk offset.
32 - - Child's name Text: Blank filled
12 - - Pointer to child Disk chunk, chunk offset.
4 60 63 "snTE" boundary tag Tag
Variable: min 44 Data-chunk table
bytes start end description range / format
4 0 3 "DCtb" boundary tag Tag
12 4 15 Pointer to End-of-Table-Tag
12 16 27 Pointer to data start Disk chunk, chunk offset.
12 28 39 Pointer to data end Disk chunk, chunk offset.
12 - - Pointer to data start Disk chunk, chunk offset.
12 - - Pointer to data end Disk chunk, chunk offset.
12 - - Pointer to data start Disk chunk, chunk offset.
12 - - Pointer to data end Disk chunk, chunk offset.
12 - - Pointer to data start Disk chunk, chunk offset.
12 - - Pointer to data end Disk chunk, chunk offset.
4 40 43 "dcTE" boundarg tag Tag
Variable: min 32 Data-chunks
(Minimum is 32 bytes, which corresponds to the size required for a free-chunk)
bytes start end description range / format
4 0 3 "DaTa" boundary tag Tag
12 4 15 Pointer to End-of-Data-Tag
16 16 27 The data
4 28 31 "dEnD" boundarg tag Tag
**/
/***********************************************************************
Includes
***********************************************************************/
#if !defined(_WIN32) && !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200112L
#endif
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#if defined(_WIN32) && !defined(__NUTC__)
# include <io.h>
# define ACCESS _access
# define OPEN _open
# define CLOSE _close
# define FILENO _fileno
# define READ _read
# define WRITE _write
# define LSEEK _lseek
#else
# include <unistd.h>
# include <sys/param.h>
# include <sys/stat.h>
# define ACCESS access
# define OPEN open
# define CLOSE close
# define FILENO fileno
# define READ read
# define WRITE write
# define LSEEK lseek
#endif
#include "ADF.h"
#include "ADF_internals.h"
#ifdef MEM_DEBUG
#include "cg_malloc.h"
#endif
#include "cgns_io.h" /* for cgio_find_file */
#if 0
#define CHECK_ABORT(E) if(E!=NO_ERROR){int a=0;int b=1/a;}
#else
#define CHECK_ABORT(E) if(E!=NO_ERROR)return;
#endif
/***********************************************************************
Large File Support - files > 2Gb on 32-bit machines
***********************************************************************/
#ifdef HAVE_OPEN64
# define file_open open64
#else
# define file_open OPEN
#endif
#ifdef HAVE_LSEEK64
# ifdef _WIN32
typedef __int64 file_offset_t;
# define file_seek _lseeki64
# else
typedef off64_t file_offset_t;
# define file_seek lseek64
# endif
#else
typedef off_t file_offset_t;
# define file_seek LSEEK
#endif
extern int ADF_sys_err;
/* how many file data structures to add when increasing */
#define ADF_FILE_INC 5
/* open file data structure */
ADF_FILE *ADF_file;
int maximum_files = 0;
/** Track the format of this machine as well as the format
of eack of the files. This is used for reading and
writing numeric data associated with the nodes, which may
include numeric-format translations.
**/
static char ADF_this_machine_format = UNDEFINED_FORMAT_CHAR ;
static char ADF_this_machine_os_size = UNDEFINED_FORMAT_CHAR ;
/** we need a block of "zz"-bytes for dead-space **/
static char block_of_ZZ[ SMALLEST_CHUNK_SIZE ] ;
static int block_of_ZZ_initialized = FALSE ;
/** we need a block of "xx"-bytes for free-blocks **/
static char block_of_XX[ DISK_BLOCK_SIZE ] ;
static int block_of_XX_initialized = FALSE ;
/** we need a block of null-bytes for disk conditioning **/
static char block_of_00[ DISK_BLOCK_SIZE ] ;
static int block_of_00_initialized = FALSE ;
/** read/write conversion buffer **/
#define CONVERSION_BUFF_SIZE 100000
static unsigned char from_to_data[ CONVERSION_BUFF_SIZE ] ;
/** read/write buffering variables **/
static char rd_block_buffer[DISK_BLOCK_SIZE] ;
static cglong_t last_rd_block = -1 ;
static int last_rd_file = -1 ;
static int num_in_rd_block = -1 ;
static char wr_block_buffer[DISK_BLOCK_SIZE] ;
static cglong_t last_wr_block = -2 ;
static int last_wr_file = -2 ;
static int flush_wr_block = -2 ;
static double last_link_ID = 0.0;
static double last_link_LID = 0.0;
enum { FLUSH, FLUSH_CLOSE };
/** Assumed machine variable sizes for the currently supported
machines. For ordering of data see the Figure_Machine_Format
function. Note that when opening a new file not in the machine
format these are the sizes used!! **/
enum { TO_FILE_FORMAT, FROM_FILE_FORMAT } ;
#define NUMBER_KNOWN_MACHINES 5
static size_t machine_sizes[NUMBER_KNOWN_MACHINES][16] = {
/* IEEE BIG 32 */ { 1, 1, 1, 2, 2, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4 },
/* IEEE SML 32 */ { 1, 1, 1, 2, 2, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4 },
/* IEEE BIG 64 */ { 1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8, 8, 8, 8, 8, 8 },
/* IEEE SML 64 */ { 1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8, 8, 8, 8, 8, 8 },
/* CRAY 64 */ { 1, 1, 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 } } ;
/***********************************************************************
pows: Powers of 16, from 16^0 to 16^7
ASCII_Hex: Hex numbers from 0 to 15.
***********************************************************************/
static const unsigned int pows[8] = { /** Powers of 16 **/
1, 16, 256, 4096, 65536, 1048576, 16777216, 268435456 } ;
static const char ASCII_Hex[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' } ;
/***********************************************************************
Character string defining the data tags:
***********************************************************************/
static char *file_header_tags[] = {
"AdF0", "AdF1", "AdF2", "AdF3", "AdF4", "AdF5" } ;
static char node_start_tag[] = "NoDe" ;
static char node_end_tag[] = "TaiL" ;
static char free_chunk_table_start_tag[] = "fCbt" ;
static char free_chunk_table_end_tag[] = "Fcte" ;
static char free_chunk_start_tag[] = "FreE" ;
static char free_chunk_end_tag[] = "EndC" ;
static char sub_node_start_tag[] = "SNTb" ;
static char sub_node_end_tag[] = "snTE" ;
static char data_chunk_table_start_tag[] = "DCtb" ;
static char data_chunk_table_end_tag[] = "dcTE" ;
char data_chunk_start_tag[] = "DaTa" ; /* needed in ADF_interface.c */
static char data_chunk_end_tag[] = "dEnD" ;
/***********************************************************************
Priority Stack Buffer is used to buffer some of the overhead of
reading small blocks of file control information like the node
header by saving the data into a memory buffer. The buffer has
a priority value associated with it and is used to determine
which entry to replace when the stack is full!! Each stack entry
could be as large as 274 bytes since the stack data could be for
a node where NODE_HEADER_SIZE = 246.
***********************************************************************/
#define MAX_STACK 50
static struct {
int file_index;
cgulong_t file_block;
unsigned int block_offset;
int stack_type;
char *stack_data;
int priority_level;
} PRISTK[MAX_STACK] ;
/* Define stack types */
enum { FILE_STK=1, NODE_STK, DISK_PTR_STK, FREE_CHUNK_STK, SUBNODE_STK };
/* Define stack control modes */
enum { INIT_STK, CLEAR_STK, CLEAR_STK_TYPE, DEL_STK_ENTRY, GET_STK, SET_STK };
/***********************************************************************
Defined macros
***********************************************************************/
#define EVAL_2_BYTES( C0, C1 ) (((C0)<<8)+((C1)))
#define EVAL_4_BYTES( C0, C1, C2, C3 ) (((C0)<<24)+((C1)<<16)+((C2)<<8)+((C3)))
/* end of file ADFI_AAA_var.c */
/* file ADFI_ASCII_Hex_2_unsigned_int.c */
/***********************************************************************
ADFI ASCII Hex to unsigned int:
Convert a number of ASCII-HEX into an unsigned integer.
input: const unsigned int minimum Expected minimum number.
input: const unsigned int maximum Expected maximum number.
input: const unsigned int string_length Length (bytes) of the input string.
input: const char string[] The input string.
output: unsigned int *number The resulting number.
output: int *error_return Error return.
Possible errors:
NO_ERROR
NULL_POINTER
NULL_STRING_POINTER
STRING_LENGTH_ZERO
STRING_LENGTH_TOO_BIG
STRING_NOT_A_HEX_STRING
NUMBER_LESS_THAN_MINIMUM
NUMBER_GREATER_THAN_MAXIMUM
***********************************************************************/
void ADFI_ASCII_Hex_2_unsigned_int(
const unsigned int minimum,
const unsigned int maximum,
const unsigned int string_length,
const char string[],
unsigned int *number,
int *error_return )
{
unsigned int i, /** Index from 0 to string_length - 1 **/
ir, /** Index from string_length - 1 to 0 **/
j, /** Temoprary integer variable **/
num ; /** Working value of the number **/
if( string == NULL ) {
*error_return = NULL_STRING_POINTER ;
return ;
} /* end if */
if( string_length == 0 ) {
*error_return = STRING_LENGTH_ZERO ;
return ;
} /* end if */
if( number == NULL ) {
*error_return = NULL_POINTER ;
return ;
} /* end if */
if( string_length > 8 ) {
*error_return = STRING_LENGTH_TOO_BIG ;
return ;
} /* end if */
if( minimum > maximum ) {
*error_return = MINIMUM_GT_MAXIMUM ;
return ;
} /* end if */
*error_return = NO_ERROR ;
/** Convert the ASCII-Hex string into decimal **/
num = 0 ;
ir = (string_length - 1) << 2;
for( i=0; i<string_length; i++) {
if (string[i] >= '0' && string[i] <= '9')
j = string[i] - 48;
else if (string[i] >= 'A' && string[i] <= 'F')
j = string[i] - 55;
else if (string[i] >= 'a' && string[i] <= 'f')
j = string[i] - 87;
else {
*error_return = STRING_NOT_A_HEX_STRING ;
return ;
}
num += (j << ir);
ir -= 4;
}
if( num < minimum ) {
*error_return = NUMBER_LESS_THAN_MINIMUM ;
return ;
} /* end if */
if( num > maximum ) {
*error_return = NUMBER_GREATER_THAN_MAXIMUM ;
return ;
} /* end if */
/** Return the number **/
*number = num ;
} /* end of ADFI_ASCII_Hex_2_unsigned_int */
/* end of file ADFI_ASCII_Hex_2_unsigned_int.c */
/*------------------------------------------------------------------------------------*/
static void ADFI_convert_integers(
const int size,
const int count,
const char from_format,
const char to_format,
const char *from_data,
char *to_data,
int *error_return)
{
int do_swap = 0;
if (from_format == 'N' || to_format == 'N') {
*error_return = CANNOT_CONVERT_NATIVE_FORMAT;
return;
}
if (from_format != to_format) {
switch (EVAL_2_BYTES(from_format, to_format)) {
case EVAL_2_BYTES('L', 'B'):
case EVAL_2_BYTES('B', 'L'):
case EVAL_2_BYTES('L', 'C'):
case EVAL_2_BYTES('C', 'L'):
do_swap = 1;
break;
case EVAL_2_BYTES('B', 'C'):
case EVAL_2_BYTES('C', 'B'):
break;
default:
*error_return = ADF_FILE_FORMAT_NOT_RECOGNIZED;
return;
}
}
*error_return = NO_ERROR;
if (do_swap) {
int n, i;
for (n = 0; n < count; n++) {
for (i = 0; i < size; i++) {
to_data[i] = from_data[size-i-1];
}
to_data += size;
from_data += size;
}
}
else {
memcpy(to_data, from_data, (size_t)size * (size_t)count);
}
}
/*------------------------------------------------------------------------------------*/
/* file ADFI_Abort.c */
/***********************************************************************
ADFI Abort:
Do any cleanup and then shut the application down.
input: const int error_code Error which caused the Abort.
output: -none- Hey, we ain't coming back...
***********************************************************************/
void ADFI_Abort(
const int error_code )
{
fprintf(stderr,"ADF Aborted: Exiting\n" ) ;
exit( error_code ) ;
} /* end of ADFI_Abort */
/* end of file ADFI_Abort.c */
/* file ADFI_ID_2_file_block_offset.c */
/***********************************************************************
ADFI ID to file block and offset:
The ID is a combination of the file-index, the block within the
file, and an offset within the block.
the file index is an unsigned 16-bit int.
block pointer is a 32-bit unsigned int.
block offset is a 16-bit unsigned int.
input: const double ID Given ADF ID.
output: unsigned int *file_index File index from the ID.
output: unsigned long *file_block File block from the ID.
output: unsigned long *block_offset Block offset from the ID.
output: int *error_return Error return.
Possible errors:
NO_ERROR
NULL_POINTER
FILE_INDEX_OUT_OF_RANGE
BLOCK_OFFSET_OUT_OF_RANGE
***********************************************************************/
void ADFI_ID_2_file_block_offset(
const double ID,
unsigned int *file_index,
cgulong_t *file_block,
cgulong_t *block_offset,
int *error_return )
{
unsigned char * cc;
if( (file_index == NULL) || (file_block == NULL) || (block_offset == NULL) ) {
*error_return = NULL_POINTER ;
return ;
} /* end if */
if( ID == 0.0 ) {
*error_return = NODE_ID_ZERO ;
return ;
} /* end if */
*error_return = NO_ERROR ;
cc = (unsigned char *) &ID;
#ifdef PRINT_STUFF
printf("In ADFI_ID_2_file_block_offset: ID=%lf\n",ID);
printf("cc[0-7] = %02X %02X %02X %02X %02X %02X %02X %02X \n",
cc[0], cc[1], cc[2], cc[3],
cc[4], cc[5], cc[6], cc[7] ) ;
#endif
/** Unmap the bytes from the character **/
#ifdef NEW_ID_MAPPING
if (ADF_this_machine_format == IEEE_LITTLE_FORMAT_CHAR) {
*file_index = (((unsigned int)(cc[7] & 0x3F)) << 6) +
(((unsigned int)(cc[6] & 0xFC)) >> 2);
*file_block = (((cgulong_t)(cc[6] & 0x03)) << 36) +
(((cgulong_t)(cc[5] & 0xFF)) << 28) +
(((cgulong_t)(cc[4] & 0xFF)) << 20) +
(((cgulong_t)(cc[3] & 0xFF)) << 12) +
(((cgulong_t)(cc[2] & 0xFF)) << 4) +
(((cgulong_t)(cc[1] & 0xF0)) >> 4);
*block_offset = (((unsigned int)(cc[1] & 0x0F)) << 8) +
(((unsigned int)(cc[0] & 0xFF)));
}
else {
*file_index = (((unsigned int)(cc[0] & 0x3F)) << 6) +
(((unsigned int)(cc[1] & 0xFC)) >> 2);
*file_block = (((cgulong_t)(cc[1] & 0x03)) << 36) +
(((cgulong_t)(cc[2] & 0xFF)) << 28) +
(((cgulong_t)(cc[3] & 0xFF)) << 20) +
(((cgulong_t)(cc[4] & 0xFF)) << 12) +
(((cgulong_t)(cc[5] & 0xFF)) << 4) +
(((cgulong_t)(cc[6] & 0xF0)) >> 4);
*block_offset = (((unsigned int)(cc[6] & 0x0F)) << 8) +
(((unsigned int)(cc[7] & 0xFF)));
}
#if 0
assert(*file_index <= 0xfff);
assert(*file_block <= 0x3fffffffff);
assert(*block_offset <= 0xfff);
#endif
#else
if ( ADF_this_machine_format == IEEE_BIG_FORMAT_CHAR ) {
*file_index = cc[1] + ((cc[0] & 0x3f) << 8) ;
*file_block = cc[2] + (cc[3]<<8) +
(cc[4]<<16) + (cc[5]<<24) ;
*block_offset = cc[6] + (cc[7]<<8) ;
} /* end if */
else if ( ADF_this_machine_format == IEEE_LITTLE_FORMAT_CHAR ) {
*file_index = cc[6] + ((cc[7] & 0x3f) << 8) ;
*file_block = cc[2] + (cc[3]<<8) +
(cc[4]<<16) + (cc[5]<<24) ;
*block_offset = cc[0] + (cc[1]<<8) ;
} /* end else if */
else {
*file_index = cc[0] + (cc[1]<<8) ;
*file_block = cc[2] + (cc[3]<<8) +
(cc[4]<<16) + (cc[5]<<24) ;
*block_offset = cc[6] + (cc[7]<<8) ;
} /* end else */
#endif
#ifdef PRINT_STUFF
printf("*file_index=%d, *file_block=%d, *block_offset=%d\n",
*file_index, *file_block, *block_offset);
#endif
if( (int)(*file_index) >= maximum_files ) {
*error_return = FILE_INDEX_OUT_OF_RANGE ;
return ;
} /* end if */
if( *block_offset >= DISK_BLOCK_SIZE ) {
*error_return = BLOCK_OFFSET_OUT_OF_RANGE ;
return ;
} /* end if */
} /* end of ADFI_ID_2_file_block_offset */
/* end of file ADFI_ID_2_file_block_offset.c */
/* file ADFI_add_2_sub_node_table.c */
/***********************************************************************
ADFI add 2 sub node table:
Add a child to a parent's sub-node table.
input: const int file_index Index of ADF file.
input: const struct DISK_POINTER *parent Location of the parent
input: const struct DISK_POINTER *child Location of the child.
output: int *error_return Error return.
Possible errors:
NO_ERROR
NULL_POINTER
ADF_FILE_NOT_OPENED
SUB_NODE_TABLE_ENTRIES_BAD
MEMORY_ALLOCATION_FAILED
***********************************************************************/
void ADFI_add_2_sub_node_table(
const int file_index,
const struct DISK_POINTER *parent,
const struct DISK_POINTER *child,
int *error_return )
{
struct NODE_HEADER parent_node, child_node ;
struct SUB_NODE_TABLE_ENTRY *sub_node_table ;
struct DISK_POINTER tmp_disk_ptr ;
unsigned int old_num_entries ;
int i ;
if( (parent == NULL) || (child == NULL) ) {
*error_return = NULL_POINTER ;
return ;
} /* end if */
if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
*error_return = ADF_FILE_NOT_OPENED ;
return ;
} /* end if */
*error_return = NO_ERROR ;
/** Get node_header for the node (parent) **/
ADFI_read_node_header( file_index, parent, &parent_node, error_return ) ;
if( *error_return != NO_ERROR )
return ;
/** Get node_header for the node (child) **/
ADFI_read_node_header( file_index, child, &child_node, error_return ) ;
if( *error_return != NO_ERROR )
return ;
/** Check current length of sub-node_table, add space if needed **/
if( parent_node.entries_for_sub_nodes <= parent_node.num_sub_nodes ) {
old_num_entries = parent_node.entries_for_sub_nodes ;
/** Increase the table space (double it) **/
if( parent_node.entries_for_sub_nodes == 0 )
parent_node.entries_for_sub_nodes = LIST_CHUNK ;
else
parent_node.entries_for_sub_nodes = (unsigned int) (
(float) parent_node.entries_for_sub_nodes * LIST_CHUNK_GROW_FACTOR ) ;
if( parent_node.entries_for_sub_nodes <= parent_node.num_sub_nodes ) {
*error_return = SUB_NODE_TABLE_ENTRIES_BAD ;
return ;
} /* end if */
/** Allocate memory for the required table space in memory **/
sub_node_table = (struct SUB_NODE_TABLE_ENTRY *)
malloc( parent_node.entries_for_sub_nodes *
sizeof( *sub_node_table ) ) ;
if( sub_node_table == NULL ) {
*error_return = MEMORY_ALLOCATION_FAILED ;
return ;
} /* end if */
/** If sub-node table exists, get it **/
if( old_num_entries > 0 ) {
ADFI_read_sub_node_table( file_index, &parent_node.sub_node_table,
sub_node_table, error_return ) ;
if( *error_return != NO_ERROR ) {
free( sub_node_table ) ;
return ;
}
} /* end if */
/** Blank out the new part of the sub-node_table **/
for( i=parent_node.num_sub_nodes; i<(int) parent_node.entries_for_sub_nodes;
i++ ) {
strncpy( sub_node_table[i].child_name,
/* " ", ADF_NAME_LENGTH ) ; */
"unused entry in sub-node-table ", ADF_NAME_LENGTH ) ;
sub_node_table[i].child_location.block = 0 ;
sub_node_table[i].child_location.offset = DISK_BLOCK_SIZE ;
} /* end for */
/** Allocate memory for the required table space on disk **/
if( parent_node.num_sub_nodes > 0 ) { /* delete old table from file */
ADFI_delete_sub_node_table( file_index, &parent_node.sub_node_table,
old_num_entries, error_return ) ;
if( *error_return != NO_ERROR ) {
free( sub_node_table ) ;
return ;
}
} /* end if */
ADFI_file_malloc( file_index, TAG_SIZE + DISK_POINTER_SIZE + TAG_SIZE +
parent_node.entries_for_sub_nodes * (ADF_NAME_LENGTH + DISK_POINTER_SIZE),
&tmp_disk_ptr, error_return ) ;
if( *error_return != NO_ERROR ) {
free( sub_node_table ) ;
return ;
}
parent_node.sub_node_table.block = tmp_disk_ptr.block ;
parent_node.sub_node_table.offset = tmp_disk_ptr.offset ;
/** Write out modified sub_node_table **/
ADFI_write_sub_node_table( file_index, &parent_node.sub_node_table,
parent_node.entries_for_sub_nodes,
(struct SUB_NODE_TABLE_ENTRY *)sub_node_table, error_return ) ;
free( sub_node_table ) ;
if( *error_return != NO_ERROR )
return ;
} /* end if */
/** Insert new entry in sub-node table **/
tmp_disk_ptr.block = parent_node.sub_node_table.block ;
tmp_disk_ptr.offset = parent_node.sub_node_table.offset +
TAG_SIZE + DISK_POINTER_SIZE +
parent_node.num_sub_nodes * (ADF_NAME_LENGTH + DISK_POINTER_SIZE) ;
ADFI_adjust_disk_pointer( &tmp_disk_ptr, error_return ) ;
if( *error_return != NO_ERROR )
return ;
/** Write the child's name **/
ADFI_write_file( file_index, tmp_disk_ptr.block, tmp_disk_ptr.offset,
ADF_NAME_LENGTH, child_node.name, error_return ) ;
if( *error_return != NO_ERROR )
return ;
/** Write out new sub_node_table entry **/
tmp_disk_ptr.offset += ADF_NAME_LENGTH ;
ADFI_adjust_disk_pointer( &tmp_disk_ptr, error_return ) ;
if( *error_return != NO_ERROR )
return ;
ADFI_write_disk_pointer_2_disk( file_index, tmp_disk_ptr.block,
tmp_disk_ptr.offset, child, error_return ) ;
if( *error_return != NO_ERROR )
return ;
/** Write out modified parent node-header **/
parent_node.num_sub_nodes++ ;
ADFI_write_node_header( file_index, parent, &parent_node, error_return ) ;
if( *error_return != NO_ERROR )
return ;
} /* end of ADFI_add_2_sub_node_table */
/* end of file ADFI_add_2_sub_node_table.c */
/* file ADFI_adjust_disk_pointer.c */
/***********************************************************************
ADFI adjust disk pointer:
Adjust the disk pointer so that the offset is in a legal
range; from 0 and < DISK_BLOCK_SIZE.
input: const struct DISK_POINTER *block_offset
output: int *error_return Error return.
Possible errors:
NO_ERROR
NULL_POINTER
BLOCK_OFFSET_OUT_OF_RANGE
***********************************************************************/
void ADFI_adjust_disk_pointer(
struct DISK_POINTER *block_offset,
int *error_return )
{
cgulong_t oblock ;
cgulong_t nblock ;
if( block_offset == NULL ) {
*error_return = NULL_POINTER ;
return ;
} /* end if */
*error_return = NO_ERROR ;
if ( block_offset->offset < DISK_BLOCK_SIZE ) return ;
/** Calculate the number of blocks in the current offset **/
nblock = (cgulong_t) (block_offset->offset / DISK_BLOCK_SIZE) ;
/** Adjust block/offset checking for block roll-over **/
oblock = block_offset->block ;
block_offset->block += nblock ;
block_offset->offset -= nblock * DISK_BLOCK_SIZE ;
if ( block_offset->block < oblock ) {
*error_return = BLOCK_OFFSET_OUT_OF_RANGE ;
return ;
} /* end if */
} /* end of ADFI_adjust_disk_pointer */
/* end of file ADFI_adjust_disk_pointer.c */
/* file ADFI_big_endian_32_swap_64.c */
/***********************************************************************
ADFI big endian 32 swap 64:
input: const char from_format Format to convert from. 'B','L','C','N'
input: const char from_os_size Format to convert from. 'B','L'
input: const char to_format Format to convert to.
input: const char to_os_size Format to convert to. 'B','L'
input: const char data_type[2] The type of data to convert.
MT I4 I8 U4 U8 R4 R8 X4 X8 C1 B1
input: const unsigned long delta_from_bytes Number of from_bytes used.
input: const unsigned long delta_to_bytes Number of to_bytes used.
input: const char *from_data The data to convert from.
output: char *to_data The resulting data.
output: int *error_return Error return.
Recognized data types:
Machine representations
Type Notation IEEE_BIG IEEE_LITTLE Cray
32 64 32 64
No data MT
Integer 32 I4 I4 I4 I4 I4 I8
Integer 64 I8 -- I8 -- I8 I8
Unsigned 32 U4 I4 I4 I4 I4 I8
Unsigned 64 U8 -- I8 -- I8 I8
Real 32 R4 R4 R4 R4 R4 R8
Real 64 R8 R8 R8 R8 R8 R8
Complex 64 X4 R4R4 R4R4 R4R4 R4R4 R8R8
Complex 128 X8 R8R8 R8R8 R8R8 R8R8 R8R8
Character (unsigned byte) C1 C1 C1 C1 C1 C1
Byte (unsigned byte) B1 C1 C1 C1 C1 C1
Machine Numeric Formats:
***IEEE_BIG (SGI-Iris Assembly Language Programmer's Guide, pages 1-2, 6-3)
I4: Byte0 Byte1 Byte2 Byte3
MSB---------------------LSB
R4: Byte0 Byte1 Byte2 Byte3
Bits: sign-bit, 8-bit exponent, 23-bit mantissa
The sign of the exponent is: 1=positive, 0=negative (NOT 2's complement)
The interpretation of the floating-point number is:
>>> 2.mantissia(fraction) X 2^exponent. <<<
R8: Byte0 Byte1 Byte2 Byte 3 Byte 4 Byte5 Byte6 Byte7
Bits: sign-bit, 11-bit exponent, 52-bit mantissa
Machine Numeric Formats:
***IEEE_LITTLE ( The backwards Big Endian )
I4: Byte0 Byte1 Byte2 Byte3
LSB---------------------MSB
R4: Byte0 Byte1 Byte2 Byte3
Bits: 23-bit mantissa, 8-bit exponent, sign-bit
The sign of the exponent is: 1=positive, 0=negative (NOT 2's complement)
The interpretation of the floating-point number is:
>>> 2.mantissia(fraction) X 2^exponent. <<<
R8: Byte0 Byte1 Byte2 Byte 3 Byte 4 Byte5 Byte6 Byte7
Bits: 52-bit mantissa, 11-bit exponent, sign-bit
Note: To convert between these two formats the order of the bytes is reversed
since by definition the Big endian starts at the LSB and goes to the MSB where
the little goes form the MSB to the LSB of the word.
***
Possible errors:
NO_ERROR
NULL_STRING_POINTER
NULL_POINTER
***********************************************************************/
void ADFI_big_endian_32_swap_64(
const char from_format,
const char from_os_size,
const char to_format,
const char to_os_size,
const char data_type[2],
const cgulong_t delta_from_bytes,
const cgulong_t delta_to_bytes,
const unsigned char *from_data,
unsigned char *to_data,
int *error_return )
{
if( (from_data == NULL) || (to_data == NULL) ) {
*error_return = NULL_STRING_POINTER ;
return ;
} /* end if */
if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
*error_return = NULL_POINTER ;
return ;
} /* end if */
if( (from_format == 'N') || (to_format == 'N') ) {
*error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
return ;
} /* end if */
*error_return = NO_ERROR ;
if ( delta_to_bytes == delta_from_bytes ) {
memcpy( to_data, from_data, (size_t)delta_from_bytes ) ;
} /* end if */
else if ( delta_from_bytes < delta_to_bytes ) {
switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
case EVAL_2_BYTES( 'I', '8' ):
if( (from_data[0] & 0x80) == 0x80 ) { /* Negative number */
to_data[0] = 0xff ;
to_data[1] = 0xff ;
to_data[2] = 0xff ;
to_data[3] = 0xff ;
} /* end if */
else {
to_data[0] = 0x00 ;
to_data[1] = 0x00 ;
to_data[2] = 0x00 ;
to_data[3] = 0x00 ;
} /* end else */
to_data[4] = from_data[0] ;
to_data[5] = from_data[1] ;
to_data[6] = from_data[2] ;
to_data[7] = from_data[3] ;
break ;
default:
*error_return = INVALID_DATA_TYPE ;
return ;
} /* end switch */
} /* end else if */
else {
switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
case EVAL_2_BYTES( 'I', '8' ):
to_data[0] = from_data[4] ;
to_data[1] = from_data[5] ;
to_data[2] = from_data[6] ;
to_data[3] = from_data[7] ;
break ;
default:
*error_return = INVALID_DATA_TYPE ;
return ;
} /* end switch */
} /* end else */
} /* end of ADFI_big_endian_32_swap_64 */
/* end of file ADFI_big_endian_32_swap_64.c */
/* file ADFI_big_endian_to_cray.c */
/***********************************************************************
ADFI big endian to cray:
input: const char from_format Format to convert from. 'B','L','C','N'
input: const char from_os_size Format to convert from. 'B','L'
input: const char to_format Format to convert to.
input: const char to_os_size Format to convert to. 'B','L'
input: const char data_type[2] The type of data to convert.
MT I4 I8 U4 U8 R4 R8 X4 X8 C1 B1
input: const unsigned long delta_from_bytes Number of from_bytes used.
input: const unsigned long delta_to_bytes Number of to_bytes used.
input: const char *from_data The data to convert from.
output: char *to_data The resulting data.
output: int *error_return Error return.
Recognized data types:
Machine representations
Type Notation IEEE_BIG IEEE_LITTLE Cray
32 64 32 64
No data MT
Integer 32 I4 I4 I4 I4 I4 I8
Integer 64 I8 -- I8 -- I8 I8
Unsigned 32 U4 I4 I4 I4 I4 I8
Unsigned 64 U8 -- I8 -- I8 I8
Real 32 R4 R4 R4 R4 R4 R8
Real 64 R8 R8 R8 R8 R8 R8
Complex 64 X4 R4R4 R4R4 R4R4 R4R4 R8R8
Complex 128 X8 R8R8 R8R8 R8R8 R8R8 R8R8