int main () { /* Empty string. */ { static const uint16_t input[] = { 0 }; ASSERT (u16_strlen (input) == 0); } /* Simple string. */ { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a) 日本語,中文,한글" */ static const uint16_t input[] = { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ', 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443, 0x0439, 0x0442, 0x0435, '!', ' ', 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2, '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ', 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, 0 }; ASSERT (u16_strlen (input) == SIZEOF (input) - 1); } /* String with characters outside the BMP. */ { static const uint16_t input[] = { '-', '(', 0xD835, 0xDD1E, 0x00D7, 0xD835, 0xDD1F, ')', '=', 0xD835, 0xDD1F, 0x00D7, 0xD835, 0xDD1E, 0 }; ASSERT (u16_strlen (input) == SIZEOF (input) - 1); } return 0; }
static void test_function (uint16_t * (*my_asnprintf) (uint16_t *, size_t *, const char *, ...)) { /* Test the support of the 's' conversion specifier for strings. */ { const char *locale_string = "\304rger"; /* Ärger */ { size_t length; uint16_t *result = my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); static const uint16_t expected[] = { 0x00c4, 'r', 'g', 'e', 'r', ' ', '3', '3', 0 }; ASSERT (result != NULL); ASSERT (u16_strcmp (result, expected) == 0); ASSERT (length == u16_strlen (result)); free (result); } { /* Width. */ size_t length; uint16_t *result = my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55); static const uint16_t expected[] = { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', ' ', '3', '3', 0 }; ASSERT (result != NULL); ASSERT (u16_strcmp (result, expected) == 0); ASSERT (length == u16_strlen (result)); free (result); } { /* FLAG_LEFT. */ size_t length; uint16_t *result = my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55); static const uint16_t expected[] = { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; ASSERT (result != NULL); ASSERT (u16_strcmp (result, expected) == 0); ASSERT (length == u16_strlen (result)); free (result); } { /* FLAG_ZERO: no effect. */ size_t length; uint16_t *result = my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55); static const uint16_t expected[] = { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', ' ', '3', '3', 0 }; ASSERT (result != NULL); ASSERT (u16_strcmp (result, expected) == 0); ASSERT (length == u16_strlen (result)); free (result); } } }
static uint16_t * my_xasprintf (const char *format, ...) { va_list args; size_t length; uint16_t *ret; va_start (args, format); ret = u16_vasnprintf (NULL, &length, format, args); va_end (args); if (ret != NULL) ASSERT (length == u16_strlen (ret)); return ret; }
static uint16_t * my_xasprintf (const char *format, ...) { va_list args; uint16_t buf[1000]; int retval; size_t length; uint16_t *result; va_start (args, format); retval = u16_vsnprintf (buf, sizeof (buf), format, args); va_end (args); if (retval < 0 || retval >= (int) sizeof (buf)) return NULL; length = u16_strlen (buf); result = XNMALLOC (length + 1, uint16_t); u16_cpy (result, buf, length + 1); return result; }
/** * show_efi_boot_opt_data() - dump UEFI load option * * @id: Load option number * @data: Value of UEFI load option variable * * Decode the value of UEFI load option variable and print information. */ static void show_efi_boot_opt_data(int id, void *data) { struct efi_load_option lo; char *label, *p; size_t label_len16, label_len; u16 *dp_str; efi_deserialize_load_option(&lo, data); label_len16 = u16_strlen(lo.label); label_len = utf16_utf8_strnlen(lo.label, label_len16); label = malloc(label_len + 1); if (!label) return; p = label; utf16_utf8_strncpy(&p, lo.label, label_len16); printf("Boot%04X:\n", id); printf("\tattributes: %c%c%c (0x%08x)\n", /* ACTIVE */ lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', /* FORCE RECONNECT */ lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', /* HIDDEN */ lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', lo.attributes); printf("\tlabel: %s\n", label); dp_str = efi_dp_str(lo.file_path); printf("\tfile_path: %ls\n", dp_str); efi_free_pool(dp_str); printf("\tdata: %s\n", lo.optional_data); free(label); }
/** * file_open() - open a file handle * * @fs: file system * @parent: directory relative to which the file is to be opened * @file_name: path of the file to be opened. '\', '.', or '..' may * be used as modifiers. A leading backslash indicates an * absolute path. * @mode: bit mask indicating the access mode (read, write, * create) * @attributes: attributes for newly created file * Returns: handle to the opened file or NULL */ static struct efi_file_handle *file_open(struct file_system *fs, struct file_handle *parent, u16 *file_name, u64 mode, u64 attributes) { struct file_handle *fh; char f0[MAX_UTF8_PER_UTF16] = {0}; int plen = 0; int flen = 0; if (file_name) { utf16_to_utf8((u8 *)f0, file_name, 1); flen = u16_strlen(file_name); } /* we could have a parent, but also an absolute path: */ if (f0[0] == '\\') { plen = 0; } else if (parent) { plen = strlen(parent->path) + 1; } /* +2 is for null and '/' */ fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2); fh->base = efi_file_handle_protocol; fh->fs = fs; if (parent) { char *p = fh->path; if (plen > 0) { strcpy(p, parent->path); p += plen - 1; *p++ = '/'; } utf16_to_utf8((u8 *)p, file_name, flen); if (sanitize_path(fh->path)) goto error; /* check if file exists: */ if (set_blk_dev(fh)) goto error; if ((mode & EFI_FILE_MODE_CREATE) && (attributes & EFI_FILE_DIRECTORY)) { if (fs_mkdir(fh->path)) goto error; } else if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path))) goto error; /* fs_exists() calls fs_close(), so open file system again */ if (set_blk_dev(fh)) goto error; /* figure out if file is a directory: */ fh->isdir = is_dir(fh); } else { fh->isdir = 1; strcpy(fh->path, ""); } return &fh->base; error: free(fh); return NULL; }
int u16_strwidth (const uint16_t *s, const char *encoding) { return u16_width (s, u16_strlen (s), encoding); }
/** * show_efi_boot_order() - show order of UEFI load options * * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Show order of UEFI load options defined by BootOrder variable. */ static int show_efi_boot_order(void) { efi_guid_t guid; u16 *bootorder = NULL; efi_uintn_t size; int num, i; char var_name[9]; u16 var_name16[9], *p16; void *data; struct efi_load_option lo; char *label, *p; size_t label_len16, label_len; efi_status_t ret; guid = efi_global_variable_guid; size = 0; ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size, NULL)); if (ret == EFI_BUFFER_TOO_SMALL) { bootorder = malloc(size); ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size, bootorder)); } if (ret == EFI_NOT_FOUND) { printf("BootOrder not defined\n"); ret = CMD_RET_SUCCESS; goto out; } else if (ret != EFI_SUCCESS) { ret = CMD_RET_FAILURE; goto out; } num = size / sizeof(u16); for (i = 0; i < num; i++) { sprintf(var_name, "Boot%04X", bootorder[i]); p16 = var_name16; utf8_utf16_strncpy(&p16, var_name, 9); size = 0; ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL)); if (ret != EFI_BUFFER_TOO_SMALL) { printf("%2d: Boot%04X: (not defined)\n", i + 1, bootorder[i]); continue; } data = malloc(size); if (!data) { ret = CMD_RET_FAILURE; goto out; } ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, data)); if (ret != EFI_SUCCESS) { free(data); ret = CMD_RET_FAILURE; goto out; } efi_deserialize_load_option(&lo, data); label_len16 = u16_strlen(lo.label); label_len = utf16_utf8_strnlen(lo.label, label_len16); label = malloc(label_len + 1); if (!label) { free(data); ret = CMD_RET_FAILURE; goto out; } p = label; utf16_utf8_strncpy(&p, lo.label, label_len16); printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label); free(label); free(data); } out: free(bootorder); return ret; }