util: fixed generate_unique_strs() to be portable
[ira/wip.git] / lib / util / genrand.c
index cd1823a9a07bcacc2063713b22c3d772398ddca6..6002c0647eac4d4bba462cd7e53d1f2913a5abbe 100644 (file)
@@ -294,6 +294,7 @@ _PUBLIC_ uint32_t generate_random(void)
 _PUBLIC_ bool check_password_quality(const char *s)
 {
        int has_digit=0, has_capital=0, has_lower=0, has_special=0, has_high=0;
+       const char* reals = s;
        while (*s) {
                if (isdigit((unsigned char)*s)) {
                        has_digit |= 1;
@@ -310,7 +311,7 @@ _PUBLIC_ bool check_password_quality(const char *s)
        }
 
        return ((has_digit + has_lower + has_capital + has_special) >= 3
-               || (has_high > strlen(s)/2));
+               || (has_high > strlen(reals)/2));
 }
 
 /**
@@ -359,3 +360,44 @@ again:
 
        return retstr;
 }
+
+/**
+ * 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 unsigned c_size = 42;
+       int i, j;
+       unsigned rem;
+       long long place;
+       char ** strs = NULL;
+
+       if (num == 0 || len == 0)
+               return NULL;
+
+       strs = talloc_array(mem_ctx, char *, num);
+       if (strs == NULL) return NULL;
+
+       for (i = 0; i < num; i++) {
+               char *retstr = (char *)talloc_size(strs, len + 1);
+               if (retstr == NULL) {
+                       talloc_free(strs);
+                       return NULL;
+               }
+               rem = i;
+               for (j = 0; j < len; j++) {
+                       retstr[j] = c_list[rem % c_size];
+                       rem = rem / c_size;
+               }
+               retstr[j] = 0;
+               strs[i] = retstr;
+       }
+
+       return strs;
+}