Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
mime_decompiler.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  * mime_decompiler.c - decompiling application/vnd.wap.multipart.*
59  * to multipart/ *
60  *
61  * This is a header for Mime decompiler for decompiling binary mime
62  * format to text mime format, which is used for transmitting POST
63  * data from mobile terminal to decrease the use of the bandwidth.
64  *
65  * See comments below for explanations on individual functions.
66  *
67  * Bruno Rodrigues
68  */
69 
70 #include <time.h>
71 #include <unistd.h>
72 #include <sys/types.h>
73 #include <sys/stat.h>
74 #include <fcntl.h>
75 #include <string.h>
76 #include <math.h>
77 #include <ctype.h>
78 
79 #include "gwlib/gwlib.h"
80 #include "wap/wsp.h"
81 #include "wap/wsp_headers.h"
82 #include "wap/wsp_strings.h"
83 #include "mime_decompiler.h"
84 
85 /***********************************************************************
86  * Declarations of data types.
87  */
88 
89 int mime_decompile(Octstr *binary_mime, Octstr **mime)
90 {
91  char *boundary = "kannel_boundary";
93  long mime_parts;
94  long i, j;
95  unsigned long headers_len, data_len;
96 
97  i = mime_parts = headers_len = data_len = 0;
98 
99  debug("wap.wsp.multipart.form.data", 0, "MIMEDEC: begining decoding");
100 
101  if(binary_mime == NULL || octstr_len(binary_mime) < 1) {
102  warning(0, "MIMEDEC: invalid mime, ending");
103  return -1;
104  }
105  *mime = octstr_create("");
106 
107  /* already dumped in deconvert_content
108  debug("mime", 0, "MMSDEC: binary mime dump:");
109  octstr_dump(binary_mime, 0);
110  */
111 
112  context = parse_context_create(binary_mime);
113  debug("mime", 0, "MIMEDEC: context created");
114 
115  mime_parts = parse_get_uintvar(context);
116  debug("mime", 0, "MIMEDEC: mime has %ld multipart entities", mime_parts);
117  if(mime_parts == 0) {
118  debug("mime", 0, "MIMEDEC: mime has none multipart entities, ending");
119  return 0;
120  }
121 
122  while(parse_octets_left(context) > 0) {
123  Octstr *headers, *data;
124  List *gwlist_headers;
125  i++;
126 
127  octstr_append(*mime, octstr_imm("--"));
128  octstr_append(*mime, octstr_imm(boundary));
129  octstr_append(*mime, octstr_imm("\n"));
130 
131  headers_len = parse_get_uintvar(context);
132  data_len = parse_get_uintvar(context);
133  debug("mime", 0, "MIMEDEC[%ld]: headers length <0x%02lx>, "
134  "data length <0x%02lx>", i, headers_len, data_len);
135 
136  if((headers = parse_get_octets(context, headers_len)) != NULL) {
137  gwlist_headers = wsp_headers_unpack(headers, 1);
138  for(j=0; j<gwlist_len(gwlist_headers);j++) {
139  octstr_append(*mime, gwlist_get(gwlist_headers, j));
140  octstr_append(*mime, octstr_imm("\n"));
141  }
142  } else {
143  error(0, "MIMEDEC[%ld]: headers length is out of range, ending", i);
144  return -1;
145  }
146 
147  if((data = parse_get_octets(context, data_len)) != NULL ||
148  (i = mime_parts && /* XXX SE-T610 eats last byte, which is generally null */
149  (data = parse_get_octets(context, data_len - 1)) != NULL)) {
150  debug("mime", 0, "MMSDEC[%ld]: body [%s]", i, octstr_get_cstr(data));
151  octstr_append(*mime, octstr_imm("\n"));
152  octstr_append(*mime, data);
153  octstr_append(*mime, octstr_imm("\n"));
154  } else {
155  error(0, "MIMEDEC[%ld]: data length is out of range, ending", i);
156  return -1;
157  }
158  }
159  octstr_append(*mime, octstr_imm("--"));
160  octstr_append(*mime, octstr_imm(boundary));
161  octstr_append(*mime, octstr_imm("--\n"));
162 
163  /* already dumped in deconvert_content
164  debug("mime", 0, "MMSDEC: text mime dump:");
165  octstr_dump(*mime, 0);
166  */
167 
168  return 0;
169 }
170 
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: parse.c:65
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static char * boundary
Definition: test_ppg.c:97
int mime_decompile(Octstr *binary_mime, Octstr **mime)
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Octstr * parse_get_octets(ParseContext *context, long length)
Definition: parse.c:230
void warning(int err, const char *fmt,...)
Definition: log.c:624
#define octstr_create(cstr)
Definition: octstr.h:125
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
List * wsp_headers_unpack(Octstr *headers, int content_type_present)
Definition: wsp_headers.c:1331
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
unsigned long parse_get_uintvar(ParseContext *context)
Definition: parse.c:246
ParseContext * parse_context_create(Octstr *str)
Definition: parse.c:74
Definition: list.c:102
long parse_octets_left(ParseContext *context)
Definition: parse.c:159
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.