xref: /illumos-gate/usr/src/cmd/msgfmt/gnu_msgs_rev.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include "gnu_msgfmt.h"
28*7c478bd9Sstevel@tonic-gate #include "../../lib/libc/inc/msgfmt.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate static unsigned int
doswap(unsigned int n)31*7c478bd9Sstevel@tonic-gate doswap(unsigned int n)
32*7c478bd9Sstevel@tonic-gate {
33*7c478bd9Sstevel@tonic-gate 	unsigned int	r;
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 	r = (n << 24) | ((n & 0xff00) << 8) |
36*7c478bd9Sstevel@tonic-gate 		((n >> 8) & 0xff00) | (n >> 24);
37*7c478bd9Sstevel@tonic-gate 	return (r);
38*7c478bd9Sstevel@tonic-gate }
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate struct messages	*
search_msg(struct catalog * p,const char * id,unsigned int hash_val)41*7c478bd9Sstevel@tonic-gate search_msg(struct catalog *p, const char *id, unsigned int hash_val)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	unsigned int	i, idx, inc;
44*7c478bd9Sstevel@tonic-gate 	struct messages	*m;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	idx = hash_val % p->thash_size;
47*7c478bd9Sstevel@tonic-gate 	inc = 1 + (hash_val % (p->thash_size - 2));
48*7c478bd9Sstevel@tonic-gate 	if (!p->thash[idx])
49*7c478bd9Sstevel@tonic-gate 		return (NULL);
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	m = p->msg;
52*7c478bd9Sstevel@tonic-gate 	for (m = p->msg; (i = p->thash[idx]) != 0;
53*7c478bd9Sstevel@tonic-gate 		idx = (idx + inc) % p->thash_size) {
54*7c478bd9Sstevel@tonic-gate 		if (strcmp(m[i - 1].id, id) == 0) {
55*7c478bd9Sstevel@tonic-gate 			/* found */
56*7c478bd9Sstevel@tonic-gate 			return (&m[i - 1]);
57*7c478bd9Sstevel@tonic-gate 		}
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 	return (NULL);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int
msg_cmp(struct messages * m1,struct messages * m2)63*7c478bd9Sstevel@tonic-gate msg_cmp(struct messages *m1, struct messages *m2)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	return (strcmp(m1->id, m2->id));
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate void
output_all_gnu_mo_files(void)69*7c478bd9Sstevel@tonic-gate output_all_gnu_mo_files(void)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	struct catalog	*p, *op;
72*7c478bd9Sstevel@tonic-gate 	struct messages	*m;
73*7c478bd9Sstevel@tonic-gate 	size_t	id_len, str_len, id_off, str_off, ids_top, strs_top;
74*7c478bd9Sstevel@tonic-gate 	unsigned int	*hash_tbl;
75*7c478bd9Sstevel@tonic-gate 	unsigned int	hash_size, off_msgstr_tbl, off_hashtbl;
76*7c478bd9Sstevel@tonic-gate 	unsigned int	num = 0, fnum = 0, unum = 0;
77*7c478bd9Sstevel@tonic-gate 	unsigned int	i, idx;
78*7c478bd9Sstevel@tonic-gate 	char	*ids, *strs;
79*7c478bd9Sstevel@tonic-gate 	struct msgtbl	*id_tbl, *str_tbl;
80*7c478bd9Sstevel@tonic-gate 	struct gnu_msg_info	header;
81*7c478bd9Sstevel@tonic-gate 	FILE	*out;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	p = catalog_head;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	while (p) {
86*7c478bd9Sstevel@tonic-gate 		num += p->nmsg;
87*7c478bd9Sstevel@tonic-gate 		fnum += p->fnum;
88*7c478bd9Sstevel@tonic-gate 		unum += p->unum;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		if (p->header)
91*7c478bd9Sstevel@tonic-gate 			num--;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 		p->msg = (struct messages *)Xrealloc(p->msg,
94*7c478bd9Sstevel@tonic-gate 			sizeof (struct messages) * p->nmsg);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 		free(p->thash);
97*7c478bd9Sstevel@tonic-gate 		/*
98*7c478bd9Sstevel@tonic-gate 		 * Sort the message array
99*7c478bd9Sstevel@tonic-gate 		 */
100*7c478bd9Sstevel@tonic-gate 		qsort(p->msg, p->nmsg, sizeof (struct messages),
101*7c478bd9Sstevel@tonic-gate 			(int (*)(const void *, const void *))msg_cmp);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		hash_size = find_prime(p->nmsg);
105*7c478bd9Sstevel@tonic-gate 		hash_tbl = (unsigned int *)Xcalloc(hash_size,
106*7c478bd9Sstevel@tonic-gate 			sizeof (unsigned int));
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		/* Setting Header info */
110*7c478bd9Sstevel@tonic-gate 		off_msgstr_tbl = sizeof (struct gnu_msg_info) +
111*7c478bd9Sstevel@tonic-gate 			p->nmsg * sizeof (struct msgtbl);
112*7c478bd9Sstevel@tonic-gate 		off_hashtbl = off_msgstr_tbl +
113*7c478bd9Sstevel@tonic-gate 			p->nmsg * sizeof (struct msgtbl);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		header.magic = doswap(GNU_MAGIC);
116*7c478bd9Sstevel@tonic-gate 		header.revision = doswap(GNU_REVISION);
117*7c478bd9Sstevel@tonic-gate 		header.num_of_str = doswap(p->nmsg);
118*7c478bd9Sstevel@tonic-gate 		header.off_msgid_tbl =
119*7c478bd9Sstevel@tonic-gate 			doswap(sizeof (struct gnu_msg_info));
120*7c478bd9Sstevel@tonic-gate 		header.off_msgstr_tbl = doswap(off_msgstr_tbl);
121*7c478bd9Sstevel@tonic-gate 		header.sz_hashtbl = doswap(hash_size);
122*7c478bd9Sstevel@tonic-gate 		header.off_hashtbl = doswap(off_hashtbl);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 		m = p->msg;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		id_len = 0;
127*7c478bd9Sstevel@tonic-gate 		str_len = 0;
128*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->nmsg; i++) {
129*7c478bd9Sstevel@tonic-gate 			id_len += m[i].id_len;
130*7c478bd9Sstevel@tonic-gate 			str_len += m[i].str_len;
131*7c478bd9Sstevel@tonic-gate 		}
132*7c478bd9Sstevel@tonic-gate 		ids = (char *)Xmalloc(id_len);
133*7c478bd9Sstevel@tonic-gate 		strs = (char *)Xmalloc(str_len);
134*7c478bd9Sstevel@tonic-gate 		id_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
135*7c478bd9Sstevel@tonic-gate 			p->nmsg);
136*7c478bd9Sstevel@tonic-gate 		str_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
137*7c478bd9Sstevel@tonic-gate 			p->nmsg);
138*7c478bd9Sstevel@tonic-gate 		id_off = 0;
139*7c478bd9Sstevel@tonic-gate 		str_off = 0;
140*7c478bd9Sstevel@tonic-gate 		ids_top = off_hashtbl +
141*7c478bd9Sstevel@tonic-gate 			sizeof (unsigned int) * hash_size;
142*7c478bd9Sstevel@tonic-gate 		strs_top = ids_top + id_len;
143*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->nmsg; i++) {
144*7c478bd9Sstevel@tonic-gate 			/*
145*7c478bd9Sstevel@tonic-gate 			 * Set the hash table
146*7c478bd9Sstevel@tonic-gate 			 */
147*7c478bd9Sstevel@tonic-gate 			idx = get_hash_index(hash_tbl, m[i].hash, hash_size);
148*7c478bd9Sstevel@tonic-gate 			hash_tbl[idx] = doswap(i + 1);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 			/*
151*7c478bd9Sstevel@tonic-gate 			 * rearrange msgid and msgstr
152*7c478bd9Sstevel@tonic-gate 			 */
153*7c478bd9Sstevel@tonic-gate 			id_tbl[i].len = doswap(m[i].id_len - 1);
154*7c478bd9Sstevel@tonic-gate 			str_tbl[i].len = doswap(m[i].str_len - 1);
155*7c478bd9Sstevel@tonic-gate 			id_tbl[i].offset = doswap(id_off + ids_top);
156*7c478bd9Sstevel@tonic-gate 			str_tbl[i].offset = doswap(str_off + strs_top);
157*7c478bd9Sstevel@tonic-gate 			(void) memcpy(ids + id_off, m[i].id, m[i].id_len);
158*7c478bd9Sstevel@tonic-gate 			(void) memcpy(strs + str_off, m[i].str, m[i].str_len);
159*7c478bd9Sstevel@tonic-gate 			id_off += m[i].id_len;
160*7c478bd9Sstevel@tonic-gate 			str_off += m[i].str_len;
161*7c478bd9Sstevel@tonic-gate 			free(m[i].id);
162*7c478bd9Sstevel@tonic-gate 			free(m[i].str);
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		if ((out = fopen(p->fname, "w")) == NULL) {
166*7c478bd9Sstevel@tonic-gate 			error(gettext(ERR_OPEN_FAILED), p->fname);
167*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		/* writing header */
171*7c478bd9Sstevel@tonic-gate 		(void) fwrite(&header, sizeof (struct gnu_msg_info),
172*7c478bd9Sstevel@tonic-gate 			1, out);
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		/* writing msgid offset table */
175*7c478bd9Sstevel@tonic-gate 		(void) fwrite(id_tbl, sizeof (struct msgtbl),
176*7c478bd9Sstevel@tonic-gate 			p->nmsg, out);
177*7c478bd9Sstevel@tonic-gate 		/* writing msgstr offset table */
178*7c478bd9Sstevel@tonic-gate 		(void) fwrite(str_tbl, sizeof (struct msgtbl),
179*7c478bd9Sstevel@tonic-gate 			p->nmsg, out);
180*7c478bd9Sstevel@tonic-gate 		/* writing hash table */
181*7c478bd9Sstevel@tonic-gate 		(void) fwrite(hash_tbl, sizeof (unsigned int),
182*7c478bd9Sstevel@tonic-gate 			hash_size, out);
183*7c478bd9Sstevel@tonic-gate 		/* writing msgid table */
184*7c478bd9Sstevel@tonic-gate 		(void) fwrite(ids, id_len, 1, out);
185*7c478bd9Sstevel@tonic-gate 		/* writing msgstr table */
186*7c478bd9Sstevel@tonic-gate 		(void) fwrite(strs, str_len, 1, out);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		(void) fclose(out);
189*7c478bd9Sstevel@tonic-gate 		free(p->fname);
190*7c478bd9Sstevel@tonic-gate 		free(p->msg);
191*7c478bd9Sstevel@tonic-gate 		free(id_tbl);
192*7c478bd9Sstevel@tonic-gate 		free(str_tbl);
193*7c478bd9Sstevel@tonic-gate 		free(hash_tbl);
194*7c478bd9Sstevel@tonic-gate 		free(ids);
195*7c478bd9Sstevel@tonic-gate 		free(strs);
196*7c478bd9Sstevel@tonic-gate 		op = p->next;
197*7c478bd9Sstevel@tonic-gate 		free(p);
198*7c478bd9Sstevel@tonic-gate 		p = op;
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
201*7c478bd9Sstevel@tonic-gate 		diag(gettext(DIAG_RESULTS), num, fnum, unum);
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate }
204