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