1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.wifi.aware;
18
19/**
20 * Provides utilities for the Wifi Aware manager/service.
21 *
22 * @hide
23 */
24public class WifiAwareUtils {
25    /**
26     * Per spec: The Service Name is a UTF-8 encoded string from 1 to 255 bytes in length. The
27     * only acceptable single-byte UTF-8 symbols for a Service Name are alphanumeric values (A-Z,
28     * a-z, 0-9), the hyphen ('-'), and the period ('.'). All valid multi-byte UTF-8 characters
29     * are acceptable in a Service Name.
30     */
31    public static void validateServiceName(byte[] serviceNameData) throws IllegalArgumentException {
32        if (serviceNameData == null) {
33            throw new IllegalArgumentException("Invalid service name - null");
34        }
35
36        if (serviceNameData.length < 1 || serviceNameData.length > 255) {
37            throw new IllegalArgumentException("Invalid service name length - must be between "
38                    + "1 and 255 bytes (UTF-8 encoding)");
39        }
40
41        int index = 0;
42        while (index < serviceNameData.length) {
43            byte b = serviceNameData[index];
44            if ((b & 0x80) == 0x00) {
45                if (!((b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
46                        || b == '-' || b == '.')) {
47                    throw new IllegalArgumentException("Invalid service name - illegal characters,"
48                            + " allowed = (0-9, a-z,A-Z, -, .)");
49                }
50            }
51            ++index;
52        }
53    }
54}
55