1/*
2  Copyright (c) 1990-2009 Info-ZIP.  All rights reserved.
3
4  See the accompanying file LICENSE, version 2009-Jan-02 or later
5  (the contents of which are also included in unzip.h) for terms of use.
6  If, for some reason, all these files are missing, the Info-ZIP license
7  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
8*/
9/*---------------------------------------------------------------------------
10
11  wcemain.c
12
13  ---------------------------------------------------------------------------*/
14
15#define __WCEMAIN_C     /* identifies this source module */
16#define UNZIP_INTERNAL
17#include "unzip.h"      /* includes, typedefs, macros, prototypes, etc. */
18
19
20int WINAPI WinMain( HINSTANCE hInstance,
21                    HINSTANCE hPrevInstance,
22                    LPTSTR    lpCmdLine,
23                    int       nCmdShow)
24{
25    int r;
26    int i;
27    LPTSTR argArray[10];
28    int argBuffSize = lstrlen(lpCmdLine) + 1;
29    void* argBuff = malloc(argBuffSize);
30    char* argv[10];
31
32
33    /* Parse the command line into an argument array */
34    int argc = 0;
35    LPTSTR argPtr = lpCmdLine;
36    LPTSTR nextParam = NULL;
37    LPTSTR closingQuote = NULL;
38    unsigned short* endOfCmdLine = lpCmdLine + lstrlen(lpCmdLine);
39    TCHAR Blank = _T(' ');
40
41    /* Init the arrays */
42    for (i = 0; i < 10;i++)
43        argArray[i];
44    for (i = 0;i < 10;i++)
45        argv[i] = NULL;
46
47
48    /* Create the argument array, we have to convert this from wchar
49     * (unicode) to mbcs (single byte ascii)
50     */
51    while(argPtr != NULL)
52    {
53        /* Look for the first non-blank character */
54        while((memcmp(argPtr, &Blank, sizeof(TCHAR)) == 0) &&
55              (argPtr < endOfCmdLine))
56        {
57            argPtr++;
58        }
59
60        /* Check for quote enclosed strings for extended file names. */
61        if (argPtr[0] == _T('"') &&
62            /* Look for closing quote */
63            (closingQuote = _tcschr(argPtr + 1, _T('"'))) != NULL)
64        {
65            /* Clear the enclosing quotes */
66            *argPtr++ = _T('\0');
67            *closingQuote = _T('\0');
68            nextParam = closingQuote + 1;
69        }
70        else
71        {
72            nextParam = argPtr;
73        }
74
75        /* Set the parameter */
76        argArray[argc] = argPtr;
77        argc++;
78
79        /* Look for the next blank */
80        argPtr = _tcschr(nextParam, _T(' '));
81        if (argPtr != NULL)
82        {
83            /* Terminate the parameter and
84               point after the blank to the keyword */
85            *argPtr++ = _T('\0');
86        }
87    }
88    /* Add one to the arg count for null terminator. */
89    argc++;
90
91#ifndef UNICODE
92    /* No need to convert the parameters */
93    argv = (char*)argArray;
94#else
95    /* Unicode so we need to convert the parameters to ascii. */
96    /* Get the storage we need to hold the converted data */
97    if (argBuff != NULL)
98    {
99
100        int i;
101        char* ptrArgBuff;
102
103        /* Clear the asci argument buffer */
104        memset(argBuff,'\0',argBuffSize);
105        /* Command line parameters give ? */
106        if (argBuffSize == 0)
107        {
108            /* No so just set the first argumen in the array to
109             * the buffer */
110            argv[0] = argBuff;
111        }
112        else
113        {
114            argv[0] = (char*)_T("UnzipCE.exe");
115            /* We have some storage build an asci version of
116             * the command parameters */
117            ptrArgBuff = (char*)argBuff;
118            for(i = 0; i < (argc - 1); i++)
119            {
120                /* convert the data */
121                int paramLength = lstrlen(argArray[i]);
122                int bytesWritten =
123                    WideCharToMultiByte(CP_ACP, 0,
124                                        argArray[i], paramLength,
125                                        ptrArgBuff, paramLength,
126                                        NULL, NULL);
127                if (bytesWritten == 0)
128                {
129                    /* Conversion failed ser return value and exit loop */
130                    r = (int)GetLastError();
131                    break;
132                }
133                else
134                {
135                    argv[i + 1] = ptrArgBuff;
136                    /* Point to the next area of the buffer */
137                    ptrArgBuff = ptrArgBuff + bytesWritten + 1;
138                }
139            }
140        }
141    }
142#endif /* UNICODE */
143    return MAIN(argc, argv);
144}
145