Debian Codegraph @

Source/winchar.cpp

raw
1 /*
2 * winchar.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17 #include "Platform.h"
18 #include "winchar.h"
19 #include "util.h"
20 #include "utf.h"
21
22 #include <stdexcept>
23 #include <cassert>
24
25 using std::runtime_error;
26
27 int WinWStrICmpASCII(const WINWCHAR *a, const char *b)
28 {
29 int diff = 0;
30 do
31 diff = static_cast<int>(S7ChLwr(*a)) - static_cast<int>(S7ChLwr(*b++));
32 while (*a++ && !diff);
33 return diff;
34 }
35 int WinWStrNICmpASCII(const WINWCHAR *a, const char *b, size_t n)
36 {
37 int diff = 0;
38 for ( ; n--; ++a, ++b )
39 {
40 diff = static_cast<int>(S7ChLwr(*a)) - static_cast<int>(S7ChLwr(*b));
41 if (diff || !*a) break;
42 }
43 return diff;
44 }
45
46 WINWCHAR* WinWStrDupFromChar(const char *s, unsigned int cp)
47 {
48 int cch = MultiByteToWideChar(cp, 0, s, -1, 0, 0);
49 wchar_t *p = (wchar_t*) malloc(cch * sizeof(wchar_t));
50 if (p)
51 {
52 MultiByteToWideChar(cp, 0, s, -1, p, cch);
53 #ifndef _WIN32
54 wchar_t *p2 = (wchar_t*) WinWStrDupFromWC(p);
55 free(p), p = p2;
56 #endif
57 }
58 return (WINWCHAR*) p;
59 }
60
61 #ifndef _WIN32
62 size_t WinWStrLen(const WINWCHAR *s)
63 {
64 #ifdef MAKENSIS // Only makensis implements all the functions in utf.cpp
65 return StrLenUTF16(s);
66 #else
67 return sizeof(wchar_t) == 2 ? wcslen((wchar_t*)s) : InlineStrLenUTF16(s);
68 #endif
69 }
70
71 WINWCHAR* WinWStrCpy(WINWCHAR *d, const WINWCHAR *s)
72 {
73 WINWCHAR *ret = d;
74 while (*s) *d++ = *s++;
75 *d = 0;
76 return ret;
77 }
78
79 WINWCHAR* WinWStrNCpy(WINWCHAR *d, const WINWCHAR *s, size_t n)
80 {
81 WINWCHAR *ret = d;
82 while (n && *s) *d++ = *s++, n--;
83 while (n--) *d++ = 0;
84 return ret;
85 }
86
87 int WinWStrCmp(const WINWCHAR *a, const WINWCHAR *b)
88 {
89 int diff = 0;
90 do
91 diff = static_cast<int>(*a) - static_cast<int>(*b++);
92 while (*a++ && !diff);
93 return diff;
94 }
95
96 WINWCHAR* WinWStrDupFromWinWStr(const WINWCHAR *s)
97 {
98 WINWCHAR *d = (WINWCHAR*) malloc((WinWStrLen(s) + 1) * sizeof(WINWCHAR));
99 if (d) WinWStrCpy(d, s);
100 assert(!d || !IS_INTRESOURCE(d)); // DialogTemplate strings can be ATOMs
101 return d;
102 }
103
104 WINWCHAR* WinWStrDupFromWC(const wchar_t *s)
105 {
106 #ifdef MAKENSIS
107 WCToUTF16LEHlpr cnv;
108 if (!cnv.Create(s)) throw runtime_error("Unicode conversion failed");
109 return (WINWCHAR*) cnv.Detach();
110 #else
111 // NOTE: Anything outside the ASCII range will not convert correctly!
112 size_t cch = wcslen(s);
113 WINWCHAR* p = (WINWCHAR*) malloc(++cch * 2);
114 for (size_t i = 0; p && i < cch; ++i) p[i] = FIX_ENDIAN_INT16((USHORT)(s[i]));
115 return p;
116 #endif
117 }
118
119 int WinWStrToInt(const WINWCHAR *s)
120 {
121 unsigned int v = 0, base = 10, top = '9';
122 int sign = 1;
123 if (*s == _T('-')) ++s, sign = -1;
124 for ( unsigned int c;; )
125 {
126 if ((c = *s++) >= '0' && c <= top) c -= '0'; else break;
127 v *= base, v += c;
128 }
129 return ((int)v) * sign;
130 }
131 #endif // ~!_WIN32