[PSAPI_WINETEST] Sync with Wine Staging 2.16. CORE-13762
[reactos.git] / dll / win32 / jscript / decode.c
1 /*
2 * Copyright 2012 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "jscript.h"
20
21 /*
22 * This file implements algorithm for decoding scripts encoded by
23 * screnc.exe. The 'secret' algorithm that's well documented here:
24 * http://www.virtualconspiracy.com/content/articles/breaking-screnc
25 */
26
27 static const unsigned char pick_encoding[64] = {
28 1,2,0,1,2,0,2,0,0,2,0,2,1,0,2,0,
29 1,0,2,0,1,1,2,0,0,2,1,0,2,0,0,2,
30 1,1,0,2,0,2,0,1,0,1,1,2,0,1,0,2,
31 1,0,2,0,1,1,2,0,0,1,1,2,0,1,0,2};
32
33 static const unsigned char dictionary[][3] = {
34 {0x00,0x00,0x00}, {0x01,0x01,0x01}, {0x02,0x02,0x02}, {0x03,0x03,0x03},
35 {0x04,0x04,0x04}, {0x05,0x05,0x05}, {0x06,0x06,0x06}, {0x07,0x07,0x07},
36 {0x08,0x08,0x08}, {0x7b,0x57,0x6e}, {0x0a,0x0a,0x0a}, {0x0b,0x0b,0x0b},
37 {0x0c,0x0c,0x0c}, {0x0d,0x0d,0x0d}, {0x0e,0x0e,0x0e}, {0x0f,0x0f,0x0f},
38 {0x10,0x10,0x10}, {0x11,0x11,0x11}, {0x12,0x12,0x12}, {0x13,0x13,0x13},
39 {0x14,0x14,0x14}, {0x15,0x15,0x15}, {0x16,0x16,0x16}, {0x17,0x17,0x17},
40 {0x18,0x18,0x18}, {0x19,0x19,0x19}, {0x1a,0x1a,0x1a}, {0x1b,0x1b,0x1b},
41 {0x1c,0x1c,0x1c}, {0x1d,0x1d,0x1d}, {0x1e,0x1e,0x1e}, {0x1f,0x1f,0x1f},
42 {0x32,0x2e,0x2d}, {0x30,0x47,0x75}, {0x21,0x7a,0x52}, {0x29,0x56,0x60},
43 {0x5b,0x42,0x71}, {0x38,0x6a,0x5e}, {0x33,0x2f,0x49}, {0x3d,0x26,0x5c},
44 {0x58,0x49,0x62}, {0x3a,0x41,0x7d}, {0x35,0x34,0x29}, {0x65,0x32,0x36},
45 {0x39,0x5b,0x20}, {0x5c,0x76,0x7c}, {0x56,0x72,0x7a}, {0x73,0x43,0x7f},
46 {0x66,0x38,0x6b}, {0x4e,0x39,0x63}, {0x45,0x70,0x33}, {0x6b,0x45,0x2b},
47 {0x62,0x68,0x68}, {0x59,0x71,0x51}, {0x78,0x4f,0x66}, {0x5e,0x09,0x76},
48 {0x7d,0x62,0x31}, {0x4a,0x44,0x64}, {0x6d,0x23,0x54}, {0x71,0x75,0x43},
49 {0x00,0x00,0x00}, {0x60,0x7e,0x3a}, {0x00,0x00,0x00}, {0x53,0x5e,0x7e},
50 {0x00,0x00,0x00}, {0x42,0x77,0x45}, {0x27,0x4a,0x2c}, {0x48,0x61,0x2a},
51 {0x72,0x5d,0x74}, {0x75,0x22,0x27}, {0x31,0x4b,0x37}, {0x37,0x6f,0x44},
52 {0x4d,0x4e,0x79}, {0x52,0x3b,0x59}, {0x22,0x4c,0x2f}, {0x54,0x50,0x6f},
53 {0x6a,0x67,0x26}, {0x47,0x2a,0x72}, {0x64,0x7d,0x6a}, {0x2d,0x74,0x39},
54 {0x20,0x54,0x7b}, {0x7f,0x2b,0x3f}, {0x2e,0x2d,0x38}, {0x4c,0x2c,0x77},
55 {0x5d,0x30,0x67}, {0x7e,0x6e,0x53}, {0x6c,0x6b,0x47}, {0x6f,0x66,0x34},
56 {0x79,0x35,0x78}, {0x74,0x25,0x5d}, {0x43,0x21,0x30}, {0x26,0x64,0x23},
57 {0x76,0x4d,0x5a}, {0x25,0x52,0x5b}, {0x24,0x63,0x6c}, {0x2b,0x3f,0x48},
58 {0x28,0x7b,0x55}, {0x23,0x78,0x70}, {0x41,0x29,0x69}, {0x34,0x28,0x2e},
59 {0x09,0x73,0x4c}, {0x2a,0x59,0x21}, {0x44,0x33,0x24}, {0x3f,0x7f,0x4e},
60 {0x77,0x6d,0x50}, {0x3b,0x55,0x09}, {0x55,0x53,0x56}, {0x69,0x7c,0x73},
61 {0x61,0x3a,0x35}, {0x63,0x5f,0x61}, {0x50,0x65,0x4b}, {0x67,0x46,0x58},
62 {0x51,0x58,0x3b}, {0x49,0x31,0x57}, {0x4f,0x69,0x22}, {0x46,0x6c,0x6d},
63 {0x68,0x5a,0x4d}, {0x7c,0x48,0x25}, {0x36,0x27,0x28}, {0x70,0x5c,0x46},
64 {0x6e,0x3d,0x4a}, {0x7a,0x24,0x32}, {0x2f,0x79,0x41}, {0x5f,0x37,0x3d},
65 {0x4b,0x60,0x5f}, {0x5a,0x51,0x4f}, {0x2c,0x20,0x42}, {0x57,0x36,0x65}};
66
67 static const int digits[] = {
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
74 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
75 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
77 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
78 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
79 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
80 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
81 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
82 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
83 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff};
84
85 static BOOL decode_dword(const WCHAR *p, DWORD *ret)
86 {
87 DWORD i;
88
89 for(i=0; i<6; i++) {
90 if(p[i] >= sizeof(digits)/sizeof(*digits) || digits[p[i]] == 0xff)
91 return FALSE;
92 }
93 if(p[6] != '=' || p[7] != '=')
94 return FALSE;
95
96 *ret = (digits[p[0]] << 2)
97 + (digits[p[1]] >> 4)
98 + ((digits[p[1]] & 0xf) << 12)
99 + ((digits[p[2]] >> 2) << 8)
100 + ((digits[p[2]] & 0x3) << 22)
101 + (digits[p[3]] << 16)
102 + ((digits[p[4]] << 2) << 24)
103 + ((digits[p[5]] >> 4) << 24);
104 return TRUE;
105 }
106
107 HRESULT decode_source(WCHAR *code)
108 {
109 const WCHAR *src = code;
110 WCHAR *dst = code;
111
112 static const WCHAR decode_beginW[] = {'#','@','~','^'};
113 static const WCHAR decode_endW[] = {'^','#','~','@'};
114
115 while(*src) {
116 if(!strncmpW(src, decode_beginW, sizeof(decode_beginW)/sizeof(*decode_beginW))) {
117 DWORD len, i, j=0, csum, s=0;
118
119 src += sizeof(decode_beginW)/sizeof(*decode_beginW);
120
121 if(!decode_dword(src, &len))
122 return JS_E_INVALID_CHAR;
123
124 src += 8;
125
126 for(i=0; i<len; i++) {
127 if (src[i] == '@') {
128 switch(src[++i]) {
129 case '#':
130 s += dst[j++] = '\r';
131 break;
132 case '&':
133 s += dst[j++] = '\n';
134 break;
135 case '!':
136 s += dst[j++] = '<';
137 break;
138 case '*':
139 s += dst[j++] = '>';
140 break;
141 case '$':
142 s += dst[j++] = '@';
143 break;
144 default:
145 FIXME("unescape %c\n", src[i]);
146 return E_FAIL;
147 }
148 }else if (src[i] < 128) {
149 s += dst[j] = dictionary[src[i]][pick_encoding[j%64]];
150 j++;
151 }else {
152 FIXME("Unsupported char %c\n", src[i]);
153 return E_FAIL;
154 }
155 }
156
157 src += len;
158 dst += j;
159
160 if(!decode_dword(src, &csum) || s != csum)
161 return JS_E_INVALID_CHAR;
162 src += 8;
163
164 if(strncmpW(src, decode_endW, sizeof(decode_endW)/sizeof(*decode_endW)))
165 return JS_E_INVALID_CHAR;
166 src += sizeof(decode_endW)/sizeof(*decode_endW);
167 }else {
168 *dst++ = *src++;
169 }
170 }
171
172 *dst = 0;
173
174 TRACE("decoded %s\n", debugstr_w(code));
175 return S_OK;
176 }