xref: /freebsd/contrib/expat/xmlwf/codepage.c (revision 646a7fea)
15bb6a25fSPoul-Henning Kamp /*
25bb6a25fSPoul-Henning Kamp                             __  __            _
35bb6a25fSPoul-Henning Kamp                          ___\ \/ /_ __   __ _| |_
45bb6a25fSPoul-Henning Kamp                         / _ \\  /| '_ \ / _` | __|
55bb6a25fSPoul-Henning Kamp                        |  __//  \| |_) | (_| | |_
65bb6a25fSPoul-Henning Kamp                         \___/_/\_\ .__/ \__,_|\__|
7220ed979SColeman Kane                                  |_| XML parser
85bb6a25fSPoul-Henning Kamp 
95bb6a25fSPoul-Henning Kamp    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
105bb6a25fSPoul-Henning Kamp    Copyright (c) 2000      Clark Cooper <coopercc@users.sourceforge.net>
115bb6a25fSPoul-Henning Kamp    Copyright (c) 2002      Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
125bb6a25fSPoul-Henning Kamp    Copyright (c) 2005-2006 Karl Waclawek <karl@waclawek.net>
135bb6a25fSPoul-Henning Kamp    Copyright (c) 2016-2019 Sebastian Pipping <sebastian@pipping.org>
145bb6a25fSPoul-Henning Kamp    Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
155bb6a25fSPoul-Henning Kamp    Licensed under the MIT license:
165bb6a25fSPoul-Henning Kamp 
175bb6a25fSPoul-Henning Kamp    Permission is  hereby granted,  free of charge,  to any  person obtaining
185bb6a25fSPoul-Henning Kamp    a  copy  of  this  software   and  associated  documentation  files  (the
195bb6a25fSPoul-Henning Kamp    "Software"),  to  deal in  the  Software  without restriction,  including
205bb6a25fSPoul-Henning Kamp    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
215bb6a25fSPoul-Henning Kamp    distribute, sublicense, and/or sell copies of the Software, and to permit
225bb6a25fSPoul-Henning Kamp    persons  to whom  the Software  is  furnished to  do so,  subject to  the
23220ed979SColeman Kane    following conditions:
245bb6a25fSPoul-Henning Kamp 
255bb6a25fSPoul-Henning Kamp    The above copyright  notice and this permission notice  shall be included
265bb6a25fSPoul-Henning Kamp    in all copies or substantial portions of the Software.
275bb6a25fSPoul-Henning Kamp 
28220ed979SColeman Kane    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
295bb6a25fSPoul-Henning Kamp    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
305bb6a25fSPoul-Henning Kamp    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
315bb6a25fSPoul-Henning Kamp    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
325bb6a25fSPoul-Henning Kamp    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
335bb6a25fSPoul-Henning Kamp    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
345bb6a25fSPoul-Henning Kamp    USE OR OTHER DEALINGS IN THE SOFTWARE.
355bb6a25fSPoul-Henning Kamp */
365bb6a25fSPoul-Henning Kamp 
375bb6a25fSPoul-Henning Kamp #include "codepage.h"
385bb6a25fSPoul-Henning Kamp #include "internal.h" /* for UNUSED_P only */
395bb6a25fSPoul-Henning Kamp 
405bb6a25fSPoul-Henning Kamp #if defined(_WIN32)
415bb6a25fSPoul-Henning Kamp #  define STRICT 1
425bb6a25fSPoul-Henning Kamp #  define WIN32_LEAN_AND_MEAN 1
435bb6a25fSPoul-Henning Kamp 
445bb6a25fSPoul-Henning Kamp #  include <windows.h>
455bb6a25fSPoul-Henning Kamp #endif /* defined(_WIN32) */
465bb6a25fSPoul-Henning Kamp 
475bb6a25fSPoul-Henning Kamp int
codepageMap(int cp,int * map)485bb6a25fSPoul-Henning Kamp codepageMap(int cp, int *map) {
495bb6a25fSPoul-Henning Kamp #if defined(_WIN32)
505bb6a25fSPoul-Henning Kamp   int i;
515bb6a25fSPoul-Henning Kamp   CPINFO info;
525bb6a25fSPoul-Henning Kamp   if (! GetCPInfo(cp, &info) || info.MaxCharSize > 2)
535bb6a25fSPoul-Henning Kamp     return 0;
545bb6a25fSPoul-Henning Kamp   for (i = 0; i < 256; i++)
555bb6a25fSPoul-Henning Kamp     map[i] = -1;
565bb6a25fSPoul-Henning Kamp   if (info.MaxCharSize > 1) {
575bb6a25fSPoul-Henning Kamp     for (i = 0; i < MAX_LEADBYTES; i += 2) {
585bb6a25fSPoul-Henning Kamp       int j, lim;
595bb6a25fSPoul-Henning Kamp       if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
605bb6a25fSPoul-Henning Kamp         break;
615bb6a25fSPoul-Henning Kamp       lim = info.LeadByte[i + 1];
625bb6a25fSPoul-Henning Kamp       for (j = info.LeadByte[i]; j <= lim; j++)
635bb6a25fSPoul-Henning Kamp         map[j] = -2;
645bb6a25fSPoul-Henning Kamp     }
655bb6a25fSPoul-Henning Kamp   }
665bb6a25fSPoul-Henning Kamp   for (i = 0; i < 256; i++) {
675bb6a25fSPoul-Henning Kamp     if (map[i] == -1) {
685bb6a25fSPoul-Henning Kamp       char c = (char)i;
69       unsigned short n;
70       if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, &c, 1,
71                               &n, 1)
72           == 1)
73         map[i] = n;
74     }
75   }
76   return 1;
77 #else
78   UNUSED_P(cp);
79   UNUSED_P(map);
80   return 0;
81 #endif
82 }
83 
84 int
codepageConvert(int cp,const char * p)85 codepageConvert(int cp, const char *p) {
86 #if defined(_WIN32)
87   unsigned short c;
88   if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c,
89                           1)
90       == 1)
91     return c;
92   return -1;
93 #else
94   UNUSED_P(cp);
95   UNUSED_P(p);
96   return -1;
97 #endif
98 }
99