HelenOS sources
This source file includes following definitions.
- fat_dentry_namecmp
- fat_dentry_name_get
- fat_dentry_name_set
- fat_dentry_vollabel_get
- fat_classify_dentry
- fat_dentry_chksum
- fat_lfn_str_nlength
- fat_lfn_size
- fat_lfn_get_entry
- fat_lfn_set_entry
- str_to_ascii
- fat_valid_name
- fat_valid_short_name
#include "fat_dentry.h"
#include <ctype.h>
#include <str.h>
#include <errno.h>
#include <byteorder.h>
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
int fat_dentry_namecmp(char *name, const char *component)
{
int rc;
size_t size;
if (!(rc = str_casecmp(name, component)))
return rc;
if (!str_chr(name, '.')) {
size = str_size(name);
name[size] = '.';
name[size + 1] = '\0';
rc = str_casecmp(name, component);
}
return rc;
}
void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
{
unsigned int i;
for (i = 0; i < FAT_NAME_LEN; i++) {
if (d->name[i] == FAT_PAD)
break;
if (d->name[i] == FAT_DENTRY_E5_ESC)
*buf++ = 0xe5;
else {
if (d->lcase & FAT_LCASE_LOWER_NAME)
*buf++ = tolower(d->name[i]);
else
*buf++ = d->name[i];
}
}
if (d->ext[0] != FAT_PAD)
*buf++ = '.';
for (i = 0; i < FAT_EXT_LEN; i++) {
if (d->ext[i] == FAT_PAD) {
*buf = '\0';
return;
}
if (d->ext[i] == FAT_DENTRY_E5_ESC)
*buf++ = 0xe5;
else {
if (d->lcase & FAT_LCASE_LOWER_EXT)
*buf++ = tolower(d->ext[i]);
else
*buf++ = d->ext[i];
}
}
*buf = '\0';
}
void fat_dentry_name_set(fat_dentry_t *d, const char *name)
{
unsigned int i;
const char fake_ext[] = " ";
bool lower_name = true;
bool lower_ext = true;
for (i = 0; i < FAT_NAME_LEN; i++) {
switch ((uint8_t) *name) {
case 0xe5:
d->name[i] = FAT_DENTRY_E5_ESC;
name++;
break;
case '\0':
case '.':
d->name[i] = FAT_PAD;
break;
default:
if (isalpha(*name)) {
if (!islower(*name))
lower_name = false;
}
d->name[i] = toupper(*name++);
break;
}
}
if (*name++ != '.')
name = fake_ext;
for (i = 0; i < FAT_EXT_LEN; i++) {
switch ((uint8_t) *name) {
case 0xe5:
d->ext[i] = FAT_DENTRY_E5_ESC;
name++;
break;
case '\0':
d->ext[i] = FAT_PAD;
break;
default:
if (isalpha(*name)) {
if (!islower(*name))
lower_ext = false;
}
d->ext[i] = toupper(*name++);
break;
}
}
if (lower_name)
d->lcase |= FAT_LCASE_LOWER_NAME;
else
d->lcase &= ~FAT_LCASE_LOWER_NAME;
if (lower_ext)
d->lcase |= FAT_LCASE_LOWER_EXT;
else
d->lcase &= ~FAT_LCASE_LOWER_EXT;
}
void fat_dentry_vollabel_get(const fat_dentry_t *d, char *buf)
{
unsigned int i;
for (i = 0; i < FAT_NAME_LEN; i++) {
if (d->name[i] == FAT_PAD)
break;
if (d->name[i] == FAT_DENTRY_E5_ESC)
*buf++ = 0xe5;
else
*buf++ = d->name[i];
}
for (i = 0; i < FAT_EXT_LEN; i++) {
if (d->ext[i] == FAT_PAD) {
*buf = '\0';
return;
}
if (d->ext[i] == FAT_DENTRY_E5_ESC)
*buf++ = 0xe5;
else
*buf++ = d->ext[i];
}
*buf = '\0';
}
fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
{
if (d->attr == FAT_ATTR_LFN) {
if (FAT_LFN_ORDER(d) & FAT_LFN_ERASED)
return FAT_DENTRY_FREE;
else
return FAT_DENTRY_LFN;
}
if (d->attr & FAT_ATTR_VOLLABEL) {
return FAT_DENTRY_VOLLABEL;
}
if (d->name[0] == FAT_DENTRY_ERASED) {
return FAT_DENTRY_FREE;
}
if (d->name[0] == FAT_DENTRY_UNUSED) {
return FAT_DENTRY_LAST;
}
if (d->name[0] == FAT_DENTRY_DOT) {
return FAT_DENTRY_SKIP;
}
return FAT_DENTRY_VALID;
}
uint8_t fat_dentry_chksum(uint8_t *name)
{
uint8_t i, sum = 0;
for (i = 0; i < (FAT_NAME_LEN + FAT_EXT_LEN); i++)
sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) + name[i];
return sum;
}
size_t fat_lfn_str_nlength(const unaligned_uint16_t *str, size_t size)
{
size_t offset = 0;
while (offset < size) {
if (str[offset] == 0 || str[offset] == FAT_LFN_PAD)
break;
offset++;
}
return offset;
}
size_t fat_lfn_size(const fat_dentry_t *d)
{
size_t size = 0;
size += fat_lfn_str_nlength(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE);
size += fat_lfn_str_nlength(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE);
size += fat_lfn_str_nlength(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE);
return size;
}
size_t fat_lfn_get_entry(const fat_dentry_t *d, uint16_t *dst, size_t *offset)
{
int i;
for (i = FAT_LFN_PART3_SIZE - 1; i >= 0 && *offset > 0; i--) {
if (d->lfn.part3[i] == 0 || d->lfn.part3[i] == FAT_LFN_PAD)
continue;
(*offset)--;
dst[(*offset)] = uint16_t_le2host(d->lfn.part3[i]);
}
for (i = FAT_LFN_PART2_SIZE - 1; i >= 0 && *offset > 0; i--) {
if (d->lfn.part2[i] == 0 || d->lfn.part2[i] == FAT_LFN_PAD)
continue;
(*offset)--;
dst[(*offset)] = uint16_t_le2host(d->lfn.part2[i]);
}
for (i = FAT_LFN_PART1_SIZE - 1; i >= 0 && *offset > 0; i--) {
if (d->lfn.part1[i] == 0 || d->lfn.part1[i] == FAT_LFN_PAD)
continue;
(*offset)--;
dst[(*offset)] = uint16_t_le2host(d->lfn.part1[i]);
}
return *offset;
}
size_t fat_lfn_set_entry(const uint16_t *src, size_t *offset, size_t size,
fat_dentry_t *d)
{
size_t idx;
for (idx = 0; idx < FAT_LFN_PART1_SIZE; idx++) {
if (*offset < size) {
d->lfn.part1[idx] = host2uint16_t_le(src[*offset]);
(*offset)++;
} else
d->lfn.part1[idx] = FAT_LFN_PAD;
}
for (idx = 0; idx < FAT_LFN_PART2_SIZE; idx++) {
if (*offset < size) {
d->lfn.part2[idx] = host2uint16_t_le(src[*offset]);
(*offset)++;
} else
d->lfn.part2[idx] = FAT_LFN_PAD;
}
for (idx = 0; idx < FAT_LFN_PART3_SIZE; idx++) {
if (*offset < size) {
d->lfn.part3[idx] = host2uint16_t_le(src[*offset]);
(*offset)++;
} else
d->lfn.part3[idx] = FAT_LFN_PAD;
}
if (src[*offset] == 0)
offset++;
FAT_LFN_ATTR(d) = FAT_ATTR_LFN;
d->lfn.type = 0;
d->lfn.firstc_lo = 0;
return *offset;
}
void str_to_ascii(char *dst, const char *src, size_t count, uint8_t pad)
{
char32_t ch;
size_t off = 0;
size_t i = 0;
while (i < count) {
if ((ch = str_decode(src, &off, STR_NO_LIMIT)) != 0) {
if (ascii_check(ch) && IS_D_CHAR(ch))
*dst = toupper(ch);
else
*dst = pad;
} else
break;
dst++;
i++;
}
*dst = '\0';
}
bool fat_valid_name(const char *name)
{
char32_t ch;
size_t offset = 0;
bool result = true;
while ((ch = str_decode(name, &offset, STR_NO_LIMIT)) != 0) {
if (str_chr(FAT_STOP_CHARS, ch) != NULL) {
result = false;
break;
}
}
return result;
}
bool fat_valid_short_name(const char *name)
{
unsigned int i;
unsigned int dot = 0;
bool dot_found = false;
for (i = 0; name[i]; i++) {
if (name[i] == '.') {
if (dot_found) {
return false;
} else {
dot_found = true;
dot = i;
}
} else {
if (!IS_D_CHAR(name[i]))
return false;
}
}
if (dot_found) {
if (dot > FAT_NAME_LEN)
return false;
if (i - dot > FAT_EXT_LEN + 1)
return false;
} else {
if (i > FAT_NAME_LEN)
return false;
}
return true;
}
HelenOS homepage, sources at GitHub