1/*	$NetBSD: cd9660_strings.c,v 1.6 2015/12/24 15:52:37 christos Exp $	*/
2
3/*
4 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5 * Perez-Rathke and Ram Vedam.  All rights reserved.
6 *
7 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8 * Alan Perez-Rathke and Ram Vedam.
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 *    copyright notice, this list of conditions and the following
17 *    disclaimer in the documentation and/or other materials provided
18 *    with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 */
34
35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h"
37#else
38#include <sys/mount.h>
39#endif
40
41#include <sys/cdefs.h>
42#include <sys/param.h>
43#include <ctype.h>
44
45#include "makefs.h"
46#include "cd9660.h"
47
48#if defined(__RCSID) && !defined(__lint)
49__RCSID("$NetBSD: cd9660_strings.c,v 1.6 2015/12/24 15:52:37 christos Exp $");
50#endif  /* !__lint */
51
52
53void
54cd9660_uppercase_characters(char *str, size_t len)
55{
56	size_t p;
57
58	for (p = 0; p < len; p++) {
59		if (islower((unsigned char)str[p]) )
60			str[p] -= 32;
61	}
62}
63
64static inline int
65cd9660_is_d_char(char c)
66{
67	return (isupper((unsigned char)c)
68		|| c == '_'
69		|| (c >= '0' && c <= '9'));
70}
71
72static inline int
73cd9660_is_a_char(char c)
74{
75	return (isupper((unsigned char)c)
76			|| c == '_'
77			|| (c >= '%' && c <= '?')
78			|| (c >= ' ' && c <= '\"'));
79}
80
81/*
82 * Test a string to see if it is composed of valid a characters
83 * @param const char* The string to test
84 * @returns int 1 if valid, 2 if valid if characters are converted to
85 *              upper case, 0 otherwise
86 */
87int
88cd9660_valid_a_chars(const char *str)
89{
90	const char *c = str;
91	int upperFound = 0;
92
93	while ((*c) != '\0') {
94		if (!(cd9660_is_a_char(*c))) {
95			if (islower((unsigned char)*c) )
96				upperFound = 1;
97			else
98				return 0;
99		}
100		c++;
101	}
102	return upperFound + 1;
103}
104
105/*
106 * Test a string to see if it is composed of valid d characters
107 * @param const char* The string to test
108 * @returns int 1 if valid, 2 if valid if characters are converted to
109 *                upper case, 0 otherwise
110 */
111int
112cd9660_valid_d_chars(const char *str)
113{
114	const char *c=str;
115	int upperFound = 0;
116
117	while ((*c) != '\0') {
118		if (!(cd9660_is_d_char(*c))) {
119			if (islower((unsigned char)*c) )
120				upperFound = 1;
121			else
122				return 0;
123		}
124		c++;
125	}
126	return upperFound + 1;
127}
128