-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgPrint.cpp
More file actions
1585 lines (1442 loc) · 51.8 KB
/
Copy pathMsgPrint.cpp
File metadata and controls
1585 lines (1442 loc) · 51.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// MsgPrint - text accumulation and scalar rendering.
//
#include "stdafx.h"
#include <float.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include <vector>
#include "MsgPrint.h"
#include "MsgValue.h"
#include "../Msgcore/Msgexception.h"
#include "../Msgcore/P2PmsgVBLock.h"
///////////////////////////////////////////////////////////////////////
// File-local helpers
// Defined with the rest of the parse-side helpers below. Declared here because
// the round-trip check in MsgPrint_FormatReal needs it, and because there is
// no _tcstod to use instead: the Platform shim maps _tcstol and stops there.
static bool
MsgPrint_ParseReal ( LPCTSTR lpszText, double& dOut );
//
// Formats a double so that reading it back yields the same double, WITHOUT
// spending seventeen digits on a value that does not need them.
// NOTES: %.17g always round-trips and always looks wrong - 0.1 comes back as
// 0.10000000000000001. %.15g looks right and does not always round
// trip. Printing at 15 and checking is the only way to get both, and
// the check is a strtod against the value that produced it.
// : Returns false for anything JSON has no literal for - NaN, either
// infinity, and Msgcore's own pseudo-null double - so the caller can
// render the cell as null rather than emit a document that will not
// parse.
//
static bool
MsgPrint_FormatReal ( double dValue, bool bSingle, CString& strOut )
{
if ( !(dValue == dValue) ) // NaN: the only value unequal to itself
return false;
if ( dValue > DBL_MAX || dValue < -DBL_MAX )
return false; // +/- infinity
if ( MsgcoreIsNULL_DBLE(dValue) )
return false; // Msgcore_NULL_DBLE, the pseudo-null
if ( bSingle )
{
// A float carries at most 9 significant decimal digits, so there is no
// shorter/longer decision to make here.
strOut.Format ( _T("%.9g"), dValue );
}
else
{
strOut.Format ( _T("%.15g"), dValue );
double dBack = 0.0;
if ( !MsgPrint_ParseReal ( (LPCTSTR)strOut, dBack ) || dBack != dValue )
strOut.Format ( _T("%.17g"), dValue );
}
// The decimal point is the C runtime's, and a host that has called
// setlocale with a comma locale would otherwise emit 1,5 - which is two
// JSON tokens and one PHP parse error.
// Rebuilt rather than patched in place: CString::Replace and SetAt are
// both MFC's, and the Platform shim carries neither. GetAt and += are
// what the shim does have, and they are what the render path already uses.
CString strFixed;
for ( int i = 0; i < strOut.GetLength ( ); i++ )
{
const TCHAR c = strOut.GetAt ( i );
strFixed += c == _T(',') ? _T('.') : c;
}
strOut = strFixed;
return true;
}
//
// Formats epoch seconds as ISO-8601 local time. Returns false when the value
// is not a time the platform can break down, which is a real case: the stored
// cell is whatever was written into it.
//
static bool
MsgPrint_FormatEpoch ( INT64 iSeconds, CString& strOut )
{
struct tm oTM;
::memset ( &oTM, 0, sizeof(oTM) );
#ifdef _WIN32
const __time64_t tValue = (__time64_t)iSeconds;
if ( _localtime64_s ( &oTM, &tValue ) != 0 )
return false;
#else
const time_t tValue = (time_t)iSeconds;
if ( ::localtime_r ( &tValue, &oTM ) == nullptr )
return false;
#endif
strOut.Format ( _T("%04d-%02d-%02dT%02d:%02d:%02d")
, oTM.tm_year + 1900, oTM.tm_mon + 1, oTM.tm_mday
, oTM.tm_hour, oTM.tm_min, oTM.tm_sec );
return true;
}
//
// Formats the sixteen bytes of a GUID in the canonical braced form.
// NOTES: The bytes are COPIED out before they are read as a GUID. The payload
// sits inside a #pragma pack(1) image at whatever offset the block walk
// landed on, so reading it through a GUID* asserts an alignment the
// address does not have - the same undefined behaviour c_vBlob()
// documents at length in P2Pmsg.h.
//
static void
MsgPrint_FormatGUID ( const void *pvGUID, CString& strOut )
{
unsigned char cGUID[16];
::memcpy ( cGUID, pvGUID, sizeof(cGUID) );
// Data1/Data2/Data3 are stored little-endian, Data4 as bytes - which is
// what makes the first three groups a byte reversal and the last two not.
strOut.Format ( _T("{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
, cGUID[3], cGUID[2], cGUID[1], cGUID[0]
, cGUID[5], cGUID[4]
, cGUID[7], cGUID[6]
, cGUID[8], cGUID[9]
, cGUID[10], cGUID[11], cGUID[12], cGUID[13], cGUID[14], cGUID[15] );
}
//
// UTF-16 to UTF-8, written out rather than delegated to WideCharToMultiByte.
// NOTES: Save() is the one place this component touches the outside world, and
// a hand-rolled encoder is what lets it do so identically on both
// targets. Surrogate pairs are combined; an unpaired surrogate becomes
// U+FFFD, because emitting it raw produces a byte sequence that is not
// UTF-8 and that a strict reader rejects.
//
static void
MsgPrint_ToUTF8 ( LPCTSTR lpszText, std::vector<char>& oOut )
{
if ( lpszText == nullptr )
return;
for ( const TCHAR *p = lpszText; *p; ++p )
{
// Cast through the character type itself, not through unsigned short: on
// the Linux target wchar_t is 32 bits and narrowing it here would lose
// every character above the BMP. The surrogate arms below are then dead
// code there, which is correct - a 32-bit code unit IS the code point,
// and a lone surrogate is not a valid one on either target.
unsigned int uCode = (unsigned int)*p;
if ( uCode >= 0xD800u && uCode <= 0xDBFFu )
{
const unsigned int uLow = (unsigned int)*(p+1);
if ( uLow >= 0xDC00u && uLow <= 0xDFFFu )
{
uCode = 0x10000u + ((uCode - 0xD800u) << 10) + (uLow - 0xDC00u);
++p;
}
else
uCode = 0xFFFDu; // Unpaired high surrogate
}
else if ( uCode >= 0xDC00u && uCode <= 0xDFFFu )
uCode = 0xFFFDu; // Unpaired low surrogate
if ( uCode < 0x80u )
oOut.push_back ( (char)uCode );
else if ( uCode < 0x800u )
{
oOut.push_back ( (char)(0xC0u | (uCode >> 6)) );
oOut.push_back ( (char)(0x80u | (uCode & 0x3Fu)) );
}
else if ( uCode < 0x10000u )
{
oOut.push_back ( (char)(0xE0u | (uCode >> 12)) );
oOut.push_back ( (char)(0x80u | ((uCode >> 6) & 0x3Fu)) );
oOut.push_back ( (char)(0x80u | (uCode & 0x3Fu)) );
}
else
{
oOut.push_back ( (char)(0xF0u | (uCode >> 18)) );
oOut.push_back ( (char)(0x80u | ((uCode >> 12) & 0x3Fu)) );
oOut.push_back ( (char)(0x80u | ((uCode >> 6) & 0x3Fu)) );
oOut.push_back ( (char)(0x80u | (uCode & 0x3Fu)) );
}
}
}
///////////////////////////////////////////////////////////////////////
// Constructors and destructor
MsgPrint::MsgPrint ( ) noexcept
{
}
MsgPrint::MsgPrint ( const MsgPrint& rhs )
{
*this = rhs;
}
MsgPrint::~MsgPrint ( )
{
}
///////////////////////////////////////////////////////////////////////
// Operators
MsgPrint&
MsgPrint::operator = ( const MsgPrint& rhs )
{
if ( this == &rhs )
return *this;
m_strText = rhs.m_strText;
m_nIndent = rhs.m_nIndent;
m_bAttributes = rhs.m_bAttributes;
m_nDepthMax = rhs.m_nDepthMax;
return *this;
}
MsgPrint::operator LPCTSTR ( ) const noexcept
{
return (LPCTSTR)m_strText;
}
///////////////////////////////////////////////////////////////////////
// Text exposure
LPCTSTR
MsgPrint::c_str ( ) const noexcept
{
return (LPCTSTR)m_strText;
}
const CString&
MsgPrint::r_text ( ) const noexcept
{
return m_strText;
}
int
MsgPrint::GetLength ( ) const noexcept
{
return m_strText.GetLength ( );
}
bool
MsgPrint::IsEmpty ( ) const noexcept
{
return m_strText.IsEmpty ( ) ? true : false;
}
void
MsgPrint::Clear ( ) noexcept
{
m_strText.Empty ( );
}
///////////////////////////////////////////////////////////////////////
// Output
void
MsgPrint::Print ( FILE *fd ) const
{
if ( fd == nullptr || m_strText.IsEmpty() )
return;
// _fputts is MSVC's; the Platform shim does not map it, and off Windows
// TCHAR is wchar_t, so the wide CRT call IS the same call.
#ifdef _WIN32
_fputts ( (LPCTSTR)m_strText, fd );
#else
::fputws ( (LPCTSTR)m_strText, fd );
#endif
}
//
// Writes the rendered document to a file as UTF-8.
//
// Parameters: LPCTSTR lpszFilename
// Destination path, created or truncated
//
// bool bBOM
// Prefix the byte-order mark
//
// Returns: BOOL
// TRUE... Written
// FALSE.. Nothing rendered, or the file could not be written
//
BOOL
MsgPrint::Save ( LPCTSTR lpszFilename, bool bBOM ) const
{
if ( lpszFilename == nullptr || *lpszFilename == 0 )
return FALSE;
std::vector<char> oBytes;
if ( bBOM )
{
// Written as char literals rather than cast from 0xEF: char is signed
// here, so (char)0xEF is a narrowing of a positive constant and W4 says
// so (C4310). The bytes are identical either way.
oBytes.push_back ( '\xEF' );
oBytes.push_back ( '\xBB' );
oBytes.push_back ( '\xBF' );
}
MsgPrint_ToUTF8 ( (LPCTSTR)m_strText, oBytes );
// BINARY, not text. The document already carries the line endings the
// dialect chose; text mode would translate them a second time and a PHP
// file served from Windows would arrive with CRCRLF.
FILE *fd = nullptr;
#ifdef _WIN32
if ( _tfopen_s ( &fd, lpszFilename, _T("wb") ) != 0 || fd == nullptr )
return FALSE;
#else
std::vector<char> oName;
MsgPrint_ToUTF8 ( lpszFilename, oName );
oName.push_back ( 0 );
fd = ::fopen ( &oName[0], "wb" );
if ( fd == nullptr )
return FALSE;
#endif
const size_t nBytes = oBytes.size ( );
const size_t nWritten = nBytes ? ::fwrite ( &oBytes[0], 1, nBytes, fd ) : 0;
::fclose ( fd );
return nWritten == nBytes ? TRUE : FALSE;
}
///////////////////////////////////////////////////////////////////////
// Properties
int
MsgPrint::SetIndent ( int nSpaces ) noexcept
{
const int nPrevious = m_nIndent;
m_nIndent = nSpaces < 0 ? 0 : nSpaces;
return nPrevious;
}
int
MsgPrint::GetIndent ( ) const noexcept
{
return m_nIndent;
}
bool
MsgPrint::SetAttributes ( bool bRender ) noexcept
{
const bool bPrevious = m_bAttributes;
m_bAttributes = bRender;
return bPrevious;
}
bool
MsgPrint::GetAttributes ( ) const noexcept
{
return m_bAttributes;
}
int
MsgPrint::SetDepthMax ( int nDepthMax ) noexcept
{
const int nPrevious = m_nDepthMax;
m_nDepthMax = nDepthMax < 1 ? 1 : nDepthMax;
return nPrevious;
}
int
MsgPrint::GetDepthMax ( ) const noexcept
{
return m_nDepthMax;
}
///////////////////////////////////////////////////////////////////////
// Scalar rendering
//
// Renders one P3PmsgData cell as unescaped text plus the kind the dialect
// should punctuate it as.
//
// Parameters: P3PmsgData& oData
// Cell to render
//
// CString& strOut
// Receives the text; emptied for Value_Null
//
// Returns: ValueKind_e
// How the text is to be punctuated
//
MsgPrint::ValueKind_e
MsgPrint::RenderScalar ( P3PmsgData& oData, CString& strOut ) const
{
strOut.Empty ( );
try
{
if ( oData.IsNull() )
return Value_Null;
switch ( oData.DataType ( ) )
{
case VBLockData_NULL:
return Value_Null;
case VBLockData_BOOL:
strOut = oData.c_bool ( ) ? _T("true") : _T("false");
return Value_Bool;
// Every integer width through one accessor. ReadAnyInt is the only
// reader that does not throw on a width or sign mismatch, and the
// unsigned subtypes have no dedicated public accessor at all - which
// is exactly why it exists.
case VBLockData_INT08: case VBLockData_UINT08:
case VBLockData_INT16: case VBLockData_UINT16:
case VBLockData_INT32: case VBLockData_UINT32:
case VBLockData_INT64: case VBLockData_UINT64:
{
INT64 iValue = 0;
bool bUnsigned = false;
if ( !oData.ReadAnyInt ( iValue, bUnsigned ) )
return Value_Null;
// %llu / %lld rather than MSVC's %I64u / %I64i: this file compiles
// on the Linux target too, where the I64 length modifier is not a
// format specifier at all. MSVC has understood ll since VS2015.
if ( bUnsigned )
strOut.Format ( _T("%llu"), (unsigned long long)iValue );
else
strOut.Format ( _T("%lld"), (long long)iValue );
return Value_Number;
}
case VBLockData_FLOAT:
return MsgPrint_FormatReal ( (double)oData.c_float(), true, strOut )
? Value_Number : Value_Null;
case VBLockData_DOUBLE:
return MsgPrint_FormatReal ( oData.c_double(), false, strOut )
? Value_Number : Value_Null;
// TIME32 reads through c_time, which accepts the TIME32 tag and the
// untagged UINT32; TIME64 through c_time64, which accepts TIME64 and
// INT64. Both are seconds, and both become a string rather than the
// bare number they are stored as - a number would be indistinguishable
// from a count.
case VBLockData_TIME32:
return MsgPrint_FormatEpoch ( (INT64)(UINT32)oData.c_time(), strOut )
? Value_String : Value_Null;
case VBLockData_TIME64:
return MsgPrint_FormatEpoch ( (INT64)oData.c_time64(), strOut )
? Value_String : Value_Null;
case VBLockData_WCHAR:
{
const wchar_t wcValue = oData.c_wchar ( );
if ( wcValue )
strOut += (TCHAR)wcValue;
return Value_String;
}
case VBLockData_GUID:
{
const void *pvGUID = oData.c_vGUID ( );
if ( pvGUID == nullptr )
return Value_Null;
MsgPrint_FormatGUID ( pvGUID, strOut );
return Value_String;
}
// Narrow strings arrive as bytes in the store's own code page; CString
// widens them, which is what P3PmsgData::ToString does with the same
// cells and the only conversion the format defines.
case VBLockData_BSTR08: case VBLockData_BSTR08var:
case VBLockData_BSTR16: case VBLockData_BSTR16var:
case VBLockData_BSTR32: case VBLockData_BSTR32var:
{
LPCSTR lpszValue = oData.c_str ( );
if ( lpszValue )
strOut = CString ( lpszValue );
return Value_String;
}
case VBLockData_WSTR08: case VBLockData_WSTR08var:
case VBLockData_WSTR16: case VBLockData_WSTR16var:
case VBLockData_WSTR32: case VBLockData_WSTR32var:
{
LPCWSTR lpszValue = oData.c_wstr ( );
if ( lpszValue )
strOut = lpszValue;
return Value_String;
}
// Base64, and read through c_vBlobCopy rather than c_vBlob: the
// payload begins at an arbitrary offset inside a packed image, so the
// bytes are copied somewhere aligned before anything reads them.
case VBLockData_BLOB08: case VBLockData_BLOB08var:
case VBLockData_BLOB16: case VBLockData_BLOB16var:
case VBLockData_BLOB32: case VBLockData_BLOB32var:
{
const size_t nBytes = oData.c_size ( );
if ( nBytes == 0 )
return Value_String; // Present and empty, which is not null
std::vector<unsigned char> oBlob ( nBytes );
const size_t nRead = oData.c_vBlobCopy ( &oBlob[0], nBytes );
strOut = Base64 ( &oBlob[0], nRead );
return Value_String;
}
default:
// A type byte this build does not implement. Reported as an empty
// cell rather than guessed at: the byte came off the wire or off
// disk, and a renderer is not the place to decide what an unknown
// layout meant.
return Value_Null;
}
}
// A throw here means the cell disagreed with its own type byte, which a
// well-formed store cannot do. Cancel() releases the event - the document
// reports one empty cell and the render continues, because abandoning the
// whole document over one cell helps nobody.
catch_pP2Pevent_Cancel
catch_ALL_Cancel
strOut.Empty ( );
return Value_Null;
}
CString
MsgPrint::TypeName ( P3PmsgData& oData )
{
CString strType;
try
{
LPCTSTR lpszType = oData.ToStringType ( );
if ( lpszType )
strType = lpszType;
return strType;
}
catch_pP2Pevent_Cancel
catch_ALL_Cancel
strType.Empty ( );
return strType;
}
///////////////////////////////////////////////////////////////////////
// Text assembly
void
MsgPrint::Append ( LPCTSTR lpszText )
{
if ( lpszText )
m_strText += lpszText;
}
void
MsgPrint::AppendBreak ( int nDepth )
{
if ( m_nIndent <= 0 )
return; // Compact: one line, no padding
m_strText += _T('\n');
for ( int i = 0; i < nDepth * m_nIndent; i++ )
m_strText += _T(' ');
}
//
// Standard base64 of a byte run, with padding.
//
CString
MsgPrint::Base64 ( const unsigned char *pcBytes, size_t nBytes )
{
static const TCHAR szAlphabet[] =
_T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
CString strOut;
if ( pcBytes == nullptr || nBytes == 0 )
return strOut;
for ( size_t i = 0; i < nBytes; i += 3 )
{
const size_t nRun = (nBytes - i) < 3 ? (nBytes - i) : 3;
const unsigned int uTrip = ((unsigned int)pcBytes[i] << 16)
| (nRun > 1 ? ((unsigned int)pcBytes[i+1] << 8) : 0u)
| (nRun > 2 ? (unsigned int)pcBytes[i+2] : 0u);
strOut += szAlphabet[(uTrip >> 18) & 0x3Fu];
strOut += szAlphabet[(uTrip >> 12) & 0x3Fu];
strOut += nRun > 1 ? szAlphabet[(uTrip >> 6) & 0x3Fu] : _T('=');
strOut += nRun > 2 ? szAlphabet[ uTrip & 0x3Fu] : _T('=');
}
return strOut;
}
///////////////////////////////////////////////////////////////////////
// File-local helpers - parsing
// MSVC deprecates sscanf and this project builds warnings-as-errors, so the
// secure name is used where there is one. The two are INTERCHANGEABLE HERE and
// only here: sscanf_s differs from sscanf solely in taking a buffer size after
// every %s, %c and %[ conversion, and every conversion below is numeric.
#ifdef _WIN32
#define MsgPrint_sscanf sscanf_s
#else
#define MsgPrint_sscanf sscanf
#endif
//
// Decodes UTF-8 into wide text: the inverse of MsgPrint_ToUTF8, and paired
// with it deliberately, because a document Save writes and Load reads back has
// to be the same document.
// NOTES: A malformed sequence becomes U+FFFD and the walk resynchronises on
// the very next byte. It does NOT fail: a parser is about to be pointed
// at this text and will report where it stopped, whereas a decoder that
// threw would report the failure a whole layer away from the line that
// caused it.
//
static void
MsgPrint_FromUTF8 ( const char *pcBytes, size_t nBytes, CString& strOut )
{
strOut.Empty ( );
if ( pcBytes == nullptr )
return;
size_t i = 0;
while ( i < nBytes )
{
const unsigned int uLead = (unsigned char)pcBytes[i];
unsigned int uCode = 0xFFFDu;
size_t nSeq = 1;
if ( uLead < 0x80u ) { nSeq = 1; uCode = uLead; }
else if ( (uLead & 0xE0u) == 0xC0u ) { nSeq = 2; uCode = uLead & 0x1Fu; }
else if ( (uLead & 0xF0u) == 0xE0u ) { nSeq = 3; uCode = uLead & 0x0Fu; }
else if ( (uLead & 0xF8u) == 0xF0u ) { nSeq = 4; uCode = uLead & 0x07u; }
else
{
// A continuation byte, or a lead byte outside the four legal forms.
strOut += (TCHAR)0xFFFD;
i++;
continue;
}
if ( nSeq > 1 )
{
if ( i + nSeq > nBytes )
{
strOut += (TCHAR)0xFFFD; // Truncated at the end of the file
break;
}
bool bValid = true;
for ( size_t k = 1; k < nSeq; k++ )
{
const unsigned int uCont = (unsigned char)pcBytes[i+k];
if ( (uCont & 0xC0u) != 0x80u )
{
bValid = false;
break;
}
uCode = (uCode << 6) | (uCont & 0x3Fu);
}
if ( !bValid )
{
strOut += (TCHAR)0xFFFD;
i++;
continue;
}
if ( uCode > 0x10FFFFu || (uCode >= 0xD800u && uCode <= 0xDFFFu) )
uCode = 0xFFFDu; // Out of range, or an encoded surrogate
}
i += nSeq;
// A surrogate pair where TCHAR is 16 bits and nothing at all where it is
// 32 - the exact mirror of the encoder, whose surrogate arms are dead
// code on that same target for the same reason.
if ( uCode >= 0x10000u && sizeof(TCHAR) == 2 )
{
const unsigned int uRel = uCode - 0x10000u;
strOut += (TCHAR)(0xD800u + (uRel >> 10));
strOut += (TCHAR)(0xDC00u + (uRel & 0x3FFu));
}
else
strOut += (TCHAR)uCode;
}
}
//
// Copies ASCII text into a narrow buffer, refusing anything above 0x7F.
// NOTES: Every text this is used on is ASCII BY CONSTRUCTION - a number
// literal, an ISO-8601 timestamp, a GUID. Narrowing them is what lets
// the conversions below use strtoll / strtod / sscanf, which exist
// identically on both targets; their wide equivalents do not. The
// Platform shim maps _tcstol and stops there, and there is no _ttoi64
// on the Linux side at all.
//
static bool
MsgPrint_Narrow ( LPCTSTR lpszText, char *pcOut, size_t nMax )
{
if ( lpszText == nullptr || pcOut == nullptr || nMax == 0 )
return false;
size_t i = 0;
for ( ; lpszText[i] && i < nMax - 1; i++ )
{
const unsigned int uChar = (unsigned int)lpszText[i];
if ( uChar > 0x7Fu )
return false;
pcOut[i] = (char)uChar;
}
if ( lpszText[i] )
return false; // Longer than the buffer, so not a number
pcOut[i] = 0;
return true;
}
//
// Text to INT64, refusing anything the whole of which is not an integer.
// NOTES: The endptr check is the point. strtoll stops at the first character
// it cannot use and reports success for the prefix, so "12abc" would
// otherwise become 12 - and a document that says 12abc is a document
// this library should refuse rather than round off.
//
static bool
MsgPrint_ParseInt ( LPCTSTR lpszText, INT64& iOut )
{
char acText[64];
if ( !MsgPrint_Narrow ( lpszText, acText, sizeof(acText) ) || acText[0] == 0 )
return false;
char *pcEnd = nullptr;
errno = 0;
const long long llValue = ::strtoll ( acText, &pcEnd, 10 );
if ( pcEnd == acText || *pcEnd != 0 || errno == ERANGE )
return false;
iOut = (INT64)llValue;
return true;
}
static bool
MsgPrint_ParseReal ( LPCTSTR lpszText, double& dOut )
{
char acText[64];
if ( !MsgPrint_Narrow ( lpszText, acText, sizeof(acText) ) || acText[0] == 0 )
return false;
char *pcEnd = nullptr;
errno = 0;
const double dValue = ::strtod ( acText, &pcEnd );
if ( pcEnd == acText || *pcEnd != 0 )
return false;
dOut = dValue;
return true;
}
//
// ISO-8601 local time back to seconds - the inverse of MsgPrint_FormatEpoch,
// and it has to read exactly what that writes.
// NOTES: tm_isdst = -1 asks the CRT to work out the offset for that local
// date, which is what makes the round trip land on the same second
// across a daylight-saving boundary. Zero would assert standard time
// and shift half the year by an hour.
//
static bool
MsgPrint_ParseEpoch ( LPCTSTR lpszText, INT64& iSeconds )
{
char acText[64];
if ( !MsgPrint_Narrow ( lpszText, acText, sizeof(acText) ) )
return false;
int nYear = 0, nMon = 0, nDay = 0, nHour = 0, nMin = 0, nSec = 0;
if ( MsgPrint_sscanf ( acText, "%d-%d-%dT%d:%d:%d"
, &nYear, &nMon, &nDay, &nHour, &nMin, &nSec ) != 6 )
return false;
struct tm oTM;
::memset ( &oTM, 0, sizeof(oTM) );
oTM.tm_year = nYear - 1900;
oTM.tm_mon = nMon - 1;
oTM.tm_mday = nDay;
oTM.tm_hour = nHour;
oTM.tm_min = nMin;
oTM.tm_sec = nSec;
oTM.tm_isdst = -1;
#ifdef _WIN32
const __time64_t tValue = ::_mktime64 ( &oTM );
#else
const time_t tValue = ::mktime ( &oTM );
#endif
if ( tValue == (decltype(tValue))-1 )
return false;
iSeconds = (INT64)tValue;
return true;
}
//
// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} back to sixteen bytes, in the byte
// order MsgPrint_FormatGUID wrote them: the first three groups reversed
// because Data1/2/3 are stored little-endian, the last two straight through
// because Data4 is stored as bytes.
//
static bool
MsgPrint_ParseGUID ( LPCTSTR lpszText, unsigned char *pcGUID )
{
char acText[64];
if ( !MsgPrint_Narrow ( lpszText, acText, sizeof(acText) ) )
return false;
unsigned int auByte[16];
const char *pcRead = acText;
if ( *pcRead == '{' )
pcRead++; // The braces the formatter writes
if ( MsgPrint_sscanf ( pcRead
, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
, &auByte[0], &auByte[1], &auByte[2], &auByte[3]
, &auByte[4], &auByte[5]
, &auByte[6], &auByte[7]
, &auByte[8], &auByte[9]
, &auByte[10], &auByte[11], &auByte[12]
, &auByte[13], &auByte[14], &auByte[15] ) != 16 )
return false;
pcGUID[3] = (unsigned char)auByte[0];
pcGUID[2] = (unsigned char)auByte[1];
pcGUID[1] = (unsigned char)auByte[2];
pcGUID[0] = (unsigned char)auByte[3];
pcGUID[5] = (unsigned char)auByte[4];
pcGUID[4] = (unsigned char)auByte[5];
pcGUID[7] = (unsigned char)auByte[6];
pcGUID[6] = (unsigned char)auByte[7];
for ( int i = 8; i < 16; i++ )
pcGUID[i] = (unsigned char)auByte[i];
return true;
}
///////////////////////////////////////////////////////////////////////
// Input
void
MsgPrint::SetText ( LPCTSTR lpszText )
{
m_strText = lpszText ? lpszText : _T("");
ClearError ( );
}
//
// Reads lpszFilename into the held text as UTF-8.
//
// Parameters: LPCTSTR lpszFilename
// File to read
//
// Returns: BOOL
// TRUE when the file was opened and read to the end
//
BOOL
MsgPrint::Load ( LPCTSTR lpszFilename )
{
if ( lpszFilename == nullptr || *lpszFilename == 0 )
return FALSE;
// BINARY, as Save writes it. Text mode would eat the CR of every CRLF and
// a document whose line endings the dialect chose would come back with
// different ones.
FILE *fd = nullptr;
#ifdef _WIN32
if ( _tfopen_s ( &fd, lpszFilename, _T("rb") ) != 0 || fd == nullptr )
return FALSE;
#else
std::vector<char> oName;
MsgPrint_ToUTF8 ( lpszFilename, oName );
oName.push_back ( 0 );
fd = ::fopen ( &oName[0], "rb" );
if ( fd == nullptr )
return FALSE;
#endif
std::vector<char> oBytes;
char acChunk[4096];
size_t nRead = 0;
while ( (nRead = ::fread ( acChunk, 1, sizeof(acChunk), fd )) > 0 )
oBytes.insert ( oBytes.end ( ), acChunk, acChunk + nRead );
const bool bFailed = ::ferror ( fd ) != 0;
::fclose ( fd );
if ( bFailed )
return FALSE;
// The BOM Save may have written, dropped whether or not one was expected.
// It is an encoding mark and not the first character of the document, and
// a parser handed one reports a syntax error on column one.
size_t nStart = 0;
if ( oBytes.size ( ) >= 3
&& (unsigned char)oBytes[0] == 0xEF
&& (unsigned char)oBytes[1] == 0xBB
&& (unsigned char)oBytes[2] == 0xBF )
nStart = 3;
CString strText;
if ( oBytes.size ( ) > nStart )
MsgPrint_FromUTF8 ( &oBytes[nStart], oBytes.size ( ) - nStart, strText );
SetText ( (LPCTSTR)strText );
return TRUE;
}
///////////////////////////////////////////////////////////////////////
// Parse status
LPCTSTR
MsgPrint::GetError ( ) const noexcept
{
return (LPCTSTR)m_strError;
}
int
MsgPrint::GetErrorPos ( ) const noexcept
{
return m_nErrorPos;
}
int
MsgPrint::GetErrorLine ( ) const noexcept
{
return m_nErrorLine;
}
void
MsgPrint::ClearError ( ) noexcept
{
m_strError.Empty ( );
m_nErrorPos = -1;
m_nErrorLine = 0;
}
//
// Records the first failure of a parse and nothing after it.
// NOTES: FIRST, not last. A recursive descent unwinds through every level
// above the failure and each of them would happily overwrite the
// message with its own, more general one - and "unexpected end of
// document" is no help to someone whose actual mistake was a missing
// comma on line 40.
// : nPos is a character offset into m_strText; the line number is counted
// from it here rather than tracked by the reader, because a reader that
// carried a line counter would have to keep it right through every
// backtrack and this cannot get it wrong.
//
void
MsgPrint::SetError ( LPCTSTR lpszError, int nPos )
{
if ( !m_strError.IsEmpty ( ) )
return;
m_strError = lpszError ? lpszError : _T("Parse failed");
m_nErrorPos = nPos;
if ( nPos < 0 )
{
m_nErrorLine = 0;
return;
}
const int nEnd = nPos < m_strText.GetLength ( ) ? nPos : m_strText.GetLength ( );
m_nErrorLine = 1;
for ( int i = 0; i < nEnd; i++ )
{
if ( m_strText[i] == _T('\n') )
m_nErrorLine++;
}
}
///////////////////////////////////////////////////////////////////////
// Scalar parsing
//
// The VBLockData byte a Msgcore type name stands for - the inverse of
// P3PmsgData::ToStringType, which is what TypeName hands out.
//
// Parameters: LPCTSTR lpszTypeName
// Type name as TypeName would have written it
//
// Returns: UCHAR
// The type byte, or VBLockData_NULL for a name it cannot place
//
// NOTES: The compare is CASE-INSENSITIVE because the two halves of the table
// it inverts are not spelled alike - ToStringType says "int32" and
// "WSTR16" - and a reader matching case exactly would work for half the
// types and fail silently for the rest.
// : TIME32 has no name in that table at all, so nothing here can produce
// one. That is the table's limit, not this function's.
//
UCHAR
MsgPrint::TypeByte ( LPCTSTR lpszTypeName )
{
if ( lpszTypeName == nullptr || *lpszTypeName == 0 )
return VBLockData_NULL;
static const struct { LPCTSTR lpszName; UCHAR uType; } aoTable[] =
{
{ _T("char"), VBLockData_INT08 },