vfat.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <linux/ctype.h>
  27. #include "dos.h"
  28. #include "fdos.h"
  29. static int dir_read (Fs_t *fs,
  30. Slot_t *dir,
  31. Directory_t *dirent,
  32. int num,
  33. struct vfat_state *v);
  34. static int unicode_read (char *in, char *out, int num);
  35. static int match (const char *s, const char *p);
  36. static unsigned char sum_shortname (char *name);
  37. static int check_vfat (struct vfat_state *v, Directory_t *dir);
  38. static char *conv_name (char *name, char *ext, char Case, char *ans);
  39. /*-----------------------------------------------------------------------------
  40. * clear_vfat --
  41. *-----------------------------------------------------------------------------
  42. */
  43. static void clear_vfat (struct vfat_state *v)
  44. {
  45. v -> subentries = 0;
  46. v -> status = 0;
  47. }
  48. /*-----------------------------------------------------------------------------
  49. * vfat_lookup --
  50. *-----------------------------------------------------------------------------
  51. */
  52. int vfat_lookup (Slot_t *dir,
  53. Fs_t *fs,
  54. Directory_t *dirent,
  55. int *entry,
  56. int *vfat_start,
  57. char *filename,
  58. int flags,
  59. char *outname,
  60. Slot_t *file)
  61. {
  62. int found;
  63. struct vfat_state vfat;
  64. char newfile [VSE_NAMELEN];
  65. int vfat_present = 0;
  66. if (*entry == -1) {
  67. return -1;
  68. }
  69. found = 0;
  70. clear_vfat (&vfat);
  71. while (1) {
  72. if (dir_read (fs, dir, dirent, *entry, &vfat) < 0) {
  73. if (vfat_start) {
  74. *vfat_start = *entry;
  75. }
  76. break;
  77. }
  78. (*entry)++;
  79. /* Empty slot */
  80. if (dirent -> name[0] == '\0'){
  81. if (vfat_start == 0) {
  82. break;
  83. }
  84. continue;
  85. }
  86. if (dirent -> attr == ATTR_VSE) {
  87. /* VSE entry, continue */
  88. continue;
  89. }
  90. if ( (dirent -> name [0] == DELMARK) ||
  91. ((dirent -> attr & ATTR_DIRECTORY) != 0 &&
  92. (flags & ACCEPT_DIR) == 0) ||
  93. ((dirent -> attr & ATTR_VOLUME) != 0 &&
  94. (flags & ACCEPT_LABEL) == 0) ||
  95. (((dirent -> attr & (ATTR_DIRECTORY | ATTR_VOLUME)) == 0) &&
  96. (flags & ACCEPT_PLAIN) == 0)) {
  97. clear_vfat (&vfat);
  98. continue;
  99. }
  100. vfat_present = check_vfat (&vfat, dirent);
  101. if (vfat_start) {
  102. *vfat_start = *entry - 1;
  103. if (vfat_present) {
  104. *vfat_start -= vfat.subentries;
  105. }
  106. }
  107. if (dirent -> attr & ATTR_VOLUME) {
  108. strncpy (newfile, dirent -> name, 8);
  109. newfile [8] = '\0';
  110. strncat (newfile, dirent -> ext, 3);
  111. newfile [11] = '\0';
  112. }
  113. else {
  114. conv_name (dirent -> name, dirent -> ext, dirent -> Case, newfile);
  115. }
  116. if (flags & MATCH_ANY) {
  117. found = 1;
  118. break;
  119. }
  120. if ((vfat_present && match (vfat.name, filename)) ||
  121. (match (newfile, filename))) {
  122. found = 1;
  123. break;
  124. }
  125. clear_vfat (&vfat);
  126. }
  127. if (found) {
  128. if ((flags & DO_OPEN) && file) {
  129. if (open_file (file, dirent) < 0) {
  130. return (-1);
  131. }
  132. }
  133. if (outname) {
  134. if (vfat_present) {
  135. strcpy (outname, vfat.name);
  136. }
  137. else {
  138. strcpy (outname, newfile);
  139. }
  140. }
  141. return (0); /* File found */
  142. } else {
  143. *entry = -1;
  144. return -1; /* File not found */
  145. }
  146. }
  147. /*-----------------------------------------------------------------------------
  148. * dir_read -- Read one directory entry
  149. *-----------------------------------------------------------------------------
  150. */
  151. static int dir_read (Fs_t *fs,
  152. Slot_t *dir,
  153. Directory_t *dirent,
  154. int num,
  155. struct vfat_state *v)
  156. {
  157. /* read the directory entry */
  158. if (read_file (fs,
  159. dir,
  160. (char *)dirent,
  161. num * MDIR_SIZE,
  162. MDIR_SIZE) != MDIR_SIZE) {
  163. return (-1);
  164. }
  165. if (v && (dirent -> attr == ATTR_VSE)) {
  166. struct vfat_subentry *vse;
  167. unsigned char id, last_flag;
  168. char *c;
  169. vse = (struct vfat_subentry *) dirent;
  170. id = vse -> id & VSE_MASK;
  171. last_flag = (vse -> id & VSE_LAST);
  172. if (id > MAX_VFAT_SUBENTRIES) {
  173. /* Invalid VSE entry */
  174. return (-1);
  175. }
  176. /* Decode VSE */
  177. if(v -> sum != vse -> sum) {
  178. clear_vfat (v);
  179. v -> sum = vse -> sum;
  180. }
  181. v -> status |= 1 << (id - 1);
  182. if (last_flag) {
  183. v -> subentries = id;
  184. }
  185. c = &(v -> name [VSE_NAMELEN * (id - 1)]);
  186. c += unicode_read (vse->text1, c, VSE1SIZE);
  187. c += unicode_read (vse->text2, c, VSE2SIZE);
  188. c += unicode_read (vse->text3, c, VSE3SIZE);
  189. if (last_flag) {
  190. *c = '\0'; /* Null terminate long name */
  191. }
  192. }
  193. return (0);
  194. }
  195. /*-----------------------------------------------------------------------------
  196. * unicode_read --
  197. *-----------------------------------------------------------------------------
  198. */
  199. static int unicode_read (char *in, char *out, int num)
  200. {
  201. int j;
  202. for (j = 0; j < num; ++j) {
  203. if (in [1])
  204. *out = '_';
  205. else
  206. *out = in [0];
  207. out ++;
  208. in += 2;
  209. }
  210. return num;
  211. }
  212. /*-----------------------------------------------------------------------------
  213. * match --
  214. *-----------------------------------------------------------------------------
  215. */
  216. static int match (const char *s, const char *p)
  217. {
  218. for (; *p != '\0'; ) {
  219. if (toupper (*s) != toupper (*p)) {
  220. return (0);
  221. }
  222. p++;
  223. s++;
  224. }
  225. if (*s != '\0') {
  226. return (0);
  227. }
  228. else {
  229. return (1);
  230. }
  231. }
  232. /*-----------------------------------------------------------------------------
  233. * sum_shortname --
  234. *-----------------------------------------------------------------------------
  235. */
  236. static unsigned char sum_shortname (char *name)
  237. {
  238. unsigned char sum;
  239. int j;
  240. for (j = sum = 0; j < 11; ++j) {
  241. sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) +
  242. (name [j] ? name [j] : ' ');
  243. }
  244. return (sum);
  245. }
  246. /*-----------------------------------------------------------------------------
  247. * check_vfat --
  248. * Return 1 if long name is valid, 0 else
  249. *-----------------------------------------------------------------------------
  250. */
  251. static int check_vfat (struct vfat_state *v, Directory_t *dir)
  252. {
  253. char name[12];
  254. if (v -> subentries == 0) {
  255. return 0;
  256. }
  257. strncpy (name, dir -> name, 8);
  258. strncpy (name + 8, dir -> ext, 3);
  259. name [11] = '\0';
  260. if (v -> sum != sum_shortname (name)) {
  261. return 0;
  262. }
  263. if( (v -> status & ((1 << v -> subentries) - 1)) !=
  264. (1 << v -> subentries) - 1) {
  265. return 0;
  266. }
  267. v->name [VSE_NAMELEN * v -> subentries] = 0;
  268. return 1;
  269. }
  270. /*-----------------------------------------------------------------------------
  271. * conv_name --
  272. *-----------------------------------------------------------------------------
  273. */
  274. static char *conv_name (char *name, char *ext, char Case, char *ans)
  275. {
  276. char tname [9], text [4];
  277. int i;
  278. i = 0;
  279. while (i < 8 && name [i] != ' ' && name [i] != '\0') {
  280. tname [i] = name [i];
  281. i++;
  282. }
  283. tname [i] = '\0';
  284. if (Case & BASECASE) {
  285. for (i = 0; i < 8 && tname [i]; i++) {
  286. tname [i] = tolower (tname [i]);
  287. }
  288. }
  289. i = 0;
  290. while (i < 3 && ext [i] != ' ' && ext [i] != '\0') {
  291. text [i] = ext [i];
  292. i++;
  293. }
  294. text [i] = '\0';
  295. if (Case & EXTCASE){
  296. for (i = 0; i < 3 && text [i]; i++) {
  297. text [i] = tolower (text [i]);
  298. }
  299. }
  300. if (*text) {
  301. strcpy (ans, tname);
  302. strcat (ans, ".");
  303. strcat (ans, text);
  304. }
  305. else {
  306. strcpy(ans, tname);
  307. }
  308. return (ans);
  309. }