xref: /freebsd/usr.sbin/makefs/cd9660/cd9660_strings.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*	$NetBSD: cd9660_strings.c,v 1.4 2007/01/16 17:32:05 hubertf 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 #include <sys/mount.h>
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <ctype.h>
41 
42 #include "makefs.h"
43 #include "cd9660.h"
44 
45 
46 void
47 cd9660_uppercase_characters(char *str, int len)
48 {
49 	int p;
50 
51 	for (p = 0; p < len; p++) {
52 		if (islower((unsigned char)str[p]) )
53 			str[p] -= 32;
54 	}
55 }
56 
57 static inline int
58 cd9660_is_d_char(char c)
59 {
60 	return (isupper((unsigned char)c)
61 		|| c == '_'
62 		|| (c >= '0' && c <= '9'));
63 }
64 
65 static inline int
66 cd9660_is_a_char(char c)
67 {
68 	return (isupper((unsigned char)c)
69 			|| c == '_'
70 			|| (c >= '%' && c <= '?')
71 			|| (c >= ' ' && c <= '\"'));
72 }
73 
74 /*
75  * Test a string to see if it is composed of valid a characters
76  * @param const char* The string to test
77  * @returns int 1 if valid, 2 if valid if characters are converted to
78  *              upper case, 0 otherwise
79  */
80 int
81 cd9660_valid_a_chars(const char *str)
82 {
83 	const char *c = str;
84 	int upperFound = 0;
85 
86 	while ((*c) != '\0') {
87 		if (!(cd9660_is_a_char(*c))) {
88 			if (islower((unsigned char)*c) )
89 				upperFound = 1;
90 			else
91 				return 0;
92 		}
93 		c++;
94 	}
95 	return upperFound + 1;
96 }
97 
98 /*
99  * Test a string to see if it is composed of valid d characters
100  * @param const char* The string to test
101  * @returns int 1 if valid, 2 if valid if characters are converted to
102  *                upper case, 0 otherwise
103  */
104 int
105 cd9660_valid_d_chars(const char *str)
106 {
107 	const char *c=str;
108 	int upperFound = 0;
109 
110 	while ((*c) != '\0') {
111 		if (!(cd9660_is_d_char(*c))) {
112 			if (islower((unsigned char)*c) )
113 				upperFound = 1;
114 			else
115 				return 0;
116 		}
117 		c++;
118 	}
119 	return upperFound + 1;
120 }
121