📄 html.c
字号:
/*//// html.c//// HTML markup tag functions//// Copyright (c) 1995-96 Jim Nelson. Permission to distribute// granted by the author. No warranties are made on the fitness of this// source code.//*/#include "htp.h"char *FindWhitespace(char *str){ assert(str != NULL); while(*str != NUL) { if(isspace(*str)) { break; } str++; } return str;}char *FindNonWhitespace(char *str){ assert(str != NULL); while(*str != NUL) { if(!isspace(*str)) { break; } str++; } return str;} /*// HTML_ATTRIBUTE functions*/BOOL MakeAttribute(HTML_ATTRIBUTE *htmlAttribute, const char *name, const char *value, BOOL quoted){ assert(htmlAttribute != NULL); assert(name != NULL); /* create a duplicate of the string for the attribute structure */ if((htmlAttribute->name = DuplicateString(name)) == NULL) { return FALSE; } /* do the same for the value, if it exists */ if(value != NULL) { if((htmlAttribute->value = DuplicateString(value)) == NULL) { FreeMemory(htmlAttribute->name); htmlAttribute->name = NULL; return FALSE; } } else { htmlAttribute->value = NULL; } /* keep track if this was a quoted value */ htmlAttribute->quoted = quoted; return TRUE;} BOOL ChangeAttributeName(HTML_ATTRIBUTE *htmlAttribute, const char *name){ assert(htmlAttribute != NULL); /* although name should never be null, let it slide */ if(htmlAttribute->name != NULL) { FreeMemory(htmlAttribute->name); htmlAttribute->name = NULL; } if((htmlAttribute->name = DuplicateString(name)) == NULL) { return FALSE; } return TRUE;}BOOL ChangeAttributeValue(HTML_ATTRIBUTE *htmlAttribute, const char *value, BOOL quoted){ assert(htmlAttribute != NULL); /* free the value's memory, if previously defined */ if(htmlAttribute->value != NULL) { FreeMemory(htmlAttribute->value); htmlAttribute->value = NULL; } /* if the new value is defined, allocate room and copy it in */ if(value != NULL) { if((htmlAttribute->value = DuplicateString(value)) == NULL) { return FALSE; } } htmlAttribute->quoted = quoted; return TRUE;} void DestroyAttribute(HTML_ATTRIBUTE *htmlAttribute){ assert(htmlAttribute != NULL); /* the attribute should always have a name */ assert(htmlAttribute->name != NULL); FreeMemory(htmlAttribute->name); htmlAttribute->name = NULL; if(htmlAttribute->value != NULL) { FreeMemory(htmlAttribute->value); htmlAttribute->value = NULL; }} /*// HTML_MARKUP functions*//* this is perhaps the ugliest piece of code in the entire program ... it is also one of the most critical. (Surprise.) Allan Todd provided a very important fix: htp 1.0 and before would crash and burn if it hit a large comment. This was problematic for JavaScript and VBScript, which embed the interpreted code in comments to prevent other browsers from gagging. Allans solution is simply to pack the entire comment into one attribute.*/BOOL PlaintextToMarkup(const char *plaintext, HTML_MARKUP *htmlMarkup){ char *plainBuffer; char *plainPtr; BOOL valueFound; BOOL quotedValue; BOOL endOfMarkup; char *token; char *value; BOOL isComment; assert(plaintext != NULL); assert(htmlMarkup != NULL); /* although this isnt a good thing, its not something to halt execution over */ if(plaintext[0] == NUL) { DEBUG_PRINT(("no plaintext to convert")); return FALSE; } /* allocate and copy the markup field into the structure */ if((plainBuffer = DuplicateString(plaintext)) == NULL) { DEBUG_PRINT(("unable to duplicate plaintext string")); return FALSE; } plainPtr = plainBuffer; /* initialize the markup structure */ htmlMarkup->tag = NULL; htmlMarkup->attribCount = 0; /* walk the markup and build tag and attribute list (re-using the markup */ /* argument to walk the copied string) */ /* walk past any initial whitespace */ plainPtr = FindNonWhitespace(plainPtr); if(*plainPtr == NUL) { FreeMemory(plainBuffer); return TRUE; } /* mark first token as the tag */ token = plainPtr; /* check if this is a comment */ isComment = (strncmp(token, "!--", 3) != 0) ? FALSE : TRUE; endOfMarkup = FALSE; /* walk to the first whitespace, mark it as NUL, and this is the tag */ plainPtr = FindWhitespace(plainPtr); if(*plainPtr == NUL) { endOfMarkup = TRUE; } /* copy the markup tag into the structure */ *plainPtr = NUL; if((htmlMarkup->tag = DuplicateString(token)) == NULL) { DEBUG_PRINT(("unable to duplicate markup token")); FreeMemory(plainBuffer); return FALSE; } if(endOfMarkup) { FreeMemory(plainBuffer); return TRUE; } /* advance past NUL */ plainPtr++; /* start walking the rest of markup, looking for attributes and their */ /* values */ while(*plainPtr != NUL) { /* walk past whitespace */ plainPtr = FindNonWhitespace(plainPtr); /* if a comment, put the whole she-bang into a single attribute */ /* and skeedaddle */ if(isComment == TRUE) { if(AddAttributeToMarkup(htmlMarkup, plainPtr, FALSE, FALSE) == TRUE) { FreeMemory(plainBuffer); return TRUE; } /* couldnt add it for some reason */ DEBUG_PRINT(("unable to add comment to markup")); DestroyMarkupStruct(htmlMarkup); FreeMemory(plainBuffer); return FALSE; } /* if not NUL, then hit an attribute */ if(*plainPtr != NUL) { /* mark the beginning of the attribute */ token = plainPtr; value = NULL; quotedValue = FALSE; /* walk through the attribute, looking for whitespace or an */ /* equal sign */ valueFound = FALSE; while(*plainPtr != NUL) { if(*plainPtr == '=') { valueFound = TRUE; break; } else if(isspace(*plainPtr)) { /* end of attribute */ break; } plainPtr++; } if(*plainPtr != NUL) { /* mark as NUL to delimit the attribute name */ *plainPtr = NUL; plainPtr++; /* skip past whitespace (looking for a value) */ plainPtr = FindNonWhitespace(plainPtr); /* is this an attribute or value for previous attribute? */ if(*plainPtr != NUL) { if(valueFound) { if(*plainPtr == '\"') { /* quoted value, search for end quote */ quotedValue = TRUE; plainPtr++; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -