-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_handler.v
More file actions
187 lines (166 loc) · 5.22 KB
/
json_handler.v
File metadata and controls
187 lines (166 loc) · 5.22 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
module main
import json
import crypto.sha256
import crypto.sha512
fn encode_pf_to_json(config Config) ! {
content := read_input_content(config.input)!
metadata := if config.input == '-' {
generate_stdin_metadata(content)
} else {
generate_file_metadata(config.input)!
}
if config.check {
if config.json_output {
response := JsonResponse{
success: true
message: 'Syntax check passed'
}
println(json.encode(response))
} else {
println('Syntax check: OK')
}
return
}
// Use line-by-line parsing as primary method
parsed_lines := parse_pf_conf_lines(content)!
pf_json := PfJsonOutput{
metadata: metadata
raw_text: '' // Usually empty since we have line-by-line structure
lines: parsed_lines
}
json_output := json.encode_pretty(pf_json)
if config.dry_run {
println('Dry run - would output:')
println(json_output)
return
}
if config.output != '' {
check_output_file_safety(config.output, config.force)!
write_file_with_permissions(config.output, json_output)!
if config.json_output {
response := JsonResponse{
success: true
message: 'File encoded successfully'
data: {
'output_file': config.output
'input_file': if config.input == '-' { '<stdin>' } else { config.input }
}
}
println(json.encode(response))
} else {
println('Encoded to: ${config.output}')
}
} else {
println(json_output)
}
}
fn decode_json_to_pf(config Config) ! {
json_content := read_input_content(config.input)!
pf_json := json.decode(PfJsonOutput, json_content)!
if config.check {
if config.json_output {
response := JsonResponse{
success: true
message: 'JSON syntax check passed'
}
println(json.encode(response))
} else {
println('Syntax check: OK')
}
return
}
// Use line-by-line structure for perfect fidelity, fallback to raw_text
pf_output := if pf_json.lines.len > 0 {
generate_pf_conf_from_lines(pf_json.lines)!
} else if pf_json.raw_text != '' {
pf_json.raw_text
} else {
return error('No valid pf.conf data found in JSON (neither lines nor raw_text)')
}
// Calculate checksums of generated output
generated_sha256 := sha256.hexhash(pf_output)
generated_sha512 := sha512.hexhash(pf_output)
generated_size := pf_output.len
// Compare with stored metadata
checksum_match := generated_sha256 == pf_json.metadata.checksums.sha256 &&
generated_sha512 == pf_json.metadata.checksums.sha512 &&
generated_size == pf_json.metadata.filesize
if !checksum_match {
filename_info := if pf_json.metadata.filename != '' { ' (from ${pf_json.metadata.filename})' } else { '' }
if config.json_output {
response := JsonResponse{
success: false
message: 'Checksum verification failed${filename_info}'
data: {
'original_sha256': pf_json.metadata.checksums.sha256
'generated_sha256': generated_sha256
'original_sha512': pf_json.metadata.checksums.sha512
'generated_sha512': generated_sha512
'original_size': pf_json.metadata.filesize.str()
'generated_size': generated_size.str()
}
error: if config.verify { 'Strict verification mode - conversion not 100% faithful' } else { 'Round-trip conversion is not 100% faithful' }
}
if config.verify {
eprintln(json.encode(response))
return error('Checksum verification failed - output does not match original metadata${filename_info}')
} else {
eprintln(json.encode(response))
}
} else {
eprintln('Warning: Generated output checksums do not match original metadata${filename_info}')
eprintln('SHA256 - Original: ${pf_json.metadata.checksums.sha256}')
eprintln('SHA256 - Generated: ${generated_sha256}')
eprintln('SHA512 - Original: ${pf_json.metadata.checksums.sha512}')
eprintln('SHA512 - Generated: ${generated_sha512}')
eprintln('Size - Original: ${pf_json.metadata.filesize} bytes')
eprintln('Size - Generated: ${generated_size} bytes')
if !config.dry_run {
eprintln('Note: This indicates the round-trip conversion is not 100% faithful')
}
if config.verify {
return error('Checksum verification failed - output does not match original metadata${filename_info}')
}
}
} else {
filename_info := if pf_json.metadata.filename != '' { ' (from ${pf_json.metadata.filename})' } else { '' }
if config.json_output {
response := JsonResponse{
success: true
message: 'Checksum verification passed${filename_info}'
data: {
'sha256': generated_sha256
'sha512': generated_sha512
'size': generated_size.str()
}
}
eprintln(json.encode(response))
} else {
eprintln('✓ Checksum verification passed - output matches original${filename_info}')
}
}
if config.dry_run {
println('Dry run - would output:')
println(pf_output)
return
}
if config.output != '' {
check_output_file_safety(config.output, config.force)!
write_file_with_permissions(config.output, pf_output)!
if config.json_output {
response := JsonResponse{
success: true
message: 'File decoded successfully'
data: {
'output_file': config.output
'input_file': if config.input == '-' { '<stdin>' } else { config.input }
}
}
println(json.encode(response))
} else {
println('Decoded to: ${config.output}')
}
} else {
println(pf_output)
}
}