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
|
implement Palmfile;
#
# Copyright © 2001-2002 Vita Nuova Holdings Limited. All rights reserved.
#
# Based on ``Palm® File Format Specification'', Document Number 3008-004, 1 May 2001, by Palm Inc.
# Doc compression based on description by Paul Lucas, 18 August 1998
#
include "sys.m";
sys: Sys;
include "daytime.m";
daytime: Daytime;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "palmfile.m";
Dbhdrlen: con 72+6;
Datahdrsize: con 4+1+3;
Resourcehdrsize: con 4+2+4;
# Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT"
Epochdelta: con 2082844800;
tzoff := 0;
init(): string
{
sys = load Sys Sys->PATH;
bufio = load Bufio Bufio->PATH;
daytime = load Daytime Daytime->PATH;
if(bufio == nil || daytime == nil)
return "can't load required module";
tzoff = daytime->local(0).tzoff;
return nil;
}
Eshort: con "file format error: too small";
Pfile.open(name: string, mode: int): (ref Pfile, string)
{
if(mode != Sys->OREAD)
return (nil, "invalid mode");
fd := sys->open(name, mode);
if(fd == nil)
return (nil, sys->sprint("%r"));
pf := mkpfile(name, mode);
(ok, d) := sys->fstat(fd);
if(ok < 0)
return (nil, sys->sprint("%r"));
length := int d.length;
if(length == 0)
return (nil, "empty file");
f := bufio->fopen(fd, mode); # automatically closed if open fails
p := array[Dbhdrlen] of byte;
if(f.read(p, Dbhdrlen) != Dbhdrlen)
return (nil, "invalid file header: too short");
ip := pf.info;
ip.name = gets(p[0:32]);
ip.attr = get2(p[32:]);
ip.version = get2(p[34:]);
ip.ctime = pilot2epoch(get4(p[36:]));
ip.mtime = pilot2epoch(get4(p[40:]));
ip.btime = pilot2epoch(get4(p[44:]));
ip.modno = get4(p[48:]);
ip.appinfo = get4(p[52:]);
ip.sortinfo = get4(p[56:]);
if(ip.appinfo < 0 || ip.sortinfo < 0 || (ip.appinfo|ip.sortinfo)&1)
return (nil, "invalid header: bad offset");
ip.dtype = xs(get4(p[60:]));
ip.creator = xs(get4(p[64:]));
pf.uidseed = ip.uidseed = get4(p[68:]);
if(get4(p[72:]) != 0)
return (nil, "chained headers not supported"); # Palm says to reject such files
nrec := get2(p[76:]);
if(nrec < 0)
return (nil, sys->sprint("invalid header: bad record count: %d", nrec));
esize := Datahdrsize;
if(ip.attr & Fresource)
esize = Resourcehdrsize;
dataoffset := length;
pf.entries = array[nrec] of ref Entry;
if(nrec > 0){
laste: ref Entry;
buf := array[esize] of byte;
for(i := 0; i < nrec; i++){
if(f.read(buf, len buf) != len buf)
return (nil, Eshort);
e := ref Entry;
if(ip.attr & Fresource){
# resource entry: type[4], id[2], offset[4]
e.name = get4(buf);
e.id = get2(buf[4:]);
e.offset = get4(buf[6:]);
e.attr = 0;
}else{
# record entry: offset[4], attr[1], id[3]
e.offset = get4(buf);
e.attr = int buf[4];
e.id = get3(buf[5:]);
e.name = 0;
}
if(laste != nil)
laste.size = e.offset - laste.offset;
laste = e;
pf.entries[i] = e;
}
if(laste != nil)
laste.size = length - laste.offset;
dataoffset = pf.entries[0].offset;
}else{
if(f.read(p, 2) != 2)
return (nil, Eshort); # discard placeholder bytes
}
n := 0;
if(ip.appinfo > 0){
n = ip.appinfo - int f.offset();
while(--n >= 0)
f.getb();
if(ip.sortinfo)
n = ip.sortinfo - ip.appinfo;
else
n = dataoffset - ip.appinfo;
pf.appinfo = array[n] of byte;
if(f.read(pf.appinfo, n) != n)
return (nil, Eshort);
}
if(ip.sortinfo > 0){
n = ip.sortinfo - int f.offset();
while(--n >= 0)
f.getb();
n = (dataoffset-ip.sortinfo)/2;
pf.sortinfo = array[n] of int;
tmp := array[2*n] of byte;
if(f.read(tmp, len tmp) != len tmp)
return (nil, Eshort);
for(i := 0; i < n; i++)
pf.sortinfo[i] = get2(tmp[2*i:]);
}
pf.f = f; # safe to save open file reference
return (pf, nil);
}
Pfile.close(pf: self ref Pfile): int
{
if(pf.f != nil){
pf.f.close();
pf.f = nil;
}
return 0;
}
Pfile.stat(pf: self ref Pfile): ref DBInfo
{
return ref *pf.info;
}
Pfile.read(pf: self ref Pfile, i: int): (ref Record, string)
{
if(i < 0 || i >= len pf.entries){
if(i == len pf.entries)
return (nil, nil); # treat as end-of-file
return (nil, "index out of range");
}
e := pf.entries[i];
r := ref Record;
r.index = i;
nb := e.size;
r.data = array[nb] of byte;
pf.f.seek(big e.offset, 0);
if(pf.f.read(r.data, nb) != nb)
return (nil, sys->sprint("%r"));
r.cat = e.attr & 16r0F;
r.attr = e.attr & 16rF0;
r.id = e.id;
r.name = e.name;
return (r, nil);
}
#Pfile.create(name: string, info: ref DBInfo): ref Pfile
#{
#}
#Pfile.wstat(pf: self ref Pfile, ip: ref DBInfo): string
#{
# if(pf.mode != Sys->OWRITE)
# return "not open for writing";
# if((ip.attr & Fresource) != (pf.info.attr & Fresource))
# return "cannot change file type";
# # copy only a subset
# pf.info.name = ip.name;
# pf.info.attr = ip.attr;
# pf.info.version = ip.version;
# pf.info.ctime = ip.ctime;
# pf.info.mtime = ip.mtime;
# pf.info.btime = ip.btime;
# pf.info.modno = ip.modno;
# pf.info.dtype = ip.dtype;
# pf.info.creator = ip.creator;
# return nil;
#}
#Pfile.setappinfo(pf: self ref Pfile, data: array of byte): string
#{
# if(pf.mode != Sys->OWRITE)
# return "not open for writing";
# pf.appinfo = array[len data] of byte;
# pf.appinfo[0:] = data;
#}
#Pfile.setsortinfo(pf: self ref Pfile, sort: array of int): string
#{
# if(pf.mode != Sys->OWRITE)
# return "not open for writing";
# pf.sortinfo = array[len sort] of int;
# pf.sortinfo[0:] = sort;
#}
#
# internal function to extend entry list if necessary, and return a
# pointer to the next available slot
#
entryensure(pf: ref Pfile, i: int): ref Entry
{
if(i < len pf.entries)
return pf.entries[i];
e := ref Entry(0, -1, 0, 0, 0);
n := len pf.entries;
if(n == 0)
n = 64;
else
n = (i+63) & ~63;
a := array[n] of ref Entry;
a[0:] = pf.entries;
a[i] = e;
pf.entries = a;
return e;
}
writefilehdr(pf: ref Pfile, mode: int, perm: int): string
{
if(len pf.entries >= 64*1024)
return "too many records for Palm file"; # is there a way to extend it?
if((f := bufio->create(pf.fname, mode, perm)) == nil)
return sys->sprint("%r");
ip := pf.info;
esize := Datahdrsize;
if(ip.attr & Fresource)
esize = Resourcehdrsize;
offset := Dbhdrlen + esize*len pf.entries + 2;
offset += 2; # placeholder bytes or gap bytes
ip.appinfo = 0;
if(len pf.appinfo > 0){
ip.appinfo = offset;
offset += len pf.appinfo;
}
ip.sortinfo = 0;
if(len pf.sortinfo > 0){
ip.sortinfo = offset;
offset += 2*len pf.sortinfo; # 2-byte entries
}
p := array[Dbhdrlen] of byte; # bigger than any entry as well
puts(p[0:32], ip.name);
put2(p[32:], ip.attr);
put2(p[34:], ip.version);
put4(p[36:], epoch2pilot(ip.ctime));
put4(p[40:], epoch2pilot(ip.mtime));
put4(p[44:], epoch2pilot(ip.btime));
put4(p[48:], ip.modno);
put4(p[52:], ip.appinfo);
put4(p[56:], ip.sortinfo);
put4(p[60:], sx(ip.dtype));
put4(p[64:], sx(ip.creator));
put4(p[68:], pf.uidseed);
put4(p[72:], 0); # next record list ID
put2(p[76:], len pf.entries);
if(f.write(p, Dbhdrlen) != Dbhdrlen)
return ewrite(f);
if(len pf.entries > 0){
for(i := 0; i < len pf.entries; i++) {
e := pf.entries[i];
e.offset = offset;
if(ip.attr & Fresource) {
put4(p, e.name);
put2(p[4:], e.id);
put4(p[6:], e.offset);
} else {
put4(p, e.offset);
p[4] = byte e.attr;
put3(p[5:], e.id);
}
if(f.write(p, esize) != esize)
return ewrite(f);
offset += e.size;
}
}
f.putb(byte 0); # placeholder bytes (figure 1.4) or gap bytes (p. 15)
f.putb(byte 0);
if(ip.appinfo != 0){
if(f.write(pf.appinfo, len pf.appinfo) != len pf.appinfo)
return ewrite(f);
}
if(ip.sortinfo != 0){
tmp := array[2*len pf.sortinfo] of byte;
for(i := 0; i < len pf.sortinfo; i++)
put2(tmp[2*i:], pf.sortinfo[i]);
if(f.write(tmp, len tmp) != len tmp)
return ewrite(f);
}
if(f.flush() != 0)
return ewrite(f);
return nil;
}
ewrite(f: ref Iobuf): string
{
e := sys->sprint("write error: %r");
f.close();
return e;
}
Doc.open(file: ref Pfile): (ref Doc, string)
{
if(file.info.dtype != "TEXt" || file.info.creator != "REAd")
return (nil, "not a Doc file: wrong type or creator");
(r, err) := file.read(0);
if(r == nil){
if(err == nil)
err = "no directory record";
return (nil, sys->sprint("not a valid Doc file: %s", err));
}
a := r.data;
if(len a < 16)
return (nil, sys->sprint("not a valid Doc file: bad length: %d", len a));
maxrec := len file.entries-1;
d := ref Doc;
d.file = file;
d.version = get2(a);
if(d.version != 1 && d.version != 2)
err = "unknown Docfile version";
# a[2:] is spare
d.length = get4(a[4:]);
d.nrec = get2(a[8:]);
if(maxrec >= 0 && d.nrec > maxrec){
d.nrec = maxrec;
err = "invalid record count";
}
d.recsize = get2(a[10:]);
d.position = get4(a[12:]);
return (d, sys->sprint("unexpected Doc file format: %s", err));
}
Doc.iscompressed(d: self ref Doc): int
{
return (d.version&7) == 2; # high-order bits are sometimes used, ignore them
}
Doc.read(doc: self ref Doc, index: int): (string, string)
{
(r, err) := doc.file.read(index+1);
if(r == nil)
return (nil, err);
(s, serr) := doc.unpacktext(r.data);
if(s == nil)
return (nil, serr);
return (s, nil);
}
Doc.unpacktext(doc: self ref Doc, a: array of byte): (string, string)
{
nb := len a;
s: string;
if(!doc.iscompressed()){
for(i := 0; i < nb; i++)
s[len s] = int a[i]; # assumes Latin-1
return (s, nil);
}
o := 0;
for(i := 0; i < nb;){
c := int a[i++];
if(c >= 9 && c <= 16r7F || c == 0)
s[o++] = c;
else if(c >= 1 && c <= 8){
if(i+c > nb)
return (nil, "missing data in record");
while(--c >= 0)
s[o++] = int a[i++];
}else if(c >= 16rC0 && c <= 16rFF){
s[o] = ' ';
s[o+1] = c & 16r7F;
o += 2;
}else{ # c >= 0x80 && c <= 16rBF
v := int a[i++];
m := ((c & 16r3F)<<5)|(v>>3);
n := (v&7) + 3;
if(m == 0 || m > o)
return (nil, sys->sprint("data is corrupt: m=%d n=%d o=%d", m, n, o));
for(; --n >= 0; o++)
s[o] = s[o-m];
}
}
return (s, nil);
}
Doc.textlength(doc: self ref Doc, a: array of byte): int
{
nb := len a;
if(!doc.iscompressed())
return nb;
o := 0;
for(i := 0; i < nb;){
c := int a[i++];
if(c >= 9 && c <= 16r7F || c == 0)
o++;
else if(c >= 1 && c <= 8){
if(i+c > nb)
return -1;
o += c;
i += c;
}else if(c >= 16rC0 && c <= 16rFF){
o += 2;
}else{ # c >= 0x80 && c <= 16rBF
v := int a[i++];
m := ((c & 16r3F)<<5)|(v>>3);
n := (v&7) + 3;
if(m == 0 || m > o)
return -1;
o += n;
}
}
return o;
}
xs(i: int): string
{
if(i == 0)
return "";
if(i & int 16r80808080)
return sys->sprint("%8.8ux", i);
return sys->sprint("%c%c%c%c", (i>>24)&16rFF, (i>>16)&16rFF, (i>>8)&16rFF, i&16rFF);
}
sx(s: string): int
{
n := 0;
for(i := 0; i < 4; i++){
c := 0;
if(i < len s)
c = s[i] & 16rFF;
n = (n<<8) | c;
}
return n;
}
mkpfile(name: string, mode: int): ref Pfile
{
pf := ref Pfile;
pf.mode = mode;
pf.fname = name;
pf.appinfo = array[0] of byte; # making it non-nil saves having to check each access
pf.sortinfo = array[0] of int;
pf.uidseed = 0;
pf.info = DBInfo.new(name, 0, nil, 0, nil);
return pf;
}
DBInfo.new(name: string, attr: int, dtype: string, version: int, creator: string): ref DBInfo
{
info := ref DBInfo;
info.name = name;
info.attr = attr;
info.version = version;
info.ctime = daytime->now();
info.mtime = daytime->now();
info.btime = 0;
info.modno = 0;
info.appinfo = 0;
info.sortinfo = 0;
info.dtype = dtype;
info.creator = creator;
info.uidseed = 0;
info.index = 0;
info.more = 0;
return info;
}
Categories.new(labels: array of string): ref Categories
{
c := ref Categories;
c.renamed = 0;
c.lastuid = 0;
c.labels = array[16] of string;
c.uids = array[] of {0 to 15 => 0};
for(i := 0; i < len labels && i < 16; i++){
c.labels[i] = labels[i];
c.lastuid = 16r80 + i;
c.uids[i] = c.lastuid;
}
return c;
}
Categories.unpack(a: array of byte): ref Categories
{
if(len a < 16r114)
return nil; # doesn't match the structure
c := ref Categories;
c.renamed = get2(a);
c.labels = array[16] of string;
c.uids = array[16] of int;
j := 2;
for(i := 0; i < 16; i++){
c.labels[i] = latin1(a[j:j+16], 0);
j += 16;
c.uids[i] = int a[16r102+i];
}
c.lastuid = int a[16r112];
# one byte of padding is shown on p. 26, but
# two more are invariably used in practice
# before application specific data.
if(len a > 16r116)
c.appdata = a[16r116:];
return c;
}
Categories.pack(c: self ref Categories): array of byte
{
a := array[16r116 + len c.appdata] of byte;
put2(a, c.renamed);
j := 2;
for(i := 0; i < 16; i++){
puts(a[j:j+16], c.labels[i]);
j += 16;
a[16r102+i] = byte c.uids[i];
}
a[16r112] = byte c.lastuid;
a[16r113] = byte 0; # pad shown on p. 26
a[16r114] = byte 0; # extra two bytes of padding used in practice
a[16r115] = byte 0;
if(c.appdata != nil)
a[16r116:] = c.appdata;
return a;
}
Categories.mkidmap(c: self ref Categories): array of int
{
a := array[256] of {* => 0};
for(i := 0; i < len c.uids; i++)
a[c.uids[i]] = i;
return a;
}
#
# because PalmOS treats all times as local times, and doesn't associate
# them with time zones, we'll convert using local time on Plan 9 and Inferno
#
pilot2epoch(t: int): int
{
if(t == 0)
return 0; # we'll assume it's not set
return t - Epochdelta + tzoff;
}
epoch2pilot(t: int): int
{
if(t == 0)
return t;
return t - tzoff + Epochdelta;
}
#
# map Palm name to string, assuming iso-8859-1,
# but remap space and /
#
latin1(a: array of byte, remap: int): string
{
s := "";
for(i := 0; i < len a; i++){
c := int a[i];
if(c == 0)
break;
if(remap){
if(c == ' ')
c = 16r00A0; # unpaddable space
else if(c == '/')
c = 16r2215; # division /
}
s[len s] = c;
}
return s;
}
#
# map from Unicode to Palm name
#
filename(name: string): string
{
s := "";
for(i := 0; i < len name; i++){
c := name[i];
if(c == ' ')
c = 16r00A0; # unpaddable space
else if(c == '/')
c = 16r2215; # division solidus
s[len s] = c;
}
return s;
}
dbname(name: string): string
{
s := "";
for(i := 0; i < len name; i++){
c := name[i];
case c {
0 => c = ' '; # unlikely, but just in case
16r2215 => c = '/';
16r00A0 => c = ' ';
}
s[len s] = c;
}
return s;
}
#
# string conversion: can't use (string a) because
# the bytes are Latin1, not Unicode
#
gets(a: array of byte): string
{
s := "";
for(i := 0; i < len a; i++)
s[len s] = int a[i];
return s;
}
puts(a: array of byte, s: string)
{
for(i := 0; i < len a-1 && i < len s; i++)
a[i] = byte s[i];
for(; i < len a; i++)
a[i] = byte 0;
}
#
# big-endian packing
#
get4(p: array of byte): int
{
return (((((int p[0] << 8) | int p[1]) << 8) | int p[2]) << 8) | int p[3];
}
get3(p: array of byte): int
{
return (((int p[0] << 8) | int p[1]) << 8) | int p[2];
}
get2(p: array of byte): int
{
return (int p[0]<<8) | int p[1];
}
put4(p: array of byte, v: int)
{
p[0] = byte (v>>24);
p[1] = byte (v>>16);
p[2] = byte (v>>8);
p[3] = byte (v & 16rFF);
}
put3(p: array of byte, v: int)
{
p[0] = byte (v>>16);
p[1] = byte (v>>8);
p[2] = byte (v & 16rFF);
}
put2(p: array of byte, v: int)
{
p[0] = byte (v>>8);
p[1] = byte (v & 16rFF);
}
|