xref: /freebsd/contrib/expat/xmlwf/codepage.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
10a48773fSEric van Gyzen /*
20a48773fSEric van Gyzen                             __  __            _
30a48773fSEric van Gyzen                          ___\ \/ /_ __   __ _| |_
40a48773fSEric van Gyzen                         / _ \\  /| '_ \ / _` | __|
50a48773fSEric van Gyzen                        |  __//  \| |_) | (_| | |_
60a48773fSEric van Gyzen                         \___/_/\_\ .__/ \__,_|\__|
70a48773fSEric van Gyzen                                  |_| XML parser
80a48773fSEric van Gyzen 
90a48773fSEric van Gyzen    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10*cc68614dSXin LI    Copyright (c) 2000      Clark Cooper <coopercc@users.sourceforge.net>
11*cc68614dSXin LI    Copyright (c) 2002      Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12*cc68614dSXin LI    Copyright (c) 2005-2006 Karl Waclawek <karl@waclawek.net>
13*cc68614dSXin LI    Copyright (c) 2016-2019 Sebastian Pipping <sebastian@pipping.org>
14*cc68614dSXin LI    Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
150a48773fSEric van Gyzen    Licensed under the MIT license:
160a48773fSEric van Gyzen 
170a48773fSEric van Gyzen    Permission is  hereby granted,  free of charge,  to any  person obtaining
180a48773fSEric van Gyzen    a  copy  of  this  software   and  associated  documentation  files  (the
190a48773fSEric van Gyzen    "Software"),  to  deal in  the  Software  without restriction,  including
200a48773fSEric van Gyzen    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
210a48773fSEric van Gyzen    distribute, sublicense, and/or sell copies of the Software, and to permit
220a48773fSEric van Gyzen    persons  to whom  the Software  is  furnished to  do so,  subject to  the
230a48773fSEric van Gyzen    following conditions:
240a48773fSEric van Gyzen 
250a48773fSEric van Gyzen    The above copyright  notice and this permission notice  shall be included
260a48773fSEric van Gyzen    in all copies or substantial portions of the Software.
270a48773fSEric van Gyzen 
280a48773fSEric van Gyzen    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
290a48773fSEric van Gyzen    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
300a48773fSEric van Gyzen    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
310a48773fSEric van Gyzen    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
320a48773fSEric van Gyzen    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
330a48773fSEric van Gyzen    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
340a48773fSEric van Gyzen    USE OR OTHER DEALINGS IN THE SOFTWARE.
355bb6a25fSPoul-Henning Kamp */
365bb6a25fSPoul-Henning Kamp 
375bb6a25fSPoul-Henning Kamp #include "codepage.h"
38be8aff81SXin LI #include "internal.h" /* for UNUSED_P only */
395bb6a25fSPoul-Henning Kamp 
400a48773fSEric van Gyzen #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>
45*cc68614dSXin LI #endif /* defined(_WIN32) */
465bb6a25fSPoul-Henning Kamp 
475bb6a25fSPoul-Henning Kamp int
codepageMap(int cp,int * map)486b2c1e49SXin LI codepageMap(int cp, int *map) {
49*cc68614dSXin LI #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) {
57220ed979SColeman Kane     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];
62220ed979SColeman Kane       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;
695bb6a25fSPoul-Henning Kamp       unsigned short n;
706b2c1e49SXin LI       if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, &c, 1,
716b2c1e49SXin LI                               &n, 1)
726b2c1e49SXin LI           == 1)
735bb6a25fSPoul-Henning Kamp         map[i] = n;
745bb6a25fSPoul-Henning Kamp     }
755bb6a25fSPoul-Henning Kamp   }
765bb6a25fSPoul-Henning Kamp   return 1;
77*cc68614dSXin LI #else
78*cc68614dSXin LI   UNUSED_P(cp);
79*cc68614dSXin LI   UNUSED_P(map);
80*cc68614dSXin LI   return 0;
81*cc68614dSXin LI #endif
825bb6a25fSPoul-Henning Kamp }
835bb6a25fSPoul-Henning Kamp 
845bb6a25fSPoul-Henning Kamp int
codepageConvert(int cp,const char * p)856b2c1e49SXin LI codepageConvert(int cp, const char *p) {
86*cc68614dSXin LI #if defined(_WIN32)
875bb6a25fSPoul-Henning Kamp   unsigned short c;
886b2c1e49SXin LI   if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c,
896b2c1e49SXin LI                           1)
906b2c1e49SXin LI       == 1)
915bb6a25fSPoul-Henning Kamp     return c;
925bb6a25fSPoul-Henning Kamp   return -1;
93*cc68614dSXin LI #else
946b2c1e49SXin LI   UNUSED_P(cp);
956b2c1e49SXin LI   UNUSED_P(p);
965bb6a25fSPoul-Henning Kamp   return -1;
97*cc68614dSXin LI #endif
985bb6a25fSPoul-Henning Kamp }
99