summaryrefslogtreecommitdiff
path: root/source3/lib/charset.c
blob: cd8c7577c96eb99be1b3c1e04004f7467989c0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Character set handling
   Copyright (C) Andrew Tridgell 1992-1995
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#define CHARSET_C
#include "includes.h"

extern int DEBUGLEVEL;

char xx_dos_char_map[256];
char xx_upper_char_map[256];
char xx_lower_char_map[256];

char *dos_char_map = NULL;
char *upper_char_map = NULL;
char *lower_char_map = NULL;

static void add_dos_char(int lower, int upper)
{
  DEBUG(6,("Adding chars 0%o 0%o\n",lower,upper));
  if (lower) dos_char_map[(char)lower] = 1;
  if (upper) dos_char_map[(char)upper] = 1;
  if (lower && upper) {
    lower_char_map[(char)upper] = (char)lower;
    upper_char_map[(char)lower] = (char)upper;
    lower_char_map[(char)lower] = (char)lower;
    upper_char_map[(char)upper] = (char)upper;
  }
}

/****************************************************************************
initialise the charset arrays
****************************************************************************/
void charset_initialise(void)
{
  int i;

  dos_char_map = &xx_dos_char_map[128];
  upper_char_map = &xx_upper_char_map[128];
  lower_char_map = &xx_lower_char_map[128];

  for (i= -128;i<=127;i++) {
    dos_char_map[(char)i] = 0;
  }

  for (i=0;i<=127;i++) {
    if (isalnum((char)i) || strchr("._^$~!#%&-{}()@'`",(char)i))
      add_dos_char(i,0);
  }

  for (i= -128;i<=127;i++) {
    char c = (char)i;
    upper_char_map[i] = lower_char_map[i] = c;
    if (isupper(c)) lower_char_map[c] = tolower(c);
    if (islower(c)) upper_char_map[c] = toupper(c);
  }

  /* valid for all DOS PC */
  add_dos_char(142,0);     /* A trema      */
  add_dos_char(143,0);     /* A o          */
  add_dos_char(144,0);     /* E '          */
  add_dos_char(146,0);     /* AE           */
  add_dos_char(153,0);     /* O trema      */
  add_dos_char(154,0);     /* U trema      */
  add_dos_char(165,0);     /* N tilda      */
  add_dos_char(128,0);     /* C cedille    */
  add_dos_char(156,0);     /* Pound        */
  add_dos_char(183,0);     /* A `     (WIN)*/
  add_dos_char(157,0);     /* Phi     (WIN)*/
  add_dos_char(212,0);     /* E`      (WIN)*/
}


/*******************************************************************
add characters depending on a string passed by the user
********************************************************************/
void add_char_string(char *s)
{
  char *extra_chars = (char *)strdup(s);
  char *t;
  if (!extra_chars) return;

  for (t=strtok(extra_chars," \t\r\n"); t; t=strtok(NULL," \t\r\n")) {
    char c1=0,c2=0;
    int i1=0,i2=0;
    if (isdigit(*t) || (*t)=='-') {
      sscanf(t,"%i:%i",&i1,&i2);
      add_dos_char(i1,i2);
    } else {
      sscanf(t,"%c:%c",&c1,&c2);
      add_dos_char(c1,c2);
    }
  }

  free(extra_chars);
}