📄 asn-stringtype.cpp
字号:
#endif;RWC;*/
}
void AsnString::Print(std::ostream& os, unsigned short /*indent*/) const
{
os << c_str() << std::endl;
}
void AsnString::PrintXML(std::ostream &os, const char *lpszTitle) const
{
const char* title = lpszTitle;
if (title == NULL)
title = typeName();
os << "<" << title << ">" << c_str() << "</" << title << ">";
}
void AsnString::BDecConsString(const AsnBuf &b, AsnLen elmtLen, AsnLen &bytesDecoded)
{
FUNC("AsnString::BDecConsString()");
AsnLen totalElmtsLen = 0;
while ((totalElmtsLen < elmtLen) || (elmtLen == INDEFINITE_LEN))
{
AsnTag innerTag = BDecTag(b, totalElmtsLen);
if ((innerTag == EOC_TAG_ID) && (elmtLen == INDEFINITE_LEN))
{
BDEC_2ND_EOC_OCTET(b, totalElmtsLen);
break;
}
AsnLen innerLen = BDecLen(b, totalElmtsLen);
if (innerTag == MAKE_TAG_ID (UNIV, PRIM, OCTETSTRING_TAG_CODE))
{
char *seg = new char[innerLen];
if (seg == NULL)
throw MemoryException(elmtLen, "seg", STACK_ENTRY);
b.GetSeg(seg, innerLen);
totalElmtsLen += innerLen;
append(seg, innerLen);
delete[] seg;
}
else if (innerTag == MAKE_TAG_ID (UNIV, CONS, OCTETSTRING_TAG_CODE))
{
BDecConsString(b, innerLen, totalElmtsLen);
}
else // wrong tag
throw InvalidTagException(typeName(), innerTag, STACK_ENTRY);
}
bytesDecoded += totalElmtsLen;
} // end of AsnString::BDecConsString()
void WideAsnString::PDec(AsnBufBits &b, AsnLen &bitsDecoded)
{
DecodeGeneral(b, bitsDecoded);
}
AsnLen WideAsnString::PEnc(AsnBufBits &b) const
{
FUNC("WideAsnString::PEnc");
if(checkConstraints(NULL))
throw ConstraintException("Wide string not within constraints", STACK_ENTRY);
return EncodeGeneral(b);
}
AsnLen WideAsnString::BEnc(AsnBuf &b) const
{
FUNC("WideAsnString::BEnc");
if(checkConstraints(NULL))
throw ConstraintException("Wide string not within constraints", STACK_ENTRY);
AsnLen l = BEncContent(b);
l += BEncDefLen(b, l);
l += BEncTag1(b, UNIV, PRIM, tagCode());
return l;
}
void WideAsnString::BDec(const AsnBuf &b, AsnLen &bytesDecoded)
{
FUNC("WideAsnString::BDec()");
AsnTag tag = BDecTag(b, bytesDecoded);
if ((tag != MAKE_TAG_ID (UNIV, PRIM, tagCode())) &&
(tag != MAKE_TAG_ID (UNIV, CONS, tagCode())))
{
throw InvalidTagException(typeName(), tag, STACK_ENTRY);
}
AsnLen elmtLen1 = BDecLen(b, bytesDecoded);
BDecContent(b, tag, elmtLen1, bytesDecoded);
}
void WideAsnString::Print(std::ostream& os, unsigned short /*indent*/) const
{
std::string utf8Form;
getAsUTF8(utf8Form);
os << utf8Form.c_str() << std::endl;
}
void WideAsnString::PrintXML(std::ostream &os, const char *lpszTitle) const
{
const char *title = lpszTitle;
if (title == NULL)
title = typeName();
std::string utf8Form;
getAsUTF8(utf8Form);
os << "<" << title << ">" << utf8Form.c_str() << "</" << title << ">";
}
// Set the wide-character string from the UTF-8 string
void WideAsnString::set(const char* str)
{
FUNC("WideAsnString::set()");
const size_t wcharSize = sizeof(wchar_t);
// Erase the existing characters
erase();
// If the string is NULL, just return
if (str == NULL)
return;
// Reserve space for a worst case wchar_t string
unsigned int strLength = strlen(str);
reserve(strLength);
// Convert the UTF-8 string to a wchar_t string
unsigned int i = 0;
while (i < strLength)
{
// Determine the number of UTF-8 octets that follow the first
unsigned short j;
for (j = 0; (j < MAX_UTF8_OCTS_PER_CHAR) &&
((gUTF8Masks[j].mask & str[i]) != gUTF8Masks[j].value); ++j)
;
// Throw an exception if the first octet was invalid or if the
// number of subsequent octets exceeds the UTF-8 string length
if ((j == MAX_UTF8_OCTS_PER_CHAR) || ((i + j) >= strLength))
throw EXCEPT("Invalid UTF-8 string", RESTRICTED_TYPE_ERROR);
// Throw an exception if the size of the wchar_t doesn't support the
// size of this UTF-8 character
//
if ( abs((j * 6) - gUTF8Masks[j].bits) > (wcharSize*8))
{
throw EXCEPT("UTF-8 character too large for wchar_t",
RESTRICTED_TYPE_ERROR);
}
// Copy the bits from the first octet into a temp wide character
wchar_t tmp = (char)(~gUTF8Masks[j].mask & str[i++]);
// Add in the bits from each subsequent octet
for (; j > 0; j--)
{
// Throw an exception if the subsequent octet isn't properly
// formatted
if ((str[i] & 0xC0) != 0x80)
throw EXCEPT("Invalid UTF-8 string", RESTRICTED_TYPE_ERROR);
tmp <<= 6;
tmp |= str[i++] & 0x3F;
}
append(1, tmp);
}
}
wchar_t* WideAsnString::getWideChar(long offset)const
{
return (wchar_t*)&(*this)[offset];
}
// Convert wide character string to UTF-8 character string
//
void WideAsnString::getAsUTF8(std::string& utf8String) const
{
FUNC("WideAsnString::getAsUTF8()");
// Size the resulting string for a worst case UTF-8 string
utf8String.resize(length() * MAX_UTF8_OCTS_PER_CHAR);
// Convert each wide character into a UTF-8 char sequence
std::string::size_type x = 0;
for (const_iterator i = begin(); i != end(); ++i)
{
// Return an error if the wide character is invalid
if (*i < 0)
throw EXCEPT("Invalid wide character", RESTRICTED_TYPE_ERROR);
// Determine the number of characters required to encode this
// wide character
unsigned int j;
for (j = 0; (j < MAX_UTF8_OCTS_PER_CHAR) &&
((unsigned)*i > (unsigned)gUTF8Masks[j].maxCharValue); ++j)
;
// Return an error if the wide character is invalid
if (j == MAX_UTF8_OCTS_PER_CHAR)
throw EXCEPT("Invalid wide character", RESTRICTED_TYPE_ERROR);
/* Skip over the first UTF-8 octet and encode the remaining octets
(if any) from right-to-left. Fill in the least significant six bits
of each octet with the low-order bits from the wide character value */
wchar_t temp_wchar = *i;
for (unsigned int y = j; y > 0; --y)
{
utf8String[x + y] = (char)(0x80 | (temp_wchar & 0x3F));
temp_wchar >>= 6;
}
// Encode the first UTF-8 octet
utf8String[x] = gUTF8Masks[j].value;
utf8String[x++] |= ~gUTF8Masks[j].mask & temp_wchar;
// Update the UTF-8 string index (skipping over the subsequent octets
// already encoded)
x += j;
}
// Resize the string to the correct length
utf8String.resize(x);
} // end of WideAsnString::getAsUTF8()
void WideAsnString::Deterpret(AsnBufBits &b, AsnLen &bitsDecoded, long offset)
{
wchar_t* seg = (wchar_t*)b.GetBits(sizeof(wchar_t));
bitsDecoded += (sizeof(wchar_t));
putWideChar(seg);
}
char* WideAsnString::getAsUTF8() const
{
std::string utf8Form;
getAsUTF8(utf8Form);
return strdup(utf8Form.c_str());
} // end of WideAsnString::getAsUTF8()
AsnLen WideAsnString::CombineConsString(const AsnBuf &b, AsnLen elmtLen,
std::string& encStr)
{
FUNC("WideAsnString::CombineConsString()");
AsnLen totalElmtsLen = 0;
while ((totalElmtsLen < elmtLen) || (elmtLen == INDEFINITE_LEN))
{
AsnTag innerTag = BDecTag(b, totalElmtsLen);
if ((innerTag == EOC_TAG_ID) && (elmtLen == INDEFINITE_LEN))
{
BDEC_2ND_EOC_OCTET(b, totalElmtsLen);
break;
}
AsnLen innerLen = BDecLen(b, totalElmtsLen);
if (innerTag == MAKE_TAG_ID (UNIV, PRIM, OCTETSTRING_TAG_CODE))
{
char *seg = new char[elmtLen];
if (seg == NULL)
throw MemoryException(elmtLen, "seg", STACK_ENTRY);
b.GetSeg(seg, elmtLen);
totalElmtsLen += elmtLen;
encStr.append(seg, elmtLen);
innerLen -= elmtLen;
delete[] seg;
}
else if (innerTag == MAKE_TAG_ID (UNIV, CONS, OCTETSTRING_TAG_CODE))
{
totalElmtsLen += CombineConsString(b, innerLen, encStr);
}
else // wrong tag
throw InvalidTagException(typeName(), innerTag, STACK_ENTRY);
}
return totalElmtsLen;
} // end of WideAsnString::CombineConsString()
bool NumericString::check() const
{
for (const_iterator i = begin(); i != end(); ++i)
{
// Check for 0-9
if ((*i < '0') || (*i > '9'))
{
// Check for space
if (*i != ' ')
return false;
}
}
return true;
}
const char* NumericString::PermittedAlphabet(int &alphaSize) const
{
alphaSize = 11;
static const char kNumAlpha[] = " 0123456789";
return kNumAlpha;
}
const SizeConstraint* NumericString::SizeConstraints(int &sizeList)const
{
sizeList = 0;
return NULL;
}
const char* PrintableString::PermittedAlphabet(int &sizeAlpha) const
{
sizeAlpha = 74;
static char pPbleAlpha[] =
{0x20, 0x27, 0x28, 0x29, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3D, 0x3F,
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, 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};
return pPbleAlpha;
}
bool PrintableString::check() const
{
for (const_iterator i = begin(); i != end(); ++i)
{
// Check for A-Z
if ((*i < 'A') || (*i > 'Z'))
{
// Check for a-z
if ((*i < 'a') || (*i > 'z'))
{
// Check for 0-9
if ((*i < '0') || (*i > '9'))
{
switch (*i)
{
case ' ': // space
case '\'': // apostrophe
case '(': // left parenthesis
case ')': // right parenthesis
case '+': // plus sign
case ',': // comma
case '-': // hyphen-minus
case '.': // full stop (period)
case '/': // solidus
case ':': // colon
case '=': // equal sign
case '?': // question mark
break;
default:
return false;
}
}
}
}
}
return true;
}
AsnLen WideAsnString::Interpret(AsnBufBits &b, long offset)const
{
AsnLen len = sizeof(wchar_t);
unsigned char* seg = (unsigned char*)getWideChar(offset);
b.PutBits(seg, len);
return len;
}
const char* WideAsnString::checkStringTypPermittedAlpha(const char* permittedAlphabet, long permittedAlphabetSize)const
{
char* pError=NULL;
const char* cstr;
int found=0;
int x = 0;
int count=length();
if(count > 0)
{
std::string utf8Form;
getAsUTF8(utf8Form);
cstr=utf8Form.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[ WIDE_STRING_PERMITTED_ALPHA ];;
}
}
return pError;
}
int WideAsnString::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(getAsUTF8())) ) ||
(sizeConstraints[count].upperBound < (strlen(getAsUTF8())) ) )
{
tmpptr = ConstraintErrorStringList[ WIDE_STRING_SIZE_VALUE_RANGE ];
}
}
else
{
if(sizeConstraints[count].lowerBound != (strlen(getAsUTF8()) ) )
{
tmpptr = ConstraintErrorStringList[ WIDE_STRING_SIZE_SINGLE_VALUE ];
}
}
if(tmpptr)
{
ptr += tmpptr;
}
else
{
sizefailed = 0;
}
}
}
else
{
sizefailed = 0;
}
if(sizePermittedAlpha > 0)
{
tmpptr = NULL;
tmpptr = checkStringTypPermittedAlpha( permittedAlphabet, sizePermittedAlpha );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -