xref: /openbsd/lib/libc/nls/catgets.c (revision e5dd7070)
1 /*	$OpenBSD: catgets.c,v 1.9 2015/09/05 11:25:30 guenther Exp $ */
2 /*-
3  * Copyright (c) 1996 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by J.T. Conklin.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #define _NLS_PRIVATE
32 
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <nl_types.h>
37 
38 char *
39 catgets(nl_catd catd, int set_id, int msg_id, const char *s)
40 {
41 	struct _nls_cat_hdr *cat_hdr;
42 	struct _nls_set_hdr *set_hdr;
43 	struct _nls_msg_hdr *msg_hdr;
44 	int l, u, i, r;
45 
46 	if (catd == (nl_catd) -1) {
47 		errno = EBADF;
48 		return (char *) s;
49 	}
50 
51 	cat_hdr = (struct _nls_cat_hdr *) catd->__data;
52 	set_hdr = (struct _nls_set_hdr *) ((char *)catd->__data
53 		+ sizeof(struct _nls_cat_hdr));
54 
55 	/* binary search, see knuth algorithm b */
56 	l = 0;
57 	u = ntohl(cat_hdr->__nsets) - 1;
58 	while (l <= u) {
59 		i = (l + u) / 2;
60 		r = set_id - ntohl(set_hdr[i].__setno);
61 
62 		if (r == 0) {
63 			msg_hdr = (struct _nls_msg_hdr *) ((char *)catd->__data
64 				+ sizeof(struct _nls_cat_hdr)
65 				+ ntohl(cat_hdr->__msg_hdr_offset));
66 
67 			l = ntohl(set_hdr[i].__index);
68 			u = l + ntohl(set_hdr[i].__nmsgs) - 1;
69 			while (l <= u) {
70 				i = (l + u) / 2;
71 				r = msg_id - ntohl(msg_hdr[i].__msgno);
72 				if (r == 0) {
73 					return (char *) catd->__data
74 					    + sizeof(struct _nls_cat_hdr)
75 					    + ntohl(cat_hdr->__msg_txt_offset)
76 					    + ntohl(msg_hdr[i].__offset);
77 				} else if (r < 0) {
78 					u = i - 1;
79 				} else {
80 					l = i + 1;
81 				}
82 			}
83 
84 			/* not found */
85 			return (char *) s;
86 
87 		} else if (r < 0) {
88 			u = i - 1;
89 		} else {
90 			l = i + 1;
91 		}
92 	}
93 
94 	/* not found */
95 	return (char *) s;
96 }
97 DEF_WEAK(catgets);
98