[go: up one dir, main page]

File: DUID.cpp

package info (click to toggle)
dibbler 0.7.3-1.3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,148 kB
  • ctags: 8,720
  • sloc: cpp: 54,863; sh: 9,389; ansic: 8,659; yacc: 2,570; makefile: 1,061; lex: 842; perl: 49; xml: 6
file content (190 lines) | stat: -rw-r--r-- 3,847 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
183
184
185
186
187
188
189
190
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 *
 * released under GNU GPL v2 only licence
 *
 * $Id: DUID.cpp,v 1.13 2008-08-29 00:07:30 thomson Exp $
 */

#include <iostream>
#include <sstream>
#include <string.h>

#include "DUID.h"
#include "Logger.h"

TDUID::TDUID()
{
    DUID=0;
    len=0;
    Plain="";
}

// packed
TDUID::TDUID(const char* DUID,int DUIDlen)
{
    this->DUID = 0;

    if ((DUID)&&(DUIDlen))
    {
        this->DUID=new char[DUIDlen];
        memcpy(this->DUID,DUID,DUIDlen);
        this->len=DUIDlen;
	this->packedToPlain();
    }
    else
    {
        this->DUID=NULL;
        this->len=0;
	this->Plain="";
    }
}

void TDUID::packedToPlain() {
    ostringstream tmp;
    for(int i=0; i<this->len; i++) {
	if (i) tmp << ":";
	tmp << setfill('0')<<setw(2)<<hex<< (unsigned int) this->DUID[i];
    }
    tmp << dec;
    this->Plain = tmp.str();
 }

void TDUID::plainToPacked() {
    int DUIDlen = Plain.length();
    char * tmp = new char[DUIDlen>>1];
    unsigned char digit;
    int i=0, j=0;
    bool twonibbles = false;
    while (i<DUIDlen)
    {
	if (Plain[i]==':') {
	    i++;
	}
	digit = Plain[i];
	if (isalpha(digit))
	    digit=toupper(digit)-'A'+10;
	else
	    digit-='0';
	tmp[j]<<=4;
	tmp[j]|=digit;
	i++;
	if (twonibbles) {
	    twonibbles = false;
	    j++;
	} else 
	    twonibbles = true;
    }

    DUID = new char[j];
    memmove(DUID, tmp, j);
    delete [] tmp;
    this->len = j;
}

// plain
TDUID::TDUID(const char* Plain)
{
    if (!Plain) {
	this->DUID=NULL;
	this->len=0;
	return;
    }

    this->Plain = Plain;

    plainToPacked();
}

TDUID::~TDUID() {
    if (this->len)
	delete [] this->DUID;
}

TDUID::TDUID(const TDUID &duid) {
    this->DUID=new char[duid.len];
    memcpy(this->DUID,duid.DUID,duid.len);
    this->len=duid.len;
    this->Plain = duid.Plain;
}

TDUID& TDUID::operator=(const TDUID &duid) {
    if (this==&duid)
        return *this;
    
    if(this->DUID)
        delete this->DUID;
    
    this->DUID=new char [duid.len];
    memcpy(this->DUID,duid.DUID,duid.len);
    this->len=duid.len;
    this->Plain=duid.Plain;
    
    return *this;
}

bool TDUID::operator==(const TDUID &duid) {
    if (this->len!=duid.len)
        return false;
    else
        return !memcmp(this->DUID,duid.DUID,this->len);
}

bool TDUID::operator<=(const TDUID &duid) {
    int minLen=this->len<duid.len?this->len:duid.len;
    int maxLen=this->len>=duid.len?this->len:duid.len;
    
    bool retVal=false;
    
    const char *longer; //,*shorter;	
    if (this->len<duid.len)
        longer=duid.DUID;
    else
    {
        longer=this->DUID;
        retVal=true;
    };
    
    //if they are not equal check if longer has only zeros at the begining
    for (int i=0;i<(maxLen-minLen);i++)
        if (longer[i])
            return retVal;
    //if they are equal sizes or longer has at the beginning only zeroes
    for (int i=minLen-1;i>=0;i--)
        if(this->DUID[this->len-i]!=duid.DUID[duid.len-i])
        {
            if(this->DUID[this->len-i]>duid.DUID[duid.len-i]) {
                return true; //right is smaller
            } else {
                if(this->DUID[this->len-i]<duid.DUID[duid.len-i])
                    return false;	//left is smaller
	    }
        };
    return false;//left is equal to right
}

int TDUID::getLen() {
    return this->len;
}

const string TDUID::getPlain() {
    return this->Plain;
}

char * TDUID::storeSelf(char* buf) {
    memcpy(buf,DUID,len);
    return buf+len;
}

ostream& operator<<(ostream& out,TDUID&  duid) {
    if ( (duid.DUID && duid.len) ) {
	out << "<duid length=\"" << duid.len << "\">"
	    << duid.Plain << "</duid>" << std::endl;
    } else {
        out << "<duid length=\"0\"></duid>" << std::endl;
    }
    return out;
}