/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

/*
 * Copyright Notice
 * Copyright (c) Ericsson, IBM, Lotus, Matsushita Communication 
 * Industrial Co., Ltd., Motorola, Nokia, Openwave Systems, Inc., 
 * Palm, Inc., Psion, Starfish Software, Symbian, Ltd. (2001).
 * All Rights Reserved.
 * Implementation of all or part of any Specification may require 
 * licenses under third party intellectual property rights, 
 * including without limitation, patent rights (such a third party 
 * may or may not be a Supporter). The Sponsors of the Specification 
 * are not responsible and shall not be held responsible in any 
 * manner for identifying or failing to identify any or all such 
 * third party intellectual property rights.
 * 
 * THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN ARE PROVIDED 
 * ON AN "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND AND ERICSSON, IBM, 
 * LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO. LTD, MOTOROLA, 
 * NOKIA, PALM INC., PSION, STARFISH SOFTWARE AND ALL OTHER SYNCML 
 * SPONSORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION 
 * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 
 * SHALL ERICSSON, IBM, LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO., 
 * LTD, MOTOROLA, NOKIA, PALM INC., PSION, STARFISH SOFTWARE OR ANY 
 * OTHER SYNCML SPONSOR BE LIABLE TO ANY PARTY FOR ANY LOSS OF 
 * PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF 
 * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTAL, 
 * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH 
 * THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
 * 
 * The above notice and this paragraph must be included on all copies 
 * of this document that are made.
 * 
 */

/**
 * This file implements the platform-dependent code for the xpt routing layer.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef UNDER_CE
#include <errno.h>
#endif
#include <stdarg.h>

#include <xpt.h>
#include <xptTransport.h>

#define WIN32_LEAN_AND_MEAN
#define STRICT
#include <windows.h>

#include <utilities.h>

#include <tchar.h>
#ifdef UNDER_CE
	#define FILENAME_MAX MAX_PATH
	#define perror(x) ((void)0)
#endif

// Definte a string copy function that works appropriately under Unicode and single-byte environments
#ifdef UNICODE
   #define _mbstotcs(a,b,c)  MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, b, -1, a, c );
	#define _tcstombs(a,b,c)  WideCharToMultiByte( CP_ACP, 0, b, -1, a, c, NULL, NULL);
#else
   #define _mbstotcs(a,b,c)  strcpy(a,b)
	#define _tcstombs(a,b,c)  strcpy(a,b)
#endif

#define DESCRIBELASTWIN32ERROR(x)   describeWin32Error((x), GetLastError())

static void describeWin32Error(const char *msg, long errcode) {
   char *msgbuffer;
   DWORD rc = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
                            | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                            NULL, errcode, 0, (LPTSTR) &msgbuffer, 0, NULL);
   if (!rc) {
      fprintf(stderr, "Received error code %ld from FormatMessage() while"
                      " trying\nto format message for error code %ld:  %s\n",
                      GetLastError(), errcode, msg);
      return;
   }

   fprintf(stderr, "%s (code %ld):  %s", msg, errcode, msgbuffer);
   LocalFree(msgbuffer);
}

static int findPropertyFile(const TCHAR *tname, char *filename) {

   // We need local wide character buffer
   TCHAR tfilename[FILENAME_MAX+1];
   HANDLE hFile;

   memset(tfilename, 0, sizeof(tfilename));

   // First look in current directory
   _tcscpy(tfilename, tname);

   hFile = CreateFile(tfilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

   if (INVALID_HANDLE_VALUE != hFile) {
      CloseHandle(hFile);
      _tcstombs(filename, tfilename, FILENAME_MAX);
      // Ensure null-termination
      filename[FILENAME_MAX-1] = '\0';
      return 1;
   }

   // Now try the directory containing the executable program
   if (GetModuleFileName(NULL, tfilename, FILENAME_MAX)) {
      TCHAR *p = _tcsrchr(tfilename, _T('\\'));
      if (p && p+1-tfilename+_tcslen(tname) < FILENAME_MAX) {
         _tcscpy(p+1, tname);
         hFile = CreateFile(tfilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
         if (INVALID_HANDLE_VALUE != hFile) {
            CloseHandle(hFile);
            _tcstombs(filename, tfilename, FILENAME_MAX);
            // Ensure null-termination
            filename[FILENAME_MAX-1] = '\0';
            return 1;
         }
      }
   }

   return 0;      // Couldn't find the file
}

char **getLibrariesToLoad(void) {
   // Read the list of libraries to load from a "properties"-type file.
   char propertyFile[FILENAME_MAX];
   char line[FILENAME_MAX+1];
   FILE *file;

   char **list = NULL;
   size_t listLen  = 0;
   size_t listSize = 0;


   if (!findPropertyFile(TEXT("dynamicTransports"), propertyFile)) {
      fprintf(stderr, "Unable to find dynamic transport property file \"%s\"\n", "dynamicTransports");
      return NULL;
   }

   file = fopen(propertyFile, "r");
   if (!file) {
#ifdef UNDER_CE
      fprintf(stderr, "Error opening dynamic transport property file \"%s\"\n", propertyFile);
#else
      fprintf(stderr, "Error opening dynamic transport property file \"%s\": %s\n", propertyFile, strerror(errno));
#endif
      return NULL;
   }

   while (fgets(line, sizeof line - 1, file)) {
      size_t lineLen = strlen(line);
      if (line[lineLen-1] != '\n') {
         fprintf(stderr, "Line too long in dynamic transport property file: %s...\n", line);
         fclose(file);
         return NULL;
      }

      // Ignore comment lines and empty lines
      if (*line == '#' || *line == '\n') continue;

      // Replace trailing newline with a null
      line[lineLen-1] = '\0';
      // The lineLen variable now includes the trailing null character

      // Make sure list is big enough to hold this new entry
      if (listLen + 1 >= listSize) {
         listSize += 5;
         list = realloc(list, listSize * sizeof(char *));
         if (!list) {
            perror("Unable to allocate storage for dynamic library list");
            fclose(file);
            return NULL;
         }
      }

      // Get storage for this new list entry
      list[listLen] = malloc(lineLen);
      if (!list[listLen]) {
         size_t i;
         perror("Unable to allocate storage for dynamic library name");
         fclose(file);
         for (i=0; i<listLen; ++i) free(list[i]);
         free(list);
         return NULL;
      }

      // Copy to item into list
      memcpy(list[listLen++], line, lineLen);
   }

   fclose(file);

   if (!list) list = malloc(sizeof(char *));

   list[listLen] = NULL;   // list is definitely big enough for this entry

   return list;
}


void releaseLibraryList(char **list) {
   char **p = list;

   if (!list) return;

   while (*p) {
      free(*p++);
   }

   free(list);
}

void *loadLibrary(const char *libname) {

   HMODULE handle;

   // Convert libname to Unicode if needed
   TCHAR tcspath[FILENAME_MAX+1];

   memset(tcspath, 0, sizeof(tcspath));
   _mbstotcs(tcspath, libname, FILENAME_MAX);

   handle = LoadLibrary(tcspath);
   if (!handle) {
      DESCRIBELASTWIN32ERROR("LoadLibrary");
      return NULL;
   }

   return handle;
}

void unloadLibrary(void *handle) {
   if (!FreeLibrary((HMODULE) handle))
      DESCRIBELASTWIN32ERROR("FreeLibrary");
}

void *lookupLibrarySymbol(const char *symbol, int arguments, void *handle) {
   FARPROC symbolAddr;

// Convert symbol to Unicode if needed
   TCHAR tcssymbol[FILENAME_MAX+1];

   memset(tcssymbol, 0, sizeof(tcssymbol));
   _mbstotcs(tcssymbol, symbol, FILENAME_MAX);

   // Try looking up plain symbol first
   symbolAddr = GetProcAddress((HMODULE) handle, tcssymbol);

   if (!symbolAddr) {
      TCHAR symbolName[270];
      if (_tcslen(tcssymbol) > 256) return NULL;

      // Try with a leading underscore (the cdecl calling convention)
      _stprintf(symbolName, TEXT("_%s"), tcssymbol);

      symbolAddr = GetProcAddress((HMODULE) handle, symbolName);

      if (!symbolAddr) {
         // Try with a leading underscore and trailing decoration for the
         // stdcall calling convention (Microsoft version).
         _stprintf(symbolName, TEXT("_%s@%d"), tcssymbol, arguments * 4);
         symbolAddr = GetProcAddress((HMODULE) handle, symbolName);

         if (!symbolAddr) {
            // Try with a no underscore and trailing decoration for the
            // stdcall calling convention (gcc version).
            _stprintf(symbolName, TEXT("%s@%d"), tcssymbol, arguments * 4);
            symbolAddr = GetProcAddress((HMODULE) handle, symbolName);

            if (!symbolAddr) DESCRIBELASTWIN32ERROR("GetProcAddress");
         }
      }
   }

   return symbolAddr;
}

void localOutput(const char *format, va_list args) {
   vprintf(format, args);
}


