wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / secur32 / base64_codec.c
blob9f5eaecc3d47e8b8a51536b2dd5695ed27d87159
1 /*
2 * base64 encoder/decoder
4 * Copyright 2005 by Kai Blin
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "windef.h"
22 #include "winerror.h"
23 #include "sspi.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
28 static const char b64[] =
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31 SECURITY_STATUS encodeBase64(PBYTE in_buf, int in_len, char* out_buf,
32 int max_len, int *out_len)
34 int div, i;
35 PBYTE d = in_buf;
36 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
38 TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
39 *out_len = bytes + pad_bytes;
41 if(bytes + pad_bytes + 1 > max_len)
42 return SEC_E_BUFFER_TOO_SMALL;
44 /* Three bytes of input give 4 chars of output */
45 div = in_len / 3;
47 i = 0;
48 while(div > 0)
50 /* first char is the first 6 bits of the first byte*/
51 out_buf[i + 0] = b64[ ( d[0] >> 2) & 0x3f ];
52 /* second char is the last 2 bits of the first byte and the first 4
53 * bits of the second byte */
54 out_buf[i + 1] = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
55 /* third char is the last 4 bits of the second byte and the first 2
56 * bits of the third byte */
57 out_buf[i + 2] = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
58 /* fourth char is the remaining 6 bits of the third byte */
59 out_buf[i + 3] = b64[ d[2] & 0x3f];
60 i += 4;
61 d += 3;
62 div--;
65 switch(pad_bytes)
67 case 1:
68 /* first char is the first 6 bits of the first byte*/
69 out_buf[i + 0] = b64[ ( d[0] >> 2) & 0x3f ];
70 /* second char is the last 2 bits of the first byte and the first 4
71 * bits of the second byte */
72 out_buf[i + 1] = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
73 /* third char is the last 4 bits of the second byte padded with
74 * two zeroes */
75 out_buf[i + 2] = b64[ ((d[1] << 2) & 0x3c) ];
76 /* fourth char is a = to indicate one byte of padding */
77 out_buf[i + 3] = '=';
78 out_buf[i + 4] = 0;
79 break;
80 case 2:
81 /* first char is the first 6 bits of the first byte*/
82 out_buf[i + 0] = b64[ ( d[0] >> 2) & 0x3f ];
83 /* second char is the last 2 bits of the first byte padded with
84 * four zeroes*/
85 out_buf[i + 1] = b64[ ((d[0] << 4) & 0x30)];
86 /* third char is = to indicate padding */
87 out_buf[i + 2] = '=';
88 /* fourth char is = to indicate padding */
89 out_buf[i + 3] = '=';
90 out_buf[i + 4] = 0;
91 break;
92 default:
93 out_buf[i] = 0;
96 return SEC_E_OK;
99 static inline BYTE decode(char c)
101 if( c >= 'A' && c <= 'Z')
102 return c - 'A';
103 if( c >= 'a' && c <= 'z')
104 return c - 'a' + 26;
105 if( c >= '0' && c <= '9')
106 return c - '0' + 52;
107 if( c == '+')
108 return 62;
109 if( c == '/')
110 return 63;
111 else
112 return 64;
115 SECURITY_STATUS decodeBase64(char *in_buf, int in_len, PBYTE out_buf,
116 int max_len, int *out_len)
118 int len = in_len, i;
119 char *d = in_buf;
120 int ip0, ip1, ip2, ip3;
122 TRACE("in_len: %d\n", in_len);
124 if((in_len % 4) != 0)
125 return SEC_E_INVALID_TOKEN;
127 if(in_len > max_len)
128 return SEC_E_BUFFER_TOO_SMALL;
130 i = 0;
131 while(len > 4)
133 if((ip0 = decode(d[0])) > 63)
134 return SEC_E_INVALID_TOKEN;
135 if((ip1 = decode(d[1])) > 63)
136 return SEC_E_INVALID_TOKEN;
137 if((ip2 = decode(d[2])) > 63)
138 return SEC_E_INVALID_TOKEN;
139 if((ip3 = decode(d[3])) > 63)
140 return SEC_E_INVALID_TOKEN;
142 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
143 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
144 out_buf[i + 2] = (ip2 << 6) | ip3;
145 len -= 4;
146 i += 3;
147 d += 4;
150 if(d[2] == '=')
152 if((ip0 = decode(d[0])) > 63)
153 return SEC_E_INVALID_TOKEN;
154 if((ip1 = decode(d[1])) > 63)
155 return SEC_E_INVALID_TOKEN;
157 out_buf[i] = (ip0 << 2) | (ip1 >> 4);
158 i++;
160 else if(d[3] == '=')
162 if((ip0 = decode(d[0])) > 63)
163 return SEC_E_INVALID_TOKEN;
164 if((ip1 = decode(d[1])) > 63)
165 return SEC_E_INVALID_TOKEN;
166 if((ip2 = decode(d[2])) > 63)
167 return SEC_E_INVALID_TOKEN;
169 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
170 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
171 i += 2;
173 else
175 if((ip0 = decode(d[0])) > 63)
176 return SEC_E_INVALID_TOKEN;
177 if((ip1 = decode(d[1])) > 63)
178 return SEC_E_INVALID_TOKEN;
179 if((ip2 = decode(d[2])) > 63)
180 return SEC_E_INVALID_TOKEN;
181 if((ip3 = decode(d[3])) > 63)
182 return SEC_E_INVALID_TOKEN;
185 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
186 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
187 out_buf[i + 2] = (ip2 << 6) | ip3;
188 i += 3;
190 *out_len = i;
191 return SEC_E_OK;