javascript.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,788 行 · 第 1/3 页
C
1,788 行
DestroyTagItem(TagItem* ti){ AVPair *temp; if(ti->text) { PR_Free(ti->text); ti->text = NULL; } while(ti->attList) { temp = ti->attList; ti->attList = ti->attList->next; if(temp->attribute) { PR_Free(temp->attribute); temp->attribute = NULL; } if(temp->value) { PR_Free(temp->value); temp->value = NULL; } PR_Free(temp); } PR_Free(ti);}/************************************************************************ * * G e t T a g T y p e */static TAG_TYPEGetTagType(char *att){ if(!PORT_Strcasecmp(att, "APPLET")) { return APPLET_TAG; } if(!PORT_Strcasecmp(att, "SCRIPT")) { return SCRIPT_TAG; } if(!PORT_Strcasecmp(att, "LINK")) { return LINK_TAG; } if(!PORT_Strcasecmp(att, "STYLE")) { return STYLE_TAG; } return OTHER_TAG;}/************************************************************************ * * F B _ C r e a t e */static FileBuffer*FB_Create(PRFileDesc* fd){ FileBuffer *fb; PRInt32 amountRead; PRInt32 storedOffset; fb = (FileBuffer*) PR_Malloc(sizeof(FileBuffer)); fb->fd = fd; storedOffset = PR_Seek(fd, 0, PR_SEEK_CUR); PR_Seek(fd, 0, PR_SEEK_SET); fb->startOffset = 0; amountRead = PR_Read(fd, fb->buf, FILE_BUFFER_BUFSIZE); if(amountRead == -1) goto loser; fb->maxIndex = amountRead-1; fb->curIndex = 0; fb->IsEOF = (fb->curIndex>fb->maxIndex) ? PR_TRUE : PR_FALSE; fb->lineNum = 1; PR_Seek(fd, storedOffset, PR_SEEK_SET); return fb;loser: PR_Seek(fd, storedOffset, PR_SEEK_SET); PR_Free(fb); return NULL;}/************************************************************************ * * F B _ G e t C h a r */static intFB_GetChar(FileBuffer *fb){ PRInt32 storedOffset; PRInt32 amountRead; int retval=-1; if(fb->IsEOF) { return EOF; } storedOffset = PR_Seek(fb->fd, 0, PR_SEEK_CUR); retval = fb->buf[fb->curIndex++]; if(retval=='\n') fb->lineNum++; if(fb->curIndex > fb->maxIndex) { /* We're at the end of the buffer. Try to get some new data from the * file */ fb->startOffset += fb->maxIndex+1; PR_Seek(fb->fd, fb->startOffset, PR_SEEK_SET); amountRead = PR_Read(fb->fd, fb->buf, FILE_BUFFER_BUFSIZE); if(amountRead==-1) goto loser; fb->maxIndex = amountRead-1; fb->curIndex = 0; } fb->IsEOF = (fb->curIndex > fb->maxIndex) ? PR_TRUE : PR_FALSE;loser: PR_Seek(fb->fd, storedOffset, PR_SEEK_SET); return retval;}/************************************************************************ * * F B _ G e t L i n e N u m * */static unsigned intFB_GetLineNum(FileBuffer *fb){ return fb->lineNum;}/************************************************************************ * * F B _ G e t P o i n t e r * */static PRInt32FB_GetPointer(FileBuffer *fb){ return fb->startOffset + fb->curIndex;}/************************************************************************ * * F B _ G e t R a n g e * */static PRInt32FB_GetRange(FileBuffer *fb, PRInt32 start, PRInt32 end, char **buf){ PRInt32 amountRead; PRInt32 storedOffset; *buf = PR_Malloc(end-start+2); if(*buf == NULL) { return 0; } storedOffset = PR_Seek(fb->fd, 0, PR_SEEK_CUR); PR_Seek(fb->fd, start, PR_SEEK_SET); amountRead = PR_Read(fb->fd, *buf, end-start+1); PR_Seek(fb->fd, storedOffset, PR_SEEK_SET); if(amountRead == -1) { PR_Free(*buf); *buf = NULL; return 0; } (*buf)[end-start+1] = '\0'; return amountRead;}/************************************************************************ * * F B _ D e s t r o y * */static voidFB_Destroy(FileBuffer *fb){ if(fb) { PR_Free(fb); }}/************************************************************************ * * P r i n t T a g I t e m * */static voidPrintTagItem(PRFileDesc *fd, TagItem *ti){ AVPair *pair; PR_fprintf(fd, "TAG:\n----\nType: "); switch(ti->type) { case APPLET_TAG: PR_fprintf(fd, "applet\n"); break; case SCRIPT_TAG: PR_fprintf(fd, "script\n"); break; case LINK_TAG: PR_fprintf(fd, "link\n"); break; case STYLE_TAG: PR_fprintf(fd, "style\n"); break; case COMMENT_TAG: PR_fprintf(fd, "comment\n"); break; case OTHER_TAG: default: PR_fprintf(fd, "other\n"); break; } PR_fprintf(fd, "Attributes:\n"); for(pair = ti->attList; pair; pair=pair->next) { PR_fprintf(fd, "\t%s=%s\n", pair->attribute, pair->value ? pair->value : ""); } PR_fprintf(fd, "Text:%s\n", ti->text ? ti->text : ""); PR_fprintf(fd, "---End of tag---\n");}/************************************************************************ * * P r i n t H T M L S t r e a m * */static voidPrintHTMLStream(PRFileDesc *fd, HTMLItem *head){ while(head) { if(head->type==TAG_ITEM) { PrintTagItem(fd, head->item.tag); } else { PR_fprintf(fd, "\nTEXT:\n-----\n%s\n-----\n\n", head->item.text); } head = head->next; }}/************************************************************************ * * S a v e I n l i n e S c r i p t * */static intSaveInlineScript(char *text, char *id, char *basedir, char *archiveDir){ char *filename=NULL; PRFileDesc *fd=NULL; int retval = -1; PRInt32 writeLen; char *ilDir=NULL; if(!text || !id || !archiveDir) { return -1; } if(dumpParse) { PR_fprintf(outputFD, "SaveInlineScript: text=%s, id=%s, \n" "basedir=%s, archiveDir=%s\n", text, id, basedir, archiveDir); } /* Make sure the archive directory is around */ if(ensureExists(basedir, archiveDir) != PR_SUCCESS) { PR_fprintf(errorFD, "ERROR: Unable to create archive directory %s.\n", archiveDir); errorCount++; return -1; } /* Make sure the inline script directory is around */ ilDir = PR_smprintf("%s/inlineScripts", archiveDir); scriptdir = "inlineScripts"; if(ensureExists(basedir, ilDir) != PR_SUCCESS) { PR_fprintf(errorFD, "ERROR: Unable to create directory %s.\n", ilDir); errorCount++; return -1; } filename = PR_smprintf("%s/%s/%s", basedir, ilDir, id); /* If the file already exists, give a warning, then blow it away */ if(PR_Access(filename, PR_ACCESS_EXISTS) == PR_SUCCESS) { PR_fprintf(errorFD, "warning: file \"%s\" already exists--will overwrite.\n", filename); warningCount++; if(rm_dash_r(filename)) { PR_fprintf(errorFD, "ERROR: Unable to delete %s.\n", filename); errorCount++; goto finish; } } /* Write text into file with name id */ fd = PR_Open(filename, PR_WRONLY|PR_CREATE_FILE|PR_TRUNCATE, 0777); if(!fd) { PR_fprintf(errorFD, "ERROR: Unable to create file \"%s\".\n", filename); errorCount++; goto finish; } writeLen = strlen(text); if( PR_Write(fd, text, writeLen) != writeLen) { PR_fprintf(errorFD, "ERROR: Unable to write to file \"%s\".\n", filename); errorCount++; goto finish; } retval = 0;finish: if(filename) { PR_smprintf_free(filename); } if(ilDir) { PR_smprintf_free(ilDir); } if(fd) { PR_Close(fd); } return retval;}/************************************************************************ * * S a v e U n n a m a b l e S c r i p t * */static intSaveUnnamableScript(char *text, char *basedir, char *archiveDir, char *HTMLfilename){ char *id=NULL; char *ext=NULL; char *start=NULL; int retval = -1; if(!text || !archiveDir || !HTMLfilename) { return -1; } if(dumpParse) { PR_fprintf(outputFD, "SaveUnnamableScript: text=%s, basedir=%s,\n" "archiveDir=%s, filename=%s\n", text, basedir, archiveDir, HTMLfilename); } /* Construct the filename */ ext = PL_strrchr(HTMLfilename, '.'); if(ext) { *ext = '\0'; } for(start=HTMLfilename; strpbrk(start, "/\\"); start=strpbrk(start, "/\\")+1); if(*start=='\0') start = HTMLfilename; id = PR_smprintf("_%s%d", start, idOrdinal++); if(ext) { *ext = '.'; } /* Now call SaveInlineScript to do the work */ retval = SaveInlineScript(text, id, basedir, archiveDir); PR_Free(id); return retval;}/************************************************************************ * * S a v e S o u r c e * */static intSaveSource(char *src, char *codebase, char *basedir, char *archiveDir){ char *from=NULL, *to=NULL; int retval = -1; char *arcDir=NULL; if(!src || !archiveDir) { return -1; } if(dumpParse) { PR_fprintf(outputFD, "SaveSource: src=%s, codebase=%s, basedir=%s,\n" "archiveDir=%s\n", src, codebase, basedir, archiveDir); } if(codebase) { arcDir = PR_smprintf("%s/%s/%s/", basedir, codebase, archiveDir); } else { arcDir = PR_smprintf("%s/%s/", basedir, archiveDir); } if(codebase) { from = PR_smprintf("%s/%s/%s", basedir, codebase, src); to = PR_smprintf("%s%s", arcDir, src); } else { from = PR_smprintf("%s/%s", basedir, src); to = PR_smprintf("%s%s", arcDir, src); } if(make_dirs(to, 0777)) { PR_fprintf(errorFD, "ERROR: Unable to create archive directory %s.\n", archiveDir); errorCount++; goto finish; } retval = copyinto(from, to);finish: if(from) PR_Free(from); if(to) PR_Free(to); if(arcDir) PR_Free(arcDir); return retval;}/************************************************************************ * * T a g T y p e T o S t r i n g * */char *TagTypeToString(TAG_TYPE type){ switch(type) { case APPLET_TAG: return "APPLET"; case SCRIPT_TAG: return "SCRIPT"; case LINK_TAG: return "LINK"; case STYLE_TAG: return "STYLE"; default: return "unknown"; } return "unknown";}/************************************************************************ * * e x t r a c t _ j s * */static intextract_js(char *filename){ PRFileDesc *fd=NULL; FileBuffer *fb=NULL; HTML_STATE state; int curchar; HTMLItem *head = NULL; HTMLItem *tail = NULL; PRInt32 textStart; PRInt32 curOffset; TagItem *tagp=NULL; char *text=NULL; HTMLItem *curitem=NULL; int retval = -1; char *tagerr=NULL; unsigned int linenum, startLine; char *archiveDir=NULL, *firstArchiveDir=NULL; HTMLItem *styleList, *styleListTail; HTMLItem *entityList, *entityListTail; char *basedir=NULL; styleList = entityList = styleListTail = entityListTail = NULL; /* Initialize the implicit ID counter for each file */ idOrdinal = 0; /* * First, parse the HTML into a stream of tags and text. */ fd = PR_Open(filename, PR_RDONLY, 0); if(!fd) { PR_fprintf(errorFD, "Unable to open %s for reading.\n", filename); errorCount++; return -1; } /* Construct base directory of filename. */ { char *cp; basedir = PL_strdup(filename); /* Remove trailing slashes */ while( (cp = PL_strprbrk(basedir, "/\\")) == (basedir + strlen(basedir) - 1)) { *cp = '\0'; } /* Now remove everything from the last slash (which will be followed * by a filename) to the end */ cp = PL_strprbrk(basedir, "/\\"); if(cp) { *cp = '\0'; } } state = TEXT_HTML_STATE; fb = FB_Create(fd); textStart=0; startLine = 0; while(linenum=FB_GetLineNum(fb), (curchar = FB_GetChar(fb)) != EOF) { switch(state) { case TEXT_HTML_STATE: if(curchar == '<') { /* * Found a tag */ /* Save the text so far to a new text item */ curOffset = FB_GetPointer(fb)-2; if(curOffset >= textStart) { if(FB_GetRange(fb, textStart, curOffset, &text) != curOffset-textStart+1) { PR_fprintf(errorFD, "Unable to read from %s.\n", filename); errorCount++; goto loser; } /* little fudge here. If the first character on a line * is '<', meaning a new tag, the preceding text item * actually ends on the previous line. In this case * we will be saying that the text segment ends on the * next line. I don't think this matters for text items. */ curitem = CreateTextItem(text, startLine, linenum); text = NULL; if(tail == NULL) { head = tail = curitem; } else { tail->next = curitem; tail = curitem; } } /* Process the tag */ tagp = ProcessTag(fb, &tagerr); if(!tagp) { if(tagerr) { PR_fprintf(errorFD, "Error in file %s: %s\n", filename, tagerr); errorCount++; } else { PR_fprintf(errorFD, "Error in file %s, in tag starting at line %d\n", filename, linenum); errorCount++; } goto loser; } /* Add the tag to the list */ curitem = CreateTagItem(tagp, linenum, FB_GetLineNum(fb)); if(tail == NULL) { head = tail = curitem; } else { tail->next = curitem; tail = curitem; } /* What's the next state */ if(tagp->type == SCRIPT_TAG) { state = SCRIPT_HTML_STATE; } /* Start recording text from the new offset */ textStart = FB_GetPointer(fb); startLine = FB_GetLineNum(fb); } else { /* regular character. Next! */ } break; case SCRIPT_HTML_STATE: if(curchar == '<') { char *cp; /* * If this is a </script> tag, then we're at the end of the * script. Otherwise, ignore */ curOffset = FB_GetPointer(fb)-1; cp = NULL; if(FB_GetRange(fb, curOffset, curOffset+8, &cp) != 9) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?