string.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /*
  16. * Locale-specific 1-byte character versions
  17. * See utf-8.c for UTF-8 versions
  18. */
  19. #include "portable.h"
  20. #include <ac/stdlib.h>
  21. #include <ac/string.h>
  22. #include <ac/time.h>
  23. #include <ac/ctype.h>
  24. #include "ldap-int.h"
  25. #if defined ( HAVE_STRSPN )
  26. #define int_strspn strspn
  27. #else
  28. static int int_strspn( const char *str, const char *delim )
  29. {
  30. int pos;
  31. const char *p=delim;
  32. for( pos=0; (*str) ; pos++,str++) {
  33. if (*str!=*p) {
  34. for( p=delim; (*p) ; p++ ) {
  35. if (*str==*p) {
  36. break;
  37. }
  38. }
  39. }
  40. if (*p=='\0') {
  41. return pos;
  42. }
  43. }
  44. return pos;
  45. }
  46. #endif
  47. #if defined( HAVE_STRPBRK )
  48. #define int_strpbrk strpbrk
  49. #else
  50. static char *(int_strpbrk)( const char *str, const char *accept )
  51. {
  52. const char *p;
  53. for( ; (*str) ; str++ ) {
  54. for( p=accept; (*p) ; p++) {
  55. if (*str==*p) {
  56. return str;
  57. }
  58. }
  59. }
  60. return NULL;
  61. }
  62. #endif
  63. char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
  64. {
  65. char *p;
  66. if (pos==NULL) {
  67. return NULL;
  68. }
  69. if (str==NULL) {
  70. if (*pos==NULL) {
  71. return NULL;
  72. }
  73. str=*pos;
  74. }
  75. /* skip any initial delimiters */
  76. str += int_strspn( str, delim );
  77. if (*str == '\0') {
  78. return NULL;
  79. }
  80. p = int_strpbrk( str, delim );
  81. if (p==NULL) {
  82. *pos = NULL;
  83. } else {
  84. *p ='\0';
  85. *pos = p+1;
  86. }
  87. return str;
  88. }
  89. char *
  90. ldap_pvt_str2upper( char *str )
  91. {
  92. char *s;
  93. /* to upper */
  94. if ( str ) {
  95. for ( s = str; *s; s++ ) {
  96. *s = TOUPPER( (unsigned char) *s );
  97. }
  98. }
  99. return( str );
  100. }
  101. struct berval *
  102. ldap_pvt_str2upperbv( char *str, struct berval *bv )
  103. {
  104. char *s = NULL;
  105. assert( bv != NULL );
  106. /* to upper */
  107. if ( str ) {
  108. for ( s = str; *s; s++ ) {
  109. *s = TOUPPER( (unsigned char) *s );
  110. }
  111. }
  112. bv->bv_val = str;
  113. bv->bv_len = (ber_len_t)(s - str);
  114. return( bv );
  115. }
  116. char *
  117. ldap_pvt_str2lower( char *str )
  118. {
  119. char *s;
  120. /* to lower */
  121. if ( str ) {
  122. for ( s = str; *s; s++ ) {
  123. *s = TOLOWER( (unsigned char) *s );
  124. }
  125. }
  126. return( str );
  127. }
  128. struct berval *
  129. ldap_pvt_str2lowerbv( char *str, struct berval *bv )
  130. {
  131. char *s = NULL;
  132. assert( bv != NULL );
  133. /* to lower */
  134. if ( str ) {
  135. for ( s = str; *s; s++ ) {
  136. *s = TOLOWER( (unsigned char) *s );
  137. }
  138. }
  139. bv->bv_val = str;
  140. bv->bv_len = (ber_len_t)(s - str);
  141. return( bv );
  142. }