mms-dump.c 45.2 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1
/*
2
 * Copyright (C) 2013-2016 Jolla Ltd.
3
 * Contact: Slava Monich <slava.monich@jolla.com>
Slava Monich's avatar
Slava Monich committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <glib.h>
#include <wsputil.h>
#include <string.h>
#include <stdio.h>
19 20
#include <ctype.h>

Slava Monich's avatar
Slava Monich committed
21 22 23 24 25 26 27 28 29
static const char pname[] = "mms-dump";

enum app_ret_value {
    RET_OK,
    RET_ERR_CMDLINE,
    RET_ERR_IO,
    RET_ERR_DECODE
};

30 31
#define N_(a) G_N_ELEMENTS(a)

Slava Monich's avatar
Slava Monich committed
32
#define MMS_DUMP_FLAG_VERBOSE (0x01)
33
#define MMS_DUMP_FLAG_DATA    (0x02)
34 35 36 37 38
#define MMS_DUMP_FLAG_EXTRACT (0x04)

#define HEADER_CONTENT_DISPOSITION  "Content-Disposition"
#define PARAM_FILENAME              "Filename"
#define PARAM_NAME                  "Name"
Slava Monich's avatar
Slava Monich committed
39 40 41 42 43 44 45 46

#define WSP_QUOTE (127)

struct mms_named_value {
    const char* name;
    unsigned int value;
};

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
struct mms_header_param {
    char* name;
    char* value;
};

struct mms_header {
    char* name;
    char* value;
    char* dump;
    struct mms_header_param** params;
    unsigned int num_params;
};

struct mms_headers {
    struct mms_header** headers;
    unsigned int num_headers;
};

Slava Monich's avatar
Slava Monich committed
65 66
typedef gboolean
(*mms_value_decoder)(
67
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags);

#define MMS_CONTENT_TYPE  "application/vnd.wap.mms-message"

#define MMS_WELL_KNOWN_HEADERS(h) \
    h(BCC,                    "Bcc",                          0x01, etext    )\
    h(CC,                     "Cc",                           0x02, etext    )\
    h(CONTENT_LOCATION,       "X-Mms-Content-Location",       0x03, text     )\
    h(CONTENT_TYPE,           "Content-Type",                 0x04, unknown  )\
    h(DATE,                   "Date",                         0x05, date     )\
    h(DELIVERY_REPORT,        "X-Mms-Delivery-Report",        0x06, bool     )\
    h(DELIVERY_TIME,          "X-Mms-Delivery-Time",          0x07, unknown  )\
    h(EXPIRY,                 "X-Mms-Expiry",                 0x08, expiry   )\
    h(FROM,                   "From",                         0x09, from     )\
    h(MESSAGE_CLASS,          "X-Mms-Message-Class",          0x0A, mclass   )\
    h(MESSAGE_ID,             "Message-ID",                   0x0B, text     )\
    h(MESSAGE_TYPE,           "X-Mms-Message-Type",           0x0C, mtype    )\
    h(MMS_VERSION,            "X-Mms-MMS-Version",            0x0D, version  )\
    h(MESSAGE_SIZE,           "X-Mms-Message-Size",           0x0E, long     )\
    h(PRIORITY,               "X-Mms-Priority",               0x0F, prio     )\
    h(READ_REPORT,            "X-Mms-Read-Report",            0x10, bool     )\
    h(REPORT_ALLOWED,         "X-Mms-Report-Allowed",         0x11, bool     )\
93
    h(RESPONSE_STATUS,        "X-Mms-Response-Status",        0x12, respstat )\
Slava Monich's avatar
Slava Monich committed
94
    h(RESPONSE_TEXT,          "X-Mms-Response-Text",          0x13, etext    )\
95
    h(SENDER_VISIBILITY,      "X-Mms-Sender-Visibility",      0x14, visiblty )\
Slava Monich's avatar
Slava Monich committed
96 97 98 99 100 101 102 103 104 105 106
    h(STATUS,                 "X-Mms-Status",                 0x15, status   )\
    h(SUBJECT,                "Subject",                      0x16, etext    )\
    h(TO,                     "To",                           0x17, etext    )\
    h(TRANSACTION_ID,         "X-Mms-Transaction-Id",         0x18, text     )\
    h(RETRIEVE_STATUS,        "X-Mms-Retrieve-Status",        0x19, retrieve )\
    h(RETRIEVE_TEXT,          "X-Mms-Retrieve-Text",          0x1A, etext    )\
    h(READ_STATUS,            "X-Mms-Read-Status",            0x1B, rstatus  )\
    h(REPLY_CHARGING,         "X-Mms-Reply-Charging",         0x1C, short    )\
    h(REPLY_CHARGING_DEADLINE,"X-Mms-Reply-Charging-Deadline",0x1D, unknown  )\
    h(REPLY_CHARGING_ID,      "X-Mms-Reply-Charging-ID",      0x1E, text     )\
    h(REPLY_CHARGING_SIZE,    "X-Mms-Reply-Charging-Size",    0x1F, long     )\
107 108
    h(PREVIOUSLY_SENT_BY,     "X-Mms-Previously-Sent-By",     0x20, prevby   )\
    h(PREVIOUSLY_SENT_DATE,   "X-Mms-Previously-Sent-Date",   0x21, prevdate )
Slava Monich's avatar
Slava Monich committed
109 110 111

#define WSP_WELL_KNOWN_HEADERS(h) \
    h(CONTENT_LOCATION,       "Content-Location",             0x0E, text     )\
112
    h(CONTENT_DISPOSITION,    HEADER_CONTENT_DISPOSITION,     0x2E, contdisp )\
Slava Monich's avatar
Slava Monich committed
113
    h(CONTENT_ID,             "Content-ID",                   0x40, quote    )\
114
    h(CONTENT_DISPOSITION2,   HEADER_CONTENT_DISPOSITION,     0x45, contdisp )
Slava Monich's avatar
Slava Monich committed
115 116 117 118

#define MMS_HEADER_ID(id) MMS_HEADER_##id
#define MMS_PART_HEADER_ID(id) MMS_PART_HEADER_##id

119
typedef enum mms_header_id {
Slava Monich's avatar
Slava Monich committed
120 121 122 123 124 125 126
    MMS_HEADER_INVALID,
#define h(id,n,x,t) MMS_HEADER_ID(id) = x,
    MMS_WELL_KNOWN_HEADERS(h)
#undef h
    MMS_HEADER_END,
} MMS_HEADER;

127
typedef enum mms_part_header_id {
Slava Monich's avatar
Slava Monich committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#define h(id,n,x,t) MMS_PART_HEADER_ID(id) = x,
    WSP_WELL_KNOWN_HEADERS(h)
#undef h
} MMS_PART_HEADER;

static
const char*
mms_message_well_known_header_name(
    int hdr)
{
    static char unknown[6];
    switch (hdr) {
#define h(id,n,x,t) case MMS_HEADER_ID(id): return n;
    MMS_WELL_KNOWN_HEADERS(h)
#undef h
    default:
        snprintf(unknown, sizeof(unknown), "0x%02X", hdr);
        unknown[sizeof(unknown)-1] = 0;
        return unknown;
    }
}

static
const char*
mms_part_well_known_header_name(
    int hdr)
{
    static char unknown[6];
    switch (hdr) {
#define h(id,n,x,t) case MMS_PART_HEADER_ID(id): return n;
    WSP_WELL_KNOWN_HEADERS(h)
#undef h
    default:
        snprintf(unknown, sizeof(unknown), "0x%02X", hdr);
        unknown[sizeof(unknown)-1] = 0;
        return unknown;
    }
}

static
const struct mms_named_value*
mms_find_named_value(
    const struct mms_named_value* values,
    unsigned int num_values,
    unsigned int value)
{
    unsigned int i;
    for (i=0; i<num_values; i++) {
        if (value == values[i].value) {
            return values + i;
        }
    }
    return NULL;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static
struct mms_header*
mms_header_new(
    const char* name)
{
    struct mms_header* header = g_slice_new0(struct mms_header);
    header->name = g_strdup(name);
    return header;
}

static
struct mms_header*
mms_header_new_content_type()
{
    return mms_header_new("Content-Type");
}

Slava Monich's avatar
Slava Monich committed
200 201
static
void
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
mms_header_free(
    struct mms_header* header)
{
    if (header) {
        unsigned int i;
        for (i=0; i<header->num_params; i++) {
            struct mms_header_param* param = header->params[i];
            g_free(param->name);
            g_free(param->value);
            g_slice_free(struct mms_header_param, param);
        }
        g_free(header->name);
        g_free(header->value);
        g_free(header->dump);
        g_free(header->params);
        g_slice_free(struct mms_header, header);
    }
}

static
void
mms_headers_free(
    struct mms_headers* headers)
{
    if (headers) {
        unsigned int i;
        for (i=0; i<headers->num_headers; i++) {
            mms_header_free(headers->headers[i]);
        }
        g_free(headers->headers);
        g_slice_free(struct mms_headers, headers);
    }
}

static
void
mms_header_print(
    const struct mms_header* header,
    const char* prefix,
Slava Monich's avatar
Slava Monich committed
241 242
    unsigned int flags)
{
243
    if (header) {
Slava Monich's avatar
Slava Monich committed
244
        unsigned int i;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        printf("%s%s: ", prefix, header->name);
        if (header->value) printf("%s", header->value);
        for (i=0; i<header->num_params; i++) {
            struct mms_header_param* param = header->params[i];
            printf("; %s=%s", param->name, param->value);
        }
        if ((flags & MMS_DUMP_FLAG_VERBOSE) && header->dump) {
            printf(" (%s)", header->dump);
        }
        printf("\n");
    }
}

static
void
mms_headers_print(
    const struct mms_headers* headers,
    const char* prefix,
    unsigned int flags)
{
    if (headers) {
        unsigned int i;
        for (i=0; i<headers->num_headers; i++) {
            mms_header_print(headers->headers[i], prefix, flags);
        }
    }
}

static
const char*
mms_header_find(
    const struct mms_header* header,
    const char* name)
{
    if (header && name) {
        unsigned int i;
        for (i=0; i<header->num_params; i++) {
            const struct mms_header_param* param = header->params[i];
            if (!g_ascii_strcasecmp(param->name, name)) {
                return param->value;
            }
        }
    }
    return NULL;
}

static
const struct mms_header*
mms_headers_find(
    const struct mms_headers* headers,
    const char* name)
{
    if (headers && name) {
        unsigned int i;
        for (i=0; i<headers->num_headers; i++) {
            const struct mms_header* header = headers->headers[i];
            if (!g_ascii_strcasecmp(header->name, name)) {
                return header;
            }
        }
    }
    return NULL;
}

static
const char*
mms_headers_find_filename(
    const struct mms_header* content_type,
    const struct mms_headers* headers)
{
    const struct mms_header* disp = mms_headers_find(headers,
        HEADER_CONTENT_DISPOSITION);
    if (disp) {
        const char* filename = mms_header_find(disp, PARAM_FILENAME);
        if (filename) {
            return filename;
        }
    }
    return mms_header_find(content_type, PARAM_NAME);
}

static
void
mms_header_set_count_param(
    struct mms_header* header,
    unsigned int count)
{
    header->num_params = 1;
    header->params = g_new(struct mms_header_param*, header->num_params);
    header->params[0] = g_slice_new0(struct mms_header_param);
    header->params[0]->name = g_strdup("Count");
    header->params[0]->value = g_strdup_printf("%u", count);
}

static
char*
mms_hexdump(
    const guint8* val,
    unsigned int len)
{
    if (len > 0) {
        unsigned int i;
        GString* buf = g_string_new_len(NULL, len*3 - 1);
        g_string_append_printf(buf, "%02X", val[0]);
Slava Monich's avatar
Slava Monich committed
349
        for (i=1; i<len; i++) {
350
            g_string_append_printf(buf, " %02X", val[i]);
Slava Monich's avatar
Slava Monich committed
351
        }
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        return g_string_free(buf, FALSE);
    }
    return g_strdup("");
}

static
void
mms_header_dump(
    struct mms_header* header,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    if ((flags & MMS_DUMP_FLAG_VERBOSE) && len > 0) {
        header->dump = mms_hexdump(val, len);
Slava Monich's avatar
Slava Monich committed
367 368 369 370 371 372
    }
}

static
gboolean
mms_value_decode_unknown(
373
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
374 375 376 377 378 379 380
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    if (type == WSP_VALUE_TYPE_TEXT) {
        if (val[0] == WSP_QUOTE) val++;
381 382
        header->value = g_strdup((char*)val);
        mms_header_dump(header, val, len, flags);
Slava Monich's avatar
Slava Monich committed
383
    } else if (len == 1) {
384 385 386
        header->value = g_strdup_printf("0x%02X", val[0]);
        if (flags & MMS_DUMP_FLAG_VERBOSE) {
            header->dump = g_strdup_printf("%u", val[0]);
Slava Monich's avatar
Slava Monich committed
387
        }
388 389
    } else if (len > 1) {
        header->value = mms_hexdump(val, len);
Slava Monich's avatar
Slava Monich committed
390 391 392 393 394 395 396
    }
    return TRUE;
}

static
gboolean
mms_value_decode_quote(
397
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
398 399 400 401 402 403 404
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    /* Quoted-string = <Octet 34> *TEXT End-of-string */
    if (type == WSP_VALUE_TYPE_TEXT && len > 0 && val[0] == 0x22) {
405 406
        header->value = g_strdup((char*)val+1);
        mms_header_dump(header, val, len, flags);
Slava Monich's avatar
Slava Monich committed
407 408 409 410 411 412 413 414
        return TRUE;
    }
    return FALSE;
}

static
gboolean
mms_value_decode_enum(
415
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
416 417 418 419 420 421 422 423 424 425 426
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    const struct mms_named_value* values,
    unsigned int num_values,
    unsigned int flags)
{
    if (type == WSP_VALUE_TYPE_SHORT && len == 1) {
        const struct mms_named_value* nv;
        nv = mms_find_named_value(values, num_values, val[0]);
        if (nv) {
427 428 429 430
            header->value = g_strdup(nv->name);
            if (flags & MMS_DUMP_FLAG_VERBOSE) {
                header->dump = g_strdup_printf("%u", nv->value);
            }
Slava Monich's avatar
Slava Monich committed
431 432 433
            return TRUE;
        }
    }
434
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
435 436 437 438 439
}

static
gboolean
mms_value_decode_version(
440
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
441 442 443 444 445 446 447
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    if (type == WSP_VALUE_TYPE_SHORT && len == 1) {
        const unsigned int value = val[0];
448 449 450 451 452
        header->value = g_strdup_printf("%u.%u",
            (val[0] & 0x70) >> 4, val[0] & 0x0f);
        if (flags & MMS_DUMP_FLAG_VERBOSE) {
            header->dump = g_strdup_printf("%u", value);
        }
Slava Monich's avatar
Slava Monich committed
453 454
        return TRUE;
    }
455
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
456 457 458 459 460
}

static
gboolean
mms_value_decode_long(
461
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
462 463 464 465 466 467 468 469 470 471 472
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    unsigned long long_val;
    if (type == WSP_VALUE_TYPE_LONG && len <= sizeof(long_val)) {
        unsigned int i;
        for (long_val = 0, i=0; i<len; i++) {
            long_val = ((long_val << 8) | val[i]);
        }
473
        header->value = g_strdup_printf("%lu", long_val);
Slava Monich's avatar
Slava Monich committed
474 475
        return TRUE;
    }
476
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
477 478 479
}

static
480 481
char*
mms_value_format_date(
Slava Monich's avatar
Slava Monich committed
482 483 484 485
    time_t t)
{
    char date[64];
    strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%S%z", localtime(&t));
486
    return g_strdup(date);
Slava Monich's avatar
Slava Monich committed
487 488
}

489 490 491 492 493 494 495 496 497 498 499 500 501 502
static
gboolean
mms_value_decode_date_value(
    const guint8* val,
    unsigned int len,
    time_t* t)
{
    if (len <= sizeof(t)) {
        unsigned int i;
        for (*t=0, i=0; i<len; i++) {
            *t = (((*t) << 8) | val[i]);
        }
        return TRUE;
    }
503
    return FALSE;
504 505
}

Slava Monich's avatar
Slava Monich committed
506 507 508
static
gboolean
mms_value_decode_date(
509
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
510 511 512 513 514 515
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    time_t t;
516 517
    if (type == WSP_VALUE_TYPE_LONG &&
        mms_value_decode_date_value(val, len, &t)) {
518 519 520 521
        header->value = mms_value_format_date(t);
        if (flags & MMS_DUMP_FLAG_VERBOSE) {
            header->dump = g_strdup_printf("%lu", (unsigned long)t);
        }
Slava Monich's avatar
Slava Monich committed
522 523
        return TRUE;
    }
524
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
525 526 527 528 529
}

static
gboolean
mms_value_decode_bool(
530
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
531 532 533 534 535 536 537 538 539
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Yes", 128 },
        { "No",  129 }
    };
540
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
541 542 543 544 545 546
}

/* Message-type-value */
static
gboolean
mms_value_decode_mtype(
547
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "M-Send.req",         128 },
        { "M-Send.conf",        129 },
        { "M-Notification.ind", 130 },
        { "M-Notifyresp.ind",   131 },
        { "M-Retrieve.conf",    132 },
        { "M-Acknowledge.ind",  133 },
        { "M-Delivery.ind",     134 },
        { "M-Read-Rec.ind",     135 },
        { "M-Read-Orig.ind",    136 },
        { "M-Forward.req",      137 },
        { "M-Forward.conf",     138 }
    };
566
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
567 568 569 570 571 572
}

/* Message-class-value */
static
gboolean
mms_value_decode_mclass(
573
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
574 575 576 577 578 579 580 581 582 583 584
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Personal",      128 },
        { "Advertisement", 129 },
        { "Informational", 130 },
        { "Auto",          131 }
    };
585
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
586 587 588 589 590 591
}

/* Priority-value */
static
gboolean
mms_value_decode_prio(
592
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
593 594 595 596 597 598 599 600 601 602
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Low",    128 },
        { "Normal", 129 },
        { "High",   130 }
    };
603
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
604 605 606 607 608 609
}

/* Retrieve-status-value */
static
gboolean
mms_value_decode_retrieve(
610
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Ok",                                  128 },
        { "Error-transient-failure",             192 },
        { "Error-transient-message-not-found",   193 },
        { "Error-transient-network-problem",     194 },
        { "Error-permanent-failure",             224 },
        { "Error-permanent-service-denied",      225 },
        { "Error-permanent-message-not-found",   226 },
        { "Error-permanent-content-unsupported", 227 }
    };
626
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
627 628 629 630 631 632
}

/* Read-status-value */
static
gboolean
mms_value_decode_rstatus(
633
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
634 635 636 637 638 639 640 641 642
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Read",    128 },
        { "Deleted", 129 }
    };
643
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
644 645 646 647 648 649
}

/* Status-value */
static
gboolean
mms_value_decode_status(
650
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Expired",       128 },
        { "Retrieved",     129 },
        { "Rejected",      130 },
        { "Deferred",      131 },
        { "Unrecognised",  132 },
        { "Indeterminate", 133 },
        { "Forwarded",     134 }
    };
665
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
Slava Monich's avatar
Slava Monich committed
666 667
}

668 669 670 671
/* Response-status-value */
static
gboolean
mms_value_decode_respstat(
672
    struct mms_header* header,
673 674 675 676 677 678 679 680
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Ok",                                                  128 },
        { "Error-unspecified",                                   129 },
681
        { "Error-service-denied",                                130 },
682 683 684 685
        { "Error-message-format-corrupt",                        131 },
        { "Error-sending-address-unresolved",                    132 },
        { "Error-message-not-found",                             133 },
        { "Error-network-problem",                               134 },
686
        { "Error-content-not-accepted",                          135 },
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
        { "Error-unsupported-message",                           136 },
        { "Error-transient-failure",                             192 },
        { "Error-transient-sending-address-unresolved",          193 },
        { "Error-transient-message-not-found",                   194 },
        { "Error-transient-network-problem",                     195 },
        { "Error-transient-partial-success",                     196 },
        { "Error-permanent-failure",                             224 },
        { "Error-permanent-service-denied",                      225 },
        { "Error-permanent-message-format-corrupt",              226 },
        { "Error-permanent-sending-address-unresolved",          227 },
        { "Error-permanent-message-not-found",                   228 },
        { "Error-permanent-content-not-accepted",                229 },
        { "Error-permanent-reply-charging-limitations-not-met",  230 },
        { "Error-permanent-reply-charging-request-not-accepted", 231 },
        { "Error-permanent-reply-charging-forwarding-denied",    232 },
        { "Error-permanent-reply-charging-not-supported",        233 },
        { "Error-permanent-address-hiding-not-supported",        234 },
        { "Error-permanent-lack-of-prepaid",                     235 }
    };
706
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
707 708
}

709
/* Encoded-string-value */
Slava Monich's avatar
Slava Monich committed
710 711
static
gboolean
712
mms_value_decode_encoded_text(
713
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
714 715
    enum wsp_value_type type,
    const guint8* val,
716
    unsigned int len)
Slava Monich's avatar
Slava Monich committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
{
    /* http://www.iana.org/assignments/character-sets */
    static const struct mms_named_value nv [] = {
        { "US-ASCII",        3},
        { "ISO_8859-1",      4},
        { "ISO_8859-2",      5},
        { "ISO_8859-3",      6},
        { "ISO_8859-4",      7},
        { "ISO_8859-5",      8},
        { "ISO_8859-6",      9},
        { "ISO_8859-7",      10},
        { "ISO_8859-8",      11},
        { "ISO_8859-9",      12},
        { "ISO-8859-10",     13},
        { "Shift_JIS",       17},
        { "EUC-JP",          18},
733
        { "CP949",           36},
Slava Monich's avatar
Slava Monich committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
        { "ISO-2022-KR",     37},
        { "EUC-KR",          38},
        { "ISO-2022-JP",     39},
        { "ISO-2022-JP-2",   40},
        { "ISO_8859-6-E",    81},
        { "ISO_8859-6-I",    82},
        { "ISO_8859-8-E",    84},
        { "ISO_8859-8-I",    85},
        { "UTF-8",           106},
        { "ISO-8859-13",     109},
        { "ISO-8859-14",     110},
        { "ISO-8859-15",     111},
        { "ISO-8859-16",     112},
        { "GBK",             113},
        { "GB18030",         114},
        { "ISO-10646-UCS-2", 1000},
        { "ISO-10646-UCS-4", 1001},
        { "ISO-10646-J-1",   1004},
        { "UTF-7",           1012},
        { "UTF-16BE",        1013},
        { "UTF-16LE",        1014},
        { "UTF-16",          1015},
        { "UTF-32",          1017},
        { "UTF-32BE",        1018},
        { "UTF-32LE",        1019},
        { "GB2312",          2025},
        { "Big5",            2026},
        { "macintosh",       2027},
        { "KOI8-R",          2084},
        { "windows-874",     2109},
        { "windows-1250",    2250},
        { "windows-1251",    2251},
        { "windows-1252",    2252},
        { "windows-1253",    2253},
        { "windows-1254",    2254},
        { "windows-1255",    2255},
        { "windows-1256",    2256},
        { "windows-1257",    2257},
        { "windows-1258",    2258}
    };

    if (type == WSP_VALUE_TYPE_TEXT) {
776
        header->value = g_strdup((char*)val);
Slava Monich's avatar
Slava Monich committed
777 778 779 780 781 782
        return TRUE;
    } else if (type == WSP_VALUE_TYPE_LONG) {
        unsigned int charset = 0;
        unsigned int consumed = 0;
        if (wsp_decode_integer(val, len, &charset, &consumed)) {
            const struct mms_named_value* cs;
783
            cs = mms_find_named_value(nv, N_(nv), charset);
Slava Monich's avatar
Slava Monich committed
784 785 786 787 788 789 790 791 792 793 794 795 796
            if (cs) {
                char* tmp = NULL;
                const char* text = NULL;
                const char* dest_charset = "UTF-8";
                if (strcmp(dest_charset, cs->name)) {
                    gsize bytes_in, bytes_out;
                    text = tmp = g_convert((char*)val+consumed, len-consumed,
                        dest_charset, cs->name, &bytes_in, &bytes_out, NULL);
                } else {
                    if (val[consumed] == WSP_QUOTE) consumed++;
                    if (consumed <= len) text = (char*)val+consumed;
                }
                if (text) {
797
                    header->value = g_strdup(text);
Slava Monich's avatar
Slava Monich committed
798 799 800 801 802 803
                    g_free(tmp);
                    return TRUE;
                }
            }
        }
    }
804 805 806 807 808 809
    return FALSE;
}

static
gboolean
mms_value_decode_etext(
810
    struct mms_header* header,
811 812 813 814 815
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
816 817
    if (mms_value_decode_encoded_text(header, type, val, len)) {
        mms_header_dump(header, val, len, flags);
818 819
        return TRUE;
    } else {
820
        return mms_value_decode_unknown(header, type, val, len, flags);
821
    }
Slava Monich's avatar
Slava Monich committed
822 823
}

824 825 826 827
/* Sender-visibility-value */
static
gboolean
mms_value_decode_visiblty(
828
    struct mms_header* header,
829 830 831 832 833 834 835 836 837
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    static const struct mms_named_value nv [] = {
        { "Hide", 128 },
        { "Show", 129 },
    };
838
    return mms_value_decode_enum(header, type, val, len, nv, N_(nv), flags);
839 840 841
}

/* From-value */
Slava Monich's avatar
Slava Monich committed
842 843 844
static
gboolean
mms_value_decode_from(
845
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
846 847 848 849 850 851 852 853 854 855 856 857
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    /*
     * Address-present-token Encoded-string-value | Insert-address-token
     * Address-present-token = <Octet 128>
     * Insert-address-token = <Octet 129>
     */
    if (type == WSP_VALUE_TYPE_LONG && len > 0) {
        if (val[0] == 0x81) {
858 859 860 861
            header->value = g_strdup("<Insert-address>");
            if (flags & MMS_DUMP_FLAG_VERBOSE) {
                header->dump = g_strdup_printf("%u", val[0]);
            }
Slava Monich's avatar
Slava Monich committed
862 863 864 865 866 867
            return TRUE;
        } else if (val[0] == 0x80 && len > 1) {
            enum wsp_value_type ftype;
            const void* fval = NULL;
            unsigned int flen = 0;
            if (wsp_decode_field(val+1, len-1, &ftype, &fval, &flen, NULL) &&
868 869
                mms_value_decode_etext(header, ftype, fval, flen, 0)) {
                mms_header_dump(header, val, len, flags);
Slava Monich's avatar
Slava Monich committed
870 871 872 873
                return TRUE;
            }
        }
    }
874
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
875 876
}

877
/* Expiry-value */
Slava Monich's avatar
Slava Monich committed
878 879 880
static
gboolean
mms_value_decode_expiry(
881
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    /*
     * Absolute-token Date-value | Relative-token Delta-seconds-value
     * Absolute-token = <Octet 128>
     * Relative-token = <Octet 129>
     */
    gboolean ok = FALSE;
    enum wsp_value_type ftype;
    const void* fval = NULL;
    unsigned int flen = 0;
    if (type == WSP_VALUE_TYPE_LONG && len > 1 &&
        wsp_decode_field(val+1, len-1, &ftype, &fval, &flen, NULL)) {
        if (val[0] == 0x80 /* Absolute-token */) {
899
            ok = mms_value_decode_date(header, ftype, fval, flen, 0);
Slava Monich's avatar
Slava Monich committed
900 901 902 903 904 905 906
        } else if (val[0] == 0x81 /* Relative-token */) {
            time_t t;
            if (ftype == WSP_VALUE_TYPE_LONG && flen > 0 && flen <= sizeof(t)) {
                unsigned int i;
                for (t=0, i=0; i<flen; i++) {
                    t = ((t << 8) | ((guint8*)fval)[i]);
                }
907
                header->value = g_strdup_printf("+%u sec", (unsigned int)t);
Slava Monich's avatar
Slava Monich committed
908 909 910 911 912
                ok = TRUE;
            }
        }
    }
    if (ok) {
913
        mms_header_dump(header, val, len, flags);
Slava Monich's avatar
Slava Monich committed
914 915
        return TRUE;
    }
916
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
917 918
}

919 920 921 922
/* Previously-sent-by-value */
static
gboolean
mms_value_decode_prevby(
923
    struct mms_header* header,
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    /*
     * Value-length Forwarded-count-value Encoded-string-value
     * Forwarded-count-value = Integer-value
     */
    if (type == WSP_VALUE_TYPE_LONG) {
        unsigned int count = 0;
        unsigned int consumed = 0;
        if (wsp_decode_integer(val, len, &count, &consumed)) {
            const guint8* ptr = val + consumed;
            const unsigned int bytes_left = len - consumed;
939 940 941 942 943 944 945 946
            enum wsp_value_type ftype;
            const void* fval;
            unsigned int flen;
            if (wsp_decode_field(ptr, bytes_left, &ftype, &fval, &flen,
                &consumed) && consumed == bytes_left &&
                mms_value_decode_encoded_text(header, ftype, fval, flen)) {
                mms_header_set_count_param(header, count);
                mms_header_dump(header, val, len, flags);
947 948 949 950
                return TRUE;
            }
        }
    }
951
    return mms_value_decode_unknown(header, type, val, len, flags);
952 953 954 955 956 957
}

/* Previously-sent-date-value */
static
gboolean
mms_value_decode_prevdate(
958
    struct mms_header* header,
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    /*
     * Value-length Forwarded-count-value Date-value
     * Forwarded-count-value = Integer-value
     */
    if (type == WSP_VALUE_TYPE_LONG) {
        unsigned int count = 0;
        unsigned int consumed = 0;
        if (wsp_decode_integer(val, len, &count, &consumed)) {
            const guint8* ptr = val + consumed;
            const unsigned int bytes_left = len - consumed;
            enum wsp_value_type date_type;
            const void* date_val;
            unsigned int date_len;
            time_t t;
            if (wsp_decode_field(ptr, bytes_left, &date_type, &date_val,
                &date_len, &consumed) && consumed == bytes_left &&
                date_type == WSP_VALUE_TYPE_LONG &&
                mms_value_decode_date_value(date_val, date_len, &t)) {
982 983 984
                header->value = mms_value_format_date(t);
                mms_header_set_count_param(header, count);
                mms_header_dump(header, val, len, flags);
985 986 987 988
                return TRUE;
            }
        }
    }
989
    return mms_value_decode_unknown(header, type, val, len, flags);
990 991
}

992 993 994
static
void
mms_value_decode_wsp_params(
995
    struct mms_header* header,
996 997 998 999 1000 1001 1002 1003
    const unsigned char* pdu,
    unsigned int len)
{
    static const struct mms_named_value nv_p [] = {
        { "Q",                 WSP_PARAMETER_TYPE_Q                  },
        { "Charset",           WSP_PARAMETER_TYPE_CHARSET            },
        { "Level",             WSP_PARAMETER_TYPE_LEVEL              },
        { "Type",              WSP_PARAMETER_TYPE_TYPE               },
1004 1005
        { PARAM_NAME,          WSP_PARAMETER_TYPE_NAME_DEFUNCT       },
        { PARAM_FILENAME,      WSP_PARAMETER_TYPE_FILENAME_DEFUNCT   },
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        { "Differences",       WSP_PARAMETER_TYPE_DIFFERENCES        },
        { "Padding",           WSP_PARAMETER_TYPE_PADDING            },
        { "Type",              WSP_PARAMETER_TYPE_CONTENT_TYPE       },
        { "Start",             WSP_PARAMETER_TYPE_START_DEFUNCT      },
        { "Start-info",        WSP_PARAMETER_TYPE_START_INFO_DEFUNCT },
        { "Comment",           WSP_PARAMETER_TYPE_COMMENT_DEFUNCT    },
        { "Domain",            WSP_PARAMETER_TYPE_DOMAIN_DEFUNCT     },
        { "Max-Age",           WSP_PARAMETER_TYPE_MAX_AGE            },
        { "Path",              WSP_PARAMETER_TYPE_PATH_DEFUNCT       },
        { "Secure",            WSP_PARAMETER_TYPE_SECURE             },
        { "SEC",               WSP_PARAMETER_TYPE_SEC                },
        { "MAC",               WSP_PARAMETER_TYPE_MAC                },
        { "Creation-date",     WSP_PARAMETER_TYPE_CREATION_DATE      },
        { "Modification-date", WSP_PARAMETER_TYPE_MODIFICATION_DATE  },
        { "Read-date",         WSP_PARAMETER_TYPE_READ_DATE          },
        { "Size",              WSP_PARAMETER_TYPE_SIZE               },
1022 1023
        { PARAM_NAME,          WSP_PARAMETER_TYPE_NAME               },
        { PARAM_FILENAME,      WSP_PARAMETER_TYPE_FILENAME           },
1024 1025 1026 1027 1028 1029 1030
        { "Start",             WSP_PARAMETER_TYPE_START              },
        { "Start-info",        WSP_PARAMETER_TYPE_START_INFO         },
        { "Comment",           WSP_PARAMETER_TYPE_COMMENT            },
        { "Domain",            WSP_PARAMETER_TYPE_DOMAIN             },
        { "Path",              WSP_PARAMETER_TYPE_PATH               }
    };

1031
    GPtrArray* tmp = g_ptr_array_new();
1032 1033 1034 1035 1036
    struct wsp_parameter_iter pi;
    struct wsp_parameter p;

    wsp_parameter_iter_init(&pi, pdu, len);
    while (wsp_parameter_iter_next(&pi, &p)) {
1037 1038 1039 1040
        struct mms_header_param* hp = g_slice_new0(struct mms_header_param);
        const struct mms_named_value* nv =
            mms_find_named_value(nv_p, N_(nv_p), p.type);
        hp->name = nv ? g_strdup(nv->name) : g_strdup_printf("0x%02x", p.type);
1041 1042
        switch (p.value) {
        case WSP_PARAMETER_VALUE_TEXT:
1043
            hp->value = g_strdup(p.text[0] == '"' ? (p.text + 1) : p.text);
1044 1045
            break;
        case WSP_PARAMETER_VALUE_INT:
1046
            hp->value = g_strdup_printf("%u", p.integer);
1047 1048
            break;
        case WSP_PARAMETER_VALUE_DATE:
1049
            hp->value = mms_value_format_date(p.date);
1050 1051
            break;
        case WSP_PARAMETER_VALUE_Q:
1052
            hp->value = g_strdup_printf("%g", p.q);
1053 1054
            break;
        }
1055
        g_ptr_array_add(tmp, hp);
1056
    }
1057 1058 1059

    header->num_params = tmp->len;
    header->params = (struct mms_header_param**)g_ptr_array_free(tmp, FALSE);
1060 1061
}

Slava Monich's avatar
Slava Monich committed
1062 1063 1064
static
gboolean
mms_value_decode_contdisp(
1065
    struct mms_header* header,
Slava Monich's avatar
Slava Monich committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
    enum wsp_value_type type,
    const guint8* val,
    unsigned int len,
    unsigned int flags)
{
    /*
     * Content-disposition-value = Value-length Disposition *(Parameter)
     *
     * Value-length = Short-length | (Length-quote Length)
     * Short-length = <Any octet 0-30>
     * Length-quote = <Octet 31>
     * Length = Uintvar-integer
     *
     * Disposition = Form-data | Attachment | Inline | Token-text
     * Form-data = <Octet 128>
     * Attachment = <Octet 129>
     * Inline = <Octet 130>
     */
    static const struct mms_named_value nv_d [] = {
        { "Form-data",  128 },
        { "Attachment", 129 },
        { "Inline",     130 }
    };

    if ((type == WSP_VALUE_TYPE_LONG ||
         type == WSP_VALUE_TYPE_SHORT) && len > 0) {
        const struct mms_named_value* nv;
1093
        nv = mms_find_named_value(nv_d, N_(nv_d), val[0]);
Slava Monich's avatar
Slava Monich committed
1094
        if (nv) {
1095 1096 1097
            header->value = g_strdup(nv->name);
            mms_value_decode_wsp_params(header, val + 1, len - 1);
            mms_header_dump(header, val, len, flags);
Slava Monich's avatar
Slava Monich committed
1098 1099 1100
            return TRUE;
        }
    }
1101
    return mms_value_decode_unknown(header, type, val, len, flags);
Slava Monich's avatar
Slava Monich committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
}

#define mms_value_decode_short  mms_value_decode_unknown
#define mms_value_decode_text   mms_value_decode_unknown

static
mms_value_decoder
mms_message_value_decoder_for_header(
    const char* hdr)
{
#define h(id,n,x,t) if (!strcmp(n,hdr)) return &mms_value_decode_##t;
    MMS_WELL_KNOWN_HEADERS(h);
#undef h
    return &mms_value_decode_unknown;
}

static
mms_value_decoder
mms_part_value_decoder_for_header(
    const char* hdr)
{
#define h(id,n,x,t) if (!strcmp(n,hdr)) return &mms_value_decode_##t;
    WSP_WELL_KNOWN_HEADERS(h);
#undef h
    return &mms_value_decode_unknown;
}

static
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
void
mms_decode_header_free_func(
    gpointer data,
    gpointer user_data)
{
    mms_header_free(data);
}

static
struct mms_headers*
Slava Monich's avatar
Slava Monich committed
1140 1141 1142 1143 1144 1145
mms_decode_headers(
    struct wsp_header_iter* iter,
    unsigned int flags,
    const char* (*well_known_header_name)(int hdr),
    mms_value_decoder (*value_decoder_for_header)(const char* hdr))
{
1146 1147
    struct mms_headers* headers;
    GPtrArray* tmp = g_ptr_array_new();
Slava Monich's avatar
Slava Monich committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    while (wsp_header_iter_next(iter)) {
        const guint8* hdr = wsp_header_iter_get_hdr(iter);
        const char* hdr_name;

        switch (wsp_header_iter_get_hdr_type(iter)) {
        case WSP_HEADER_TYPE_WELL_KNOWN:
            hdr_name = well_known_header_name(hdr[0] & 0x7f);
            break;
        case WSP_HEADER_TYPE_APPLICATION:
            hdr_name = (const char*)hdr;
            break;
        default:
1160 1161
            hdr_name = NULL;
            break;
Slava Monich's avatar
Slava Monich committed
1162
        }
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
        if (hdr_name) {
            const guint8* val = wsp_header_iter_get_val(iter);
            unsigned int val_len = wsp_header_iter_get_val_len(iter);
            enum wsp_value_type val_type = wsp_header_iter_get_val_type(iter);
            mms_value_decoder dec = value_decoder_for_header(hdr_name);
            struct mms_header* header = mms_header_new(hdr_name);
            g_ptr_array_add(tmp, header);
            if (!val_len ||
               (dec && dec(header, val_type, val, val_len, flags))) {
                continue;
            }
Slava Monich's avatar
Slava Monich committed
1174
        }
1175 1176 1177
        g_ptr_array_foreach(tmp, mms_decode_header_free_func, NULL);
        g_ptr_array_unref(tmp);
        return NULL;
Slava Monich's avatar
Slava Monich committed
1178
    }
1179 1180 1181 1182
    headers = g_slice_new0(struct mms_headers);
    headers->num_headers = tmp->len;
    headers->headers = (struct mms_header**)g_ptr_array_free(tmp, FALSE);
    return headers;
Slava Monich's avatar
Slava Monich committed
1183 1184 1185
}

static
1186
struct mms_headers*
Slava Monich's avatar
Slava Monich committed
1187 1188 1189 1190
mms_message_decode_headers(
    struct wsp_header_iter* iter,
    unsigned int flags)
{
1191
    return mms_decode_headers(iter, flags,
Slava Monich's avatar
Slava Monich committed
1192 1193 1194 1195 1196
        mms_message_well_known_header_name,
        mms_message_value_decoder_for_header);
}

static
1197
struct mms_headers*
Slava Monich's avatar
Slava Monich committed
1198 1199 1200 1201
mms_part_decode_headers(
    struct wsp_header_iter* iter,
    unsigned int flags)
{
1202
    return mms_decode_headers(iter, flags,
Slava Monich's avatar
Slava Monich committed
1203 1204 1205 1206
        mms_part_well_known_header_name,
        mms_part_value_decoder_for_header);
}

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
static
void
mms_decode_dump_data(
    const unsigned char* data,
    unsigned int len)
{
    const unsigned int linelen = 16;
    unsigned int i;
    char line[80];
    for (i=0; i<len; i+=linelen) {
        unsigned int j;
        char tmp[8];
        sprintf(line,"  %04x: ", (unsigned int)i);
        for (j=i; j<(i+linelen); j++) {
            if ((j%16) == 8) strcat(line, " ");
            if (j < len) {
                sprintf(tmp, "%02x ", (unsigned int)data[j]);
                strcat(line, tmp);
            } else {
                strcat(line, "   ");
            }
        }
        strcat(line, "  ");
        for (j=i; j<(i+linelen); j++) {
            if (j<len) {
                sprintf(tmp, "%c", isprint(data[j]) ? data[j] : '.');
                strcat(line, tmp);
            } else {
                strcat(line, " ");
            }
        }
        printf("%s\n", line);
    }
}

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
static
void
mms_extract_attachment(
    int index,
    const struct mms_header* content_type,
    const struct mms_headers* headers,
    const void* data,
    unsigned int len)
{
    FILE* f;
    char* tmp = NULL;
    const char* filename = mms_headers_find_filename(content_type, headers);
    if (!filename) {
        if (index < 0) {
            filename = "attachment.dat";
        } else {
            filename = tmp = g_strdup_printf("attachment.%03d.dat", index);
        }
    }
    f = fopen(filename, "wb");
    if (f) {
        printf("Writing %s\n", filename);
        fwrite(data, 1, len, f);
        fclose(f);
    } else {
        printf("ERROR! Can't create %s\n", filename);
    }
    g_free(tmp);
}

Slava Monich's avatar
Slava Monich committed
1272 1273
static
gboolean
1274
mms_decode_multipart(
Slava Monich's avatar
Slava Monich committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
    struct wsp_header_iter* iter,
    unsigned int flags)
{
    struct wsp_multipart_iter mi;
    if (wsp_multipart_iter_init(&mi, iter, NULL, NULL)) {
        int i;
        for (i=0; wsp_multipart_iter_next(&mi); i++) {
            unsigned int n;
            const void* type;
            const unsigned char* ct = wsp_multipart_iter_get_content_type(&mi);
            unsigned int ct_len = wsp_multipart_iter_get_content_type_len(&mi);
            if (wsp_decode_content_type(ct, ct_len, &type, &n, NULL)) {
                struct wsp_header_iter hi;
                const unsigned char* body = wsp_multipart_iter_get_body(&mi);
                unsigned int len = wsp_multipart_iter_get_body_len(&mi);
                unsigned int off = body - wsp_header_iter_get_pdu(iter);
1291 1292 1293
                struct mms_header* ctype = mms_header_new_content_type();
                struct mms_headers* headers;
                const char* prefix = "  ";
Slava Monich's avatar
Slava Monich committed
1294 1295 1296 1297 1298 1299 1300 1301
                printf("Attachment #%d:\n", i+1);
                if (flags & MMS_DUMP_FLAG_VERBOSE) {
                    printf("Offset: %u (0x%x)\n", off, off);
                    printf("Length: %u (0x%x)\n", len, len);
                } else {
                    printf("Offset: %u\n", off);
                    printf("Length: %u\n", len);
                }
1302 1303 1304 1305
                ctype->value = g_strdup((char*)type);
                mms_value_decode_wsp_params(ctype, ct + n, ct_len - n);
                mms_header_dump(ctype, ct, ct_len, flags);
                mms_header_print(ctype, prefix, flags);
Slava Monich's avatar
Slava Monich committed
1306 1307
                wsp_header_iter_init(&hi, wsp_multipart_iter_get_hdr(&mi),
                    wsp_multipart_iter_get_hdr_len(&mi), 0);
1308 1309
                headers = mms_part_decode_headers(&hi, flags);
                mms_headers_print(headers, prefix, flags);
1310 1311 1312
                if (flags & MMS_DUMP_FLAG_DATA) {
                    printf("Data:\n");
                    mms_decode_dump_data(body, len);
Slava Monich's avatar
Slava Monich committed
1313
                }
1314 1315 1316 1317 1318
                if (flags & MMS_DUMP_FLAG_EXTRACT) {
                    mms_extract_attachment(i+1, ctype, headers, body, len);
                }
                mms_header_free(ctype);
                mms_headers_free(headers);
1319
                if (wsp_header_iter_at_end(&hi)) continue;
Slava Monich's avatar
Slava Monich committed
1320 1321 1322 1323 1324 1325 1326 1327
            }
            return FALSE;
        }
        return wsp_multipart_iter_close(&mi, iter);
    }
    return FALSE;
}

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
static
gboolean
mms_decode_attachment(
    struct wsp_header_iter* iter,
    unsigned int flags)
{
    const unsigned char* pdu = iter->pdu + iter->pos + 1;
    unsigned int len = iter->max - iter->pos - 1;
    unsigned int consumed, parlen;
    const void *type = NULL;
    if (wsp_decode_content_type(pdu, len, &type, &consumed, &parlen)) {
1339
        struct mms_header* content_type = mms_header_new_content_type();
1340 1341
        unsigned int total = consumed + parlen;
        unsigned int off = iter->pos + 1 + total;
1342
        const unsigned char* body = iter->pdu + off;
1343 1344 1345 1346 1347 1348 1349 1350 1351
        len -= total;
        printf("Attachment:\n");
        if (flags & MMS_DUMP_FLAG_VERBOSE) {
            printf("Offset: %u (0x%x)\n", off, off);
            printf("Length: %u (0x%x)\n", len, len);
        } else {
            printf("Offset: %u\n", off);
            printf("Length: %u\n", len);
        }
1352 1353 1354 1355
        content_type->value = g_strdup((char*)type);
        mms_value_decode_wsp_params(content_type, pdu + consumed, parlen);
        mms_header_dump(content_type, iter->pdu + iter->pos, total+1, flags);
        mms_header_print(content_type, "  ", flags);
1356 1357
        if (flags & MMS_DUMP_FLAG_DATA) {
            printf("Data:\n");
1358 1359 1360 1361
            mms_decode_dump_data(body, len);
        }
        if (flags & MMS_DUMP_FLAG_EXTRACT) {
            mms_extract_attachment(-1, content_type, NULL, body, len);
1362
        }
1363
        mms_header_free(content_type);
1364 1365 1366 1367 1368
        return TRUE;
    }
    return FALSE;
}

Slava Monich's avatar
Slava Monich committed
1369 1370 1371 1372 1373 1374 1375 1376
static
int
mms_decode_data(
    const guint8* data,
    gsize len,
    unsigned int flags)
{
    struct wsp_header_iter iter;
1377
    struct mms_headers* headers;
Slava Monich's avatar
Slava Monich committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403

    /* Skip WSP Push notification header if we find one */
    if (len >= 3 && data[1] == 6 /* Push PDU */) {
        unsigned int hdrlen = 0;
        unsigned int off = 0;
        const guint8* wsp_data = data+2;
        gsize wsp_len = len - 2;

        /* Hdrlen */
        if (wsp_decode_uintvar(wsp_data, wsp_len, &hdrlen, &off) &&
            (off + hdrlen) <= wsp_len) {
            const void* ct = NULL;
            wsp_data += off;
            wsp_len -= off;
            if (wsp_decode_content_type(wsp_data, hdrlen, &ct, &off, NULL) &&
                strcmp(ct, MMS_CONTENT_TYPE) == 0) {
                printf("WSP header:\n  %s\n", (char*)ct);
                data = wsp_data + hdrlen;
                len = wsp_len - hdrlen;
            }
        }
    }

    printf("MMS headers:\n");
    wsp_header_iter_init(&iter, data, len, WSP_HEADER_ITER_FLAG_REJECT_CP |
        WSP_HEADER_ITER_FLAG_DETECT_MMS_MULTIPART);
1404 1405 1406 1407
    headers = mms_message_decode_headers(&iter, flags);
    if (headers) {
        mms_headers_print(headers, "  ", flags);
        mms_headers_free(headers);
1408 1409 1410 1411 1412 1413 1414 1415 1416
        if (wsp_header_iter_at_end(&iter)) {
            return RET_OK;
        } else if (wsp_header_iter_is_content_type(&iter)) {
            if (wsp_header_iter_is_multipart(&iter)) {
                if (mms_decode_multipart(&iter, flags) &&
                    wsp_header_iter_at_end(&iter)) {
                    return RET_OK;
                }
            } else if (mms_decode_attachment(&iter, flags)) {
1417 1418 1419
                return RET_OK;
            }
        }
Slava Monich's avatar
Slava Monich committed
1420
    }
1421
    printf("Decoding FAILED\n");
Slava Monich's avatar
Slava Monich committed
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    return RET_ERR_DECODE;
}

static
int
mms_decode_file(
    const char* fname,
    unsigned int flags)
{
    int ret = RET_ERR_IO;
    GError* error = NULL;
    GMappedFile* map = g_mapped_file_new(fname, FALSE, &error);
    if (map) {
        const void* data = g_mapped_file_get_contents(map);
        const gsize size = g_mapped_file_get_length(map);
        ret = mms_decode_data(data, size, flags);
        g_mapped_file_unref(map);
    } else {
        printf("%s: %s\n", pname, error->message);
        g_error_free(error);
    }
    return ret;
}

int main(int argc, char* argv[])
{
    int ret = RET_ERR_CMDLINE;
1449
    gboolean ok, verbose = FALSE, data = FALSE, extract = FALSE;
Slava Monich's avatar
Slava Monich committed
1450 1451 1452 1453
    GError* error = NULL;
    GOptionEntry entries[] = {
        { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
          "Enable verbose output", NULL },
1454 1455
        { "data", 'd', 0, G_OPTION_ARG_NONE, &data,
          "Dump attachment data", NULL },
1456 1457
        { "extract", 'x', 0, G_OPTION_ARG_NONE, &extract,
          "Extract attachments", NULL },
Slava Monich's avatar
Slava Monich committed
1458 1459 1460 1461 1462 1463 1464 1465 1466
        { NULL }
    };
    GOptionContext* options = g_option_context_new("FILES");
    g_option_context_add_main_entries(options, entries, NULL);
    ok = g_option_context_parse(options, &argc, &argv, &error);
    if (ok) {
        if (argc > 1) {
            int i, flags = 0;
            if (verbose) flags |= MMS_DUMP_FLAG_VERBOSE;
1467
            if (extract) flags |= MMS_DUMP_FLAG_EXTRACT;
1468
            if (data) flags |= MMS_DUMP_FLAG_DATA;
Slava Monich's avatar
Slava Monich committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
            for (i=1; i<argc; i++) {
                const char* fname = argv[i];
                if (argc > 2) printf("\n%s\n\n", fname);
                ret = mms_decode_file(fname, flags);
                if (ret != RET_OK) break;
            }
        } else {
            char* help = g_option_context_get_help(options, TRUE, NULL);
            printf("%s", help);
            g_free(help);
        } 
    } else {
        printf("%s: %s\n", pname, error->message);
        g_error_free(error);
    }
    g_option_context_free(options);
    return ret;
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */