forked from bougou/go-ipmi
/
helpers.go
546 lines (463 loc) · 12.4 KB
/
helpers.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
package ipmi
import (
"encoding/base32"
"encoding/base64"
"encoding/binary"
"fmt"
"math"
"math/rand"
"time"
"github.com/kr/pretty"
)
func debugBytes(header string, data []byte, width int) {
fmt.Printf("%s (%d bytes)\n", header, len(data))
for k, v := range data {
if k%width == 0 && k != 0 {
fmt.Printf("\n")
}
fmt.Printf("%02x ", v)
}
fmt.Printf("\n")
}
// debugf pretty print any object
func debugf(format string, object ...interface{}) {
pretty.Printf(format, object...)
}
func debug(header string, object interface{}) {
if header == "" {
pretty.Printf("%# v\n", object)
} else {
pretty.Printf("%s: \n%# v\n", header, object)
}
}
func (c *Client) Debugf(format string, object ...interface{}) {
if !c.debug {
return
}
debugf(format, object...)
}
func (c *Client) Debug(header string, object interface{}) {
if !c.debug {
return
}
debug(header, object)
}
// DebugBytes print byte slices with a fixed width of bytes on each line.
func (c *Client) DebugBytes(header string, data []byte, width int) {
if !c.debug {
return
}
debugBytes(header, data, width)
}
// 37 Timestamp Format
func parseTimestamp(timestamp uint32) time.Time {
return time.Unix(int64(timestamp), 0)
}
func formatBool(b bool, trueStr string, falseStr string) string {
if b {
return trueStr
}
return falseStr
}
// padBytes will padding the origin "s" string to fixed "width" length,
// with "pad" as the padding byte.
func padBytes(s string, width int, pad byte) []byte {
o := []byte(s)
if len(s) >= width {
return o[:width]
}
for i := 0; i < width-len(s); i++ {
o = append(o, pad)
}
return o
}
func isByteSliceEqual(b1 []byte, b2 []byte) bool {
// not equal if both are nil
if b1 == nil || b2 == nil {
return false
}
if len(b1) != len(b2) {
return false
}
for k := range b1 {
if b1[k] != b2[k] {
return false
}
}
return true
}
func array16(s []byte) [16]byte {
var out [16]byte
copy(out[:16], s[:])
return out
}
func randomUint32() uint32 {
rand.Seed(time.Now().Unix())
return rand.Uint32()
}
func randomBytes(n int) []byte {
rand.Seed(time.Now().Unix())
b := make([]byte, n)
rand.Read(b)
return b
}
// onesComplement returns the signed interger of the input number encoded with 1's complement.
// The lowest significant 'bitSize' bits of the input number i is considered.
func onesComplement(i uint32, bitSize uint8) int32 {
var leftBitSize uint8 = 32 - bitSize
var temp uint32 = i << uint32(leftBitSize) >> uint32(leftBitSize)
var mask uint32 = 1 << (bitSize - 1)
if temp&mask == 0 {
// means the bit at `bitSize-1` (from left starting at 0) is 0
// so the result should be a positive value
return int32(temp)
}
// means the bit at `bitSize-1` (from left starting at 0) is 1
// so the result should be a negative value
t := temp ^ 0xffff
t = t << uint32(leftBitSize) >> uint32(leftBitSize)
return -int32(t)
}
// twosComplement returns the signed interger of the input number encoded with 2's complement.
// The lowest significant 'bitSize' bits of the input number i is considered.
func twosComplement(i uint32, bitSize uint8) int32 {
var leftBitSize uint8 = 32 - bitSize
var temp uint32 = i << uint32(leftBitSize) >> uint32(leftBitSize)
var mask uint32 = 1 << (bitSize - 1)
if temp&mask == 0 {
// means the bit at `bitSize-1` (from left starting at 0) is 0
// so the result should be a positive value
return int32(temp)
}
// means the bit at `bitSize-1` (from left starting at 0) is 1
// so the result should be a negative value
t := temp ^ 0xffff + 1
t = t << uint32(leftBitSize) >> uint32(leftBitSize)
return -int32(t)
}
func onesComplementEncode(i int32, bitSize uint8) uint32 {
if i >= 0 {
return uint32(i)
}
var total int32 = int32(math.Pow(2, float64(bitSize))) - 1
return uint32(total + i)
}
func twosComplementEncode(i int32, bitSize uint8) uint32 {
if i >= 0 {
return uint32(i)
}
var total int32 = int32(math.Pow(2, float64(bitSize)))
return uint32(total + i)
}
// The TCP/IP standard network byte order is big-endian.
var base32HexNoPadEncoding = base32.HexEncoding.WithPadding(base32.NoPadding)
func fromBase32(s []byte) (buf []byte, err error) {
for i, b := range s {
if b >= 'a' && b <= 'z' {
s[i] = b - 32
}
}
buflen := base32HexNoPadEncoding.DecodedLen(len(s))
buf = make([]byte, buflen)
n, err := base32HexNoPadEncoding.Decode(buf, s)
buf = buf[:n]
return
}
func toBase32(b []byte) string {
return base32HexNoPadEncoding.EncodeToString(b)
}
func fromBase64(s []byte) (buf []byte, err error) {
buflen := base64.StdEncoding.DecodedLen(len(s))
buf = make([]byte, buflen)
n, err := base64.StdEncoding.Decode(buf, s)
buf = buf[:n]
return
}
func toBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func setBit7(b uint8) uint8 {
return b | 0x80
}
func setBit6(b uint8) uint8 {
return b | 0x40
}
func setBit5(b uint8) uint8 {
return b | 0x20
}
func setBit4(b uint8) uint8 {
return b | 0x10
}
func setBit3(b uint8) uint8 {
return b | 0x08
}
func setBit2(b uint8) uint8 {
return b | 0x04
}
func setBit1(b uint8) uint8 {
return b | 0x02
}
func setBit0(b uint8) uint8 {
return b | 0x01
}
func clearBit7(b uint8) uint8 {
return b & 0x7f
}
func clearBit6(b uint8) uint8 {
return b & 0xbf
}
func clearBit5(b uint8) uint8 {
return b & 0xdf
}
func clearBit4(b uint8) uint8 {
return b & 0xef
}
func clearBit3(b uint8) uint8 {
return b & 0xf7
}
func clearBit2(b uint8) uint8 {
return b & 0xfb
}
func clearBit1(b uint8) uint8 {
return b & 0xfd
}
func clearBit0(b uint8) uint8 {
return b & 0xfe
}
func isBit7Set(b uint8) bool {
return b&0x80 == 0x80
}
func isBit6Set(b uint8) bool {
return b&0x40 == 0x40
}
func isBit5Set(b uint8) bool {
return b&0x20 == 0x20
}
func isBit4Set(b uint8) bool {
return b&0x10 == 0x10
}
func isBit3Set(b uint8) bool {
return b&0x08 == 0x08
}
func isBit2Set(b uint8) bool {
return b&0x04 == 0x04
}
func isBit1Set(b uint8) bool {
return b&0x02 == 0x02
}
func isBit0Set(b uint8) bool {
return b&0x01 == 0x01
}
func unpackUint8(msg []byte, off int) (uint8, int, error) {
if off+1 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint8")
}
return msg[off], off + 1, nil
}
// packUint8 fills an uint8 value (i) into byte slice (msg) at the index of (offset)
func packUint8(i uint8, msg []byte, off int) (int, error) {
if off+1 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint8")
}
msg[off] = i
return off + 1, nil
}
func packBytes(v []byte, msg []byte, off int) (int, error) {
if off+len(v) > len(msg) {
return len(msg), fmt.Errorf("overflow packing byte slice")
}
for k, b := range v {
msg[off+k] = b
}
return off + len(v), nil
}
func unpackBytes(msg []byte, off int, length int) ([]byte, int, error) {
out := []byte{}
if off+length > len(msg) {
return out, off, fmt.Errorf("overflow unpacking %d bytes", length)
}
out = append(out, msg[off:off+length]...)
return out, off + length, nil
}
// unpackBytesMost unpacks most length of bytes from msg starting from off index.
// It stops when reaching the msg end or reaching the most length.
// This functions never failed, it always return nil error.
// The caller should check the length of the returned out byte
func unpackBytesMost(msg []byte, off int, length int) ([]byte, int, error) {
out := make([]byte, length)
var i int = 0
for ; i < length; i++ {
if off+i >= len(msg) {
break
}
out[i] = msg[off+i]
}
return out[:i], off + len(out), nil
}
func unpackUint16(msg []byte, off int) (uint16, int, error) {
if off+2 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint16")
}
return binary.BigEndian.Uint16(msg[off:]), off + 2, nil
}
func unpackUint16L(msg []byte, off int) (uint16, int, error) {
if off+2 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint16")
}
return binary.LittleEndian.Uint16(msg[off:]), off + 2, nil
}
func packUint16(i uint16, msg []byte, off int) (int, error) {
if off+2 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint16")
}
binary.BigEndian.PutUint16(msg[off:], i)
return off + 2, nil
}
func packUint16L(i uint16, msg []byte, off int) (int, error) {
if off+2 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint16")
}
binary.LittleEndian.PutUint16(msg[off:], i)
return off + 2, nil
}
func unpackUint24(msg []byte, off int) (uint32, int, error) {
if off+3 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint32 as uint24")
}
i := uint32(msg[off])<<16 | uint32(msg[off+1])<<8 | uint32(msg[off+2])
off += 3
return i, off, nil
}
func unpackUint24L(msg []byte, off int) (uint32, int, error) {
if off+3 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint32 as uint24")
}
i := uint32(msg[off]) | uint32(msg[off+1])<<8 | uint32(msg[off+2])<<16
off += 3
return i, off, nil
}
func packUint24(i uint32, msg []byte, off int) (int, error) {
if off+3 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint32 as uint24")
}
msg[off] = byte(i >> 16)
msg[off+1] = byte(i >> 8)
msg[off+2] = byte(i)
off += 3
return off, nil
}
func packUint24L(i uint32, msg []byte, off int) (int, error) {
if off+3 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint32 as uint24")
}
msg[off] = byte(i)
msg[off+1] = byte(i >> 8)
msg[off+2] = byte(i >> 16)
off += 3
return off, nil
}
func unpackUint32(msg []byte, off int) (uint32, int, error) {
if off+4 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint32")
}
return binary.BigEndian.Uint32(msg[off:]), off + 4, nil
}
func unpackUint32L(msg []byte, off int) (uint32, int, error) {
if off+4 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint32")
}
return binary.LittleEndian.Uint32(msg[off:]), off + 4, nil
}
func packUint32(i uint32, msg []byte, off int) (int, error) {
if off+4 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint32")
}
binary.BigEndian.PutUint32(msg[off:], i)
return off + 4, nil
}
func packUint32L(i uint32, msg []byte, off int) (int, error) {
if off+4 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint32")
}
binary.LittleEndian.PutUint32(msg[off:], i)
return off + 4, nil
}
func unpackUint48(msg []byte, off int) (uint64, int, error) {
if off+6 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint64 as uint48")
}
i := uint64(msg[off])<<40 | uint64(msg[off+1])<<32 | uint64(msg[off+2])<<24 | uint64(msg[off+3])<<16 |
uint64(msg[off+4])<<8 | uint64(msg[off+5])
off += 6
return i, off, nil
}
func unpackUint48L(msg []byte, off int) (uint64, int, error) {
if off+6 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint64 as uint48")
}
i := uint64(msg[off]) | uint64(msg[off+1])<<8 | uint64(msg[off+2])<<16 | uint64(msg[off+3])<<24 |
uint64(msg[off+4])<<32 | uint64(msg[off+5])<<40
off += 6
return i, off, nil
}
func packUint48(i uint64, msg []byte, off int) (int, error) {
if off+6 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint64 as uint48")
}
msg[off] = byte(i >> 40)
msg[off+1] = byte(i >> 32)
msg[off+2] = byte(i >> 24)
msg[off+3] = byte(i >> 16)
msg[off+4] = byte(i >> 8)
msg[off+5] = byte(i)
off += 6
return off, nil
}
func packUint48L(i uint64, msg []byte, off int) (int, error) {
if off+6 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint64 as uint48")
}
msg[off] = byte(i)
msg[off+1] = byte(i >> 8)
msg[off+2] = byte(i >> 16)
msg[off+3] = byte(i >> 24)
msg[off+4] = byte(i >> 32)
msg[off+5] = byte(i >> 40)
off += 6
return off, nil
}
func unpackUint64(msg []byte, off int) (uint64, int, error) {
if off+8 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint64")
}
return binary.BigEndian.Uint64(msg[off:]), off + 8, nil
}
func unpackUint64L(msg []byte, off int) (uint64, int, error) {
if off+8 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint64")
}
return binary.LittleEndian.Uint64(msg[off:]), off + 8, nil
}
func packUint64(i uint64, msg []byte, off int) (int, error) {
if off+8 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint64")
}
binary.BigEndian.PutUint64(msg[off:], i)
off += 8
return off, nil
}
func packUint64L(i uint64, msg []byte, off int) (int, error) {
if off+8 > len(msg) {
return len(msg), fmt.Errorf("overflow packing uint64")
}
binary.LittleEndian.PutUint64(msg[off:], i)
off += 8
return off, nil
}
// 8421 BCD
// bcdUint8 decodes BCD encoded interger to normal unsigned interger.
func bcdUint8(i uint8) uint8 {
msb4 := i >> 4
lsb4 := i & 0x0f
return msb4*10 + lsb4
}