1st attempt at an initial import.
[qwerkisync] / EventParsers / MMSParser.cpp
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18
19 #include "MMSParser.h"
20
21 #include <QDebug>
22
23 #include <QFile>
24 #include <QString>
25
26 using namespace EventParsers;
27
28 iEventParser *MMSParser::IsValid(QFile &eventFile)
29 {
30         qDebug() << "Checking if a MMS file...";
31
32         // A buffer that can hold 3 fields with a single byte value...and a
33         // bonus terminator byte if required (2*3 + 1)
34         char fileIDBuf[0x07];
35         qint64 bytesRead(eventFile.read(fileIDBuf, sizeof(fileIDBuf) - 1));
36         eventFile.seek(0);
37         if(bytesRead == sizeof(fileIDBuf) - 1)
38         {
39                 // This is the hex 8c (message type '0c' with high bit set), 80 (transaction id), 8D, 92, 85, 04
40                 char fieldMessageType = 0x8C;
41                 char fieldTransactionID = 0x98; // Optional
42                 char fieldMessageMMSVersion = 0x8D;
43                 char fieldMessageDate = 0x85;
44                 uint offset = 0;
45                 if(fileIDBuf[offset++] == fieldMessageType)
46                 {
47                         char valueMessageType = fileIDBuf[offset++] ^ 0x80;
48                         qDebug() << "...looks like a MMS file. Message type is: " << valueMessageType;
49
50                         char valueTransactionID = -1;
51                         if(fileIDBuf[offset] == fieldTransactionID)
52                                 valueTransactionID = fileIDBuf[offset++] ^ 0x80;
53
54                         if(fileIDBuf[offset] == fieldMessageMMSVersion)
55                         {
56                                 char valueMessageMMSVersion = fileIDBuf[offset++] ^ 0x80;
57                                 int majorVersion(valueMessageMMSVersion >> 4);
58                                 int minorVersion(valueMessageMMSVersion & 0x0F);
59                                 qDebug() << QString("...MMS version type is: %1.%2")
60                                                         .arg(majorVersion)
61                                                         .arg(minorVersion);
62
63                                 // We only support up to version 1.3
64                                 if(majorVersion == 1 && minorVersion <= 3)
65                                         return new MMSParser(eventFile.fileName());
66                                 else
67                                         qDebug() << QString("%1 is an unsupported MMS version")
68                                                                 .arg(eventFile.fileName());
69                         }
70                         else
71                                 qDebug() << QString("%1 is a malformed MMS. Expected %2, got %3")
72                                                         .arg(eventFile.fileName())
73                                                         .arg(fieldMessageMMSVersion).arg(fileIDBuf[offset]);
74 //                              }
75 //                              else
76 //                              {
77 //                                      QString hexs;
78 //                                      for(int i(0); i<0x16; ++i)
79 //                                              hexs.append(QString::number(fileIDBuf[i], 16).rightJustified(2, '0'));
80 //                                      qDebug() << eventFile.fileName() << " has bad signature: " << hexs;
81                 }
82         }
83         else
84                 qDebug() << eventFile.fileName() << " has size mismatch.";
85
86         return false;
87 }
88
89 MMSParser::MMSParser(const QString &filename)
90 {
91 }
92
93 EventTypes::EventFromFileList MMSParser::ParseFile(QFile &eventFile, const QList<uint> &recordsToReturn)
94 {
95         qDebug() << "MMS Parsing NYI!";
96         return EventTypes::EventFromFileList();
97 }