PerDecoder.cc 11.7 KB
Newer Older
C
calincerchez 已提交
1
//
C
calin cerchez 已提交
2 3
// Copyright (C) 2012 Calin Cerchez
//
C
calincerchez 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// 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 Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
// 

#include "PerDecoder.h"

C
calin cerchez 已提交
20 21
PerDecoder::PerDecoder(char *buffer)  {
	this->buffer = buffer;
C
calincerchez 已提交
22 23 24 25 26 27 28 29
	this->it = 0;
	leftBits = 8;
}

PerDecoder::~PerDecoder() {

}

C
calin cerchez 已提交
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
int64_t PerDecoder::decodeConstrainedValue(int64_t lowerBound, int64_t upperBound) {
    int64_t range = upperBound - lowerBound + 1;
    int64_t length;
    int64_t value;

    if (range < 2) {
        return 0;
    } else if (range < 256) {
        int64_t numBits = countBits(range - 1, 0);
        value = (decodeBits(numBits) >> (8 - numBits)) + lowerBound;
    } else if (range < 65537) {
        length = (countBits(range - 1, 0) + 7) / 8;
        value = decodeValue(length) + lowerBound;
    } else {
        length = decodeLength((countBits(lowerBound, 0) + 7) / 8, (countBits(upperBound, 0) + 7) / 8);
        value = decodeValue(length) + lowerBound;
    }
    return value;
}

int64_t PerDecoder::decodeUnconstrainedValue() {
    return decodeValue(decodeLength(0, INT_MAX));
}

char *PerDecoder::decodeSmallBitString(unsigned char length) {
    char *buffer = (char*)calloc((length + 7) / 8, sizeof(char));
    if (length < 9) {
        *buffer = decodeBits(length);
    } else {
        *buffer = decodeBits(8);
        *(buffer + 1) = decodeBits(length - 8);
    }
    return buffer;
}

char *PerDecoder::decodeBytes(int64_t length) {
    // gets the buffer from iterator position and moves the iterator
    allignIterator();

    char *buffer = (char *)calloc(sizeof(char), length);
    memcpy(buffer, this->buffer + it, length);
    it += length;

    return buffer;
}

unsigned char PerDecoder::decodeBits(unsigned char length) {
    // returns a maximum of 8 bits from the tail of the buffer
    // the bits are shifted 1100....
    unsigned char value = 0;

    // if it has to read bits also from the next byte in the buffer
    if (leftBits - length < 0) {

        // takes the bits from the current byte and moves the iterator
        value = ((unsigned char)this->buffer[it++] << (8 - leftBits));

        // takes the bits also from the next byte
        value += ((unsigned char)this->buffer[it] >> leftBits);

        // changes the number of left bits in the iterator byte
        leftBits = (8 - (length - leftBits));

    // all the bits are in the same byte
    } else {
        value = (unsigned char)((this->buffer[it] & bitMask(leftBits - length, leftBits)) << (8 - leftBits));
        leftBits -= length;

        // if there are no more bits to read in the iterator byte
        // reset the number of left bits to 8 and move the iterator
        if (!leftBits) {
            leftBits = 8;
            it++;
        }
    }
    return value;
}

int64_t PerDecoder::decodeValue(int64_t length) {
    int64_t val = 0;
    allignIterator();

    for (int i = length - 1; i >= 0; i--) {
        val += ((1 << (8 * i))) * ((int64_t )*(this->buffer + it++));
    }
    return val;
}

int64_t PerDecoder::decodeSmallNumber() {
    int64_t value;

    if ((*(this->buffer + it) << (8 - leftBits)) < 128) {
        value = (decodeBits(7) >> 1);
    } else {
        it++;
        value = decodeLength(0, INT_MAX);
    }
    return value;
}

int64_t PerDecoder::decodeLength(int64_t lowerBound, int64_t upperBound) {
    int64_t length;

    if (upperBound > 65535) {
        allignIterator();
        if ((unsigned char)*(buffer + it) < 128) {
            length = decodeValue(1);
        } else if ((unsigned char)*(buffer + it) < 192) {
            length = decodeValue(2) - 32768;
        } else { /* FIXME */
            while ((unsigned char)*(buffer + it) > 191) {
                length += (decodeValue(1) - 192) * 16384;
            }
            length += decodeLength(0, INT_MAX);
        }
    } else {
        length = decodeConstrainedValue(lowerBound, upperBound);
    }

    return length;

}

unsigned char PerDecoder::bitMask(unsigned char start, unsigned char end) {
    unsigned char mask = ((1 << (end - start)) - 1);
    return mask << start;
}

void PerDecoder::allignIterator() {
    if (leftBits != 8) {
        it++;
        leftBits = 8;
    }
}

C
calincerchez 已提交
165 166 167 168

bool PerDecoder::decodeAbstractType(AbstractType& abstractType) {
	switch(abstractType.getTag()) {
		case INTEGER:
C
calin cerchez 已提交
169
			return decodeInteger(dynamic_cast<IntegerBase&>(abstractType));
C
calincerchez 已提交
170
		case ENUMERATED:
C
calin cerchez 已提交
171
			return decodeEnumerated(dynamic_cast<EnumeratedBase&>(abstractType));
C
calincerchez 已提交
172
		case BITSTRING:
C
calin cerchez 已提交
173
			return decodeBitString(dynamic_cast<BitStringBase&>(abstractType));
C
calincerchez 已提交
174
		case OCTETSTRING:
C
calin cerchez 已提交
175
			return decodeOctetString(dynamic_cast<OctetStringBase&>(abstractType));
C
calin 已提交
176
		case _BOOLEAN:
C
calin.cerchez 已提交
177
		    return decodeBoolean(dynamic_cast<Boolean&>(abstractType));
178 179
		case _NULL:
		    return decodeNull(dynamic_cast<Null&>(abstractType));
C
calincerchez 已提交
180
		case SEQUENCE:
C
calin cerchez 已提交
181
			return decodeSequence(dynamic_cast<Sequence&>(abstractType));
C
calincerchez 已提交
182
		case SEQUENCEOF:
C
calin cerchez 已提交
183
			return decodeSequenceOf(dynamic_cast<SequenceOfBase&>(abstractType));
C
calincerchez 已提交
184
		case CHOICE:
C
calin cerchez 已提交
185
			return decodeChoice(dynamic_cast<Choice&>(abstractType));
C
calincerchez 已提交
186
		case PRINTABLESTRING:
C
calin cerchez 已提交
187
			return decodePrintableString(dynamic_cast<PrintableStringBase&>(abstractType));
C
calincerchez 已提交
188
		case OPENTYPE:
C
calin cerchez 已提交
189
			return decodeOpenType(dynamic_cast<OpenType&>(abstractType));
C
calincerchez 已提交
190 191 192 193 194 195 196 197
		default:
			return false;
	}
	return true;
}

bool PerDecoder::decodeOpenType(OpenType& openType) {
	openType.setLength(decodeLength(0, INT_MAX));
C
calin cerchez 已提交
198
	openType.setValue(buffer + it);
C
calincerchez 已提交
199 200 201 202
	it += openType.getLength();
	return true;
}

C
calin.cerchez 已提交
203 204 205 206 207
bool PerDecoder::decodeBoolean(Boolean& boolean) {
    boolean.setValue(decodeBits(1));
    return true;
}

208 209 210 211
bool PerDecoder::decodeNull(Null& null) {
    return true;
}

C
calincerchez 已提交
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 241 242 243 244 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
bool PerDecoder::decodeInteger(IntegerBase& integer) {
	if ((integer.isExtendable() && decodeBits(1))
			|| integer.getConstraintType() > EXTCONSTRAINED)
		integer.setValue(decodeUnconstrainedValue());
	else
		integer.setValue(decodeConstrainedValue(integer.getLowerBound(), integer.getUpperBound()));
	return true;
}

bool PerDecoder::decodeEnumerated(EnumeratedBase& enumerated) {
	if ((enumerated.isExtendable() && decodeBits(1)))
		enumerated.setValue(decodeSmallNumber());
	else
		enumerated.setValue(decodeConstrainedValue(0, enumerated.getUpperBound()));
	return true;
}

bool PerDecoder::decodeBitString(BitStringBase& bitString) {
	if ((bitString.isExtendable() && decodeBits(1))
			|| (bitString.getLowerBound() != bitString.getUpperBound())
			|| (bitString.getUpperBound() > 65536)
			|| (bitString.getConstraintType() == UNCONSTRAINED)) {
		int64_t upperBound = bitString.getConstraintType() == UNCONSTRAINED ? INT_MAX : bitString.getUpperBound();
		bitString.setLength(decodeLength(bitString.getLowerBound(), upperBound));
		bitString.setValue(decodeBytes((bitString.getLength() + 7) / 8));
	} else {
		if (!bitString.getLength()) {
			return true;
		} else if (bitString.getLength() < 17) {
			bitString.setValue(decodeSmallBitString(bitString.getLength()));
		} else {
			bitString.setValue(decodeBytes((bitString.getLength() + 7) / 8));
		}
	}
	return true;
}

bool PerDecoder::decodeOctetString(OctetStringBase& octetString) {
	if ((octetString.isExtendable() && decodeBits(1))
			|| (octetString.getLowerBound() != octetString.getUpperBound())
			|| (octetString.getUpperBound() > 65536)
			|| (octetString.getConstraintType() == UNCONSTRAINED)) {
		int64_t upperBound = octetString.getConstraintType() == UNCONSTRAINED ? INT_MAX : octetString.getUpperBound();
		octetString.setLength(decodeLength(octetString.getLowerBound(), upperBound));
		octetString.setValue(decodeBytes(octetString.getLength()));
	} else {
		if (!octetString.getUpperBound()) {
			return true;
		} else if (octetString.getUpperBound() < 3) {
			octetString.setValue(decodeSmallBitString(octetString.getUpperBound() * 8));
		} else {
			octetString.setValue(decodeBytes(octetString.getUpperBound()));
		}
	}
	return true;
}

bool PerDecoder::decodeSequence(Sequence& sequence) {
C
calin cerchez 已提交
270
    // TODO add support for extension
C
calincerchez 已提交
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 349 350 351 352 353
	bool hasExtension = false;
	if (sequence.isExtendable() && decodeBits(1))
		hasExtension = true;

	char *optFlags = sequence.getOptFlags();
	for (int64_t i = 0; i < sequence.getInfo()->sizeOpt; i += 8) {
		if (sequence.getInfo()->sizeOpt - i < 8) {
			optFlags[i / 8] = decodeBits(sequence.getInfo()->sizeOpt - i);
			break;
		} else
			optFlags[i / 8] = decodeBits(8);
	}
	int64_t optIndex = 0;
	for (int64_t i = 0; i < sequence.getInfo()->sizeRoot; i++) {
		if (sequence.isOptional(i)) {
			if (!sequence.getOptFlag(optIndex++))
				continue;
		}
		if (!decodeAbstractType(*(sequence.at(i))))
			return false;
	}

	if (hasExtension) {

	}
///*
//	if ((!sequence.getExtBit().isEmpty()) &&
//			sequence.getExtBit().get(0)) {
//
//		extSize = decodeSmallNumber();
//		extPresent = new BitString(CONSTRAINED, extSize, extSize, extSize);
//
//		if (!decodeBitString(extPresent))
//			return false;
//
//		sequence.setExtPresent(extPresent);
//
//		for (unsigned i = 0; i < extSize; i++)
//			if (!decodeAbstractType(sequence.getExtItem(i))) // trebuie OpenType
//				return false;
//
//	}
//*/
	return true;
}

int64_t PerDecoder::decodeSequenceOfSize(SequenceOfBase& sequenceOf) {
	int64_t size = 0;
	int64_t upperBound = sequenceOf.getUpperBound();

	if ((sequenceOf.getLowerBound() != sequenceOf.getUpperBound()) || (sequenceOf.getUpperBound() > 65536)) {
		if (sequenceOf.getConstraintType() == EXTCONSTRAINED && decodeBits(1))
			upperBound = INT_MAX;
		size = decodeLength(sequenceOf.getLowerBound(), upperBound);
	} else {
		size = sequenceOf.getUpperBound();
	}
	return size;
}

bool PerDecoder::decodeSequenceOf(SequenceOfBase& sequenceOf) {
	int64_t size = 0;

	if ((size = decodeSequenceOfSize(sequenceOf)) == -1)
		return false;

	for (int64_t i = 0; i < size; i++) {
		sequenceOf.push_back(sequenceOf.createItem());
		if (!decodeAbstractType(*(sequenceOf.at(i))))
			sequenceOf.pop_back();
	}
	return true;
}

bool PerDecoder::decodeChoiceValue(Choice& choice) {
	if (choice.isExtendable() && decodeBits(1)) {
		choice.createChoice(decodeSmallNumber());
	} else
		choice.createChoice(decodeConstrainedValue(0, choice.getUpperBound()));
	return true;
}

bool PerDecoder::decodeChoice(Choice& choice) {
C
calin cerchez 已提交
354
	// TODO add support for extension
C
calincerchez 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	if (!decodeChoiceValue(choice))
		return false;

	bool isExtension = choice.getChoice() > choice.getUpperBound() ? true : false;

	if ((choice.getTag() == CONSTRAINED) || (!isExtension)) {
		choice.createValue();
		if (!decodeAbstractType(*(choice.getValue())))
			return false;
	} else {
//		OpenType *openType = new OpenType();
//		openType.setEmpty(0);
//
//		if (!decodeOpenType(openType))
//			return false;
//
//		PerDecoder *perDec = new PerDecoder(openType.getValue());
//
//		if (!perDec.decodeAbstractType(choice.getItem()))
//			return false;
	}
	return true;
}

bool PerDecoder::decodePrintableString(PrintableStringBase& printableString) {
	if ((printableString.isExtendable() && decodeBits(1))
			|| (printableString.getLowerBound() != printableString.getUpperBound())
			|| (printableString.getUpperBound() > 65536)
			|| (printableString.getConstraintType() == UNCONSTRAINED)) {
		int64_t upperBound = printableString.getConstraintType() == UNCONSTRAINED ? INT_MAX : printableString.getUpperBound();
		int64_t len = decodeLength(printableString.getLowerBound(), upperBound);
		printableString.setValue(decodeBytes(len));
	} else {
		printableString.setValue(decodeBytes(printableString.getUpperBound()));
	}

	return true;
}