aboutsummaryrefslogtreecommitdiff
path: root/src/com/novell/sasl/client/DirectiveList.java
blob: fc26a6b0ee0a36a5727d712d8494730509c45b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* **************************************************************************
 * $OpenLDAP: /com/novell/sasl/client/DirectiveList.java,v 1.4 2005/01/17 15:00:54 sunilk Exp $
 *
 * Copyright (C) 2002 Novell, Inc. All Rights Reserved.
 *
 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
 * TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
 * TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
 * AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
 * IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
 * OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
 * PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
 * THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
 ******************************************************************************/
package com.novell.sasl.client;

import java.util.*;
import org.apache.harmony.javax.security.sasl.*;
import java.io.UnsupportedEncodingException;

/**
 * Implements the DirectiveList class whihc will be used by the 
 * DigestMD5SaslClient class
 */
class DirectiveList extends Object
{
    private static final int STATE_LOOKING_FOR_FIRST_DIRECTIVE  = 1;
    private static final int STATE_LOOKING_FOR_DIRECTIVE        = 2;
    private static final int STATE_SCANNING_NAME                = 3;
    private static final int STATE_LOOKING_FOR_EQUALS            = 4;
    private static final int STATE_LOOKING_FOR_VALUE            = 5;
    private static final int STATE_LOOKING_FOR_COMMA            = 6;
    private static final int STATE_SCANNING_QUOTED_STRING_VALUE    = 7;
    private static final int STATE_SCANNING_TOKEN_VALUE            = 8;
    private static final int STATE_NO_UTF8_SUPPORT              = 9;

    private int        m_curPos;
    private int        m_errorPos;
    private String     m_directives;
    private int        m_state;
    private ArrayList  m_directiveList;
    private String     m_curName;
    private int        m_scanStart;

    /**
     *  Constructs a new DirectiveList.
     */
     DirectiveList(
        byte[] directives)
    {
        m_curPos = 0;
        m_state = STATE_LOOKING_FOR_FIRST_DIRECTIVE;
        m_directiveList = new ArrayList(10);
        m_scanStart = 0;
        m_errorPos = -1;
        try
        {
            m_directives = new String(directives, "UTF-8");
        }
        catch(UnsupportedEncodingException e)
        {
            m_state = STATE_NO_UTF8_SUPPORT;
        }
    }

    /**
     * This function takes a US-ASCII character string containing a list of comma
     * separated directives, and parses the string into the individual directives
     * and their values. A directive consists of a token specifying the directive
     * name followed by an equal sign (=) and the directive value. The value is
     * either a token or a quoted string
     *
     * @exception SaslException  If an error Occurs
     */
    void parseDirectives() throws SaslException
    {
        char        prevChar;
        char        currChar;
        int            rc = 0;
        boolean        haveQuotedPair = false;
        String      currentName = "<no name>";

        if (m_state == STATE_NO_UTF8_SUPPORT)
            throw new SaslException("No UTF-8 support on platform");

        prevChar = 0;

        while (m_curPos < m_directives.length())
        {
            currChar = m_directives.charAt(m_curPos);
            switch (m_state)
            {
            case STATE_LOOKING_FOR_FIRST_DIRECTIVE:
            case STATE_LOOKING_FOR_DIRECTIVE:
                if (isWhiteSpace(currChar))
                {
                    break;
                }
                else if (isValidTokenChar(currChar))
                {
                    m_scanStart = m_curPos;
                    m_state = STATE_SCANNING_NAME;
                }
                else
                {
                     m_errorPos = m_curPos;
                    throw new SaslException("Parse error: Invalid name character");
                }
                break;

            case STATE_SCANNING_NAME:
                if (isValidTokenChar(currChar))
                {
                    break;
                }
                else if (isWhiteSpace(currChar))
                {
                    currentName = m_directives.substring(m_scanStart, m_curPos);
                    m_state = STATE_LOOKING_FOR_EQUALS;
                }
                else if ('=' == currChar)
                {
                    currentName = m_directives.substring(m_scanStart, m_curPos);
                    m_state = STATE_LOOKING_FOR_VALUE;
                }
                else
                {
                     m_errorPos = m_curPos;
                    throw new SaslException("Parse error: Invalid name character");
                }
                break;

            case STATE_LOOKING_FOR_EQUALS:
                if (isWhiteSpace(currChar))
                {
                    break;
                }
                else if ('=' == currChar)
                {
                    m_state = STATE_LOOKING_FOR_VALUE;
                }
                else
                {
                    m_errorPos = m_curPos;
                    throw new SaslException("Parse error: Expected equals sign '='.");
                }
                break;

            case STATE_LOOKING_FOR_VALUE:
                if (isWhiteSpace(currChar))
                {
                    break;
                }
                else if ('"' == currChar)
                {
                    m_scanStart = m_curPos+1; /* don't include the quote */
                    m_state = STATE_SCANNING_QUOTED_STRING_VALUE;
                }
                else if (isValidTokenChar(currChar))
                {
                    m_scanStart = m_curPos;
                    m_state = STATE_SCANNING_TOKEN_VALUE;
                }
                else
                {
                    m_errorPos = m_curPos;
                    throw new SaslException("Parse error: Unexpected character");
                }
                break;

            case STATE_SCANNING_TOKEN_VALUE:
                if (isValidTokenChar(currChar))
                {
                    break;
                }
                else if (isWhiteSpace(currChar))
                {
                    addDirective(currentName, false);
                    m_state = STATE_LOOKING_FOR_COMMA;
                }
                else if (',' == currChar)
                {
                    addDirective(currentName, false);
                    m_state = STATE_LOOKING_FOR_DIRECTIVE;
                }
                else
                {
                     m_errorPos = m_curPos;
                    throw new SaslException("Parse error: Invalid value character");
                }
                break;

            case STATE_SCANNING_QUOTED_STRING_VALUE:
                if ('\\' == currChar)
                    haveQuotedPair = true;
                if ( ('"' == currChar) &&
                     ('\\' != prevChar) )
                {
                    addDirective(currentName, haveQuotedPair);
                    haveQuotedPair = false;
                    m_state = STATE_LOOKING_FOR_COMMA;
                }
                break;

            case STATE_LOOKING_FOR_COMMA:
                if (isWhiteSpace(currChar))
                    break;
                else if (currChar == ',')
                    m_state = STATE_LOOKING_FOR_DIRECTIVE;
                else
                {
                    m_errorPos = m_curPos;
                    throw new SaslException("Parse error: Expected a comma.");
                }
                break;
            }
            if (0 != rc)
                break;
            prevChar = currChar;
            m_curPos++;
        } /* end while loop */


        if (rc == 0)
        {
            /* check the ending state */
            switch (m_state)
            {
            case STATE_SCANNING_TOKEN_VALUE:
                addDirective(currentName, false);
                break;

            case STATE_LOOKING_FOR_FIRST_DIRECTIVE:
            case STATE_LOOKING_FOR_COMMA:
                break;

            case STATE_LOOKING_FOR_DIRECTIVE:
                    throw new SaslException("Parse error: Trailing comma.");

            case STATE_SCANNING_NAME:
            case STATE_LOOKING_FOR_EQUALS:
            case STATE_LOOKING_FOR_VALUE:
                    throw new SaslException("Parse error: Missing value.");

            case STATE_SCANNING_QUOTED_STRING_VALUE:
                    throw new SaslException("Parse error: Missing closing quote.");
            }
        }

    }

    /**
     * This function returns TRUE if the character is a valid token character.
     *
     *     token          = 1*<any CHAR except CTLs or separators>
     *
     *      separators     = "(" | ")" | "<" | ">" | "@"
     *                     | "," | ";" | ":" | "\" | <">
     *                     | "/" | "[" | "]" | "?" | "="
     *                     | "{" | "}" | SP | HT
     *
     *      CTL            = <any US-ASCII control character
     *                       (octets 0 - 31) and DEL (127)>
     *
     *      CHAR           = <any US-ASCII character (octets 0 - 127)>
     *
     * @param c  character to be tested
     *
     * @return Returns TRUE if the character is a valid token character.
     */
    boolean isValidTokenChar(
        char c)
    {
        if ( ( (c >= '\u0000') && (c <='\u0020') ) ||
             ( (c >= '\u003a') && (c <= '\u0040') ) ||
             ( (c >= '\u005b') && (c <= '\u005d') ) ||
             ('\u002c' == c) ||
             ('\u0025' == c) ||
             ('\u0028' == c) ||
             ('\u0029' == c) ||
             ('\u007b' == c) ||
             ('\u007d' == c) ||
             ('\u007f' == c) )
            return false;

        return true;
    }

    /**
     * This function returns TRUE if the character is linear white space (LWS).
     *         LWS = [CRLF] 1*( SP | HT )
     * @param c  Input charcter to be tested
     *
     * @return Returns TRUE if the character is linear white space (LWS)
     */
    boolean isWhiteSpace(
        char c)
    {
        if ( ('\t' == c) ||  // HORIZONTAL TABULATION.
             ('\n' == c) ||  // LINE FEED.
             ('\r' == c) ||  // CARRIAGE RETURN.
             ('\u0020' == c) )
            return true;

        return false;
    }

    /**
     * This function creates a directive record and adds it to the list, the
     * value will be added later after it is parsed.
     *
     * @param name  Name
     * @param haveQuotedPair true if quoted pair is there else false
     */
    void addDirective(
        String    name,
        boolean   haveQuotedPair)
    {
        String value;
        int    inputIndex;
        int    valueIndex;
        char   valueChar;
        int    type;

        if (!haveQuotedPair)
        {
            value = m_directives.substring(m_scanStart, m_curPos);
        }
        else
        { //copy one character at a time skipping backslash excapes.
            StringBuffer valueBuf = new StringBuffer(m_curPos - m_scanStart);
            valueIndex = 0;
            inputIndex = m_scanStart;
            while (inputIndex < m_curPos)
            {
                if ('\\' == (valueChar = m_directives.charAt(inputIndex)))
                    inputIndex++;
                valueBuf.setCharAt(valueIndex, m_directives.charAt(inputIndex));
                valueIndex++;
                inputIndex++;
            }
            value = new String(valueBuf);
        }

        if (m_state == STATE_SCANNING_QUOTED_STRING_VALUE)
            type = ParsedDirective.QUOTED_STRING_VALUE;
        else
            type = ParsedDirective.TOKEN_VALUE;
        m_directiveList.add(new ParsedDirective(name, value, type));
    }


    /**
     * Returns the List iterator.
     *
     * @return     Returns the Iterator Object for the List.
     */
    Iterator getIterator()
    {
        return m_directiveList.iterator();
    }
}