vfat.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * (C) Copyright 2002
  3. * Stäubli Faverges - <www.staubli.com>
  4. * Pierre AUBERT p.aubert@staubli.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <config.h>
  26. #if (CONFIG_COMMANDS & CFG_CMD_FDOS)
  27. #include <linux/ctype.h>
  28. #include "dos.h"
  29. #include "fdos.h"
  30. static int dir_read (Fs_t *fs,
  31. Slot_t *dir,
  32. Directory_t *dirent,
  33. int num,
  34. struct vfat_state *v);
  35. static int unicode_read (char *in, char *out, int num);
  36. static int match (const char *s, const char *p);
  37. static unsigned char sum_shortname (char *name);
  38. static int check_vfat (struct vfat_state *v, Directory_t *dir);
  39. static char *conv_name (char *name, char *ext, char Case, char *ans);
  40. /*-----------------------------------------------------------------------------
  41. * clear_vfat --
  42. *-----------------------------------------------------------------------------
  43. */
  44. static void clear_vfat (struct vfat_state *v)
  45. {
  46. v -> subentries = 0;
  47. v -> status = 0;
  48. }
  49. /*-----------------------------------------------------------------------------
  50. * vfat_lookup --
  51. *-----------------------------------------------------------------------------
  52. */
  53. int vfat_lookup (Slot_t *dir,
  54. Fs_t *fs,
  55. Directory_t *dirent,
  56. int *entry,
  57. int *vfat_start,
  58. char *filename,
  59. int flags,
  60. char *outname,
  61. Slot_t *file)
  62. {
  63. int found;
  64. struct vfat_state vfat;
  65. char newfile [VSE_NAMELEN];
  66. int vfat_present = 0;
  67. if (*entry == -1) {
  68. return -1;
  69. }
  70. found = 0;
  71. clear_vfat (&vfat);
  72. while (1) {
  73. if (dir_read (fs, dir, dirent, *entry, &vfat) < 0) {
  74. if (vfat_start) {
  75. *vfat_start = *entry;
  76. }
  77. break;
  78. }
  79. (*entry)++;
  80. /* Empty slot */
  81. if (dirent -> name[0] == '\0'){
  82. if (vfat_start == 0) {
  83. break;
  84. }
  85. continue;
  86. }
  87. if (dirent -> attr == ATTR_VSE) {
  88. /* VSE entry, continue */
  89. continue;
  90. }
  91. if ( (dirent -> name [0] == DELMARK) ||
  92. ((dirent -> attr & ATTR_DIRECTORY) != 0 &&
  93. (flags & ACCEPT_DIR) == 0) ||
  94. ((dirent -> attr & ATTR_VOLUME) != 0 &&
  95. (flags & ACCEPT_LABEL) == 0) ||
  96. (((dirent -> attr & (ATTR_DIRECTORY | ATTR_VOLUME)) == 0) &&
  97. (flags & ACCEPT_PLAIN) == 0)) {
  98. clear_vfat (&vfat);
  99. continue;
  100. }
  101. vfat_present = check_vfat (&vfat, dirent);
  102. if (vfat_start) {
  103. *vfat_start = *entry - 1;
  104. if (vfat_present) {
  105. *vfat_start -= vfat.subentries;
  106. }
  107. }
  108. if (dirent -> attr & ATTR_VOLUME) {
  109. strncpy (newfile, dirent -> name, 8);
  110. newfile [8] = '\0';
  111. strncat (newfile, dirent -> ext, 3);
  112. newfile [11] = '\0';
  113. }
  114. else {
  115. conv_name (dirent -> name, dirent -> ext, dirent -> Case, newfile);
  116. }
  117. if (flags & MATCH_ANY) {
  118. found = 1;
  119. break;
  120. }
  121. if ((vfat_present && match (vfat.name, filename)) ||
  122. (match (newfile, filename))) {
  123. found = 1;
  124. break;
  125. }
  126. clear_vfat (&vfat);
  127. }
  128. if (found) {
  129. if ((flags & DO_OPEN) && file) {
  130. if (open_file (file, dirent) < 0) {
  131. return (-1);
  132. }
  133. }
  134. if (outname) {
  135. if (vfat_present) {
  136. strcpy (outname, vfat.name);
  137. }
  138. else {
  139. strcpy (outname, newfile);
  140. }
  141. }
  142. return (0); /* File found */
  143. } else {
  144. *entry = -1;
  145. return -1; /* File not found */
  146. }
  147. }
  148. /*-----------------------------------------------------------------------------
  149. * dir_read -- Read one directory entry
  150. *-----------------------------------------------------------------------------
  151. */
  152. static int dir_read (Fs_t *fs,
  153. Slot_t *dir,
  154. Directory_t *dirent,
  155. int num,
  156. struct vfat_state *v)
  157. {
  158. /* read the directory entry */
  159. if (read_file (fs,
  160. dir,
  161. (char *)dirent,
  162. num * MDIR_SIZE,
  163. MDIR_SIZE) != MDIR_SIZE) {
  164. return (-1);
  165. }
  166. if (v && (dirent -> attr == ATTR_VSE)) {
  167. struct vfat_subentry *vse;
  168. unsigned char id, last_flag;
  169. char *c;
  170. vse = (struct vfat_subentry *) dirent;
  171. id = vse -> id & VSE_MASK;
  172. last_flag = (vse -> id & VSE_LAST);
  173. if (id > MAX_VFAT_SUBENTRIES) {
  174. /* Invalid VSE entry */
  175. return (-1);
  176. }
  177. /* Decode VSE */
  178. if(v -> sum != vse -> sum) {
  179. clear_vfat (v);
  180. v -> sum = vse -> sum;
  181. }
  182. v -> status |= 1 << (id - 1);
  183. if (last_flag) {
  184. v -> subentries = id;
  185. }
  186. c = &(v -> name [VSE_NAMELEN * (id - 1)]);
  187. c += unicode_read (vse->text1, c, VSE1SIZE);
  188. c += unicode_read (vse->text2, c, VSE2SIZE);
  189. c += unicode_read (vse->text3, c, VSE3SIZE);
  190. if (last_flag) {
  191. *c = '\0'; /* Null terminate long name */
  192. }
  193. }
  194. return (0);
  195. }
  196. /*-----------------------------------------------------------------------------
  197. * unicode_read --
  198. *-----------------------------------------------------------------------------
  199. */
  200. static int unicode_read (char *in, char *out, int num)
  201. {
  202. int j;
  203. for (j = 0; j < num; ++j) {
  204. if (in [1])
  205. *out = '_';
  206. else
  207. *out = in [0];
  208. out ++;
  209. in += 2;
  210. }
  211. return num;
  212. }
  213. /*-----------------------------------------------------------------------------
  214. * match --
  215. *-----------------------------------------------------------------------------
  216. */
  217. static int match (const char *s, const char *p)
  218. {
  219. for (; *p != '\0'; ) {
  220. if (toupper (*s) != toupper (*p)) {
  221. return (0);
  222. }
  223. p++;
  224. s++;
  225. }
  226. if (*s != '\0') {
  227. return (0);
  228. }
  229. else {
  230. return (1);
  231. }
  232. }
  233. /*-----------------------------------------------------------------------------
  234. * sum_shortname --
  235. *-----------------------------------------------------------------------------
  236. */
  237. static unsigned char sum_shortname (char *name)
  238. {
  239. unsigned char sum;
  240. int j;
  241. for (j = sum = 0; j < 11; ++j) {
  242. sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) +
  243. (name [j] ? name [j] : ' ');
  244. }
  245. return (sum);
  246. }
  247. /*-----------------------------------------------------------------------------
  248. * check_vfat --
  249. * Return 1 if long name is valid, 0 else
  250. *-----------------------------------------------------------------------------
  251. */
  252. static int check_vfat (struct vfat_state *v, Directory_t *dir)
  253. {
  254. char name[12];
  255. if (v -> subentries == 0) {
  256. return 0;
  257. }
  258. strncpy (name, dir -> name, 8);
  259. strncpy (name + 8, dir -> ext, 3);
  260. name [11] = '\0';
  261. if (v -> sum != sum_shortname (name)) {
  262. return 0;
  263. }
  264. if( (v -> status & ((1 << v -> subentries) - 1)) !=
  265. (1 << v -> subentries) - 1) {
  266. return 0;
  267. }
  268. v->name [VSE_NAMELEN * v -> subentries] = 0;
  269. return 1;
  270. }
  271. /*-----------------------------------------------------------------------------
  272. * conv_name --
  273. *-----------------------------------------------------------------------------
  274. */
  275. static char *conv_name (char *name, char *ext, char Case, char *ans)
  276. {
  277. char tname [9], text [4];
  278. int i;
  279. i = 0;
  280. while (i < 8 && name [i] != ' ' && name [i] != '\0') {
  281. tname [i] = name [i];
  282. i++;
  283. }
  284. tname [i] = '\0';
  285. if (Case & BASECASE) {
  286. for (i = 0; i < 8 && tname [i]; i++) {
  287. tname [i] = tolower (tname [i]);
  288. }
  289. }
  290. i = 0;
  291. while (i < 3 && ext [i] != ' ' && ext [i] != '\0') {
  292. text [i] = ext [i];
  293. i++;
  294. }
  295. text [i] = '\0';
  296. if (Case & EXTCASE){
  297. for (i = 0; i < 3 && text [i]; i++) {
  298. text [i] = tolower (text [i]);
  299. }
  300. }
  301. if (*text) {
  302. strcpy (ans, tname);
  303. strcat (ans, ".");
  304. strcat (ans, text);
  305. }
  306. else {
  307. strcpy(ans, tname);
  308. }
  309. return (ans);
  310. }
  311. #endif