1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/securityanddataprivacytools/securitytools/certapp/utils/utils.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,176 @@
1.4 +/*
1.5 +* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <fstream>
1.23 +#include "utils.h"
1.24 +#include "logger.h"
1.25 +
1.26 +#ifndef __LINUX__
1.27 +#include <windows.h>
1.28 +#endif
1.29 +
1.30 +typedef unsigned char TUint8;
1.31 +typedef unsigned int TUint;
1.32 +
1.33 +#ifdef __LINUX__
1.34 +void OpenUtf8FStreamForRead(std::fstream &aFStream, const char *aUtf8FileName)
1.35 +{
1.36 + aFStream.open(aUtf8FileName, std::ios_base::binary | std::ios_base::in);
1.37 +}
1.38 +
1.39 +void OpenUtf8FStreamForWrite(std::fstream &aFStream, const char *aUtf8FileName)
1.40 +{
1.41 + aFStream.open(aUtf8FileName, std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
1.42 +}
1.43 +
1.44 +#else
1.45 +
1.46 +WCHAR *WindowsUtf8ToUtf16(const char *aUtf8)
1.47 +{
1.48 + int utf8Len = strlen(aUtf8) + 1; // We want to copy the terminator to the new string
1.49 + int utf16Space = MultiByteToWideChar(CP_UTF8, 0, aUtf8, utf8Len, 0, 0);
1.50 + if(utf16Space <= 0)
1.51 + {
1.52 + dbg << Log::Indent() << "Failed to convert UTF-8 '" << aUtf8 << "' to UTF16" << Log::Endl();
1.53 + FatalError();
1.54 + return 0;
1.55 + }
1.56 + // prog << Log::Indent() << "utf16Space " << utf16Space << Log::Endl();
1.57 + WCHAR *utf16 = new WCHAR[utf16Space];
1.58 + (void) MultiByteToWideChar(CP_UTF8, 0, aUtf8, utf8Len, utf16, utf16Space);
1.59 +#if 0
1.60 + prog << Log::Indent() << "Raw UTF16 name is (chars " << utf16Space << ")" << Log::Endl();
1.61 + for(int i=0; i<utf16Space*2; ++i)
1.62 + {
1.63 + prog << TUint(((TUint8 *)utf16)[i]) << Log::Endl();
1.64 + }
1.65 +#endif
1.66 + return utf16;
1.67 +}
1.68 +
1.69 +CHAR *WindowsUtf16ToUtf8(const WCHAR *aUtf16)
1.70 +{
1.71 + int utf16Len = wcslen(aUtf16) + 1; // We want to copy the terminator to the new string
1.72 + int utf8Space = WideCharToMultiByte(CP_UTF8, 0, aUtf16, utf16Len, 0, 0, 0, 0);
1.73 + if(utf8Space <= 0)
1.74 + {
1.75 + dbg << Log::Indent() << "Failed to convert UTF16 string to UTF8" << Log::Endl();
1.76 + FatalError();
1.77 + return 0;
1.78 + }
1.79 + //prog << Log::Indent() << "utf8Space " << utf8Space << Log::Endl();
1.80 + CHAR *utf8 = new CHAR[utf8Space];
1.81 + (void) WideCharToMultiByte(CP_UTF8, 0, aUtf16, utf16Len, utf8, utf8Space, 0, 0);
1.82 +
1.83 +#if 0
1.84 + prog << Log::Indent() << "Raw UTF8 name is (chars " << utf8Space << ")" << Log::Endl();
1.85 + for(int i=0; i<utf8Space; ++i)
1.86 + {
1.87 + prog << TUint(((TUint8 *)utf8)[i]) << Log::Endl();
1.88 + }
1.89 +#endif
1.90 + return utf8;
1.91 +}
1.92 +
1.93 +WCHAR *WindowsRetrieveDosName(const WCHAR *aLongName)
1.94 +{
1.95 + // Convert to a short DOS file name. File MUST already exist!
1.96 + // len will be space required for string and terminator
1.97 + DWORD len = GetShortPathNameW((WCHAR *)aLongName, 0, 0);
1.98 + if( len <= 0 )
1.99 + {
1.100 + prog << Log::Indent() << "Failed to find existing file and retrieve short name" << Log::Endl();
1.101 + return 0;
1.102 + }
1.103 +
1.104 + //prog << Log::Indent() << "space required for short name is " << len << Log::Endl();
1.105 + WCHAR *shortName = new WCHAR[len];
1.106 + (void) GetShortPathNameW(aLongName, shortName, len);
1.107 + return shortName;
1.108 +}
1.109 +
1.110 +void OpenUtf8FStreamForRead(std::fstream &aFStream, const char *aUtf8FileName)
1.111 +{
1.112 + // Unfortunately the windows C++ STL library will not open files specified via a UTF-8 filename.
1.113 + // The workaround is to convert the UTF-8 name to the short DOS compatible name and open that instead
1.114 +
1.115 + // First convert the UTF-8 name to UTF-16 for use with the windows APIs
1.116 + WCHAR *utf16Name = WindowsUtf8ToUtf16(aUtf8FileName);
1.117 +
1.118 + // Obtain short DOS compatible name for file
1.119 + WCHAR *utf16NameDos = WindowsRetrieveDosName(utf16Name);
1.120 + if(utf16NameDos == 0)
1.121 + {
1.122 + aFStream.setstate(std::ios_base::badbit);
1.123 + delete [] utf16Name;
1.124 + return;
1.125 + }
1.126 +
1.127 + char *utf8NameDos = WindowsUtf16ToUtf8(utf16NameDos);
1.128 +
1.129 + aFStream.open(utf8NameDos, std::ios_base::binary | std::ios_base::in);
1.130 +
1.131 + delete [] utf8NameDos;
1.132 + delete [] utf16NameDos;
1.133 + delete [] utf16Name;
1.134 + return;
1.135 +}
1.136 +
1.137 +void OpenUtf8FStreamForWrite(std::fstream &aFStream, const char *aUtf8FileName)
1.138 +{
1.139 + // Unfortunately the windows C++ STL library will not open files specified via a UTF-8 filename.
1.140 + // The workaround is to make sure the file already exists and then convert the UTF-8 name to the short DOS compatible name
1.141 + // and open that instead
1.142 +
1.143 + // First convert the UTF-8 name to UTF-16 for use with the windows APIs
1.144 + WCHAR *utf16Name = WindowsUtf8ToUtf16(aUtf8FileName);
1.145 +
1.146 + // Make sure the file exists
1.147 + HANDLE fh = CreateFileW((WCHAR *)utf16Name, GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
1.148 + if(fh == INVALID_HANDLE_VALUE)
1.149 + {
1.150 + dbg << Log::Indent() << "Failed to create file '" << aUtf8FileName << "'" << Log::Endl();
1.151 + aFStream.setstate(std::ios_base::badbit);
1.152 + delete [] utf16Name;
1.153 + return;
1.154 + }
1.155 + // Close handle
1.156 + CloseHandle(fh);
1.157 +
1.158 +
1.159 + // Obtain short DOS compatible name for file
1.160 + WCHAR *utf16NameDos = WindowsRetrieveDosName(utf16Name);
1.161 + if(utf16NameDos == 0)
1.162 + {
1.163 + aFStream.setstate(std::ios_base::badbit);
1.164 + delete [] utf16Name;
1.165 + return;
1.166 + }
1.167 +
1.168 + char *utf8NameDos = WindowsUtf16ToUtf8(utf16NameDos);
1.169 +
1.170 + aFStream.open(utf8NameDos, std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
1.171 +
1.172 + delete [] utf8NameDos;
1.173 + delete [] utf16NameDos;
1.174 + delete [] utf16Name;
1.175 + return;
1.176 +}
1.177 +#endif
1.178 +
1.179 +// End of file