msdosfs_conv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* $OpenBSD: msdosfs_conv.c,v 1.18 2014/12/16 18:30:04 tedu Exp $ */
  2. /* $NetBSD: msdosfs_conv.c,v 1.24 1997/10/17 11:23:54 ws Exp $ */
  3. /*-
  4. * Copyright (C) 1995, 1997 Wolfgang Solfrank.
  5. * Copyright (C) 1995, 1997 TooLs GmbH.
  6. * All rights reserved.
  7. * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by TooLs GmbH.
  20. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  29. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  31. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  32. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Written by Paul Popelka (paulp@uts.amdahl.com)
  36. *
  37. * You can do anything you want with this software, just don't say you wrote
  38. * it, and don't remove this notice.
  39. *
  40. * This software is provided "as is".
  41. *
  42. * The author supplies this software to be publicly redistributed on the
  43. * understanding that the author is not responsible for the correct
  44. * functioning of this software in any circumstances and is not liable for
  45. * any damages caused by this software.
  46. *
  47. * October 1992
  48. */
  49. /*
  50. * System include files.
  51. */
  52. #include <sys/param.h>
  53. #include <sys/systm.h>
  54. #include <sys/time.h>
  55. #include <sys/kernel.h> /* defines tz */
  56. #include <sys/dirent.h>
  57. #include <sys/vnode.h>
  58. #include <sys/lock.h>
  59. /*
  60. * MSDOSFS include files.
  61. */
  62. #include <msdosfs/direntry.h>
  63. #include <msdosfs/denode.h>
  64. /*
  65. * Days in each month in a regular year.
  66. */
  67. const u_short regyear[] = {
  68. 31, 28, 31, 30, 31, 30,
  69. 31, 31, 30, 31, 30, 31
  70. };
  71. /*
  72. * Days in each month in a leap year.
  73. */
  74. const u_short leapyear[] = {
  75. 31, 29, 31, 30, 31, 30,
  76. 31, 31, 30, 31, 30, 31
  77. };
  78. /*
  79. * Variables used to remember parts of the last time conversion. Maybe we
  80. * can avoid a full conversion.
  81. */
  82. time_t lasttime;
  83. uint32_t lastday;
  84. u_short lastddate;
  85. u_short lastdtime;
  86. /*
  87. * Convert the unix version of time to dos's idea of time to be used in
  88. * file timestamps. The passed in unix time is assumed to be in GMT.
  89. */
  90. void
  91. unix2dostime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp)
  92. {
  93. time_t t;
  94. uint32_t days;
  95. uint32_t inc;
  96. uint32_t year;
  97. uint32_t month;
  98. const u_short *months;
  99. /*
  100. * If the time from the last conversion is the same as now, then
  101. * skip the computations and use the saved result.
  102. */
  103. t = tsp->tv_sec - (tz.tz_minuteswest * 60)
  104. /* +- daylight saving time correction */ ;
  105. t &= ~1;
  106. /*
  107. * Before 1/1/1980 there is only a timeless void. After 12/31/2107
  108. * there is only Cthulhu.
  109. */
  110. #define DOSEPOCH 315532800LL
  111. #define DOSENDTIME 4354775999LL
  112. if (t < DOSEPOCH || t > DOSENDTIME)
  113. t = DOSEPOCH;
  114. if (lasttime != t) {
  115. lasttime = t;
  116. lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
  117. + (((t / 60) % 60) << DT_MINUTES_SHIFT)
  118. + (((t / 3600) % 24) << DT_HOURS_SHIFT);
  119. /*
  120. * If the number of days since 1970 is the same as the last
  121. * time we did the computation then skip all this leap year
  122. * and month stuff.
  123. */
  124. days = t / (24 * 60 * 60);
  125. if (days != lastday) {
  126. lastday = days;
  127. for (year = 1970;; year++) {
  128. inc = year & 0x03 ? 365 : 366;
  129. if (days < inc)
  130. break;
  131. days -= inc;
  132. }
  133. months = year & 0x03 ? regyear : leapyear;
  134. for (month = 0; month < 12; month++) {
  135. if (days < months[month])
  136. break;
  137. days -= months[month];
  138. }
  139. lastddate = ((days + 1) << DD_DAY_SHIFT)
  140. + ((month + 1) << DD_MONTH_SHIFT);
  141. /*
  142. * Remember dos's idea of time is relative to 1980.
  143. * unix's is relative to 1970. If somehow we get a
  144. * time before 1980 then don't give totally crazy
  145. * results.
  146. */
  147. if (year > 1980)
  148. lastddate += (year - 1980) << DD_YEAR_SHIFT;
  149. }
  150. }
  151. if (dtp != NULL)
  152. *dtp = lastdtime;
  153. if (dhp != NULL)
  154. *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
  155. *ddp = lastddate;
  156. }
  157. /*
  158. * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
  159. * interval there were 8 regular years and 2 leap years.
  160. */
  161. #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
  162. u_short lastdosdate;
  163. uint32_t lastseconds;
  164. /*
  165. * Convert from dos' idea of time to unix'. This will probably only be
  166. * called from the stat(), and fstat() system calls and so probably need
  167. * not be too efficient.
  168. */
  169. void
  170. dos2unixtime(u_int dd, u_int dt, u_int dh, struct timespec *tsp)
  171. {
  172. uint32_t seconds;
  173. uint32_t m, month;
  174. uint32_t y, year;
  175. uint32_t days;
  176. const u_short *months;
  177. if (dd == 0) {
  178. /*
  179. * Uninitialized field, return the epoch.
  180. */
  181. tsp->tv_sec = 0;
  182. tsp->tv_nsec = 0;
  183. return;
  184. }
  185. seconds = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2
  186. + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
  187. + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
  188. + dh / 100;
  189. /*
  190. * If the year, month, and day from the last conversion are the
  191. * same then use the saved value.
  192. */
  193. if (lastdosdate != dd) {
  194. lastdosdate = dd;
  195. days = 0;
  196. year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
  197. for (y = 0; y < year; y++)
  198. days += y & 0x03 ? 365 : 366;
  199. months = year & 0x03 ? regyear : leapyear;
  200. /*
  201. * Prevent going from 0 to 0xffffffff in the following
  202. * loop.
  203. */
  204. month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
  205. if (month == 0) {
  206. printf("dos2unixtime(): month value out of range (%u)\n",
  207. month);
  208. month = 1;
  209. }
  210. for (m = 0; m < month - 1; m++)
  211. days += months[m];
  212. days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
  213. lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
  214. }
  215. tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
  216. /* -+ daylight saving time correction */ ;
  217. tsp->tv_nsec = (dh % 100) * 10000000;
  218. }
  219. static const u_char
  220. unix2dos[256] = {
  221. 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
  222. 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
  223. 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
  224. 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
  225. 0, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  226. 0x28, 0x29, 0, 0, 0, 0x2d, 0, 0, /* 28-2f */
  227. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  228. 0x38, 0x39, 0, 0, 0, 0, 0, 0, /* 38-3f */
  229. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
  230. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
  231. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
  232. 0x58, 0x59, 0x5a, 0, 0, 0, 0x5e, 0x5f, /* 58-5f */
  233. 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
  234. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
  235. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
  236. 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */
  237. 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
  238. 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
  239. 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
  240. 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
  241. 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
  242. 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
  243. 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
  244. 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
  245. 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
  246. 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
  247. 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
  248. 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
  249. 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
  250. 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
  251. 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
  252. 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
  253. };
  254. static const u_char
  255. dos2unix[256] = {
  256. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
  257. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
  258. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
  259. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
  260. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  261. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
  262. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  263. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
  264. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
  265. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
  266. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
  267. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
  268. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
  269. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
  270. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
  271. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
  272. 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
  273. 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
  274. 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
  275. 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
  276. 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
  277. 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
  278. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
  279. 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
  280. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
  281. 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
  282. 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
  283. 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
  284. 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
  285. 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
  286. 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
  287. 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
  288. };
  289. static const u_char
  290. u2l[256] = {
  291. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
  292. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
  293. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
  294. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
  295. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  296. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
  297. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  298. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
  299. 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
  300. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
  301. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
  302. 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
  303. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
  304. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
  305. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
  306. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
  307. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
  308. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
  309. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
  310. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
  311. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
  312. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
  313. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
  314. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
  315. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
  316. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
  317. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
  318. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
  319. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
  320. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
  321. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
  322. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
  323. };
  324. /*
  325. * DOS filenames are made of 2 parts, the name part and the extension part.
  326. * The name part is 8 characters long and the extension part is 3
  327. * characters long. They may contain trailing blanks if the name or
  328. * extension are not long enough to fill their respective fields.
  329. */
  330. /*
  331. * Convert a DOS filename to a unix filename. And, return the number of
  332. * characters in the resulting unix filename excluding the terminating
  333. * null.
  334. */
  335. int
  336. dos2unixfn(u_char dn[11], u_char *un, int lower)
  337. {
  338. int i;
  339. int thislong = 1;
  340. u_char c;
  341. /*
  342. * If first char of the filename is SLOT_E5 (0x05), then the real
  343. * first char of the filename should be 0xe5. But, they couldn't
  344. * just have a 0xe5 mean 0xe5 because that is used to mean a freed
  345. * directory slot. Another dos quirk.
  346. */
  347. if (*dn == SLOT_E5)
  348. c = dos2unix[0xe5];
  349. else
  350. c = dos2unix[*dn];
  351. *un++ = lower ? u2l[c] : c;
  352. dn++;
  353. /*
  354. * Copy the name portion into the unix filename string.
  355. */
  356. for (i = 1; i < 8 && *dn != ' '; i++) {
  357. c = dos2unix[*dn++];
  358. *un++ = lower ? u2l[c] : c;
  359. thislong++;
  360. }
  361. dn += 8 - i;
  362. /*
  363. * Now, if there is an extension then put in a period and copy in
  364. * the extension.
  365. */
  366. if (*dn != ' ') {
  367. *un++ = '.';
  368. thislong++;
  369. for (i = 0; i < 3 && *dn != ' '; i++) {
  370. c = dos2unix[*dn++];
  371. *un++ = lower ? u2l[c] : c;
  372. thislong++;
  373. }
  374. }
  375. *un++ = 0;
  376. return (thislong);
  377. }
  378. /*
  379. * Convert a unix filename to a DOS filename according to Win95 rules.
  380. * If applicable and gen is not 0, it is inserted into the converted
  381. * filename as a generation number.
  382. * Returns
  383. * 0 if name couldn't be converted
  384. * 1 if the converted name is the same as the original
  385. * (no long filename entry necessary for Win95)
  386. * 2 if conversion was successful
  387. * 3 if conversion was successful and generation number was inserted
  388. */
  389. int
  390. unix2dosfn(u_char *un, u_char dn[11], int unlen, u_int gen)
  391. {
  392. int i, j, l;
  393. int conv = 1;
  394. u_char *cp, *dp, *dp1;
  395. u_char gentext[6];
  396. /*
  397. * Fill the dos filename string with blanks. These are DOS's pad
  398. * characters.
  399. */
  400. for (i = 0; i < 11; i++)
  401. dn[i] = ' ';
  402. /*
  403. * The filenames "." and ".." are handled specially, since they
  404. * don't follow dos filename rules.
  405. */
  406. if (un[0] == '.' && unlen == 1) {
  407. dn[0] = '.';
  408. return gen <= 1;
  409. }
  410. if (un[0] == '.' && un[1] == '.' && unlen == 2) {
  411. dn[0] = '.';
  412. dn[1] = '.';
  413. return gen <= 1;
  414. }
  415. /*
  416. * Filenames with only blanks and dots are not allowed!
  417. */
  418. for (cp = un, i = unlen; --i >= 0; cp++)
  419. if (*cp != ' ' && *cp != '.')
  420. break;
  421. if (i < 0)
  422. return 0;
  423. /*
  424. * Now find the extension
  425. * Note: dot as first char doesn't start extension
  426. * and trailing dots and blanks are ignored
  427. */
  428. dp = dp1 = 0;
  429. for (cp = un + 1, i = unlen - 1; --i >= 0;) {
  430. switch (*cp++) {
  431. case '.':
  432. if (!dp1)
  433. dp1 = cp;
  434. break;
  435. case ' ':
  436. break;
  437. default:
  438. if (dp1)
  439. dp = dp1;
  440. dp1 = 0;
  441. break;
  442. }
  443. }
  444. /*
  445. * Now convert it
  446. */
  447. if (dp) {
  448. if (dp1)
  449. l = dp1 - dp;
  450. else
  451. l = unlen - (dp - un);
  452. for (i = 0, j = 8; i < l && j < 11; i++, j++) {
  453. if (dp[i] != (dn[j] = unix2dos[dp[i]])
  454. && conv != 3)
  455. conv = 2;
  456. if (!dn[j]) {
  457. conv = 3;
  458. dn[j--] = ' ';
  459. }
  460. }
  461. if (i < l)
  462. conv = 3;
  463. dp--;
  464. } else {
  465. for (dp = cp; *--dp == ' ' || *dp == '.';);
  466. dp++;
  467. }
  468. /*
  469. * Now convert the rest of the name
  470. */
  471. for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
  472. if (*un != (dn[j] = unix2dos[*un])
  473. && conv != 3)
  474. conv = 2;
  475. if (!dn[j]) {
  476. conv = 3;
  477. dn[j--] = ' ';
  478. }
  479. }
  480. if (un < dp)
  481. conv = 3;
  482. /*
  483. * If we didn't have any chars in filename,
  484. * generate a default
  485. */
  486. if (!j)
  487. dn[0] = '_';
  488. /*
  489. * The first character cannot be E5,
  490. * because that means a deleted entry
  491. */
  492. if (dn[0] == 0xe5)
  493. dn[0] = SLOT_E5;
  494. /*
  495. * If there wasn't any char dropped,
  496. * there is no place for generation numbers
  497. */
  498. if (conv != 3) {
  499. if (gen > 1)
  500. return 0;
  501. return conv;
  502. }
  503. /*
  504. * Now insert the generation number into the filename part
  505. */
  506. for (cp = gentext + sizeof(gentext); cp > gentext && gen; gen /= 10)
  507. *--cp = gen % 10 + '0';
  508. if (gen)
  509. return 0;
  510. for (i = 8; dn[--i] == ' ';);
  511. if (gentext + sizeof(gentext) - cp + 1 > 8 - i)
  512. i = 8 - (gentext + sizeof(gentext) - cp + 1);
  513. dn[i++] = '~';
  514. while (cp < gentext + sizeof(gentext))
  515. dn[i++] = *cp++;
  516. return 3;
  517. }
  518. /*
  519. * Create a Win95 long name directory entry
  520. * Note: assumes that the filename is valid,
  521. * i.e. doesn't consist solely of blanks and dots
  522. */
  523. int
  524. unix2winfn(u_char *un, int unlen, struct winentry *wep, int cnt, int chksum)
  525. {
  526. u_int8_t *cp;
  527. int i;
  528. /*
  529. * Drop trailing blanks and dots
  530. */
  531. for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
  532. un += (cnt - 1) * WIN_CHARS;
  533. unlen -= (cnt - 1) * WIN_CHARS;
  534. /*
  535. * Initialize winentry to some useful default
  536. */
  537. for (cp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *cp++ = 0xff);
  538. wep->weCnt = cnt;
  539. wep->weAttributes = ATTR_WIN95;
  540. wep->weReserved1 = 0;
  541. wep->weChksum = chksum;
  542. wep->weReserved2 = 0;
  543. /*
  544. * Now convert the filename parts
  545. */
  546. for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  547. if (--unlen < 0)
  548. goto done;
  549. *cp++ = *un++;
  550. *cp++ = 0;
  551. }
  552. for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  553. if (--unlen < 0)
  554. goto done;
  555. *cp++ = *un++;
  556. *cp++ = 0;
  557. }
  558. for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  559. if (--unlen < 0)
  560. goto done;
  561. *cp++ = *un++;
  562. *cp++ = 0;
  563. }
  564. if (!unlen)
  565. wep->weCnt |= WIN_LAST;
  566. return unlen;
  567. done:
  568. *cp++ = 0;
  569. *cp++ = 0;
  570. wep->weCnt |= WIN_LAST;
  571. return 0;
  572. }
  573. /*
  574. * Compare our filename to the one in the Win95 entry
  575. * Returns the checksum or -1 if no match
  576. */
  577. int
  578. winChkName(u_char *un, int unlen, struct winentry *wep, int chksum)
  579. {
  580. u_int8_t *cp;
  581. int i;
  582. /*
  583. * First compare checksums
  584. */
  585. if (wep->weCnt&WIN_LAST)
  586. chksum = wep->weChksum;
  587. else if (chksum != wep->weChksum)
  588. chksum = -1;
  589. if (chksum == -1)
  590. return -1;
  591. /*
  592. * Offset of this entry
  593. */
  594. i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
  595. if ((unlen -= i) <= 0)
  596. return -1;
  597. un += i;
  598. if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
  599. return -1;
  600. /*
  601. * Compare the name parts
  602. */
  603. for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  604. if (--unlen < 0) {
  605. if (!*cp++ && !*cp)
  606. return chksum;
  607. return -1;
  608. }
  609. if (u2l[*cp++] != u2l[*un++] || *cp++)
  610. return -1;
  611. }
  612. for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  613. if (--unlen < 0) {
  614. if (!*cp++ && !*cp)
  615. return chksum;
  616. return -1;
  617. }
  618. if (u2l[*cp++] != u2l[*un++] || *cp++)
  619. return -1;
  620. }
  621. for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  622. if (--unlen < 0) {
  623. if (!*cp++ && !*cp)
  624. return chksum;
  625. return -1;
  626. }
  627. if (u2l[*cp++] != u2l[*un++] || *cp++)
  628. return -1;
  629. }
  630. return chksum;
  631. }
  632. /*
  633. * Convert Win95 filename to dirbuf.
  634. * Returns the checksum or -1 if impossible
  635. */
  636. int
  637. win2unixfn(struct winentry *wep, struct dirent *dp, int chksum)
  638. {
  639. u_int8_t *cp;
  640. u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
  641. int i;
  642. if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
  643. || !(wep->weCnt&WIN_CNT))
  644. return -1;
  645. /*
  646. * First compare checksums
  647. */
  648. if (wep->weCnt&WIN_LAST) {
  649. chksum = wep->weChksum;
  650. /*
  651. * This works even though d_namlen is one byte!
  652. */
  653. dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
  654. } else if (chksum != wep->weChksum)
  655. chksum = -1;
  656. if (chksum == -1)
  657. return -1;
  658. /*
  659. * Offset of this entry
  660. */
  661. i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
  662. np = (u_int8_t *)dp->d_name + i;
  663. /*
  664. * Convert the name parts
  665. */
  666. for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  667. switch (*np++ = *cp++) {
  668. case 0:
  669. dp->d_namlen -= sizeof(wep->wePart2)/2
  670. + sizeof(wep->wePart3)/2 + i + 1;
  671. return chksum;
  672. case '/':
  673. np[-1] = 0;
  674. return -1;
  675. }
  676. /*
  677. * The size comparison should result in the compiler
  678. * optimizing the whole if away
  679. */
  680. if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
  681. && np > ep) {
  682. np[-1] = 0;
  683. return -1;
  684. }
  685. if (*cp++)
  686. return -1;
  687. }
  688. for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  689. switch (*np++ = *cp++) {
  690. case 0:
  691. dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
  692. return chksum;
  693. case '/':
  694. np[-1] = 0;
  695. return -1;
  696. }
  697. /*
  698. * The size comparisons should be optimized away
  699. */
  700. if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
  701. && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
  702. && np > ep) {
  703. np[-1] = 0;
  704. return -1;
  705. }
  706. if (*cp++)
  707. return -1;
  708. }
  709. for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  710. switch (*np++ = *cp++) {
  711. case 0:
  712. dp->d_namlen -= i + 1;
  713. return chksum;
  714. case '/':
  715. np[-1] = 0;
  716. return -1;
  717. }
  718. /*
  719. * See above
  720. */
  721. if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
  722. && np > ep) {
  723. np[-1] = 0;
  724. return -1;
  725. }
  726. if (*cp++)
  727. return -1;
  728. }
  729. return chksum;
  730. }
  731. /*
  732. * Compute the checksum of a DOS filename for Win95 use
  733. */
  734. u_int8_t
  735. winChksum(u_int8_t *name)
  736. {
  737. int i;
  738. u_int8_t s;
  739. for (s = 0, i = 11; --i >= 0; s += *name++)
  740. s = (s << 7)|(s >> 1);
  741. return s;
  742. }
  743. /*
  744. * Determine the number of slots necessary for Win95 names
  745. */
  746. int
  747. winSlotCnt(u_char *un, int unlen)
  748. {
  749. for (un += unlen; unlen > 0; unlen--)
  750. if (*--un != ' ' && *un != '.')
  751. break;
  752. if (unlen > WIN_MAXLEN)
  753. return 0;
  754. return howmany(unlen, WIN_CHARS);
  755. }