#include <sys/param.h>
#include <sys/systm.h>
#include <sys/dirent.h>
#include <sys/iconv.h>
#include <sys/mount.h>
#include <fs/msdosfs/bpb.h>
#include <fs/msdosfs/direntry.h>
#include <fs/msdosfs/msdosfsmount.h>
extern struct iconv_functions *msdosfs_iconv;
static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
static u_char * dos2unixchr(u_char *, const u_char **, size_t *, int, struct msdosfsmount *);
static uint16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
static u_char * win2unixchr(u_char *, uint16_t, struct msdosfsmount *);
static uint16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
static const u_char
unix2dos[256] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0, 1, 1, 0x2d, 2, 0,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0, 1, 0, 1, 0, 0,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f,
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0xad, 0xbd, 0x9c, 0xc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
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
};
static const u_char
l2u[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
};
int
dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp)
{
size_t i;
int thislong = 0;
u_char *c, tmpbuf[5];
if (*dn == SLOT_E5)
*dn = 0xe5;
for (i = 8; i > 0 && *dn != ' ';) {
c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), &i,
lower & LCASE_BASE, pmp);
while (*c != '\0') {
*un++ = *c++;
thislong++;
}
}
dn += i;
if (*dn != ' ') {
*un++ = '.';
thislong++;
for (i = 3; i > 0 && *dn != ' ';) {
c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn),
&i, lower & LCASE_EXT, pmp);
while (*c != '\0') {
*un++ = *c++;
thislong++;
}
}
}
*un++ = 0;
return (thislong);
}
int
unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen,
struct msdosfsmount *pmp)
{
ssize_t i, j;
int l;
int conv = 1;
const u_char *cp, *dp, *dp1;
u_char gentext[6], *wcp;
uint16_t c;
for (i = 0; i < 11; i++)
dn[i] = ' ';
dn[11] = 0;
if (un[0] == '.' && unlen == 1) {
dn[0] = '.';
return gen <= 1;
}
if (un[0] == '.' && un[1] == '.' && unlen == 2) {
dn[0] = '.';
dn[1] = '.';
return gen <= 1;
}
for (cp = un, i = unlen; --i >= 0; cp++)
if (*cp != ' ' && *cp != '.')
break;
if (i < 0)
return 0;
for (cp = un, i = unlen; i > 0;)
if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
return 0;
dp = dp1 = NULL;
for (cp = un + 1, i = unlen - 1; --i >= 0;) {
switch (*cp++) {
case '.':
if (!dp1)
dp1 = cp;
break;
case ' ':
break;
default:
if (dp1)
dp = dp1;
dp1 = NULL;
break;
}
}
if (dp) {
if (dp1)
l = dp1 - dp;
else
l = unlen - (dp - un);
for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
c = unix2doschr(&cp, (size_t *)&i, pmp);
if (c & 0xff00) {
dn[j] = c >> 8;
if (++j < 11) {
dn[j] = c;
if (conv != 3)
conv = 2;
continue;
} else {
conv = 3;
dn[j-1] = ' ';
break;
}
} else {
dn[j] = c;
}
if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
conv = 2;
if (dn[j] == 1) {
conv = 3;
dn[j] = '_';
}
if (dn[j] == 2) {
conv = 3;
dn[j--] = ' ';
}
}
if (i > 0)
conv = 3;
dp--;
} else {
for (dp = cp; *--dp == ' ' || *dp == '.';);
dp++;
}
for (i = dp - un, j = 0; un < dp && j < 8; j++) {
c = unix2doschr(&un, &i, pmp);
if (c & 0xff00) {
dn[j] = c >> 8;
if (++j < 8) {
dn[j] = c;
if (conv != 3)
conv = 2;
continue;
} else {
conv = 3;
dn[j-1] = ' ';
break;
}
} else {
dn[j] = c;
}
if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
conv = 2;
if (dn[j] == 1) {
conv = 3;
dn[j] = '_';
}
if (dn[j] == 2) {
conv = 3;
dn[j--] = ' ';
}
}
if (un < dp)
conv = 3;
if (!j)
dn[0] = '_';
if (conv != 3) {
if (gen > 1)
conv = 0;
goto done;
}
if (gen == 0)
goto done;
for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
*--wcp = gen % 10 + '0';
if (gen) {
conv = 0;
goto done;
}
for (i = 8; dn[--i] == ' ';);
i++;
if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
i = 8 - (gentext + sizeof(gentext) - wcp + 1);
cp = dn;
i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
dn[i++] = '~';
while (wcp < gentext + sizeof(gentext))
dn[i++] = *wcp++;
while (i < 8)
dn[i++] = ' ';
conv = 3;
done:
if (dn[0] == 0xe5)
dn[0] = SLOT_E5;
return conv;
}
int
unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
int chksum, struct msdosfsmount *pmp)
{
uint8_t *wcp;
int i, end;
uint16_t code;
unlen = winLenFixup(un, unlen);
unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
pmp->pm_flags, pmp->pm_u2w);
memset(wep, 0xff, sizeof(*wep));
wep->weCnt = cnt;
wep->weAttributes = ATTR_WIN95;
wep->weReserved1 = 0;
wep->weChksum = chksum;
wep->weReserved2 = 0;
end = 0;
for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
code = unix2winchr(&un, &unlen, 0, pmp);
*wcp++ = code;
*wcp++ = code >> 8;
if (!code)
end = WIN_LAST;
}
for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
code = unix2winchr(&un, &unlen, 0, pmp);
*wcp++ = code;
*wcp++ = code >> 8;
if (!code)
end = WIN_LAST;
}
for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
code = unix2winchr(&un, &unlen, 0, pmp);
*wcp++ = code;
*wcp++ = code >> 8;
if (!code)
end = WIN_LAST;
}
if (!unlen)
end = WIN_LAST;
wep->weCnt |= end;
return !end;
}
int
winChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum,
struct msdosfsmount *pmp)
{
size_t len;
uint16_t c1, c2;
u_char *np;
struct dirent dirbuf;
if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0)
return -1;
#ifdef MSDOSFS_DEBUG
printf("winChkName(): un=%s:%zu,d_name=%s:%d\n", un, unlen,
dirbuf.d_name,
dirbuf.d_namlen);
#endif
len = dirbuf.d_namlen;
if (unlen != len)
return -2;
for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
c1 = unix2winchr(__DECONST(const u_char **, &np), &len,
LCASE_BASE, pmp);
c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
if (c1 != c2)
return -2;
}
return chksum;
}
int
win2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum,
struct msdosfsmount *pmp)
{
u_char *c, tmpbuf[5];
uint8_t *cp;
uint8_t *np, name[WIN_CHARS * 3 + 1];
uint16_t code;
int i;
if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
|| !(wep->weCnt&WIN_CNT))
return -1;
if (wep->weCnt&WIN_LAST) {
chksum = wep->weChksum;
} else if (chksum != wep->weChksum)
chksum = -1;
if (chksum == -1)
return -1;
np = name;
for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
code = (cp[1] << 8) | cp[0];
switch (code) {
case 0:
*np = '\0';
if (mbnambuf_write(nbp, name,
(wep->weCnt & WIN_CNT) - 1) != 0)
return -1;
return chksum;
case '/':
*np = '\0';
return -1;
default:
c = win2unixchr(tmpbuf, code, pmp);
while (*c != '\0')
*np++ = *c++;
break;
}
cp += 2;
}
for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
code = (cp[1] << 8) | cp[0];
switch (code) {
case 0:
*np = '\0';
if (mbnambuf_write(nbp, name,
(wep->weCnt & WIN_CNT) - 1) != 0)
return -1;
return chksum;
case '/':
*np = '\0';
return -1;
default:
c = win2unixchr(tmpbuf, code, pmp);
while (*c != '\0')
*np++ = *c++;
break;
}
cp += 2;
}
for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
code = (cp[1] << 8) | cp[0];
switch (code) {
case 0:
*np = '\0';
if (mbnambuf_write(nbp, name,
(wep->weCnt & WIN_CNT) - 1) != 0)
return -1;
return chksum;
case '/':
*np = '\0';
return -1;
default:
c = win2unixchr(tmpbuf, code, pmp);
while (*c != '\0')
*np++ = *c++;
break;
}
cp += 2;
}
*np = '\0';
if (mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1) != 0)
return -1;
return chksum;
}
uint8_t
winChksum(uint8_t *name)
{
int i;
uint8_t s;
for (s = 0, i = 11; --i >= 0; s += *name++)
s = (s << 7)|(s >> 1);
return (s);
}
int
winSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp)
{
size_t wlen;
char wn[WIN_MAXLEN * 2 + 1], *wnp;
unlen = winLenFixup(un, unlen);
if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
wlen = WIN_MAXLEN * 2;
wnp = wn;
msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
if (unlen > 0)
return 0;
return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
}
if (unlen > WIN_MAXLEN)
return 0;
return howmany(unlen, WIN_CHARS);
}
size_t
winLenFixup(const u_char *un, size_t unlen)
{
for (un += unlen; unlen > 0; unlen--)
if (*--un != ' ' && *un != '.')
break;
return unlen;
}
static int
mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
{
char *outp, outstr[outlen * weight + 1];
if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
outp = outstr;
outlen *= weight;
msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
return (inlen);
}
(*instr) += min(inlen, outlen);
return (inlen - min(inlen, outlen));
}
static u_char *
dos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
{
u_char c, *outp, *outp1;
size_t i, len, olen;
outp = outbuf;
if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
olen = len = 4;
outp1 = outp;
if (lower & (LCASE_BASE | LCASE_EXT))
msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
ilen, (char **)&outp, &olen, KICONV_LOWER);
else
msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
ilen, (char **)&outp, &olen);
for (i = 0; i < outp - outp1; i++) {
if (outp1[i] == '/')
outp1[i] = '?';
}
len -= olen;
if (len == 0) {
(*ilen)--;
(*instr)++;
*outp++ = '?';
}
} else {
(*ilen)--;
c = *(*instr)++;
c = dos2unix[c];
if (lower & (LCASE_BASE | LCASE_EXT))
c = u2l[c];
if (c == '/')
c = '?';
*outp++ = c;
outbuf[1] = '\0';
}
*outp = '\0';
outp = outbuf;
return (outp);
}
static uint16_t
unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
{
u_char c;
char *up, *outp, unicode[3], outbuf[3];
uint16_t wc;
size_t len, ucslen, unixlen, olen;
if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
ucslen = 2;
len = *ilen;
up = unicode;
msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
ilen, &up, &ucslen);
unixlen = len - *ilen;
if (unixlen == 0) {
(*ilen)--;
(*instr)++;
return (0);
}
if (unixlen == 1) {
c = *(*instr -1);
if (! (c & 0x80)) {
c = unix2dos[c];
if (c <= 2)
return (c);
}
}
*instr -= unixlen;
*ilen = len;
olen = len = 2;
outp = outbuf;
msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
ilen, &outp, &olen, KICONV_FROM_UPPER);
len -= olen;
if (len == 0) {
(*ilen) -= unixlen;
(*instr) += unixlen;
return (1);
}
wc = 0;
while(len--)
wc |= (*(outp - len - 1) & 0xff) << (len << 3);
return (wc);
}
(*ilen)--;
c = *(*instr)++;
c = l2u[c];
c = unix2dos[c];
return ((uint16_t)c);
}
static u_char *
win2unixchr(u_char *outbuf, uint16_t wc, struct msdosfsmount *pmp)
{
u_char *inp, *outp, inbuf[3];
size_t ilen, olen, len;
outp = outbuf;
if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
inbuf[0] = (u_char)(wc>>8);
inbuf[1] = (u_char)wc;
inbuf[2] = '\0';
ilen = 2;
olen = len = 4;
inp = inbuf;
msdosfs_iconv->convchr(pmp->pm_w2u, __DECONST(const char **,
&inp), &ilen, (char **)&outp, &olen);
len -= olen;
if (len == 0)
*outp++ = '?';
} else {
*outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff);
}
*outp = '\0';
outp = outbuf;
return (outp);
}
static uint16_t
unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
{
u_char *outp, outbuf[3];
uint16_t wc;
size_t olen;
if (*ilen == 0)
return (0);
if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
outp = outbuf;
olen = 2;
if (lower & (LCASE_BASE | LCASE_EXT))
msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
ilen, (char **)&outp, &olen,
KICONV_FROM_LOWER);
else
msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
ilen, (char **)&outp, &olen);
if (olen == 2)
return (0);
wc = (outbuf[0]<<8) | outbuf[1];
return (wc);
}
(*ilen)--;
wc = (*instr)[0];
if (lower & (LCASE_BASE | LCASE_EXT))
wc = u2l[wc];
(*instr)++;
return (wc);
}
void
mbnambuf_init(struct mbnambuf *nbp)
{
nbp->nb_len = 0;
nbp->nb_last_id = -1;
nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0';
}
int
mbnambuf_write(struct mbnambuf *nbp, char *name, int id)
{
char *slot;
size_t count, newlen;
if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) {
#ifdef MSDOSFS_DEBUG
printf("msdosfs: non-decreasing id: id %d, last id %d\n",
id, nbp->nb_last_id);
#endif
return (EINVAL);
}
slot = &nbp->nb_buf[id * WIN_CHARS];
count = strlen(name);
newlen = nbp->nb_len + count;
if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) {
#ifdef MSDOSFS_DEBUG
printf("msdosfs: file name length %zu too large\n", newlen);
#endif
return (ENAMETOOLONG);
}
if (count > WIN_CHARS && nbp->nb_len != 0) {
if ((id * WIN_CHARS + count + nbp->nb_len) >
sizeof(nbp->nb_buf))
return (ENAMETOOLONG);
memmove(slot + count, slot + WIN_CHARS, nbp->nb_len);
}
memcpy(slot, name, count);
nbp->nb_len = newlen;
nbp->nb_last_id = id;
return (0);
}
char *
mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp)
{
if (nbp->nb_len > sizeof(dp->d_name) - 1) {
mbnambuf_init(nbp);
return (NULL);
}
memcpy(dp->d_name, &nbp->nb_buf[0], nbp->nb_len);
dp->d_name[nbp->nb_len] = '\0';
dp->d_namlen = nbp->nb_len;
mbnambuf_init(nbp);
return (dp->d_name);
}