1 /* Reading binary .mo files.
2 Copyright (C) 1995-1998, 2000-2007 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, April 1995.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This include file describes the main part of binary .mo format. */
37 #include "binary-io.h"
42 #define _(str) gettext (str)
51 /* We read the file completely into memory. This is more efficient than
52 lots of lseek(). This struct represents the .mo file in memory. */
58 enum mo_endianness endian;
62 /* Read the contents of the given input stream. */
64 read_binary_mo_file (struct binary_mo_file *bfp,
65 FILE *fp, const char *filename)
74 const size_t increment = 4096;
75 if (size + increment > alloc)
77 alloc = alloc + alloc / 2;
78 if (alloc < size + increment)
79 alloc = size + increment;
80 buf = (char *) xrealloc (buf, alloc);
82 count = fread (buf + size, 1, increment, fp);
86 error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
92 buf = (char *) xrealloc (buf, size);
93 bfp->filename = filename;
98 /* Get a 32-bit number from the file, at the given file position. */
100 get_uint32 (const struct binary_mo_file *bfp, size_t offset)
102 nls_uint32 b0, b1, b2, b3;
104 if (offset + 4 > bfp->size)
105 error (EXIT_FAILURE, 0, _("file \"%s\" is truncated"), bfp->filename);
107 b0 = *(unsigned char *) (bfp->data + offset + 0);
108 b1 = *(unsigned char *) (bfp->data + offset + 1);
109 b2 = *(unsigned char *) (bfp->data + offset + 2);
110 b3 = *(unsigned char *) (bfp->data + offset + 3);
111 if (bfp->endian == MO_LITTLE_ENDIAN)
112 return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
114 return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
117 /* Get a static string from the file, at the given file position. */
119 get_string (const struct binary_mo_file *bfp, size_t offset, size_t *lengthp)
121 /* See 'struct string_desc'. */
122 nls_uint32 s_length = get_uint32 (bfp, offset);
123 nls_uint32 s_offset = get_uint32 (bfp, offset + 4);
125 if (s_offset + s_length + 1 > bfp->size)
126 error (EXIT_FAILURE, 0, _("file \"%s\" is truncated"), bfp->filename);
127 if (bfp->data[s_offset + s_length] != '\0')
128 error (EXIT_FAILURE, 0,
129 _("file \"%s\" contains a not NUL terminated string"),
132 *lengthp = s_length + 1;
133 return bfp->data + s_offset;
136 /* Get a system dependent string from the file, at the given file position. */
138 get_sysdep_string (const struct binary_mo_file *bfp, size_t offset,
139 const struct mo_file_header *header, size_t *lengthp)
141 /* See 'struct sysdep_string'. */
148 /* Compute the length. */
150 for (i = 4; ; i += 8)
152 nls_uint32 segsize = get_uint32 (bfp, offset + i);
153 nls_uint32 sysdepref = get_uint32 (bfp, offset + i + 4);
154 nls_uint32 sysdep_segment_offset;
155 nls_uint32 ss_length;
156 nls_uint32 ss_offset;
161 if (sysdepref == SEGMENTS_END)
163 if (sysdepref >= header->n_sysdep_segments)
165 error (EXIT_FAILURE, 0, _("file \"%s\" is not in GNU .mo format"),
167 /* See 'struct sysdep_segment'. */
168 sysdep_segment_offset = header->sysdep_segments_offset + sysdepref * 8;
169 ss_length = get_uint32 (bfp, sysdep_segment_offset);
170 ss_offset = get_uint32 (bfp, sysdep_segment_offset + 4);
171 if (ss_offset + ss_length > bfp->size)
172 error (EXIT_FAILURE, 0, _("file \"%s\" is truncated"), bfp->filename);
173 if (!(ss_length > 0 && bfp->data[ss_offset + ss_length - 1] == '\0'))
176 sprintf (location, "sysdep_segment[%u]", (unsigned int) sysdepref);
177 error (EXIT_FAILURE, 0,
178 _("file \"%s\" contains a not NUL terminated string, at %s"),
179 bfp->filename, location);
181 n = strlen (bfp->data + ss_offset);
182 length += (n > 1 ? 1 + n + 1 : n);
185 /* Allocate and fill the string. */
186 string = XNMALLOC (length, char);
188 s_offset = get_uint32 (bfp, offset);
189 for (i = 4; ; i += 8)
191 nls_uint32 segsize = get_uint32 (bfp, offset + i);
192 nls_uint32 sysdepref = get_uint32 (bfp, offset + i + 4);
193 nls_uint32 sysdep_segment_offset;
194 nls_uint32 ss_length;
195 nls_uint32 ss_offset;
198 if (s_offset + segsize > bfp->size)
199 error (EXIT_FAILURE, 0, _("file \"%s\" is truncated"), bfp->filename);
200 memcpy (p, bfp->data + s_offset, segsize);
204 if (sysdepref == SEGMENTS_END)
206 if (sysdepref >= header->n_sysdep_segments)
208 /* See 'struct sysdep_segment'. */
209 sysdep_segment_offset = header->sysdep_segments_offset + sysdepref * 8;
210 ss_length = get_uint32 (bfp, sysdep_segment_offset);
211 ss_offset = get_uint32 (bfp, sysdep_segment_offset + 4);
212 if (ss_offset + ss_length > bfp->size)
214 if (!(ss_length > 0 && bfp->data[ss_offset + ss_length - 1] == '\0'))
216 n = strlen (bfp->data + ss_offset);
219 memcpy (p, bfp->data + ss_offset, n);
225 if (p != string + length)
232 /* Reads an existing .mo file and adds the messages to mlp. */
234 read_mo_file (message_list_ty *mlp, const char *filename)
237 struct binary_mo_file bf;
238 struct mo_file_header header;
240 static lex_pos_ty pos = { __FILE__, __LINE__ };
242 if (strcmp (filename, "-") == 0 || strcmp (filename, "/dev/stdin") == 0)
245 SET_BINARY (fileno (fp));
249 fp = fopen (filename, "rb");
251 error (EXIT_FAILURE, errno,
252 _("error while opening \"%s\" for reading"), filename);
255 /* Read the file contents into memory. */
256 read_binary_mo_file (&bf, fp, filename);
258 /* Get a 32-bit number from the file header. */
259 # define GET_HEADER_FIELD(field) \
260 get_uint32 (&bf, offsetof (struct mo_file_header, field))
262 /* We must grope the file to determine which endian it is.
263 Perversity of the universe tends towards maximum, so it will
264 probably not match the currently executing architecture. */
265 bf.endian = MO_BIG_ENDIAN;
266 header.magic = GET_HEADER_FIELD (magic);
267 if (header.magic != _MAGIC)
269 bf.endian = MO_LITTLE_ENDIAN;
270 header.magic = GET_HEADER_FIELD (magic);
271 if (header.magic != _MAGIC)
274 error (EXIT_FAILURE, 0, _("file \"%s\" is not in GNU .mo format"),
279 header.revision = GET_HEADER_FIELD (revision);
281 /* We support only the major revisions 0 and 1. */
282 switch (header.revision >> 16)
286 /* Fill the header parts that apply to major revisions 0 and 1. */
287 header.nstrings = GET_HEADER_FIELD (nstrings);
288 header.orig_tab_offset = GET_HEADER_FIELD (orig_tab_offset);
289 header.trans_tab_offset = GET_HEADER_FIELD (trans_tab_offset);
290 header.hash_tab_size = GET_HEADER_FIELD (hash_tab_size);
291 header.hash_tab_offset = GET_HEADER_FIELD (hash_tab_offset);
293 for (i = 0; i < header.nstrings; i++)
303 /* Read the msgctxt and msgid. */
304 msgid = get_string (&bf, header.orig_tab_offset + i * 8,
306 /* Split into msgctxt and msgid. */
307 separator = strchr (msgid, MSGCTXT_SEPARATOR);
308 if (separator != NULL)
310 /* The part before the MSGCTXT_SEPARATOR is the msgctxt. */
313 msgid = separator + 1;
314 msgid_len -= msgid - msgctxt;
319 /* Read the msgstr. */
320 msgstr = get_string (&bf, header.trans_tab_offset + i * 8,
323 mp = message_alloc (msgctxt,
325 (strlen (msgid) + 1 < msgid_len
326 ? msgid + strlen (msgid) + 1
330 message_list_append (mlp, mp);
333 switch (header.revision & 0xffff)
339 /* Fill the header parts that apply to minor revision >= 1. */
340 header.n_sysdep_segments = GET_HEADER_FIELD (n_sysdep_segments);
341 header.sysdep_segments_offset =
342 GET_HEADER_FIELD (sysdep_segments_offset);
343 header.n_sysdep_strings = GET_HEADER_FIELD (n_sysdep_strings);
344 header.orig_sysdep_tab_offset =
345 GET_HEADER_FIELD (orig_sysdep_tab_offset);
346 header.trans_sysdep_tab_offset =
347 GET_HEADER_FIELD (trans_sysdep_tab_offset);
349 for (i = 0; i < header.n_sysdep_strings; i++)
361 /* Read the msgctxt and msgid. */
362 offset = get_uint32 (&bf, header.orig_sysdep_tab_offset + i * 4);
363 msgid = get_sysdep_string (&bf, offset, &header, &msgid_len);
364 /* Split into msgctxt and msgid. */
365 separator = strchr (msgid, MSGCTXT_SEPARATOR);
366 if (separator != NULL)
368 /* The part before the MSGCTXT_SEPARATOR is the msgctxt. */
371 msgid = separator + 1;
372 msgid_len -= msgid - msgctxt;
377 /* Read the msgstr. */
378 offset = get_uint32 (&bf, header.trans_sysdep_tab_offset + i * 4);
379 msgstr = get_sysdep_string (&bf, offset, &header, &msgstr_len);
381 mp = message_alloc (msgctxt,
383 (strlen (msgid) + 1 < msgid_len
384 ? msgid + strlen (msgid) + 1
389 /* Only messages with c-format or objc-format annotation are
390 recognized as having system-dependent strings by msgfmt.
391 Which one of the two, we don't know. We have to guess,
392 assuming that c-format is more probable than objc-format and
393 that the .mo was likely produced by "msgfmt -c". */
394 for (f = format_c; ; f = format_objc)
397 struct formatstring_parser *parser = formatstring_parsers[f];
401 str_end = msgid + msgid_len;
402 for (str = msgid; str < str_end; str += strlen (str) + 1)
404 char *invalid_reason = NULL;
406 parser->parse (str, false, NULL, &invalid_reason);
409 parser->free (descr);
412 free (invalid_reason);
419 str_end = msgstr + msgstr_len;
420 for (str = msgstr; str < str_end; str += strlen (str) + 1)
422 char *invalid_reason = NULL;
424 parser->parse (str, true, NULL, &invalid_reason);
427 parser->free (descr);
430 free (invalid_reason);
439 /* Found the most likely among c-format, objc-format. */
440 mp->is_format[f] = yes;
445 if (f == format_objc)
449 message_list_append (mlp, mp);