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