📄 looseprs.cpp
字号:
{ if(end - pos < 8) { buf = pos; return; } pos = (const char*)xmlStr.GetNextChar(uLen); if(strncasecmp(pos, "include", 7) == 0) { pos += 7; m_comment_get_arg = 1; m_comment_pos = 0; strcpy(m_comment_command, "include"); } } } break; case 2: if(*pos == '-') m_comment_state = 3; else m_comment_state = 1; break; case 3: if(*pos == '>') { nCommentDepth--; if (nCommentDepth == 0) { m_comment_state = 0; buf = (const char*)xmlStr.GetNextChar(uLen); } else m_comment_state = 1; } else m_comment_state = 1; break; case 4: // Ignore nested comments while looking for our end tag if (*pos == '!') m_comment_state = 5; else m_comment_state = 1; break; case 5: if (*pos == '-') m_comment_state = 6; else m_comment_state = 1; break; case 6: if (*pos == '-') { nCommentDepth++; } m_comment_state = 1; break; } if(m_comment_state > 0) { switch(m_comment_get_arg) { case 1: if(*pos != '"' && !isspace(*pos)) m_comment_get_arg = 0; else if(*pos == '"') m_comment_get_arg = 2; break; case 2: if(*pos != '"') if (m_comment_pos < 1023) m_comment_arg[m_comment_pos++] = *pos; else { if (m_comment_pos < 1024) m_comment_arg[m_comment_pos] = 0; m_comment_get_arg = 3; } break; default: break; } } pos = (const char*)xmlStr.GetNextChar(uLen); }}XMLParseResultXMLParser::Parse(const char*& buf, UINT32 len, XMLTag*& tag, BOOL bIsFinal){ const char* open; const char* close; const char* cur; const char* afterclose; tag = NULL; if(m_comment_state > 0) { FindCommentClose(buf, buf, buf+len); if(m_comment_state != 0) { SetError(m_pLastError, XMLErrorNoClose, 0, 0, buf, len, 0); return XMLPNoClose; } else if(m_comment_get_arg != 3) { tag = new XMLTag(m_bStrictCompliance, m_bStoreErrors); tag->new_attribute()->value = new_string(""); // dummy tag return XMLPComment; } // Got a comment command tag = new XMLTag(m_bStrictCompliance, m_bStoreErrors); tag->new_attribute()->value = new_string(m_comment_arg); tag->m_cur_attribute->name = new_string(m_comment_command); return XMLPComment; } if(*buf != '<') { // If there isn't a tag right away, tell the user there's just plain // text here. UINT32 ulLine = 0; UINT32 ulCol = 0; const char* errPos = NULL; UINT32 errLen = 0; cur = buf; while(((UINT32)(cur - buf) < len) && (*cur != '<')) { if(*cur == '\n') { m_ulCurrentLine++; m_ulCurrentCol = 1; } else { m_ulCurrentCol++; } if (m_bStoreErrors) { // check for ]]> // validate refferences. if (cur - buf > 3 && *cur == ']' && *(cur+1) == ']' && *(cur+2) == '>' && !errPos) { ulLine = m_ulCurrentLine; ulCol = m_ulCurrentCol; errPos = buf; errLen = len; } } cur++; } tag = new XMLTag(m_bStrictCompliance, m_bStoreErrors); char* pText = new char[cur - buf + 1]; strncpy(pText, buf, cur - buf); /* Flawfinder: ignore */ pText[cur - buf] = '\0'; tag->new_attribute()->value = new_string(pText); if (errPos) { XMLError* err = NULL; SetError(err, XMLErrorInvalidGTafter2RSQB, ulLine, ulCol, errPos, errLen, 0); tag->m_errs->Add(err); } delete [] pText; buf = cur; return XMLPPlainText; } open = buf; CHXXMLEncode xmlStr(m_pEncoding, (BYTE*)open, len); UINT16 uLen = 0; m_ulTagStartLine = m_ulCurrentLine; m_ulTagStartCol = m_ulCurrentCol; BOOL bInDoubleQuote = FALSE; BOOL bInSingleQuote = FALSE; BOOL bInComment = FALSE; BOOL bInDeclaration = FALSE; UINT16 nCommentDepth = 0; UINT32 ulLine = 0; UINT32 ulCol = 0; const char* errPos = NULL; UINT32 errLen = 0; if(*(open+1) && *(open+1) == '!' && *(open+2) && *(open+2) == '-' && *(open+3) && *(open+3) == '-') { // '<!--' starts a comment bInComment = TRUE; } for(close = (const char*)xmlStr.GetNextChar(uLen); close < buf+len; close = (const char*)xmlStr.GetNextChar(uLen)) { if(*close == '\n') { m_ulCurrentLine++; m_ulCurrentCol = 1; } else { m_ulCurrentCol++; } if(*close == '"' && !bInComment) { if(!bInSingleQuote) { if(bInDoubleQuote) { bInDoubleQuote = FALSE; } else { bInDoubleQuote = TRUE; } } } else if(*close == '\'' && !bInComment) { if(!bInDoubleQuote) { if(bInSingleQuote) { bInSingleQuote = FALSE; } else { bInSingleQuote = TRUE; } } } else if(*close == '[' && !bInDeclaration) { bInDeclaration = TRUE; } else if(*close == ']' && bInDeclaration) { bInDeclaration = FALSE; } // Increase the depth if we find a comment within a comment else if(*(close) == '<' && bInComment) { if(*(close+1) && *(close+1) == '!' && *(close+2) && *(close+2) == '-' && *(close+3) && *(close+3) == '-') { // '<!--' starts a comment nCommentDepth++; } } else if(*close == '>') { // If we are in a comment, we should only stop at a comment end // (Comments must end with "-->") if (bInComment) { if ((!m_bAllowNonXMLComments && (close - open) > 5 && *(close-1) == '-' && *(close-2) == '-') || (m_bAllowNonXMLComments && (close - open) > 3)) { nCommentDepth--; if (!nCommentDepth) { break; } } } else { if (!bInDoubleQuote && !bInSingleQuote && !bInDeclaration) { break; } } } if (m_bStoreErrors && bInComment && !errPos) { if (*close == '-' && *(close+1) == '-' && *(close + 2) != '>' && (close - open > 4)) { ulLine = m_ulCurrentLine; ulCol = m_ulCurrentCol; errPos = buf; errLen = len; } } } if( (close<=buf+len) && *close != '>') { if(!bIsFinal) { return XMLPNotDone; } SetError(m_pLastError, XMLErrorNoClose, 0, 0, buf, len, 0); buf = open; return XMLPNoClose; } afterclose = close+1; if(*(open+1) == '!') { if(*(open+2) == '-' && *(open+3) == '-') { // '<!--' starts a comment m_comment_state = 1; m_comment_start = TRUE; m_bCommentWasFound = TRUE; FindCommentClose(buf, open+4, buf + len); if(m_comment_state != 0) { SetError(m_pLastError, XMLErrorNoClose, 0, 0, buf, len, 0); return XMLPNoClose; } else if(m_comment_get_arg != 3) { tag = new XMLTag(m_bStrictCompliance, m_bStoreErrors); const char* pBeginComment = open + 4; int commentLen = buf - pBeginComment - 3; tag->new_attribute()->value = new_string(pBeginComment, commentLen); if (errPos) { XMLError* err = NULL; SetError(err, XMLErrorTwoDashNotAllowed, ulLine, ulCol, errPos, errLen, 0); tag->m_errs->Add(err); } return XMLPComment; } // Got a comment command tag = new XMLTag(m_bStrictCompliance, m_bStoreErrors); tag->new_attribute()->value = new_string(m_comment_arg); tag->m_cur_attribute->name = new_string(m_comment_command); return XMLPComment; } XMLParseResult rc = ParseTag(open+1, close, XMLDirectiveTag, tag); if(XMLPTag == rc) { // TODO - Scan Directive buf = afterclose; return XMLPDirective; } else if(XMLPAttributeValueNotQuoted == rc) { SetError(m_pLastError, XMLErrorMissingQuote, 0, 0, buf, len, 0); } else { SetError(m_pLastError, XMLErrorNoClose, 0, 0, buf, len, 0); } buf = afterclose; return XMLPBadDirective; } if(*(open + 1) == '?') { // A Processing Instruction XMLParseResult rc = ParseTag(open+1, close, XMLProcInstTag, tag); if(XMLPTag == rc) { buf = afterclose; if (m_bStrictCompliance) { //[SMIL 1.0 Compliance] Fixes PR 9862. No comment can // precede a processor instruction (xml prolog): if (m_bCommentWasFound && m_bXMLandSMIL10FullCompliance) { SetError(m_pLastError, XMLErrorCommentBeforeProcInst, 0, 0, buf, len, 0); return XMLPCommentBeforeProcInst; } if (m_bStoreErrors) { XMLError* err = NULL; SetError(err, XMLErrorCommentBeforeProcInst, 0, 0, buf, len, 0); tag->m_errs->Add(err); } } if (m_bStoreErrors) { ScanTag(open+1, close, tag); } return XMLPProcInst; } SetError(m_pLastError, XMLErrorNoClose, 0, 0, buf, len, 0); return XMLPBadProcInst; } // just a plain old tag XMLParseResult rc = ParseTag(open, close, XMLPlainTag, tag); if(XMLPTag == rc) { buf = afterclose; if (m_bStoreErrors) { ScanTag(open, close, tag); } return XMLPTag; } else if(XMLPBadEndTag == rc) { if(m_pCurrentFrame && m_pCurrentFrame->name) { SetError(m_pLastError, XMLErrorBadEndTag, 0, 0, buf, len, m_pCurrentFrame->name); } else { SetError(m_pLastError, XMLErrorBadEndTag, 0, 0, buf, len, 0); } } else if(XMLPBadAttribute == rc) { SetError(m_pLastError, XMLErrorBadAttribute, 0, 0, buf, len, 0); } else if(XMLPAttributeValueNotQuoted == rc) { SetError(m_pLastError, XMLErrorMissingQuote, 0, 0, buf, len, 0); } else if(XMLPDupAttribute == rc) { SetError(m_pLastError, XMLErrorDupAttribute, 0, 0, buf, len, 0); } else { SetError(m_pLastError, XMLUnknownError, 0, 0, buf, len, 0); } return rc;}XMLParseResultXMLParser::ParseTag(const char* open, const char* close, XMLTagType tType, XMLTag*& tag){ const char* cur = open+1; const char* afterclose = close+1; BOOL bHasAttributeNames = TRUE; BOOL bUseNonQuotedValues = FALSE; BOOL bHasDirectives = FALSE; tag = new XMLTag(m_bStrictCompliance, m_bStoreErrors); switch(tType) { case XMLPlainTag: { if(*(close - 1) == '/') { tag->m_need_close = FALSE; close--; } } break; case XMLProcInstTag: { tag->m_need_close = FALSE; if(*(close - 1) == '?') { close--; } } break; case XMLDirectiveTag: { bHasAttributeNames = FALSE; bUseNonQuotedValues = TRUE; bHasDirectives = TRUE; tag->m_need_close = FALSE; } break; default: { tag->m_need_close = FALSE; } break; } tag->m_type = tType; GetStringResult res = GetString(cur, close, tag->m_name, TagType); if(res == GSEndTag) { tag->m_type = XMLEndTag; tag->m_need_close = FALSE; if(!m_pCurrentFrame || !m_pCurrentFrame->name) { return XMLPBadEndTag; } if(m_bStrictCompliance) { if(strcmp(tag->m_name, m_pCurrentFrame->name) != 0) { return XMLPBadEndTag; } } else { if(strcasecmp(tag->m_name, m_pCurrentFrame->name) != 0) { return XMLPBadEndTag; } } tag->elem = m_pCurrentFrame->elemcount; if(m_pCurrentFrame) delete m_pCurrentFrame; m_pCurrentFrame = (XMLFrame*)m_pStack.Pop(); return XMLPTag; } else if(res == GSMissingQuote) { delete tag; tag = NULL; return XMLPAttributeValueNotQuoted; } else if(tag->m_name && tag->m_need_close) { tag->elem = m_pCurrentFrame->elemcount++; XMLFrame* frame = new XMLFrame; frame->elemcount = 0; frame->name = new_string(tag->m_name); m_pStack.Push(m_pCurrentFrame); m_pCurrentFrame = frame; } else { tag->elem = m_pCurrentFrame->elemcount++; } if(GSFoundExpected != res) { delete tag; tag = NULL; return XMLPNoTagType; } else { while(cur < close) { if(bHasAttributeNames) { GetStringResult res = GetString(cur, close, tag->new_attribute()->name, AttributeName); if(res == GSNoValue) { delete tag->m_cur_attribute; tag->m_numAttributes--; break; } switch(res) { case GSValueOnly: // The user of this parser will fill in the name of this // attribute tag->m_cur_attribute->value = tag->m_cur_attribute->name; tag->m_cur_attribute->name = NULL; continue; case GSFoundExpected: break; default: delete tag;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -