Remove unnecessary trailing newlines
[qemu] / block-vvfat.c
index 873956d..57763b3 100644 (file)
@@ -53,7 +53,7 @@
 #define stderr STDERR
 FILE* stderr = NULL;
 
-static void checkpoint();
+static void checkpoint(void);
 
 #ifdef __MINGW32__
 void nonono(const char* file, int line, const char* msg) {
@@ -93,7 +93,6 @@ static inline void array_free(array_t* array)
 
 /* does not automatically grow */
 static inline void* array_get(array_t* array,unsigned int index) {
-    assert(index >= 0);
     assert(index < array->next);
     return array->pointer + index * array->item_size;
 }
@@ -102,7 +101,7 @@ static inline int array_ensure_allocated(array_t* array, int index)
 {
     if((index + 1) * array->item_size > array->size) {
        int new_size = (index + 32) * array->item_size;
-       array->pointer = realloc(array->pointer, new_size);
+       array->pointer = qemu_realloc(array->pointer, new_size);
        if (!array->pointer)
            return -1;
        array->size = new_size;
@@ -128,7 +127,7 @@ static inline void* array_get_next(array_t* array) {
 static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
     if((array->next+count)*array->item_size>array->size) {
        int increment=count*array->item_size;
-       array->pointer=realloc(array->pointer,array->size+increment);
+       array->pointer=qemu_realloc(array->pointer,array->size+increment);
        if(!array->pointer)
            return 0;
        array->size+=increment;
@@ -195,7 +194,6 @@ static int array_remove(array_t* array,int index)
 static int array_index(array_t* array, void* pointer)
 {
     size_t offset = (char*)pointer - array->pointer;
-    assert(offset >= 0);
     assert((offset % array->item_size) == 0);
     assert(offset/array->item_size < array->next);
     return offset/array->item_size;
@@ -454,8 +452,7 @@ static inline direntry_t* create_long_filename(BDRVVVFATState* s,const char* fil
 
 static char is_free(const direntry_t* direntry)
 {
-    /* return direntry->name[0]==0 ; */
-    return direntry->attributes == 0 || direntry->name[0]==0xe5;
+    return direntry->name[0]==0xe5 || direntry->name[0]==0x00;
 }
 
 static char is_volume_label(const direntry_t* direntry)
@@ -628,7 +625,7 @@ static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
 
     entry=array_get_next(&(s->directory));
     memset(entry->name,0x20,11);
-    strncpy((char*)entry->name,filename,i);
+    memcpy(entry->name, filename, i);
 
     if(j > 0)
        for (i = 0; i < 3 && filename[j+1+i]; i++)
@@ -893,7 +890,6 @@ static int init_directories(BDRVVVFATState* s,
     s->path = mapping->path;
 
     for (i = 0, cluster = 0; i < s->mapping.next; i++) {
-       int j;
        /* MS-DOS expects the FAT to be 0 for the root directory
         * (except for the media byte). */
        /* LATER TODO: still true for FAT32? */
@@ -926,20 +922,25 @@ static int init_directories(BDRVVVFATState* s,
 
        assert(mapping->begin < mapping->end);
 
+       /* next free cluster */
+       cluster = mapping->end;
+
+       if(cluster > s->cluster_count) {
+           fprintf(stderr,"Directory does not fit in FAT%d (capacity %s)\n",
+                   s->fat_type,
+                   s->fat_type == 12 ? s->sector_count == 2880 ? "1.44 MB"
+                                                               : "2.88 MB"
+                                     : "504MB");
+           return -EINVAL;
+       }
+
        /* fix fat for entry */
        if (fix_fat) {
+           int j;
            for(j = mapping->begin; j < mapping->end - 1; j++)
                fat_set(s, j, j+1);
            fat_set(s, mapping->end - 1, s->max_fat_value);
        }
-
-       /* next free cluster */
-       cluster = mapping->end;
-
-       if(cluster > s->cluster_count) {
-           fprintf(stderr,"Directory does not fit in FAT%d\n",s->fat_type);
-           return -1;
-       }
     }
 
     mapping = array_get(&(s->mapping), 0);
@@ -1055,7 +1056,7 @@ DLOG(if (stderr == NULL) {
 
     i = strrchr(dirname, ':') - dirname;
     assert(i >= 3);
-    if (dirname[i-2] == ':' && isalpha(dirname[i-1]))
+    if (dirname[i-2] == ':' && qemu_isalpha(dirname[i-1]))
        /* workaround for DOS drive names */
        dirname += i-1;
     else
@@ -1411,7 +1412,12 @@ static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
 }
 
 typedef struct {
-    unsigned char name[1024];
+    /*
+     * Since the sequence number is at most 0x3f, and the filename
+     * length is at most 13 times the sequence number, the maximal
+     * filename length is 0x3f * 13 bytes.
+     */
+    unsigned char name[0x3f * 13 + 1];
     int checksum, len;
     int sequence_number;
 } long_file_name;
@@ -1436,6 +1442,7 @@ static int parse_long_name(long_file_name* lfn,
        lfn->sequence_number = pointer[0] & 0x3f;
        lfn->checksum = pointer[13];
        lfn->name[0] = 0;
+       lfn->name[lfn->sequence_number * 13] = 0;
     } else if ((pointer[0] & 0x3f) != --lfn->sequence_number)
        return -1;
     else if (pointer[13] != lfn->checksum)
@@ -1478,7 +1485,7 @@ static int parse_short_name(BDRVVVFATState* s,
        if (direntry->name[i] <= ' ' || direntry->name[i] > 0x7f)
            return -1;
        else if (s->downcase_short_names)
-           lfn->name[i] = tolower(direntry->name[i]);
+           lfn->name[i] = qemu_tolower(direntry->name[i]);
        else
            lfn->name[i] = direntry->name[i];
     }
@@ -1491,7 +1498,7 @@ static int parse_short_name(BDRVVVFATState* s,
            if (direntry->extension[j] <= ' ' || direntry->extension[j] > 0x7f)
                return -2;
            else if (s->downcase_short_names)
-               lfn->name[i + j] = tolower(direntry->extension[j]);
+               lfn->name[i + j] = qemu_tolower(direntry->extension[j]);
            else
                lfn->name[i + j] = direntry->extension[j];
        }
@@ -1730,7 +1737,7 @@ static int check_directory_consistency(BDRVVVFATState *s,
     char path2[PATH_MAX];
 
     assert(path_len < PATH_MAX); /* len was tested before! */
-    strcpy(path2, path);
+    pstrcpy(path2, sizeof(path2), path);
     path2[path_len] = '/';
     path2[path_len + 1] = '\0';
 
@@ -1804,7 +1811,8 @@ DLOG(fprintf(stderr, "check direntry %d: \n", i); print_direntry(direntries + i)
                fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
                goto fail;
            }
-           strcpy(path2 + path_len + 1, (char*)lfn.name);
+            pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
+                    (char*)lfn.name);
 
            if (is_directory(direntries + i)) {
                if (begin_of_direntry(direntries + i) == 0) {
@@ -2233,7 +2241,6 @@ static int commit_one_file(BDRVVVFATState* s,
 
        assert((size - offset == 0 && fat_eof(s, c)) ||
                (size > offset && c >=2 && !fat_eof(s, c)));
-       assert(size >= 0);
 
        ret = vvfat_read(s->bs, cluster2sector(s, c),
            (uint8_t*)cluster, (rest_size + 0x1ff) / 0x200);
@@ -2370,8 +2377,9 @@ static int handle_renames_and_mkdirs(BDRVVVFATState* s)
 
                            assert(!strncmp(m->path, mapping->path, l2));
 
-                           strcpy(new_path, mapping->path);
-                           strcpy(new_path + l1, m->path + l2);
+                            pstrcpy(new_path, l + diff + 1, mapping->path);
+                            pstrcpy(new_path + l1, l + diff + 1 - l1,
+                                    m->path + l2);
 
                            schedule_rename(s, m->begin, new_path);
                        }
@@ -2813,7 +2821,7 @@ BlockDriver bdrv_vvfat = {
 };
 
 #ifdef DEBUG
-static void checkpoint() {
+static void checkpoint(void) {
     assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
     check1(vvv);
     check2(vvv);
@@ -2840,4 +2848,3 @@ static void checkpoint() {
     print_direntry(NULL);
 }
 #endif
-