# protocol.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       protocol.cc
    00003 ///             USB Blackberry bulk protocol API
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00008
    00009     This program is free software; you can redistribute it and/or modify
    00010     it under the terms of the GNU General Public License as published by
    00011     the Free Software Foundation; either version 2 of the License, or
    00012     (at your option) any later version.
    00013
    00014     This program is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00017
    00018     See the GNU General Public License in the COPYING file at the
    00019     root directory of this project for more details.
    00020 */
    00021
    00022 #include "protocol.h"
    00023 #include "protostructs.h"
    00024 #include "data.h"
    00025 #include "endian.h"
    00026 #include "error.h"
    00027 #include "debug.h"
    00028
    00029 #include <sstream>
    00030
    00031 namespace Barry { namespace Protocol {
    00032
    00033 // This function is only valid for Packet, JLPacket, and JVMPacket structs,
    00034 // as long as they don't differ from each other in header layout, when
    00035 // it comes to the .size field.  (see protostructs.h)
    00036 void CheckSize(const Data &packet, size_t requiredsize)
    00037 {
    00038         const Packet *p = (const Packet *) packet.GetData();
    00039
    00040         // when packets are larger than 0xFFFF bytes, packet->size is no
    00041         // longer reliable, so we go with the Data class size
    00042         if( (packet.GetSize() >= 4 && btohs(p->size) != packet.GetSize() && packet.GetSize() <= 0xFFFF) ||
    00043             packet.GetSize() < requiredsize )
    00044
    00045         {
    00046                 BadSize bs(packet.GetSize() >= 4 ? btohs(p->size) : 0,
    00047                         packet.GetSize(), requiredsize);
    00048                 eout(bs.what());
    00049                 eout(packet);
    00050                 throw bs;
    00051         }
    00052 }
    00053
    00054 unsigned int GetSize(const Data &packet)
    00055 {
    00056         CheckSize(packet, 4);
    00057
    00058         // when packets are larger than 0xFFFF bytes, packet->size is no
    00059         // longer reliable, so we go with the Data class size
    00060         if( packet.GetSize() > 0xFFFF ) {
    00061                 return packet.GetSize();
    00062         }
    00063         else {
    00064                 const Packet *p = (const Packet *) packet.GetData();
    00065                 return btohs(p->size);
    00066         }
    00067 }
    00068
    00069 }} // namespace Barry::Protocol
    00070