📄 asn-stringtype.cpp
字号:
if(tmpptr)
{
ptr += tmpptr;
}
else
{
alphafailed = 0;
}
}
else
{
alphafailed = 0;
}
if(sizefailed || alphafailed)
{
if(pConstraintFails!=NULL)
pConstraintFails->push_back(ptr);
return 1;
}
return 0;
}
/*
char* WideAsnString::checkStringTypSize(unsigned int m_Size)const
{
char* pError=NULL;
char cTmperr[300];
std::string utf8Form;
getAsUTF8(utf8Form);
if(m_Size>=(strlen(utf8Form.c_str())))
{
return pError;
}
else
{
sprintf(cTmperr, "_______\nRestricted Char String Type--Size Constraint:\n_______\nError: --String length must be less < or = to Size Constraint--\nValue: %d is not < or = to Size Constraint: %d \n", strlen(utf8Form.c_str()), m_Size);
pError = strdup(cTmperr);
return pError;
}
return pError;
}
char* WideAsnString::checkStringTypSize(unsigned int m_LowerSize, unsigned int m_UpperSize)const
{
char* pError=NULL;
char cTmperr[500];
std::string utf8Form;
getAsUTF8(utf8Form);
if(m_UpperSize>=(strlen(utf8Form.c_str())) && m_LowerSize<=(strlen(utf8Form.c_str())))
{
return pError;
}
else
{
sprintf(cTmperr, "_______\nRestricted Char String Type--Size Constraint:\n_______\nError: --String length must be between bounds--\nValue: %d is not between Lower Size Constraint: %d\n and Upper Size Constraint %d \n", strlen(utf8Form.c_str()), m_LowerSize, m_UpperSize);
pError = strdup(cTmperr);
return pError;
}
return pError;
}
*/
const char* AsnString::checkStringTypPermittedAlpha(const char* permittedAlphabet, long permittedAlphabetSize)const
{
const char* pError=NULL;
const char* cstr;
int found=0;
int x = 0;
int count = length();
if(count > 0)
{
cstr=c_str();
while(count)
{
found = 0;
for(x = 0; x < permittedAlphabetSize; x++)
{
if(permittedAlphabet[x] == cstr[count - 1])
{
found = 1;
}
}
if(found == 0)
break;
count--;
}
if(found == 1)
{
return pError;
}
else
{
return ConstraintErrorStringList[ STRING_PERMITTED_ALPHA ];
}
}
return pError;
}
int AsnString::checkConstraints (ConstraintFailList* pConstraintFails)const
{
int count = 0;
int sizefailed = 1;
int alphafailed = 1;
std::string ptr;
const char* tmpptr = NULL;
int numSizeConstraints;
const SizeConstraint* sizeConstraints = SizeConstraints(numSizeConstraints);
int sizePermittedAlpha;
const char* permittedAlphabet = PermittedAlphabet(sizePermittedAlpha);
if(sizeConstraints)
{
for(count = 0; count< numSizeConstraints; count++)
{
tmpptr = NULL;
if(sizeConstraints[count].upperBoundExists == 1)
{
if( ( sizeConstraints[count].lowerBound > (strlen(c_str())) ) ||
sizeConstraints[count].upperBound < (strlen(c_str())) )
{
tmpptr = ConstraintErrorStringList[ STRING_SIZE_VALUE_RANGE ];
}
}
else
{
if( sizeConstraints[count].lowerBound != (strlen(c_str())) )
{
tmpptr = ConstraintErrorStringList[ STRING_SIZE_SINGLE_VALUE ];
}
}
if(tmpptr)
{
ptr += tmpptr;
}
else
{
sizefailed = 0;
}
}
}
else
{
sizefailed = 0;
}
if(sizePermittedAlpha > 0)
{
tmpptr = NULL;
tmpptr = checkStringTypPermittedAlpha( permittedAlphabet, sizePermittedAlpha );
if(tmpptr)
{
ptr += tmpptr;
}
else
{
alphafailed = 0;
}
}
if(sizefailed || alphafailed)
{
if(pConstraintFails!=NULL)
pConstraintFails->push_back(ptr);
return 1;
}
return 0;
}
const char* IA5String::PermittedAlphabet(int &sizeAlpha) const
{
sizeAlpha = 128;
static const char IA5Alpha[]=
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
};
return IA5Alpha;
}
bool IA5String::check() const
{
for (const_iterator i = begin(); i != end(); ++i)
{
// Check that character is less than 128
if ( ((unsigned)*i < 0) || ((unsigned)*i > 127) )
return false;
}
return true;
}
const char* VisibleString::PermittedAlphabet(int &sizeAlpha) const
{
sizeAlpha = 95;
static const char VisibleAlpha[]=
{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e
};
return VisibleAlpha;
}
bool VisibleString::check() const
{
for (const_iterator i = begin(); i != end(); ++i)
{
// Check that character is between ' ' and '~' inclusive
if ((*i < ' ') || (*i > '~'))
return false;
}
return true;
}
// Encode BMPString octets into byte stream
//
AsnLen BMPString::BEncContent(AsnBuf &b) const
{
FUNC("BMPString::BEncContent");
const size_t wcharSize = sizeof(wchar_t);
AsnLen len = 0;
const_reverse_iterator wstrI;
for (wstrI = rbegin(); wstrI != rend(); ++wstrI)
{
wchar_t wc = *wstrI;
for (unsigned int i = 0; i < 2; ++i)
{
b.PutByteRvs((unsigned char)wc);
wc >>= 8;
++len;
}
// Check that the upper bytes, if any, are zero
if ((wcharSize > 2) && (wc != 0))
throw EXCEPT("Invalid BMPString", RESTRICTED_TYPE_ERROR);
}
return len;
}
void BMPString::BDecContent(const AsnBuf &b, AsnTag tagId, AsnLen elmtLen,
AsnLen &bytesDecoded)
{
FUNC("BMPString::BDecContent");
// Erase the existing characters
erase();
if (elmtLen == INDEFINITE_LEN || elmtLen > b.length())
{
throw MemoryException(elmtLen, "elmtLen requests for too much data", STACK_ENTRY);
}
// If tag is constructed, decode and combine the segments
std::string encStr;
if (TAG_IS_CONS(tagId))
{
bytesDecoded += CombineConsString(b, elmtLen, encStr);
}
else // tag is primitive, just combine the one segment
{
char *seg = new char[elmtLen];
if (seg == NULL)
throw MemoryException(elmtLen, "seg", STACK_ENTRY);
b.GetSeg(seg, elmtLen);
bytesDecoded += elmtLen;
encStr.append(seg, elmtLen);
delete[] seg;
}
// encoding length must be a multiple of two since BMPString uses
// 2 bytes to represent a character.
//
if (encStr.length() % 2 != 0)
{
throw EXCEPT("Invalid BMPString length not multiple of 2",
RESTRICTED_TYPE_ERROR);
}
// decode BMPString into wide string
//
resize(encStr.length() / 2);
std::string::const_iterator iEnc = encStr.begin();
for (size_type i = 0; i < size(); ++i)
{
wchar_t wtmpCh = *iEnc++;
wtmpCh <<= 8;
wtmpCh |= *iEnc++;
at(i) = wtmpCh;
}
}
// Encode UniversalString octets into byte stream
//
AsnLen UniversalString::BEncContent(AsnBuf &b) const
{
FUNC("UniversalString::BEncContent");
const size_t wcharSize = sizeof(wchar_t);
AsnLen len = 0;
const_reverse_iterator wstrI;
for (wstrI = rbegin(); wstrI != rend(); ++wstrI)
{
wchar_t wc = *wstrI;
for (unsigned int i = 0; i < 4; ++i)
{
if (i < wcharSize)
b.PutByteRvs((unsigned char)wc);
else
b.PutByteRvs(0);
wc >>= 8;
++len;
}
// Check that the upper bytes, if any, are zero
if ((wcharSize > 4) && (wc != 0))
throw EXCEPT("Invalid UniversalString", RESTRICTED_TYPE_ERROR);
}
return len;
}
void UniversalString::BDecContent(const AsnBuf &b, AsnTag tagId, AsnLen elmtLen,
AsnLen &bytesDecoded)
{
FUNC("UniversalString::BDecContent");
// Erase the existing characters
erase();
if (elmtLen == INDEFINITE_LEN || elmtLen > b.length())
{
throw MemoryException(elmtLen, "elmtLen requests for too much data", STACK_ENTRY);
}
// If tag is constructed, decode and combine the segments
std::string encStr;
if (TAG_IS_CONS(tagId))
{
bytesDecoded += CombineConsString(b, elmtLen, encStr);
}
else // tag is primitive, just combine the one segment
{
char *seg = new char[elmtLen];
if (seg == NULL)
throw MemoryException(elmtLen, "seg", STACK_ENTRY);
b.GetSeg(seg, elmtLen);
bytesDecoded += elmtLen;
encStr.append(seg, elmtLen);
delete[] seg;
}
// encoding length must be a multiple of four since UniversalString
// uses 4 bytes to represent a character.
//
if (encStr.length() % 4 != 0)
{
throw EXCEPT("Invalid UniversalString length not multiple of 4",
RESTRICTED_TYPE_ERROR);
}
const size_t wcharSize = sizeof(wchar_t);
// decode UniversalString into wide string
//
resize(encStr.length() / 4);
std::string::iterator iEnc = encStr.begin();
for (size_type i = 0; i < size(); ++i)
{
wchar_t wtmpCh = 0;
for (unsigned int iByte = 4; iByte > 0; --iByte)
{
// Check that the wchar_t won't overflow
if ((wcharSize - iByte) < 0)
{
if (*iEnc++ != 0)
{
throw EXCEPT("UniversalString not supported by platform wchar_t size",
RESTRICTED_TYPE_ERROR);
}
// else just skip the zero byte
}
else
{
wtmpCh <<= 8;
wtmpCh |= *iEnc++;
}
}
at(i) = wtmpCh;
}
}
AsnLen UTF8String::BEncContent(AsnBuf &b) const
{
std::string utf8;
getAsUTF8(utf8);
AsnLen len = utf8.length();
b.PutSegRvs(utf8.data(), len);
return len;
}
void UTF8String::BDecContent(const AsnBuf &b, AsnTag tagId, AsnLen elmtLen,
AsnLen &bytesDecoded)
{
FUNC("UTF8String::BDecContent()");
// Erase the existing characters
erase();
try
{
if (elmtLen == INDEFINITE_LEN || elmtLen > b.length())
{
throw MemoryException(elmtLen, "elmtLen requests for too much data", STACK_ENTRY);
}
// If tag is constructed, decode and combine the segments
std::string encStr;
if (TAG_IS_CONS(tagId))
{
bytesDecoded += CombineConsString(b, elmtLen, encStr);
}
else // tag is primitive, just combine the one segment
{
char *seg = new char[elmtLen];
if (seg == NULL)
throw MemoryException(elmtLen, "seg", STACK_ENTRY);
b.GetSeg(seg, elmtLen);
bytesDecoded += elmtLen;
encStr.append(seg, elmtLen);
delete[] seg;
}
// Decode this UTF-8 string and assign it to this object
set(encStr.c_str());
}
catch (SnaccException& snaccE) {
snaccE.push(STACK_ENTRY);
throw;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -