~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to ftp_lite/src/radix.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
/*
 
9
This product includes software developed by and/or derived
 
10
from the Globus Project (http://www.globus.org/)
 
11
to which the U.S. Government retains certain rights.
 
12
*/
 
13
 
 
14
/*
 
15
This file in particular was adapted from the Globus toolkit.
 
16
These functions encode binary data in base 64 ascii.  They
 
17
were adapted from similarly-named functions in globus_ftp_control_client.c
 
18
*/
 
19
 
 
20
 
 
21
#include "ftp_lite.h"
 
22
#include <string.h>
 
23
 
 
24
static char *radixN =
 
25
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
26
 
 
27
static char pad = '=';
 
28
 
 
29
int ftp_lite_radix_encode( const unsigned char * inbuf, unsigned char * outbuf, int * length )
 
30
{
 
31
        int i;
 
32
        int j;
 
33
        unsigned char c=0;
 
34
        
 
35
        for (i=0,j=0; i < *length; i++)
 
36
        {
 
37
                switch (i%3) 
 
38
                {
 
39
                case 0:
 
40
                        outbuf[j++] = radixN[inbuf[i]>>2];
 
41
                        c = (inbuf[i]&3)<<4;
 
42
                        break;
 
43
                case 1:
 
44
                        outbuf[j++] = radixN[c|inbuf[i]>>4];
 
45
                        c = (inbuf[i]&15)<<2;
 
46
                        break;
 
47
                case 2:
 
48
                        outbuf[j++] = radixN[c|inbuf[i]>>6];
 
49
                        outbuf[j++] = radixN[inbuf[i]&63];
 
50
                        c = 0;
 
51
                }
 
52
        }
 
53
        
 
54
        if (i%3) 
 
55
        {
 
56
                outbuf[j++] = radixN[c];
 
57
        }
 
58
        
 
59
        switch (i%3) 
 
60
        {
 
61
        case 1: 
 
62
                outbuf[j++] = pad;
 
63
        case 2: 
 
64
                outbuf[j++] = pad;
 
65
        }
 
66
        
 
67
        outbuf[*length = j] = '\0';
 
68
 
 
69
        return 1;
 
70
}
 
71
 
 
72
int ftp_lite_radix_decode( const unsigned char *inbuf, unsigned char *outbuf, int * length )
 
73
{
 
74
        int i;
 
75
        int j;
 
76
        int D;
 
77
        char *p;
 
78
 
 
79
        for (i=0,j=0; inbuf[i] && inbuf[i] != pad; i++) 
 
80
        {
 
81
 
 
82
                if ((p = strchr(radixN, inbuf[i])) == NULL) 
 
83
                {
 
84
                        return 0;
 
85
                }
 
86
 
 
87
                D = p - radixN;
 
88
                switch (i&3) 
 
89
                {
 
90
                case 0:
 
91
                        outbuf[j] = D<<2;
 
92
                        break;
 
93
                case 1:
 
94
                        outbuf[j++] |= D>>4;
 
95
                        outbuf[j] = (D&15)<<4;
 
96
                        break;
 
97
                case 2:
 
98
                        outbuf[j++] |= D>>2;
 
99
                        outbuf[j] = (D&3)<<6;
 
100
                        break;
 
101
                case 3:
 
102
                        outbuf[j++] |= D;
 
103
                }
 
104
        }
 
105
        switch (i&3) 
 
106
        {
 
107
        case 1: 
 
108
                return 0;
 
109
        case 2: 
 
110
                if (D&15)
 
111
                {
 
112
                        return 0;
 
113
                }
 
114
                if (strcmp((char *)&inbuf[i], "=="))
 
115
                {
 
116
                        return 0;
 
117
                }
 
118
                break;
 
119
        case 3: 
 
120
                if (D&3) 
 
121
                {
 
122
                        return 0;
 
123
                }
 
124
                if (strcmp((char *)&inbuf[i], "=")) 
 
125
                {
 
126
                        return 0;
 
127
                }
 
128
        }
 
129
        *length = j;
 
130
 
 
131
        return 1;
 
132
}