xref: /netbsd-src/lib/libc/nls/catgets.c (revision 9e66e6d75e9910b3de5f4ef031995955f69a7dd1)
1*9e66e6d7Sabs /*	$NetBSD: catgets.c,v 1.19 2012/06/25 22:32:45 abs Exp $	*/
27ce7ea94Scgd 
3adfd5ba7Sjtc /*-
4adfd5ba7Sjtc  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5adfd5ba7Sjtc  * All rights reserved.
6adfd5ba7Sjtc  *
7adfd5ba7Sjtc  * This code is derived from software contributed to The NetBSD Foundation
8adfd5ba7Sjtc  * by J.T. Conklin.
9adfd5ba7Sjtc  *
10adfd5ba7Sjtc  * Redistribution and use in source and binary forms, with or without
11adfd5ba7Sjtc  * modification, are permitted provided that the following conditions
12adfd5ba7Sjtc  * are met:
13adfd5ba7Sjtc  * 1. Redistributions of source code must retain the above copyright
14adfd5ba7Sjtc  *    notice, this list of conditions and the following disclaimer.
15adfd5ba7Sjtc  * 2. Redistributions in binary form must reproduce the above copyright
16adfd5ba7Sjtc  *    notice, this list of conditions and the following disclaimer in the
17adfd5ba7Sjtc  *    documentation and/or other materials provided with the distribution.
18adfd5ba7Sjtc  *
19adfd5ba7Sjtc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20adfd5ba7Sjtc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21adfd5ba7Sjtc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2287f4ccd4Sjtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2387f4ccd4Sjtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24adfd5ba7Sjtc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25adfd5ba7Sjtc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26adfd5ba7Sjtc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27adfd5ba7Sjtc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28adfd5ba7Sjtc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29adfd5ba7Sjtc  * POSSIBILITY OF SUCH DAMAGE.
3056fa6d53Sjtc  */
3156fa6d53Sjtc 
3288c3eadbSlukem #include <sys/cdefs.h>
3388c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
34*9e66e6d7Sabs __RCSID("$NetBSD: catgets.c,v 1.19 2012/06/25 22:32:45 abs Exp $");
3588c3eadbSlukem #endif /* LIBC_SCCS and not lint */
3688c3eadbSlukem 
37adfd5ba7Sjtc #define _NLS_PRIVATE
3856fa6d53Sjtc 
394515f6e8Schristos #include "namespace.h"
40bf5c90d5Sjtc #include <errno.h>
41adfd5ba7Sjtc #include <stdlib.h>
42adfd5ba7Sjtc #include <string.h>
4356fa6d53Sjtc #include <nl_types.h>
4456fa6d53Sjtc 
459f5f4ac6Smycroft #ifdef __weak_alias
__weak_alias(catgets,_catgets)469f5f4ac6Smycroft __weak_alias(catgets, _catgets)
479f5f4ac6Smycroft #endif
489f5f4ac6Smycroft 
4926d1df90Scgd char *
50*9e66e6d7Sabs _catgets(nl_catd catd, int set_id, int msg_id, const char *s)
5156fa6d53Sjtc {
52adfd5ba7Sjtc 	struct _nls_cat_hdr *cat_hdr;
53adfd5ba7Sjtc 	struct _nls_set_hdr *set_hdr;
54adfd5ba7Sjtc 	struct _nls_msg_hdr *msg_hdr;
55adfd5ba7Sjtc 	int l, u, i, r;
56adfd5ba7Sjtc 
57bf5c90d5Sjtc 	if (catd == (nl_catd) -1) {
58bf5c90d5Sjtc 		errno = EBADF;
5903256c6eSchristos 		return __UNCONST(s);
6056fa6d53Sjtc 	}
6156fa6d53Sjtc 
62adfd5ba7Sjtc 	cat_hdr = (struct _nls_cat_hdr *)catd->__data;
6338676913Schristos 	set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data
64adfd5ba7Sjtc 		+ sizeof(struct _nls_cat_hdr));
65adfd5ba7Sjtc 
66adfd5ba7Sjtc 	/* binary search, see knuth algorithm b */
67adfd5ba7Sjtc 	l = 0;
6838676913Schristos 	u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
69adfd5ba7Sjtc 	while (l <= u) {
70adfd5ba7Sjtc 		i = (l + u) / 2;
7138676913Schristos 		r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
72adfd5ba7Sjtc 
73adfd5ba7Sjtc 		if (r == 0) {
7438676913Schristos 			msg_hdr = (struct _nls_msg_hdr *)
7538676913Schristos 			    (void *)((char *)catd->__data +
7638676913Schristos 			    sizeof(struct _nls_cat_hdr) +
7738676913Schristos 			    ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
78adfd5ba7Sjtc 
7938676913Schristos 			l = ntohl((u_int32_t)set_hdr[i].__index);
8038676913Schristos 			u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
81adfd5ba7Sjtc 			while (l <= u) {
82adfd5ba7Sjtc 				i = (l + u) / 2;
8338676913Schristos 				r = msg_id -
8438676913Schristos 				    ntohl((u_int32_t)msg_hdr[i].__msgno);
85adfd5ba7Sjtc 				if (r == 0) {
8638676913Schristos 					return ((char *) catd->__data +
8738676913Schristos 					    sizeof(struct _nls_cat_hdr) +
8838676913Schristos 					    ntohl((u_int32_t)
8938676913Schristos 					    cat_hdr->__msg_txt_offset) +
9038676913Schristos 					    ntohl((u_int32_t)
9138676913Schristos 					    msg_hdr[i].__offset));
92adfd5ba7Sjtc 				} else if (r < 0) {
93adfd5ba7Sjtc 					u = i - 1;
94adfd5ba7Sjtc 				} else {
95adfd5ba7Sjtc 					l = i + 1;
96adfd5ba7Sjtc 				}
97adfd5ba7Sjtc 			}
98adfd5ba7Sjtc 
99adfd5ba7Sjtc 			/* not found */
100c06a85e4Skleink 			goto notfound;
101adfd5ba7Sjtc 
102adfd5ba7Sjtc 		} else if (r < 0) {
103adfd5ba7Sjtc 			u = i - 1;
104adfd5ba7Sjtc 		} else {
105adfd5ba7Sjtc 			l = i + 1;
106adfd5ba7Sjtc 		}
107adfd5ba7Sjtc 	}
108adfd5ba7Sjtc 
109c06a85e4Skleink notfound:
110adfd5ba7Sjtc 	/* not found */
111c06a85e4Skleink 	errno = ENOMSG;
11203256c6eSchristos 	return __UNCONST(s);
113adfd5ba7Sjtc }
114