os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/sl_utl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "sl_std.h"
    17 
    18 //-----------------------------------------------------------------------------
    19 
    20 TTime DosTimeToTTime(TInt aDosTime,TInt aDosDate)
    21 //
    22 //	Deciphers the dos time/date entry information and converts to TTime
    23 //
    24 	{
    25 	TInt secMask=0x1F;
    26 	TInt minMask=0x07E0;
    27 	TInt hrMask=0xF800;
    28 	TInt dayMask=0x1F;
    29 	TInt monthMask=0x01E0;
    30 	TInt yearMask=0xFE00;
    31 
    32 	TInt secs=(aDosTime&secMask)*2;
    33 	TInt mins=(aDosTime&minMask)>>5;
    34 	TInt hrs=(aDosTime&hrMask)>>11;
    35 	TInt days=(aDosDate&dayMask)-1;
    36 	TMonth months=(TMonth)(((aDosDate&monthMask)>>5)-1);
    37 	TInt years=((aDosDate&yearMask)>>9)+1980;
    38 	
    39 	TDateTime datetime;
    40 	TInt ret=datetime.Set(years,months,days,hrs,mins,secs,0);
    41 	if (ret==KErrNone)
    42 		return(TTime(datetime));
    43 	return(TTime(0));
    44 	}
    45 
    46 TInt DosTimeFromTTime(const TTime& aTime)
    47 //
    48 // Converts a TTime to a dos time
    49 //
    50 	{
    51 	TDateTime dateTime=aTime.DateTime();
    52 	TInt dosSecs=dateTime.Second()/2;
    53 	TInt dosMins=dateTime.Minute()<<5;
    54 	TInt dosHrs=dateTime.Hour()<<11;
    55 	return dosSecs|dosMins|dosHrs;
    56 	}
    57 
    58 TInt DosDateFromTTime(const TTime& aTime)
    59 //
    60 // Converts a TTime to a dos date
    61 //
    62 	{
    63 
    64 	TDateTime dateTime=aTime.DateTime();
    65 	TInt dosDays=dateTime.Day()+1;
    66 	TInt dosMonths=(dateTime.Month()+1)<<5;
    67 	TInt dosYears=(dateTime.Year()-1980)<<9;
    68 	return dosDays|dosMonths|dosYears;
    69 	}
    70 
    71 TBuf8<12> DosNameToStdFormat(const TDesC8& aDosName)
    72 //
    73 // Converts xxx.yyy to standard format aaaaaaaayyy
    74 //
    75 	{
    76 
    77 	__ASSERT_DEBUG(aDosName.Length()>=0 && aDosName.Length()<=12,Fault(EFatBadDosFormatName));
    78 	TBuf8<12> result;
    79 	Mem::Fill((TUint8*)result.Ptr(),result.MaxSize(),' ');
    80 	TInt dotPos=aDosName.Locate('.');
    81 	if (dotPos==KErrNotFound)
    82 		{
    83 		result=aDosName;
    84 		result.SetLength(11);
    85 		return result;
    86 		}
    87 	result=aDosName.Left(dotPos);
    88 	result.SetLength(11);
    89 	TPtr8 ext(&result[8],3);
    90 	ext=aDosName.Right(aDosName.Length()-dotPos-1);
    91 	return result;
    92 	}
    93 
    94 TBuf8<12> DosNameFromStdFormat(const TDesC8& aStdFormatName)
    95 //
    96 // Converts aaaaaaaayyy to dos name format xxx.yyy
    97 //
    98 	{
    99 
   100 	__ASSERT_DEBUG(aStdFormatName.Length()==11,Fault(EFatBadStdFormatName));
   101 	TBuf8<12> result;
   102 	TInt nameLen=aStdFormatName.Locate(' ');
   103 	if (nameLen>8 || nameLen==KErrNotFound)
   104 		nameLen=8;
   105 	result=aStdFormatName.Left(nameLen);
   106 	TPtrC8 ext(&aStdFormatName[8],3);
   107 	TInt extLen=ext.Locate(' ');
   108 	if (extLen)
   109 		result.Append(TChar('.'));
   110 	if (extLen==KErrNotFound)
   111 		extLen=3;
   112 	result.Append(ext.Left(extLen));
   113     if(result.Length() && result[0]==0x05 )
   114 	    {
   115 	    result[0]=0xE5;
   116 	    }
   117 	return result;
   118 	}
   119 
   120 TInt NumberOfVFatEntries(TInt aNameLength)
   121 //
   122 // Return the number of VFat entries required to describe a filename of length aNameLength
   123 //
   124 	{
   125 	TInt numberOfEntries=0;
   126 	if (aNameLength%KMaxVFatEntryName)
   127 		aNameLength++;	//	Include a zero terminator
   128 //	If aNameLength is a exact multiple of KMaxVFatEntryName, don't bother
   129 //	with a zero terminator - it just adds an unnecessary directory entry		
   130 	
   131 	numberOfEntries=(1+(aNameLength/KMaxVFatEntryName));	
   132 	
   133 	if (aNameLength%KMaxVFatEntryName)
   134 		numberOfEntries++;
   135 	
   136 	return(numberOfEntries);
   137 	}
   138 
   139 //-----------------------------------------------------------------------------
   140 /** 
   141     Calculate DOS short name checksum
   142     @param aShortName short name descriptor (must be at least 11 bytes long)
   143     @return checksum
   144 */
   145 TUint8 CalculateShortNameCheckSum(const TDesC8& aShortName)
   146     {
   147 
   148     ASSERT(aShortName.Length() >= KFatDirNameSize);
   149     const TUint8* pName = aShortName.Ptr();
   150 
   151     const TUint32 w0 = ((const TUint32*)pName)[0];
   152     const TUint32 w1 = ((const TUint32*)pName)[1];
   153 
   154     TUint32 chkSum = w0 & 0xFF;
   155     
   156     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w0 << 16) >> 24));
   157     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w0 << 8)  >> 24));
   158     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ( w0 >> 24));
   159 
   160     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + (w1) & 0xFF);
   161     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w1 << 16) >> 24));
   162     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w1 << 8)  >> 24));
   163     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ( w1 >> 24));
   164 
   165     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[8]);
   166     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[9]);
   167     chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[10]);
   168 
   169     return (TUint8)chkSum;
   170     }
   171 
   172 
   173 
   174 
   175 
   176