00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <time.h>
00071 #include <unistd.h>
00072 #include <sys/types.h>
00073 #include <sys/stat.h>
00074 #include <fcntl.h>
00075 #include <string.h>
00076 #include <math.h>
00077 #include <ctype.h>
00078
00079 #include "gwlib/gwlib.h"
00080 #include "wap/wsp.h"
00081 #include "wap/wsp_headers.h"
00082 #include "wap/wsp_strings.h"
00083 #include "mime_decompiler.h"
00084
00085
00086
00087
00088
00089 int mime_decompile(Octstr *binary_mime, Octstr **mime)
00090 {
00091 char *boundary = "kannel_boundary";
00092 ParseContext *context;
00093 long mime_parts;
00094 long i, j;
00095 unsigned long headers_len, data_len;
00096
00097 i = mime_parts = headers_len = data_len = 0;
00098
00099 debug("wap.wsp.multipart.form.data", 0, "MIMEDEC: begining decoding");
00100
00101 if(binary_mime == NULL || octstr_len(binary_mime) < 1) {
00102 warning(0, "MIMEDEC: invalid mime, ending");
00103 return -1;
00104 }
00105 *mime = octstr_create("");
00106
00107
00108
00109
00110
00111
00112 context = parse_context_create(binary_mime);
00113 debug("mime", 0, "MIMEDEC: context created");
00114
00115 mime_parts = parse_get_uintvar(context);
00116 debug("mime", 0, "MIMEDEC: mime has %ld multipart entities", mime_parts);
00117 if(mime_parts == 0) {
00118 debug("mime", 0, "MIMEDEC: mime has none multipart entities, ending");
00119 return 0;
00120 }
00121
00122 while(parse_octets_left(context) > 0) {
00123 Octstr *headers, *data;
00124 List *gwlist_headers;
00125 i++;
00126
00127 octstr_append(*mime, octstr_imm("--"));
00128 octstr_append(*mime, octstr_imm(boundary));
00129 octstr_append(*mime, octstr_imm("\n"));
00130
00131 headers_len = parse_get_uintvar(context);
00132 data_len = parse_get_uintvar(context);
00133 debug("mime", 0, "MIMEDEC[%ld]: headers length <0x%02lx>, "
00134 "data length <0x%02lx>", i, headers_len, data_len);
00135
00136 if((headers = parse_get_octets(context, headers_len)) != NULL) {
00137 gwlist_headers = wsp_headers_unpack(headers, 1);
00138 for(j=0; j<gwlist_len(gwlist_headers);j++) {
00139 octstr_append(*mime, gwlist_get(gwlist_headers, j));
00140 octstr_append(*mime, octstr_imm("\n"));
00141 }
00142 } else {
00143 error(0, "MIMEDEC[%ld]: headers length is out of range, ending", i);
00144 return -1;
00145 }
00146
00147 if((data = parse_get_octets(context, data_len)) != NULL ||
00148 (i = mime_parts &&
00149 (data = parse_get_octets(context, data_len - 1)) != NULL)) {
00150 debug("mime", 0, "MMSDEC[%ld]: body [%s]", i, octstr_get_cstr(data));
00151 octstr_append(*mime, octstr_imm("\n"));
00152 octstr_append(*mime, data);
00153 octstr_append(*mime, octstr_imm("\n"));
00154 } else {
00155 error(0, "MIMEDEC[%ld]: data length is out of range, ending", i);
00156 return -1;
00157 }
00158 }
00159 octstr_append(*mime, octstr_imm("--"));
00160 octstr_append(*mime, octstr_imm(boundary));
00161 octstr_append(*mime, octstr_imm("--\n"));
00162
00163
00164
00165
00166
00167
00168 return 0;
00169 }
00170
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.