OSDN Git Service

Add #include guard for version.h.
[android-x86/external-exfat.git] / libexfat / utils.c
index 77c8d24..dccb0be 100644 (file)
@@ -1,11 +1,22 @@
 /*
- *  utils.c
- *  exFAT file system implementation library.
- *
- *  Created by Andrew Nayenko on 04.09.09.
- *  This software is distributed under the GNU General Public License 
- *  version 3 or any later.
- */
+       utils.c (04.09.09)
+       exFAT file system implementation library.
+
+       Copyright (C) 2009, 2010  Andrew Nayenko
+
+       This program is free software: you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation, either version 3 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "exfat.h"
 #include <string.h>
@@ -58,62 +69,45 @@ static const time_t days_in_year[] =
        0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334
 };
 
-union exfat_date
-{
-       uint16_t raw;
-       struct
-       {
-               uint16_t day   : 5; /* 1-31 */
-               uint16_t month : 4; /* 1-12 */
-               uint16_t year  : 7; /* 1-127 (+1980) */
-       };
-};
-
-union exfat_time
-{
-       uint16_t raw;
-       struct
-       {
-               uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
-               uint16_t min    : 6; /* 0-59 */
-               uint16_t hour   : 5; /* 0-23 */
-       };
-};
-
 time_t exfat_exfat2unix(le16_t date, le16_t time)
 {
-       union exfat_date edate;
-       union exfat_time etime;
        time_t unix_time = EPOCH_DIFF_SEC;
+       uint16_t ndate = le16_to_cpu(date);
+       uint16_t ntime = le16_to_cpu(time);
+
+       uint16_t day    = ndate & 0x1f;     /* 5 bits, 1-31 */
+       uint16_t month  = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
+       uint16_t year   = ndate >> 9;       /* 7 bits, 1-127 (+1980) */
 
-       edate.raw = le16_to_cpu(date);
-       etime.raw = le16_to_cpu(time);
+       uint16_t twosec = ntime & 0x1f;     /* 5 bits, 0-29 (2 sec granularity) */
+       uint16_t min    = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
+       uint16_t hour   = ntime >> 11;      /* 5 bits, 0-23 */
 
-       if (edate.day == 0 || edate.month == 0 || edate.month > 12)
+       if (day == 0 || month == 0 || month > 12)
        {
                exfat_error("bad date %hu-%02hu-%02hu",
-                               edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
+                               year + EXFAT_EPOCH_YEAR, month, day);
                return 0;
        }
-       if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
+       if (hour > 23 || min > 59 || twosec > 29)
        {
                exfat_error("bad time %hu:%02hu:%02hu",
-                       etime.hour, etime.min, etime.twosec * 2);
+                               hour, min, twosec * 2);
                return 0;
        }
 
        /* every 4th year between 1904 and 2096 is leap */
-       unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
-       unix_time += days_in_year[edate.month] * SEC_IN_DAY;
+       unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
+       unix_time += days_in_year[month] * SEC_IN_DAY;
        /* if it's leap year and February has passed we should add 1 day */
-       if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
+       if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
                unix_time += SEC_IN_DAY;
-       unix_time += (edate.day - 1) * SEC_IN_DAY;
+       unix_time += (day - 1) * SEC_IN_DAY;
 
-       unix_time += etime.hour * SEC_IN_HOUR;
-       unix_time += etime.min * SEC_IN_MIN;
+       unix_time += hour * SEC_IN_HOUR;
+       unix_time += min * SEC_IN_MIN;
        /* exFAT represents time with 2 sec granularity */
-       unix_time += etime.twosec * 2;
+       unix_time += twosec * 2;
 
        /* exFAT stores timestamps in local time, so we correct it to UTC */
        unix_time += timezone;
@@ -123,9 +117,9 @@ time_t exfat_exfat2unix(le16_t date, le16_t time)
 
 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
 {
-       union exfat_date edate;
-       union exfat_time etime;
        time_t shift = EPOCH_DIFF_SEC + timezone;
+       uint16_t day, month, year;
+       uint16_t twosec, min, hour;
        int days;
        int i;
 
@@ -136,28 +130,29 @@ void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
        unix_time -= shift;
 
        days = unix_time / SEC_IN_DAY;
-       edate.year = (4 * days) / (4 * 365 + 1);
-       days -= edate.year * 365 + LEAP_YEARS(edate.year);
+       year = (4 * days) / (4 * 365 + 1);
+       days -= year * 365 + LEAP_YEARS(year);
+       month = 0;
        for (i = 1; i <= 12; i++)
        {
-               int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
-               int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
+               int leap_day = (IS_LEAP_YEAR(year) && i == 2);
+               int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
 
                if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
                {
-                       edate.month = i;
+                       month = i;
                        days -= days_in_year[i] + leap_sub;
                        break;
                }
        }
-       edate.day = days + 1;
+       day = days + 1;
 
-       etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
-       etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
-       etime.twosec = (unix_time % SEC_IN_MIN) / 2;
+       hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
+       min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
+       twosec = (unix_time % SEC_IN_MIN) / 2;
 
-       *date = cpu_to_le16(edate.raw);
-       *time = cpu_to_le16(etime.raw);
+       *date = cpu_to_le16(day | (month << 5) | (year << 9));
+       *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
 }
 
 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
@@ -166,7 +161,7 @@ void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
                exfat_bug("failed to convert name to UTF-8");
 }
 
-uint16_t exfat_start_checksum(const struct exfat_file* entry)
+uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
 {
        uint16_t sum = 0;
        int i;
@@ -186,8 +181,8 @@ uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
        return sum;
 }
 
-le16_t exfat_calc_checksum(const struct exfat_file* meta1,
-               const struct exfat_file_info* meta2, const le16_t* name)
+le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
+               const struct exfat_entry_meta2* meta2, const le16_t* name)
 {
        uint16_t checksum;
        const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
@@ -197,7 +192,7 @@ le16_t exfat_calc_checksum(const struct exfat_file* meta1,
        checksum = exfat_add_checksum(meta2, checksum);
        for (i = 0; i < name_entries; i++)
        {
-               struct exfat_file_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
+               struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
                memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
                                EXFAT_ENAME_MAX * sizeof(le16_t));
                checksum = exfat_add_checksum(&name_entry, checksum);