return next_entry;
}
-/*
- * Write at most 'size' bytes from 'buffer' into the specified cluster.
- * Return 0 on success, -1 otherwise.
+/**
+ * set_cluster() - write data to cluster
+ *
+ * Write 'size' bytes from 'buffer' into the specified cluster.
+ *
+ * @mydata: data to be written
+ * @clustnum: cluster to be written to
+ * @buffer: data to be written
+ * @size: bytes to be written (but not more than the size of a cluster)
+ * Return: 0 on success, -1 otherwise
*/
static int
-set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
- unsigned long size)
+set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
{
- __u32 idx = 0;
- __u32 startsect;
+ u32 idx = 0;
+ u32 startsect;
int ret;
if (clustnum > 0)
if (size) {
ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
-
+ /* Do not leak content of stack */
+ memset(tmpbuf, 0, mydata->sect_size);
memcpy(tmpbuf, buffer, size);
ret = disk_write(startsect, 1, tmpbuf);
if (ret != 1) {
set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
loff_t maxsize, loff_t *gotsize)
{
- loff_t filesize;
unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
__u32 curclust = START(dentptr);
__u32 endclust = 0, newclust = 0;
- loff_t cur_pos, offset, actsize, wsize;
+ u64 cur_pos, filesize;
+ loff_t offset, actsize, wsize;
*gotsize = 0;
filesize = pos + maxsize;
curclust = endclust;
filesize -= cur_pos;
- assert(!(cur_pos % bytesperclust));
+ assert(!do_div(cur_pos, bytesperclust));
set_clusters:
/* allocate and write */
/* set remaining bytes */
actsize = filesize;
- if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
+ if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
debug("error: writing cluster\n");
return -1;
}
return 0;
getit:
- if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
+ if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
debug("error: writing cluster\n");
return -1;
}
return 0;
}
+/**
+ * normalize_longname() - check long file name and convert to lower case
+ *
+ * We assume here that the FAT file system is using an 8bit code page.
+ * Linux typically uses CP437, EDK2 assumes CP1250.
+ *
+ * @l_filename: preallocated buffer receiving the normalized name
+ * @filename: filename to normalize
+ * Return: 0 on success, -1 on failure
+ */
static int normalize_longname(char *l_filename, const char *filename)
{
- const char *p, legal[] = "!#$%&\'()-.@^`_{}~";
- char c;
- int name_len;
-
- /* Check that the filename is valid */
- for (p = filename; p < filename + strlen(filename); p++) {
- c = *p;
-
- if (('0' <= c) && (c <= '9'))
- continue;
- if (('A' <= c) && (c <= 'Z'))
- continue;
- if (('a' <= c) && (c <= 'z'))
- continue;
- if (strchr(legal, c))
- continue;
- /* extended code */
- if ((0x80 <= c) && (c <= 0xff))
- continue;
+ const char *p, illegal[] = "<>:\"/\\|?*";
+ if (strlen(filename) >= VFAT_MAXLEN_BYTES)
return -1;
- }
- /* Normalize it */
- name_len = strlen(filename);
- if (name_len >= VFAT_MAXLEN_BYTES)
- /* should return an error? */
- name_len = VFAT_MAXLEN_BYTES - 1;
+ for (p = filename; *p; ++p) {
+ if ((unsigned char)*p < 0x20)
+ return -1;
+ if (strchr(illegal, *p))
+ return -1;
+ }
- memcpy(l_filename, filename, name_len);
- l_filename[name_len] = 0; /* terminate the string */
- downcase(l_filename, INT_MAX);
+ strcpy(l_filename, filename);
+ downcase(l_filename, VFAT_MAXLEN_BYTES);
return 0;
}
char *filename_copy, *dirname, *basename;
filename_copy = strdup(filename);
+ if (!filename_copy) {
+ printf("Error: allocating memory\n");
+ ret = -ENOMEM;
+ goto exit;
+ }
split_filename(filename_copy, &dirname, &basename);
if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
itr = malloc_cache_aligned(sizeof(fat_itr));
if (!itr) {
printf("Error: allocating memory\n");
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto exit;
}
ret = fat_itr_root(itr, &fsdata);