utils.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ident "ldclt @(#)utils.c 1.4 01/01/11"
  2. /** BEGIN COPYRIGHT BLOCK
  3. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  4. * Copyright (C) 2006 Red Hat, Inc.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. /*
  14. FILE : utils.c
  15. AUTHOR : Jean-Luc SCHWING
  16. VERSION : 1.0
  17. DATE : 14 November 2000
  18. DESCRIPTION :
  19. This file contains the utilities functions that will be
  20. used as well by ldclt and by the genldif command, e.g.
  21. the random generator functions, etc...
  22. The main target/reason for creating this file is to be
  23. able to easely separate these functions from ldclt's
  24. framework and data structures, and thus to provide a
  25. kind of library suitable for any command.
  26. LOCAL : None.
  27. */
  28. #include "utils.h" /* Basic definitions for this file */
  29. #include <stdio.h> /* sprintf(), etc... */
  30. #include <stdlib.h> /* lrand48(), etc... */
  31. #include <ctype.h> /* isascii(), etc... */ /*JLS 16-11-00*/
  32. #include <string.h> /* strerror(), etc... */ /*JLS 14-11-00*/
  33. /*
  34. * Some global variables...
  35. */
  36. #define ldcltrand48() lrand48()
  37. /* ****************************************************************************
  38. FUNCTION : utilsInit
  39. PURPOSE : Initiates the utilities functions.
  40. INPUT : None.
  41. OUTPUT : None.
  42. RETURN : -1 if error, 0 else.
  43. DESCRIPTION :
  44. *****************************************************************************/
  45. int
  46. utilsInit (void)
  47. {
  48. /*
  49. * No error
  50. */
  51. return (0);
  52. }
  53. /* ****************************************************************************
  54. FUNCTION : rndlim
  55. PURPOSE : Returns a random number between the given limits.
  56. INPUT : low = low limit
  57. high = high limit
  58. OUTPUT : None.
  59. RETURN : The random value.
  60. DESCRIPTION :
  61. *****************************************************************************/
  62. int
  63. rndlim (
  64. int low,
  65. int high)
  66. {
  67. return (low + ldcltrand48() % (high-low+1));
  68. }
  69. /* ****************************************************************************
  70. FUNCTION : rnd
  71. PURPOSE : Creates a random number string between the given
  72. arguments. The string is returned in the buffer.
  73. INPUT : low = low limit
  74. high = high limit
  75. ndigits = number of digits - 0 means no zero pad
  76. OUTPUT : buf = buffer to write the random string. Note that
  77. it is generated with fixed number of digits,
  78. completed with leading '0', if ndigits > 0
  79. RETURN : None.
  80. DESCRIPTION :
  81. *****************************************************************************/
  82. void
  83. rnd (
  84. char *buf,
  85. int low,
  86. int high,
  87. int ndigits)
  88. {
  89. sprintf (buf, "%0*d", ndigits,
  90. (int)(low + (ldcltrand48() % (high-low+1)))); /*JLS 14-11-00*/
  91. }
  92. /* ****************************************************************************
  93. FUNCTION : rndstr
  94. PURPOSE : Return a random string, of length ndigits. The string
  95. is returned in the buf.
  96. INPUT : ndigits = number of digits required.
  97. OUTPUT : buf = the buf must be long enough to contain the
  98. requested string.
  99. RETURN : None.
  100. DESCRIPTION :
  101. *****************************************************************************/
  102. void
  103. rndstr (
  104. char *buf,
  105. int ndigits)
  106. {
  107. unsigned int rndNum; /* The random value */
  108. int charNum; /* Random char in buf */
  109. int byteNum; /* Byte in rndNum */
  110. char newChar; /* The new byte as a char */
  111. char *rndArray; /* To cast the rndNum into chars */
  112. charNum = 0;
  113. byteNum = 4;
  114. rndArray = (char *)(&rndNum);
  115. while (charNum < ndigits)
  116. {
  117. /*
  118. * Maybe we should generate a new random number ?
  119. */
  120. if (byteNum == 4)
  121. {
  122. rndNum = ldcltrand48(); /*JLS 14-11-00*/
  123. byteNum = 0;
  124. }
  125. /*
  126. * Is it a valid char ?
  127. */
  128. newChar = rndArray[byteNum];
  129. /*
  130. * The last char must not be a '\' nor a space.
  131. */
  132. if (!(((charNum+1) == ndigits) &&
  133. ((newChar == '\\') || (newChar == ' '))))
  134. {
  135. /*
  136. * First, there are some special characters that have a meaning for
  137. * LDAP, and thus that must be quoted. What LDAP's rfc1779 means by
  138. * "to quote" may be translated by "to antislash"
  139. * Note: we do not check the \ because in this way, it leads to randomly
  140. * quote some valid characters.
  141. */
  142. if ((newChar == '=') || (newChar == ';') || (newChar == ',') ||
  143. (newChar == '+') || (newChar == '"') || (newChar == '<') ||
  144. (newChar == '>') || (newChar == '#'))
  145. {
  146. /*
  147. * Ensure that the previous char is *not* a \ otherwise
  148. * it will result in a \\, rather than a \,
  149. * If it is the case, add one more \ to have \\\,
  150. */
  151. if ((charNum > 0) && (buf[charNum-1] == '\\'))
  152. {
  153. if ((charNum+3) < ndigits)
  154. {
  155. buf[charNum++] = '\\';
  156. buf[charNum++] = '\\';
  157. buf[charNum++] = newChar;
  158. }
  159. }
  160. else
  161. {
  162. if ((charNum+2) < ndigits)
  163. {
  164. buf[charNum++] = '\\';
  165. buf[charNum++] = newChar;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. /*
  172. * strict ascii required
  173. */
  174. if (isascii (newChar) && !iscntrl(newChar))
  175. buf[charNum++] = newChar;
  176. }
  177. }
  178. /*
  179. * Next byte of the random value.
  180. */
  181. byteNum++;
  182. }
  183. /*
  184. * End of function
  185. */
  186. buf[charNum] = '\0';
  187. }
  188. /* increment val - if val > max, wrap value around to start over at min */
  189. /* the range is max - min + 1 = number of possible values */
  190. /* the new value is (((val + incr) - min) % range) + min */
  191. int
  192. incr_and_wrap(int val, int min, int max, int incr)
  193. {
  194. int range = max - min + 1;
  195. return (((val + incr) - min) % range) + min;
  196. }