Repository navigation
Expand file tree
/
Copy pathmake_sample.py
More file actions
executable file
·230 lines (202 loc) · 9.28 KB
/
Copy pathmake_sample.py
File metadata and controls
executable file
·230 lines (202 loc) · 9.28 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
#!/usr/bin/env python3
"""
Generate a small synthetic X9.100-187 file so x9view can be exercised without
production data. Writes EBCDIC, variable-length records, with two hand-built
TIFF images per item (front and back).
python3 make_sample.py sample.x937
"""
import struct
import sys
EBCDIC = "cp037"
def fld(value, length, numeric=False):
"""Pad a field to its fixed width: numerics right-justified zero-filled,
alphanumerics left-justified blank-filled, as X9 requires."""
text = str(value)
if numeric:
return text[-length:].rjust(length, "0")
return text[:length].ljust(length)
def rec(*parts):
"""Encode a text record to EBCDIC and prefix it with its 4-byte length."""
body = "".join(parts).encode(EBCDIC)
return struct.pack(">I", len(body)) + body
def binary_rec(prefix_text, image):
body = prefix_text.encode(EBCDIC) + image
return struct.pack(">I", len(body)) + body
def tiny_tiff(width=96, height=40, stripe=8):
"""A 1-bit uncompressed TIFF with horizontal stripes.
Real X9 files carry CCITT Group 4 images; uncompressed keeps this generator
dependency-free while still producing a file that `sips` and Preview read.
"""
row_bytes = (width + 7) // 8
pixels = bytearray()
for y in range(height):
# Photometric is WhiteIsZero, so 0xFF is a black run.
fill = 0xFF if (y // stripe) % 2 else 0x00
pixels.extend(bytes([fill]) * row_bytes)
header_size = 8
entries = [
(256, 3, 1, width), # ImageWidth
(257, 3, 1, height), # ImageLength
(258, 3, 1, 1), # BitsPerSample
(259, 3, 1, 1), # Compression: none
(262, 3, 1, 0), # PhotometricInterpretation: WhiteIsZero
(273, 4, 1, None), # StripOffsets (patched below)
(277, 3, 1, 1), # SamplesPerPixel
(278, 4, 1, height), # RowsPerStrip
(279, 4, 1, len(pixels)), # StripByteCounts
(282, 5, 1, None), # XResolution (patched below)
(283, 5, 1, None), # YResolution (patched below)
(296, 3, 1, 2), # ResolutionUnit: inch
]
ifd_size = 2 + 12 * len(entries) + 4
xres_off = header_size + ifd_size
yres_off = xres_off + 8
data_off = yres_off + 8
patched = []
for tag, typ, count, value in entries:
if tag == 273:
value = data_off
elif tag == 282:
value = xres_off
elif tag == 283:
value = yres_off
patched.append((tag, typ, count, value))
out = bytearray(b"II" + struct.pack("<HI", 42, header_size))
out += struct.pack("<H", len(patched))
for tag, typ, count, value in patched:
out += struct.pack("<HHI", tag, typ, count)
if typ == 3: # SHORT occupies the first two bytes of the value field
out += struct.pack("<HH", value, 0)
else:
out += struct.pack("<I", value)
out += struct.pack("<I", 0) # no next IFD
out += struct.pack("<II", 200, 1) # XResolution 200 dpi
out += struct.pack("<II", 200, 1) # YResolution 200 dpi
out += pixels
return bytes(out)
def image_view_detail(side):
return rec(
"50",
fld("1", 1), # image indicator: image present
fld("061000227", 9), # image creator routing number
fld("20260805", 8), # image creator date
fld("00", 2), # format indicator: TIFF
fld("00", 2), # compression: G4
fld(0, 7, numeric=True), # image view data size (0 = not used)
fld(side, 1), # 0 front, 1 back
fld("00", 2), # view descriptor: full view
fld("0", 1), # digital signature indicator: none
fld("00", 2),
fld(0, 5, numeric=True),
fld(0, 7, numeric=True),
fld(0, 7, numeric=True),
fld("0", 1),
fld("", 8),
fld("", 15),
)
def image_view_data(seq, image):
prefix = "".join([
"52",
fld("061000227", 9),
fld("20260805", 8),
fld("01", 2),
fld(seq, 15, numeric=True),
fld("", 16), # security originator name
fld("", 16), # security authenticator name
fld("", 16), # security key name
fld("0", 1), # clipping origin: no clipping
fld(0, 4, numeric=True),
fld(0, 4, numeric=True),
fld(0, 4, numeric=True),
fld(0, 4, numeric=True),
fld(0, 4, numeric=True), # length of image reference key: 0
fld(0, 5, numeric=True), # length of digital signature: 0
fld(len(image), 7, numeric=True), # length of image data
])
return binary_rec(prefix, image)
def build(path):
items = [
("000000000000001", 12550, "121000248", "1234567890 ", "ALEX MORGAN"),
("000000000000002", 899900, "021000021", "9876543210 ", "ACME SUPPLY CO"),
("000000000000003", 4500, "061000227", "5555000123 ", "JANE DOE"),
]
total = sum(amount for _, amount, _, _, _ in items)
out = [
rec("01",
fld("03", 2), fld("T", 1), # standard level, test file
fld("061000227", 9), fld("121000248", 9),
fld("20260805", 8), fld("1430", 4), fld("0", 1),
fld("DEST BANK NA", 18), fld("ORIGIN BANK NA", 18),
fld("A", 1), fld("US", 2), fld("", 4)),
rec("10",
fld("01", 2), fld("061000227", 9), fld("121000248", 9),
fld("20260805", 8), fld("20260805", 8), fld("1430", 4),
fld("I", 1), # record type indicator: images
fld("G", 1), # documentation type: image
fld("CL000001", 8), fld("OPS DESK", 14), fld("5551234567", 10),
fld("", 1), fld("", 1), fld("", 2)),
rec("20",
fld("01", 2), fld("061000227", 9), fld("121000248", 9),
fld("20260805", 8), fld("20260805", 8), fld("BUNDLE0001", 10),
fld(1, 4, numeric=True), fld("01", 2), fld("", 9), fld("", 5),
fld("", 12)),
]
for seq, amount, routing, on_us, payee in items:
out.append(rec("25",
fld("", 15), # auxiliary on-us
fld("", 1), # external processing code
fld(routing[:8], 8), fld(routing[8], 1),
fld(on_us, 20), fld(amount, 10, numeric=True),
fld(seq, 15, numeric=True),
fld("G", 1), fld("1", 1), fld("1", 1), fld("1", 1),
fld(1, 2, numeric=True), fld("0", 1), fld("A", 1)))
out.append(rec("26",
fld("1", 1), fld("061000227", 9), fld("20260805", 8),
fld(seq, 15, numeric=True), fld("000123456789", 18),
fld("00042", 5), fld(payee, 15),
fld("Y", 1), fld("0", 1), fld("0", 1), fld("", 1), fld("", 3)))
for side in ("0", "1"):
image = tiny_tiff(stripe=6 if side == "0" else 10)
out.append(image_view_detail(side))
out.append(image_view_data(seq, image))
# One returned item, so the type 31 layout gets exercised too.
ret_seq, ret_amount = "000000000000004", 32100
out.append(rec("31",
fld("12100024", 8), fld("8", 1),
fld("4444555566", 20), fld(ret_amount, 10, numeric=True),
fld("A", 1), # return reason: NSF
fld(0, 2, numeric=True), fld("G", 1), fld("20260805", 8),
fld(ret_seq, 15, numeric=True),
fld("", 1), fld("0", 1), fld("A", 1), fld("1", 1), fld("", 8)))
for side in ("0", "1"):
image = tiny_tiff(stripe=4 if side == "0" else 12)
out.append(image_view_detail(side))
out.append(image_view_data(ret_seq, image))
total += ret_amount
out.append(rec("70",
fld(len(items) + 1, 4, numeric=True),
fld(total, 12, numeric=True),
fld(total, 12, numeric=True),
fld((len(items) + 1) * 2, 5, numeric=True),
fld("", 20), fld("", 25)))
out.append(rec("90",
fld(1, 6, numeric=True),
fld(len(items) + 1, 8, numeric=True),
fld(total, 14, numeric=True),
fld((len(items) + 1) * 2, 9, numeric=True),
fld("ORIGIN BANK NA", 18), fld("20260806", 8),
fld("0", 1), fld("", 14)))
body = b"".join(out)
record_count = len(out) + 1
body += rec("99",
fld(1, 6, numeric=True),
fld(record_count, 8, numeric=True),
fld(len(items) + 1, 8, numeric=True),
fld(total, 16, numeric=True),
fld("OPS DESK", 14), fld("5551234567", 10),
fld("0", 1), fld("", 15))
with open(path, "wb") as fh:
fh.write(body)
print("wrote %s (%d bytes, %d records)" % (path, len(body), record_count))
if __name__ == "__main__":
build(sys.argv[1] if len(sys.argv) > 1 else "sample.x937")