xref: /freebsd/sys/cam/cam.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*
2  * Generic utility routines for the Common Access Method layer.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      $Id$
29  */
30 #include <sys/param.h>
31 
32 #include <cam/cam.h>
33 
34 void
35 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
36 {
37 
38 	/* Trim leading/trailing spaces. */
39 	while (srclen > 0 && src[0] == ' ')
40 		src++, srclen--;
41 	while (srclen > 0 && src[srclen-1] == ' ')
42 		srclen--;
43 
44 	while (srclen > 0 && dstlen > 1) {
45 		u_int8_t *cur_pos = dst;
46 
47 		if (*src < 0x20 || *src >= 0x80) {
48 			/* SCSI-II Specifies that these should never occur. */
49 			/* non-printable character */
50 			if (dstlen > 4) {
51 				*cur_pos++ = '\\';
52 				*cur_pos++ = ((*src & 0300) >> 6) + '0';
53 				*cur_pos++ = ((*src & 0070) >> 3) + '0';
54 				*cur_pos++ = ((*src & 0007) >> 0) + '0';
55 			} else {
56 				*cur_pos++ = '?';
57 			}
58 		} else {
59 			/* normal character */
60 			*cur_pos++ = *src;
61 		}
62 		src++;
63 		srclen--;
64 		dstlen -= cur_pos - dst;
65 		dst = cur_pos;
66 	}
67 	*dst = '\0';
68 }
69 
70 /*
71  * Compare string with pattern, returning 0 on match.
72  * Short pattern matches trailing blanks in name,
73  * wildcard '*' in pattern matches rest of name,
74  * wildcard '?' matches a single non-space character.
75  */
76 int
77 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
78 {
79 
80 	while (*pattern != '\0'&& str_len > 0) {
81 
82 		if (*pattern == '*') {
83 			return (0);
84 		}
85 		if ((*pattern != *str)
86 		 && (*pattern != '?' || *str == ' ')) {
87 			return (1);
88 		}
89 		pattern++;
90 		str++;
91 		str_len--;
92 	}
93 	while (str_len > 0 && *str++ == ' ')
94 		str_len--;
95 
96 	return (str_len);
97 }
98 
99 caddr_t
100 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
101 	       int entry_size, cam_quirkmatch_t *comp_func)
102 {
103 	for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
104 		if ((*comp_func)(target, quirk_table) == 0)
105 			return (quirk_table);
106 	}
107 	return (NULL);
108 }
109