Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wmlsdasm.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wmlsdasm.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Disassembler for WMLScript byte-code.
67  *
68  */
69 
70 #include "wsint.h"
71 #include "gwlib/gwlib.h"
72 
73 #include <sys/stat.h>
74 
75 /* TODO:
76  - print pragmas
77  - option to disassemble only a named external function
78  - print constants in assembler => wsasm.c */
79 
80 /********************* Prototypes for static functions ******************/
81 
82 /* Print usage message to the stdout. */
83 static void usage(void);
84 
85 /* Lookup the name of the function `index' from the function names
86  section of the byte-code structure `bc'. The function returns NULL
87  if the function is internal. */
88 const char *lookup_function(WsBc *bc, WsUInt8 index);
89 
90 /********************* Static variables *********************************/
91 
92 /* The name of the compiler program. */
93 static char *program;
94 
95 /********************* Global functions *********************************/
96 
97 int main(int argc, char *argv[])
98 {
99  int i;
100  int opt;
101  WsBool print_constants = WS_FALSE;
102  WsBool print_function_names = WS_FALSE;
103  WsBool print_functions = WS_FALSE;
104  WsUInt8 k;
105  WsUInt16 j;
106 
107  program = strrchr(argv[0], '/');
108  if (program)
109  program++;
110  else
111  program = argv[0];
112 
113  /* Process command line arguments. */
114  while ((opt = getopt(argc, argv, "cfnh")) != EOF) {
115  switch (opt) {
116  case 'c':
117  print_constants = WS_TRUE;
118  break;
119 
120  case 'f':
121  print_functions = WS_TRUE;
122  break;
123 
124  case 'n':
125  print_function_names = WS_TRUE;
126  break;
127 
128  case 'h':
129  usage();
130  exit(0);
131  break;
132 
133  case '?':
134  printf("Try `%s -h' for a complete list of options.\n",
135  program);
136  exit(1);
137  }
138  }
139 
140  for (i = optind; i < argc; i++) {
141  FILE *fp;
142  struct stat stat_st;
143  unsigned char *data;
144  size_t data_len;
145  WsBc *bc;
146 
147  if (stat(argv[i], &stat_st) < 0) {
148  fprintf(stderr, "%s: could not access `%s': %s\n",
149  program, argv[i], strerror(errno));
150  exit(1);
151  }
152  data_len = stat_st.st_size;
153 
154  data = ws_malloc(data_len);
155  if (data == NULL) {
156  fprintf(stderr, "%s: out of memory: %s\n",
157  program, strerror(errno));
158  exit(1);
159  }
160 
161  fp = fopen(argv[i], "rb");
162  if (fp == NULL) {
163  fprintf(stderr, "%s: could not open input file `%s': %s\n",
164  program, argv[i], strerror(errno));
165  exit(1);
166  }
167 
168  if (fread(data, 1, data_len, fp) != data_len) {
169  fprintf(stderr, "%s: could not read file `%s': %s\n",
170  program, argv[i], strerror(errno));
171  exit(1);
172  }
173  fclose(fp);
174 
175  /* Decode byte-code. */
176  bc = ws_bc_decode(data, data_len);
177  if (bc == NULL) {
178  fprintf(stderr, "%s: invalid byte-code file `%s'\n",
179  program, argv[i]);
180  continue;
181  }
182 
183  /* Print all requested data. */
184  printf("\n%s:\t%lu bytes\n\n", argv[i], (unsigned long) data_len);
185 
186  if (print_constants) {
187  printf("Disassembly of section Constants:\n\n");
188  for (j = 0; j < bc->num_constants; j++) {
189  printf("%4d:\t", j);
190  switch (bc->constants[j].type) {
192  printf("%ld\n", (long) bc->constants[j].u.v_int);
193  break;
194 
196  printf("%f\n", bc->constants[j].u.v_float);
197  break;
198 
200  printf("NaN\n");
201  break;
202 
204  printf("+infinity\n");
205  break;
206 
208  printf("-infinity\n");
209  break;
210 
212  {
213  size_t pos = 0;
214  size_t column = 8;
215  unsigned long ch;
216 
217  printf("\"");
218 
219  while (ws_utf8_get_char(&bc->constants[j].u.v_string,
220  &ch, &pos)) {
221  if (ch < ' ') {
222  printf("\\%02lx", ch);
223  column += 3;
224  } else if (ch <= 0xff) {
225  printf("%c", (unsigned char) ch);
226  column++;
227  } else {
228  printf("\\u%04lx", ch);
229  column += 5;
230  }
231  if (column >= 75) {
232  printf("\"\n\t\"");
233  column = 8;
234  }
235  }
236  printf("\"\n");
237  }
238  break;
239 
241  printf("\"\"\n");
242  break;
243  }
244  }
245  }
246 
247  if (print_function_names) {
248  printf("Disassembly of section Function names:\n\n");
249  for (k = 0; k < bc->num_function_names; k++)
250  printf(" %-40.40s%8d\n",
251  bc->function_names[k].name,
252  bc->function_names[k].index);
253  }
254 
255  if (print_functions) {
256  WsCompilerPtr compiler = ws_create(NULL);
257 
258  printf("Disassembly of section Functions:\n");
259 
260  for (k = 0; k < bc->num_functions; k++) {
261  const char *name = lookup_function(bc, k);
262 
263  printf("\nFunction %u", (unsigned int) k);
264 
265  if (name)
266  printf(" <%s>", name);
267 
268  printf(":\n");
269 
270  ws_asm_dasm(compiler, bc->functions[k].code,
271  bc->functions[k].code_size);
272  }
273 
274  ws_destroy(compiler);
275  }
276 
277  if (!print_constants && !print_function_names && !print_functions) {
278  printf("Sections:\n\
279  Name\t\t Items\n\
280  Constants\t%8d\n\
281  Pragmas\t%8d\n\
282  Function names\t%8d\n\
283  Functions\t%8d\n",
284  bc->num_constants,
285  bc->num_pragmas,
286  bc->num_function_names,
287  bc->num_functions);
288  }
289 
290  ws_bc_free(bc);
291  }
292 
293  return 0;
294 }
295 
296 /********************* Static functions *********************************/
297 
298 static void usage(void)
299 {
300  printf("Usage: %s OPTION... FILE...\n\
301  \n\
302  -c print constants\n\
303  -f disassemble functions\n\
304  -n print function names\n\
305  -h print this help message and exit successfully\n\
306  \n",
307  program);
308 }
309 
310 
311 const char *lookup_function(WsBc *bc, WsUInt8 index)
312 {
313  WsUInt8 i;
314 
315  for (i = 0; i < bc->num_function_names; i++)
316  if (bc->function_names[i].index == index)
317  return bc->function_names[i].name;
318 
319  return NULL;
320 }
WsBcConstantType type
Definition: wsbc.h:121
WsUInt16 num_constants
Definition: wsbc.h:194
Definition: wsint.h:131
static char * program
Definition: wmlsdasm.c:93
WsBcFunction * functions
Definition: wsbc.h:207
WsInt32 v_int
Definition: wsbc.h:125
int optind
Definition: attgetopt.c:80
WsCompilerPtr ws_create(WsCompilerParams *params)
Definition: ws.c:135
WsUInt8 num_functions
Definition: wsbc.h:206
WsBcConstant * constants
Definition: wsbc.h:195
WsBcFunctionName * function_names
Definition: wsbc.h:204
WsUtf8String v_string
Definition: wsbc.h:127
WsFloat v_float
Definition: wsbc.h:126
static void usage(void)
Definition: wmlsdasm.c:298
unsigned char WsUInt8
Definition: wsint.h:116
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
WsUInt8 num_function_names
Definition: wsbc.h:203
void ws_destroy(WsCompilerPtr compiler)
Definition: ws.c:163
unsigned short WsUInt16
Definition: wsint.h:119
void ws_bc_free(WsBc *bc)
Definition: wsbc.c:97
WsUInt32 code_size
Definition: wsbc.h:179
int main(int argc, char *argv[])
Definition: wmlsdasm.c:97
void ws_asm_dasm(WsCompilerPtr compiler, const unsigned char *code, size_t len)
Definition: wsasm.c:185
char * name
Definition: smsc_cimd2.c:212
int ws_utf8_get_char(const WsUtf8String *string, unsigned long *ch_return, size_t *posp)
Definition: wsutf8.c:293
WsBool
Definition: wsint.h:128
WsUInt16 num_pragmas
Definition: wsbc.h:198
union WsBcConstantRec::@116 u
unsigned char * code
Definition: wsbc.h:180
Definition: wsbc.h:186
const char * lookup_function(WsBc *bc, WsUInt8 index)
Definition: wmlsdasm.c:311
void * ws_malloc(size_t size)
Definition: wsalloc.c:77
WsBc * ws_bc_decode(const unsigned char *data, size_t data_len)
Definition: wsbc.c:457
WsUInt8 index
Definition: wsbc.h:165
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.