-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte_formatter.go
More file actions
231 lines (214 loc) · 5.12 KB
/
byte_formatter.go
File metadata and controls
231 lines (214 loc) · 5.12 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
231
/*
* Copyright 2022-2026 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package services
import (
"bytes"
"fmt"
"os"
"golang.org/x/text/encoding/charmap"
)
const maximumFormatLength = 4096
// FormatByteBuffer formats the byte array to an output with a hexadecimal part, a ASCII part and
// a EBCDIC converted part of the same data
func FormatByteBuffer(header string, b []byte) string {
nRows := 16
var buffer bytes.Buffer
buffer.WriteString(header)
buffer.WriteString(":")
buffer.WriteString(fmt.Sprintf(" Dump len=%d(0x%x)", len(b), len(b)))
var x [16][2]byte
r := 0
newLine := 0
nrLine := 0
rlen := len(b)
byteIndex := 0
for byteIndex < rlen {
// for (int i = 0; i < rlen; nrLine++) {
fi := byteIndex
ti := byteIndex + nRows
// if (ti >= rlen) {
// ti = rlen - 1;
// }
isEqual := false
if nrLine > 0 {
isEqual = true
for j := 0; j < nRows; j++ {
if byteIndex < rlen {
x[j][r] = b[byteIndex]
if x[j][r] != x[j][(r+1)%2] {
isEqual = false
}
} else {
x[j][r] = 0
}
byteIndex++
}
}
if isEqual {
newLine++
} else {
newLine = 0
}
if ti >= rlen {
newLine = 0
}
if (nrLine > 0) && (newLine > 0) {
if newLine == 1 {
buffer.WriteString(fmt.Sprintf("\n%04x ... ", fi))
}
} else {
byteIndex = fi
for j := 0; j < nRows; j++ {
if byteIndex%nRows == 0 {
buffer.WriteString(fmt.Sprintf("\n%04x %02x",
byteIndex, b[byteIndex]))
} else {
if byteIndex < rlen {
buffer.WriteString(fmt.Sprintf("%02x", b[byteIndex]))
} else {
buffer.WriteString(" ")
}
}
if (byteIndex-1)%2 == 0 {
buffer.WriteString(" ")
}
byteIndex++
}
byteIndex = fi
buffer.WriteString(" ")
for j := 0; j < nRows; j++ {
if byteIndex < rlen {
if b[byteIndex] > 31 {
buffer.WriteString(fmt.Sprintf("%c", b[byteIndex]))
} else {
buffer.WriteString(".")
}
} else {
buffer.WriteString(" ")
}
byteIndex++
}
byteIndex = fi
buffer.WriteString(" ")
for j := 0; j < nRows; j++ {
if byteIndex < rlen {
a := convertToASCII(b[byteIndex])
if a > 31 {
buffer.WriteString(fmt.Sprintf("%c", a))
} else {
buffer.WriteString(".")
}
}
byteIndex++
}
r = (r + 1) % 2
}
byteIndex = ti
// if (!noLimit) && (nrLine > MAX_LINES) {
// buffer.WriteString("\n------- rest skipped -----")
// break
// }
nrLine++
}
buffer.WriteString("\n")
return buffer.String()
}
func convertToASCII(b byte) byte {
dec := charmap.CodePage037.NewDecoder()
bebcdic := []byte{b}
bascii := make([]byte, 1)
_, _, err := dec.Transform(bascii, bebcdic, false)
if err != nil {
return 0
}
return bascii[0]
}
// FormatBytes formats a given byte array and modulo space operator. The modulo space defines the
// the possition a space is added to the output. The maximum give the maximum characters per line.
// This function enhance the display with showing the length if showLength is set to true
func FormatBytes(header string, b []byte, bufferLength int, modSpace int, max int, showLength bool) string {
var buffer bytes.Buffer
buffer.WriteString(header)
formatLength := bufferLength
if os.Getenv("DUMP_BIG") == "" && formatLength > maximumFormatLength {
formatLength = maximumFormatLength
}
if showLength {
buffer.WriteString(fmt.Sprintf(" length=%d", bufferLength))
}
if max != -1 {
buffer.WriteString("\n")
}
lineCr := max
var lastLine []byte
if max < 1 {
lineCr = formatLength
lastLine = make([]byte, 0)
} else {
lastLine = make([]byte, max)
}
noticed := true
for offset := 0; offset < formatLength; offset += lineCr {
if offset+lineCr < formatLength {
if max > 0 && offset > 0 && offset+lineCr < formatLength && bytes.Equal(lastLine, b[offset:offset+lineCr]) {
if noticed {
buffer.WriteString(fmt.Sprintf("%04X skipped equal lines\n", offset))
}
noticed = false
continue
}
noticed = true
if max > 0 {
copy(lastLine, b[offset:offset+lineCr])
}
}
for j := 0; j < lineCr; j++ {
if max > -1 && j == 0 {
buffer.WriteString(fmt.Sprintf("%04X ", offset+j))
}
if formatLength > (offset + j) {
buffer.WriteString(fmt.Sprintf("%02X", b[offset+j]))
} else {
buffer.WriteString(" ")
}
if modSpace > 0 && (j+1)%modSpace == 0 {
buffer.WriteString(" ")
}
}
buffer.WriteString(" [")
for j := 0; j < lineCr; j++ {
if formatLength > (offset + j) {
if b[offset+j] > 31 {
buffer.WriteString(fmt.Sprintf("%c", b[offset+j]))
} else {
buffer.WriteString(".")
}
} else {
buffer.WriteString(" ")
}
}
buffer.WriteString("] [")
for j := 0; j < lineCr; j++ {
if formatLength > (offset + j) {
a := convertToASCII(b[offset+j])
if a > 31 {
buffer.WriteString(fmt.Sprintf("%c", a))
} else {
buffer.WriteString(".")
}
} else {
buffer.WriteString(" ")
}
}
buffer.WriteString("]\n")
}
return buffer.String()
}