Menu

[r34]: / trunk / ext / build / utf.c  Maximize  Restore  History

Download this file

1332 lines (1179 with data), 45.0 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
/*
* utf.c: UTF-8 conversion routines
*
* ====================================================================
* Copyright (c) 2000-2007 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
*/
#include <string.h>
#include <assert.h>
#include <apr_strings.h>
#include <apr_lib.h>
#include <apr_xlate.h>
#include "svn_string.h"
#include "svn_error.h"
#include "svn_pools.h"
#include "svn_ctype.h"
#include "svn_utf.h"
#include "utf_impl.h"
#include "svn_private_config.h"
#define SVN_UTF_NTOU_XLATE_HANDLE "svn-utf-ntou-xlate-handle"
#define SVN_UTF_UTON_XLATE_HANDLE "svn-utf-uton-xlate-handle"
#ifndef AS400
#define SVN_APR_UTF8_CHARSET "UTF-8"
#else
#define SVN_APR_UTF8_CHARSET (const char*)1208
#endif
#if !APR_HAS_XLATE
#if APR_HAS_THREADS
static apr_thread_mutex_t *xlate_handle_mutex = NULL;
#endif
/* The xlate handle cache is a global hash table with linked lists of xlate
* handles. In multi-threaded environments, a thread "borrows" an xlate
* handle from the cache during a translation and puts it back afterwards.
* This avoids holding a global lock for all translations.
* If there is no handle for a particular key when needed, a new is
* handle is created and put in the cache after use.
* This means that there will be at most N handles open for a key, where N
* is the number of simultanous handles in use for that key. */
typedef struct xlate_handle_node_t {
apr_xlate_t *handle;
/* FALSE if the handle is not valid, since its pool is being
destroyed. */
svn_boolean_t valid;
/* The name of a char encoding or APR_LOCALE_CHARSET. */
const char *frompage, *topage;
struct xlate_handle_node_t *next;
} xlate_handle_node_t;
/* This maps const char * userdata_key strings to xlate_handle_node_t **
handles to the first entry in the linked list of xlate handles. We don't
store the pointer to the list head directly in the hash table, since we
remove/insert entries at the head in the list in the code below, and
we can't use apr_hash_set() in each character translation because that
function allocates memory in each call where the value is non-NULL.
Since these allocations take place in a global pool, this would be a
memory leak. */
static apr_hash_t *xlate_handle_hash = NULL;
/* Clean up the xlate handle cache. */
static apr_status_t
xlate_cleanup(void *arg)
{
/* We set the cache variables to NULL so that translation works in other
cleanup functions, even if it isn't cached then. */
#if APR_HAS_THREADS
apr_thread_mutex_destroy(xlate_handle_mutex);
xlate_handle_mutex = NULL;
#endif
xlate_handle_hash = NULL;
return APR_SUCCESS;
}
/* Set the handle of ARG to NULL. */
static apr_status_t
xlate_handle_node_cleanup(void *arg)
{
xlate_handle_node_t *node = arg;
node->valid = FALSE;
return APR_SUCCESS;
}
#endif /* !APR_HAS_XLATE */
void
svn_utf_initialize(apr_pool_t *pool)
{
#if APR_HAS_XLATE
apr_pool_t *subpool;
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex;
#endif
if (!xlate_handle_hash)
{
/* We create our own subpool, which we protect with the mutex.
We can't use the pool passed to us by the caller, since we will
use it for xlate handle allocations, possibly in multiple threads,
and pool allocation is not thread-safe. */
subpool = svn_pool_create(pool);
#if APR_HAS_THREADS
if (apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, subpool)
== APR_SUCCESS)
xlate_handle_mutex = mutex;
else
return;
#endif
xlate_handle_hash = apr_hash_make(subpool);
apr_pool_cleanup_register(subpool, NULL, xlate_cleanup,
apr_pool_cleanup_null);
}
#endif /* APR_HAS_XLATE */
}
#if APR_HAS_XLATE
/* Return a unique string key based on TOPAGE and FROMPAGE. TOPAGE and
* FROMPAGE can be any valid arguments of the same name to
* apr_xlate_open(). Allocate the returned string in POOL. */
static const char*
get_xlate_key(const char *topage,
const char *frompage,
apr_pool_t *pool)
{
#ifndef AS400
/* In the cases of SVN_APR_LOCALE_CHARSET and SVN_APR_DEFAULT_CHARSET
* topage/frompage is really an int, not a valid string. So generate a
* unique key accordingly. */
if (frompage == SVN_APR_LOCALE_CHARSET)
frompage = "APR_LOCALE_CHARSET";
else if (frompage == SVN_APR_DEFAULT_CHARSET)
frompage = "APR_DEFAULT_CHARSET";
if (topage == SVN_APR_LOCALE_CHARSET)
topage = "APR_LOCALE_CHARSET";
else if (topage == SVN_APR_DEFAULT_CHARSET)
topage = "APR_DEFAULT_CHARSET";
return apr_pstrcat(pool, "svn-utf-", frompage, "to", topage,
"-xlate-handle", NULL);
#else
/* OS400 code pages are always ints. */
return apr_psprintf(pool, "svn-utf-%dto%d-xlate-handle", (int)frompage,
(int)topage);
#endif
}
/* Set *RET to a handle node for converting from FROMPAGE to TOPAGE,
creating the handle node if it doesn't exist in USERDATA_KEY.
If a node is not cached and apr_xlate_open() returns APR_EINVAL or
APR_ENOTIMPL, set (*RET)->handle to NULL. If fail for any other
reason, return the error.
Allocate *RET and its xlate handle in POOL if svn_utf_initialize()
hasn't been called or USERDATA_KEY is NULL. Else, allocate them
in the pool of xlate_handle_hash. */
static svn_error_t *
get_xlate_handle_node(xlate_handle_node_t **ret,
const char *topage, const char *frompage,
const char *userdata_key, apr_pool_t *pool)
{
xlate_handle_node_t **old_node_p;
xlate_handle_node_t *old_node = NULL;
apr_status_t apr_err;
apr_xlate_t *handle;
svn_error_t *err = NULL;
/* If we already have a handle, just return it. */
if (userdata_key)
{
if (xlate_handle_hash)
{
#if APR_HAS_THREADS
apr_err = apr_thread_mutex_lock(xlate_handle_mutex);
if (apr_err != APR_SUCCESS)
return svn_error_create(apr_err, NULL,
_("Can't lock charset translation mutex"));
#endif
old_node_p = apr_hash_get(xlate_handle_hash, userdata_key,
APR_HASH_KEY_STRING);
if (old_node_p)
old_node = *old_node_p;
if (old_node)
{
/* Ensure that the handle is still valid. */
if (old_node->valid)
{
/* Remove from the list. */
*old_node_p = old_node->next;
old_node->next = NULL;
#if APR_HAS_THREADS
apr_err = apr_thread_mutex_unlock(xlate_handle_mutex);
if (apr_err != APR_SUCCESS)
return svn_error_create(apr_err, NULL,
_("Can't unlock charset "
"translation mutex"));
#endif
*ret = old_node;
return SVN_NO_ERROR;
}
}
}
else
{
void *p;
/* We fall back on a per-pool cache instead. */
apr_pool_userdata_get(&p, userdata_key, pool);
old_node = p;
/* Ensure that the handle is still valid. */
if (old_node && old_node->valid)
{
*ret = old_node;
return SVN_NO_ERROR;
}
}
}
/* Note that we still have the mutex locked (if it is initialized), so we
can use the global pool for creating the new xlate handle. */
/* The error handling doesn't support the following cases, since we don't
use them currently. Catch this here. */
#ifndef AS400
/* On OS400 V5R4 with UTF support, APR_DEFAULT_CHARSET and
* APR_LOCALE_CHARSET are both UTF-8 (CCSID 1208), so we won't get far
* with this assert active. */
assert(frompage != SVN_APR_DEFAULT_CHARSET
&& topage != SVN_APR_DEFAULT_CHARSET
&& (frompage != SVN_APR_LOCALE_CHARSET
|| topage != SVN_APR_LOCALE_CHARSET));
#endif
/* Use the correct pool for creating the handle. */
if (userdata_key && xlate_handle_hash)
pool = apr_hash_pool_get(xlate_handle_hash);
/* Try to create a handle. */
#ifndef AS400
apr_err = apr_xlate_open(&handle, topage, frompage, pool);
#else
apr_err = apr_xlate_open(&handle, (int)topage, (int)frompage, pool);
#endif
if (APR_STATUS_IS_EINVAL(apr_err) || APR_STATUS_IS_ENOTIMPL(apr_err))
handle = NULL;
else if (apr_err != APR_SUCCESS)
{
const char *errstr;
/* Can't use svn_error_wrap_apr here because it calls functions in
this file, leading to infinite recursion. */
#ifndef AS400
if (frompage == SVN_APR_LOCALE_CHARSET)
errstr = apr_psprintf(pool,
_("Can't create a character converter from "
"native encoding to '%s'"), topage);
else if (topage == SVN_APR_LOCALE_CHARSET)
errstr = apr_psprintf(pool,
_("Can't create a character converter from "
"'%s' to native encoding"), frompage);
else
errstr = apr_psprintf(pool,
_("Can't create a character converter from "
"'%s' to '%s'"), frompage, topage);
#else
/* Handle the error condition normally prevented by the assert
* above. */
errstr = apr_psprintf(pool,
_("Can't create a character converter from "
"'%i' to '%i'"), frompage, topage);
#endif
err = svn_error_create(apr_err, NULL, errstr);
goto cleanup;
}
/* Allocate and initialize the node. */
*ret = apr_palloc(pool, sizeof(xlate_handle_node_t));
(*ret)->handle = handle;
(*ret)->valid = TRUE;
(*ret)->frompage = ((frompage != SVN_APR_LOCALE_CHARSET)
? apr_pstrdup(pool, frompage) : frompage);
(*ret)->topage = ((topage != SVN_APR_LOCALE_CHARSET)
? apr_pstrdup(pool, topage) : topage);
(*ret)->next = NULL;
/* If we are called from inside a pool cleanup handler, the just created
xlate handle will be closed when that handler returns by a newly
registered cleanup handler, however, the handle is still cached by us.
To prevent this, we register a cleanup handler that will reset the valid
flag of our node, so we don't use an invalid handle. */
if (handle)
apr_pool_cleanup_register(pool, *ret, xlate_handle_node_cleanup,
apr_pool_cleanup_null);
cleanup:
/* Don't need the lock anymore. */
#if APR_HAS_THREADS
if (userdata_key && xlate_handle_hash)
{
apr_status_t unlock_err = apr_thread_mutex_unlock(xlate_handle_mutex);
if (unlock_err != APR_SUCCESS)
return svn_error_create(unlock_err, NULL,
_("Can't unlock charset translation mutex"));
}
#endif
return err;
}
/* Put back NODE into the xlate handle cache for use by other calls.
If there is no global cache, store the handle in POOL.
Ignore errors related to locking/unlocking the mutex.
### Mutex errors here are very weird. Should we handle them "correctly"
### even if that complicates error handling in the routines below? */
static void
put_xlate_handle_node(xlate_handle_node_t *node,
const char *userdata_key,
apr_pool_t *pool)
{
assert(node->next == NULL);
if (!userdata_key)
return;
if (xlate_handle_hash)
{
xlate_handle_node_t **node_p;
#if APR_HAS_THREADS
if (apr_thread_mutex_lock(xlate_handle_mutex) != APR_SUCCESS)
abort();
#endif
node_p = apr_hash_get(xlate_handle_hash, userdata_key,
APR_HASH_KEY_STRING);
if (node_p == NULL)
{
userdata_key = apr_pstrdup(apr_hash_pool_get(xlate_handle_hash),
userdata_key);
node_p = apr_palloc(apr_hash_pool_get(xlate_handle_hash),
sizeof(*node_p));
*node_p = NULL;
apr_hash_set(xlate_handle_hash, userdata_key,
APR_HASH_KEY_STRING, node_p);
}
node->next = *node_p;
*node_p = node;
#if APR_HAS_THREADS
if (apr_thread_mutex_unlock(xlate_handle_mutex) != APR_SUCCESS)
abort();
#endif
}
else
{
/* Store it in the per-pool cache. */
apr_pool_userdata_set(node, userdata_key, apr_pool_cleanup_null, pool);
}
}
/* Return the apr_xlate handle for converting native characters to UTF-8. */
static svn_error_t *
get_ntou_xlate_handle_node(xlate_handle_node_t **ret, apr_pool_t *pool)
{
return get_xlate_handle_node(ret, SVN_APR_UTF8_CHARSET,
SVN_APR_LOCALE_CHARSET,
SVN_UTF_NTOU_XLATE_HANDLE, pool);
}
/* Return the apr_xlate handle for converting UTF-8 to native characters.
Create one if it doesn't exist. If unable to find a handle, or
unable to create one because apr_xlate_open returned APR_EINVAL, then
set *RET to null and return SVN_NO_ERROR; if fail for some other
reason, return error. */
static svn_error_t *
get_uton_xlate_handle_node(xlate_handle_node_t **ret, apr_pool_t *pool)
{
return get_xlate_handle_node(ret, SVN_APR_LOCALE_CHARSET,
SVN_APR_UTF8_CHARSET,
SVN_UTF_UTON_XLATE_HANDLE, pool);
}
#endif /* APR_HAS_XLATE */
/* Copy LEN bytes of SRC, converting non-ASCII and zero bytes to ?\nnn
sequences, allocating the result in POOL. */
static const char *
fuzzy_escape(const char *src, apr_size_t len, apr_pool_t *pool)
{
const char *src_orig = src, *src_end = src + len;
apr_size_t new_len = 0;
char *new;
const char *new_orig;
/* First count how big a dest string we'll need. */
while (src < src_end)
{
if (! svn_ctype_isascii(*src) || *src == '\0')
new_len += 5; /* 5 slots, for "?\XXX" */
else
new_len += 1; /* one slot for the 7-bit char */
src++;
}
/* Allocate that amount. */
new = apr_palloc(pool, new_len + 1);
new_orig = new;
/* And fill it up. */
while (src_orig < src_end)
{
if (! svn_ctype_isascii(*src_orig) || src_orig == '\0')
{
/* This is the same format as svn_xml_fuzzy_escape uses, but that
function escapes different characters. Please keep in sync!
### If we add another fuzzy escape somewhere, we should abstract
### this out to a common function. */
sprintf(new, "?\\%03u", (unsigned char) *src_orig);
new += 5;
}
else
{
*new = *src_orig;
new += 1;
}
src_orig++;
}
*new = '\0';
return new_orig;
}
#if APR_HAS_XLATE
/* Convert SRC_LENGTH bytes of SRC_DATA in NODE->handle, store the result
in *DEST, which is allocated in POOL. */
static svn_error_t *
convert_to_stringbuf(xlate_handle_node_t *node,
const char *src_data,
apr_size_t src_length,
svn_stringbuf_t **dest,
apr_pool_t *pool)
{
apr_size_t buflen = src_length * 2;
apr_status_t apr_err;
apr_size_t srclen = src_length;
apr_size_t destlen = buflen;
char *destbuf;
/* Initialize *DEST to an empty stringbuf. */
*dest = svn_stringbuf_create("", pool);
destbuf = (*dest)->data;
/* Not only does it not make sense to convert an empty string, but
apr-iconv is quite unreasonable about not allowing that. */
if (src_length == 0)
return SVN_NO_ERROR;
do
{
/* A 1:2 ratio of input bytes to output bytes (as assigned above)
should be enough for most translations, and if it turns out not
to be enough, we'll grow the buffer again, sizing it based on a
1:3 ratio of the remainder of the string.
We also want to ensure that the output buffer always has at
least 3 bytes spare so that we always have room to convert at
least one character (we assume that no encoding uses more than
three bytes for a character) */
if (destlen < 3)
buflen += (srclen * 3);
/* Ensure that *DEST has sufficient storage for the translated
result. */
svn_stringbuf_ensure(*dest, buflen + 1);
/* Update the destination buffer pointer to the first character
after already-converted output. */
destbuf = (*dest)->data + (*dest)->len;
/* Set up state variables for xlate. */
destlen = buflen - (*dest)->len;
assert(destlen >= 3);
/* Attempt the conversion. */
apr_err = apr_xlate_conv_buffer(node->handle,
src_data + (src_length - srclen),
&srclen,
destbuf,
&destlen);
/* Now, update the *DEST->len to track the amount of output data
churned out so far from this loop. */
(*dest)->len += ((buflen - (*dest)->len) - destlen);
} while (! apr_err && srclen);
/* If we exited the loop with an error, return the error. */
if (apr_err)
{
const char *errstr;
svn_error_t *err;
/* Can't use svn_error_wrap_apr here because it calls functions in
this file, leading to infinite recursion. */
#ifndef AS400
if (node->frompage == SVN_APR_LOCALE_CHARSET)
errstr = apr_psprintf
(pool, _("Can't convert string from native encoding to '%s':"),
node->topage);
else if (node->topage == SVN_APR_LOCALE_CHARSET)
errstr = apr_psprintf
(pool, _("Can't convert string from '%s' to native encoding:"),
node->frompage);
else
errstr = apr_psprintf
(pool, _("Can't convert string from '%s' to '%s':"),
node->frompage, node->topage);
#else
/* On OS400 V5R4 every possible node->topage and node->frompage
* *really* is an int. */
errstr = apr_psprintf
(pool, _("Can't convert string from CCSID '%i' to CCSID '%i'"),
node->frompage, node->topage);
#endif
err = svn_error_create(apr_err, NULL, fuzzy_escape(src_data,
src_length, pool));
return svn_error_create(apr_err, err, errstr);
}
/* Else, exited due to success. Trim the result buffer down to the
right length. */
(*dest)->data[(*dest)->len] = '\0';
return SVN_NO_ERROR;
}
#endif /* APR_HAS_XLATE */
/* Return APR_EINVAL if the first LEN bytes of DATA contain anything
other than seven-bit, non-control (except for whitespace) ASCII
characters, finding the error pool from POOL. Otherwise, return
SVN_NO_ERROR. */
static svn_error_t *
check_non_ascii(const char *data, apr_size_t len, apr_pool_t *pool)
{
const char *data_start = data;
for (; len > 0; --len, data++)
{
if ((! apr_isascii(*data))
|| ((! apr_isspace(*data))
&& apr_iscntrl(*data)))
{
/* Show the printable part of the data, followed by the
decimal code of the questionable character. Because if a
user ever gets this error, she's going to have to spend
time tracking down the non-ASCII data, so we want to help
as much as possible. And yes, we just call the unsafe
data "non-ASCII", even though the actual constraint is
somewhat more complex than that. */
if (data - data_start)
{
const char *error_data
= apr_pstrndup(pool, data_start, (data - data_start));
return svn_error_createf
(APR_EINVAL, NULL,
_("Safe data '%s' was followed by non-ASCII byte %d: "
"unable to convert to/from UTF-8"),
error_data, *((const unsigned char *) data));
}
else
{
return svn_error_createf
(APR_EINVAL, NULL,
_("Non-ASCII character (code %d) detected, "
"and unable to convert to/from UTF-8"),
*((const unsigned char *) data));
}
}
}
return SVN_NO_ERROR;
}
/* Construct an error with a suitable message to describe the invalid UTF-8
* sequence DATA of length LEN (which may have embedded NULLs). We can't
* simply print the data, almost by definition we don't really know how it
* is encoded.
*/
static svn_error_t *
invalid_utf8(const char *data, apr_size_t len, apr_pool_t *pool)
{
const char *last = svn_utf__last_valid(data, len);
const char *valid_txt = "", *invalid_txt = "";
int i, valid, invalid;
/* We will display at most 24 valid octets (this may split a leading
multi-byte character) as that should fit on one 80 character line. */
valid = last - data;
if (valid > 24)
valid = 24;
for (i = 0; i < valid; ++i)
valid_txt = apr_pstrcat(pool, valid_txt,
apr_psprintf(pool, " %02x",
(unsigned char)last[i-valid]), NULL);
/* 4 invalid octets will guarantee that the faulty octet is displayed */
invalid = data + len - last;
if (invalid > 4)
invalid = 4;
for (i = 0; i < invalid; ++i)
invalid_txt = apr_pstrcat(pool, invalid_txt,
apr_psprintf(pool, " %02x",
(unsigned char)last[i]), NULL);
return svn_error_createf(APR_EINVAL, NULL,
_("Valid UTF-8 data\n(hex:%s)\n"
"followed by invalid UTF-8 sequence\n(hex:%s)"),
valid_txt, invalid_txt);
}
/* Verify that the sequence DATA of length LEN is valid UTF-8 */
static svn_error_t *
check_utf8(const char *data, apr_size_t len, apr_pool_t *pool)
{
if (! svn_utf__is_valid(data, len))
return invalid_utf8(data, len, pool);
return SVN_NO_ERROR;
}
/* Verify that the NULL terminated sequence DATA is valid UTF-8 */
static svn_error_t *
check_cstring_utf8(const char *data, apr_pool_t *pool)
{
if (! svn_utf__cstring_is_valid(data))
return invalid_utf8(data, strlen(data), pool);
return SVN_NO_ERROR;
}
svn_error_t *
svn_utf_stringbuf_to_utf8(svn_stringbuf_t **dest,
const svn_stringbuf_t *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(get_ntou_xlate_handle_node(&node, pool));
if (node->handle)
{
err = convert_to_stringbuf(node, src->data, src->len, dest, pool);
if (! err)
err = check_utf8((*dest)->data, (*dest)->len, pool);
}
else
{
err = check_non_ascii(src->data, src->len, pool);
if (! err)
*dest = svn_stringbuf_dup(src, pool);
}
put_xlate_handle_node(node, SVN_UTF_NTOU_XLATE_HANDLE, pool);
return err;
#else
apr_size_t widelen = 0;
apr_size_t utf8len = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_ACP, 0, src->data, src->len, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_ACP, 0, src->data, src->len, widebuf,
(*dest)->len);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
utf8len = WideCharToMultiByte(CP_UTF8, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (utf8len == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = svn_stringbuf_ncreate("", utf8len, pool);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
utf8len = WideCharToMultiByte(CP_UTF8, 0, widebuf, widelen,
(*dest)->data, (*dest)->len, NULL, NULL);
free(widebuf);
if (utf8len == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
svn_error_t *
svn_utf_string_to_utf8(const svn_string_t **dest,
const svn_string_t *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
svn_stringbuf_t *destbuf;
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(get_ntou_xlate_handle_node(&node, pool));
if (node->handle)
{
err = convert_to_stringbuf(node, src->data, src->len, &destbuf, pool);
if (! err)
err = check_utf8(destbuf->data, destbuf->len, pool);
if (! err)
*dest = svn_string_create_from_buf(destbuf, pool);
}
else
{
err = check_non_ascii(src->data, src->len, pool);
if (! err)
*dest = svn_string_dup(src, pool);
}
put_xlate_handle_node(node, SVN_UTF_NTOU_XLATE_HANDLE, pool);
return err;
#else
apr_size_t widelen = 0;
apr_size_t utf8len = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_ACP, 0, src->data, src->len, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_ACP, 0, src->data, src->len, widebuf,
(*dest)->len);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
utf8len = WideCharToMultiByte(CP_UTF8, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (utf8len == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = svn_string_ncreate("", utf8len+1, pool);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
utf8len = WideCharToMultiByte(CP_UTF8, 0, widebuf, widelen,
(*dest)->data, utf8len, NULL, NULL);
free(widebuf);
if (utf8len == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
#if APR_HAS_XLATE
/* Common implementation for svn_utf_cstring_to_utf8,
svn_utf_cstring_to_utf8_ex, svn_utf_cstring_from_utf8 and
svn_utf_cstring_from_utf8_ex. Convert SRC to DEST using NODE->handle as
the translator and allocating from POOL. */
static svn_error_t *
convert_cstring(const char **dest,
const char *src,
xlate_handle_node_t *node,
apr_pool_t *pool)
{
if (node->handle)
{
svn_stringbuf_t *destbuf;
SVN_ERR(convert_to_stringbuf(node, src, strlen(src),
&destbuf, pool));
*dest = destbuf->data;
}
else
{
apr_size_t len = strlen(src);
SVN_ERR(check_non_ascii(src, len, pool));
*dest = apr_pstrmemdup(pool, src, len);
}
return SVN_NO_ERROR;
}
#endif /* APR_HAS_XLATE */
svn_error_t *
svn_utf_cstring_to_utf8(const char **dest,
const char *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(get_ntou_xlate_handle_node(&node, pool));
err = convert_cstring(dest, src, node, pool);
put_xlate_handle_node(node, SVN_UTF_NTOU_XLATE_HANDLE, pool);
SVN_ERR(err);
SVN_ERR(check_cstring_utf8(*dest, pool));
return SVN_NO_ERROR;
#else
apr_size_t widelen = 0;
apr_size_t utf8len = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_ACP, 0, src, -1, widebuf, widelen);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
utf8len = WideCharToMultiByte(CP_UTF8, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (utf8len == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = apr_pstrmemdup(pool, "", utf8len+1);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
utf8len = WideCharToMultiByte(CP_UTF8, 0, widebuf, widelen,
*dest, utf8len, NULL, NULL);
free(widebuf);
if (utf8len == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
svn_error_t *
svn_utf_cstring_to_utf8_ex2(const char **dest,
const char *src,
const char *frompage,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
xlate_handle_node_t *node;
svn_error_t *err;
const char *convset_key = get_xlate_key(SVN_APR_UTF8_CHARSET, frompage,
pool);
SVN_ERR(get_xlate_handle_node(&node, SVN_APR_UTF8_CHARSET, frompage,
convset_key, pool));
err = convert_cstring(dest, src, node, pool);
put_xlate_handle_node(node, convset_key, pool);
SVN_ERR(err);
SVN_ERR(check_cstring_utf8(*dest, pool));
return SVN_NO_ERROR;
#else
/* UI clients most likely never use the frompage param but leave
it NULL since they don't change the thread locale they're running
on. Which means converting from/to the 'current locale' is always
ok. */
assert((frompage == APR_DEFAULT_CHARSET)||(frompage == APR_LOCALE_CHARSET));
return svn_utf_cstring_to_utf8(dest, src, pool);
#endif
}
svn_error_t *
svn_utf_cstring_to_utf8_ex(const char **dest,
const char *src,
const char *frompage,
const char *convset_key,
apr_pool_t *pool)
{
return svn_utf_cstring_to_utf8_ex2(dest, src, frompage, pool);
}
svn_error_t *
svn_utf_stringbuf_from_utf8(svn_stringbuf_t **dest,
const svn_stringbuf_t *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(get_uton_xlate_handle_node(&node, pool));
if (node->handle)
{
err = check_utf8(src->data, src->len, pool);
if (! err)
err = convert_to_stringbuf(node, src->data, src->len, dest, pool);
}
else
{
err = check_non_ascii(src->data, src->len, pool);
if (! err)
*dest = svn_stringbuf_dup(src, pool);
}
put_xlate_handle_node(node, SVN_UTF_UTON_XLATE_HANDLE, pool);
return err;
#else
apr_size_t widelen = 0;
apr_size_t acplen = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_UTF8, 0, src->data, src->len, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_UTF8, 0, src->data, src->len,
widebuf, widelen);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (acplen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = svn_stringbuf_ncreate("", acplen, pool);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen,
(*dest)->data, (*dest)->len, NULL, NULL);
free(widebuf);
if (acplen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
svn_error_t *
svn_utf_string_from_utf8(const svn_string_t **dest,
const svn_string_t *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
svn_stringbuf_t *dbuf;
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(get_uton_xlate_handle_node(&node, pool));
if (node->handle)
{
err = check_utf8(src->data, src->len, pool);
if (! err)
err = convert_to_stringbuf(node, src->data, src->len,
&dbuf, pool);
if (! err)
*dest = svn_string_create_from_buf(dbuf, pool);
}
else
{
err = check_non_ascii(src->data, src->len, pool);
if (! err)
*dest = svn_string_dup(src, pool);
}
put_xlate_handle_node(node, SVN_UTF_UTON_XLATE_HANDLE, pool);
return err;
#else
apr_size_t widelen = 0;
apr_size_t acplen = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_UTF8, 0, src->data, src->len, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_UTF8, 0, src->data, src->len,
widebuf, widelen);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (acplen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = svn_string_ncreate("", acplen, pool);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen,
(*dest)->data, (*dest)->len, NULL, NULL);
free(widebuf);
if (acplen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
svn_error_t *
svn_utf_cstring_from_utf8(const char **dest,
const char *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(check_utf8(src, strlen(src), pool));
SVN_ERR(get_uton_xlate_handle_node(&node, pool));
err = convert_cstring(dest, src, node, pool);
put_xlate_handle_node(node, SVN_UTF_UTON_XLATE_HANDLE, pool);
return err;
#else
apr_size_t widelen = 0;
apr_size_t acplen = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_UTF8, 0, src, -1, widebuf, widelen);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (acplen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = apr_pstrmemdup(pool, "", acplen+1);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen,
*dest, acplen, NULL, NULL);
free(widebuf);
if (acplen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
svn_error_t *
svn_utf_cstring_from_utf8_ex2(const char **dest,
const char *src,
const char *topage,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
xlate_handle_node_t *node;
svn_error_t *err;
const char *convset_key = get_xlate_key(topage, SVN_APR_UTF8_CHARSET,
pool);
SVN_ERR(check_utf8(src, strlen(src), pool));
SVN_ERR(get_xlate_handle_node(&node, topage, SVN_APR_UTF8_CHARSET,
convset_key, pool));
err = convert_cstring(dest, src, node, pool);
put_xlate_handle_node(node, convset_key, pool);
return err;
#else
assert((topage == APR_DEFAULT_CHARSET) || (topage == APR_LOCALE_CHARSET));
return svn_utf_cstring_from_utf8(dest, src, pool);
#endif
}
svn_error_t *
svn_utf_cstring_from_utf8_ex(const char **dest,
const char *src,
const char *topage,
const char *convset_key,
apr_pool_t *pool)
{
return svn_utf_cstring_from_utf8_ex2(dest, src, topage, pool);
}
const char *
svn_utf__cstring_from_utf8_fuzzy(const char *src,
apr_pool_t *pool,
svn_error_t *(*convert_from_utf8)
(const char **, const char *, apr_pool_t *))
{
const char *escaped, *converted;
svn_error_t *err;
escaped = fuzzy_escape(src, strlen(src), pool);
/* Okay, now we have a *new* UTF-8 string, one that's guaranteed to
contain only 7-bit bytes :-). Recode to native... */
err = convert_from_utf8(((const char **) &converted), escaped, pool);
if (err)
{
svn_error_clear(err);
return escaped;
}
else
return converted;
/* ### Check the client locale, maybe we can avoid that second
* conversion! See Ulrich Drepper's patch at
* http://subversion.tigris.org/issues/show_bug.cgi?id=807.
*/
}
const char *
svn_utf_cstring_from_utf8_fuzzy(const char *src,
apr_pool_t *pool)
{
return svn_utf__cstring_from_utf8_fuzzy(src, pool,
svn_utf_cstring_from_utf8);
}
svn_error_t *
svn_utf_cstring_from_utf8_stringbuf(const char **dest,
const svn_stringbuf_t *src,
apr_pool_t *pool)
{
svn_stringbuf_t *destbuf;
SVN_ERR(svn_utf_stringbuf_from_utf8(&destbuf, src, pool));
*dest = destbuf->data;
return SVN_NO_ERROR;
}
svn_error_t *
svn_utf_cstring_from_utf8_string(const char **dest,
const svn_string_t *src,
apr_pool_t *pool)
{
#if APR_HAS_XLATE || !WIN32
svn_stringbuf_t *dbuf;
xlate_handle_node_t *node;
svn_error_t *err;
SVN_ERR(get_uton_xlate_handle_node(&node, pool));
if (node->handle)
{
err = check_utf8(src->data, src->len, pool);
if (! err)
err = convert_to_stringbuf(node, src->data, src->len,
&dbuf, pool);
if (! err)
*dest = dbuf->data;
}
else
{
err = check_non_ascii(src->data, src->len, pool);
if (! err)
*dest = apr_pstrmemdup(pool, src->data, src->len);
}
put_xlate_handle_node(node, SVN_UTF_UTON_XLATE_HANDLE, pool);
return err;
#else
apr_size_t widelen = 0;
apr_size_t acplen = 0;
wchar_t * widebuf = NULL;
widelen = MultiByteToWideChar(CP_UTF8, 0, src->data, src->len, NULL, 0);
if (widelen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
widebuf = calloc(widelen, sizeof(wchar_t));
if (widebuf == NULL)
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
widelen = MultiByteToWideChar(CP_UTF8, 0, src->data, src->len,
widebuf, widelen);
if (widelen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen, NULL, 0,
NULL, NULL);
if (acplen == 0)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
}
*dest = apr_pstrmemdup(pool, "", acplen+1);
if (*dest == NULL)
{
free(widebuf);
return svn_error_createf(APR_EINVAL, NULL,
_("Error allocating memory during utf8 conversion\n"));
}
acplen = WideCharToMultiByte(CP_ACP, 0, widebuf, widelen,
*dest, acplen, NULL, NULL);
free(widebuf);
if (acplen == 0)
return svn_error_createf(APR_EINVAL, NULL,
_("Error converting string to utf8\n"));
return SVN_NO_ERROR;
#endif
}
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.