source: npl/internetserver/poptop_ppp/patches/ppp-2.4.3-winbind.base64.patch @ 0105685

gcc484ntopperl-5.22
Last change on this file since 0105685 was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[c5c522c]1--- ppp-2.4.3/pppd/plugins/winbind.c.orig       2005-05-12 14:18:10.000000000 -0700
2+++ ppp-2.4.3/pppd/plugins/winbind.c    2005-05-12 14:37:59.000000000 -0700
3@@ -200,50 +200,41 @@
4        return num_chars;
5 }
6 
7-static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
8+static const char *codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9 
10 /**
11  * Encode a base64 string into a malloc()ed string caller to free.
12- *
13- *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
14  **/
15 char * base64_encode(const char *data)
16 {
17-       int bits = 0;
18-       int char_count = 0;
19-       size_t out_cnt = 0;
20-       size_t len = strlen(data);
21-       size_t output_len = strlen(data) * 2;
22-       char *result = malloc(output_len); /* get us plenty of space */
23-
24-       while (len-- && out_cnt < (output_len) - 5) {
25-               int c = (unsigned char) *(data++);
26-               bits += c;
27-               char_count++;
28-               if (char_count == 3) {
29-                       result[out_cnt++] = b64[bits >> 18];
30-                       result[out_cnt++] = b64[(bits >> 12) & 0x3f];
31-                       result[out_cnt++] = b64[(bits >> 6) & 0x3f];
32-           result[out_cnt++] = b64[bits & 0x3f];
33-           bits = 0;
34-           char_count = 0;
35-       } else {
36-           bits <<= 8;
37-       }
38+    int i;
39+    size_t len = strlen(data);
40+    size_t leven = 3*(len / 3);
41+    size_t output_len = 4 * ((len + 2) / 3) + 1;
42+    char *p, *result = malloc(output_len); /* get us plenty of space */
43+    const char *in;
44+    p = result;
45+    in = data;
46+    for (i = 0; i < leven; i += 3)
47+    {
48+        *p++ = codes[(in[0] >> 2) & 0x3F];
49+        *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
50+        *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
51+        *p++ = codes[in[2] & 0x3F];
52+        in += 3;
53     }
54-    if (char_count != 0) {
55-       bits <<= 16 - (8 * char_count);
56-       result[out_cnt++] = b64[bits >> 18];
57-       result[out_cnt++] = b64[(bits >> 12) & 0x3f];
58-       if (char_count == 1) {
59-           result[out_cnt++] = '=';
60-           result[out_cnt++] = '=';
61-       } else {
62-           result[out_cnt++] = b64[(bits >> 6) & 0x3f];
63-           result[out_cnt++] = '=';
64-       }
65+    /* Pad it if necessary...  */
66+    if (i < len)
67+    {
68+        unsigned a = in[0];
69+        unsigned b = (i+1 < len) ? in[1] : 0;
70+        *p++ = codes[(a >> 2) & 0x3F];
71+        *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
72+        *p++ = (i+1 < len) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
73+        *p++ = '=';
74     }
75-    result[out_cnt] = '\0';    /* terminate */
76+    /* append a NULL byte */
77+    *p = '\0';
78     return result;
79 }
80 
Note: See TracBrowser for help on using the repository browser.