summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/catgets.c
blob: d9f5a970e54c6c794c7d4eaf3cc176fde9882cb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * catgets.c
 */

#pragma weak _catgets = catgets

#include "lint.h"
#include <sys/types.h>
#include <nl_types.h>
#include <errno.h>
#include "nlspath_checks.h"

char *
catgets(nl_catd catd_st, int set_id, int msg_id, const char *def_str)
{
	int			hi, lo, mid;
	struct	_cat_hdr 	*p;
	struct	_cat_set_hdr	*q;
	struct	_cat_msg_hdr	*r;
	void			*catd;

	if ((catd_st == NULL) || (catd_st == (nl_catd)-1)) {
		/* invalid message catalog descriptor */
		errno = EBADF;
		return ((char *)def_str);
	}

	if ((catd_st->__content == NULL) &&
	    (catd_st->__size == 0) && (catd_st->__trust == 1)) {
		/* special message catalog descriptor for C locale */
		return ((char *)def_str);
	} else if ((catd_st->__content == NULL) || (catd_st->__size == 0)) {
		/* invalid message catalog descriptor */
		errno = EBADF;
		return ((char *)def_str);
	}

	catd = catd_st->__content;
	p = (struct _cat_hdr *)catd_st->__content;
	hi = p->__nsets - 1;
	lo = 0;
	/*
	 * Two while loops will perform binary search.
	 * Outer loop searches the set and inner loop searches
	 * message id
	 */
	while (hi >= lo) {
		mid = (hi + lo) / 2;
		q = (struct _cat_set_hdr *)
		    ((uintptr_t)catd
		    + _CAT_HDR_SIZE
		    + _CAT_SET_HDR_SIZE * mid);
		if (q->__set_no == set_id) {
			lo = q->__first_msg_hdr;
			hi = lo + q->__nmsgs - 1;
			while (hi >= lo) {
				mid = (hi + lo) / 2;
				r = (struct _cat_msg_hdr *)
				    ((uintptr_t)catd
				    + _CAT_HDR_SIZE
				    + p->__msg_hdr_offset
				    + _CAT_MSG_HDR_SIZE * mid);
				if (r->__msg_no == msg_id) {
					char *msg = (char *)catd
					    + _CAT_HDR_SIZE
					    + p->__msg_text_offset
					    + r->__msg_offset;

					if (!catd_st->__trust) {
						int errno_save = errno;
						char *cmsg = check_format(
						    def_str, msg, 0);
						if (cmsg == def_str) {
							/* security */
							return ((char *)
							    def_str);
						} else {
							errno = errno_save;
							return (msg);
						}
					} else {
						return (msg);
					}
				} else if (r->__msg_no < msg_id)
					lo = mid + 1;
				else
					hi = mid - 1;
			} /* while */

			/* In case set number not found */
			errno = ENOMSG;
			return ((char *)def_str);
		} else if (q->__set_no < set_id)
			lo = mid + 1;
		else
			hi = mid - 1;
	} /* while */

	/* In case msg_id not found. */
	errno = ENOMSG;
	return ((char *)def_str);
}