Hirdetés

Aktív témák

  • TheVeryGuest

    senior tag

    smsconv.cxx:

    #include <cstdlib>
    #include <cstdio>
    #include <iostream>
    #include ''symbol.h''
    #include <string>

    using namespace std;

    bool convertToSmsChar(unsigned short ch,
    std::string &str) {
    static unsigned short store = 0;

    if (0x1b == ch) {
    store = ch;
    return false;
    } else {
    if (0x00 != store) {
    for (int i = 0; i < sizeof(tabExtIdx)/sizeof(short); ++i) {
    if (tabExtIdx == ch) {
    str += tabExtSymbol
    ;
    }
    }
    store = 0;
    } else {
    str += tabSymbol[ch];
    }
    }
    return true;
    }

    std::string convert7To8Bit(const unsigned char *buff,
    unsigned int len) {
    unsigned char maskLo[] = {
    0x7f,
    0x3f,
    0x1f,
    0x0f,
    0x07,
    0x03,
    0x01
    };

    unsigned char maskHi[] = {
    0x80,
    0xc0,
    0xe0,
    0xf0,
    0xf8,
    0xfc,
    0xfe
    };

    std::string finalStr;
    unsigned int pos = 0;
    unsigned int bitHave = 0;
    unsigned int remainder = 0;

    int lenBit = len * 7;
    int lenByte = lenBit / 8;
    if (0 != (lenBit % 8)) {
    ++lenByte;
    }

    for (int i = 0; i < lenByte; ++i) {
    int value = remainder + ((buff & maskLo[bitHave]) << bitHave);
    remainder = (buff
    & maskHi[bitHave]) >> (7 - bitHave);
    ++bitHave;

    convertToSmsChar(value, finalStr);
    // cout << (char) value << endl;

    if (7 == bitHave) {
    bitHave = 0;
    convertToSmsChar(remainder, finalStr);
    // cout << (char) remainder << endl;
    remainder = 0;
    }
    }
    return finalStr;
    }

    std::string decodeCallNumber(const unsigned char *buff,
    unsigned int size) {
    char strVal[size + 1];
    const unsigned char mask[] = {
    0x0f,
    0xf0
    };

    const unsigned char shift[] = {
    0x00,
    0x04
    };

    for (int i = 0; i < size; ++i) {
    unsigned short val = (buff[i >> 1] & mask[i % 2]) >> shift[i % 2];
    // cout << val << endl;
    strVal = (char) '0' + val;
    }
    strVal[size] = 0;
    return string(strVal);
    }

    int decodeNumber(const unsigned char *buff,
    unsigned int size) {
    int sum = 0;
    const unsigned char mask[] = {
    0x0f,
    0xf0
    };

    const unsigned char shift[] = {
    0x00,
    0x04
    };

    for (int i = 0; i < size; ++i) {
    unsigned short val = (buff[i >> 1] & mask[i % 2]) >> shift[i % 2];
    // cout << val << endl;
    sum = sum * 10 + val;
    }
    return sum;
    }


    std::string decodeTimeStamp(const unsigned char *buff) {
    const unsigned char mask[] = {
    0x0f,
    0xf0
    };

    const unsigned char shift[] = {
    0x00,
    0x04
    };

    const char *tabGmt[] = {
    ''0'',
    ''1'',
    ''2'',
    ''3'',
    ''4'',
    ''5'',
    ''6'',
    ''7'',
    ''8'',
    ''9'',
    ''10'',
    ''11'',
    ''12''
    };

    std::string strTs = decodeCallNumber(buff, 12);
    // cout << strTs << endl;
    strTs.insert(2, ''.'');
    strTs.insert(5, ''.'');
    strTs.insert(8, ''. '');
    strTs.insert(12, '':'');
    strTs.insert(15, '':'');

    int diffGmt = decodeNumber(buff + 6, 2);
    diffGmt = ((diffGmt % 10) * 10 + diffGmt / 10) / 4;
    const char *sign;
    if (diffGmt < 0) {
    diffGmt *= -1;
    sign = '' - '';
    } else {
    sign = '' + '';
    }

    strTs += '', GMT'';
    if (0 != diffGmt) {
    strTs += sign;
    strTs += tabGmt[diffGmt];
    }

    // cout << (int) *(buff + 12) << endl;

    // cout << diffGmt << endl;

    return strTs;
    }

    bool decodeSms(const unsigned char *buff) {
    unsigned short lenSmscInfo = *buff;
    unsigned short typSmscInfo = *(buff + 1);

    // cout << (unsigned int) *buff << endl;

    if ((0x07 != lenSmscInfo) || (0x91 != typSmscInfo)) {
    cerr << ''[Decoder]: unknown format of SMSC info -> skipped '';
    } else {
    cerr << ''[Decoder]: SMS Center: '' << decodeCallNumber(buff + 2, 11) << endl;
    }

    // SMS Delivery byte skipped
    const unsigned char *infoSender = buff + lenSmscInfo + 2;

    unsigned short lenSenderNum = *infoSender;
    unsigned short typSenderNum = *(infoSender + 1);

    // cout << lenSenderNum << endl << typSenderNum << endl;

    if ((0x0b != lenSenderNum) || (0x91 != typSenderNum)) {
    cerr << ''[Decoder]: unknown format of Sender number -> returning '';
    return false;
    } else {
    cerr << ''[Decoder]: Sender: '' << decodeCallNumber(infoSender + 2, 11) << endl;
    }

    const unsigned char *infoTp = infoSender + 8;

    unsigned short pidTp = *infoTp;
    unsigned short dcsTp = *(infoTp + 1);

    if ((0x00 != pidTp) || (0x00 != dcsTp)) {
    cerr << ''[Decoder]: unknown format in TP info -> returning '';
    return false;
    }

    cerr << ''[Decoder]: TimeStamp: '' << decodeTimeStamp(infoTp + 2) << endl;

    unsigned short lenMsg = *(infoTp + 9);

    cerr << ''[Decoder]: SMS length: '' << lenMsg << endl;

    cout << convert7To8Bit(infoTp + 10, lenMsg) << endl;

    return true;
    }

    int main(int argc, char **argv) {
    if (argc < 2) {
    cerr << ''<ERROR> Input file name required. '';
    return 1;
    }

    cerr << ''Using '' << argv[1] << ''as input file. '';

    FILE *f = fopen(argv[1], ''rb'');
    if (!f) {
    cerr << ''<ERROR> File open error ('' << argv[1] << ''). '';
    return 1;
    }

    fseek(f, 0, SEEK_END);
    int szFile = ftell(f);

    if (181 != szFile) {
    cerr << ''<ERROR> Unknown file size ('' << szFile << '') for '' << argv[1] << ''. '';
    return 1;
    }

    unsigned char *bufFile = new unsigned char [szFile];
    if (!bufFile) {
    cerr << ''<ERROR> Memory allocation error ('' << szFile << '' bytes) '';
    return 1;
    }

    rewind(f);
    if (!fread(bufFile, szFile, 1, f)) {
    cerr << ''<ERROR> Error reading file ('' << argv[1] << ''). '';
    delete [] bufFile;
    return 1;
    }

    cerr << ''Decoding... '';

    if (!decodeSms(bufFile + 6)) {
    cerr << ''<ERROR> Decoder error. '';
    delete [] bufFile;
    return 1;
    }

    cerr << ''<OK> SMS decoded successfully '';
    delete [] bufFile;

    return 0;
    }

Aktív témák