// // File: asn-listset.h // // Contents: AsnList, AsnSeqOf, AsnSetOf template classes // // #ifndef SNACC_ASN_LISTSET_H #define SNACC_ASN_LISTSET_H #ifdef _MSC_VER #pragma warning(disable: 4786) // Disable symbols truncated warning #endif #ifdef WIN32 #include #include #include #endif ///////////////////////////////// // Abstract AsnList template // /////////////////////////////// template class AsnList : public std::list, public SNACC::AsnType, public SNACC::PERGeneral { public: // Appends newly inserted element to list and returns its iterator typename std::list::iterator append(const T& x = T()) { return this->insert(this->end(), x); } // encode and decode routines virtual SNACC::AsnLen PEnc(SNACC::AsnBufBits& b) const; virtual SNACC::AsnLen BEnc(SNACC::AsnBuf& b) const = 0; virtual SNACC::AsnLen BEncContent(SNACC::AsnBuf& b) const; virtual void PDec(SNACC::AsnBufBits& b, SNACC::AsnLen& bytesDecoded); virtual void BDec(const SNACC::AsnBuf& b, SNACC::AsnLen& bytesDecoded) = 0; virtual void BDecContent(const SNACC::AsnBuf& b, SNACC::AsnTag tagId, SNACC::AsnLen elmtLen, SNACC::AsnLen& bytesDecoded); virtual const char* typeName() const = 0; virtual int checkConstraints(SNACC::ConstraintFailList* pConstraintFails) const; void PrintXML(std::ostream& os, const char* lpszTitle = NULL) const; void Print(std::ostream& os, unsigned short indent) const; // int checkListConstraints(SNACC::ConstraintFailList* pConstraintFails) const; /*JKG--8/12/03--*/ char* checkSOfVRange(long m_Lower, long m_Upper) const; char* checkSOfSingleVal(long m_SingleVal) const; virtual SNACC::SizeConstraint* SizeConstraints() const { return NULL; } // virtual SNACC::SizeConstraint* SizeConstraints() { return NULL; } virtual void Allocate(long size) {} virtual void Clear() { this->clear(); } virtual SNACC::AsnLen Interpret(SNACC::AsnBufBits& b, long offset) const; virtual void Deterpret(SNACC::AsnBufBits& b, SNACC::AsnLen& bitsDecoded, long offset); // virtual long lEncLen() const { return size(); } }; ////////////////////////////////// // Abstract AsnList template // // CODE // /////////////////////////////// template void AsnList::Deterpret(SNACC::AsnBufBits& b, SNACC::AsnLen& bitsDecoded, long offset) { append()->PDec(b, bitsDecoded); } template SNACC::AsnLen AsnList::Interpret(SNACC::AsnBufBits& b, long offset) const { FUNC("AsnList::Interpret()"); typename AsnList::const_iterator i = this->begin(); while ((i != this->end()) && (offset > 0)) { ++i; --offset; } if (i != this->end()) return i->PEnc(b); else throw SNACC_EXCEPT("Null element found in List"); } template SNACC::AsnLen AsnList::PEnc(SNACC::AsnBufBits& b) const { FUNC("AsnList::PEnc()"); if (!SizeConstraints()) return EncodeGeneral(b); else { SNACC::AsnLen sum = 0; if (SizeConstraints()->upperBoundExists == 0) { if (this->size() != (unsigned int) SizeConstraints()->lowerBound) { throw EXCEPT("Number of elements in AsnList does not match singlevalue constraint", ENCODE_ERROR); } for (typename AsnList::const_iterator i = this->begin(); i != this->end(); ++i) sum += i->PEnc(b); } else if (SizeConstraints()->upperBoundExists == 1) { if ((this->size() < SizeConstraints()->lowerBound) || (this->size() > SizeConstraints()->upperBound)) { throw EXCEPT("Number of elements in AsnList is not within the size constraint", ENCODE_ERROR); } unsigned char pStr[] = {0x00, 0x00, 0x00, 0x00}; long tempRange = SizeConstraints()->upperBound - SizeConstraints()->lowerBound; int minBitsNeeded = 0; while (tempRange > 0) { tempRange -= (long)(1 << minBitsNeeded); minBitsNeeded += 1; } sum += b.OctetAlignWrite(); long minBytesNeeded = minBitsNeeded / 8; minBitsNeeded %= 8; long numExtra = this->size() - SizeConstraints()->lowerBound; if (minBytesNeeded > 0) { pStr[0] = (unsigned char)(numExtra >> minBitsNeeded); sum += b.PutBits(pStr, 8); } pStr[0] = (unsigned char)numExtra; pStr[0] <<= 8 - minBitsNeeded; sum += b.PutBits(pStr, minBitsNeeded); sum += b.OctetAlignWrite(); } else { if (this->size() < SizeConstraints()->lowerBound) { throw EXCEPT("Number of elements in AsnList is below the minimum size constraint", ENCODE_ERROR); } SNACC::AsnInt listLen(this->size() - SizeConstraints()->lowerBound); sum += b.OctetAlignWrite(); listLen.PEnc(b); sum += b.OctetAlignWrite(); } // Encode the elements for (typename AsnList::const_iterator i = this->begin(); i != this->end(); ++i) sum += i->PEnc(b); return sum; } } // template void AsnList::PDec(SNACC::AsnBufBits& b, SNACC::AsnLen& bitsDecoded) { // Remove existing elements this->clear(); if (!SizeConstraints()) { DecodeGeneral(b, bitsDecoded); } else if (SizeConstraints()->upperBoundExists == 0) { for (unsigned int i = 0; i < SizeConstraints()->lowerBound; i++) append()->PDec(b, bitsDecoded); } else if (SizeConstraints()->upperBoundExists == 1) { long tempRange = SizeConstraints()->upperBound - SizeConstraints()->lowerBound; int minBitsNeeded = 0; int minBytesNeeded = 0; long decodeSize = 0; unsigned char *pStr; while (tempRange > 0) { tempRange -= (long)(1 << minBitsNeeded); minBitsNeeded += 1; } minBytesNeeded = minBitsNeeded / 8; minBitsNeeded = minBitsNeeded % 8; bitsDecoded += b.OctetAlignRead(); if (minBytesNeeded > 0) { pStr = b.GetBits(8); bitsDecoded += 8; decodeSize <<= 8; decodeSize |= (long)pStr[0]; delete [] pStr; } pStr = b.GetBits(minBitsNeeded); bitsDecoded += minBitsNeeded; if (minBitsNeeded > 0) { decodeSize <<= minBitsNeeded; pStr[0] >>= (8 - minBitsNeeded); decodeSize |= (long)pStr[0]; } decodeSize += SizeConstraints()->lowerBound; bitsDecoded += b.OctetAlignRead(); for (int i = 0; i < decodeSize; i++) append()->PDec(b, bitsDecoded); delete [] pStr; } else { bitsDecoded += b.OctetAlignRead(); SNACC::AsnInt listLen; listLen.PDec(b, bitsDecoded); bitsDecoded += b.OctetAlignRead(); int tempLen = listLen + SizeConstraints()->lowerBound; for (int i = 0; i < tempLen; i++) append()->PDec(b, bitsDecoded); } } template char* AsnList::checkSOfSingleVal(long m_SingleVal) const { char* pError = NULL; if (this->size() != m_SingleVal) { char cTmperr[200]; sprintf(cTmperr, "_______\nList--SingleValue Constraints:\n_______\nError: --Invalid Number of Elements in List--\nNumber of Elements: %d must match the Constraint Single Value: %d \n", this->size(), m_SingleVal); pError = strdup(cTmperr); } return pError; } template char* AsnList::checkSOfVRange(long m_Lower, long m_Upper) const { char* pError = NULL; if (this->size() < m_Lower) { char cTmperr[200]; sprintf(cTmperr, "_______\nList--Valuerange Constraints:\n_______\nError: --Not Enough Elements In List--\nNumber of Elements: %d is below the Lower Limit: %d \n", this->size(), m_Lower); pError = strdup(cTmperr); } if (this->size() > m_Upper) { char cTmperr[200]; sprintf(cTmperr, "_______\nList--Valuerange Constraints:\n_______\nError: --Too Many Elements In List--\nNumber of Elements: %d is above the Upper Limit: %d \n", this->size(), m_Upper); pError = strdup(cTmperr); } return pError; } template int AsnList::checkConstraints(SNACC::ConstraintFailList* pConstraintFails) const { for (typename AsnList::const_iterator i = this->begin(); i != this->end(); ++i) i->checkConstraints(pConstraintFails); return 0; } template SNACC::AsnLen AsnList::BEncContent(SNACC::AsnBuf& b) const { SNACC::AsnLen sum = 0; for (typename AsnList::const_reverse_iterator i = this->rbegin(); i != this->rend(); ++i) sum += i->BEnc(b); return sum; } template void AsnList::BDecContent(const SNACC::AsnBuf& b, SNACC::AsnTag , SNACC::AsnLen elmtLen, SNACC::AsnLen& bytesDecoded) { SNACC::AsnLen localBytesDecoded = 0; while ((elmtLen == INDEFINITE_LEN) || (localBytesDecoded < elmtLen)) { if (elmtLen == INDEFINITE_LEN) { if (b.PeekByte() == EOC_TAG_ID) { SNACC::BDecEoc(b, localBytesDecoded); break; } } append()->BDec(b, localBytesDecoded); } bytesDecoded += localBytesDecoded; } // AsnList::BDecContent() // template void AsnList::Print(std::ostream& os, unsigned short indent) const { os << "OF "; ++indent; if (this->empty()) os << "is EMPTY\n"; else { os << this->front().typeName() << " \n"; for (typename AsnList::const_iterator i = this->begin(); i != this->end(); ++i) { SNACC::Indent(os, indent); i->Print(os, indent); } SNACC::Indent(os, --indent); } os << "}\n"; } // AsnList::Print() template void AsnList::PrintXML(std::ostream& os, const char* lpszTitle) const { if (this->empty()) os << "-- Void --\n"; else { //os << "<" << first->elmt->typeName() << ">" << endl; for (typename AsnList::const_iterator i = this->begin(); i != this->end(); ++i) i->PrintXML(os, lpszTitle); //os << "elmt->typeName() << ">" << endl; } } // AsnList::PrintXML() ////////////////////////// // AsnSeqOf template // //////////////////////// template #if (_MSC_VER <= 1200) // 6.0 or earlier class SNACCDLL_API AsnSeqOf : public AsnList #else class AsnSeqOf : public AsnList #endif { public: SNACC::AsnLen BEnc(SNACC::AsnBuf &b) const; void BDec(const SNACC::AsnBuf &b, SNACC::AsnLen &bytesDecoded); void Print(std::ostream& os, unsigned short indent = 0) const; void PrintXML(std::ostream& os, const char* lpszTitle = NULL) const; //virtual int checkConstraints(ConstraintFailList* pConstraintFails)const; }; ////////////////////////// // AsnSeqOf template // // CODE // //////////////////////// template SNACC::AsnLen AsnSeqOf::BEnc(SNACC::AsnBuf& b) const { SNACC::AsnLen l = this->BEncContent(b); l += SNACC::BEncDefLen(b, l); l += BEncTag1(b, SNACC::UNIV, SNACC::CONS, SNACC::SEQ_TAG_CODE); return l; } template void AsnSeqOf::BDec(const SNACC::AsnBuf& b, SNACC::AsnLen& bytesDecoded) { // Clear the existing contents this->clear(); SNACC::AsnTag tagId = SNACC::BDecTag(b, bytesDecoded); if (tagId != MAKE_TAG_ID (SNACC::UNIV, SNACC::CONS, SNACC::SEQ_TAG_CODE)) { throw SNACC::InvalidTagException(this->typeName(), tagId, __FILE__, __LINE__, "AsnSeqOf:BDec()"); } SNACC::AsnLen elmtLen; elmtLen = SNACC::BDecLen(b, bytesDecoded); this->BDecContent(b, tagId, elmtLen, bytesDecoded); } template void AsnSeqOf::Print(std::ostream& os, unsigned short indent) const { os << "{ -- " << this->typeName() << " SEQUENCE OF "; ++indent; if (this->empty()) os << "is EMPTY\n"; else { os << this->front().typeName() << " \n"; for (typename AsnSeqOf::const_iterator i = this->begin(); i != this->end(); ++i) { SNACC::Indent(os, indent); i->Print(os, indent); } SNACC::Indent(os, --indent); } os << "}\n"; } template void AsnSeqOf::PrintXML(std::ostream& os, const char* lpszTitle) const { if (lpszTitle) { os << "<" << lpszTitle; os << " typeName=\"" << this->typeName() << "\""; } else os << "<" << this->typeName(); os << " type=\"SEQUENCE_OF\">\n"; AsnList::PrintXML(os, lpszTitle); if (lpszTitle) os << ""; else os << "typeName() << ">"; } //////////////////////// // AsnSetOf template // ////////////////////// template #if (_MSC_VER <= 1200) // 6.0 or earlier class SNACCDLL_API AsnSetOf : public AsnList #else class AsnSetOf : public AsnList #endif { public: SNACC::AsnLen PEnc(SNACC::AsnBufBits& b) const; SNACC::AsnLen BEnc(SNACC::AsnBuf& b) const; SNACC::AsnLen BEncContent(SNACC::AsnBuf& b) const; void BDec(const SNACC::AsnBuf& b, SNACC::AsnLen& bytesDecoded); void PDec(SNACC::AsnBufBits& b, SNACC::AsnLen& bitsDecoded); void Print(std::ostream& os, unsigned short indent = 0) const; void PrintXML(std::ostream& os, const char* lpszTitle = NULL) const; //virtual int checkCosntraints(ConstraintFailList* pConstraintFails); }; //////////////////////// // AsnSetOf template // // CODE // ////////////////////// template SNACC::AsnLen AsnSetOf::BEnc(SNACC::AsnBuf& b) const { SNACC::AsnLen l = BEncContent(b); l += SNACC::BEncDefLen(b, l); l += BEncTag1(b, SNACC::UNIV, SNACC::CONS, SNACC::SET_TAG_CODE); return l; } template SNACC::AsnLen AsnSetOf::BEncContent(SNACC::AsnBuf& b) const { SNACC::AsnLen totalLen = BEncEocIfNec(b); // Encode each component of the SET OF into the AsnBuf list; std::list bufList; for (typename AsnSetOf::const_iterator i = this->begin(); i != this->end(); ++i) { SNACC::AsnBuf& bufRef = *bufList.insert(bufList.end(), SNACC::AsnBuf()); totalLen += i->BEnc(bufRef); } // Sort array of AsnBuf according to DER rules for SET OF // bufList.sort(); std::list::reverse_iterator ri; for (ri = bufList.rbegin(); ri != bufList.rend(); ++ri) b.splice(*ri); return totalLen; } template void AsnSetOf::BDec(const SNACC::AsnBuf& b, SNACC::AsnLen& bytesDecoded) { // Clear the existing elements this->clear(); SNACC::AsnTag tagId = SNACC::BDecTag(b, bytesDecoded); if (tagId != MAKE_TAG_ID (SNACC::UNIV, SNACC::CONS, SNACC::SET_TAG_CODE)) { throw SNACC::InvalidTagException(this->typeName(), tagId, __FILE__,__LINE__,"AsnSetOf::BDec()"); } SNACC::AsnLen elmtLen; elmtLen = SNACC::BDecLen(b, bytesDecoded); this->BDecContent(b, tagId, elmtLen, bytesDecoded); } // // template void AsnSetOf::PDec(SNACC::AsnBufBits& b, SNACC::AsnLen& bitsDecoded) { // Clear the existing elements this->clear(); SNACC::AsnInt intSetOfLength; intSetOfLength.PDec(b, bitsDecoded); for (int i = 0; i < intSetOfLength; i++) this->append()->PDec(b, bitsDecoded); } template SNACC::AsnLen AsnSetOf::PEnc(SNACC::AsnBufBits& b) const { SNACC::AsnInt intSetOfLength(this->size()); intSetOfLength.PEnc(b); SNACC::AsnLen totalLen = 0; std::list bufList; // Encode each component of the SET OF into the AsnBuf list for (typename AsnSetOf::const_iterator i = this->begin(); i != this->end(); ++i) { SNACC::AsnBufBits& bufRef = *bufList.insert(bufList.end(), SNACC::AsnBufBits(b.IsAligned())); totalLen += i->PEnc(bufRef); } bufList.sort(); std::list::iterator iBuf; for (iBuf = bufList.begin(); iBuf != bufList.end(); ++iBuf) iBuf->AppendTo(b); return totalLen; } // end of AsnSetOf::PEnc() template void AsnSetOf::Print(std::ostream& os, unsigned short indent) const { os << "{ -- " << this->typeName() << " SET OF "; ++indent; if (this->empty()) os << "is EMPTY\n"; else { os << this->front().typeName() << " \n"; for (typename AsnSetOf::const_iterator i = this->begin(); i != this->end(); ++i) { SNACC::Indent(os, indent); i->Print(os, indent); } SNACC::Indent(os, --indent); } os << "}\n"; } template void AsnSetOf::PrintXML(std::ostream& os, const char* lpszTitle) const { if (lpszTitle) { os << "<" << lpszTitle; os << " typeName=\"" << this->typeName() << "\""; } else os << "<" << this->typeName(); os << " type=\"SET_OF\">"; AsnList::PrintXML(os); if (lpszTitle) os << ""; else os << "typeName() << ">"; } /* template int ListsEquiv (AsnList& l1, AsnList& l2) { if (l1.Count() != l2.Count()) return false; l1.SetCurrToFirst(); l2.SetCurrToFirst(); for (; l1.Curr() != NULL; l1.GoNext(), l2.GoNext()) { if (*l1.Curr() != *l2.Curr()) { return false; } } return true; } */ #endif // SNACC_ASN_LISTSET_H