Skip to content

Commit 5192c5b

Browse files
committed
✨ windows_code_page small dedicated tests
1 parent 818e0c3 commit 5192c5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+403
-122
lines changed

include/ztd/cuneicode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <ztd/cuneicode/mcerr.h>
4242
#include <ztd/cuneicode/mcstate.h>
4343
#include <ztd/cuneicode/detection.h>
44-
#include <ztd/cuneicode/max_output.h>
44+
#include <ztd/cuneicode/max_input_output.h>
4545
#include <ztd/cuneicode/error_handler.h>
4646

4747
#include <ztd/idk/charN_t.h>

include/ztd/cuneicode/detail/buffer_size.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include <ztd/cuneicode/version.h>
3636

37-
#include <ztd/cuneicode/max_output.h>
37+
#include <ztd/cuneicode/max_input_output.h>
3838

3939
#define CNC_DEFAULT_CONVERSION_INTERMEDIATE_BUFFER_SIZE \
4040
(((ZTD_CUNEICODE_INTERMEDIATE_BUFFER_SUGGESTED_BYTE_SIZE_I_) < CNC_MC_MAX) \

include/ztd/cuneicode/detail/decl_boilerplate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include <ztd/cuneicode/version.h>
3636

37-
#include <ztd/cuneicode/max_output.h>
37+
#include <ztd/cuneicode/max_input_output.h>
3838
#include <ztd/cuneicode/mcerr.h>
3939
#include <ztd/cuneicode/mcstate.h>
4040
#include <ztd/cuneicode/result.h>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// ============================================================================
2+
//
3+
// ztd.cuneicode
4+
// Copyright © JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
5+
// Contact: [email protected]
6+
//
7+
// Commercial License Usage
8+
// Licensees holding valid commercial ztd.cuneicode licenses may use this file
9+
// in accordance with the commercial license agreement provided with the
10+
// Software or, alternatively, in accordance with the terms contained in
11+
// a written agreement between you and Shepherd's Oasis, LLC.
12+
// For licensing terms and conditions see your agreement. For
13+
// further information contact [email protected].
14+
//
15+
// Apache License Version 2 Usage
16+
// Alternatively, this file may be used under the terms of Apache License
17+
// Version 2.0 (the "License"); you may not use this file except in compliance
18+
// with the License. You may obtain a copy of the License at
19+
//
20+
// https://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
//
28+
// ========================================================================= //
29+
30+
#ifndef ZTD_CUNEICODE_MAX_INPUT_OUTPUT_H
31+
#define ZTD_CUNEICODE_MAX_INPUT_OUTPUT_H
32+
33+
#pragma once
34+
35+
#include <ztd/cuneicode/version.h>
36+
37+
//////
38+
/// @addtogroup ztd_cuneicode_max_macros Maximum-Size Macros
39+
/// @{
40+
41+
//////
42+
/// @brief The maximum size that can be output by a single `cnc_cxnrtomcn` function call.
43+
#define CNC_MC_MAX 64
44+
//////
45+
/// @brief The maximum size that can be output by a single `cnc_cxnrtomwcn` function call.
46+
#define CNC_MWC_MAX 32
47+
//////
48+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc32n` function call.
49+
#define CNC_C32_MAX 16
50+
//////
51+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc16n` function call.
52+
#define CNC_C16_MAX (CNC_C32_MAX * 2)
53+
//////
54+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc8n` function call.
55+
#define CNC_C8_MAX (CNC_C32_MAX * 4)
56+
57+
//////
58+
/// @brief The maximum size that can be output by a single `cnc_cxnrtomcn` function call.
59+
#define CNC_MC_INPUT_MAX 17
60+
//////
61+
/// @brief The maximum size that can be output by a single `cnc_cxnrtomwcn` function call.
62+
#define CNC_MWC_INPUT_MAX 17
63+
//////
64+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc32n` function call.
65+
#define CNC_C32_INPUT_MAX 17
66+
//////
67+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc16n` function call.
68+
#define CNC_C16_INPUT_MAX 17
69+
//////
70+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc8n` function call.
71+
#define CNC_C8_INPUT_MAX 17
72+
73+
//////
74+
/// @}
75+
76+
#endif

include/ztd/cuneicode/max_output.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@
5454
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc8n` function call.
5555
#define CNC_C8_MAX (CNC_C32_MAX * 4)
5656

57+
//////
58+
/// @brief The maximum size that can be output by a single `cnc_cxnrtomcn` function call.
59+
#define CNC_MC_INPUT_MAX 17
60+
//////
61+
/// @brief The maximum size that can be output by a single `cnc_cxnrtomwcn` function call.
62+
#define CNC_MWC_INPUT_MAX 17
63+
//////
64+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc32n` function call.
65+
#define CNC_C32_INPUT_MAX 17
66+
//////
67+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc16n` function call.
68+
#define CNC_C16_INPUT_MAX 17
69+
//////
70+
/// @brief The maximum size that can be output by a single `cnc_cxnrtoc8n` function call.
71+
#define CNC_C8_INPUT_MAX 17
72+
5773
//////
5874
/// @}
5975

include/ztd/cuneicode/result.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <ztd/cuneicode/version.h>
3636

3737
#include <ztd/cuneicode/mcerr.h>
38-
#include <ztd/cuneicode/max_output.h>
38+
#include <ztd/cuneicode/max_input_output.h>
3939
#include <ztd/cuneicode/prefix.h>
4040

4141
#include <ztd/idk/charN_t.h>

source/include/ztd/cuneicode/detail/core_mcharn.hpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ namespace cnc {
287287
BOOL __default_char_used = false;
288288
CHAR __default_char = '?';
289289
auto __used_defaults
290-
= ::ztd::__idk_detail::__windows::__multibyte_to_widechar_used_char(
290+
= ::ztd::__idk_detail::__windows::__widechar_to_multibyte_used_char(
291291
::ztd::__idk_detail::__windows::__code_page_active_thread, &__default_char,
292292
&__default_char_used);
293293
const size_t __intermediate_size
@@ -300,6 +300,9 @@ namespace cnc {
300300
: INT_MAX);
301301
for (size_t __intermediate_input_read = 1;
302302
__intermediate_input_read <= __intermediate_size; ++__intermediate_input_read) {
303+
if (__intermediate_input_read > CNC_MWC_INPUT_MAX) {
304+
break;
305+
}
303306
const int __win32_err = ::WideCharToMultiByte(
304307
::ztd::__idk_detail::__windows::__code_page_active_thread,
305308
WC_ERR_INVALID_CHARS, __intermediate_output,
@@ -309,10 +312,11 @@ namespace cnc {
309312
DWORD __last_win32_err = ::GetLastError();
310313
if (__last_win32_err == ERROR_NO_UNICODE_TRANSLATION
311314
|| __default_char_used) {
312-
// we can break early: it was illegal stuff that can't translate
313-
__p_src[0] = __initial_src;
314-
__p_src_len[0] = __initial_src_len;
315-
return cnc_mcerr_invalid_sequence;
315+
// loop around; we don't know if this is from a partial read (because
316+
// of our
317+
// artifical limitations) or because it's a genuine error. This is, of
318+
// course, the problem with these crappy 1990s/2000s APIs.
319+
continue;
316320
}
317321
else if (__last_win32_err == ERROR_INSUFFICIENT_BUFFER) {
318322
if (!_IsUnbounded) {
@@ -326,6 +330,16 @@ namespace cnc {
326330
}
327331
}
328332
else {
333+
if (__win32_err == 1) {
334+
// double-check if we were screwed over by the conversion: given
335+
// Win32's undocumented fuckups around this, the only way to know if
336+
// we actually failed is by checking if the single character we output
337+
// is equal to a replacement character, and if the replacement
338+
// character is NOT present in the original stream. The proper way to
339+
// do this is to call GetCPInfoExW and then using a comparison to the
340+
// MultiByte stream. But there's so many different things wrong with
341+
// it, and it's hard to know.
342+
}
329343
// okay, it should be good
330344
if (!_IsUnbounded) {
331345
if (__p_maybe_dst_len[0] < static_cast<size_t>(__win32_err)) {
@@ -1093,7 +1107,7 @@ namespace cnc {
10931107
= ::ztd::__idk_detail::__windows::__multibyte_to_widechar_flags(
10941108
ztd::__idk_detail::__windows::__code_page_active_thread);
10951109
for (; __input_read_size <= __initial_src_len; ++__input_read_size) {
1096-
if (__input_read_size > CNC_MC_MAX) {
1110+
if (__input_read_size > CNC_MC_INPUT_MAX) {
10971111
// can't do much else
10981112
return cnc_mcerr_invalid_sequence;
10991113
}
@@ -1103,9 +1117,8 @@ namespace cnc {
11031117
static_cast<int>(__input_read_size), __p_intermediate_output,
11041118
static_cast<int>(__intermediate_output_initial_size));
11051119
if (__win32_err == 0) {
1106-
if (::GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
1107-
return cnc_mcerr_invalid_sequence;
1108-
}
1120+
// not sure; we have to keep looping, unfortunately.
1121+
continue;
11091122
}
11101123
else {
11111124
__intermediate_size = static_cast<size_t>(__win32_err);

source/include/ztd/cuneicode/detail/transcode.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include <ztd/cuneicode/version.h>
3636

37-
#include <ztd/cuneicode/max_output.h>
37+
#include <ztd/cuneicode/max_input_output.h>
3838
#include <ztd/cuneicode/mcstate.h>
3939
#include <ztd/cuneicode/detail/boilerplate.h>
4040

source/ztd/cuneicode/amiga_1251.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <ztd/cuneicode/version.h>
3131

3232
#include <ztd/cuneicode/amiga_1251.h>
33-
#include <ztd/cuneicode/max_output.h>
33+
#include <ztd/cuneicode/max_input_output.h>
3434
#include <ztd/cuneicode/detail/transcode.hpp>
3535

3636
#if 0

source/ztd/cuneicode/ascii.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <ztd/cuneicode/version.h>
3131

3232
#include <ztd/cuneicode/ascii.h>
33-
#include <ztd/cuneicode/max_output.h>
33+
#include <ztd/cuneicode/max_input_output.h>
3434
#include <ztd/cuneicode/detail/transcode.hpp>
3535

3636
#include <memory>

0 commit comments

Comments
 (0)