1 /*
2  * user32.c -- implementation of routines in user32.dll
3  * (C)Copyright 2000 by Hiroshi Takekawa
4  * This file is part of Enfle.
5  *
6  * Last Modified: Wed Dec 26 08:35:16 2001.
7  * $Id: user32.c,v 1.1 2003/08/01 13:41:06 makeinu Exp $
8  *
9  * Enfle is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * Enfle is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21  */
22 
23 #include <stdlib.h>
24 #include <stdarg.h>
25 
26 #include "w32api.h"
27 #include "module.h"
28 
29 #include "converter.h"
30 #include "pe_image.h"
31 #include "user32.h"
32 
33 #define REQUIRE_STRING_H
34 #include "compat.h"
35 #include "common.h"
36 
37 DECLARE_W32API(void, unknown_symbol, (void));
38 DECLARE_W32API(BOOL, EnumThreadWindows, (DWORD, WNDENUMPROC, LPARAM));
39 DECLARE_W32API(INT, MessageBoxA, (HWND, LPCSTR, LPCSTR, UINT));
40 DECLARE_W32API(INT, LoadStringA, (HINSTANCE, UINT, LPSTR, INT));
41 DECLARE_W32API(INT, GetSystemMetrics, (INT));
42 DECLARE_W32API(INT, GetKeyboardType, (INT));
43 DECLARE_W32API(INT, wsprintfA, (LPSTR, LPCSTR, ...));
44 
45 static Symbol_info symbol_infos[] = {
46   { "EnumThreadWindows", EnumThreadWindows },
47   { "MessageBoxA", MessageBoxA },
48   { "LoadStringA", LoadStringA },
49   { "GetSystemMetrics", GetSystemMetrics },
50   { "GetKeyboardType", GetKeyboardType },
51   { "wsprintfA", wsprintfA },
52   { NULL, unknown_symbol }
53 };
54 
55 DEFINE_W32API(BOOL, EnumThreadWindows, (DWORD id, WNDENUMPROC func, LPARAM param))
56 {
57   debug_message_fn("(%d, %p) called\n", id, func);
58   return TRUE;
59 }
60 
61 DEFINE_W32API(INT, MessageBoxA, (HWND handle, LPCSTR text, LPCSTR title, UINT type))
62 {
63   debug_message_fn("(%s: %s) called\n", title, text);
64   return 1;
65 }
66 
67 DEFINE_W32API(INT, LoadStringA, (HINSTANCE handle, UINT id, LPSTR buffer, INT len))
68 {
69   PE_image *p = (PE_image *)handle;
70   unsigned int rid = ((id >> 4) & 0xffff) + 1;
71   unsigned int sid = id & 0xf;
72   //unsigned int size;
73   char buf[256];
74   void *d;
75 
76   debug_message_fn("(%p, %d(rid: %d, sid: %d), %p, %d) called\n", handle, id, rid, sid, buffer, len);
77 
78   if (len == 0 || buffer == NULL)
79     return 0;
80 
81   memset(buffer, 0, len);
82 
83   snprintf(buf, 256, "/0x6/0x%x/0x%x", rid, 0);
84   if ((d = hash_lookup_str(p->resource, buf)) == NULL) {
85     /* Japanese */
86     snprintf(buf, 256, "/0x6/0x%x/0x%x", rid, 0x411);
87     d = hash_lookup_str(p->resource, buf);
88   }
89 
90   if (d) {
91     unsigned char *data = d;
92     char *s, *dest;
93     unsigned int i;
94     int l, L;
95 
96     //size = (((((data[0] << 8) | data[1]) << 8) | data[2]) << 8) | data[3];
97     data += 4;
98     for (i = 0; i < sid; i++)
99       data += (*data + 1) << 1;
100     l = *data << 1;
101     s = calloc(1, l + 2);
102     memcpy(s, data + 2, l);
103     L = converter_convert(s, &dest, l, (char *)"UCS-2LE", (char *)"EUC-JP");
104     strncpy(buffer, dest, len);
105     free(dest);
106   }
107 
108   return strlen(buffer);
109 }
110 
111 DEFINE_W32API(INT, GetSystemMetrics, (INT i))
112 {
113   debug_message_fn("(%d) called\n", i);
114   return 0;
115 }
116 
117 DEFINE_W32API(INT, GetKeyboardType, (INT type))
118 {
119   debug_message_fn("(%d) called\n", type);
120 
121   switch (type) {
122   case 0:
123     return 4;
124   case 1:
125     return 0;
126   case 2:
127     return 12;
128   default:
129     return 0;
130   }
131 }
132 
133 DEFINE_W32API(INT, wsprintfA, (LPSTR buf, LPCSTR format, ...))
134 {
135   va_list va;
136 
137   va_start(va, format);
138   vsnprintf(buf, 1024, format, va);
139   va_end(va);
140 
141   debug_message_fn(": formatted: %s", buf);
142 
143   return strlen(buf);
144 }
145 
146 /* unimplemened */
147 
148 DEFINE_W32API(void, unknown_symbol, (void))
149 {
150   show_message("unknown symbol in user32 called\n");
151 }
152 
153 /* export */
154 
155 Symbol_info *
user32_get_export_symbols(void)156 user32_get_export_symbols(void)
157 {
158   module_register("user32.dll", symbol_infos);
159   return symbol_infos;
160 }
161