nx_box_lib.nx source
↩ module page · 107 lines · 4168 B
1// nx_box_lib.nx -- Nishi Box (F503): matter-scoped LEGAL DMS on the immutable CID plane.
2// Adds the LEGAL layer ON TOP of the existing /office doc suite (nx_docx/xlsx/pptx storage):
3// matter-scoping, privilege/work-product tags, PRIVILEGE LOG generation (e-discovery), and
4// BATES numbering. Versioned by construction (CID plane). DRY: reuses nx_matter_lib helpers
5// (mt_field/mt_streq/mt_catcopy + reg_*/canon_encode). LIB (no main). license_tier: ORIGINAL
6
7import "nx_matter_lib.nx"
8const K_MAGIC_100000: i64 = 100000
9const K_MAGIC_2048: i64 = 2048
10
11// Bates: "<PREFIX>-000001" (zero-padded 6 digits, per-matter sequential).
12func box_bates(prefix: *u8, seq: i64, out: *u8) -> i64 {
13 var o: i64 = mt_catcopy(out, 0, prefix)
14 out[o] = 45 as u8
15 o = o + 1
16 var d: i64 = K_MAGIC_100000
17 while d >= 1 {
18 out[o] = (48 + ((seq / d) % 10)) as u8
19 o = o + 1
20 d = d / 10
21 }
22 out[o] = 0 as u8
23 return o
24}
25
26// put a document scoped to a matter, with privilege tag + Bates. Key = "<matter>:<docid>".
27func box_doc_put(prefix: *u8, matter: *u8, docid: *u8, title: *u8, cid: *u8, priv: *u8, bates: *u8) -> i64 {
28 let did: *u8 = sys_mmap(128)
29 var o: i64 = mt_catcopy(did, 0, matter)
30 did[o] = 58 as u8
31 o = o + 1
32 o = mt_catcopy(did, o, docid)
33 did[o] = 0 as u8
34 let k: *i64 = sys_mmap(8 * 5) as *i64
35 let v: *i64 = sys_mmap(8 * 5) as *i64
36 k[0] = ("matter" as *u8) as i64
37 v[0] = matter as i64
38 k[1] = ("title" as *u8) as i64
39 v[1] = title as i64
40 k[2] = ("cid" as *u8) as i64
41 v[2] = cid as i64
42 k[3] = ("privilege" as *u8) as i64
43 v[3] = priv as i64
44 k[4] = ("bates" as *u8) as i64
45 v[4] = bates as i64
46 let rec: *u8 = sys_mmap(K_MAGIC_2048)
47 let rl: i64 = canon_encode(k, v, 5, rec)
48 return reg_put(prefix, "doc:" as *u8, "doc:__idx__" as *u8, did, rec, rl)
49}
50
51// scan a matter's documents: returns total; priv_out[0]=privileged count; batesbuf gets the
52// PRIVILEGE LOG (newline-sep Bates numbers of privileged docs = the e-discovery deliverable).
53func box_matter_scan(prefix: *u8, matter: *u8, priv_out: *i64, batesbuf: *u8) -> i64 {
54 // SIZE-TO-NEED: buffer derived from the index itself. The pair this replaces
55 // returned -1 once the doc index passed 1 MiB, and -1 skips the walk below --
56 // a document scan would have reported an EMPTY box on a large matter.
57 let idxbox: *i64 = sys_mmap(16) as *i64
58 let ilen: i64 = reg_index_read(prefix, "doc:__idx__" as *u8, idxbox)
59 let idx: *u8 = idxbox[0] as *u8
60 let mp: *u8 = sys_mmap(128)
61 var mpl: i64 = mt_catcopy(mp, 0, matter)
62 mp[mpl] = 58 as u8
63 mpl = mpl + 1
64 mp[mpl] = 0 as u8
65 let idb: *u8 = sys_mmap(256)
66 let po: *i64 = sys_mmap(16) as *i64
67 let lo: *i64 = sys_mmap(16) as *i64
68 let pf: *u8 = sys_mmap(128)
69 var total: i64 = 0
70 var npriv: i64 = 0
71 var bo: i64 = 0
72 var ls: i64 = 0
73 var i: i64 = 0
74 while i <= ilen {
75 var eol: i64 = 0
76 if i == ilen { eol = 1 }
77 if i < ilen { if idx[i] == 10 as u8 { eol = 1 } }
78 if eol == 1 {
79 if i > ls {
80 var c: i64 = 0
81 while ls + c < i { idb[c] = idx[ls + c]; c = c + 1 }
82 idb[c] = 0 as u8
83 var mtch: i64 = 1
84 var x: i64 = 0
85 while mp[x] != (0 as u8) { if idb[x] != mp[x] { mtch = 0 } x = x + 1 }
86 if mtch == 1 {
87 if reg_get(prefix, "doc:" as *u8, idb, po, lo) == 1 {
88 total = total + 1
89 mt_field(po[0] as *u8, lo[0], "privilege" as *u8, 9, pf)
90 if mt_streq(pf, "yes" as *u8) == 1 {
91 npriv = npriv + 1
92 mt_field(po[0] as *u8, lo[0], "bates" as *u8, 5, pf)
93 bo = mt_catcopy(batesbuf, bo, pf)
94 batesbuf[bo] = 10 as u8
95 bo = bo + 1
96 }
97 }
98 }
99 }
100 ls = i + 1
101 }
102 i = i + 1
103 }
104 batesbuf[bo] = 0 as u8
105 priv_out[0] = npriv
106 return total
107}