Index: firmware/drivers/fat.c =================================================================== --- firmware/drivers/fat.c (revision 13767) +++ firmware/drivers/fat.c (working copy) @@ -1172,13 +1172,47 @@ return 0; } - +static bool is_char_legal(char c) +{ + switch(c) + { + case 0x22: /* " */ + case 0x2a: /* * */ + case 0x2b: /* + */ + case 0x2c: /* , */ + case 0x2e: /* . */ + case 0x3a: /* : */ + case 0x3b: /* ; */ + case 0x3c: /* < */ + case 0x3d: /* = */ + case 0x3e: /* > */ + case 0x3f: /* ? */ + case 0x5b: /* [ */ + case 0x5c: /* \ */ + case 0x5d: /* ] */ + case 0x7c: /* | */ + return false; + default: + if(c < 0x20) /* space */ + return false; + } + return true; +} static int fat_checkname(const unsigned char* newname) { + int len = strlen(newname); /* More sanity checks are probably needed */ - if ( newname[strlen(newname) - 1] == '.' ) { + if (len > 255 || newname[len - 1] == '.' || + newname[0] == ' ' || newname[len - 1] == ' ' ) + { return -1; } + while (*newname) + { + if (!is_char_legal(*newname)) + return -1; + newname++; + } return 0; } @@ -1346,35 +1380,19 @@ static unsigned char char2dos(unsigned char c, int* randomize) { - switch(c) + if (!is_char_legal(c)) { - case 0x22: - case 0x2a: - case 0x2b: - case 0x2c: - case 0x2e: - case 0x3a: - case 0x3b: - case 0x3c: - case 0x3d: - case 0x3e: - case 0x3f: - case 0x5b: - case 0x5c: - case 0x5d: - case 0x7c: + if(c <= 0x20) + c = 0; /* Illegal char, remove */ + else + { /* Illegal char, replace */ c = '_'; *randomize = 1; /* as per FAT spec */ - break; - - default: - if(c <= 0x20) - c = 0; /* Illegal char, remove */ - else - c = toupper(c); - break; + } } + else + c = toupper(c); return c; }