FreeRDP
LOMHash.c
1 #include <stdio.h>
2 typedef unsigned short UINT16;
3 typedef unsigned char BYTE;
4 static UINT16 HuffCodeLOM[] = { 0x0001, 0x0000, 0x0002, 0x0009, 0x0006, 0x0005, 0x000d, 0x000b,
5  0x0003, 0x001b, 0x0007, 0x0017, 0x0037, 0x000f, 0x004f, 0x006f,
6  0x002f, 0x00ef, 0x001f, 0x005f, 0x015f, 0x009f, 0x00df, 0x01df,
7  0x003f, 0x013f, 0x00bf, 0x01bf, 0x007f, 0x017f, 0x00ff, 0x01ff };
8 
9 UINT16 HashTable[32] = { [0 ... 31] = 0xffff };
10 
11 BYTE tab[4] = { 0, 4, 10, 19 };
12 
13 UINT16 hash(UINT16 key)
14 {
15  return ((key & 0x1f) ^ (key >> 5) ^ (key >> 9));
16 }
17 
18 BYTE minihash(UINT16 key)
19 {
20  BYTE h;
21  h = (key >> 4) & 0xf;
22  return ((h ^ (h >> 2) ^ (h >> 3)) & 0x3);
23 }
24 
25 void buildhashtable(void)
26 {
27  for (int i = 0; i < 32; i++)
28  {
29  UINT16 h = hash(HuffCodeLOM[i]);
30 
31  if (HashTable[h] != 0xffff)
32  {
33  HashTable[h] ^= (HuffCodeLOM[i] & 0xfe0) ^ 0xfe0;
34  HashTable[tab[minihash(HuffCodeLOM[i])]] = i;
35  }
36  else
37  {
38  HashTable[h] = i;
39  HashTable[h] ^= 0xfe0;
40  }
41 
42  printf("at %d %" PRIu16 "=0x%" PRIx16 "\n", i, h, HashTable[h]);
43  }
44 }
45 
46 BYTE getvalue(UINT16 huff)
47 {
48  UINT16 h = HashTable[hash(huff)];
49 
50  if ((h ^ huff) >> 5)
51  {
52  return h & 0x1f;
53  }
54  else
55  return HashTable[tab[minihash(huff)]];
56 }
57 
58 main()
59 {
60  buildhashtable();
61  printf("static UINT16 HuffIndexLOM[32] = {\n");
62 
63  for (int i = 0; i < 32; i++)
64  {
65  if (i == 31)
66  printf("0x%" PRIx16 " };\n", HashTable[i]);
67  else
68  printf("0x%" PRIx16 ", ", HashTable[i]);
69  }
70 
71  for (int i = 0; i < 32; i++)
72  if (i != getvalue(HuffCodeLOM[i]))
73  printf("Fail :( at %d : 0x%04" PRIx16 " got %" PRIu8 "\n", i, HuffCodeLOM[i],
74  getvalue(HuffCodeLOM[i]));
75 
76  return 0;
77 }