open_catalog.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <byteswap.h>
  15. #include <endian.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #ifdef _POSIX_MAPPED_FILES
  22. # include <sys/mman.h>
  23. #endif
  24. #include <sys/stat.h>
  25. #include "catgetsinfo.h"
  26. #include <not-cancel.h>
  27. #define SWAPU32(w) bswap_32 (w)
  28. int
  29. __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
  30. __nl_catd catalog)
  31. {
  32. int fd = -1;
  33. struct __stat64_t64 st;
  34. int swapping;
  35. size_t cnt;
  36. size_t max_offset;
  37. size_t tab_size;
  38. const char *lastp;
  39. int result = -1;
  40. char *buf = NULL;
  41. if (strchr (cat_name, '/') != NULL || nlspath == NULL)
  42. fd = __open_nocancel (cat_name, O_RDONLY | O_CLOEXEC);
  43. else
  44. {
  45. const char *run_nlspath = nlspath;
  46. #define ENOUGH(n) \
  47. if (__glibc_unlikely (bufact + (n) >= bufmax)) \
  48. { \
  49. char *old_buf = buf; \
  50. bufmax += (bufmax < 256 + (n)) ? 256 + (n) : bufmax; \
  51. buf = realloc (buf, bufmax); \
  52. if (__glibc_unlikely (buf == NULL)) \
  53. { \
  54. free (old_buf); \
  55. return -1; \
  56. } \
  57. }
  58. /* The RUN_NLSPATH variable contains a colon separated list of
  59. descriptions where we expect to find catalogs. We have to
  60. recognize certain % substitutions and stop when we found the
  61. first existing file. */
  62. size_t bufact;
  63. size_t bufmax = 0;
  64. size_t len;
  65. fd = -1;
  66. while (*run_nlspath != '\0')
  67. {
  68. bufact = 0;
  69. if (*run_nlspath == ':')
  70. {
  71. /* Leading colon or adjacent colons - treat same as %N. */
  72. len = strlen (cat_name);
  73. ENOUGH (len);
  74. memcpy (&buf[bufact], cat_name, len);
  75. bufact += len;
  76. }
  77. else
  78. while (*run_nlspath != ':' && *run_nlspath != '\0')
  79. if (*run_nlspath == '%')
  80. {
  81. const char *tmp;
  82. ++run_nlspath; /* We have seen the `%'. */
  83. switch (*run_nlspath++)
  84. {
  85. case 'N':
  86. /* Use the catalog name. */
  87. len = strlen (cat_name);
  88. ENOUGH (len);
  89. memcpy (&buf[bufact], cat_name, len);
  90. bufact += len;
  91. break;
  92. case 'L':
  93. /* Use the current locale category value. */
  94. len = strlen (env_var);
  95. ENOUGH (len);
  96. memcpy (&buf[bufact], env_var, len);
  97. bufact += len;
  98. break;
  99. case 'l':
  100. /* Use language element of locale category value. */
  101. tmp = env_var;
  102. do
  103. {
  104. ENOUGH (1);
  105. buf[bufact++] = *tmp++;
  106. }
  107. while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
  108. break;
  109. case 't':
  110. /* Use territory element of locale category value. */
  111. tmp = env_var;
  112. do
  113. ++tmp;
  114. while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
  115. if (*tmp == '_')
  116. {
  117. ++tmp;
  118. do
  119. {
  120. ENOUGH (1);
  121. buf[bufact++] = *tmp++;
  122. }
  123. while (*tmp != '\0' && *tmp != '.');
  124. }
  125. break;
  126. case 'c':
  127. /* Use code set element of locale category value. */
  128. tmp = env_var;
  129. do
  130. ++tmp;
  131. while (*tmp != '\0' && *tmp != '.');
  132. if (*tmp == '.')
  133. {
  134. ++tmp;
  135. do
  136. {
  137. ENOUGH (1);
  138. buf[bufact++] = *tmp++;
  139. }
  140. while (*tmp != '\0');
  141. }
  142. break;
  143. case '%':
  144. ENOUGH (1);
  145. buf[bufact++] = '%';
  146. break;
  147. default:
  148. /* Unknown variable: ignore this path element. */
  149. bufact = 0;
  150. while (*run_nlspath != '\0' && *run_nlspath != ':')
  151. ++run_nlspath;
  152. break;
  153. }
  154. }
  155. else
  156. {
  157. ENOUGH (1);
  158. buf[bufact++] = *run_nlspath++;
  159. }
  160. ENOUGH (1);
  161. buf[bufact] = '\0';
  162. if (bufact != 0)
  163. {
  164. fd = __open_nocancel (buf, O_RDONLY | O_CLOEXEC);
  165. if (fd >= 0)
  166. break;
  167. }
  168. ++run_nlspath;
  169. }
  170. }
  171. /* Avoid dealing with directories and block devices */
  172. if (__builtin_expect (fd, 0) < 0)
  173. {
  174. free (buf);
  175. return -1;
  176. }
  177. if (__glibc_unlikely (__fstat64_time64 (fd, &st) < 0))
  178. goto close_unlock_return;
  179. if (__builtin_expect (!S_ISREG (st.st_mode), 0)
  180. || (size_t) st.st_size < sizeof (struct catalog_obj))
  181. {
  182. /* `errno' is not set correctly but the file is not usable.
  183. Use an reasonable error value. */
  184. __set_errno (EINVAL);
  185. goto close_unlock_return;
  186. }
  187. catalog->file_size = st.st_size;
  188. #ifdef _POSIX_MAPPED_FILES
  189. # ifndef MAP_COPY
  190. /* Linux seems to lack read-only copy-on-write. */
  191. # define MAP_COPY MAP_PRIVATE
  192. # endif
  193. # ifndef MAP_FILE
  194. /* Some systems do not have this flag; it is superfluous. */
  195. # define MAP_FILE 0
  196. # endif
  197. catalog->file_ptr =
  198. (struct catalog_obj *) __mmap (NULL, st.st_size, PROT_READ,
  199. MAP_FILE|MAP_COPY, fd, 0);
  200. if (__builtin_expect (catalog->file_ptr != (struct catalog_obj *) MAP_FAILED,
  201. 1))
  202. /* Tell the world we managed to mmap the file. */
  203. catalog->status = mmapped;
  204. else
  205. #endif /* _POSIX_MAPPED_FILES */
  206. {
  207. /* mmap failed perhaps because the system call is not
  208. implemented. Try to load the file. */
  209. size_t todo;
  210. catalog->file_ptr = malloc (st.st_size);
  211. if (catalog->file_ptr == NULL)
  212. goto close_unlock_return;
  213. todo = st.st_size;
  214. /* Save read, handle partial reads. */
  215. do
  216. {
  217. size_t now = __read_nocancel (fd, (((char *) catalog->file_ptr)
  218. + (st.st_size - todo)), todo);
  219. if (now == 0 || now == (size_t) -1)
  220. {
  221. #ifdef EINTR
  222. if (now == (size_t) -1 && errno == EINTR)
  223. continue;
  224. #endif
  225. free ((void *) catalog->file_ptr);
  226. goto close_unlock_return;
  227. }
  228. todo -= now;
  229. }
  230. while (todo > 0);
  231. catalog->status = malloced;
  232. }
  233. /* Determine whether the file is a catalog file and if yes whether
  234. it is written using the correct byte order. Else we have to swap
  235. the values. */
  236. if (__glibc_likely (catalog->file_ptr->magic == CATGETS_MAGIC))
  237. swapping = 0;
  238. else if (catalog->file_ptr->magic == SWAPU32 (CATGETS_MAGIC))
  239. swapping = 1;
  240. else
  241. {
  242. invalid_file:
  243. /* Invalid file. Free the resources and mark catalog as not
  244. usable. */
  245. #ifdef _POSIX_MAPPED_FILES
  246. if (catalog->status == mmapped)
  247. __munmap ((void *) catalog->file_ptr, catalog->file_size);
  248. else
  249. #endif /* _POSIX_MAPPED_FILES */
  250. free (catalog->file_ptr);
  251. goto close_unlock_return;
  252. }
  253. #define SWAP(x) (swapping ? SWAPU32 (x) : (x))
  254. /* Get dimensions of the used hashing table. */
  255. catalog->plane_size = SWAP (catalog->file_ptr->plane_size);
  256. catalog->plane_depth = SWAP (catalog->file_ptr->plane_depth);
  257. /* The file contains two versions of the pointer tables. Pick the
  258. right one for the local byte order. */
  259. #if __BYTE_ORDER == __LITTLE_ENDIAN
  260. catalog->name_ptr = &catalog->file_ptr->name_ptr[0];
  261. #elif __BYTE_ORDER == __BIG_ENDIAN
  262. catalog->name_ptr = &catalog->file_ptr->name_ptr[catalog->plane_size
  263. * catalog->plane_depth
  264. * 3];
  265. #else
  266. # error Cannot handle __BYTE_ORDER byte order
  267. #endif
  268. /* The rest of the file contains all the strings. They are
  269. addressed relative to the position of the first string. */
  270. catalog->strings =
  271. (const char *) &catalog->file_ptr->name_ptr[catalog->plane_size
  272. * catalog->plane_depth * 3 * 2];
  273. /* Determine the largest string offset mentioned in the table. */
  274. max_offset = 0;
  275. tab_size = 3 * catalog->plane_size * catalog->plane_depth;
  276. for (cnt = 2; cnt < tab_size; cnt += 3)
  277. if (catalog->name_ptr[cnt] > max_offset)
  278. max_offset = catalog->name_ptr[cnt];
  279. /* Now we can check whether the file is large enough to contain the
  280. tables it says it contains. */
  281. if ((size_t) st.st_size
  282. <= (sizeof (struct catalog_obj) + 2 * tab_size + max_offset))
  283. /* The last string is not contained in the file. */
  284. goto invalid_file;
  285. lastp = catalog->strings + max_offset;
  286. max_offset = (st.st_size
  287. - sizeof (struct catalog_obj) + 2 * tab_size + max_offset);
  288. while (*lastp != '\0')
  289. {
  290. if (--max_offset == 0)
  291. goto invalid_file;
  292. ++lastp;
  293. }
  294. /* We succeeded. */
  295. result = 0;
  296. /* Release the lock again. */
  297. close_unlock_return:
  298. __close_nocancel_nostatus (fd);
  299. free (buf);
  300. return result;
  301. }
  302. libc_hidden_def (__open_catalog)