-
Notifications
You must be signed in to change notification settings - Fork 392
/
omrstr.c
3227 lines (2937 loc) · 105 KB
/
omrstr.c
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 (c) 1991, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#if defined(OMR_OS_WINDOWS)
#include <windows.h>
#else
#include <time.h>
#endif /* defined(OMR_OS_WINDOWS) */
#include "omrportasserts.h"
#include <stdarg.h>
#include <string.h>
#if defined(OMR_OS_WINDOWS)
#include <malloc.h>
#elif defined(LINUX) || defined(AIXPPC) || defined(OSX)
#include <alloca.h>
#elif defined(J9ZOS390)
#include <stdlib.h>
#endif /* defined(OMR_OS_WINDOWS) */
#include <errno.h>
/*
#define J9STR_DEBUG
*/
#if defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) || defined(OSX)
#include <iconv.h>
typedef iconv_t charconvState_t;
#define J9STR_USE_ICONV
/* need to get the EBCDIC version of nl_langinfo */
#define J9_USE_ORIG_EBCDIC_LANGINFO 1
#include "omriconvhelpers.h"
#else /* defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) || defined(OSX) */
typedef void *charconvState_t; /*dummy type */
#endif /* defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) || defined(OSX) */
/* for sprintf, which is used for printing floats */
#include <stdio.h>
#include "omrutil.h"
#include "omrcomp.h"
#include "omrport.h"
#include "omrgetasid.h"
#include "omrgetjobid.h"
#include "omrgetjobname.h"
#include "omrgetsysname.h"
#include "omrstdarg.h"
#include "hashtable_api.h"
#define J9FTYPE_U64 1
#define J9FTYPE_U32 2
#define J9FTYPE_DBL 3
#define J9FTYPE_PTR 4
#define J9FTYPE_IMMEDIATE 5
#define J9FFLAG_DASH 0x01
#define J9FFLAG_HASH 0x02
#define J9FFLAG_ZERO 0x04
#define J9FFLAG_SPACE 0x08
#define J9FFLAG_PLUS 0x10
#define J9FSPEC_LL 0x20
#define J9FSPEC_L 0x40
#define J9F_NO_VALUE ((uint64_t)-1)
extern const char *utf8;
extern const char *utf16;
extern const char *ebcdic;
typedef union {
uint64_t u64;
double dbl;
void *ptr;
} J9FormatValue;
typedef struct {
uint8_t tag;
uint8_t index;
uint8_t widthIndex;
uint8_t precisionIndex;
const char *type;
} J9FormatSpecifier;
#define J9F_MAX_SPECS 16
#define J9F_MAX_ARGS (J9F_MAX_SPECS * 3)
typedef struct {
const char *formatString;
J9FormatValue value[J9F_MAX_ARGS];
uint8_t valueType[J9F_MAX_ARGS];
J9FormatSpecifier spec[J9F_MAX_SPECS];
uint8_t valueCount;
uint8_t immediateCount;
uint8_t specCount;
} J9FormatData;
typedef struct J9TimeInfo {
uint32_t second;
uint32_t minute;
uint32_t hour;
uint32_t day;
uint32_t month;
uint32_t year;
} J9TimeInfo;
typedef struct J9TokenEntry {
char *key;
char *value;
size_t keyLen; /* The length of the key string (excluding the \0) */
uintptr_t valueLen; /* The length of the value string (excluding the \0) */
uintptr_t memLen; /* The length of the allocated memory */
} J9TokenEntry;
/* The hash table does support growing beyond the specified size */
#define J9TOKEN_TABLE_INIT_SIZE 32
#define J9TOKEN_TABLE_ALIGNMENT sizeof(uintptr_t)
#define J9TOKEN_MAX_KEY_LEN 32
static const char digits_dec[] = "0123456789";
static const char digits_hex_lower[] = "0123456789abcdef";
static const char digits_hex_upper[] = "0123456789ABCDEF";
extern uint32_t encodeUTF8Char(uintptr_t unicode, uint8_t *result);
static const char *parseTagChar(const char *format, J9FormatData *result);
static void readValues(struct OMRPortLibrary *portLibrary, J9FormatData *result, va_list args);
static int parseFormatString(struct OMRPortLibrary *portLibrary, J9FormatData *result);
static uintptr_t writeDoubleToBuffer(char *buf, uintptr_t bufLen, uint64_t width, uint64_t precision, double value, uint8_t type, uint8_t tag);
static const char *parseModifier(const char *format, J9FormatData *result);
static const char *parseType(const char *format, J9FormatData *result);
static const char *parseWidth(const char *format, J9FormatData *result);
static uintptr_t writeFormattedString(struct OMRPortLibrary *portLibrary, J9FormatData *data, char *result, uintptr_t length);
static uintptr_t writeSpec(J9FormatData *data, J9FormatSpecifier *spec, char *result, uintptr_t length);
static const char *parseIndex(const char *format, uint8_t *result);
static uintptr_t writeStringToBuffer(char *buf, uintptr_t bufLen, uint64_t width, uint64_t precision, const char *value, uint8_t tag);
static const char *parsePrecision(const char *format, J9FormatData *result);
static uintptr_t writeIntToBuffer(char *buf, uintptr_t bufLen, uint64_t width, uint64_t precision, uint64_t value, uint8_t tag, int isSigned, const char *digits);
static uintptr_t writeUnicodeStringToBuffer(char *buf, uintptr_t bufLen, uint64_t width, uint64_t precision, const uint16_t *value, uint8_t tag);
static void convertUTCMillisToLocalJ9Time(int64_t millisUTC, struct J9TimeInfo *tm);
static void setJ9TimeToEpoch(struct J9TimeInfo *tm);
static uint32_t omrstr_subst_time(struct OMRPortLibrary *portLibrary, char *buf, uint32_t bufLen, const char *format, int64_t timeMillis);
static intptr_t omrstr_set_token_from_buf(struct OMRPortLibrary *portLibrary, struct J9StringTokens *tokens, const char *key, char *tokenBuf, uint32_t tokenLen);
static int32_t convertPlatformToMutf8(struct OMRPortLibrary *portLibrary, uint32_t codePage, const uint8_t *inBuffer, uintptr_t inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertMutf8ToPlatform(struct OMRPortLibrary *portLibrary, uint32_t codePage, const uint8_t *inBuffer, uintptr_t inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertWideToMutf8(const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertUtf8ToMutf8(struct OMRPortLibrary *portLibrary, const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertPlatformToUtf8(struct OMRPortLibrary *portLibrary, const uint8_t *inBuffer, uintptr_t inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertMutf8ToWide(const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertPlatformToWide(struct OMRPortLibrary *portLibrary, charconvState_t encodingState, uint32_t codePage, const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertWideToPlatform(struct OMRPortLibrary *portLibrary, charconvState_t encodingState, uint32_t codePage, const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertLatin1ToMutf8(struct OMRPortLibrary *portLibrary, const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
static int32_t convertMutf8ToLatin1(struct OMRPortLibrary *portLibrary, const uint8_t **inBuffer, uintptr_t *inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize);
#if defined(OMR_OS_WINDOWS)
static void convertJ9TimeToSYSTEMTIME(J9TimeInfo *j9TimeInfo, SYSTEMTIME *systemTime);
static void convertTimeMillisToJ9Time(int64_t timeMillis, J9TimeInfo *tm);
static void convertSYSTEMTIMEToJ9Time(SYSTEMTIME *systemTime, J9TimeInfo *j9TimeInfo);
static BOOLEAN firstDateComesBeforeSecondDate(J9TimeInfo *firstDate, J9TimeInfo *secondDate);
#endif /* defined(OMR_OS_WINDOWS) */
/**
* Write characters to a string as specified by format.
*
* @param[in] portLibrary The port library.
* @param[in, out] buf The string buffer to be written.
* @param[in] bufLen The size of the string buffer to be written.
* @param[in] format The format of the string.
* @param[in] ... Arguments for the format string.
*
* @return The number of characters printed not including the NUL terminator.
*
* @note When buf is NULL, the size of the buffer required to print to the string, including
* the NUL terminator is returned.
*
* @internal @note Supported, portable format specifiers are described in the document entitled "PortLibrary printf"
* in the "Inside J9" Lotus Notes database.
*/
uintptr_t
omrstr_printf(struct OMRPortLibrary *portLibrary, char *buf, uintptr_t bufLen, const char *format, ...)
{
uintptr_t rc;
va_list args;
va_start(args, format);
rc = portLibrary->str_vprintf(portLibrary, buf, bufLen, format, args);
va_end(args);
return rc;
}
/**
* This function converts strings between either the environment's platform encoding or Unicode (wide character),
* and modified UTF-8.
* Modified UTF-8 is described in the JVMTI specification.
* Conversion between arbitrary character encodings is not supported.
*
* @param[in] portLibrary The port library
* @param[in] fromCode Input string encoding. Only the following encodings are allowed:
* J9STR_CODE_MUTF8 (Modified UTF-8)
* J9STR_CODE_WIDE (UTF-16)
* J9STR_CODE_LATIN1
* J9STR_CODE_PLATFORM_RAW (encoding used by the operating system)
* J9STR_CODE_PLATFORM_OMR_INTERNAL (encoding used by certain operating system calls)
* J9STR_CODE_WINTHREADACP, J9STR_CODE_WINDEFAULTACP (thread and default ANSI code page, Windows only)
* @param[in] toCode Output string encoding. Only the encodings listed above are allowed.
* @param[in] inBuffer Input text to be converted. May contain embedded null characters.
* @param[in] inBufferSize input string size in bytes, not including the terminating null.
* @param[in] outBuffer user-allocated output buffer that stores converted characters, ignored if outBufferSize is 0.
* @param[in] outBufferSize output buffer size in bytes (zero to request the required output buffer size)
*
* @return:
* - positive value on success.
* - the number of bytes required to hold the converted text if outBufferSize is 0.
* - the number of bytes written to the buffer indicated by outBuffer.
* - negative value on failure:
* - OMRPORT_ERROR_STRING_BUFFER_TOO_SMALL, if the size of outBuffer is not large enough to hold the converted text.
* - OMRPORT_ERROR_STRING_ICONV_OPEN_FAILED, if call iconv_open() failed.
* - OMRPORT_ERROR_STRING_ILLEGAL_STRING if the input string is malformed
* - OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING if the input or output encoding is not supported
* - OMRPORT_ERROR_STRING_MEM_ALLOCATE_FAILED if the port library could not allocate a working buffer
* The following translations are supported:
* ANSI code page to modified UTF-8 (Windows only)
* platform raw to [wide, modified UTF-8, UTF-8]
* modified UTF-8 to [platform raw, wide, ISO Latin-1 (8859-1)]
* wide to [modified UTF-8, platform raw]
* [ISO Latin-1 (8859-1), UTF-8, Windows ANSI default and current code pages] to modified UTF-8
* @note J9STR_CODE_PLATFORM_OMR_INTERNAL is an alias for other encodings depending on the platform.
* @note J9STR_CODE_PLATFORM is deprecated. Use J9STR_CODE_PLATFORM_OMR_INTERNAL for results of system calls such as getenv (see stdlib.h) and
* @note J9STR_CODE_PLATFORM_RAW where the port library does not do implicit translation.
*/
#ifndef OS_ENCODING_CODE_PAGE
/* placeholder on non-Windows systems */
#define OS_ENCODING_CODE_PAGE 0
#endif
int32_t
omrstr_convert(struct OMRPortLibrary *portLibrary, int32_t fromCode, int32_t toCode,
uint8_t *inBuffer, uintptr_t inBufferSize, uint8_t *outBuffer, uintptr_t outBufferSize)
{
int32_t result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
switch (fromCode) {
case J9STR_CODE_PLATFORM_RAW: {
switch (toCode) {
case J9STR_CODE_MUTF8:
result = convertPlatformToMutf8(portLibrary, OS_ENCODING_CODE_PAGE, inBuffer, inBufferSize, outBuffer, outBufferSize);
break;
case J9STR_CODE_WIDE:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
case J9STR_CODE_UTF8:
result = convertPlatformToUtf8(portLibrary, (const uint8_t*)inBuffer, inBufferSize, outBuffer, outBufferSize);
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
}
break;
#if defined(OMR_OS_WINDOWS)
case J9STR_CODE_WINTHREADACP:
case J9STR_CODE_WINDEFAULTACP: {
switch (toCode) {
case J9STR_CODE_MUTF8:
result = convertPlatformToMutf8(portLibrary, (fromCode == J9STR_CODE_WINTHREADACP)? CP_THREAD_ACP: CP_ACP, inBuffer, inBufferSize, outBuffer, outBufferSize);
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
}
break;
#endif /* defined(OMR_OS_WINDOWS) */
case J9STR_CODE_MUTF8: {
switch (toCode) {
case J9STR_CODE_PLATFORM_RAW:
result = convertMutf8ToPlatform(portLibrary, OS_ENCODING_CODE_PAGE, inBuffer, inBufferSize, outBuffer, outBufferSize);
break;
case J9STR_CODE_LATIN1: {
const uint8_t *mutf8Cursor = inBuffer;
uintptr_t mutf8Remaining = inBufferSize;
result = convertMutf8ToLatin1(portLibrary, &mutf8Cursor, &mutf8Remaining, outBuffer, outBufferSize);
/*
* convertMutf8ToLatin1 is resumable so does not return error if buffer too small.
* In this case we should have consumed all the data
*/
if (mutf8Remaining > 0) {
result = OMRPORT_ERROR_STRING_BUFFER_TOO_SMALL;
}
}
break;
#if defined(OMR_OS_WINDOWS)
case J9STR_CODE_WINTHREADACP:
result = convertMutf8ToPlatform(portLibrary, CP_THREAD_ACP, inBuffer, inBufferSize, outBuffer, outBufferSize);
break;
case J9STR_CODE_WINDEFAULTACP:
result = convertMutf8ToPlatform(portLibrary, CP_ACP, inBuffer, inBufferSize, outBuffer, outBufferSize);
break;
#endif /* defined(OMR_OS_WINDOWS) */
case J9STR_CODE_WIDE: {
const uint8_t *mutf8Cursor = inBuffer;
uintptr_t mutf8Remaining = inBufferSize;
result = convertMutf8ToWide(&mutf8Cursor, &mutf8Remaining, outBuffer, outBufferSize);
/*
* inBuffer parameters are updated to reflect data untranslated due to insufficient space in the output buffer.
* In this case, all input characters should have been consumed.
*/
if (mutf8Remaining > 0) {
result = OMRPORT_ERROR_STRING_BUFFER_TOO_SMALL;
}
}
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
}
break;
case J9STR_CODE_UTF8: {
switch (toCode) {
case J9STR_CODE_MUTF8: {
const uint8_t *utf8Cursor = inBuffer;
uintptr_t utf8Remaining = inBufferSize;
result = convertUtf8ToMutf8(portLibrary, &utf8Cursor, &utf8Remaining, outBuffer, outBufferSize);
/*
* convertUtf8ToMutf8 is resumable so does not return error if buffer too small.
* In this case we should have consumed all the data
*/
if (utf8Remaining > 0) {
result = OMRPORT_ERROR_STRING_BUFFER_TOO_SMALL;
}
}
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
}
break;
case J9STR_CODE_WIDE: {
switch (toCode) {
case J9STR_CODE_PLATFORM_RAW:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
case J9STR_CODE_MUTF8: {
const uint8_t *wideCursor = inBuffer;
uintptr_t wideRemaining = inBufferSize;
result = convertWideToMutf8(&wideCursor, &wideRemaining, outBuffer, outBufferSize);
/*
* inBuffer parameters are updated to reflect data untranslated due to insufficient space in the output buffer.
* In this case, all input characters should have been consumed.
*/
if (wideRemaining > 0) {
result = OMRPORT_ERROR_STRING_BUFFER_TOO_SMALL;
}
}
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
}
break;
case J9STR_CODE_LATIN1: {
switch (toCode) {
case J9STR_CODE_MUTF8: {
const uint8_t *latin1Cursor = inBuffer;
uintptr_t latin1Remaining = inBufferSize;
result = convertLatin1ToMutf8(portLibrary, &latin1Cursor, &latin1Remaining, outBuffer, outBufferSize);
/*
* convertLatin1ToMutf8 is resumable so does not return error if buffer too small.
* In this case we should have consumed all the data
*/
if (latin1Remaining > 0) {
result = OMRPORT_ERROR_STRING_BUFFER_TOO_SMALL;
}
}
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
}
break;
default:
result = OMRPORT_ERROR_STRING_UNSUPPORTED_ENCODING;
break;
}
return result;
}
/**
* Write characters to a string as specified by format.
*
* @param[in] portLibrary The port library.
* @param[in, out] buf The string buffer to be written.
* @param[in] bufLen The size of the string buffer to be written.
* @param[in] format The format of the string.
* @param[in] args Arguments for the format string.
*
* @return The number of characters printed not including the NUL terminator.
*
* @note When buf is NULL, the size of the buffer required to print to the string, including
* the NUL terminator is returned.
*
* @internal @note Supported, portable format specifiers are described in the document entitled "PortLibrary printf"
* in the "Inside J9" Lotus Notes database.
*/
uintptr_t
omrstr_vprintf(struct OMRPortLibrary *portLibrary, char *buf, uintptr_t bufLen, const char *format, va_list args)
{
J9FormatData formatData;
memset(&formatData, 0, sizeof(formatData));
formatData.formatString = format;
parseFormatString(portLibrary, &formatData);
readValues(portLibrary, &formatData, args);
return writeFormattedString(portLibrary, &formatData, buf, bufLen);
}
static int
parseFormatString(struct OMRPortLibrary *portLibrary, J9FormatData *result)
{
const char *format = result->formatString;
while (*format) {
switch (*format) {
case '%':
format++;
switch (*format) {
case '%':
/* literal '%' */
format++;
break;
default:
format = parseIndex(format, &result->spec[result->specCount].index);
format = parseTagChar(format, result);
format = parseWidth(format, result);
format = parsePrecision(format, result);
format = parseModifier(format, result);
format = parseType(format, result);
if (format == NULL) {
return -1;
}
result->specCount++;
}
break;
default:
format++;
}
}
return 0;
}
static const char *
parseTagChar(const char *format, J9FormatData *result)
{
switch (*format) {
case '0':
result->spec[result->specCount].tag |= J9FFLAG_ZERO;
format++;
break;
case ' ':
result->spec[result->specCount].tag |= J9FFLAG_SPACE;
format++;
break;
case '-':
result->spec[result->specCount].tag |= J9FFLAG_DASH;
format++;
break;
case '+':
result->spec[result->specCount].tag |= J9FFLAG_PLUS;
format++;
break;
case '#':
result->spec[result->specCount].tag |= J9FFLAG_HASH;
format++;
break;
}
return format;
}
static const char *
parseWidth(const char *format, J9FormatData *result)
{
uint8_t index;
if (*format == '*') {
format = parseIndex(format + 1, &result->spec[result->specCount].widthIndex);
index = result->spec[result->specCount].widthIndex;
if (index == 0xFF) {
index = result->valueCount;
result->spec[result->specCount].widthIndex = index;
}
result->valueCount++;
result->valueType[index] = J9FTYPE_U32;
result->value[index].u64 = J9F_NO_VALUE;
return format;
} else {
uint32_t width = 0;
int anyDigits = 0;
for (;;) {
switch (*format) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
anyDigits = 1;
width = width * 10 + (*format - '0');
format += 1;
break;
default:
index = J9F_MAX_ARGS - ++result->immediateCount;
result->spec[result->specCount].widthIndex = index;
result->valueType[index] = J9FTYPE_IMMEDIATE;
if (anyDigits) {
result->value[index].u64 = width;
} else {
result->value[index].u64 = J9F_NO_VALUE;
}
return format;
}
}
}
}
static const char *
parsePrecision(const char *format, J9FormatData *result)
{
uint8_t index;
if (*format == '.') {
format += 1;
} else {
index = J9F_MAX_ARGS - ++result->immediateCount;
result->spec[result->specCount].precisionIndex = index;
result->valueType[index] = J9FTYPE_IMMEDIATE;
result->value[index].u64 = J9F_NO_VALUE;
return format;
}
if (*format == '*') {
format = parseIndex(format + 1, &result->spec[result->specCount].precisionIndex);
index = result->spec[result->specCount].precisionIndex;
if (index == 0xFF) {
index = result->valueCount;
result->spec[result->specCount].precisionIndex = index;
}
result->valueCount++;
result->valueType[index] = J9FTYPE_U32;
result->value[index].u64 = J9F_NO_VALUE;
return format;
} else {
uint32_t precision = 0;
int anyDigits = 0;
for (;;) {
switch (*format) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
anyDigits = 1;
precision = precision * 10 + (*format - '0');
format += 1;
break;
default:
index = J9F_MAX_ARGS - ++result->immediateCount;
result->spec[result->specCount].precisionIndex = index;
result->valueType[index] = J9FTYPE_IMMEDIATE;
if (anyDigits) {
result->value[index].u64 = precision;
} else {
result->value[index].u64 = J9F_NO_VALUE;
}
return format;
}
}
}
}
static const char *
parseModifier(const char *format, J9FormatData *result)
{
switch (*format) {
case 'z':
format++;
#ifdef OMR_ENV_DATA64
result->spec[result->specCount].tag |= J9FSPEC_LL;
#endif
break;
case 'l':
format++;
if (*format == 'l') {
format++;
result->spec[result->specCount].tag |= J9FSPEC_LL;
} else {
result->spec[result->specCount].tag |= J9FSPEC_L;
}
break;
}
return format;
}
static const char *
parseType(const char *format, J9FormatData *result)
{
const char *type = format++;
uint8_t tag = result->spec[result->specCount].tag;
uint8_t index = result->spec[result->specCount].index;
if (index == 0xFF) {
index = result->valueCount;
result->spec[result->specCount].index = index;
}
result->valueCount++;
result->spec[result->specCount].type = type;
switch (*type) {
/* integers */
case 'c':
result->valueType[index] = J9FTYPE_U32;
break;
case 'i':
case 'd':
case 'u':
case 'x':
case 'X':
result->valueType[index] = tag & J9FSPEC_LL ? J9FTYPE_U64 : J9FTYPE_U32;
break;
/* pointers */
case 'p':
case 's':
result->valueType[index] = J9FTYPE_PTR;
break;
/* floats */
case 'f':
case 'e':
case 'E':
case 'F':
case 'g':
case 'G':
result->valueType[index] = J9FTYPE_DBL;
break;
default:
return NULL;
}
return format;
}
static void
readValues(struct OMRPortLibrary *portLibrary, J9FormatData *result, va_list args)
{
uint8_t index;
va_list argsCopy;
COPY_VA_LIST(argsCopy, args);
for (index = 0; index < result->valueCount; index++) {
switch (result->valueType[index]) {
case J9FTYPE_U64:
result->value[index].u64 = va_arg(argsCopy, uint64_t);
break;
case J9FTYPE_U32:
result->value[index].u64 = va_arg(argsCopy, uint32_t);
break;
case J9FTYPE_DBL:
result->value[index].dbl = va_arg(argsCopy, double);
break;
case J9FTYPE_PTR:
result->value[index].ptr = va_arg(argsCopy, void *);
break;
case J9FTYPE_IMMEDIATE:
/* shouldn't be encountered -- these should all be at the end of the value array */
break;
}
}
END_VA_LIST_COPY(argsCopy);
}
static uintptr_t
writeSpec(J9FormatData *data, J9FormatSpecifier *spec, char *result, uintptr_t length)
{
J9FormatValue *value = &data->value[spec->index];
uint64_t width = data->value[spec->widthIndex].u64;
uint64_t precision = data->value[spec->precisionIndex] .u64;
uintptr_t index = 0;
switch (*spec->type) {
case 'i':
case 'd':
index = writeIntToBuffer(result, length, width, precision, value->u64, spec->tag, 1, digits_dec);
break;
case 'u':
index = writeIntToBuffer(result, length, width, precision, value->u64, spec->tag, 0, digits_dec);
break;
case 'x':
index = writeIntToBuffer(result, length, width, precision, value->u64, spec->tag, 0, digits_hex_lower);
break;
case 'X':
index = writeIntToBuffer(result, length, width, precision, value->u64, spec->tag, 0, digits_hex_upper);
break;
case 'p':
index = writeIntToBuffer(result, length, sizeof(uintptr_t) * 2, sizeof(uintptr_t) * 2, (uintptr_t)value->ptr, 0, 0, digits_hex_upper);
break;
case 'c':
if (spec->tag & J9FSPEC_L) {
char asUTF8[4];
uint32_t numberWritten = encodeUTF8Char((uintptr_t)value->u64, (uint8_t *)asUTF8);
/* what if width/precision is less than size of asUTF8? [truncate?] */
asUTF8[numberWritten] = '\0';
index = writeStringToBuffer(result, length, width, precision, asUTF8, spec->tag);
} else {
index = writeStringToBuffer(result, length, width, precision, " ", spec->tag);
if (index <= length) {
if (result) {
result[index - 1] = (char)value->u64;
}
}
}
break;
case 's':
if (value->ptr) {
if (spec->tag & J9FSPEC_L) {
index = writeUnicodeStringToBuffer(result, length, width, precision, (const uint16_t *)value->ptr, spec->tag);
} else {
index = writeStringToBuffer(result, length, width, precision, (const char *)value->ptr, spec->tag);
}
} else {
index = writeStringToBuffer(result, length, width, precision, "<NULL>", spec->tag);
}
break;
/* floats */
case 'f':
case 'e':
case 'E':
case 'F':
case 'g':
case 'G':
index = writeDoubleToBuffer(result, length, width, precision, value->dbl, *spec->type, spec->tag);
break;
}
return index;
}
/**
* @internal
*
* Writes the string representation of the specified value into the provided buffer
* using the specified precision.
*
* @param[in] buf The buffer into which results should be written.
* @param[in] bufLen The length of the buffer in bytes, never writes more than this number of bytes.
* @param[in] precision The maximum number of digits to display, J9F_NO_VALUE for as many digits as required.
* @param[in] value The value to print.
* @param[in] tag One of the J9FFLAG_* constants that controls the output format.
* @param[in] isSigned Non-zero if the value is signed.
* @param[in] digits A string containing hex/decimal digits (starting at zero).
*
* @return The number of bytes written to buf.
*/
static uintptr_t
writeIntToBuffer(char *buf, uintptr_t bufLen, uint64_t width, uint64_t precision, uint64_t value, uint8_t tag, int isSigned, const char *digits)
{
uint32_t index = 0;
uint32_t length = 0;
uint32_t rightSpace = 0;
uint64_t temp;
size_t base = strlen(digits);
int32_t actualPrecision = 0; /* precision is specified unsigned, but code may decrement temp values below zero */
char signChar = 0;
if (isSigned) {
int64_t signedValue;
if (tag & J9FSPEC_LL) {
signedValue = (int64_t)value;
} else {
signedValue = (int32_t)value;
}
if (signedValue < 0) {
signChar = '-';
value = (uint64_t)(signedValue * -1);
} else if (signedValue >= 0 && (tag & J9FFLAG_PLUS)) {
signChar = '+';
}
}
/* find the end of the number */
temp = value;
do {
length++;
temp /= base;
} while (temp);
if (precision != J9F_NO_VALUE) {
actualPrecision = (int32_t)precision;
/* subtle: actualPrecision known to be non-negative (hence cast to uint32_t)
* for purposes of comparison.
*/
if ((uint32_t)actualPrecision > length) {
length = actualPrecision;
}
}
/* Account for "-" Must be after setting actualPrecision, before calculation of rightSpace */
if (signChar) {
length++;
}
if (width != J9F_NO_VALUE) {
uint32_t actualWidth = (uint32_t)width; /* shorten user-specified width to uint32_t */
if (actualWidth > length) {
if (tag & J9FFLAG_DASH) {
rightSpace = actualWidth - length;
}
length = actualWidth;
}
}
if (tag & J9FFLAG_ZERO) {
actualPrecision = length - (signChar ? 1 : 0);
}
/* now write the number out backwards */
for (; rightSpace != 0; rightSpace--) {
length -= 1;
if (bufLen > length) {
if (buf) {
buf[length] = ' ';
}
index += 1;
}
}
/* write out the digits */
temp = value;
do {
length -= 1;
actualPrecision -= 1;
if (bufLen > length) {
if (buf) {
buf[length] = digits[(int)(temp % base)];
}
index += 1;
}
temp /= base;
} while (temp);
/* zero extend to the left according the the requested precision */
while (length > 0) {
length -= 1;
actualPrecision -= 1;
if (bufLen > length) {
if (buf) {
if (actualPrecision >= 0) {
buf[length] = '0';
} else {
if (signChar) {
buf[length] = signChar;
/* only print the sign char once */
signChar = 0;
} else {
buf[length] = ' ';
}
}
}
index += 1;
}
}
return index;
}
static uintptr_t
writeStringToBuffer(char *buf, uintptr_t bufLen, uint64_t width, uint64_t precision, const char *value, uint8_t tag)
{
size_t leftPadding = 0;
size_t rightPadding = 0;
if (precision == J9F_NO_VALUE) {
precision = strlen(value);
} else {
int32_t n;
/* detect if the string is shorter than precision */
for (n = 0; n < precision; n++) {
if (value[n] == 0) {
precision = n;
break;
}
}
}
if (width == J9F_NO_VALUE) {
width = precision;
}
if (width > precision) {
if (tag & J9FFLAG_DASH) {
rightPadding = (size_t)(width - precision);
} else {
leftPadding = (size_t)(width - precision);
}
}
if (leftPadding > bufLen) {
leftPadding = bufLen;
}
if (0 != leftPadding) {
if (NULL != buf) {
memset(buf, ' ', leftPadding);
buf += leftPadding;
}
bufLen -= leftPadding;
}
if (precision > bufLen) {
precision = bufLen;
}
if (NULL != buf) {
memcpy(buf, value, (size_t)precision);
buf += (size_t)precision;
}
bufLen -= (size_t)precision;
if (rightPadding > bufLen) {
rightPadding = bufLen;
}
if ((NULL != buf) && (0 != rightPadding)) {
memset(buf, ' ', rightPadding);
}
return leftPadding + (size_t)precision + rightPadding;
}
/*
* @internal
*
* To determine size of buffer required for format string pass in a NULL buffer and the maximum
* size willing to create. For example for no restrictions result=NULL, length=(uint32_t)(-1), to restrict
* the buffer to 2k, result=NULL, length=2048
*
* Value returned does not include space required for the null terminator
*/
static uintptr_t
writeFormattedString(struct OMRPortLibrary *portLibrary, J9FormatData *data, char *result, uintptr_t length)
{
const char *format = data->formatString;
uint8_t specIndex = 0;
uintptr_t index = 0;
if (NULL == result) {
length = (uintptr_t)-1;
} else if (0 == length) {
/* empty buffer */