]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/yenc.qh
Merge branch 'terencehill/menu_hudskin_selector' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / yenc.qh
1 #ifndef YENC_H
2 #define YENC_H
3
4 #include "test.qh"
5
6 #define yenc_single(c, ret) \
7         MACRO_BEGIN { \
8                 int conv = c; \
9                 conv += 42; \
10                 if (conv >= 256) conv -= 256; \
11                 switch (conv) \
12                 { \
13                         default: \
14                         { \
15                                 string yenc_it = chr2str(conv); \
16                                 ret = yenc_it; \
17                                 break; \
18                         } \
19                         case 0: \
20                         case '\n': \
21                         case '\r': \
22                         case '=': \
23                         { \
24                                 conv += 64; \
25                                 string yenc_it = chr2str('=', conv); \
26                                 ret = yenc_it; \
27                                 break; \
28                         } \
29                 } \
30         } MACRO_END
31
32 #define ydec_single(stringiter, ret) \
33         MACRO_BEGIN { \
34                 int conv = STRING_ITERATOR_GET(stringiter); \
35                 if (conv <= 0) { \
36                         ret = -1; \
37                 } else { \
38                         bool esc = false; \
39                         if (conv == '=') { \
40                                 esc = true; \
41                                 conv = STRING_ITERATOR_GET(stringiter); \
42                                 conv -= 64; \
43                         } \
44                         if (conv < 42) conv += 256; \
45                         conv -= 42; \
46                         ret = conv; \
47                 } \
48         } MACRO_END
49
50 TEST(yEncDec)
51 {
52         for (int i = 0; i <= 255; ++i)
53         {
54                 int expect = i;
55
56                 string fragment;
57                 yenc_single(expect, fragment);
58
59                 int encdec;
60                 STRING_ITERATOR(fragmentiterator, fragment, 0);
61                 ydec_single(fragmentiterator, encdec);
62
63                 TEST_Check(encdec == expect);
64         }
65         TEST_OK();
66 }
67
68 #endif