📄 ospxmlutil.c
字号:
} } } else { /* not a character reference - check for predefined name */ for (cnt=0; cnt<OSPVXMLDocEntitiesSize; cnt++) { if (OSPM_MEMCMP((const char *)ospvName, (const char *)OSPVXMLDocEntities[cnt].ospmEntName, OSPC_XMLDOC_ENTITYSIZE) == 0) { break; } } if (cnt < OSPVXMLDocEntitiesSize) { *ospvChar = OSPVXMLDocEntities[cnt].ospmEntValue; } else { ospvErrCode = OSPC_ERR_XML_BAD_ENTITY; } } } return(ospvErrCode);}/**//*-----------------------------------------------------------------------* * OSPPXMLDocReadChar() - read a single character from an XML document *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDocReadChar( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding, /* character encoding for document */ unsigned char *ospvChar /* place to store read character */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; int readChar = '\0'; if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvChar == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { /* read the first byte */ readChar = OSPPBfrReadByte(*ospvBfrAddr); if (readChar == -1) { ospvErrCode = OSPC_ERR_BUF_INCOMPLETE; } } /* what we do next depends on the encoding */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { /* UTF-8 is pretty easy -- we're done */ if (ospvEncoding == ospeXMLEncUTF8) { *ospvChar = (unsigned char)readChar; } else { if (ospvEncoding == ospeXMLEncUTF16l) { *ospvChar = (unsigned char)readChar; readChar = OSPPBfrReadByte(*ospvBfrAddr); if (readChar == -1) { ospvErrCode = OSPC_ERR_BUF_INCOMPLETE; } else if (readChar != OSPC_XMLDOC_UTF16NULL) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } } else { if (ospvEncoding == ospeXMLEncUTF16b) { readChar = OSPPBfrReadByte(*ospvBfrAddr); if (readChar == -1) { ospvErrCode = OSPC_ERR_BUF_INCOMPLETE; } else if (readChar != OSPC_XMLDOC_UTF16NULL) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { *ospvChar = (unsigned char)readChar; } } } } } return(ospvErrCode);}#ifdef OSPC_DEBUG/**//*-----------------------------------------------------------------------* * OSPPXMLDocPeekCharN() - look at the Nth character in an XML document *-----------------------------------------------------------------------*/voidOSPPXMLDocPeekCharN( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding, /* character encoding for the document */ unsigned ospvCnt, /* character to look at */ unsigned char *ospvChar, /* place to store character */ int *ospvErrCode /* error code */){ int readChar = '\0'; if (ospvBfrAddr == OSPC_OSNULL) { *ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { *ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { *ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvChar == OSPC_OSNULL) { *ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (*ospvErrCode == OSPC_ERR_NO_ERROR) { /* handle each encoding separately */ switch (ospvEncoding) { case ospeXMLEncUTF8: readChar = OSPPBfrPeekByteN(*ospvBfrAddr, ospvCnt); break; case ospeXMLEncUTF16l: readChar = OSPPBfrPeekByteN(*ospvBfrAddr, 2*ospvCnt); break; case ospeXMLEncUTF16b: readChar = OSPPBfrPeekByteN(*ospvBfrAddr, (2*ospvCnt+1)); break; default: *ospvErrCode = (unsigned )OSPC_ERR_OS_FAILURE; break; } } if (*ospvErrCode == OSPC_ERR_NO_ERROR) { if(readChar == -1) { *ospvErrCode = OSPC_ERR_BUF_INCOMPLETE; } else { *ospvChar = (unsigned char)readChar; } } return;}#endif /* OSPC_DEBUG *//**//*-----------------------------------------------------------------------* * OSPPXMLDocSkipWhite() - skip whitespace in an XML document *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDocSkipWhite( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding /* character encoding for the document */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned char readChar = 0; if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } while (ospvErrCode == OSPC_ERR_NO_ERROR) { OSPPXMLDocPeekCharN(ospvBfrAddr, ospvEncoding, 0, &readChar, (int *)&ospvErrCode); if (ospvErrCode == OSPC_ERR_NO_ERROR) { if (OSPPXMLIsSpace(readChar)) { ospvErrCode = OSPPXMLDocReadChar(ospvBfrAddr, ospvEncoding, &readChar); } else { break; } } } return(ospvErrCode);}/**//*-----------------------------------------------------------------------* * OSPPXMLAddReference() - add entity references to XML character data *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLAddReference( const unsigned char *ospvRawData, /* data to reference */ unsigned ospvDataSize, /* number of bytes of data */ OSPTBFR **ospvBfrAddr /* buffer to stuff output */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned dataCnt; unsigned char ch; if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvRawData == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { for ( dataCnt=0; (dataCnt<ospvDataSize) && (ospvErrCode == OSPC_ERR_NO_ERROR); dataCnt++, ospvRawData++) { /* * There are two things that will cause us to replace a raw * data byte with an entity reference: (1) a value that * XML does not consider a valid character (e.g. 0); and * (2) the "greater than" character (>) that marks the * end of element tags and CDATA sections. (Other sections * aren't relevant because they won't be confused with * character data anyway.) */ if (OSPPXMLIsChar(*ospvRawData) && (*ospvRawData != OSPC_XMLDOC_CLOSE)) { /* this character's okay as is */ if (OSPPBfrWriteByte(ospvBfrAddr, *ospvRawData) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } else { /* this one needs to be referenced */ if (OSPPBfrWriteByte(ospvBfrAddr, OSPC_XMLDOC_REF) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { if (OSPPBfrWriteByte(ospvBfrAddr, OSPC_XMLDOC_CHARREF) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } if (ospvErrCode == OSPC_ERR_NO_ERROR) { if (OSPPBfrWriteByte(ospvBfrAddr, OSPC_XMLDOC_HEXREF) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } if (ospvErrCode == OSPC_ERR_NO_ERROR) { ch = (char)('0' + (*ospvRawData)/16); if (ch > '9') { ch += 'a' - '0' - 10; } if (OSPPBfrWriteByte(ospvBfrAddr, ch) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } if (ospvErrCode == OSPC_ERR_NO_ERROR) { ch = (char)('0' + ((*ospvRawData)%16)); if (ch > '9') { ch += 'a' - '0' - 10; } if (OSPPBfrWriteByte(ospvBfrAddr, ch) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } if (ospvErrCode == OSPC_ERR_NO_ERROR) { if (OSPPBfrWriteByte(ospvBfrAddr, OSPC_XMLDOC_REFEND) != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } } } } return(ospvErrCode);}/**//*-----------------------------------------------------------------------* * OSPPXMLDereference() - resolve entity references in XML character data *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDereference( const unsigned char *ospvCharData, /* character data to deference */ unsigned *ospvDataSize, /* input: max, output: actual */ unsigned char *ospvRawData /* place to put output */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned dataCnt; unsigned charCnt; unsigned char entity[OSPC_XMLDOC_ENTITYSIZE]; unsigned char *start = ospvRawData; if (ospvCharData == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (ospvDataSize == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (ospvRawData == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } /* * As we start the loop through the characters, we can end * the loop one of two ways: (1) we get to the end of the * string, which is indicated by a NULL byte; or (2) something * bad happens. Bad things will be flagged by ospvErrCode * so the loop can watch that. */ /* keep track of how much data we've written */ dataCnt = 0; /* and go into our loop */ while ((ospvErrCode == OSPC_ERR_NO_ERROR) && (*ospvCharData != '\0')) { /* is the byte a reference instead of a raw character? */ if (*ospvCharData == OSPC_XMLDOC_REF) { /* this is an entity reference, get the whole entity name */ /* first skipping past the reference character */ ospvCharData++; for (charCnt=0; charCnt<OSPC_XMLDOC_ENTITYSIZE; charCnt++) { /* are we at the end of the reference? */ if (*ospvCharData == '\0') { /* yes, but we didn't finish! */ ospvErrCode = OSPC_ERR_XML_BAD_ENTITY; break; } else if (*ospvCharData == OSPC_XMLDOC_REFEND) { /* * We've found the end of the entity reference; * skip past it and break. */ ospvCharData++; break; } else { /* just another part of the reference */ entity[charCnt] = *ospvCharData++; } } /* we got to the end of the reference, make sure no errors */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { entity[charCnt] = '\0'; ospvErrCode = OSPPXMLDocTranslateEntity(entity, ospvRawData); ospvRawData++; dataCnt++; } } else { /* just a plain old character */ *ospvRawData++ = *ospvCharData++; dataCnt++; } /* make sure there's still room in the data area */ /* if (dataCnt >= *ospvDataSize) */ if (dataCnt > *ospvDataSize) { ospvErrCode = (unsigned )OSPC_ERR_OS_FAILURE; break; } } /* update the size of the data */ *ospvDataSize = dataCnt; ospvRawData = start; return(ospvErrCode);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -