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