ReactOS  0.4.13-dev-563-g0561610
nls_base.c File Reference
#include <linux/module.h>
#include <linux/string.h>
#include <linux/config.h>
#include <linux/nls.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
Include dependency graph for nls_base.c:

Go to the source code of this file.

Classes

struct  utf8_table
 

Functions

int utf8_mbtowc (wchar_t *p, const __u8 *s, int n)
 
int utf8_mbstowcs (wchar_t *pwcs, const __u8 *s, int n)
 
int utf8_wctomb (__u8 *s, wchar_t wc, int maxlen)
 
int utf8_wcstombs (__u8 *s, const wchar_t *pwcs, int maxlen)
 
int register_nls (struct nls_table *nls)
 
int unregister_nls (struct nls_table *nls)
 
static struct nls_tablefind_nls (char *charset)
 
struct nls_tableload_nls (char *charset)
 
void unload_nls (struct nls_table *nls)
 
static int uni2char (wchar_t uni, unsigned char *out, int boundlen)
 
static int char2uni (const unsigned char *rawstring, int boundlen, wchar_t *uni)
 
 EXPORT_SYMBOL (register_nls)
 
 EXPORT_SYMBOL (unregister_nls)
 
 EXPORT_SYMBOL (unload_nls)
 
 EXPORT_SYMBOL (load_nls)
 
 EXPORT_SYMBOL (load_nls_default)
 
 EXPORT_SYMBOL (utf8_mbtowc)
 
 EXPORT_SYMBOL (utf8_mbstowcs)
 
 EXPORT_SYMBOL (utf8_wctomb)
 
 EXPORT_SYMBOL (utf8_wcstombs)
 
 MODULE_LICENSE ("Dual BSD/GPL")
 

Variables

struct nls_tabletables
 
spinlock_t nls_lock
 
static struct utf8_table utf8_table []
 
wchar_t charset2uni [256]
 
static unsigned char page00 [256]
 
static unsigned charpage_uni2charset [256]
 
static unsigned char charset2lower [256]
 
static unsigned char charset2upper [256]
 

Function Documentation

◆ char2uni()

static int char2uni ( const unsigned char rawstring,
int  boundlen,
wchar_t uni 
)
static

Definition at line 458 of file nls_base.c.

459 {
460  *uni = charset2uni[*rawstring];
461  if (*uni == 0x0000)
462  return -EINVAL;
463  return 1;
464 }
Definition: arc.h:39
wchar_t charset2uni[256]
Definition: nls_base.c:245

◆ EXPORT_SYMBOL() [1/9]

EXPORT_SYMBOL ( register_nls  )

◆ EXPORT_SYMBOL() [2/9]

EXPORT_SYMBOL ( unregister_nls  )

◆ EXPORT_SYMBOL() [3/9]

EXPORT_SYMBOL ( unload_nls  )

◆ EXPORT_SYMBOL() [4/9]

EXPORT_SYMBOL ( load_nls  )

◆ EXPORT_SYMBOL() [5/9]

EXPORT_SYMBOL ( load_nls_default  )

◆ EXPORT_SYMBOL() [6/9]

EXPORT_SYMBOL ( utf8_mbtowc  )

◆ EXPORT_SYMBOL() [7/9]

EXPORT_SYMBOL ( utf8_mbstowcs  )

◆ EXPORT_SYMBOL() [8/9]

EXPORT_SYMBOL ( utf8_wctomb  )

◆ EXPORT_SYMBOL() [9/9]

EXPORT_SYMBOL ( utf8_wcstombs  )

◆ find_nls()

static struct nls_table* find_nls ( char charset)
static

Definition at line 202 of file nls_base.c.

203 {
204  struct nls_table *nls;
206  for (nls = tables; nls; nls = nls->next) {
207  if (!strcmp(nls->charset, charset))
208  break;
209  if (nls->alias && !strcmp(nls->alias, charset))
210  break;
211  }
212  if (nls && !try_module_get(nls->owner))
213  nls = NULL;
215  return nls;
216 }
CFF_Charset charset
Definition: cffcmap.c:138
#define spin_lock(sl)
Definition: module.h:304
#define nls
Definition: kernel32.h:13
smooth NULL
Definition: ftsmooth.c:416
spinlock_t nls_lock
Definition: nls_base.c:23
#define try_module_get(x)
Definition: module.h:273
#define spin_unlock(sl)
Definition: module.h:305
struct define * next
Definition: wpp.c:35
struct nls_table * tables
Definition: nls_base.c:22
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469

Referenced by load_nls().

◆ load_nls()

struct nls_table* load_nls ( char charset)

Definition at line 218 of file nls_base.c.

219 {
220  struct nls_table *nls;
221 #ifdef CONFIG_KMOD
222  int ret;
223 #endif
224 
225  nls = find_nls(charset);
226  if (nls)
227  return nls;
228 
229 #ifdef CONFIG_KMOD
230  ret = request_module("nls_%s", charset);
231  if (ret != 0) {
232  printk("Unable to load NLS charset %s\n", charset);
233  return NULL;
234  }
235  nls = find_nls(charset);
236 #endif
237  return nls;
238 }
CFF_Charset charset
Definition: cffcmap.c:138
#define nls
Definition: kernel32.h:13
smooth NULL
Definition: ftsmooth.c:416
asmlinkage int printk(const char *fmt,...)
Definition: output.c:76
static struct nls_table * find_nls(char *charset)
Definition: nls_base.c:202
int ret

◆ MODULE_LICENSE()

MODULE_LICENSE ( "Dual BSD/GPL"  )

◆ register_nls()

int register_nls ( struct nls_table nls)

Definition at line 162 of file nls_base.c.

163 {
164  struct nls_table ** tmp = &tables;
165 
166  if (!nls)
167  return -EINVAL;
168  if (nls->next)
169  return -EBUSY;
170 
172  while (*tmp) {
173  if (nls == *tmp) {
175  return -EBUSY;
176  }
177  tmp = &(*tmp)->next;
178  }
179  nls->next = tables;
180  tables = nls;
182  return 0;
183 }
struct nls_table * next
Definition: module.h:1040
Definition: arc.h:39
Definition: arc.h:37
#define spin_lock(sl)
Definition: module.h:304
#define nls
Definition: kernel32.h:13
spinlock_t nls_lock
Definition: nls_base.c:23
#define spin_unlock(sl)
Definition: module.h:305
struct define * next
Definition: wpp.c:35
struct nls_table * tables
Definition: nls_base.c:22

◆ uni2char()

static int uni2char ( wchar_t  uni,
unsigned char out,
int  boundlen 
)
static

Definition at line 441 of file nls_base.c.

442 {
443  unsigned char *uni2charset;
444  unsigned char cl = uni & 0x00ff;
445  unsigned char ch = (uni & 0xff00) >> 8;
446 
447  if (boundlen <= 0)
448  return -ENAMETOOLONG;
449 
450  uni2charset = page_uni2charset[ch];
451  if (uni2charset && uni2charset[cl])
452  out[0] = uni2charset[cl];
453  else
454  return -EINVAL;
455  return 1;
456 }
Definition: arc.h:39
static FILE * out
Definition: regtests2xml.c:44
static unsigned char * page_uni2charset[256]
Definition: nls_base.c:364

◆ unload_nls()

void unload_nls ( struct nls_table nls)

Definition at line 240 of file nls_base.c.

241 {
242  module_put(nls->owner);
243 }
#define nls
Definition: kernel32.h:13
#define module_put(x)
Definition: module.h:274

◆ unregister_nls()

int unregister_nls ( struct nls_table nls)

Definition at line 185 of file nls_base.c.

186 {
187  struct nls_table ** tmp = &tables;
188 
190  while (*tmp) {
191  if (nls == *tmp) {
192  *tmp = nls->next;
194  return 0;
195  }
196  tmp = &(*tmp)->next;
197  }
199  return -EINVAL;
200 }
struct nls_table * next
Definition: module.h:1040
Definition: arc.h:39
#define spin_lock(sl)
Definition: module.h:304
#define nls
Definition: kernel32.h:13
spinlock_t nls_lock
Definition: nls_base.c:23
#define spin_unlock(sl)
Definition: module.h:305
struct define * next
Definition: wpp.c:35
struct nls_table * tables
Definition: nls_base.c:22

◆ utf8_mbstowcs()

int utf8_mbstowcs ( wchar_t pwcs,
const __u8 s,
int  n 
)

Definition at line 79 of file nls_base.c.

80 {
81  __u16 *op;
82  const __u8 *ip;
83  int size;
84 
85  op = pwcs;
86  ip = s;
87  while (*ip && n > 0) {
88  if (*ip & 0x80) {
89  size = utf8_mbtowc(op, ip, n);
90  if (size == -1) {
91  /* Ignore character and move on */
92  ip++;
93  n--;
94  } else {
95  op++;
96  ip += size;
97  n -= size;
98  }
99  } else {
100  *op++ = *ip++;
101  n--;
102  }
103  }
104  return (int)(op - pwcs);
105 }
unsigned char __u8
Definition: compat.h:88
GLdouble n
Definition: glext.h:7729
int utf8_mbtowc(wchar_t *p, const __u8 *s, int n)
Definition: nls_base.c:49
int ip[4]
Definition: rtl.c:1176
GLsizeiptr size
Definition: glext.h:5919
unsigned short __u16
Definition: compat.h:89
Definition: dhcpd.h:61
GLdouble s
Definition: gl.h:2039
UINT op
Definition: effect.c:223

◆ utf8_mbtowc()

int utf8_mbtowc ( wchar_t p,
const __u8 s,
int  n 
)

Definition at line 49 of file nls_base.c.

50 {
51  long l;
52  int c0, c, nc;
53  struct utf8_table *t;
54 
55  nc = 0;
56  c0 = *s;
57  l = c0;
58  for (t = utf8_table; t->cmask; t++) {
59  nc++;
60  if ((c0 & t->cmask) == t->cval) {
61  l &= t->lmask;
62  if (l < t->lval)
63  return -1;
64  *p = (wchar_t)l;
65  return nc;
66  }
67  if (n <= nc)
68  return -1;
69  s++;
70  c = (*s ^ 0x80) & 0xFF;
71  if (c & 0xC0)
72  return -1;
73  l = (l << 6) | c;
74  }
75  return -1;
76 }
GLdouble n
Definition: glext.h:7729
GLdouble GLdouble t
Definition: gl.h:2047
r l[0]
Definition: byte_order.h:167
const GLubyte * c
Definition: glext.h:8905
GLdouble s
Definition: gl.h:2039
int cmask
Definition: nls_base.c:30
#define c
Definition: ke_i.h:80
#define wchar_t
Definition: wchar.h:102
GLfloat GLfloat p
Definition: glext.h:8902
long lval
Definition: nls_base.c:34

Referenced by utf8_mbstowcs().

◆ utf8_wcstombs()

int utf8_wcstombs ( __u8 s,
const wchar_t pwcs,
int  maxlen 
)

Definition at line 136 of file nls_base.c.

137 {
138  const __u16 *ip;
139  __u8 *op;
140  int size;
141 
142  op = s;
143  ip = pwcs;
144  while (*ip && maxlen > 0) {
145  if (*ip > 0x7f) {
146  size = utf8_wctomb(op, *ip, maxlen);
147  if (size == -1) {
148  /* Ignore character and move on */
149  maxlen--;
150  } else {
151  op += size;
152  maxlen -= size;
153  }
154  } else {
155  *op++ = (__u8) *ip;
156  }
157  ip++;
158  }
159  return (int)(op - s);
160 }
unsigned char __u8
Definition: compat.h:88
int utf8_wctomb(__u8 *s, wchar_t wc, int maxlen)
Definition: nls_base.c:108
int ip[4]
Definition: rtl.c:1176
GLsizeiptr size
Definition: glext.h:5919
unsigned short __u16
Definition: compat.h:89
Definition: dhcpd.h:61
GLdouble s
Definition: gl.h:2039
UINT op
Definition: effect.c:223

◆ utf8_wctomb()

int utf8_wctomb ( __u8 s,
wchar_t  wc,
int  maxlen 
)

Definition at line 108 of file nls_base.c.

109 {
110  long l;
111  int c, nc;
112  struct utf8_table *t;
113 
114  if (s == 0)
115  return 0;
116 
117  l = wc;
118  nc = 0;
119  for (t = utf8_table; t->cmask && maxlen; t++, maxlen--) {
120  nc++;
121  if (l <= t->lmask) {
122  c = t->shift;
123  *s = (__u8)(t->cval | (l >> c));
124  while (c > 0) {
125  c -= 6;
126  s++;
127  *s = 0x80 | (__u8)((l >> c) & 0x3F);
128  }
129  return nc;
130  }
131  }
132  return -1;
133 }
unsigned char __u8
Definition: compat.h:88
GLdouble GLdouble t
Definition: gl.h:2047
long lmask
Definition: nls_base.c:33
r l[0]
Definition: byte_order.h:167
const GLubyte * c
Definition: glext.h:8905
GLdouble s
Definition: gl.h:2039
int cmask
Definition: nls_base.c:30
#define c
Definition: ke_i.h:80

Referenced by utf8_wcstombs().

Variable Documentation

◆ charset2lower

unsigned char charset2lower[256]
static

Definition at line 368 of file nls_base.c.

◆ charset2uni

wchar_t charset2uni[256]

Definition at line 245 of file nls_base.c.

Referenced by char2uni().

◆ charset2upper

unsigned char charset2upper[256]
static

Definition at line 404 of file nls_base.c.

◆ nls_lock

spinlock_t nls_lock

Definition at line 23 of file nls_base.c.

Referenced by find_nls(), register_nls(), and unregister_nls().

◆ page00

unsigned char page00[256]
static

Definition at line 328 of file nls_base.c.

◆ page_uni2charset

unsigned char* page_uni2charset[256]
static
Initial value:
= {
}
static unsigned char page00[256]
Definition: nls_base.c:328

Definition at line 364 of file nls_base.c.

Referenced by uni2char().

◆ tables

◆ utf8_table

Initial value:
=
{
{0x80, 0x00, 0*6, 0x7F, 0, },
{0xE0, 0xC0, 1*6, 0x7FF, 0x80, },
{0xF0, 0xE0, 2*6, 0xFFFF, 0x800, },
{0xF8, 0xF0, 3*6, 0x1FFFFF, 0x10000, },
{0xFC, 0xF8, 4*6, 0x3FFFFFF, 0x200000, },
{0xFE, 0xFC, 5*6, 0x7FFFFFFF, 0x4000000, },
{0, }
}

Definition at line 37 of file nls_base.c.