Menu

[r3316]: / trunk / src / xmlrpc_string.c  Maximize  Restore  History

Download this file

994 lines (731 with data), 29.7 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
/*=============================================================================
xmlrpc_string
===============================================================================
Routines for the "string" type of xmlrpc_value.
By Bryan Henderson.
Contributed to the public domain by its author.
=============================================================================*/
#include "xmlrpc_config.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "bool.h"
#include "mallocvar.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/util.h"
void
xmlrpc_destroyString(xmlrpc_value * const valueP) {
if (valueP->_wcs_block)
xmlrpc_mem_block_free(valueP->_wcs_block);
xmlrpc_mem_block_free(valueP->blockP);
}
static void
verifyNoNulls(xmlrpc_env * const envP,
const char * const contents,
unsigned int const len) {
/*----------------------------------------------------------------------------
Verify that the character array 'contents', which is 'len' bytes long,
does not contain any NUL characters, which means it can be made into
a passable ASCIIZ string just by adding a terminating NUL.
Fail if the array contains a NUL.
-----------------------------------------------------------------------------*/
unsigned int i;
for (i = 0; i < len && !envP->fault_occurred; ++i)
if (contents[i] == '\0')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR,
"String must not contain NUL characters");
}
#if HAVE_UNICODE_WCHAR
static void
verifyNoNullsW(xmlrpc_env * const envP,
const wchar_t * const contents,
unsigned int const len) {
/*----------------------------------------------------------------------------
Same as verifyNoNulls(), but for wide characters.
-----------------------------------------------------------------------------*/
unsigned int i;
for (i = 0; i < len && !envP->fault_occurred; i++)
if (contents[i] == '\0')
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR,
"String must not contain NUL characters");
}
#endif
static void
validateStringType(xmlrpc_env * const envP,
const xmlrpc_value * const valueP) {
if (valueP->_type != XMLRPC_TYPE_STRING) {
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value of type %s supplied where "
"string type was expected.",
xmlrpc_type_name(valueP->_type));
}
}
static void
accessStringValue(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const char ** const contentsP) {
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
size_t const size =
XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP);
const char * const contents =
XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
size_t const len = size - 1;
/* The memblock has a null character added to the end */
verifyNoNulls(envP, contents, len);
*lengthP = len;
*contentsP = contents;
}
}
void
xmlrpc_read_string(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
/*----------------------------------------------------------------------------
Read the value of an XML-RPC string as an ASCIIZ string, with
LF for line delimiters.
Return the string in newly malloc'ed storage that Caller must free.
Fail if the string contains null characters (which means it wasn't
really a string, but XML-RPC doesn't seem to understand what a string
is, and such values are possible).
-----------------------------------------------------------------------------*/
size_t length;
const char * contents;
accessStringValue(envP, valueP, &length, &contents);
if (!envP->fault_occurred) {
char * stringValue;
MALLOCARRAY(stringValue, length + 1);
if (stringValue == NULL)
xmlrpc_faultf(envP, "Unable to allocate space "
"for %u-character string", (unsigned)length);
else {
memcpy(stringValue, contents, length);
stringValue[length] = '\0';
*stringValueP = stringValue;
}
}
}
static unsigned int
lineDelimCount(const char * const start,
const char * const end) {
unsigned int count;
const char * p;
for (p = start, count = 0; p < end; ) {
const char * const nlPos = memchr(p, '\n', end-p);
if (nlPos) {
++count;
p = nlPos + 1;
} else
p = end;
}
return count;
}
static void
copyAndConvertLfToCrlf(xmlrpc_env * const envP,
size_t const srcLen,
const char * const src,
size_t * const dstLenP,
const char ** const dstP) {
const char * const srcEnd = src + srcLen;
unsigned int const nLineDelim = lineDelimCount(src, srcEnd);
size_t const dstLen = srcLen + nLineDelim;
char * dst;
MALLOCARRAY(dst, dstLen + 1);
if (dst == NULL)
xmlrpc_faultf(envP, "Unable to allocate space "
"for %u-character string", (unsigned)dstLen + 1);
else {
const char * p; /* source pointer */
char * q; /* destination pointer */
for (p = &src[0], q = &dst[0]; p < srcEnd; ++p) {
if (*p == '\n')
*q++ = '\r';
*q++ = *p;
}
XMLRPC_ASSERT(q == dst + dstLen);
*q = '\0';
*dstP = dst;
*dstLenP = dstLen;
}
}
void
xmlrpc_read_string_crlf(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
/*----------------------------------------------------------------------------
Same as xmlrpc_read_string(), but return CRLF instead of LF for
line delimiters.
-----------------------------------------------------------------------------*/
size_t length;
const char * contents;
accessStringValue(envP, valueP, &length, &contents);
if (!envP->fault_occurred) {
size_t stringLen;
copyAndConvertLfToCrlf(envP, length, contents,
&stringLen, stringValueP);
}
}
void
xmlrpc_read_string_old(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP) {
/*----------------------------------------------------------------------------
Like xmlrpc_read_string(), except it returns as *stringValueP a pointer
into memory owned by *valueP, rather than new memory to be owned by
Caller.
This is for internal use; it's necessary to implement the deprecated
xmlrpc_parse_value(), which also returns pointers to someone else's
storage.
-----------------------------------------------------------------------------*/
size_t length;
accessStringValue(envP, valueP, &length, stringValueP);
}
void
xmlrpc_read_string_lp(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const char ** const stringValueP) {
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
size_t const size =
XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP);
const char * const contents =
XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
char * stringValue;
stringValue = malloc(size);
if (stringValue == NULL)
xmlrpc_faultf(envP, "Unable to allocate %u bytes for string.",
(unsigned int)size);
else {
memcpy(stringValue, contents, size);
*stringValueP = stringValue;
*lengthP = size - 1; /* Size includes terminating NUL */
}
}
}
void
xmlrpc_read_string_lp_crlf(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const char ** const stringValueP) {
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
size_t const size =
XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP); /* Includes NUL */
const char * const contents =
XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
copyAndConvertLfToCrlf(envP, size-1, contents,
lengthP, stringValueP);
}
}
void
xmlrpc_read_string_lp_old(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const char ** const stringValueP) {
/*----------------------------------------------------------------------------
This is to xmlrpc_read_string_lp() as xmlrpc_read_string_old() is
to xmlrpc_read_string().
-----------------------------------------------------------------------------*/
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
*lengthP = XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP) - 1;
*stringValueP = XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
}
}
static __inline__ void
setupWcsBlock(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
/*----------------------------------------------------------------------------
Add a wcs block (wchar_t string) to the indicated xmlrpc_value if it
doesn't have one already.
-----------------------------------------------------------------------------*/
if (!valueP->_wcs_block) {
char * const contents =
XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
size_t const len =
XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP) - 1;
valueP->_wcs_block =
xmlrpc_utf8_to_wcs(envP, contents, len + 1);
}
}
#if HAVE_UNICODE_WCHAR
static void
accessStringValueW(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
size_t * const lengthP,
const wchar_t ** const stringValueP) {
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
setupWcsBlock(envP, valueP);
if (!envP->fault_occurred) {
wchar_t * const wcontents =
XMLRPC_MEMBLOCK_CONTENTS(wchar_t, valueP->_wcs_block);
size_t const len =
XMLRPC_MEMBLOCK_SIZE(wchar_t, valueP->_wcs_block) - 1;
verifyNoNullsW(envP, wcontents, len);
*lengthP = len;
*stringValueP = wcontents;
}
}
}
void
xmlrpc_read_string_w(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
const wchar_t ** const stringValueP) {
size_t length;
const wchar_t * wcontents;
accessStringValueW(envP, valueP, &length, &wcontents);
if (!envP->fault_occurred) {
wchar_t * stringValue;
MALLOCARRAY(stringValue, length + 1);
if (stringValue == NULL)
xmlrpc_faultf(envP, "Unable to allocate space for %u-byte string",
(unsigned)length);
else {
memcpy(stringValue, wcontents, length * sizeof(wchar_t));
stringValue[length] = '\0';
*stringValueP = stringValue;
}
}
}
static unsigned int
lineDelimCountW(const wchar_t * const start,
const wchar_t * const end) {
unsigned int count;
const wchar_t * p;
count = 0;
p = start;
while (p && p < end) {
/* We used to use memchr(), but Windows doesn't have it */
p = wcsstr(p, L"\n");
if (p && p < end) {
++count; /* count the newline */
++p; /* skip the newline */
}
}
return count;
}
static void
wCopyAndConvertLfToCrlf(xmlrpc_env * const envP,
size_t const srcLen,
const wchar_t * const src,
size_t * const dstLenP,
const wchar_t ** const dstP) {
const wchar_t * const srcEnd = src + srcLen;
unsigned int const nLineDelim = lineDelimCountW(src, srcEnd);
size_t const dstLen = srcLen + nLineDelim;
wchar_t * dst;
MALLOCARRAY(dst, dstLen + 1);
if (dst == NULL)
xmlrpc_faultf(envP, "Unable to allocate space "
"for %u-character string", (unsigned)dstLen + 1);
else {
const wchar_t * p; /* source pointer */
wchar_t * q; /* destination pointer */
for (p = &src[0], q = &dst[0]; p < srcEnd; ++p) {
if (*p == '\n')
*q++ = '\r';
*q++ = *p;
}
XMLRPC_ASSERT(q == dst + dstLen);
*q = '\0';
*dstP = dst;
*dstLenP = dstLen;
}
}
void
xmlrpc_read_string_w_crlf(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
const wchar_t ** const stringValueP) {
size_t size;
const wchar_t * contents;
accessStringValueW(envP, valueP, &size, &contents);
if (!envP->fault_occurred) {
size_t stringLen;
wCopyAndConvertLfToCrlf(envP, size, contents,
&stringLen, stringValueP);
}
}
void
xmlrpc_read_string_w_old(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
const wchar_t ** const stringValueP) {
/*----------------------------------------------------------------------------
This is to xmlrpc_read_string_w() as xmlrpc_read_string_old() is
to xmlrpc_read_string().
-----------------------------------------------------------------------------*/
size_t length;
accessStringValueW(envP, valueP, &length, stringValueP);
}
void
xmlrpc_read_string_w_lp(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
size_t * const lengthP,
const wchar_t ** const stringValueP) {
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
setupWcsBlock(envP, valueP);
if (!envP->fault_occurred) {
wchar_t * const wcontents =
XMLRPC_MEMBLOCK_CONTENTS(wchar_t, valueP->_wcs_block);
size_t const size =
XMLRPC_MEMBLOCK_SIZE(wchar_t, valueP->_wcs_block);
wchar_t * stringValue;
MALLOCARRAY(stringValue, size);
if (stringValue == NULL)
xmlrpc_faultf(envP,
"Unable to allocate space for %u-byte string",
(unsigned int)size);
else {
memcpy(stringValue, wcontents, size * sizeof(wchar_t));
*lengthP = size - 1; /* size includes terminating NUL */
*stringValueP = stringValue;
}
}
}
}
void
xmlrpc_read_string_w_lp_crlf(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
size_t * const lengthP,
const wchar_t ** const stringValueP) {
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
setupWcsBlock(envP, valueP);
if (!envP->fault_occurred) {
size_t const size =
XMLRPC_MEMBLOCK_SIZE(wchar_t, valueP->_wcs_block);
wchar_t * const wcontents =
XMLRPC_MEMBLOCK_CONTENTS(wchar_t, valueP->_wcs_block);
wCopyAndConvertLfToCrlf(envP, size-1, wcontents,
lengthP, stringValueP);
}
}
}
void
xmlrpc_read_string_w_lp_old(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
size_t * const lengthP,
const wchar_t ** const stringValueP) {
/*----------------------------------------------------------------------------
This is to xmlrpc_read_string_w_lp() as xmlrpc_read_string_old() is
to xmlrpc_read_string().
-----------------------------------------------------------------------------*/
validateStringType(envP, valueP);
if (!envP->fault_occurred) {
setupWcsBlock(envP, valueP);
if (!envP->fault_occurred) {
wchar_t * const wcontents =
XMLRPC_MEMBLOCK_CONTENTS(wchar_t, valueP->_wcs_block);
size_t const size =
XMLRPC_MEMBLOCK_SIZE(wchar_t, valueP->_wcs_block);
*lengthP = size - 1; /* size includes terminating NUL */
*stringValueP = wcontents;
}
}
}
#endif /* HAVE_UNICODE_WCHAR */
void
xmlrpc_string_validate(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
/*----------------------------------------------------------------------------
Validate that *valueP is not the kind of value that would serialize into
invalid XML.
To wit, the string does not contain any characters that are not valid XML
characters, which means it does not contain ASCII control characters
other than carriage return, line feed, and tab.
Xmlrpc-c has the odd function, because of mistakes that were made in the
early days of XML-RPC, that it is possible to cause it to generate an
XML-RPC call or response that is invalid XML, which you do with a string
value which contains things other than XML characters. But it's a really
bad idea to exercise that option, so code that constructs string values can
use this function to ensure that it does not. See the discussion of the
string data type in the Xmlrpc-c user's guide for a detailed explanation of
this issue.
-----------------------------------------------------------------------------*/
enum {LF = 0x0a, CR = 0x0d, TAB = 0x08};
size_t length;
const char * contents;
accessStringValue(envP, valueP, &length, &contents);
if (!envP->fault_occurred) {
unsigned int i;
for (i = 0; i < length && !envP->fault_occurred; ++i) {
char const c = contents[i];
if (iscntrl(c) && c != LF && c != CR && c != TAB)
xmlrpc_faultf(envP, "String contains an invalid value "
"(Not a Unicode codepoint for a legal XML "
"character) x%02x at position %u",
c, i);
}
}
}
static void
copyLines(xmlrpc_env * const envP,
const char * const src,
size_t const srcLen,
xmlrpc_mem_block ** const dstPP) {
/*----------------------------------------------------------------------------
Generate the internal representation of string xmlrpc_value contents for
string 'src', which is 'srcLen' characters long and has lines separated by
LF, CR, and/or CRLF. Return it as a new xmlrpc_mem_block at *dstPP.
Note that the source format differs from the destination format in
that in the destination format, lines are separated only by newline
(LF).
It is tempting to believe that if we just put the user's line
delimiters in the xmlrpc_value here (i.e. where user has CRLF, the
xmlrpc_value also has CRLF), the user's line delimiters would go
all the way across to the XML-RPC partner. But that won't work,
because the XML processor on the other side will, following Section
2.11 of the XML spec, normalize all line endings to LF anyhow. So
then you might ask, why do we bother to do all the work to convert
them here? Because: besides just being logically cleaner, this way
xmlrpc_read_string() gets the proper value -- the same one the
XML-RPC partner would see.
-----------------------------------------------------------------------------*/
/* Destination format is sometimes smaller than source (because
CRLF turns into LF), but never smaller. So we allocate
destination space equal to source size (plus one for
terminating NUL), but don't necessarily use it all.
*/
/* To convert LF, CR, and CRLF to LF, all we have to do is
copy everything up to a CR verbatim, then insert an LF and
skip the CR and any following LF, and repeat.
*/
xmlrpc_mem_block * dstP;
dstP = XMLRPC_MEMBLOCK_NEW(char, envP, srcLen + 1);
if (!envP->fault_occurred) {
const char * const srcEnd = &src[srcLen];
char * const contents = XMLRPC_MEMBLOCK_CONTENTS(char, dstP);
const char * srcCursor;
char * dstCursor;
for (srcCursor = &src[0], dstCursor = &contents[0];
srcCursor < srcEnd;) {
char * const crPos = memchr(srcCursor, '\r', srcEnd - srcCursor);
if (crPos) {
size_t const copyLen = crPos - srcCursor;
memcpy(dstCursor, srcCursor, copyLen);
srcCursor += copyLen;
dstCursor += copyLen;
*(dstCursor++) = '\n';
XMLRPC_ASSERT(*srcCursor == '\r');
++srcCursor; /* Move past CR */
if (*srcCursor == '\n')
++srcCursor; /* Move past LF */
} else {
size_t const remainingLen = srcEnd - srcCursor;
memcpy(dstCursor, srcCursor, remainingLen);
srcCursor += remainingLen;
dstCursor += remainingLen;
}
}
*dstCursor++ = '\0';
XMLRPC_ASSERT((unsigned)(dstCursor - &contents[0]) <= srcLen + 1);
XMLRPC_MEMBLOCK_RESIZE(char, envP, dstP, dstCursor - &contents[0]);
if (envP->fault_occurred)
XMLRPC_MEMBLOCK_FREE(char, dstP);
}
*dstPP = dstP;
}
static void
copySimple(xmlrpc_env * const envP,
const char * const src,
size_t const srcLen,
xmlrpc_mem_block ** const dstPP) {
/*----------------------------------------------------------------------------
Generate the internal representation of string xmlrpc_value contents for
string 'src', which is 'srcLen' characters long and, conveniently enough,
has the exact same format as the internal representation.
To wit, 'src' has lines separated by LFs only -- no CR or CRLF.
Return it as a new xmlrpc_mem_block at *dstPP.
-----------------------------------------------------------------------------*/
xmlrpc_mem_block * dstP;
dstP = XMLRPC_MEMBLOCK_NEW(char, envP, srcLen + 1);
if (!envP->fault_occurred) {
char * const contents = XMLRPC_MEMBLOCK_CONTENTS(char, dstP);
memcpy(contents, src, srcLen);
contents[srcLen] = '\0';
}
*dstPP = dstP;
}
enum crTreatment { CR_IS_LINEDELIM, CR_IS_CHAR };
static void
stringNew(xmlrpc_env * const envP,
size_t const length,
const char * const value,
enum crTreatment const crTreatment,
xmlrpc_value ** const valPP) {
xmlrpc_value * valP;
xmlrpc_validate_utf8(envP, value, length);
if (!envP->fault_occurred) {
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_STRING;
valP->_wcs_block = NULL;
/* Note that copyLines() works for strings with no CRs, but
it's slower.
*/
if (memchr(value, '\r', length) && crTreatment == CR_IS_LINEDELIM)
copyLines(envP, value, length, &valP->blockP);
else
copySimple(envP, value, length, &valP->blockP);
if (envP->fault_occurred)
free(valP);
else
*valPP = valP;
}
}
}
xmlrpc_value *
xmlrpc_string_new_lp(xmlrpc_env * const envP,
size_t const length,
const char * const value) {
xmlrpc_value * retval;
stringNew(envP, length, value, CR_IS_LINEDELIM, &retval);
return retval;
}
xmlrpc_value *
xmlrpc_string_new_lp_cr(xmlrpc_env * const envP,
size_t const length,
const char * const value) {
xmlrpc_value * retval;
stringNew(envP, length, value, CR_IS_CHAR, &retval);
return retval;
}
xmlrpc_value *
xmlrpc_string_new(xmlrpc_env * const envP,
const char * const value) {
xmlrpc_value * retval;
stringNew(envP, strlen(value), value, CR_IS_LINEDELIM, &retval);
return retval;
}
xmlrpc_value *
xmlrpc_string_new_cr(xmlrpc_env * const envP,
const char * const value) {
xmlrpc_value * retval;
stringNew(envP, strlen(value), value, CR_IS_CHAR, &retval);
return retval;
}
xmlrpc_value *
xmlrpc_string_new_va(xmlrpc_env * const envP,
const char * const format,
va_list args) {
const char * formattedString;
xmlrpc_value * retvalP;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT(format != NULL);
xmlrpc_vasprintf(&formattedString, format, args);
if (xmlrpc_strnomem(formattedString)) {
xmlrpc_faultf(envP, "Out of memory building formatted string");
retvalP = NULL; /* defeat compiler warning */
} else
retvalP = xmlrpc_string_new(envP, formattedString);
xmlrpc_strfree(formattedString);
return retvalP;
}
xmlrpc_value *
xmlrpc_string_new_f(xmlrpc_env * const envP,
const char * const format,
...) {
va_list args;
xmlrpc_value * retval;
va_start(args, format);
retval = xmlrpc_string_new_va(envP, format, args);
va_end(args);
return retval;
}
#if HAVE_UNICODE_WCHAR
static void
stringWNew(xmlrpc_env * const envP,
size_t const length,
const wchar_t * const value,
enum crTreatment const crTreatment,
xmlrpc_value ** const valPP) {
xmlrpc_mem_block * utf8P;
utf8P = xmlrpc_wcs_to_utf8(envP, value, length);
if (!envP->fault_occurred) {
char * const utf8_value = XMLRPC_MEMBLOCK_CONTENTS(char, utf8P);
size_t const utf8_len = XMLRPC_MEMBLOCK_SIZE(char, utf8P);
if (!envP->fault_occurred) {
stringNew(envP, utf8_len, utf8_value, crTreatment, valPP);
XMLRPC_MEMBLOCK_FREE(char, utf8P);
}
}
}
xmlrpc_value *
xmlrpc_string_w_new_lp(xmlrpc_env * const envP,
size_t const length,
const wchar_t * const value) {
xmlrpc_value * valP;
stringWNew(envP, length, value, CR_IS_LINEDELIM, &valP);
return valP;
}
xmlrpc_value *
xmlrpc_string_w_new_lp_cr(xmlrpc_env * const envP,
size_t const length,
const wchar_t * const value) {
xmlrpc_value * valP;
stringWNew(envP, length, value, CR_IS_CHAR, &valP);
return valP;
}
xmlrpc_value *
xmlrpc_string_w_new(xmlrpc_env * const envP,
const wchar_t * const value) {
xmlrpc_value * valP;
stringWNew(envP, wcslen(value), value, CR_IS_LINEDELIM, &valP);
return valP;
}
xmlrpc_value *
xmlrpc_string_w_new_cr(xmlrpc_env * const envP,
const wchar_t * const value) {
xmlrpc_value * valP;
stringWNew(envP, wcslen(value), value, CR_IS_CHAR, &valP);
return valP;
}
#endif /* HAVE_UNICODE_WCHAR */
xmlrpc_value *
xmlrpc_string_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * valP;
if (valueP->_type != XMLRPC_TYPE_STRING) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not a string. "
"It is type #%d", valueP->_type);
valP = NULL;
} else {
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_STRING;
valP->blockP =
xmlrpc_mem_block_new(envP,
xmlrpc_mem_block_size(valueP->blockP));
if (!envP->fault_occurred) {
memcpy(xmlrpc_mem_block_contents(valP->blockP),
xmlrpc_mem_block_contents(valueP->blockP),
xmlrpc_mem_block_size(valueP->blockP));
}
}
if (!envP->fault_occurred) {
if (valueP->_wcs_block) {
valP->_wcs_block =
xmlrpc_mem_block_new(
envP,
xmlrpc_mem_block_size(valueP->_wcs_block));
if (!envP->fault_occurred) {
memcpy(xmlrpc_mem_block_contents(valP->_wcs_block),
xmlrpc_mem_block_contents(valueP->_wcs_block),
xmlrpc_mem_block_size(valueP->_wcs_block));
}
} else
valP->_wcs_block = NULL;
}
}
return valP;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.