wininet: Correctly set headers size in CommitUrlCacheEntryW function.
[wine] / dlls / 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 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
24
25 /*
26  * This file implements algorithm for decoding scripts encoded by
27  * screnc.exe. The 'secret' algorithm that's well documented here:
28  * http://www.virtualconspiracy.com/content/articles/breaking-screnc
29  */
30
31 static const unsigned char pick_encoding[64] = {
32     1,2,0,1,2,0,2,0,0,2,0,2,1,0,2,0,
33     1,0,2,0,1,1,2,0,0,2,1,0,2,0,0,2,
34     1,1,0,2,0,2,0,1,0,1,1,2,0,1,0,2,
35     1,0,2,0,1,1,2,0,0,1,1,2,0,1,0,2};
36
37 static const unsigned char dictionary[][3] = {
38     {0x00,0x00,0x00}, {0x01,0x01,0x01}, {0x02,0x02,0x02}, {0x03,0x03,0x03},
39     {0x04,0x04,0x04}, {0x05,0x05,0x05}, {0x06,0x06,0x06}, {0x07,0x07,0x07},
40     {0x08,0x08,0x08}, {0x7b,0x57,0x6e}, {0x0a,0x0a,0x0a}, {0x0b,0x0b,0x0b},
41     {0x0c,0x0c,0x0c}, {0x0d,0x0d,0x0d}, {0x0e,0x0e,0x0e}, {0x0f,0x0f,0x0f},
42     {0x10,0x10,0x10}, {0x11,0x11,0x11}, {0x12,0x12,0x12}, {0x13,0x13,0x13},
43     {0x14,0x14,0x14}, {0x15,0x15,0x15}, {0x16,0x16,0x16}, {0x17,0x17,0x17},
44     {0x18,0x18,0x18}, {0x19,0x19,0x19}, {0x1a,0x1a,0x1a}, {0x1b,0x1b,0x1b},
45     {0x1c,0x1c,0x1c}, {0x1d,0x1d,0x1d}, {0x1e,0x1e,0x1e}, {0x1f,0x1f,0x1f},
46     {0x32,0x2e,0x2d}, {0x30,0x47,0x75}, {0x21,0x7a,0x52}, {0x29,0x56,0x60},
47     {0x5b,0x42,0x71}, {0x38,0x6a,0x5e}, {0x33,0x2f,0x49}, {0x3d,0x26,0x5c},
48     {0x58,0x49,0x62}, {0x3a,0x41,0x7d}, {0x35,0x34,0x29}, {0x65,0x32,0x36},
49     {0x39,0x5b,0x20}, {0x5c,0x76,0x7c}, {0x56,0x72,0x7a}, {0x73,0x43,0x7f},
50     {0x66,0x38,0x6b}, {0x4e,0x39,0x63}, {0x45,0x70,0x33}, {0x6b,0x45,0x2b},
51     {0x62,0x68,0x68}, {0x59,0x71,0x51}, {0x78,0x4f,0x66}, {0x5e,0x09,0x76},
52     {0x7d,0x62,0x31}, {0x4a,0x44,0x64}, {0x6d,0x23,0x54}, {0x71,0x75,0x43},
53     {0x00,0x00,0x00}, {0x60,0x7e,0x3a}, {0x00,0x00,0x00}, {0x53,0x5e,0x7e},
54     {0x00,0x00,0x00}, {0x42,0x77,0x45}, {0x27,0x4a,0x2c}, {0x48,0x61,0x2a},
55     {0x72,0x5d,0x74}, {0x75,0x22,0x27}, {0x31,0x4b,0x37}, {0x37,0x6f,0x44},
56     {0x4d,0x4e,0x79}, {0x52,0x3b,0x59}, {0x22,0x4c,0x2f}, {0x54,0x50,0x6f},
57     {0x6a,0x67,0x26}, {0x47,0x2a,0x72}, {0x64,0x7d,0x6a}, {0x2d,0x74,0x39},
58     {0x20,0x54,0x7b}, {0x7f,0x2b,0x3f}, {0x2e,0x2d,0x38}, {0x4c,0x2c,0x77},
59     {0x5d,0x30,0x67}, {0x7e,0x6e,0x53}, {0x6c,0x6b,0x47}, {0x6f,0x66,0x34},
60     {0x79,0x35,0x78}, {0x74,0x25,0x5d}, {0x43,0x21,0x30}, {0x26,0x64,0x23},
61     {0x76,0x4d,0x5a}, {0x25,0x52,0x5b}, {0x24,0x63,0x6c}, {0x2b,0x3f,0x48},
62     {0x28,0x7b,0x55}, {0x23,0x78,0x70}, {0x41,0x29,0x69}, {0x34,0x28,0x2e},
63     {0x09,0x73,0x4c}, {0x2a,0x59,0x21}, {0x44,0x33,0x24}, {0x3f,0x7f,0x4e},
64     {0x77,0x6d,0x50}, {0x3b,0x55,0x09}, {0x55,0x53,0x56}, {0x69,0x7c,0x73},
65     {0x61,0x3a,0x35}, {0x63,0x5f,0x61}, {0x50,0x65,0x4b}, {0x67,0x46,0x58},
66     {0x51,0x58,0x3b}, {0x49,0x31,0x57}, {0x4f,0x69,0x22}, {0x46,0x6c,0x6d},
67     {0x68,0x5a,0x4d}, {0x7c,0x48,0x25}, {0x36,0x27,0x28}, {0x70,0x5c,0x46},
68     {0x6e,0x3d,0x4a}, {0x7a,0x24,0x32}, {0x2f,0x79,0x41}, {0x5f,0x37,0x3d},
69     {0x4b,0x60,0x5f}, {0x5a,0x51,0x4f}, {0x2c,0x20,0x42}, {0x57,0x36,0x65}};
70
71 static const int digits[] = {
72     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77     0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
78     0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
79     0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80     0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
81     0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
82     0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
83     0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
84     0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
85     0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
86     0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
87     0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff};
88
89 static BOOL decode_dword(const WCHAR *p, DWORD *ret)
90 {
91     DWORD i;
92
93     for(i=0; i<6; i++) {
94         if(p[i] > sizeof(digits)/sizeof(*digits) || digits[p[i]] == 0xff)
95             return FALSE;
96     }
97     if(p[6] != '=' || p[7] != '=')
98         return FALSE;
99
100     *ret = (digits[p[0]] << 2)
101         + (digits[p[1]] >> 4)
102         + ((digits[p[1]] & 0xf) << 12)
103         + ((digits[p[2]] >> 2) << 8)
104         + ((digits[p[2]] & 0x3) << 22)
105         + (digits[p[3]] << 16)
106         + ((digits[p[4]] << 2) << 24)
107         + ((digits[p[5]] >> 4) << 24);
108     return TRUE;
109 }
110
111 HRESULT decode_source(WCHAR *code)
112 {
113     const WCHAR *src = code;
114     WCHAR *dst = code;
115
116     static const WCHAR decode_beginW[] = {'#','@','~','^'};
117     static const WCHAR decode_endW[] = {'^','#','~','@'};
118
119     while(*src) {
120         if(!strncmpW(src, decode_beginW, sizeof(decode_beginW)/sizeof(*decode_beginW))) {
121             DWORD len, i, j=0, csum, s=0;
122
123             src += sizeof(decode_beginW)/sizeof(*decode_beginW);
124
125             if(!decode_dword(src, &len))
126                 return JS_E_INVALID_CHAR;
127
128             src += 8;
129
130             for(i=0; i<len; i++) {
131                 if (src[i] == '@') {
132                     switch(src[++i]) {
133                     case '#':
134                         s += dst[j++] = '\r';
135                         break;
136                     case '&':
137                         s += dst[j++] = '\n';
138                         break;
139                     case '!':
140                         s += dst[j++] = '<';
141                         break;
142                     case '*':
143                         s += dst[j++] = '>';
144                         break;
145                     case '$':
146                         s += dst[j++] = '@';
147                         break;
148                     default:
149                         FIXME("unescape %c\n", src[i]);
150                         return E_FAIL;
151                     }
152                 }else if (src[i] < 128) {
153                     s += dst[j] = dictionary[src[i]][pick_encoding[j%64]];
154                     j++;
155                 }else {
156                     FIXME("Unsupported char %c\n", src[i]);
157                     return E_FAIL;
158                 }
159             }
160
161             src += len;
162             dst += j;
163
164             if(!decode_dword(src, &csum) || s != csum)
165                 return JS_E_INVALID_CHAR;
166             src += 8;
167
168             if(strncmpW(src, decode_endW, sizeof(decode_endW)/sizeof(*decode_endW)))
169                 return JS_E_INVALID_CHAR;
170             src += sizeof(decode_endW)/sizeof(*decode_endW);
171         }else {
172             *dst++ = *src++;
173         }
174     }
175
176     *dst = 0;
177
178     TRACE("decoded %s\n", debugstr_w(code));
179     return S_OK;
180 }