+static inline bool
+is_valid_windows_filename_char(utf16lechar c)
+{
+ return le16_to_cpu(c) > 31 &&
+ c != cpu_to_le16('/') &&
+ c != cpu_to_le16('<') &&
+ c != cpu_to_le16('>') &&
+ c != cpu_to_le16(':') &&
+ c != cpu_to_le16('"') &&
+ c != cpu_to_le16('/' ) &&
+ c != cpu_to_le16('\\') &&
+ c != cpu_to_le16('|') &&
+ c != cpu_to_le16('?') &&
+ c != cpu_to_le16('*');
+}
+
+/* Is the character valid in a filename on the current platform? */
+static inline bool
+is_valid_filename_char(utf16lechar c)
+{
+#ifdef __WIN32__
+ return is_valid_windows_filename_char(c);
+#else
+ return c != cpu_to_le16('\0') && c != cpu_to_le16('/');
+#endif
+}
+
+/* Generate a random filename and return its length. */
+static int
+generate_random_filename(utf16lechar name[], int max_len,
+ struct generation_context *ctx)
+{
+ int len;
+
+ /* Choose the length of the name. */
+ switch (rand32() % 8) {
+ default:
+ /* short name */
+ len = 1 + (rand32() % 6);
+ break;
+ case 2:
+ case 3:
+ case 4:
+ /* medium-length name */
+ len = 7 + (rand32() % 8);
+ break;
+ case 5:
+ case 6:
+ /* long name */
+ len = 15 + (rand32() % 15);
+ break;
+ case 7:
+ /* very long name */
+ len = 30 + (rand32() % 90);
+ break;
+ }
+ len = min(len, max_len);
+
+retry:
+ /* Generate the characters in the name. */
+ for (int i = 0; i < len; i++) {
+ do {
+ name[i] = rand16();
+ } while (!is_valid_filename_char(name[i]));
+ }
+
+ /* Add a null terminator. */
+ name[len] = cpu_to_le16('\0');
+
+ /* Don't generate . and .. */
+ if (name[0] == cpu_to_le16('.') &&
+ (len == 1 || (len == 2 && name[1] == cpu_to_le16('.'))))
+ goto retry;
+
+ return len;
+}
+
+/* The set of characters which are valid in short filenames. */
+static const char valid_short_name_chars[] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
+ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ '!', '#', '$', '%', '&', '\'', '(', ')', '-', '@', '^', '_', '`', '{',
+ '}', '~',
+ /* Note: Windows does not allow space and 128-255 in short filenames
+ * (tested on both NTFS and FAT). */
+};
+
+static int
+generate_short_name_component(utf16lechar p[], int len)
+{
+ for (int i = 0; i < len; i++) {
+ char c = valid_short_name_chars[rand32() %
+ ARRAY_LEN(valid_short_name_chars)];
+ p[i] = cpu_to_le16(c);
+ }
+ return len;
+}
+
+/* Generate a random short (8.3) filename and return its length.
+ * The @name array must have length >= 13 (8 + 1 + 3 + 1). */
+static int
+generate_random_short_name(utf16lechar name[], struct generation_context *ctx)
+{
+ /*
+ * Legal short names on Windows consist of 1 to 8 characters, optionally
+ * followed by a dot then 1 to 3 more characters. Only certain
+ * characters are allowed.
+ */
+ int base_len = 1 + (rand32() % 8);
+ int ext_len = rand32() % 4;
+ int total_len;
+
+ base_len = generate_short_name_component(name, base_len);
+
+ if (ext_len) {
+ name[base_len] = cpu_to_le16('.');
+ ext_len = generate_short_name_component(&name[base_len + 1],
+ ext_len);
+ total_len = base_len + 1 + ext_len;
+ } else {
+ total_len = base_len;
+ }
+ name[total_len] = cpu_to_le16('\0');
+ return total_len;
+}
+
+