target-alpha: Fix bug: palcode is at least 6 bits.
[qemu] / block-vvfat.c
index 6de76c6..01e9c04 100644 (file)
@@ -78,7 +78,7 @@ typedef struct array_t {
 
 static inline void array_init(array_t* array,unsigned int item_size)
 {
-    array->pointer=0;
+    array->pointer = NULL;
     array->size=0;
     array->next=0;
     array->item_size=item_size;
@@ -129,7 +129,7 @@ static inline void* array_insert(array_t* array,unsigned int index,unsigned int
        int increment=count*array->item_size;
        array->pointer=qemu_realloc(array->pointer,array->size+increment);
        if(!array->pointer)
-           return 0;
+            return NULL;
        array->size+=increment;
     }
     memmove(array->pointer+(index+count)*array->item_size,
@@ -159,7 +159,7 @@ static inline int array_roll(array_t* array,int index_to,int index_from,int coun
     is=array->item_size;
     from=array->pointer+index_from*is;
     to=array->pointer+index_to*is;
-    buf=malloc(is*count);
+    buf=qemu_malloc(is*count);
     memcpy(buf,from,is*count);
 
     if(index_to<index_from)
@@ -604,8 +604,8 @@ static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
        unsigned int directory_start, const char* filename, int is_dot)
 {
     int i,j,long_index=s->directory.next;
-    direntry_t* entry=0;
-    direntry_t* entry_long=0;
+    direntry_t* entry = NULL;
+    direntry_t* entry_long = NULL;
 
     if(is_dot) {
        entry=array_get_next(&(s->directory));
@@ -625,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++)
@@ -696,7 +696,7 @@ static int read_directory(BDRVVVFATState* s, int mapping_index)
     int first_cluster = mapping->begin;
     int parent_index = mapping->info.dir.parent_mapping_index;
     mapping_t* parent_mapping = (mapping_t*)
-       (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : 0);
+        (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
     int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
 
     DIR* dir=opendir(dirname);
@@ -725,8 +725,7 @@ static int read_directory(BDRVVVFATState* s, int mapping_index)
        if(first_cluster == 0 && (is_dotdot || is_dot))
            continue;
 
-       buffer=(char*)malloc(length);
-       assert(buffer);
+       buffer=(char*)qemu_malloc(length);
        snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
 
        if(stat(buffer,&st)<0) {
@@ -847,8 +846,7 @@ static int init_directories(BDRVVVFATState* s,
     memset(&(s->first_sectors[0]),0,0x40*0x200);
 
     s->cluster_size=s->sectors_per_cluster*0x200;
-    s->cluster_buffer=malloc(s->cluster_size);
-    assert(s->cluster_buffer);
+    s->cluster_buffer=qemu_malloc(s->cluster_size);
 
     /*
      * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
@@ -890,7 +888,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? */
@@ -923,20 +920,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);
@@ -1052,7 +1054,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
@@ -1123,10 +1125,10 @@ static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_
     int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
     mapping_t* mapping;
     if(index>=s->mapping.next)
-       return 0;
+        return NULL;
     mapping=array_get(&(s->mapping),index);
     if(mapping->begin>cluster_num)
-       return 0;
+        return NULL;
     assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
     return mapping;
 }
@@ -1245,7 +1247,7 @@ static void print_direntry(const direntry_t* direntry)
        unsigned char* c=(unsigned char*)direntry;
        int i;
        for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
-#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = '°'; j++;}
+#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
            ADD_CHAR(c[i]);
        for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
            ADD_CHAR(c[i]);
@@ -1481,7 +1483,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];
     }
@@ -1494,7 +1496,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];
        }
@@ -1724,7 +1726,7 @@ static int check_directory_consistency(BDRVVVFATState *s,
        int cluster_num, const char* path)
 {
     int ret = 0;
-    unsigned char* cluster = malloc(s->cluster_size);
+    unsigned char* cluster = qemu_malloc(s->cluster_size);
     direntry_t* direntries = (direntry_t*)cluster;
     mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
 
@@ -1733,7 +1735,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';
 
@@ -1807,7 +1809,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) {
@@ -1864,7 +1867,7 @@ DLOG(checkpoint());
      */
     if (s->fat2 == NULL) {
        int size = 0x200 * s->sectors_per_fat;
-       s->fat2 = malloc(size);
+       s->fat2 = qemu_malloc(size);
        memcpy(s->fat2, s->fat.pointer, size);
     }
     check = vvfat_read(s->bs,
@@ -2206,7 +2209,7 @@ static int commit_one_file(BDRVVVFATState* s,
     uint32_t first_cluster = c;
     mapping_t* mapping = find_mapping_for_cluster(s, c);
     uint32_t size = filesize_of_direntry(direntry);
-    char* cluster = malloc(s->cluster_size);
+    char* cluster = qemu_malloc(s->cluster_size);
     uint32_t i;
     int fd = 0;
 
@@ -2368,12 +2371,13 @@ static int handle_renames_and_mkdirs(BDRVVVFATState* s)
                            mapping_t* m = find_mapping_for_cluster(s,
                                    begin_of_direntry(d));
                            int l = strlen(m->path);
-                           char* new_path = malloc(l + diff + 1);
+                           char* new_path = qemu_malloc(l + diff + 1);
 
                            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);
                        }
@@ -2768,7 +2772,7 @@ static int enable_write_target(BDRVVVFATState *s)
 
     array_init(&(s->commits), sizeof(commit_t));
 
-    s->qcow_filename = malloc(1024);
+    s->qcow_filename = qemu_malloc(1024);
     get_tmp_filename(s->qcow_filename, 1024);
     if (bdrv_create(&bdrv_qcow,
                s->qcow_filename, s->sector_count, "fat:", 0) < 0)
@@ -2801,17 +2805,14 @@ static void vvfat_close(BlockDriverState *bs)
 }
 
 BlockDriver bdrv_vvfat = {
-    "vvfat",
-    sizeof(BDRVVVFATState),
-    NULL, /* no probe for protocols */
-    vvfat_open,
-    vvfat_read,
-    vvfat_write,
-    vvfat_close,
-    NULL, /* ??? Not sure if we can do any meaningful flushing.  */
-    NULL,
-    vvfat_is_allocated,
-    .protocol_name = "fat",
+    .format_name       = "vvfat",
+    .instance_size     = sizeof(BDRVVVFATState),
+    .bdrv_open         = vvfat_open,
+    .bdrv_read         = vvfat_read,
+    .bdrv_write                = vvfat_write,
+    .bdrv_close                = vvfat_close,
+    .bdrv_is_allocated = vvfat_is_allocated,
+    .protocol_name     = "fat",
 };
 
 #ifdef DEBUG
@@ -2842,4 +2843,3 @@ static void checkpoint(void) {
     print_direntry(NULL);
 }
 #endif
-