idnx 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +239 -0
- data/README.md +52 -0
- data/lib/idnx.rb +20 -0
- data/lib/idnx/idn2.rb +60 -0
- data/lib/idnx/version.rb +5 -0
- data/lib/idnx/windows.rb +102 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 931f92a4d37e74baad51a046058aa31aa0054563936e40e451f77eecd8eaf1b1
|
4
|
+
data.tar.gz: bed5e05b59d801504c2110d851723e09b302dc4a48169334ddebb4a493ed07d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 905cf486978554c95bb7c7b912338439cb4acd431bb50d11cbe6afaee8047a9b53e28f522b4986873817ca8c76523eab7a5b784109f877001846228e7212ebfa
|
7
|
+
data.tar.gz: c94fd02aaf52386076a9a022f922d1962c1ce813b82a403f35f0d50fd4c68bb719a7def9a401a34a1aca62f8ac8443695e9dbadba5ed7bf8316459ea0b001a16
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,239 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
https://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
Copyright 2013-2017 Docker, Inc.
|
180
|
+
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
182
|
+
you may not use this file except in compliance with the License.
|
183
|
+
You may obtain a copy of the License at
|
184
|
+
|
185
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
186
|
+
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
190
|
+
See the License for the specific language governing permissions and
|
191
|
+
limitations under the License.
|
192
|
+
|
193
|
+
|
194
|
+
* lib/httpx/domain_name.rb
|
195
|
+
|
196
|
+
This file is derived from the implementation of punycode available at
|
197
|
+
here:
|
198
|
+
|
199
|
+
https://www.verisign.com/en_US/channel-resources/domain-registry-products/idn-sdks/index.xhtml
|
200
|
+
|
201
|
+
Copyright (C) 2000-2002 Verisign Inc., All rights reserved.
|
202
|
+
|
203
|
+
Redistribution and use in source and binary forms, with or
|
204
|
+
without modification, are permitted provided that the following
|
205
|
+
conditions are met:
|
206
|
+
|
207
|
+
1) Redistributions of source code must retain the above copyright
|
208
|
+
notice, this list of conditions and the following disclaimer.
|
209
|
+
|
210
|
+
2) Redistributions in binary form must reproduce the above copyright
|
211
|
+
notice, this list of conditions and the following disclaimer in
|
212
|
+
the documentation and/or other materials provided with the
|
213
|
+
distribution.
|
214
|
+
|
215
|
+
3) Neither the name of the VeriSign Inc. nor the names of its
|
216
|
+
contributors may be used to endorse or promote products derived
|
217
|
+
from this software without specific prior written permission.
|
218
|
+
|
219
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
220
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
221
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
222
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
223
|
+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
224
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
225
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
226
|
+
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
227
|
+
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
228
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
229
|
+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
230
|
+
POSSIBILITY OF SUCH DAMAGE.
|
231
|
+
|
232
|
+
This software is licensed under the BSD open source license. For more
|
233
|
+
information visit www.opensource.org.
|
234
|
+
|
235
|
+
Authors:
|
236
|
+
John Colosi (VeriSign)
|
237
|
+
Srikanth Veeramachaneni (VeriSign)
|
238
|
+
Nagesh Chigurupati (Verisign)
|
239
|
+
Praveen Srinivasan(Verisign)
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Idnx
|
2
|
+
|
3
|
+
|
4
|
+
`idnx` provides a Ruby API for decoding Internationalized domain names into Punycode.
|
5
|
+
|
6
|
+
It provides multi-platform support by using the most approriate strategy based on the target environment:
|
7
|
+
|
8
|
+
* It uses (and requires the installation of) [libidn2](https://github.com/libidn/libidn2) in Linux / MacOS;
|
9
|
+
* It uses [the appropriate winnls APIs](https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-idntoascii) in Windows;
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
If you're on Linux or Mac OS, you'll have to install `libidn2` first:
|
14
|
+
|
15
|
+
```
|
16
|
+
# Mac OS
|
17
|
+
> brew install libidn2
|
18
|
+
# Ubuntu, as an example
|
19
|
+
> apt-get install idn2
|
20
|
+
```
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'idnx'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle install
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install idnx
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require "idnx"
|
40
|
+
|
41
|
+
Idnx.to_punycode("bücher.de") #=> "xn--bcher-kva.de"
|
42
|
+
```
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
If you want to contribute, fork this project, and submit changes via a PR on github.
|
47
|
+
|
48
|
+
For running tests, you can run `rake test`.
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/HoneyryderChuck/idnx.
|
data/lib/idnx.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "idnx/version"
|
4
|
+
require "ffi"
|
5
|
+
|
6
|
+
module Idnx
|
7
|
+
Error = Class.new(StandardError)
|
8
|
+
|
9
|
+
module_function
|
10
|
+
|
11
|
+
def to_punycode(hostname)
|
12
|
+
Lib.lookup(hostname)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
if FFI::Platform.windows?
|
17
|
+
require "idnx/windows"
|
18
|
+
else
|
19
|
+
require "idnx/idn2"
|
20
|
+
end
|
data/lib/idnx/idn2.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Idnx
|
4
|
+
module Lib
|
5
|
+
extend FFI::Library
|
6
|
+
|
7
|
+
if FFI::Platform.mac?
|
8
|
+
ffi_lib ["libidn2", "libidn2.0"]
|
9
|
+
else
|
10
|
+
ffi_lib ["libidn2.so", "libidn2.so.0"]
|
11
|
+
end
|
12
|
+
|
13
|
+
attach_function :idn2_check_version, [:string], :string
|
14
|
+
|
15
|
+
VERSION = idn2_check_version(nil)
|
16
|
+
|
17
|
+
IDN2_OK = 0
|
18
|
+
|
19
|
+
IDN2_NFC_INPUT = 1
|
20
|
+
IDN2_TRANSITIONAL = 4
|
21
|
+
IDN2_NONTRANSITIONAL = 8
|
22
|
+
|
23
|
+
FLAGS = if Gem::Version.new(VERSION) >= Gem::Version.new("0.14.0")
|
24
|
+
IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL
|
25
|
+
else
|
26
|
+
IDN2_NFC_INPUT
|
27
|
+
end
|
28
|
+
|
29
|
+
attach_function :idn2_lookup_ul, [:string, :pointer, :int], :int
|
30
|
+
attach_function :idn2_strerror, [:int], :string
|
31
|
+
attach_function :idn2_free, [:pointer], :void
|
32
|
+
|
33
|
+
module_function
|
34
|
+
|
35
|
+
def lookup(hostname)
|
36
|
+
string_ptr = FFI::MemoryPointer.new(:pointer)
|
37
|
+
result = idn2_lookup_ul(hostname, string_ptr, FLAGS)
|
38
|
+
|
39
|
+
if result != IDN2_OK
|
40
|
+
result = idn2_lookup_ul(hostname, string_ptr, IDN2_TRANSITIONAL)
|
41
|
+
end
|
42
|
+
|
43
|
+
if result != IDN2_OK
|
44
|
+
string_ptr.free
|
45
|
+
raise Error, "Failed to convert \"#{hostname}\" to ascii; (error: #{idn2_strerror(result)})"
|
46
|
+
end
|
47
|
+
|
48
|
+
ptr = string_ptr.read_pointer
|
49
|
+
|
50
|
+
raise Error, "Failed to read \"#{hostname}\" to ascii" if ptr.null?
|
51
|
+
|
52
|
+
ascii = ptr.read_string
|
53
|
+
|
54
|
+
idn2_free(ptr)
|
55
|
+
string_ptr.free
|
56
|
+
|
57
|
+
ascii
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/idnx/version.rb
ADDED
data/lib/idnx/windows.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "win32ole"
|
4
|
+
|
5
|
+
module Idnx
|
6
|
+
module Lib
|
7
|
+
extend FFI::Library
|
8
|
+
|
9
|
+
ffi_lib "Normaliz.dll"
|
10
|
+
ffi_lib "kernel32.dll"
|
11
|
+
|
12
|
+
IDN_MAX_LENGTH = 255
|
13
|
+
MB_ERR_INVALID_CHARS = 0x00000008
|
14
|
+
|
15
|
+
ERROR_INSUFFICIENT_BUFFER = 0x7A
|
16
|
+
ERROR_INVALID_FLAGS = 0x3EC
|
17
|
+
ERROR_INVALID_NAME = 0x7B
|
18
|
+
ERROR_INVALID_PARAMETER = 0x57
|
19
|
+
ERROR_NO_UNICODE_TRANSLATION = 0x459
|
20
|
+
|
21
|
+
# int IdnToAscii(
|
22
|
+
# DWORD dwFlags,
|
23
|
+
# LPCWSTR lpUnicodeCharStr,
|
24
|
+
# int cchUnicodeChar,
|
25
|
+
# LPWSTR lpASCIICharStr,
|
26
|
+
# int cchASCIIChar
|
27
|
+
# );
|
28
|
+
#
|
29
|
+
# int MultiByteToWideChar(
|
30
|
+
# UINT CodePage,
|
31
|
+
# DWORD dwFlags,
|
32
|
+
# _In_NLS_string_(cbMultiByte)LPCCH lpMultiByteStr,
|
33
|
+
# int cbMultiByte,
|
34
|
+
# LPWSTR lpWideCharStr,
|
35
|
+
# int cchWideChar
|
36
|
+
# );
|
37
|
+
#
|
38
|
+
# int WideCharToMultiByte(
|
39
|
+
# UINT CodePage,
|
40
|
+
# DWORD dwFlags,
|
41
|
+
# _In_NLS_string_(cchWideChar)LPCWCH lpWideCharStr,
|
42
|
+
# int cchWideChar,
|
43
|
+
# LPSTR lpMultiByteStr,
|
44
|
+
# int cbMultiByte,
|
45
|
+
# LPCCH lpDefaultChar,
|
46
|
+
# LPBOOL lpUsedDefaultChar
|
47
|
+
# );
|
48
|
+
attach_function :IdnToAscii, [:uint, :pointer, :int, :pointer, :int], :int
|
49
|
+
attach_function :MultiByteToWideChar, [:uint, :uint, :string, :int, :pointer, :int], :int
|
50
|
+
attach_function :WideCharToMultiByte, [:uint, :uint, :pointer, :int, :pointer, :int, :pointer, :pointer], :int
|
51
|
+
|
52
|
+
module_function
|
53
|
+
|
54
|
+
def lookup(hostname)
|
55
|
+
# turn to wchar
|
56
|
+
wchar_len = MultiByteToWideChar(WIN32OLE::CP_UTF8, MB_ERR_INVALID_CHARS, hostname, -1, nil, 0)
|
57
|
+
raise Error, "Failed to convert \"#{hostname}\" to wchar" if wchar_len.zero?
|
58
|
+
wchar_ptr = FFI::MemoryPointer.new(:wchar_t, wchar_len)
|
59
|
+
wchar_len = MultiByteToWideChar(WIN32OLE::CP_UTF8, 0, hostname, -1, wchar_ptr, wchar_len)
|
60
|
+
raise Error, "Failed to convert \"#{hostname}\" to wchar" if wchar_len.zero?
|
61
|
+
|
62
|
+
# translate to punycode
|
63
|
+
punycode = FFI::MemoryPointer.new(:wchar_t, IDN_MAX_LENGTH)
|
64
|
+
punycode_len = IdnToAscii(0, wchar_ptr, -1, punycode, IDN_MAX_LENGTH)
|
65
|
+
wchar_ptr.free
|
66
|
+
|
67
|
+
if punycode_len == 0
|
68
|
+
last_error = FFI::LastError.error
|
69
|
+
|
70
|
+
# operation completed successfully, hostname is not an IDN
|
71
|
+
# return hostname if last_error == 0
|
72
|
+
|
73
|
+
message = case last_error
|
74
|
+
when ERROR_INSUFFICIENT_BUFFER
|
75
|
+
"The supplied buffer size was not large enough, or it was incorrectly set to NULL"
|
76
|
+
when ERROR_INVALID_FLAGS
|
77
|
+
"The values supplied for flags were not valid"
|
78
|
+
when ERROR_INVALID_NAME
|
79
|
+
"An invalid name was supplied to the function"
|
80
|
+
when ERROR_INVALID_PARAMETER
|
81
|
+
"Any of the parameter values was invalid"
|
82
|
+
when ERROR_NO_UNICODE_TRANSLATION
|
83
|
+
"An invalid Unicode was found in a string"
|
84
|
+
else
|
85
|
+
"Failed to convert \"#{hostname}\"; (error: #{last_error})" \
|
86
|
+
"\n\nhttps://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes#system-error-codes-1"
|
87
|
+
end
|
88
|
+
punycode.free
|
89
|
+
raise Error, message
|
90
|
+
end
|
91
|
+
|
92
|
+
# turn to unicode
|
93
|
+
unicode_len = WideCharToMultiByte(WIN32OLE::CP_UTF8, 0, punycode, -1, nil, 0, nil, nil)
|
94
|
+
raise Error, "Failed to convert \"#{hostname}\" to utf8" if unicode_len.zero?
|
95
|
+
utf8_ptr = FFI::MemoryPointer.new(:char, unicode_len)
|
96
|
+
unicode_len = WideCharToMultiByte(WIN32OLE::CP_UTF8, 0, punycode, -1, utf8_ptr, unicode_len, nil, nil)
|
97
|
+
raise Error, "Failed to convert \"#{hostname}\" to utf8" if unicode_len.zero?
|
98
|
+
unicode = utf8_ptr.read_string(utf8_ptr.size)
|
99
|
+
unicode.strip! # remove null-byte
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: idnx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- HoneyryderChuck
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
description: |2
|
28
|
+
Converts International Domain Names into Punycode.
|
29
|
+
It uses (via FFI) 'libidn2' for Mac and Linux; for Windows, it uses native APIs.
|
30
|
+
email:
|
31
|
+
- cardoso_tiago@hotmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files:
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
files:
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- lib/idnx.rb
|
41
|
+
- lib/idnx/idn2.rb
|
42
|
+
- lib/idnx/version.rb
|
43
|
+
- lib/idnx/windows.rb
|
44
|
+
homepage: https://github.com/honeyryderchuck/idnx
|
45
|
+
licenses: []
|
46
|
+
metadata:
|
47
|
+
homepage_uri: https://github.com/honeyryderchuck/idnx
|
48
|
+
source_code_uri: https://github.com/honeyryderchuck/idnx
|
49
|
+
changelog_uri: https://github.com/honeyryderchuck/idnx/-/blob/master/CHANGELOG.md
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 2.4.0
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubygems_version: 3.2.15
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: Converts International Domain Names into Punycode. It uses (via FFI) 'libidn2'
|
69
|
+
for Mac and Linux; for Windows, it uses native APIs.
|
70
|
+
test_files: []
|