00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "ocilib_internal.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 int OCI_StringCopy4to2bytes
00054 (
00055 const unsigned int* src,
00056 size_t src_size,
00057 unsigned short * dst,
00058 size_t dst_size
00059 )
00060 {
00061 int cp_size = 0;
00062
00063 const unsigned int *src_end = NULL;
00064 const unsigned short *dst_end = NULL;
00065
00066 OCI_CHECK(src == NULL, 0);
00067 OCI_CHECK(dst == NULL, 0);
00068
00069 src_end = src + src_size;
00070 dst_end = dst + dst_size;
00071
00072 while (src < src_end)
00073 {
00074 unsigned int c;
00075
00076 if (dst >= dst_end) return -1;
00077
00078 c = *src++;
00079
00080 if (c <= UNI_MAX_BMP)
00081 {
00082 if ((c >= UNI_SUR_HIGH_START) && (c < UNI_SUR_LOW_END))
00083 *dst++ = UNI_REPLACEMENT_CHAR;
00084 else
00085 *dst++ = (unsigned short) c;
00086
00087 cp_size++;
00088 }
00089 else if (c > UNI_MAX_LEGAL_UTF32)
00090 {
00091 *dst++ = UNI_REPLACEMENT_CHAR;
00092
00093 cp_size++;
00094 }
00095 else
00096 {
00097 if ((dst + (size_t) 1) >= dst_end) return -2;
00098
00099 c -= UNI_BASE;
00100
00101 if (dst)
00102 {
00103 *dst++ = (unsigned short)((c >> UNI_SHIFT) + UNI_SUR_HIGH_START);
00104 *dst++ = (unsigned short)((c & UNI_MASK ) + UNI_SUR_LOW_START );
00105 }
00106
00107 cp_size++;
00108 cp_size++;
00109 }
00110 }
00111
00112 return cp_size;
00113 }
00114
00115
00116
00117
00118
00119 int OCI_StringCopy2to4bytes
00120 (
00121 const unsigned short* src,
00122 size_t src_size,
00123 unsigned int * dst,
00124 size_t dst_size
00125 )
00126 {
00127 int cp_size = 0;
00128
00129 const unsigned short *src_end = NULL;
00130 const unsigned int *dst_end = NULL;
00131
00132 unsigned int c1, c2;
00133
00134 OCI_CHECK(src == NULL, 0);
00135 OCI_CHECK(dst == NULL, 0);
00136
00137 src_end = src + src_size;
00138 dst_end = dst + dst_size;
00139
00140 while (src < src_end)
00141 {
00142 c1 = *src++;
00143
00144 if ((c1 >= UNI_SUR_HIGH_START) && (c1 <= UNI_SUR_HIGH_END))
00145 {
00146 if (src < src_end)
00147 {
00148 c2 = *src;
00149
00150 if ((c2 >= UNI_SUR_LOW_START) && (c2 <= UNI_SUR_LOW_END))
00151 {
00152 c1 = ((c1 - UNI_SUR_HIGH_START) << UNI_SHIFT) +
00153 (c2 - UNI_SUR_LOW_START ) + UNI_BASE;
00154
00155 ++src;
00156 }
00157 }
00158 else
00159 return -1;
00160 }
00161
00162 if (dst >= dst_end) return -2;
00163
00164 *dst++ = c1;
00165
00166 cp_size++;
00167 }
00168
00169 return cp_size;
00170 }
00171
00172
00173
00174
00175
00176 size_t OCI_StringLength
00177 (
00178 void *ptr,
00179 size_t size_elem
00180 )
00181 {
00182 int size = 0;
00183
00184 OCI_CHECK(ptr == NULL, 0);
00185
00186 if (size_elem == sizeof(char))
00187 {
00188 const char *s = (const char *) ptr;
00189 const char *e = (const char *) ptr;
00190
00191 while (*e++) ;
00192
00193 size = (int) (e - s - (size_t) 1);
00194 }
00195 else if (size_elem == sizeof(short))
00196 {
00197 const short *s = (const short *) ptr;
00198 const short *e = (const short *) ptr;
00199
00200 while (*e++) ;
00201
00202 size = (int) (e - s - (size_t) 1);
00203 }
00204 else if (size_elem == sizeof(int))
00205 {
00206 const int *s = (const int *) ptr;
00207 const int *e = (const int *) ptr;
00208
00209 while (*e++) ;
00210
00211 size = (int) (e - s - (size_t) 1);
00212 }
00213
00214 return size;
00215 }
00216
00217
00218
00219
00220
00221 int OCI_StringUTF8Length
00222 (
00223 const char *str
00224 )
00225 {
00226 int i = 0;
00227 int size = 0;
00228
00229 while (*str)
00230 {
00231 if ((*str & 0xc0) != 0x80)
00232 size++;
00233 i++;
00234 }
00235
00236 return size;
00237 }
00238
00239
00240
00241
00242
00243 void * OCI_GetInputString
00244 (
00245 void *src,
00246 int *size,
00247 size_t size_char_in,
00248 size_t size_char_out
00249 )
00250 {
00251 OCI_CHECK(src == NULL, NULL);
00252 OCI_CHECK(size == NULL, NULL);
00253
00254 if (size_char_in == size_char_out)
00255 {
00256
00257
00258 if (*size == -1)
00259 *size = (int) (OCI_StringLength(src, size_char_in) * size_char_in);
00260
00261 return src;
00262 }
00263 else
00264 {
00265
00266
00267 int char_count = 0;
00268 void *dest = NULL;
00269
00270 if (*size == -1)
00271 char_count = (int) OCI_StringLength(src, size_char_in);
00272 else
00273 char_count = *size / (int) size_char_in;
00274
00275 *size = 0;
00276
00277 dest = OCI_MemAlloc(OCI_IPC_STRING, size_char_out, char_count + 1, 0);
00278
00279 if (dest != NULL)
00280 {
00281 unsigned int null_char = 0;
00282
00283 if (memcmp(src, &null_char, (size_t) size_char_in) != 0)
00284 {
00285 if (size_char_in > size_char_out)
00286 {
00287 if ((size_char_in == sizeof(int )) &&
00288 (size_char_out == sizeof(short)))
00289 {
00290
00291
00292 char_count = OCI_StringCopy4to2bytes
00293 (
00294 (unsigned int *) src, char_count,
00295 (unsigned short *) dest, char_count
00296 );
00297 }
00298 else
00299 {
00300
00301
00302 char_count = (int) wcstombs(dest, src, (size_t) (char_count+1));
00303 }
00304 }
00305 else
00306 {
00307 if ((size_char_in == sizeof(short)) &&
00308 (size_char_out == sizeof(int )))
00309 {
00310
00311
00312 char_count = OCI_StringCopy2to4bytes
00313 (
00314 (unsigned short *) src, char_count,
00315 (unsigned int *) dest, char_count
00316 );
00317 }
00318 else
00319 {
00320
00321
00322 char_count = (int) mbstowcs(dest, src, (size_t) (char_count+1));
00323 }
00324 }
00325 }
00326
00327 *size = char_count * (int) size_char_out;
00328
00329 memset( (void*) (((char*) dest) + (size_t) (*size)), 0, size_char_out);
00330 }
00331
00332 return dest;
00333 }
00334 }
00335
00336
00337
00338
00339
00340 void OCI_GetOutputString
00341 (
00342 void *src,
00343 void *dest,
00344 int *size,
00345 size_t size_char_in,
00346 size_t size_char_out
00347 )
00348 {
00349 if ((src == NULL) || (dest == NULL) || (size == NULL))
00350 return;
00351
00352
00353
00354 if (size_char_in != size_char_out)
00355 {
00356 int char_count = 0;
00357
00358 if (*size == -1)
00359 char_count = (int) OCI_StringLength(src, size_char_in);
00360 else
00361 char_count = *size / (int) size_char_in;
00362
00363 if (size_char_in > size_char_out)
00364 {
00365 if ((size_char_in == sizeof(int )) &&
00366 (size_char_out == sizeof(short)))
00367 {
00368
00369
00370 char_count = OCI_StringCopy4to2bytes
00371 (
00372 (unsigned int *) src, char_count,
00373 (unsigned short *) dest, char_count
00374 );
00375 }
00376 else
00377 {
00378
00379
00380 char_count = (int) wcstombs(dest, src, (size_t) (char_count+1));
00381 }
00382 }
00383 else
00384 {
00385 if ((size_char_in == sizeof(short)) &&
00386 (size_char_out == sizeof(int )))
00387 {
00388
00389
00390 char_count = OCI_StringCopy2to4bytes
00391 (
00392 (unsigned short *) src, char_count,
00393 (unsigned int *) dest, char_count
00394 );
00395 }
00396 else
00397 {
00398
00399
00400 char_count = (int) mbstowcs(dest, src, (size_t) (char_count+1));
00401 }
00402 }
00403
00404 *size = char_count * (int) size_char_out;
00405 }
00406 }
00407
00408
00409
00410
00411
00412 void OCI_MoveString
00413 (
00414 void *src,
00415 void *dst,
00416 int char_count,
00417 size_t size_char_in,
00418 size_t size_char_out
00419 )
00420 {
00421 if ((src == NULL) || (dst == NULL))
00422 return;
00423
00424
00425
00426 if (size_char_out > size_char_in)
00427 {
00428
00429
00430 if ((size_char_in == sizeof(short)) &&
00431 (size_char_out == sizeof(int)))
00432 {
00433
00434
00435 unsigned short *str1 = (unsigned short *) src;
00436 unsigned int *str2 = (unsigned int *) dst;
00437
00438 if (*str1 == 0)
00439 return;
00440
00441 while (char_count--)
00442 str2[char_count] = (unsigned int) str1[char_count];
00443 }
00444
00445 else if ((size_char_in == sizeof(char)) &&
00446 (size_char_out == sizeof(short)))
00447 {
00448
00449
00450 unsigned char *str1 = (unsigned char *) src;
00451 unsigned short *str2 = (unsigned short *) dst;
00452
00453 if (*str1 == 0)
00454 return;
00455
00456 while (char_count--)
00457 str2[char_count] = (unsigned short) str1[char_count];
00458 }
00459 else if ((size_char_in == sizeof(char)) &&
00460 (size_char_out == sizeof(int)))
00461 {
00462
00463
00464 unsigned char *str1 = (unsigned char *) src;
00465 unsigned int *str2 = (unsigned int *) dst;
00466
00467 if (*str1 == 0)
00468 return;
00469
00470 while (char_count--)
00471 str2[char_count] = (unsigned int) str1[char_count];
00472 }
00473 }
00474 else if (size_char_out < size_char_in)
00475 {
00476
00477
00478 if ((size_char_in == sizeof(int)) &&
00479 (size_char_out == sizeof(short)))
00480 {
00481
00482
00483 unsigned int *str1 = (unsigned int *) src;
00484 unsigned short *str2 = (unsigned short *) dst;
00485 int i = 0;
00486
00487 if (*str1 == 0)
00488 return;
00489
00490 while (++i < char_count)
00491 str2[i] = (unsigned short) str1[i];
00492 }
00493 else if ((size_char_in == sizeof(short)) &&
00494 (size_char_out == sizeof(char)))
00495 {
00496
00497
00498 unsigned short *str1 = (unsigned short *) src;
00499 unsigned char *str2 = (unsigned char *) dst;
00500 int i = 0;
00501
00502 if (*str1 == 0)
00503 return;
00504
00505 while (++i < char_count)
00506 str2[i] = (unsigned char) str1[i];
00507 }
00508 else if ((size_char_in == sizeof(int)) &&
00509 (size_char_out == sizeof(char)))
00510 {
00511
00512
00513 unsigned int *str1 = (unsigned int *) src;
00514 unsigned char *str2 = (unsigned char *) dst;
00515 int i = 0;
00516
00517 if (*str1 == 0)
00518 return;
00519
00520 while (++i < char_count)
00521 str2[i] = (unsigned char) str1[i];
00522 }
00523 }
00524 }
00525
00526
00527
00528
00529
00530 void OCI_ConvertString
00531 (
00532 void *str,
00533 int char_count,
00534 size_t size_char_in,
00535 size_t size_char_out
00536 )
00537 {
00538
00539
00540 OCI_MoveString(str, str, char_count, size_char_in, size_char_out);
00541 }
00542
00543
00544
00545
00546
00547 void OCI_CopyString
00548 (
00549 void *src,
00550 void *dest,
00551 int *size,
00552 size_t size_char_in,
00553 size_t size_char_out
00554 )
00555 {
00556 if ((src == NULL) || (dest == NULL) || (size == NULL))
00557 return;
00558
00559 if (size_char_out == size_char_in)
00560 {
00561 memcpy(dest, src, (size_t) *size);
00562 memset((void*) (((char*) dest) + (size_t) (*size)), 0, (size_t) size_char_out);
00563 }
00564 else
00565 OCI_GetOutputString(src, dest, size, size_char_in, size_char_out);
00566 }
00567
00568
00569
00570
00571
00572 void OCI_ReleaseMetaString
00573 (
00574 void *str
00575 )
00576 {
00577 if (str == NULL)
00578 return;
00579
00580 #ifdef OCI_CHECK_METASTRINGS
00581
00582 OCI_MemFree(str);
00583
00584 #endif
00585 }
00586
00587
00588
00589
00590
00591 void OCI_ReleaseDataString
00592 (
00593 void *str
00594 )
00595 {
00596 if (str == NULL)
00597 return;
00598
00599 #ifdef OCI_CHECK_DATASTRINGS
00600
00601 OCI_MemFree(str);
00602
00603 #endif
00604 }
00605
00606
00607
00608
00609
00610 void * OCI_StringFromStringPtr
00611 (
00612 OCIString *str,
00613 void **buf,
00614 int *buflen
00615 )
00616 {
00617 void *tmp = NULL;
00618 void *ret = NULL;
00619
00620 int olen = 0;
00621 int osize = 0;
00622 int esize = 0;
00623 int msize = 0;
00624
00625 OCI_CHECK(buf == NULL, NULL);
00626 OCI_CHECK(buflen == NULL, NULL);
00627
00628 tmp = OCIStringPtr(OCILib.env, str);
00629
00630 if (tmp != NULL)
00631 {
00632
00633 #if defined(OCI_CHARSET_MIXED)
00634
00635
00636
00637 esize = 1;
00638 msize = (int) sizeof(dtext);
00639 olen = (int) strlen((char* ) tmp);
00640 osize = olen * esize;
00641
00642 #elif defined(OCI_CHECK_DATASTRINGS)
00643
00644
00645
00646 esize = (int) sizeof(odtext);
00647 msize = (int) sizeof(dtext);
00648 olen = (int) OCI_StringLength(tmp, sizeof(odtext));
00649 osize = olen * esize;
00650
00651 #else
00652
00653 OCI_NOT_USED(esize);
00654
00655 #endif
00656
00657
00658
00659 if (olen > 0)
00660 {
00661
00662
00663 if ((*buf) == NULL)
00664 {
00665 *buflen = (olen+1) * msize;
00666 *buf = OCI_MemAlloc(OCI_IPC_STRING, (size_t) msize,
00667 (size_t) (olen+1), FALSE);
00668 }
00669 else if ((*buflen) < ((olen+1) * msize))
00670 {
00671 *buflen = (olen+1) * msize;
00672 *buf = OCI_MemRealloc(*buf, OCI_IPC_STRING, (size_t) msize,
00673 (size_t) (olen+1));
00674 }
00675 }
00676
00677 #if defined(OCI_CHARSET_MIXED)
00678
00679 mbstowcs(*buf, tmp, olen + OCI_CVT_CHAR);
00680
00681 memset( (void*) (((char*) *buf) + (olen*msize)), 0, msize);
00682
00683 ret = *buf;
00684
00685 #elif defined(OCI_CHECK_DATASTRINGS)
00686
00687 OCI_GetOutputDataString(tmp, *buf, &osize);
00688
00689 memset( (void*) (((char*) *buf) + (osize)), 0, msize);
00690
00691 ret = *buf;
00692
00693 #else
00694
00695 osize = 0;
00696 ret = tmp;
00697
00698 #endif
00699
00700 }
00701 else
00702 {
00703 ret = tmp;
00704 }
00705
00706 return ret;
00707 }
00708
00709
00710
00711
00712
00713 boolean OCI_StringToStringPtr
00714 (
00715 OCIString **str,
00716 OCIError *err,
00717 void *value,
00718 void **buf,
00719 int *buflen
00720 )
00721 {
00722 boolean res = TRUE;
00723 void *ostr = NULL;
00724 int osize = 0;
00725
00726 #ifdef OCI_CHARSET_MIXED
00727
00728 int olen = 0;
00729 int esize = 0;
00730
00731 #endif
00732
00733 OCI_CHECK(str == NULL, FALSE);
00734 OCI_CHECK(buf == NULL, FALSE);
00735 OCI_CHECK(buflen == NULL, FALSE);
00736
00737 #ifdef OCI_CHARSET_MIXED
00738
00739
00740
00741 esize = (int) 1;
00742 olen = (int) dtslen((dtext*) value);
00743 osize = olen;
00744
00745
00746
00747 if (olen > 0)
00748 {
00749
00750
00751 if ((*buf) == NULL)
00752 {
00753 *buflen = (olen+1) * esize;
00754 *buf = OCI_MemAlloc(OCI_IPC_STRING, esize, olen+1, FALSE);
00755 }
00756 else if ((*buflen) < ((olen+1) * esize))
00757 {
00758 *buflen = (olen+1) * esize;
00759 *buf = OCI_MemRealloc(*buf, OCI_IPC_STRING, esize, olen+1);
00760 }
00761
00762 }
00763
00764 wcstombs((char *) *buf, (dtext *) value, olen + OCI_CVT_CHAR);
00765
00766 ostr = *buf;
00767
00768 #else
00769
00770 osize = -1;
00771 ostr = OCI_GetInputDataString(value, &osize);
00772
00773 #endif
00774
00775 OCI_CALL3
00776 (
00777 res, err,
00778
00779 OCIStringAssignText(OCILib.env, err, (oratext *) ostr, (ub4) osize, str)
00780 )
00781
00782 #ifndef OCI_CHARSET_MIXED
00783
00784 OCI_ReleaseDataString(ostr);
00785
00786 #endif
00787 return res;
00788 }
00789
00790
00791
00792
00793
00794 boolean OCI_StringGetFromAttrHandle
00795 (
00796 OCI_Connection *con,
00797 void *handle,
00798 unsigned int type,
00799 unsigned int attr,
00800 mtext **str
00801 )
00802 {
00803 boolean res = TRUE;
00804 void *ostr = NULL;
00805 int osize = -1;
00806
00807 OCI_CHECK(str == NULL, FALSE);
00808
00809 OCI_CALL2
00810 (
00811 res, con,
00812
00813 OCIAttrGet((dvoid *) handle,
00814 (ub4 ) type,
00815 (dvoid *) &ostr,
00816 (ub4 *) &osize,
00817 (ub4 ) attr,
00818 con->err)
00819 )
00820
00821 if ((res == TRUE) && (ostr != NULL))
00822 {
00823 *str = (void *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
00824 (size_t) ((osize / (int) sizeof(omtext)) + 1),
00825 TRUE);
00826
00827 if (*str != NULL)
00828 {
00829 OCI_CopyString(ostr, *str, &osize, sizeof(omtext), sizeof(mtext));
00830 }
00831 else
00832 res = FALSE;
00833 }
00834
00835 return res;
00836 }
00837
00838
00839
00840
00841
00842 boolean OCI_StringSetToAttrHandle
00843 (
00844 OCI_Connection *con,
00845 void *handle,
00846 unsigned int type,
00847 unsigned int attr,
00848 mtext **str,
00849 const mtext *value
00850 )
00851 {
00852 boolean res = TRUE;
00853 void *ostr = NULL;
00854 int osize = -1;
00855
00856 ostr = OCI_GetInputMetaString(value, &osize);
00857
00858 if (osize == -1)
00859 osize = 0;
00860
00861 OCI_CALL2
00862 (
00863 res, con,
00864
00865 OCIAttrSet((dvoid *) handle,
00866 (ub4 ) type,
00867 (dvoid *) ostr,
00868 (ub4 ) osize,
00869 (ub4 ) attr,
00870 con->err)
00871 )
00872
00873 OCI_ReleaseMetaString(ostr);
00874
00875 if ((res == TRUE) && (str != NULL))
00876 {
00877 OCI_FREE(*str);
00878
00879 if (value != NULL)
00880 {
00881 *str = mtsdup(value);
00882 }
00883 }
00884
00885 return res;
00886 }
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896 char * ocistrdup
00897 (
00898 const char * src
00899 )
00900 {
00901 char *dst;
00902
00903 OCI_CHECK(src == NULL, NULL)
00904
00905 dst = (char *) OCI_MemAlloc(OCI_IPC_STRING, 1, strlen(src) + 1, 0);
00906
00907 if (dst != NULL)
00908 strcpy(dst, src);
00909
00910 return dst;
00911 }
00912
00913
00914
00915
00916
00917 int ocistrcasecmp
00918 (
00919 const char *str1,
00920 const char *str2
00921 )
00922 {
00923 if (str1 == NULL && str2 == NULL)
00924 return 0;
00925
00926 if (str1 == NULL)
00927 return 1;
00928
00929 if (str2 == NULL)
00930 return -1;
00931
00932 while (((*str1) != 0) &&
00933 ((*str2) != 0) &&
00934 (tolower((int)(*str1)) == tolower((int)(*str2))))
00935 {
00936 str1++;
00937 str2++;
00938 }
00939
00940 return (tolower((int) (*str1)) - tolower((int) (*str2)));
00941 }
00942
00943
00944
00945
00946
00947 int ocisprintf
00948 (
00949 char *str,
00950 int size,
00951 const char *format,
00952 ...
00953 )
00954 {
00955 va_list args;
00956 int n;
00957
00958 va_start(args, format);
00959
00960 n = (int) vsnprintf(str, (size_t) size, format, args);
00961
00962 va_end(args);
00963
00964 return n;
00965 }
00966
00967 #ifdef OCI_INCLUDE_WCHAR
00968
00969
00970
00971
00972
00973 wchar_t * ociwcsdup
00974 (
00975 const wchar_t * src
00976 )
00977 {
00978 wchar_t *dst;
00979
00980 OCI_CHECK(src == NULL, NULL)
00981
00982 dst = (wchar_t *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(wchar_t), wcslen(src) + 1, 0);
00983
00984 if (dst != NULL)
00985 wcscpy(dst, src);
00986
00987 return dst;
00988 }
00989
00990
00991
00992
00993
00994 int ociwcscasecmp
00995 (
00996 const wchar_t *str1,
00997 const wchar_t *str2
00998 )
00999 {
01000 if (str1 == NULL && str2 == NULL)
01001 return 0;
01002
01003 if (str1 == NULL)
01004 return 1;
01005
01006 if (str2 == NULL)
01007 return -1;
01008
01009 while ((*str1 != 0) && (*str2 != 0) &&
01010 (towlower((wint_t)*str1) == towlower((wint_t)*str2)))
01011 {
01012 str1++;
01013 str2++;
01014 }
01015
01016 return (towlower((wint_t) *str1) - towlower((wint_t) *str2));
01017 }
01018
01019 #endif