-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheprefquark.go
565 lines (460 loc) · 15 KB
/
eprefquark.go
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
package errpref
import (
"fmt"
"strings"
"sync"
)
// defaultErrorPrefixDisplayLineLength - Do NOT access
// defaultErrorPrefixDisplayLineLength without first locking
// 'defaultErrPrefLineLenLock'
var defaultErrorPrefixDisplayLineLength uint
var defaultErrPrefLineLenLock *sync.Mutex
type errPrefQuark struct {
lock *sync.Mutex
}
// convertNonPrintableChars - Receives a string containing
// non-printable characters and converts them to 'printable'
// characters returned in a string.
//
// Examples of non-printable characters are '\n', '\t' or 0x06
// (Acknowledge). These example characters would be translated into
// printable string characters as: "\\n", "\\t" and "[ACK]".
//
// Space characters are typically translated as " ". However, if
// the input parameter 'convertSpace' is set to 'true' then all
// spaces are converted to "[SPACE]" in the returned string.
//
// Reference:
// https://www.juniper.net/documentation/en_US/idp5.1/topics/reference/general/intrusion-detection-prevention-custom-attack-object-extended-ascii.html
//
//
// ------------------------------------------------------------------------
//
// Input Parameters
//
// nonPrintableChars []rune
// - An array of runes containing non-printable characters.
// The non-printable characters will be converted to
// printable characters.
//
// convertSpace bool
// - Space or white space characters (0x20) are by default
// translated as " ". However, if this parameter is set to
// 'true', space characters will be converted to "[SPACE]".
//
//
// ePrefix string
// - This is an error prefix which is included in all returned
// error messages. Usually, it contains the names of the calling
// method or methods. Note: Be sure to leave a space at the end
// of 'ePrefix'. This parameter is optional.
//
//
// ------------------------------------------------------------------------
//
// Return Values
//
// printableChars string
// - This returned string is identical to input parameter
// 'nonPrintableChars' with the exception that non-printable
// characters are translated into printable characters.
//
//
// err error
// - If this method completes successfully, the returned error
// Type is set equal to 'nil'. If errors are encountered during
// processing, the returned error Type will encapsulate an error
// message. Note that this error message will incorporate the
// method chain and text passed by input parameter, 'ePrefix'.
// The 'ePrefix' text will be prefixed to the beginning of the
// error message.
//
//
// ------------------------------------------------------------------------
//
// Example Usage
//
// testStr := "Hello world!\n"
// testRunes := []rune(testStr)
// ePrefix := "theCallingFunction()"
//
// ePrefQuark := errPrefQuark{}
//
// actualStr :=
// ePrefQuark.
// convertNonPrintableChars(
// testRunes,
// true,
// ePrefix)
//
// ----------------------------------------------------
// 'actualStr' is now equal to:
// "Hello[SPACE]world!\\n"
//
//
func (ePrefQuark *errPrefQuark) convertNonPrintableChars(
nonPrintableChars []rune,
convertSpace bool,
ePrefix string) (
printableChars string,
err error) {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
if len(ePrefix) > 0 {
ePrefix += "\n"
}
ePrefix += "errPrefQuark.convertNonPrintableChars() "
lenNonPrintableChars := len(nonPrintableChars)
if lenNonPrintableChars == 0 {
err = fmt.Errorf("%v\n"+
"Error: Input parameter 'nonPrintableChars' is invalid!\n"+
"'nonPrintableChars' is an empty or zero lenght rune array.\n",
ePrefix)
return "[EMPTY] Array!", err
}
var b strings.Builder
b.Grow(lenNonPrintableChars * 5)
for i := 0; i < lenNonPrintableChars; i++ {
cRune := nonPrintableChars[i]
switch cRune {
case 0:
b.WriteString("[NULL]") // 0x00 NULL
case 1:
b.WriteString("[SOH]") // 0x01 State of Heading
case 2:
b.WriteString("[STX]") // 0x02 State of Text
case 3:
b.WriteString("[ETX]") // 0x03 End of Text
case 4:
b.WriteString("[EOT]") // 0X04 End of Transmission
case 5:
b.WriteString("[ENQ]") // 0x05 Enquiry
case 6:
b.WriteString("[ACK]") // 0x06 Acknowledge
case '\a':
b.WriteString("\\a") // U+0007 alert or bell
case '\b':
b.WriteString("\\b") // U+0008 backspace
case '\t':
b.WriteString("\\t") // U+0009 horizontal tab
case '\n':
b.WriteString("\\n") // U+000A line feed or newline
case '\v':
b.WriteString("\\v") // U+000B vertical tab
case '\f':
b.WriteString("\\f") // U+000C form feed
case '\r':
b.WriteString("\\r") // U+000D carriage return
case 14:
b.WriteString("[SO]") // U+000E Shift Out
case 15:
b.WriteString("[SI]") // U+000F Shift In
case '\\':
b.WriteString("\\") // U+005c backslash
case ' ':
// 0X20 Space character
if convertSpace {
b.WriteString("[SPACE]")
} else {
b.WriteRune(' ')
}
default:
b.WriteRune(cRune)
}
}
return b.String(), err
}
// convertPrintableChars - Converts selected printable characters
// to their non-printable or native equivalent. For example,
// instances of '\\n' in a string will be converted to '\n'.
//
// Additional examples of converted printable string characters
// are: "\\n", "\\t" and "[ACK]". These printable characters be
// converted into their native, non-printable state: '\n', '\t' or
// 0x06 (Acknowledge).
//
//
// Reference:
// https://www.juniper.net/documentation/en_US/idp5.1/topics/reference/general/intrusion-detection-prevention-custom-attack-object-extended-ascii.html
//
//
// ------------------------------------------------------------------------
//
// Input Parameters
//
// printableChars string
// - A string which may contain non-printable characters converted
// to their printable equivalents. These printable characters will
// be converted back to their native, non-printable values.
//
//
// ePrefix string
// - This is an error prefix which is included in all returned
// error messages. Usually, it contains the names of the calling
// method or methods. Note: Be sure to leave a space at the end
// of 'ePrefix'. This parameter is optional.
//
//
// ------------------------------------------------------------------------
//
// Return Values
//
// nonPrintableChars []rune
// - An array of runes containing non-printable characters.
// The non-printable characters were be converted from the
// printable characters contained in input parameter
// 'printableChars'.
//
//
// err error
// - If this method completes successfully, the returned error
// Type is set equal to 'nil'. If errors are encountered during
// processing, the returned error Type will encapsulate an error
// message. Note that this error message will incorporate the
// method chain and text passed by input parameter, 'ePrefix'.
// The 'ePrefix' text will be prefixed to the beginning of the
// error message.
//
//
// ------------------------------------------------------------------------
//
// Example Usage
//
// testStr := "Hello[SPACE]world!\\n"
// ePrefix := "theCallingFunction()"
//
// ePrefQuark := errPrefQuark{}
//
// actualRuneArray :=
// ePrefQuark.
// convertPrintableChars(
// testStr,
// ePrefix)
//
// ----------------------------------------------------
// 'actualRuneArray' is now equal to:
// "Hello world!\n"
//
func (ePrefQuark *errPrefQuark) convertPrintableChars(
printableChars string,
ePrefix string) (
nonPrintableChars []rune,
err error) {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
if len(ePrefix) > 0 {
ePrefix += "\n"
}
ePrefix += "errPrefQuark.convertPrintableChars() "
nonPrintableChars = make([]rune, 0)
lenPrintableChars := len(printableChars)
if lenPrintableChars == 0 {
err = fmt.Errorf("%v\n"+
"Error: Input parameter 'printableChars' is invalid!\n"+
"'printableChars' is an empty or zero lenght string.\n",
ePrefix)
return nonPrintableChars, err
}
printableChars = strings.Replace(
printableChars, "[SPACE]", " ", -1)
printableChars = strings.Replace(
printableChars, "[NULL]", string(rune(0x00)), -1) // 0x00 NULL
printableChars = strings.Replace(
printableChars, "[SOH]", string(rune(0x01)), -1) // 0x01 State of Heading
printableChars = strings.Replace(
printableChars, "[STX]", string(rune(0x02)), -1) // 0x02 State of Text
printableChars = strings.Replace(
printableChars, "[ETX]", string(rune(0x03)), -1) // 0x03 End of Text
printableChars = strings.Replace(
printableChars, "[EOT]", string(rune(0x04)), -1) // 0X04 End of Transmission
printableChars = strings.Replace(
printableChars, "[ENQ]", string(rune(0x05)), -1) // 0x05 Enquiry
printableChars = strings.Replace(
printableChars, "[ACK]", string(rune(0x06)), -1) // 0x06 Acknowledge
printableChars = strings.Replace(
printableChars, "\\a", string(rune(0x07)), -1) // U+0007 alert or bell
printableChars = strings.Replace(
printableChars, "\\b", string(rune(0x08)), -1) // U+0008 backspace
printableChars = strings.Replace(
printableChars, "\\t", string(rune(0x09)), -1) // U+0009 horizontal tab
printableChars = strings.Replace(
printableChars, "\\n", string(rune(0x0A)), -1) // U+000A line feed or newline
printableChars = strings.Replace(
printableChars, "\\v", string(rune(0x0B)), -1) // U+000B vertical tab
printableChars = strings.Replace(
printableChars, "\\f", string(rune(0x0C)), -1) // U+000C form feed
printableChars = strings.Replace(
printableChars, "\\r", string(rune(0x0D)), -1) // U+000D carriage return
printableChars = strings.Replace(
printableChars, "[SO]", string(rune(0x0E)), -1) // U+000E Shift Out
printableChars = strings.Replace(
printableChars, "[SI]", string(rune(0x0F)), -1) // U+000F Shift In
printableChars = strings.Replace(
printableChars, "\\", string(rune(0x5c)), -1) // U+005c backslash
nonPrintableChars = []rune(printableChars)
return nonPrintableChars, err
}
// getErrPrefDisplayLineLength - Returns the current value for the
// maximum number of characters allowed in an error prefix text
// display line.
//
//
// ----------------------------------------------------------------
//
// Input Parameters
//
// --- NONE ---
//
//
// -----------------------------------------------------------------
//
// Return Values
//
// int
// - This method will return an unsigned integer value
// specifying the maximum number of characters allowed
// in an error prefix text display line.
//
func (ePrefQuark *errPrefQuark) getErrPrefDisplayLineLength() uint {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
if defaultErrPrefLineLenLock == nil {
defaultErrPrefLineLenLock = new(sync.Mutex)
}
var maxErrPrefixStringLength uint
defaultErrPrefLineLenLock.Lock()
defer defaultErrPrefLineLenLock.Unlock()
erPrfPreon := errPrefPreon{}
minimumErrPrefLineLen := erPrfPreon.getMinErrPrefLineLength()
if defaultErrorPrefixDisplayLineLength <
minimumErrPrefLineLen {
defaultErrorPrefixDisplayLineLength =
erPrfPreon.getDefaultErrPrefLineLength()
}
maxErrPrefixStringLength =
defaultErrorPrefixDisplayLineLength
return maxErrPrefixStringLength
}
// getMasterErrPrefDisplayLineLength - Always returns the master
// error prefix display line length. The default maximum length
// for error prefix display lines is currently 40-characters.
//
func (ePrefQuark *errPrefQuark) getMasterErrPrefDisplayLineLength() uint {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
if defaultErrPrefLineLenLock == nil {
defaultErrPrefLineLenLock = new(sync.Mutex)
}
defaultErrPrefLineLenLock.Lock()
defer defaultErrPrefLineLenLock.Unlock()
return errPrefPreon{}.ptr().getDefaultErrPrefLineLength()
}
// isEmptyOrWhiteSpace - Analyzes the incoming string and returns
// 'true' if the strings is empty or consists of all white space.
//
func (ePrefQuark *errPrefQuark) isEmptyOrWhiteSpace(
targetStr string) bool {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
targetLen := len(targetStr)
for i := 0; i < targetLen; i++ {
if targetStr[i] != ' ' {
return false
}
}
return true
}
// ptr() - Returns a pointer to a new instance of
// errPrefQuark.
//
func (ePrefQuark errPrefQuark) ptr() *errPrefQuark {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
return &errPrefQuark{}
}
// resetErrPrefDisplayLineLengthToDefault - Reset the maximum error
// prefix string line length to the default value.
func (ePrefQuark *errPrefQuark) resetErrPrefDisplayLineLengthToDefault() {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
if defaultErrPrefLineLenLock == nil {
defaultErrPrefLineLenLock = new(sync.Mutex)
}
defaultErrPrefLineLenLock.Lock()
defer defaultErrPrefLineLenLock.Unlock()
defaultErrorPrefixDisplayLineLength =
errPrefPreon{}.ptr().getDefaultErrPrefLineLength()
}
// setErrPrefDisplayLineLength - Sets the value of the maximum
// error prefix line length. This maximum limit controls the length
// of text lines produced for display of error prefix information.
//
// This method will set a global variable to the line length
// specified by input parameter 'maxErrPrefixTextLineLength'. Once
// this maximum limit is set it will control the line lengths for
// all instances of the 'ErrPref' type unless or until this limit
// is reset to another value by calling this method.
//
//
// ----------------------------------------------------------------
//
// Input Parameters
//
// maxErrPrefixTextLineLength uint
// - This unsigned integer value will be used to set the
// maximum number of characters allowed in a text display
// line for error prefix information.
//
// If 'maxErrPrefixTextLineLength' is set to a value less
// than the minimum Error Prefix Line Length, this method
// will take no action and return.
//
//
// -----------------------------------------------------------------
//
// Return Values
//
// --- NONE ---
//
//
func (ePrefQuark *errPrefQuark) setErrPrefDisplayLineLength(
maxErrPrefixStringLength uint) {
if ePrefQuark.lock == nil {
ePrefQuark.lock = new(sync.Mutex)
}
ePrefQuark.lock.Lock()
defer ePrefQuark.lock.Unlock()
if defaultErrPrefLineLenLock == nil {
defaultErrPrefLineLenLock = new(sync.Mutex)
}
defaultErrPrefLineLenLock.Lock()
defer defaultErrPrefLineLenLock.Unlock()
minimum := errPrefPreon{}.ptr().getMinErrPrefLineLength()
if maxErrPrefixStringLength < minimum {
return
}
defaultErrorPrefixDisplayLineLength =
maxErrPrefixStringLength
return
}