-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundart-2016.c
1326 lines (1249 loc) · 42.2 KB
/
undart-2016.c
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
#if 0
echo Self compiling source on machine type $(uname -m)
if [ $(uname -m) == 'x86_64' ];then M=-m64;else M='';fi
echo gcc -g $M -Wall -Werror -o /usr/local/bin/undart main.c md5.c
time gcc -g $M -Wall -Werror -o /usr/local/bin/undart main.c md5.c
return
#endif
/* undart-2016.c
* 2016 notes
* =======================================================================================
I am now on Linux distribution Ubuntu 15.04 with XFCE not Unity. The uname -a says:
Linux work8 3.19.0-20-generic #20-Ubuntu SMP
Fri May 29 10:10:47 UTC 2015
x86_64 x86_64 x86_64 GNU/Linux
last year it was
Linux work15 35-generic #62-Ubuntu SMP
Fri Aug 15 01:58:42 UTC 2014
x86_64 x86_64 x86_64 GNU/Linux
The SAIDART blob serial numbering may be considered as
the 'Accession Numbers' for the collection of SAILDART
as if it were a Digital Libary.
For my 2015 simplified SETUP of a TARGET disk,
the disk today happens to be at /dev/sdd1
so mount point /d and generic LINUX standard file system ext4
without my usual LOOP mounts and RAID1 mechanism.
# Initialize file system on large disk ( 2 TiB )
# Was: echo ,,L | sfdisk /dev/sdf
# new this year
sgdisk -Z /dev/sdd
gdisk ENTER then commands 'n' and 'w' take all the defaults to make one partition
mkfs.ext4 /dev/sdd1
mount /dev/sdd1 /d
# Make TARGET directories
mkdir -p /d/2015/{csv,dart,data13/sn,data7/sn,log,tbz}
chown bgb:bgb -R /d/
# Link absolute pathnames to point at the new TARGET file system directories
sudo bash
ln -s /d/2015/data7 /data7
ln -s /d/2015/data13 /data13
# Unpack the compressed DART records from the 229 reels of tape ... 17m34s
# yielding 41_594 dart records
cd /d/2015/dart
time tar -xf /d/2015/lzma/dart_records_from_229_reels.tar.lzma
# run the UNDART .. took 75m 25 seconds
`2014-09-12 bgbaumgart@mac.com'
`2015-06-16 bgbaumgart@mac.com'
#
# Short test, undart.
#
cd /dartrecords
undart p3000.00?
# ASSERT 356 sn files created
# ASSERT file /data7/sn/000030 is FORTUN.TXT with 119 fortune cookie lines.
# Check that $(head -1 /data7/sn/000030) yields the fortune cookie:
# "You will soon meet a person who will play an important role in your life."
#
# Full run, undart. Single process.
#
cd /dartrecords
# ASSERT readdir .|wc -l is 41594
time undart p*
#
# 2015 Maria DB replacing Oracle Mysql, using the ubuntu 15.04 package
#
apt-get install mariadb-server
mysql_secure_installation
mysql -h localhost -u root mysql -p PASSWORD_FOR_DB_ROOT
CREATE USER 'bgb'@'localhost' IDENTIFIED BY 'foo99baz';
CREATE DATABASE saildart CHARACTER SET UTF8;
GRANT ALL ON saildart.* TO 'bgb'@'localhost';
#
# Load DART meta from CSV files and do some SQL scripting
#
1st-create-dart.sql
2nd-create-snhash-and-mfd.sql
3rd-create-people.sql
#
# data conversions from PAST blobs to PRESENT formats
#
#
# creat static web site into Lcorpus
#
ln -s /Lcorpus/static /static
ln -s /Lcorpus/static/html /html
ln -s /Lcorpus/content /content
#
#
#
* =======================================================================================
* copyright:(c)Ⓒ2001-2015 Bruce Guenther Baumgart
* software_license: GPL license Version 3.
* http://www.gnu.org/licenses/gpl.txt MD5=393a5ca445f6965873eca0259a17f833
* =======================================================================================
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <assert.h>
#include "md5.h"
typedef unsigned long long uint64;
typedef long long int64;
/* PDP-10 word formats (little endian for Intel and AMD x86 architecture)
* ----------------------------------------------------------------------
* The PDP-6 and the PDP-10 are BIG endian. Network order is BIG endian.
* The PDP-11, Intel and AMD are LITTLE endian. x86 ( 32bit i686 and 64bit x86_64 ).
*
* Union and 'C' bit field struct are endian dependent.
* This union is only good on little endian machines.
*/
typedef union
{
uint64 fw;
char byte[8];
struct { uint64 word:36,:28; } full;
struct { int64 right:18,left:18,:28; } half;
struct { int64 right:18,x:4,i:1,a:4,op:9,:28; } instruction;
struct { uint64 byte5:4, byte4:8, byte3:8, byte2:8, byte1:8,:28; } tape; // packing.
struct { uint64 c6:6,c5:6,c4:6,c3:6,c2:6,c1:6,:28; } sixbit; // ASCII sixbit+040
struct { uint64 bit35:1,a5:7,a4:7,a3:7,a2:7,a1:7,:28; } seven; // seven bit ASCII
// WAITS file RIB "Retrieval Information Block" format
struct { uint64 :15,date_hi:3,:46; } word2;
struct { uint64 date_lo:12,time:11,mode:4,prot:9,:28; } word3;
struct { uint64 count:22,:42; } word6;
// HEAD TAIL block bit fields
struct { uint64 date_lo:12,time:11,:9,prev_media_flag:1,date_hi:3,:28; } head_tail_word3;
} pdp10_word;
// #define MAX_BYTE_COUNT 39168000 // Largest DART tape pdp10-file in bytes, so lets use 40 MB.
#define MAX_BYTE_COUNT 40000000 // Larger then all DART tape pdp10-file in bytes.
#define MAX_WORD_COUNT ((MAX_BYTE_COUNT+4)/5)
unsigned char data5[ MAX_BYTE_COUNT ];
pdp10_word data8[ MAX_WORD_COUNT ]; // 64-bit words.
extern int errno;
/*
* globals
*/
char *saildartroot = ""; /* was "/data/YEAR" */
FILE *stdlog;
FILE *stdcsv;
FILE *snhash;
char str[128];
int tapeno=0;
char dartreel[32];
long long int byte_count, word_count;
int bad_data_word=0;
int waitsdate; /* WAITS file date and time */
int waitstime; /* WAITS file date and time */
int waitsprot; /* WAITS file protection */
int waitsmode; /* WAITS file data mode */
int sailprot; /* WAITS file protection === SAIL prot of SYSTEM.DMP[J17,SYS] */
char iso_datetime[64];
int year, month, day, hour, minute, second;
char device[7];
char filnam[7], ext[4], prj[4], prg[4];
int data_word_count; /* Number of 36-bit words in current file */
int record_type;
int record_length;
int ma_old, length_old;
/*
* MD5 one way hash, from the FSF coreutil package.
*/
struct md5_ctx ctx;
unsigned char data5_MD5_digest[16]; char data5_MD5_hash[32+1];
unsigned char data7_MD5_digest[16]; char data7_MD5_hash[32+1];
unsigned char data8_MD5_digest[16]; char data8_MD5_hash[32+1];
int serial_number=0;
/*
* File output buffers for present-day (circa 2010) formats.
*/
unsigned long ofile_switch;
unsigned long obuf8_word_count;
unsigned long size5, size8;
int ubyte_cnt, uchar_cnt;
unsigned long long obuf8[ MAX_WORD_COUNT ];
unsigned char obuf5[ MAX_BYTE_COUNT ];
unsigned char obuf_utf8[ 3*MAX_BYTE_COUNT ];
unsigned char ucontent[ 3*MAX_BYTE_COUNT ]; /* utf8 back slashed for insert statement value */
char ucommand[ 3*MAX_BYTE_COUNT ]; /* insert command */
/*
* PDP-10 file statistics used to assign filetype TEXT when likely, otherwise BINARY.
*/
long pdp10_words=0;
long bit35_count=0; /* count words in file with BIT#35 on */
long non_numeric_byte5_and_bit35 = 0; /* NOT possible in TEXT file, common in BINARY. */
long pushj_count=0; // PDP binary code criterion
long crlf_count=0; /* count CR-LF two byte strings marking end-of-line of text */
long nzbytes=0; // count NON-ZERO byte characters in a data5 file
char filetype='?';
char Public='?'; // 'Y' or 'N' default is NO presume PRIVATE.
char copyright_hit='N'; // yes or no on whether or not the string 'copyright' appeared in the file
/* ascii SIXBIT decoding functions
* -------------------------------------------------------------------- *
* sixbit word into ASCII string
* sixbit halfword into ASCII string
*/
char *
sixbit_word_into_ascii(char *p0,pdp10_word w)
{
char *p=p0;
*p++ = (w.sixbit.c1 + 040);
*p++ = (w.sixbit.c2 + 040);
*p++ = (w.sixbit.c3 + 040);
*p++ = (w.sixbit.c4 + 040);
*p++ = (w.sixbit.c5 + 040);
*p++ = (w.sixbit.c6 + 040);
*p++= 0;
return p0;
}
char *
sixbit_halfword_into_ascii(char *p0,int halfword)
{
char *p=p0;
*p++ = ((halfword >> 12) & 077) + 040;
*p++ = ((halfword >> 6) & 077) + 040;
*p++ = (halfword & 077) + 040;
*p++ = 0;
return p0;
}
/* Convert SAIL file WAITS protection bits into Unix file protection bits.
* -----------------------------------------------------------------------
*/
int
waits2unixprot(unsigned int prot)
{
prot &= ~0444;// Turn off meaningless bits
prot <<= 1; // Shift the WAITS "rw" bits into the correct UNIX places
prot ^= 0666; // Change polarity
prot |= 0400; // ENABLE read for owner
return(prot);
}
/* Convert 5-byte PDP-10 "tape-words" into 8-byte unsigned long long words.
* -------------------------------------------------------------------- *
* The PDP-10 ( DEC System-20 ) tape words were 36-bits packed into bytes in
* BIG Endian order with the final four bits in the low order of the fifth byte.
* The 36-bit PDP-10 words are placed into the low order side of a 64-bit
* unsigned long long union so that bit field notation can be used for unpacking.
*/
void
convert_words_5bytes_to_8bytes( char *src_file ){
int i,ma;
/*
* reset
*/
bzero(data5,sizeof(data5));
bzero(data8,sizeof(data8));
fflush(stdlog);
fflush(stdcsv);
/*
* Input from source file
*/
errno = 0;
i = open(src_file,O_RDONLY);
if (i<0){
fprintf(stderr,"ERROR: input source open file \"%s\" failed.\n",src_file);
return;
}
byte_count = read(i,data5,sizeof(data5));
close(i);
word_count = byte_count/5;
if(0)
fprintf(stderr,"%9s read n=%lld bytes, errno=%d, word_count=%lld=0%llo\n",
src_file, byte_count,errno,word_count,word_count);
fprintf(stdlog,"%9s read n=%lld bytes, errno=%d, word_count=%lld=0%llo\n",
src_file, byte_count,errno,word_count,word_count);
fflush(stdlog);
assert( word_count < MAX_WORD_COUNT );
for( ma=0; ma < word_count; ma++ ){
int q = ma*5;
data8[ma].tape.byte1 = data5[q];
data8[ma].tape.byte2 = data5[q+1];
data8[ma].tape.byte3 = data5[q+2];
data8[ma].tape.byte4 = data5[q+3];
data8[ma].tape.byte5 = data5[q+4];
}
}
void
force_to_lowercase(char *p)
{
for (;*p;p++)
if ('A'<=*p && *p<='Z')
*p |= 040;
}
/* Replace ugly SIXBIT filename chararacters
* -----------------------------------------
* In filename SIXBIT there are 26 + 10 alphanumeric characters,
* plus nine more characters that may appear in a SAILDART filename:
* hash # dollar $
* percent % ampersand &
* plus + minus -
* equal sign =
*
* Use capital letters in unix version of PDP-10 filenames to replace characters
* that are ugly to use on unix GNU/Linux file systems.
* CHARACTER NAME UGLY Replacement
* -------------- ---- -----------
* ampersand '&' A
* colon ':' C
* space ' ' S
* bang '!' B
* double quote '"' Q
* left parens '(' L left
* right parens ')' R right
* left square bracket '[' I like In
* right square bracket ']' O like Out
* caret '^' V
* underbar '_' U
* asterisk(aka star) '*' X
* comma ',' Y
* slash '/' Z
* less-than '<' N thaN
* greater-than '>' M More
* question-mark '?' W Who-What-Where-When WTF
* back-slash '\\' K bacK
* dot '\.' D
* semicolon ';' S
* apostrophe "'" P aPPostroPPhe dang FILNAM . LPT [SPL,SYS]
*/
void
replace_ugly_characters(char *p)
{
char *q;
char *ugly = ";'" "#" "&" ":" "!" "\"" "()" "[]" "^" "_" "*" "," "/" "<>" "?" "\\" ".";
char *pretty = "SP" "H" "A" "C" "B" "Q" "LR" "IO" "V" "U" "X" "Y" "Z" "NM" "W" "K" "D";
assert( strlen(ugly) == strlen(pretty)); /* so check the character count */
while ((q= strpbrk(p,ugly)))
{
*q = pretty[index(ugly,*q)-ugly]; /* Beautification by table lookup */
}
}
void
omit_spaces(char *q)
{
char *t,*p;
for (p= t= q; *p; p++)
if (*p != ' ')
*t++ = *p;
*t++ = 0;
}
void
unixname(char* flat, char* filnam, char* ext, char* prj, char* prg)
{
char clean_filnam[16]={},clean_ext[16]={},clean_prj[16]={},clean_prg[16]={};
bzero(clean_filnam,16);
bzero(clean_ext,16);
bzero(clean_prj,16);
bzero(clean_prg,16);
/* Convert UPPER to lower and replace ugly punctuation with UPPER codes */
strncpy( clean_filnam, filnam, 6 );
strncpy( clean_ext, ext, 3 );
strncpy( clean_prj, prj, 3 );
strncpy( clean_prg, prg, 3 );
omit_spaces( clean_filnam );
omit_spaces( clean_ext );
omit_spaces( clean_prj );
omit_spaces( clean_prg );
force_to_lowercase( clean_filnam );
force_to_lowercase( clean_ext );
force_to_lowercase( clean_prj );
force_to_lowercase( clean_prg );
replace_ugly_characters( clean_filnam );
replace_ugly_characters( clean_ext );
/* UNIX like order: Programmer, Project, Filename, dot, Extension */
sprintf( flat, "%.3s_%.3s_%.6s.%.3s", clean_prg, clean_prj, clean_filnam, clean_ext );
/* Remove trailing dot, if extension is blank */
if ( flat[strlen(flat)-1] == '.' ){
flat[strlen(flat)-1] = 0;
}
}
/* Find next good record header
* -------------------------------------------------------------------- *
*/
int
advance_to_good_record_header(int ma)
{
int ma2, p_length;
long long n, q;
unsigned char *p;
int ma_new;
/**/
bad_data_word++;
if (ma == (ma_old+1)){
ma += 2;
}
assert( ma < MAX_WORD_COUNT );
q = ma*5;
// Scan for record marker \223 \72 \300 \0 \0 which is SIXBIT/DSK /.
p = data5 + q - 1;
do {
p++;
n = byte_count - (p - data5);
p = (unsigned char *)memchr((void *)p,'\223', n );
p_length = p ? ((p[-3]&077)<<12) | (p[-2]<<4) | (p[-1]&0xF) : 0;
// p_DSK = p ? ((uint64)p[0]<<28 | p[1]<<20 | p[2]<<12 | p[3]<<4 | (0xF & p[4])) : 0;
} while (p && !(
p[0]==0223 && p[1]==072 && p[2]==0300 && p[3]==0 && p[4]==0 // sixbit 'DSK '
&& p[-5]==0377 && p[-4]==0377 && (p[-3]&0300)==0100 // xwd -3,,length
&& p_length>=59
&& p_length<=10238
));
if ( ! p ){
// ABORT scan. No further good record marker found.
return word_count; // Return next ma just beyond EOF.
}
// repack into words starting from the possibly good byte position.
p -= 5;
ma_new = ((p+1) - data5) / 5;
assert( ma_new < MAX_WORD_COUNT );
for( ma2=ma_new; ma2 < word_count; ma2++ ){
data8[ma2].tape.byte1 = *p++;
data8[ma2].tape.byte2 = *p++;
data8[ma2].tape.byte3 = *p++;
data8[ma2].tape.byte4 = *p++;
data8[ma2].tape.byte5 = *p++;
}
ma_old = ma_new;
length_old = p_length; // supposed length of newly found record.
return ma_new; // continue scan.
}
/* This table provides a unicode 16-bit value for each SAIL 7-bit character. */
unsigned short sail_unicode[128]={
0,
0x2193, // ↓ down arrow
0x03b1, // α alpha
0x03b2, // β beta
0x2227, // ∧ boolean AND
0x00ac, // ¬ boolean NOT
0x03b5, // ε epsilon
0x03c0, // π pi
0x03bb, // λ lambda
011, 012, 013, 014, 015, // TAB, LF, VT, FF, CR as white space
0x221e, // ∞ infinity
0x2202, // ∂ partial differential
0x2282, 0x2283, 0x2229, 0x222a, // ⊂ ⊃ ∩ ∪ horseshoes left, right, up down.
0x2200, 0x2203, 0x2297, 0x2194, // ∀ ∃ ⊗ ↔ forall, exists, xor, double arrow.
0x005f, 0x2192, 0x007e, 0x2260, // _ → ~ ≠ underbar, right arrow, tilde, neq
0x2264, 0x2265, 0x2261, 0x2228, // ≤ ≥ ≡ ∨ le, ge, equ, boolean OR
040,041,042,043,044,045,046,047,
050,051,052,053,054,055,056,057,
060,061,062,063,064,065,066,067,
070,071,072,073,074,075,076,077,
0100,0101,0102,0103,0104,0105,0106,0107,
0110,0111,0112,0113,0114,0115,0116,0117,
0120,0121,0122,0123,0124,0125,0126,0127,
0130,0131,0132,0133,0134,0135,
0x2191, // ↑ up arrow
0x2190, // ← left arrow
0140,0141,0142,0143,0144,0145,0146,0147,
0150,0151,0152,0153,0154,0155,0156,0157,
0160,0161,0162,0163,0164,0165,0166,0167,
0170,0171,0172,
0x7b, // { left curly bracket
0x7c, // | vertical bar
0x2387, // ⎇ altmode is a keystroke character glyph NOT a shift key.
0x7d, // } right curly bracket ASCII octal 0175, but SAIL code 0176 !
0x2408 // Backspace unicode BS backspace glyph ␈u2408
};
/*
* Initialize the UTF8 tables using the 16-bit unicode characters
* defined in sail_unicode[128] corresponding to the 7-bit SAIL incompatible ASCII codes.
*/
char utf8_[128*4]; // content of utf strings
char *utf8[128]; // string pointers
int utf8size[128]; // string length
#define PUTCHAR(U)*p++=(U)
void
initialize_utf8_tables(){
int i,u;
char *p=utf8_;
/**/
for (i=0;i<128;i++){
utf8[i] = p;
u = sail_unicode[i];
if ( 0x0001<=u && u<=0x007f ){
utf8size[i]=1;
PUTCHAR( u );
PUTCHAR(0);
PUTCHAR(0);
PUTCHAR(0);
} else
if ( 0x00a0<=u && u<=0x07ff ){
utf8size[i]=2;
PUTCHAR(0xC0 | ((u>>6) & 0x1F));
PUTCHAR(0x80 | ( u & 0x3F));
PUTCHAR(0);
PUTCHAR(0);
} else
if ( 0x0800<=u && u<=0xffff ){
utf8size[i]=3;
PUTCHAR(0xE0 | ((u>>12) & 0x0F));
PUTCHAR(0x80 | ((u>>6 ) & 0x3F));
PUTCHAR(0x80 | ( u & 0x3F));
PUTCHAR(0);
} else {
utf8size[i]=0;
PUTCHAR(0);
PUTCHAR(0);
PUTCHAR(0);
PUTCHAR(0);
}
}
}
#undef PUTCHAR
void
convert_data5_into_utf8(void){
unsigned char *p=obuf_utf8;
unsigned char c;
int i,n;
ubyte_cnt = 0;
uchar_cnt = 0;
for ( i=0; i < size5; i++ ){
c = obuf5[i] & 0x7F;
n = utf8size[c];
if ( !n ) continue;
strncpy( (char *)p, (char *)utf8[c], n );
ubyte_cnt += n;
uchar_cnt += 1;
p += n;
// Replace one CR-LF of output with one LF. Note input i continues.
if ( i>0 && p[-2]==015 && p[-1]==012 ){
uchar_cnt--;
ubyte_cnt--;
p[-2] = 012;
p[-1] = 0;
p--;
}
}
}
void
output_data7_file( int wrdcnt ){
char data7_md5_name[120];
char data7_sn_name[120];
int i,o,n;
char *p, *q;
/**/
convert_data5_into_utf8();
md5_init_ctx( &ctx );
md5_process_bytes( obuf_utf8, ubyte_cnt, &ctx );
md5_finish_ctx( &ctx, data7_MD5_digest );
p = (char *)data7_MD5_digest;
q = data7_MD5_hash;
for (i=0; i<16; i++)
{
*q++ = "0123456789abcdef"[(*p & 0xF0 ) >> 4 ];
*q++ = "0123456789abcdef"[(*p++) & 0xF ];
}
sprintf( data7_md5_name,"%s/data7/md5/%s",saildartroot,data7_MD5_hash );
sprintf( data7_sn_name,"%s/data7/sn/%06d",saildartroot,serial_number);
errno = 0;
if(!access( data7_sn_name, F_OK )){
/* file content already exists. */
errno = 0;
return;
}
errno = 0;
o = open( data7_sn_name, O_CREAT | O_WRONLY | O_EXCL, 0400 );
if (!( o > 0)){
fprintf(stderr,"Open failed data7 filename='%s'\n", data7_sn_name);
/* ASSUME file content already exists (parallel processing) */
errno=0;
return;
}
assert( o > 0);
n = write( o, obuf_utf8, ubyte_cnt );
assert( n == ubyte_cnt );
assert(!close( o ));
// assert(!link(data7_md5_name,data7_sn_name));
}
/*
* Wrap the source string inside Double Quote marks and
* do the back slashing of infixed single-quote, double-quite and slash.
*/
void dq( char *dst, char *src ){
*dst++ = '"';
while( *src ){
switch( *src ){
case '"':
case '\\':
case '\'':
*dst++ = '\\'; // backslash prefix for the csv special characters
}
*dst++ = *src++;
}
*dst++ = '"';
*dst++ = 0;
}
// Poor man's database for looking up an accession serial number,
// given a content blob MD5 hash string.
char *key[999999]; // Sorted MD5 hash strings of content blobs,
long val[999999]; // corresponding SN# serial number for each content blob.
long snmax=886464; // next sn
long
select_sn(char *hash){
int h,j,k;
j = k = ( snmax >>1 );
// binary search
while((k>0) && (h=strncmp(key[j],hash,32))){
k >>= 1;
if( h>0 ){ j -= k; }else
if( h<0 ){ j += k; }else
return val[j];
}
// linear search
for(;0>(h=strncmp(key[j],hash,32));j++){}
for(;0<(h=strncmp(key[j],hash,32));j--){}
// assert( h==0 );
if(h != 0){
int snval = snmax++; // mint coin a new serial number
// whirr
for( k=snval-2; strncmp(key[k],hash,32)>0; k--){
key[k+1] = key[k];
val[k+1] = val[k];
}
// plop
key[k] = malloc(34);
strncpy(key[k],hash,32);
val[k] = snval;
return snval;
}
return val[j];
}
void
output_data8_file(int wrdcnt){
int i,o,n; char *p, *q;
char data8_md5_name[120];
char data8_sn_name[120];
/*
* hash content of output buffer into an MD5 value
*/
md5_init_ctx( &ctx );
md5_process_bytes( obuf8, wrdcnt*8, &ctx );
md5_finish_ctx( &ctx, data8_MD5_digest );
p = (char *)data8_MD5_digest;
q = data8_MD5_hash;
for (i=0; i<16; i++)
{
*q++ = "0123456789abcdef"[(*p & 0xF0 ) >> 4 ];
*q++ = "0123456789abcdef"[(*p++) & 0xF ];
}
sprintf(data8_md5_name,"%s/data8/md5/%s",saildartroot,data8_MD5_hash);
sprintf(data8_sn_name,"%s/data8/sn/%06d",saildartroot,serial_number);
obuf8_word_count = wrdcnt;
size8 = obuf8_word_count * 8;
errno=0;
if(! access(data8_sn_name,F_OK) ){
/* file content already exists */
errno=0;
return;
}
errno = 0;
o = open( data8_sn_name, O_CREAT | O_WRONLY | O_EXCL, 0400 );
if (!( o > 0)){
fprintf(stderr,"Open failed filename data8_sn_name='%s'\n", data8_sn_name);
}
assert( o > 0);
n = write( o, obuf8, size8 );
assert( n == size8 );
assert(!close( o ));
// assert(!link(data8_md5_name,data8_sn_name));
}
void
output_data13_file(int wrdcnt){
char data13_sn_name[120];
FILE *fd;
int i;
sprintf(data13_sn_name,"%s/data13/sn/%06d",saildartroot,serial_number);
errno = 0;
if(! access(data13_sn_name,F_OK) ){
/* file content already exists */
errno=0;
return;
}
errno = 0;
fd = fopen( data13_sn_name, "w" );
if (errno){
fprintf(stderr,"Open failed filename data13_sn_name='%s'\n", data13_sn_name);
/* ASSUME file content already exists (parallel processing) */
errno=0;
return;
}
assert(!errno);
for ( i=0; i<wrdcnt; i++ ){
fprintf(fd,"%012llo\n",obuf8[i]);
}
assert(!fclose( fd ));
}
void
data_record_payload_to_output_buffers( int base, int seen, int wrdcnt ){
int i;
/*
* Copy from input buffer to output buffers
*/
for (i=0; i<wrdcnt; i++){
int offset=5*(seen+i);
/* data5 format */
obuf5[offset] = data8[base+i].seven.a1;
obuf5[offset+1] = data8[base+i].seven.a2;
obuf5[offset+2] = data8[base+i].seven.a3;
obuf5[offset+3] = data8[base+i].seven.a4;
obuf5[offset+4] = data8[base+i].seven.a5 | (data8[base+i].seven.bit35 ? 0x80 : 0);
/* data8 format */
obuf8[seen+i] = data8[base+i].fw ;
}
}
#define MAX_SNHASH 886463
unsigned char snhash5[999999];
int
output_data5_file(int wrdcnt){
char *p, *q;
int i;
/* compute md5 hash of this content blob */
md5_init_ctx( &ctx );
md5_process_bytes( obuf5, wrdcnt*5, &ctx );
md5_finish_ctx( &ctx, data5_MD5_digest );
p = (char *)data5_MD5_digest;
q = data5_MD5_hash;
for (i=0; i<16; i++)
{
*q++ = "0123456789abcdef"[(*p & 0xF0 ) >> 4 ];
*q++ = "0123456789abcdef"[(*p++) & 0xF ];
}
// In the EARLY YEARS of SAILDART (1998 to 2008) I renumbered the content lumps
// each time a reconversion from the DART files was done. Increment the serial number here
// serial_number++ and writing md5 filenames to detect and to avoid collisions.
// For latter day reconversions the BENEFIT of maintaining the serial numbering was attractive.
// If new raw DART material surfaces (perhaps from a 2nd reading of the original tapes)
// then I would suggest generating yet higher serial numbers for the new lumps of content.
size5 = wrdcnt*5; // Global var used in output_data7_file (sigh).
serial_number = select_sn( data5_MD5_hash );
assert( serial_number );
assert( serial_number <= 999999 ); // Tolerate new ones
if( snhash5[serial_number] )
return 0; // Already did this one.
snhash5[serial_number] = 1; // mark TRUE.
return 1;
}
void
pdp10_file_statistics(){
int i; char c;
/* reset */
pushj_count = 0;
bit35_count = 0;
crlf_count = 0;
filetype = '?';
nzbytes = 0;
non_numeric_byte5_and_bit35 = 0;
/* empty file content is type '?' */
if(!data_word_count){
return;
}
/*
* Scan by 36-bit word, count specific OPCODE and TEXT patterns.
*/
for (i=0;i<data_word_count;i++){
pdp10_word w;
w.fw = obuf8[i];
if ( w.instruction.op == 0260)
pushj_count++;
if ( w.seven.bit35 ){
bit35_count++;
c = w.seven.a5;
// TVEDIT line numbers were marked with 8th bit in 5th digit.
// TVEDIT pages were terminated with five spaces, 5th one as bits 0240.
if (!( ('0'<=c && c<='9') || c==' '))
non_numeric_byte5_and_bit35++;
}
}
/*
* Scan by 7-bit byte, count NON-Zero bytes and count CRLF pairs.
*/
if ( obuf5[0] )
nzbytes++;
for ( i=1; i<size5; i++ ){
if ( obuf5[i-1]=='\r' && obuf5[i]=='\n')
crlf_count++ ;
if ( obuf5[i] )
nzbytes++;
}
/*
* Determine type of data in file from content clues only.
*/
filetype = '?';
if ( pushj_count > 100 ){
filetype = 'X';
}
/*
* Test for text editor E header lines
*/
if ( !strncmp( (char *)obuf5, "COMMENT \26 VALID ",18)
&& !strncmp( (char *)(obuf5+23), " PAGES\r\nC REC PAGE DESCRIPTION\r\n",34)){
filetype = 'C';
} else if ( crlf_count >= bit35_count/2 && non_numeric_byte5_and_bit35<=9 ) {
/* CRLF new lines equals or exceeds half the bit35 count,
with no significant number of binary looking byte5's.
So ten words of noise tolerated inside a text file.
*/
filetype = 'T';
}
/*
* Test for the appearance of the word 'copyright'
*/
copyright_hit = strcasestr((char *)obuf_utf8,"copyright") ? 'Y' : 'N';
/*
* Test for a video file header.
*/
if ( obuf8[0]== 0xFFFFFFFFFLL
&& ( obuf8[1]==4 || obuf8[1]==6 )
&& ((obuf8[7] & 0x800000000LL) != 0 )){
filetype = 'V';
}
}
void
process_data_record( int ma, char *dartfile ){
int j, record_type, wrdcnt, record_length, mode, prot;
uint64 check_sum_now, check_sum_word;
char xorcheck[8];
static int data_words_seen=0;
/**/
record_type = data8[ma].half.left;
record_length = data8[ma].half.right;
sixbit_word_into_ascii( filnam, data8[ma+2] );
sixbit_halfword_into_ascii( ext, data8[ma+3].half.left );
sixbit_halfword_into_ascii( prj, data8[ma+5].half.left );
sixbit_halfword_into_ascii( prg, data8[ma+5].half.right );
waitsdate= data8[ma+3].word2.date_hi <<12 | data8[ma+4].word3.date_lo;
waitstime= data8[ma+4].word3.time;
waitsmode= data8[ma+4].word3.mode;
waitsprot= data8[ma+4].word3.prot;
sailprot= data8[ma+4].word3.prot; // sweeter by another name
prot = waits2unixprot( waitsprot );
mode = waitsmode;
day = waitsdate % 31 + 1;
month = (waitsdate/31) % 12 + 1;
year = ((waitsdate/31) / 12 + 64) + 1900;
hour = waitstime / 60;
minute = waitstime % 60;
second = 0;
sprintf( iso_datetime, "%d-%02d-%02dT%02d:%02d:%02d", year,month,day, hour,minute,second );
data_word_count = data8[ma+7].word6.count;
assert( data_word_count < MAX_WORD_COUNT );
// checksum check using XOR
check_sum_now = 0;
check_sum_word = (uint64)data8[ma+1+record_length].full.word;
for( j=0; j<record_length; j++){
check_sum_now ^= data8[ma+1+j].full.word;
}
if ( check_sum_now == check_sum_word ){
sprintf(xorcheck,"^^OK^^");
if (record_type == -3){
data_words_seen = 0;
}
wrdcnt = record_length - 59;
data_record_payload_to_output_buffers( ma+36, data_words_seen, wrdcnt );
data_words_seen += wrdcnt;
if ( data_words_seen == data_word_count ){
int new_sn=0; // Boolean
data8_MD5_hash[0] = 0; size8 = 0;
data5_MD5_hash[0] = 0; size5 = 0;
data7_MD5_hash[0] = 0; ubyte_cnt = 0;
#define TOKEN(x,y,z) ((x<<16)|(y<<8)|z)
switch(TOKEN(prg[0],prg[1],prg[2])){
case TOKEN(' ',' ','2'):
// [2,2] email area is private.
// [3,2] and [1,2] notices and announcements are public.
Public = (!strncmp(" 2",prj,3)) ? 'N' : 'Y';
break;
case TOKEN(' ',' ','3'):
case TOKEN('B','G','B'):
case TOKEN('D','O','C'):
case TOKEN('C','S','R'):
case TOKEN('S','Y','S'):
case TOKEN('L','S','P'):
case TOKEN('A','I','L'):
case TOKEN('N','E','T'):
case TOKEN('D','E','K'):
case TOKEN('M','U','S'):
case TOKEN('M','R','C'):
case TOKEN('T','E','X'):
case TOKEN('D','R','W'):
Public = 'Y';
break;
default:
Public = 'N';
break;
}
#undef TOKEN
new_sn = output_data5_file( data_word_count );
if ( new_sn ){
pdp10_file_statistics();
output_data7_file( data_word_count );
// insert_text_into_database();
output_data8_file( data_word_count );
output_data13_file( data_word_count );
fprintf(snhash,"%06d" // 1. sn
",%s" // 2. hash
",%s" // hash
",%s" // hash
",%c" // 3. type
",%ld" // 4. nzbytes
",%ld" // 5. crlf
",%ld" // 6. bit35
",%ld" // 7. pushj
",%ld" // 8. notdig
",%ld" // 9. size8
",%d" // 10. bytes
",%d" // 11. chars
",%c" // string 'copyright' hit
"\n",
serial_number, // 1.
data5_MD5_hash, // 2.
data7_MD5_hash,
data8_MD5_hash,
filetype, // 3.
nzbytes, // 4.
crlf_count, // 5.
bit35_count, // 6.
pushj_count, // 7.
non_numeric_byte5_and_bit35, // 8.
size8, // 9.
ubyte_cnt, // 10.
uchar_cnt, // 11.
copyright_hit
);
fflush(snhash);
}
{
char dq_filnam[16];
char dq_ext[16];