Created
December 29, 2022 21:28
-
-
Save sayotte/122f071cae9d248c7c76dba7432b5288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package network | |
var uoHuffmanTree = [][]int{ | |
/*node*/ /*leaf0 leaf1*/ | |
/* 0*/ | |
{2, 1}, | |
/* 1*/ | |
{4, 3}, | |
/* 2*/ | |
{0, 5}, | |
/* 3*/ | |
{7, 6}, | |
/* 4*/ | |
{9, 8}, | |
/* 5*/ | |
{11, 10}, | |
/* 6*/ | |
{13, 12}, | |
/* 7*/ | |
{14, -256}, | |
/* 8*/ | |
{16, 15}, | |
/* 9*/ | |
{18, 17}, | |
/* 10*/ | |
{20, 19}, | |
/* 11*/ | |
{22, 21}, | |
/* 12*/ | |
{23, -1}, | |
/* 13*/ | |
{25, 24}, | |
/* 14*/ | |
{27, 26}, | |
/* 15*/ | |
{29, 28}, | |
/* 16*/ | |
{31, 30}, | |
/* 17*/ | |
{33, 32}, | |
/* 18*/ | |
{35, 34}, | |
/* 19*/ | |
{37, 36}, | |
/* 20*/ | |
{39, 38}, | |
/* 21*/ | |
{-64, 40}, | |
/* 22*/ | |
{42, 41}, | |
/* 23*/ | |
{44, 43}, | |
/* 24*/ | |
{45, -6}, | |
/* 25*/ | |
{47, 46}, | |
/* 26*/ | |
{49, 48}, | |
/* 27*/ | |
{51, 50}, | |
/* 28*/ | |
{52, -119}, | |
/* 29*/ | |
{53, -32}, | |
/* 30*/ | |
{-14, 54}, | |
/* 31*/ | |
{-5, 55}, | |
/* 32*/ | |
{57, 56}, | |
/* 33*/ | |
{59, 58}, | |
/* 34*/ | |
{-2, 60}, | |
/* 35*/ | |
{62, 61}, | |
/* 36*/ | |
{64, 63}, | |
/* 37*/ | |
{66, 65}, | |
/* 38*/ | |
{68, 67}, | |
/* 39*/ | |
{70, 69}, | |
/* 40*/ | |
{72, 71}, | |
/* 41*/ | |
{73, -51}, | |
/* 42*/ | |
{75, 74}, | |
/* 43*/ | |
{77, 76}, | |
/* 44*/ | |
{-111, -101}, | |
/* 45*/ | |
{-97, -4}, | |
/* 46*/ | |
{79, 78}, | |
/* 47*/ | |
{80, -110}, | |
/* 48*/ | |
{-116, 81}, | |
/* 49*/ | |
{83, 82}, | |
/* 50*/ | |
{-255, 84}, | |
/* 51*/ | |
{86, 85}, | |
/* 52*/ | |
{88, 87}, | |
/* 53*/ | |
{90, 89}, | |
/* 54*/ | |
{-10, -15}, | |
/* 55*/ | |
{92, 91}, | |
/* 56*/ | |
{93, -21}, | |
/* 57*/ | |
{94, -117}, | |
/* 58*/ | |
{96, 95}, | |
/* 59*/ | |
{98, 97}, | |
/* 60*/ | |
{100, 99}, | |
/* 61*/ | |
{101, -114}, | |
/* 62*/ | |
{102, -105}, | |
/* 63*/ | |
{103, -26}, | |
/* 64*/ | |
{105, 104}, | |
/* 65*/ | |
{107, 106}, | |
/* 66*/ | |
{109, 108}, | |
/* 67*/ | |
{111, 110}, | |
/* 68*/ | |
{-3, 112}, | |
/* 69*/ | |
{-7, 113}, | |
/* 70*/ | |
{-131, 114}, | |
/* 71*/ | |
{-144, 115}, | |
/* 72*/ | |
{117, 116}, | |
/* 73*/ | |
{118, -20}, | |
/* 74*/ | |
{120, 119}, | |
/* 75*/ | |
{122, 121}, | |
/* 76*/ | |
{124, 123}, | |
/* 77*/ | |
{126, 125}, | |
/* 78*/ | |
{128, 127}, | |
/* 79*/ | |
{-100, 129}, | |
/* 80*/ | |
{-8, 130}, | |
/* 81*/ | |
{132, 131}, | |
/* 82*/ | |
{134, 133}, | |
/* 83*/ | |
{135, -120}, | |
/* 84*/ | |
{-31, 136}, | |
/* 85*/ | |
{138, 137}, | |
/* 86*/ | |
{-234, -109}, | |
/* 87*/ | |
{140, 139}, | |
/* 88*/ | |
{142, 141}, | |
/* 89*/ | |
{144, 143}, | |
/* 90*/ | |
{145, -112}, | |
/* 91*/ | |
{146, -19}, | |
/* 92*/ | |
{148, 147}, | |
/* 93*/ | |
{-66, 149}, | |
/* 94*/ | |
{-145, 150}, | |
/* 95*/ | |
{-65, -13}, | |
/* 96*/ | |
{152, 151}, | |
/* 97*/ | |
{154, 153}, | |
/* 98*/ | |
{155, -30}, | |
/* 99*/ | |
{157, 156}, | |
/* 100*/ | |
{158, -99}, | |
/* 101*/ | |
{160, 159}, | |
/* 102*/ | |
{162, 161}, | |
/* 103*/ | |
{163, -23}, | |
/* 104*/ | |
{164, -29}, | |
/* 105*/ | |
{165, -11}, | |
/* 106*/ | |
{-115, 166}, | |
/* 107*/ | |
{168, 167}, | |
/* 108*/ | |
{170, 169}, | |
/* 109*/ | |
{171, -16}, | |
/* 110*/ | |
{172, -34}, | |
/* 111*/ | |
{-132, 173}, | |
/* 112*/ | |
{-108, 174}, | |
/* 113*/ | |
{-22, 175}, | |
/* 114*/ | |
{-9, 176}, | |
/* 115*/ | |
{-84, 177}, | |
/* 116*/ | |
{-37, -17}, | |
/* 117*/ | |
{178, -28}, | |
/* 118*/ | |
{180, 179}, | |
/* 119*/ | |
{182, 181}, | |
/* 120*/ | |
{184, 183}, | |
/* 121*/ | |
{186, 185}, | |
/* 122*/ | |
{-104, 187}, | |
/* 123*/ | |
{-78, 188}, | |
/* 124*/ | |
{-61, 189}, | |
/* 125*/ | |
{-178, -79}, | |
/* 126*/ | |
{-134, -59}, | |
/* 127*/ | |
{-25, 190}, | |
/* 128*/ | |
{-18, -83}, | |
/* 129*/ | |
{-57, 191}, | |
/* 130*/ | |
{192, -67}, | |
/* 131*/ | |
{193, -98}, | |
/* 132*/ | |
{-68, -12}, | |
/* 133*/ | |
{195, 194}, | |
/* 134*/ | |
{-128, -55}, | |
/* 135*/ | |
{-50, -24}, | |
/* 136*/ | |
{196, -70}, | |
/* 137*/ | |
{-33, -94}, | |
/* 138*/ | |
{-129, 197}, | |
/* 139*/ | |
{198, -74}, | |
/* 140*/ | |
{199, -82}, | |
/* 141*/ | |
{-87, -56}, | |
/* 142*/ | |
{200, -44}, | |
/* 143*/ | |
{201, -248}, | |
/* 144*/ | |
{-81, -163}, | |
/* 145*/ | |
{-123, -52}, | |
/* 146*/ | |
{-113, 202}, | |
/* 147*/ | |
{-41, -48}, | |
/* 148*/ | |
{-40, -122}, | |
/* 149*/ | |
{-90, 203}, | |
/* 150*/ | |
{204, -54}, | |
/* 151*/ | |
{-192, -86}, | |
/* 152*/ | |
{206, 205}, | |
/* 153*/ | |
{-130, 207}, | |
/* 154*/ | |
{208, -53}, | |
/* 155*/ | |
{-45, -133}, | |
/* 156*/ | |
{210, 209}, | |
/* 157*/ | |
{-91, 211}, | |
/* 158*/ | |
{213, 212}, | |
/* 159*/ | |
{-88, -106}, | |
/* 160*/ | |
{215, 214}, | |
/* 161*/ | |
{217, 216}, | |
/* 162*/ | |
{-49, 218}, | |
/* 163*/ | |
{220, 219}, | |
/* 164*/ | |
{222, 221}, | |
/* 165*/ | |
{224, 223}, | |
/* 166*/ | |
{226, 225}, | |
/* 167*/ | |
{-102, 227}, | |
/* 168*/ | |
{228, -160}, | |
/* 169*/ | |
{229, -46}, | |
/* 170*/ | |
{230, -127}, | |
/* 171*/ | |
{231, -103}, | |
/* 172*/ | |
{233, 232}, | |
/* 173*/ | |
{234, -60}, | |
/* 174*/ | |
{-76, 235}, | |
/* 175*/ | |
{-121, 236}, | |
/* 176*/ | |
{-73, 237}, | |
/* 177*/ | |
{238, -149}, | |
/* 178*/ | |
{-107, 239}, | |
/* 179*/ | |
{240, -35}, | |
/* 180*/ | |
{-27, -71}, | |
/* 181*/ | |
{241, -69}, | |
/* 182*/ | |
{-77, -89}, | |
/* 183*/ | |
{-118, -62}, | |
/* 184*/ | |
{-85, -75}, | |
/* 185*/ | |
{-58, -72}, | |
/* 186*/ | |
{-80, -63}, | |
/* 187*/ | |
{-42, 242}, | |
/* 188*/ | |
{-157, -150}, | |
/* 189*/ | |
{-236, -139}, | |
/* 190*/ | |
{-243, -126}, | |
/* 191*/ | |
{-214, -142}, | |
/* 192*/ | |
{-206, -138}, | |
/* 193*/ | |
{-146, -240}, | |
/* 194*/ | |
{-147, -204}, | |
/* 195*/ | |
{-201, -152}, | |
/* 196*/ | |
{-207, -227}, | |
/* 197*/ | |
{-209, -154}, | |
/* 198*/ | |
{-254, -153}, | |
/* 199*/ | |
{-156, -176}, | |
/* 200*/ | |
{-210, -165}, | |
/* 201*/ | |
{-185, -172}, | |
/* 202*/ | |
{-170, -195}, | |
/* 203*/ | |
{-211, -232}, | |
/* 204*/ | |
{-239, -219}, | |
/* 205*/ | |
{-177, -200}, | |
/* 206*/ | |
{-212, -175}, | |
/* 207*/ | |
{-143, -244}, | |
/* 208*/ | |
{-171, -246}, | |
/* 209*/ | |
{-221, -203}, | |
/* 210*/ | |
{-181, -202}, | |
/* 211*/ | |
{-250, -173}, | |
/* 212*/ | |
{-164, -184}, | |
/* 213*/ | |
{-218, -193}, | |
/* 214*/ | |
{-220, -199}, | |
/* 215*/ | |
{-249, -190}, | |
/* 216*/ | |
{-217, -230}, | |
/* 217*/ | |
{-216, -169}, | |
/* 218*/ | |
{-197, -191}, | |
/* 219*/ | |
{243, -47}, | |
/* 220*/ | |
{245, 244}, | |
/* 221*/ | |
{247, 246}, | |
/* 222*/ | |
{-159, -148}, | |
/* 223*/ | |
{249, 248}, | |
/* 224*/ | |
{-93, -92}, | |
/* 225*/ | |
{-225, -96}, | |
/* 226*/ | |
{-95, -151}, | |
/* 227*/ | |
{251, 250}, | |
/* 228*/ | |
{252, -241}, | |
/* 229*/ | |
{-36, -161}, | |
/* 230*/ | |
{254, 253}, | |
/* 231*/ | |
{-39, -135}, | |
/* 232*/ | |
{-124, -187}, | |
/* 233*/ | |
{-251, 255}, | |
/* 234*/ | |
{-238, -162}, | |
/* 235*/ | |
{-38, -242}, | |
/* 236*/ | |
{-125, -43}, | |
/* 237*/ | |
{-253, -215}, | |
/* 238*/ | |
{-208, -140}, | |
/* 239*/ | |
{-235, -137}, | |
/* 240*/ | |
{-237, -158}, | |
/* 241*/ | |
{-205, -136}, | |
/* 242*/ | |
{-141, -155}, | |
/* 243*/ | |
{-229, -228}, | |
/* 244*/ | |
{-168, -213}, | |
/* 245*/ | |
{-194, -224}, | |
/* 246*/ | |
{-226, -196}, | |
/* 247*/ | |
{-233, -183}, | |
/* 248*/ | |
{-167, -231}, | |
/* 249*/ | |
{-189, -174}, | |
/* 250*/ | |
{-166, -252}, | |
/* 251*/ | |
{-222, -198}, | |
/* 252*/ | |
{-179, -188}, | |
/* 253*/ | |
{-182, -223}, | |
/* 254*/ | |
{-186, -180}, | |
/* 255*/ | |
{-247, -245}, | |
} | |
type UOHuffmanCodec struct { | |
// TODO optimize for fewer allocations/copies | |
// - could use a buffer pool to reduce GC pressure | |
// - could use a ring buffer | |
buf []byte | |
} | |
func (uohc *UOHuffmanCodec) Write(p []byte) (int, error) { | |
uohc.buf = append(uohc.buf, p...) | |
return len(p), nil | |
} | |
func (uohc *UOHuffmanCodec) Next() ([]byte, bool) { | |
var node int | |
bitNum := 8 | |
// preallocate output buffer backing array to minimize grow operations | |
dest := make([]byte, 0, len(uohc.buf)*2) | |
for srcOffset := 0; srcOffset < len(uohc.buf); { | |
leaf := (uohc.buf[srcOffset] >> (bitNum - 1)) & 1 | |
leafValue := uoHuffmanTree[node][leaf] | |
// all numbers below 1 (0..-256) are codewords | |
// if the halt codeword has been found, skip this byte | |
if leafValue == -256 { | |
srcOffset++ | |
uohc.buf = append([]byte{}, uohc.buf[srcOffset:]...) | |
return dest, true | |
} | |
// otherwise, emit the absolute value of the leafValue, | |
// and start over at the top of the tree with the next bit | |
if leafValue < 1 { | |
dest = append(dest, byte(-leafValue)) | |
leafValue = 0 | |
} | |
bitNum-- | |
node = leafValue | |
/* if it's the end of the byte, go to the next byte */ | |
if bitNum < 1 { | |
bitNum = 8 | |
srcOffset++ | |
} | |
} | |
return nil, false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package network | |
import ( | |
"bytes" | |
"testing" | |
) | |
func TestUOHuffmanCodec(t *testing.T) { | |
t.Run("two network, one buffer", func(t *testing.T) { | |
var input = []byte{ | |
0xb3, 0x03, 0x40, 0x81, 0x5a, 0x49, 0x14, 0x74, 0xb7, 0x06, 0x6b, 0x57, 0x90, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe9, 0x6b, 0x86, 0x1f, 0x44, 0x53, | |
0x3a, 0xdc, 0xd0, 0xfa, 0x41, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x07, 0x4b, 0x5d, 0xa0, 0xb8, 0x60, 0xb5, 0x5b, 0x99, 0x02, 0x0f, 0x97, 0x50, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x2d, 0x76, 0x8d, 0xf1, 0x37, 0x30, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd6, 0x83, 0x49, | |
0xa4, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, | |
0xb0, 0x8d, 0xf3, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xf2, 0x07, 0x9b, 0x51, 0xf8, | |
0x36, 0x3d, 0x33, 0x20, 0x5b, 0x2d, 0x9f, 0xcf, 0xf9, 0x67, 0x00, 0x00, 0x00, 0x01, 0xf4, 0x64, | |
0x1e, 0x3e, 0x23, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x79, 0x03, 0xcd, 0xb3, 0x9d, 0x21, | |
0xf1, 0xe9, 0x1a, 0xd5, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x08, 0xa7, 0x21, 0xc1, 0xcd, 0xd2, 0x0f, | |
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x6d, 0xfe, 0x7c, 0xe6, 0xb7, 0x39, 0x0f, 0xcf, 0x4c, | |
0x17, 0x6b, 0x58, 0xdc, 0x78, 0x80, 0x00, 0x00, 0x00, 0xd1, 0x19, 0xe3, 0xc7, 0x17, 0x0b, 0x57, | |
0x8b, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x40, 0xf3, 0x6f, 0x36, 0x35, 0x03, 0xc3, 0x57, | |
0x48, 0x6e, 0xd6, 0xb1, 0xb8, 0xf1, 0x00, 0x00, 0x00, 0x03, 0xaa, 0xf4, 0x38, 0x3c, 0x5d, 0xa0, | |
0xc6, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x20, 0x79, 0xb5, 0x7a, 0x1f, 0x44, 0x57, 0x96, | |
0xaf, 0x30, 0xdc, 0x07, 0x6b, 0x58, 0xdc, 0x78, 0x80, 0x00, 0x00, 0x14, 0x23, 0x3a, 0x38, 0x60, | |
0xf1, 0x8d, 0x83, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xe4, 0x0f, 0x36, 0xa4, 0x61, 0xf9, | |
0xe9, 0x9a, 0xd4, 0x3f, 0x88, 0x7c, 0x5d, 0xad, 0x5e, 0xe8, 0x8a, 0xf3, 0x0f, 0x88, 0x00, 0x01, | |
0xdd, 0xdd, 0x03, 0xcb, 0x57, 0x8c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x79, 0x03, 0xcd, | |
0xa8, 0xcf, 0x30, 0xe3, 0x7c, 0xf1, 0xe9, 0x0d, 0x9d, 0x6b, 0x1b, 0x8f, 0x10, 0x00, 0x00, 0x00, | |
0x65, 0xcd, 0x58, 0x74, 0x87, 0xd2, 0xd9, 0xc8, 0x7d, 0x3c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0b, | |
0xc8, 0x1e, 0x6d, 0xc7, 0xf4, 0x6a, 0x8d, 0xf1, 0xc7, 0xcc, 0x37, 0x01, 0xda, 0xd6, 0x37, 0x1e, | |
0x20, 0x00, 0x00, 0x07, 0x04, 0xaf, 0xcb, 0xb5, 0x8f, 0x98, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x02, 0xf2, 0x07, 0x9b, 0x58, 0xd0, 0xfc, 0x71, 0xbf, 0xc7, 0x8e, 0x4b, 0x58, 0xdc, 0x78, 0x80, | |
0x00, 0x00, 0x00, 0xc5, 0xeb, 0x8d, 0x8d, 0x6a, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, | |
0xc8, 0x1e, 0x6d, 0x9c, 0xf1, 0x37, 0x8e, 0x68, 0x2e, 0xa9, 0xad, 0x56, 0xa1, 0xfa, 0x43, 0x45, | |
0x79, 0x76, 0xcc, 0x00, 0x00, 0x00, 0x70, 0x68, | |
} | |
uohc := &UOHuffmanCodec{} | |
_, _ = uohc.Write(input) | |
var expectedFirst = []byte{0xB9, 0x00, 0x00} | |
actual, pktComplete := uohc.Next() | |
if !pktComplete { | |
t.Fatal("pktComplete != true") | |
} | |
if !bytes.Equal(actual, expectedFirst) { | |
if len(actual) != len(expectedFirst) { | |
t.Error("len(actual) != len(expected)") | |
} | |
for i := 0; i < len(actual) && i < len(expectedFirst); i++ { | |
if actual[i] != expectedFirst[i] { | |
t.Errorf("actual[%d](0x%02X) != expected[%d](0x%02X)", i, actual[i], i, expectedFirst[i]) | |
break | |
} | |
} | |
} | |
var expectedSecond = []byte{ | |
0xA9, 0x03, 0xAB, 0x05, 0x61, 0x20, 0x6D, 0x75, 0x6C, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x61, 0x20, 0x67, 0x72, 0x61, 0x76, 0x79, 0x20, 0x74, 0x72, 0x61, 0x69, 0x6E, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x20, 0x73, 0x69, | |
0x67, 0x69, 0x6C, 0x20, 0x74, 0x68, 0x69, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x20, 0x73, 0x63, 0x6F, 0x75, 0x74, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x4C, 0x69, 0x7A, 0x7A, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x0A, 0x00, 0x59, 0x65, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x54, 0x68, 0x65, 0x20, 0x45, 0x6D, 0x70, 0x61, 0x74, 0x68, 0x20, 0x41, 0x62, 0x62, 0x65, | |
0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x01, 0x4D, 0x69, 0x6E, 0x6F, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x54, 0x68, 0x65, 0x20, 0x42, 0x61, 0x72, 0x6E, 0x61, 0x63, 0x6C, 0x65, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, | |
0x42, 0x72, 0x69, 0x74, 0x61, 0x69, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, | |
0x77, 0x65, 0x65, 0x74, 0x20, 0x44, 0x72, 0x65, 0x61, 0x6D, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4D, | |
0x6F, 0x6F, 0x6E, 0x67, 0x6C, 0x6F, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, | |
0x65, 0x20, 0x53, 0x63, 0x68, 0x6F, 0x6C, 0x61, 0x72, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x54, 0x72, | |
0x69, 0x6E, 0x73, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, | |
0x20, 0x54, 0x72, 0x61, 0x76, 0x65, 0x6C, 0x65, 0x72, 0x27, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4D, 0x61, 0x67, | |
0x69, 0x6E, 0x63, 0x69, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, | |
0x47, 0x72, 0x65, 0x61, 0x74, 0x20, 0x48, 0x6F, 0x72, 0x6E, 0x73, 0x20, 0x54, 0x61, 0x76, 0x65, | |
0x72, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x4A, 0x68, 0x65, 0x6C, | |
0x6F, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x4D, | |
0x65, 0x72, 0x63, 0x65, 0x6E, 0x61, 0x72, 0x79, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x53, 0x6B, 0x61, 0x72, 0x61, | |
0x20, 0x42, 0x72, 0x61, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x46, 0x61, | |
0x6C, 0x63, 0x6F, 0x6E, 0x65, 0x72, 0x27, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x56, 0x65, 0x73, 0x70, 0x65, 0x72, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x49, 0x72, 0x6F, | |
0x6E, 0x77, 0x6F, 0x6F, 0x64, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x4F, 0x63, 0x63, 0x6C, 0x6F, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x42, 0x6F, 0x75, 0x6E, | |
0x74, 0x69, 0x66, 0x75, 0x6C, 0x20, 0x48, 0x61, 0x72, 0x76, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, | |
} | |
actual, pktComplete = uohc.Next() | |
if !pktComplete { | |
t.Fatal("pktComplete != true") | |
} | |
if !bytes.Equal(actual, expectedSecond) { | |
if len(actual) != len(expectedSecond) { | |
t.Error("len(actual) != len(expected)") | |
} | |
for i := 0; i < len(actual) && i < len(expectedSecond); i++ { | |
if actual[i] != expectedSecond[i] { | |
t.Errorf("actual[%d](0x%02X) != expected[%d](0x%02X)", i, actual[i], i, expectedSecond[i]) | |
break | |
} | |
} | |
} | |
}) | |
t.Run("fragmented packet", func(t *testing.T) { | |
uohc := &UOHuffmanCodec{} | |
_, _ = uohc.Write([]byte{0x78, 0x83}) | |
_, complete := uohc.Next() | |
if complete { | |
t.Fatal("complete == true, unexpectedly") | |
} | |
var expected = []byte{0xbd, 0x00, 0x03} | |
_, _ = uohc.Write([]byte{0x4d}) | |
actual, complete := uohc.Next() | |
if !complete { | |
t.Fatal("complete != true") | |
} | |
if !bytes.Equal(actual, expected) { | |
if len(actual) != len(expected) { | |
t.Error("len(actual) != len(expected)") | |
} | |
for i := 0; i < len(actual) && i < len(expected); i++ { | |
if actual[i] != expected[i] { | |
t.Errorf("actual[%d](0x%02X) != expected[%d](0x%02X)", i, actual[i], i, expected[i]) | |
break | |
} | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test output: