1 #define _BSD_SOURCE
2 #include <nl_types.h>
3 #include <endian.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <errno.h>
7
8 #define V(p) be32toh(*(uint32_t *)(p))
9
cmp(const void * a,const void * b)10 static int cmp(const void *a, const void *b)
11 {
12 uint32_t x = V(a), y = V(b);
13 return x<y ? -1 : x>y ? 1 : 0;
14 }
15
catgets(nl_catd catd,int set_id,int msg_id,const char * s)16 char *catgets (nl_catd catd, int set_id, int msg_id, const char *s)
17 {
18 const char *map = (const char *)catd;
19 uint32_t nsets = V(map+4);
20 const char *sets = map+20;
21 const char *msgs = map+20+V(map+12);
22 const char *strings = map+20+V(map+16);
23 uint32_t set_id_be = htobe32(set_id);
24 uint32_t msg_id_be = htobe32(msg_id);
25 const char *set = bsearch(&set_id_be, sets, nsets, 12, cmp);
26 if (!set) {
27 errno = ENOMSG;
28 return (char *)s;
29 }
30 uint32_t nmsgs = V(set+4);
31 msgs += 12*V(set+8);
32 const char *msg = bsearch(&msg_id_be, msgs, nmsgs, 12, cmp);
33 if (!msg) {
34 errno = ENOMSG;
35 return (char *)s;
36 }
37 return (char *)(strings + V(msg+8));
38 }
39