Fix removal of the locked waypoint
[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                         bool esc = false; \
38                         if (conv == '=') { \
39                                 esc = true; \
40                                 conv = STRING_ITERATOR_GET(stringiter); \
41                                 conv -= 64; \
42                         } \
43                         if (conv < 42) conv += 256; \
44                         conv -= 42; \
45                         ret = conv; \
46                 } \
47         } MACRO_END
48
49 TEST(yEnc, EncodeDecode)
50 {
51         for (int i = 0; i <= 255; ++i)
52         {
53                 int expect = i;
54
55                 string fragment = string_null;
56                 yenc_single(expect, fragment);
57
58                 int encdec = 0;
59                 STRING_ITERATOR(fragmentiterator, fragment, 0);
60                 ydec_single(fragmentiterator, encdec);
61
62                 EXPECT_EQ(expect, encdec);
63         }
64         SUCCEED();
65 }