lib/util: add unique string generator helper function
authorSteven Danneman <steven.danneman@isilon.com>
Tue, 8 Sep 2009 19:09:39 +0000 (12:09 -0700)
committerSteven Danneman <steven.danneman@isilon.com>
Tue, 8 Sep 2009 20:43:06 +0000 (13:43 -0700)
lib/util/genrand.c
lib/util/util.h

index 5b8456547ac9fc5ca1f8615a69297be96240f9c7..1519931222a25f57821853abeafeca2af6d7f89f 100644 (file)
@@ -360,3 +360,62 @@ again:
 
        return retstr;
 }
 
        return retstr;
 }
+
+/**
+ * Define our own pow() function to avoid linking in libm
+ */
+static double s_pow(double x, double y)
+{
+       int i;
+       double ret = x;
+
+       if (y == 0)
+               return 1;
+
+       for (i = 1; i < y; i++)
+               ret *= x;
+
+       return ret;
+}
+
+
+/**
+ * Generate an array of unique text strings all of the same length.
+ * The returned string will be allocated.
+ * Returns NULL if the number of unique combinations cannot be created.
+ *
+ * Characters used are: abcdefghijklmnopqrstuvwxyz0123456789+_-#.,
+ */
+_PUBLIC_ char** generate_unique_strs(TALLOC_CTX *mem_ctx, size_t len,
+                                    uint32_t num)
+{
+       const char *c_list = "abcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
+       const int c_size = 42;
+       int i, j, rem;
+       long long place;
+       char ** strs = NULL;
+
+       if (num == 0 || len == 0)
+               return NULL;
+
+       /* We'll never return more than UINT32_MAX strings. Since 42^6 is more
+        * than UINT32_MAX, we only have to check if we've been asked to return
+        * more than the total number of permutations for lengths less than 6.*/
+       if ((len < 6) && (num > s_pow(c_size, len)))
+               return NULL;
+
+       strs = talloc_array(mem_ctx, char *, num);
+
+       for (i = 0; i < num; i++) {
+               char *retstr = talloc_zero_size(mem_ctx, len + 1);
+               rem = i;
+               for (j = len - 1; j >= 0; j--) {
+                       place = s_pow(c_size, j);
+                       retstr[j] = c_list[rem / place];
+                       rem = rem % place;
+               }
+               strs[i] = retstr;
+       }
+
+       return strs;
+}
index 20050d2f0a6929c19823420cdcb0c3d9399accca..aa9f91ef968f93ec7bc4e9e9982791669f4653c5 100644 (file)
@@ -192,6 +192,16 @@ _PUBLIC_ char *generate_random_str_list(TALLOC_CTX *mem_ctx, size_t len, const c
  */
 _PUBLIC_ char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len);
 
  */
 _PUBLIC_ char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len);
 
+/**
+ * Generate an array of unique text strings all of the same length.
+ * The returned strings will be allocated.
+ * Returns NULL if the number of unique combinations cannot be created.
+ *
+ * Characters used are: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,
+ */
+_PUBLIC_ char** generate_unique_strs(TALLOC_CTX *mem_ctx, size_t len,
+                                         uint32_t num);
+
 /* The following definitions come from lib/util/dprintf.c  */
 #if _SAMBA_BUILD_ == 4
 
 /* The following definitions come from lib/util/dprintf.c  */
 #if _SAMBA_BUILD_ == 4