| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  |  *  Normalise HTML text. | 
					
						
							|  |  |  |  *  Decode MS Script Encoder protection.  | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-04-02 15:24:51 +00:00
										 |  |  |  *  Copyright (C) 2007-2008 Sourcefire, Inc. | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-04-02 15:24:51 +00:00
										 |  |  |  *  Authors: Trog | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  This program is free software; you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2008-04-02 15:24:51 +00:00
										 |  |  |  *  it under the terms of the GNU General Public License version 2 as | 
					
						
							|  |  |  |  *  published by the Free Software Foundation. | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  *  GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  *  along with this program; if not, write to the Free Software | 
					
						
							| 
									
										
										
										
											2006-04-09 19:59:28 +00:00
										 |  |  |  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | 
					
						
							|  |  |  |  *  MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-09 15:23:50 +00:00
										 |  |  | #if HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include "clamav-config.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2006-10-09 15:23:50 +00:00
										 |  |  | #ifdef	HAVE_UNISTD_H
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2006-10-09 15:23:50 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  | #include <ctype.h>
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if HAVE_MMAP
 | 
					
						
							|  |  |  | #if HAVE_SYS_MMAN_H
 | 
					
						
							|  |  |  | #include <sys/mman.h>
 | 
					
						
							|  |  |  | #else /* HAVE_SYS_MMAN_H */
 | 
					
						
							|  |  |  | #undef HAVE_MMAP
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "others.h"
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | #include "htmlnorm.h"
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | #include "entconv.h"
 | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | #include "jsparse/js-norm.h"
 | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | #define HTML_STR_LENGTH 1024
 | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | #define MAX_TAG_CONTENTS_LENGTH HTML_STR_LENGTH
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | typedef enum { | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  |     HTML_BAD_STATE, | 
					
						
							|  |  |  |     HTML_NORM, | 
					
						
							|  |  |  |     HTML_COMMENT, | 
					
						
							|  |  |  |     HTML_CHAR_REF, | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  |     HTML_ENTITY_REF_DECODE, | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  |     HTML_SKIP_WS, | 
					
						
							|  |  |  |     HTML_TRIM_WS, | 
					
						
							|  |  |  |     HTML_TAG, | 
					
						
							|  |  |  |     HTML_TAG_ARG, | 
					
						
							|  |  |  |     HTML_TAG_ARG_VAL, | 
					
						
							|  |  |  |     HTML_TAG_ARG_EQUAL, | 
					
						
							|  |  |  |     HTML_PROCESS_TAG, | 
					
						
							|  |  |  |     HTML_CHAR_REF_DECODE, | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  |     HTML_LOOKFOR_SCRENC, | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  |     HTML_JSDECODE, | 
					
						
							|  |  |  |     HTML_JSDECODE_LENGTH, | 
					
						
							|  |  |  |     HTML_JSDECODE_DECRYPT, | 
					
						
							|  |  |  |     HTML_SPECIAL_CHAR, | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  |     HTML_RFC2397_TYPE, | 
					
						
							|  |  |  |     HTML_RFC2397_INIT, | 
					
						
							|  |  |  |     HTML_RFC2397_DATA, | 
					
						
							|  |  |  |     HTML_RFC2397_FINISH, | 
					
						
							|  |  |  |     HTML_RFC2397_ESC, | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  |     HTML_ESCAPE_CHAR | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | } html_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef enum { | 
					
						
							|  |  |  |     SINGLE_QUOTED, | 
					
						
							|  |  |  |     DOUBLE_QUOTED, | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  |     NOT_QUOTED | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | } quoted_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define HTML_FILE_BUFF_LEN 8192
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct file_buff_tag { | 
					
						
							|  |  |  | 	int fd; | 
					
						
							|  |  |  | 	unsigned char buffer[HTML_FILE_BUFF_LEN]; | 
					
						
							|  |  |  | 	int length; | 
					
						
							|  |  |  | } file_buff_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | struct tag_contents { | 
					
						
							|  |  |  | 	unsigned char contents[MAX_TAG_CONTENTS_LENGTH + 1]; | 
					
						
							|  |  |  | 	size_t pos; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | static const int base64_chars[256] = { | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, | 
					
						
							|  |  |  |     52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14, | 
					
						
							|  |  |  |     15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, | 
					
						
							|  |  |  |     41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  |     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int table_order[] = { | 
					
						
							|  |  |  |        00, 02, 01, 00, 02, 01, 02, 01, 01, 02, 01, 02, 00, 01, 02, 01, | 
					
						
							|  |  |  |        00, 01, 02, 01, 00, 00, 02, 01, 01, 02, 00, 01, 02, 01, 01, 02, | 
					
						
							|  |  |  |        00, 00, 01, 02, 01, 02, 01, 00, 01, 00, 00, 02, 01, 00, 01, 02, | 
					
						
							|  |  |  |        00, 01, 02, 01, 00, 00, 02, 01, 01, 00, 00, 02, 01, 00, 01, 02 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | int decrypt_tables[3][128] = { | 
					
						
							|  |  |  |       {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x57, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | 
					
						
							|  |  |  |        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, | 
					
						
							|  |  |  |        0x2E, 0x47, 0x7A, 0x56, 0x42, 0x6A, 0x2F, 0x26, 0x49, 0x41, 0x34, 0x32, 0x5B, 0x76, 0x72, 0x43, | 
					
						
							|  |  |  |        0x38, 0x39, 0x70, 0x45, 0x68, 0x71, 0x4F, 0x09, 0x62, 0x44, 0x23, 0x75, 0x3C, 0x7E, 0x3E, 0x5E, | 
					
						
							|  |  |  |        0xFF, 0x77, 0x4A, 0x61, 0x5D, 0x22, 0x4B, 0x6F, 0x4E, 0x3B, 0x4C, 0x50, 0x67, 0x2A, 0x7D, 0x74, | 
					
						
							|  |  |  |        0x54, 0x2B, 0x2D, 0x2C, 0x30, 0x6E, 0x6B, 0x66, 0x35, 0x25, 0x21, 0x64, 0x4D, 0x52, 0x63, 0x3F, | 
					
						
							|  |  |  |        0x7B, 0x78, 0x29, 0x28, 0x73, 0x59, 0x33, 0x7F, 0x6D, 0x55, 0x53, 0x7C, 0x3A, 0x5F, 0x65, 0x46, | 
					
						
							|  |  |  |        0x58, 0x31, 0x69, 0x6C, 0x5A, 0x48, 0x27, 0x5C, 0x3D, 0x24, 0x79, 0x37, 0x60, 0x51, 0x20, 0x36}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x7B, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | 
					
						
							|  |  |  |        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, | 
					
						
							|  |  |  |        0x32, 0x30, 0x21, 0x29, 0x5B, 0x38, 0x33, 0x3D, 0x58, 0x3A, 0x35, 0x65, 0x39, 0x5C, 0x56, 0x73, | 
					
						
							|  |  |  |        0x66, 0x4E, 0x45, 0x6B, 0x62, 0x59, 0x78, 0x5E, 0x7D, 0x4A, 0x6D, 0x71, 0x3C, 0x60, 0x3E, 0x53, | 
					
						
							|  |  |  |        0xFF, 0x42, 0x27, 0x48, 0x72, 0x75, 0x31, 0x37, 0x4D, 0x52, 0x22, 0x54, 0x6A, 0x47, 0x64, 0x2D, | 
					
						
							|  |  |  |        0x20, 0x7F, 0x2E, 0x4C, 0x5D, 0x7E, 0x6C, 0x6F, 0x79, 0x74, 0x43, 0x26, 0x76, 0x25, 0x24, 0x2B, | 
					
						
							|  |  |  |        0x28, 0x23, 0x41, 0x34, 0x09, 0x2A, 0x44, 0x3F, 0x77, 0x3B, 0x55, 0x69, 0x61, 0x63, 0x50, 0x67, | 
					
						
							|  |  |  |        0x51, 0x49, 0x4F, 0x46, 0x68, 0x7C, 0x36, 0x70, 0x6E, 0x7A, 0x2F, 0x5F, 0x4B, 0x5A, 0x2C, 0x57}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x6E, 0x0A, 0x0B, 0x0C, 0x06, 0x0E, 0x0F, | 
					
						
							|  |  |  |        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, | 
					
						
							|  |  |  |        0x2D, 0x75, 0x52, 0x60, 0x71, 0x5E, 0x49, 0x5C, 0x62, 0x7D, 0x29, 0x36, 0x20, 0x7C, 0x7A, 0x7F, | 
					
						
							|  |  |  |        0x6B, 0x63, 0x33, 0x2B, 0x68, 0x51, 0x66, 0x76, 0x31, 0x64, 0x54, 0x43, 0x3C, 0x3A, 0x3E, 0x7E, | 
					
						
							|  |  |  |        0xFF, 0x45, 0x2C, 0x2A, 0x74, 0x27, 0x37, 0x44, 0x79, 0x59, 0x2F, 0x6F, 0x26, 0x72, 0x6A, 0x39, | 
					
						
							|  |  |  |        0x7B, 0x3F, 0x38, 0x77, 0x67, 0x53, 0x47, 0x34, 0x78, 0x5D, 0x30, 0x23, 0x5A, 0x5B, 0x6C, 0x48, | 
					
						
							|  |  |  |        0x55, 0x70, 0x69, 0x2E, 0x4C, 0x21, 0x24, 0x4E, 0x50, 0x09, 0x56, 0x73, 0x35, 0x61, 0x4B, 0x58, | 
					
						
							|  |  |  |        0x3B, 0x57, 0x22, 0x6D, 0x4D, 0x25, 0x28, 0x46, 0x4A, 0x32, 0x41, 0x3D, 0x5F, 0x4F, 0x42, 0x65} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | static inline unsigned int rewind_tospace(const unsigned char* chunk, unsigned int len) | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 	unsigned int count = len; | 
					
						
							|  |  |  | 	while (!isspace(chunk[len - 1]) && (len > 1)) { | 
					
						
							|  |  |  | 		len--; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (len == 1) { | 
					
						
							|  |  |  | 		return count; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return len; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | /* read at most @max_len of data from @m_area or @stream, skipping NULL chars.
 | 
					
						
							|  |  |  |  * This used to be called cli_readline, but we don't stop at end-of-line anymore */ | 
					
						
							|  |  |  | static unsigned char *cli_readchunk(FILE *stream, m_area_t *m_area, unsigned int max_len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned char *chunk, *start, *ptr, *end; | 
					
						
							|  |  |  | 	unsigned int chunk_len, count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	chunk = (unsigned char *) cli_malloc(max_len); | 
					
						
							|  |  |  | 	if (!chunk) { | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	/* Try and use the memory buffer first */ | 
					
						
							|  |  |  | 	if (m_area) { | 
					
						
							|  |  |  | 		start = ptr = m_area->buffer + m_area->offset; | 
					
						
							|  |  |  | 		end = m_area->buffer + m_area->length; | 
					
						
							|  |  |  | 		if (start >= end) { | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			free(chunk); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 		/* maximum we can copy into the buffer,
 | 
					
						
							|  |  |  | 		 * we could have less than max_len bytes available */ | 
					
						
							|  |  |  | 		chunk_len = MIN(end-start, max_len-1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* look for NULL chars */ | 
					
						
							|  |  |  | 		ptr = memchr(start, 0, chunk_len); | 
					
						
							|  |  |  | 	        if(!ptr) { | 
					
						
							|  |  |  | 			/* no NULL chars found, copy all */ | 
					
						
							|  |  |  | 			memcpy(chunk, start, chunk_len); | 
					
						
							|  |  |  | 			chunk[chunk_len] = '\0'; | 
					
						
							|  |  |  | 			m_area->offset += chunk_len; | 
					
						
							|  |  |  | 			/* point ptr to end of chunk,
 | 
					
						
							|  |  |  | 			 * so we can check and rewind to a space below */ | 
					
						
							|  |  |  | 			ptr = start + chunk_len; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			/* copy portion that doesn't contain NULL chars */ | 
					
						
							|  |  |  | 			chunk_len = ptr - start; | 
					
						
							|  |  |  | 			if(chunk_len < max_len) { | 
					
						
							|  |  |  | 				memcpy(chunk, start, chunk_len); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				chunk_len = 0; | 
					
						
							|  |  |  | 				ptr = start; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			/* we have unknown number of NULL chars,
 | 
					
						
							|  |  |  | 			 * copy char-by-char and skip them */ | 
					
						
							|  |  |  | 			while((ptr < end) && (chunk_len < max_len-1)) { | 
					
						
							|  |  |  | 				const unsigned char c = *ptr++; | 
					
						
							|  |  |  | 				if(c) { | 
					
						
							|  |  |  | 					chunk[chunk_len++] = c; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			chunk[chunk_len] = '\0'; | 
					
						
							|  |  |  | 			/* we can't use chunk_len to determine how many bytes we read, since
 | 
					
						
							|  |  |  | 			 * we skipped chars */ | 
					
						
							|  |  |  | 			m_area->offset = ptr - m_area->buffer; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(ptr && ptr < end && !isspace(*ptr)) { | 
					
						
							|  |  |  | 			/* we hit max_len, rewind to a space */ | 
					
						
							|  |  |  | 			count = rewind_tospace(chunk, chunk_len); | 
					
						
							|  |  |  | 			if(count < chunk_len) { | 
					
						
							|  |  |  | 				chunk[count] = '\0'; | 
					
						
							|  |  |  | 				m_area->offset -= chunk_len - count; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (!stream) { | 
					
						
							|  |  |  | 			cli_dbgmsg("No HTML stream\n"); | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			free(chunk); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 		chunk_len = fread(chunk, 1, max_len-1, stream); | 
					
						
							|  |  |  | 		if(!chunk_len || chunk_len > max_len-1) { | 
					
						
							|  |  |  | 			/* EOF, or prevent overflow */ | 
					
						
							|  |  |  | 			free(chunk); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 		/* Look for NULL chars */ | 
					
						
							|  |  |  | 		ptr = memchr(chunk, 0, chunk_len); | 
					
						
							|  |  |  | 		if(ptr) { | 
					
						
							|  |  |  | 			/* NULL char found */ | 
					
						
							|  |  |  | 			/* save buffer limits */ | 
					
						
							|  |  |  | 		        start = ptr; | 
					
						
							|  |  |  | 			end = chunk + chunk_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* start of NULL chars, we will copy non-NULL characters
 | 
					
						
							|  |  |  | 			 * to this position */ | 
					
						
							|  |  |  | 			chunk_len = ptr - chunk; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* find first non-NULL char */ | 
					
						
							|  |  |  | 			while((ptr < end) && !(*ptr)) { | 
					
						
							|  |  |  | 				ptr++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			/* skip over NULL chars, and move back the rest */ | 
					
						
							|  |  |  | 		        while((ptr < end) && (chunk_len < max_len-1)) { | 
					
						
							|  |  |  | 				const unsigned char c = *ptr++; | 
					
						
							|  |  |  | 				if(c) { | 
					
						
							|  |  |  | 					chunk[chunk_len++] = c; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			chunk[chunk_len] = '\0'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(chunk_len == max_len - 1) { | 
					
						
							|  |  |  | 			/* rewind to a space (which includes newline) */ | 
					
						
							|  |  |  | 			count = rewind_tospace(chunk, chunk_len); | 
					
						
							|  |  |  | 			if(count < chunk_len) { | 
					
						
							|  |  |  | 				chunk[count] = '\0'; | 
					
						
							|  |  |  | 				/* seek-back to space */ | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 				fseek(stream, -(long)(chunk_len - count), SEEK_CUR); | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return chunk; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | static void html_output_flush(file_buff_t *fbuff) | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	if (fbuff && (fbuff->length > 0)) { | 
					
						
							|  |  |  | 		cli_writen(fbuff->fd, fbuff->buffer, fbuff->length); | 
					
						
							|  |  |  | 		fbuff->length = 0; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | static inline void html_output_c(file_buff_t *fbuff1, unsigned char c) | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (fbuff1) { | 
					
						
							|  |  |  | 		if (fbuff1->length == HTML_FILE_BUFF_LEN) { | 
					
						
							|  |  |  | 			html_output_flush(fbuff1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fbuff1->buffer[fbuff1->length++] = c; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-21 20:21:43 +00:00
										 |  |  | static void html_output_str(file_buff_t *fbuff, const unsigned char *str, size_t len) | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	if (fbuff) { | 
					
						
							|  |  |  | 		if ((fbuff->length + len) >= HTML_FILE_BUFF_LEN) { | 
					
						
							|  |  |  | 			html_output_flush(fbuff); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-17 10:09:23 +00:00
										 |  |  | 		if (len >= HTML_FILE_BUFF_LEN) { | 
					
						
							|  |  |  | 			html_output_flush(fbuff); | 
					
						
							|  |  |  | 			cli_writen(fbuff->fd, str, len); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			memcpy(fbuff->buffer + fbuff->length, str, len); | 
					
						
							|  |  |  | 			fbuff->length += len; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  | static char *html_tag_arg_value(tag_arguments_t *tags, const char *tag) | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	for (i=0; i < tags->count; i++) { | 
					
						
							|  |  |  | 		if (strcmp(tags->tag[i], tag) == 0) { | 
					
						
							|  |  |  | 			return tags->value[i]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  | static void html_tag_arg_set(tag_arguments_t *tags, const char *tag, const char *value) | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	for (i=0; i < tags->count; i++) { | 
					
						
							|  |  |  | 		if (strcmp(tags->tag[i], tag) == 0) { | 
					
						
							|  |  |  | 			free(tags->value[i]); | 
					
						
							| 
									
										
										
										
											2007-01-31 18:13:17 +00:00
										 |  |  | 			tags->value[i] = cli_strdup(value); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | static void html_tag_arg_add(tag_arguments_t *tags, | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  | 		const unsigned char *tag, unsigned char *value) | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 	int len, i; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	tags->count++; | 
					
						
							| 
									
										
										
										
											2007-05-25 23:10:58 +00:00
										 |  |  | 	tags->tag = (unsigned char **) cli_realloc2(tags->tag, | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				tags->count * sizeof(char *)); | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 	if (!tags->tag) { | 
					
						
							|  |  |  | 		goto abort; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-05-25 23:10:58 +00:00
										 |  |  | 	tags->value = (unsigned char **) cli_realloc2(tags->value, | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				tags->count * sizeof(char *)); | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 	if (!tags->value) { | 
					
						
							|  |  |  | 		goto abort; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	if(tags->scanContents) { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 		tags->contents= (unsigned char **) cli_realloc2(tags->contents, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 				tags->count*sizeof(*tags->contents)); | 
					
						
							|  |  |  | 		if(!tags->contents) { | 
					
						
							|  |  |  | 			goto abort; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		tags->contents[tags->count-1]=NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-01-31 18:13:17 +00:00
										 |  |  | 	tags->tag[tags->count-1] = cli_strdup(tag); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	if (value) { | 
					
						
							|  |  |  | 		if (*value == '"') { | 
					
						
							| 
									
										
										
										
											2007-01-31 18:13:17 +00:00
										 |  |  | 			tags->value[tags->count-1] = cli_strdup(value+1); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			len = strlen(value+1); | 
					
						
							|  |  |  | 			if (len > 0) { | 
					
						
							|  |  |  | 				tags->value[tags->count-1][len-1] = '\0'; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-07-12 15:06:09 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-01-31 18:13:17 +00:00
										 |  |  | 			tags->value[tags->count-1] = cli_strdup(value); | 
					
						
							| 
									
										
										
										
											2004-07-12 15:06:09 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		tags->value[tags->count-1] = NULL; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 	return; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | abort: | 
					
						
							|  |  |  | 	/* Bad error - can't do 100% recovery */ | 
					
						
							|  |  |  | 	tags->count--; | 
					
						
							|  |  |  | 	for (i=0; i < tags->count; i++) { | 
					
						
							|  |  |  | 		if (tags->tag) { | 
					
						
							|  |  |  | 			free(tags->tag[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (tags->value) { | 
					
						
							|  |  |  | 			free(tags->value[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 		if(tags->contents) { | 
					
						
							|  |  |  | 			if(tags->contents[i]) | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 				free(tags->contents[i]); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (tags->tag) { | 
					
						
							|  |  |  | 		free(tags->tag); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (tags->value) { | 
					
						
							|  |  |  | 		free(tags->value); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	if (tags->contents) | 
					
						
							|  |  |  | 		free(tags->contents); | 
					
						
							|  |  |  | 	tags->contents=NULL; | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 	tags->tag = tags->value = NULL; | 
					
						
							|  |  |  | 	tags->count = 0;	 | 
					
						
							|  |  |  | 	return; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void html_output_tag(file_buff_t *fbuff, char *tag, tag_arguments_t *tags) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  | 	int i, j, len; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 	html_output_c(fbuff, '<'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	html_output_str(fbuff, tag, strlen(tag)); | 
					
						
							|  |  |  | 	for (i=0; i < tags->count; i++) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 		html_output_c(fbuff, ' '); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		html_output_str(fbuff, tags->tag[i], strlen(tags->tag[i])); | 
					
						
							|  |  |  | 		if (tags->value[i]) { | 
					
						
							|  |  |  | 			html_output_str(fbuff, "=\"", 2); | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  | 			len = strlen(tags->value[i]); | 
					
						
							|  |  |  | 			for (j=0 ; j<len ; j++) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 				html_output_c(fbuff, tolower(tags->value[i][j])); | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 			html_output_c(fbuff, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 	html_output_c(fbuff, '>'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void html_tag_arg_free(tag_arguments_t *tags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	for (i=0; i < tags->count; i++) { | 
					
						
							|  |  |  | 		free(tags->tag[i]); | 
					
						
							|  |  |  | 		if (tags->value[i]) { | 
					
						
							|  |  |  | 			free(tags->value[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 		if(tags->contents) | 
					
						
							|  |  |  | 			if (tags->contents[i]) | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 				free(tags->contents[i]); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (tags->tag) { | 
					
						
							|  |  |  | 		free(tags->tag); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (tags->value) { | 
					
						
							|  |  |  | 		free(tags->value); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	if(tags->contents) | 
					
						
							|  |  |  | 		free(tags->contents); | 
					
						
							|  |  |  | 	tags->contents = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	tags->tag = tags->value = NULL; | 
					
						
							|  |  |  | 	tags->count = 0; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * the displayed text for an <a href> tag | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | static inline void html_tag_contents_append(struct tag_contents *cont, const unsigned char* begin,const unsigned char *end) | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 	size_t i; | 
					
						
							|  |  |  | 	if(!begin || !end) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	for(i = cont->pos; i < MAX_TAG_CONTENTS_LENGTH && (begin < end);i++) { | 
					
						
							|  |  |  | 		cont->contents[i] = *begin++; | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 	cont->pos = i; | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | static inline void html_tag_contents_done(tag_arguments_t *tags,int idx, struct tag_contents *cont) | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 	unsigned char *p; | 
					
						
							|  |  |  | 	cont->contents[cont->pos++] = '\0'; | 
					
						
							|  |  |  | 	p = cli_malloc(cont->pos); | 
					
						
							|  |  |  | 	if(!p) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	memcpy(p, cont->contents, cont->pos); | 
					
						
							|  |  |  | 	tags->contents[idx-1] = p; | 
					
						
							|  |  |  | 	cont->pos = 0; | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | struct screnc_state { | 
					
						
							|  |  |  | 	uint32_t length; | 
					
						
							|  |  |  | 	uint32_t sum; | 
					
						
							|  |  |  | 	uint8_t  table_pos; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* inplace decoding, so that we can normalize it later */ | 
					
						
							| 
									
										
										
										
											2008-07-14 16:24:43 +00:00
										 |  |  | static void screnc_decode(unsigned char *ptr, struct screnc_state *s) | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	uint8_t  value; | 
					
						
							|  |  |  | 	unsigned char *dst = ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!ptr || !s) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	while(s->length > 0 && *ptr) { | 
					
						
							|  |  |  | 		if ((*ptr == '\n') || (*ptr == '\r')) { | 
					
						
							|  |  |  | 			ptr++; | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (*ptr < 0x80) { | 
					
						
							|  |  |  | 			value = decrypt_tables[table_order[s->table_pos]][*ptr]; | 
					
						
							|  |  |  | 			if (value == 0xFF) { /* special character */ | 
					
						
							|  |  |  | 				ptr++; | 
					
						
							|  |  |  | 				s->length--; | 
					
						
							|  |  |  | 				switch (*ptr) { | 
					
						
							|  |  |  | 					case '\0': | 
					
						
							|  |  |  | 						/* Fixup for end of line */ | 
					
						
							|  |  |  | 						ptr--; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					case 0x21: | 
					
						
							|  |  |  | 						value = 0x3c; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					case 0x23: | 
					
						
							|  |  |  | 						value = 0x0d; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					case 0x24: | 
					
						
							|  |  |  | 						value = 0x40; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					case 0x26: | 
					
						
							|  |  |  | 						value = 0x0a; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					case 0x2a: | 
					
						
							|  |  |  | 						value = 0x3e; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			s->sum += value; | 
					
						
							|  |  |  | 			*dst++ = value; | 
					
						
							|  |  |  | 			s->table_pos = (s->table_pos + 1) % 64; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			*dst++ = *ptr++; | 
					
						
							|  |  |  | 			*dst++ = *ptr; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ptr++; | 
					
						
							|  |  |  | 		s->length--; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(!s->length) { | 
					
						
							|  |  |  | 		size_t remaining; | 
					
						
							|  |  |  | 		if(strlen(ptr) >= 12) { | 
					
						
							|  |  |  | 			uint32_t expected; | 
					
						
							|  |  |  | 			expected = base64_chars[ptr[0]] << 2; | 
					
						
							|  |  |  | 			expected += base64_chars[ptr[1]] >> 4; | 
					
						
							|  |  |  | 			expected += (base64_chars[ptr[1]] & 0x0f) << 12; | 
					
						
							|  |  |  | 			expected += (base64_chars[ptr[2]] >> 2) << 8; | 
					
						
							|  |  |  | 			expected += (base64_chars[ptr[2]] & 0x03) << 22; | 
					
						
							|  |  |  | 			expected += base64_chars[ptr[3]] << 16; | 
					
						
							|  |  |  | 			expected += (base64_chars[ptr[4]] << 2) << 24; | 
					
						
							|  |  |  | 			expected += (base64_chars[ptr[5]] >> 4) << 24; | 
					
						
							|  |  |  | 			ptr += 8; | 
					
						
							|  |  |  | 			if(s->sum != expected) { | 
					
						
							| 
									
										
										
										
											2008-08-21 20:21:43 +00:00
										 |  |  | 				cli_dbgmsg("screnc_decode: checksum mismatch: %u != %u\n", s->sum, expected); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				if(strncmp(ptr, "^#~@", 4) != 0) { | 
					
						
							|  |  |  | 					cli_dbgmsg("screnc_decode: terminator not found\n"); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					cli_dbgmsg("screnc_decode: OK\n"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			ptr += 4; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* copy remaining */ | 
					
						
							|  |  |  | 		remaining = strlen(ptr) + 1; | 
					
						
							|  |  |  | 		memmove(dst, ptr, remaining); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		*dst = '\0'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | static void js_process(struct parser_state *js_state, const char *js_begin, const char *js_end, | 
					
						
							|  |  |  | 		const char *line, const char *ptr, int in_script, const char *dirname) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(!js_begin) | 
					
						
							|  |  |  | 		js_begin = line; | 
					
						
							|  |  |  | 	if(!js_end) | 
					
						
							|  |  |  | 		js_end = ptr; | 
					
						
							|  |  |  | 	if(js_end > js_begin && | 
					
						
							|  |  |  | 			CLI_ISCONTAINED(line, 8192, js_begin, 1) && | 
					
						
							|  |  |  | 			CLI_ISCONTAINED(line, 8192, js_end, 1)) { | 
					
						
							|  |  |  | 		cli_js_process_buffer(js_state, js_begin, js_end - js_begin); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(!in_script) { | 
					
						
							|  |  |  | 		/*  we found a /script, normalize script now */ | 
					
						
							|  |  |  | 		cli_js_parse_done(js_state); | 
					
						
							|  |  |  | 		cli_js_output(js_state, dirname); | 
					
						
							|  |  |  | 		cli_js_destroy(js_state); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag_arguments_t *hrefs,const struct cli_dconf* dconf) | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 	int fd_tmp, tag_length, tag_arg_length, binary; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	int retval=FALSE, escape, value = 0, hex, tag_val_length=0; | 
					
						
							| 
									
										
										
										
											2008-08-25 14:18:48 +00:00
										 |  |  | 	int look_for_screnc=FALSE, in_screnc=FALSE,in_script=FALSE, text_space_written=FALSE; | 
					
						
							| 
									
										
										
										
											2007-05-01 20:13:27 +00:00
										 |  |  | 	FILE *stream_in = NULL; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	html_state state=HTML_NORM, next_state=HTML_BAD_STATE, saved_next_state=HTML_BAD_STATE; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	char filename[1024], tag[HTML_STR_LENGTH+1], tag_arg[HTML_STR_LENGTH+1]; | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 	char tag_val[HTML_STR_LENGTH+1], *tmp_file; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	unsigned char *line, *ptr, *arg_value, *ptr_screnc; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	tag_arguments_t tag_args; | 
					
						
							|  |  |  | 	quoted_state quoted; | 
					
						
							|  |  |  | 	unsigned long length; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	struct screnc_state screnc_state; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 	file_buff_t *file_buff_o2, *file_buff_text; | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 	file_buff_t *file_tmp_o1; | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	int in_ahref=0;/* index of <a> tag, whose contents we are parsing. Indexing starts from 1, 0 means outside of <a>*/ | 
					
						
							|  |  |  | 	unsigned char* href_contents_begin=NULL;/*beginning of the next portion of <a> contents*/ | 
					
						
							|  |  |  | 	unsigned char* ptrend=NULL;/*end of <a> contents*/ | 
					
						
							|  |  |  | 	unsigned char* in_form_action = NULL;/* the action URL of the current <form> tag, if any*/ | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 	struct entity_conv conv; | 
					
						
							|  |  |  | 	unsigned char entity_val[HTML_STR_LENGTH+1]; | 
					
						
							|  |  |  | 	size_t entity_val_length = 0; | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 	const int dconf_entconv = dconf && dconf->phishing&PHISHING_CONF_ENTCONV; | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 	const int dconf_js = dirname && dconf && dconf->doc&DOC_CONF_JSNORM; /* TODO */ | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 	/* dconf for phishing engine sets scanContents, so no need for a flag here */ | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 	struct parser_state *js_state = NULL; | 
					
						
							| 
									
										
										
										
											2008-07-14 19:31:56 +00:00
										 |  |  | 	const unsigned char *js_begin = NULL, *js_end = NULL; | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 	struct tag_contents contents; | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 	tag_args.scanContents=0;/* do we need to store the contents of <a></a>?*/ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 	contents.pos = 0; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	if (!m_area) { | 
					
						
							|  |  |  | 		if (fd < 0) { | 
					
						
							|  |  |  | 			cli_dbgmsg("Invalid HTML fd\n"); | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		lseek(fd, 0, SEEK_SET);	 | 
					
						
							|  |  |  | 		fd_tmp = dup(fd); | 
					
						
							|  |  |  | 		if (fd_tmp < 0) { | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		stream_in = fdopen(fd_tmp, "r"); | 
					
						
							|  |  |  | 		if (!stream_in) { | 
					
						
							|  |  |  | 			close(fd_tmp); | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tag_args.count = 0; | 
					
						
							|  |  |  | 	tag_args.tag = NULL; | 
					
						
							|  |  |  | 	tag_args.value = NULL; | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	tag_args.contents = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	if (dirname) { | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 		snprintf(filename, 1024, "%s/rfc2397", dirname); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 		if (mkdir(filename, 0700) && errno != EEXIST) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 			file_buff_o2 = file_buff_text = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 			goto abort; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:56:26 +00:00
										 |  |  | 		file_buff_o2 = (file_buff_t *) cli_malloc(sizeof(file_buff_t)); | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 		if (!file_buff_o2) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 			file_buff_o2 = file_buff_text = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 			goto abort; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 		/* this will still contains scripts that are inside comments */ | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		snprintf(filename, 1024, "%s/nocomment.html", dirname); | 
					
						
							| 
									
										
										
										
											2004-12-22 16:11:05 +00:00
										 |  |  | 		file_buff_o2->fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); | 
					
						
							| 
									
										
										
										
											2008-05-27 16:30:47 +00:00
										 |  |  | 		if (file_buff_o2->fd == -1) { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			cli_dbgmsg("open failed: %s\n", filename); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:56:26 +00:00
										 |  |  | 			free(file_buff_o2); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 			file_buff_o2 = file_buff_text = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-15 12:28:43 +00:00
										 |  |  | 			goto abort; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 		file_buff_text = (file_buff_t *) cli_malloc(sizeof(file_buff_t)); | 
					
						
							|  |  |  | 		if(!file_buff_text) { | 
					
						
							| 
									
										
										
										
											2008-05-27 16:30:47 +00:00
										 |  |  | 			close(file_buff_o2->fd); | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 			free(file_buff_o2); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 			file_buff_o2 = file_buff_text = NULL; | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 			goto abort; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 		snprintf(filename, 1024, "%s/notags.html", dirname); | 
					
						
							|  |  |  | 		file_buff_text->fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); | 
					
						
							| 
									
										
										
										
											2008-05-27 16:30:47 +00:00
										 |  |  | 		if(file_buff_text->fd == -1) { | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 			cli_dbgmsg("open failed: %s\n", filename); | 
					
						
							|  |  |  | 			close(file_buff_o2->fd); | 
					
						
							|  |  |  | 			free(file_buff_o2); | 
					
						
							|  |  |  | 			free(file_buff_text); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 			file_buff_o2 = file_buff_text = NULL; | 
					
						
							| 
									
										
										
										
											2008-05-27 16:30:47 +00:00
										 |  |  | 			goto abort; | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:56:26 +00:00
										 |  |  | 		file_buff_o2->length = 0; | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 		file_buff_text->length = 0; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		file_buff_o2 = NULL; | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 		file_buff_text = NULL; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 	binary = FALSE; | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-01 19:38:52 +00:00
										 |  |  | 	ptr = line = cli_readchunk(stream_in, m_area, 8192); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	while (line) { | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 		if(href_contents_begin) | 
					
						
							|  |  |  | 			href_contents_begin=ptr;/*start of a new line, last line already appended to contents see below*/ | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		while (*ptr && isspace(*ptr)) { | 
					
						
							|  |  |  | 			ptr++; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		while (*ptr) { | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 			if (!binary && *ptr == '\n') { | 
					
						
							| 
									
										
										
										
											2004-09-20 09:47:12 +00:00
										 |  |  | 				/* Convert it to a space and re-process */ | 
					
						
							|  |  |  | 				*ptr = ' '; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 			if (!binary && *ptr == '\r') { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				ptr++; | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			switch (state) { | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  | 			case HTML_SPECIAL_CHAR: | 
					
						
							|  |  |  | 				cli_dbgmsg("Impossible, special_char can't occur here\n"); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			case HTML_BAD_STATE: | 
					
						
							|  |  |  | 				/* An engine error has occurred */ | 
					
						
							|  |  |  | 				cli_dbgmsg("HTML Engine Error\n"); | 
					
						
							|  |  |  | 				goto abort; | 
					
						
							|  |  |  | 			case HTML_SKIP_WS: | 
					
						
							|  |  |  | 				if (isspace(*ptr)) { | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					state = next_state; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_TRIM_WS: | 
					
						
							|  |  |  | 				if (isspace(*ptr)) { | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-26 11:05:11 +00:00
										 |  |  | 					if(!in_script) | 
					
						
							|  |  |  | 						html_output_c(file_buff_o2, ' '); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = next_state; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_NORM: | 
					
						
							|  |  |  | 				if (*ptr == '<') { | 
					
						
							| 
									
										
										
										
											2007-04-28 20:15:22 +00:00
										 |  |  | 					ptrend=ptr; /* for use by scanContents */ | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, '<'); | 
					
						
							|  |  |  | 					if (!in_script && !text_space_written) { | 
					
						
							|  |  |  | 						html_output_c(file_buff_text, ' '); | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 						text_space_written = TRUE; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 					if(hrefs && hrefs->scanContents && in_ahref && href_contents_begin) { | 
					
						
							|  |  |  | 						/*append this text portion to the contents of <a>*/ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 						html_tag_contents_append(&contents,href_contents_begin,ptr); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 						href_contents_begin=NULL;/*We just encountered another tag inside <a>, so skip it*/ | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					ptr++; | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					tag_length=0; | 
					
						
							|  |  |  | 					next_state = HTML_TAG; | 
					
						
							|  |  |  | 				} else if (isspace(*ptr)) { | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 					if(!text_space_written && !in_script) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_text, ' '); | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 						text_space_written = TRUE; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = HTML_TRIM_WS; | 
					
						
							|  |  |  | 					next_state = HTML_NORM; | 
					
						
							|  |  |  | 				} else if (*ptr == '&') { | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 					if(!text_space_written && !in_script) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_text, ' '); | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 						text_space_written = TRUE; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = HTML_CHAR_REF; | 
					
						
							|  |  |  | 					next_state = HTML_NORM; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-26 21:34:44 +00:00
										 |  |  | 					unsigned char c = tolower(*ptr); | 
					
						
							|  |  |  | 					/* normalize ' to " for scripts */ | 
					
						
							|  |  |  | 					if(in_script && c == '\'') c = '"'; | 
					
						
							|  |  |  | 					html_output_c(file_buff_o2, c); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					if (!in_script) { | 
					
						
							| 
									
										
										
										
											2008-02-26 11:05:11 +00:00
										 |  |  | 						if(*ptr < 0x20) { | 
					
						
							|  |  |  | 							if(!text_space_written) { | 
					
						
							|  |  |  | 								html_output_c(file_buff_text, ' '); | 
					
						
							|  |  |  | 								text_space_written = TRUE; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2008-02-26 21:34:44 +00:00
										 |  |  | 							html_output_c(file_buff_text, c); | 
					
						
							| 
									
										
										
										
											2008-02-26 11:05:11 +00:00
										 |  |  | 							text_space_written = FALSE; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_TAG: | 
					
						
							|  |  |  | 				if ((tag_length == 0) && (*ptr == '!')) { | 
					
						
							|  |  |  | 					/* Comment */ | 
					
						
							|  |  |  | 					if (in_script) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						/* we still write scripts to nocomment.html */ | 
					
						
							|  |  |  | 						html_output_c(file_buff_o2, '!'); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						/* Need to rewind in the no-comment output stream */ | 
					
						
							|  |  |  | 						if (file_buff_o2 && (file_buff_o2->length > 0)) { | 
					
						
							|  |  |  | 							file_buff_o2->length--; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					state = HTML_COMMENT; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else if (*ptr == '>') { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, '>'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					ptr++; | 
					
						
							|  |  |  | 					tag[tag_length] = '\0'; | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					next_state = HTML_PROCESS_TAG; | 
					
						
							|  |  |  | 				} else if (!isspace(*ptr)) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, tolower(*ptr)); | 
					
						
							| 
									
										
										
										
											2008-02-26 22:20:13 +00:00
										 |  |  | 					/* if we're inside a script we only care for </script>.*/ | 
					
						
							|  |  |  | 					if(in_script && tag_length==0 && *ptr != '/') { | 
					
						
							|  |  |  | 						state = HTML_NORM; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					if (tag_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 						tag[tag_length++] = tolower(*ptr); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				}  else { | 
					
						
							|  |  |  | 					tag[tag_length] = '\0'; | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					tag_arg_length = 0; | 
					
						
							| 
									
										
										
										
											2008-02-26 22:20:13 +00:00
										 |  |  | 					/* if we'd go to HTML_TAG_ARG whitespace would be inconsistently normalized for in_script*/ | 
					
						
							| 
									
										
										
										
											2008-02-27 18:51:00 +00:00
										 |  |  | 					next_state = !in_script ? HTML_TAG_ARG : HTML_PROCESS_TAG; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_TAG_ARG: | 
					
						
							|  |  |  | 				if (*ptr == '=') { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, '='); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					tag_arg[tag_arg_length] = '\0'; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					escape = FALSE; | 
					
						
							|  |  |  | 					quoted = NOT_QUOTED; | 
					
						
							|  |  |  | 					tag_val_length = 0; | 
					
						
							|  |  |  | 					next_state = HTML_TAG_ARG_VAL; | 
					
						
							|  |  |  | 				} else if (isspace(*ptr)) { | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 					tag_arg[tag_arg_length] = '\0'; | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					next_state = HTML_TAG_ARG_EQUAL; | 
					
						
							|  |  |  | 				} else if (*ptr == '>') { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, '>'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					if (tag_arg_length > 0) { | 
					
						
							|  |  |  | 						tag_arg[tag_arg_length] = '\0'; | 
					
						
							|  |  |  | 						html_tag_arg_add(&tag_args, tag_arg, NULL); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 					state = HTML_PROCESS_TAG; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if (tag_arg_length == 0) { | 
					
						
							|  |  |  | 						/* Start of new tag - add space */ | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2,' '); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, tolower(*ptr)); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					if (tag_arg_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 						tag_arg[tag_arg_length++] = tolower(*ptr); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_TAG_ARG_EQUAL: | 
					
						
							|  |  |  | 				if (*ptr == '=') { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, '='); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					ptr++; | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					escape = FALSE; | 
					
						
							|  |  |  | 					quoted = NOT_QUOTED; | 
					
						
							|  |  |  | 					tag_val_length = 0; | 
					
						
							|  |  |  | 					next_state = HTML_TAG_ARG_VAL; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if (tag_arg_length > 0) { | 
					
						
							|  |  |  | 						tag_arg[tag_arg_length] = '\0'; | 
					
						
							|  |  |  | 						html_tag_arg_add(&tag_args, tag_arg, NULL); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					tag_arg_length=0; | 
					
						
							|  |  |  | 					state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_TAG_ARG_VAL: | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				if ((tag_val_length == 5) && (strncmp(tag_val, "data:", 5) == 0)) { | 
					
						
							|  |  |  | 					/* RFC2397 inline data */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					/* Rewind one byte so we don't recursuive */ | 
					
						
							|  |  |  | 					if (file_buff_o2 && (file_buff_o2->length > 0)) { | 
					
						
							|  |  |  | 						file_buff_o2->length--; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 					if (quoted != NOT_QUOTED) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					tag_val_length = 0; | 
					
						
							|  |  |  | 					state = HTML_RFC2397_TYPE; | 
					
						
							|  |  |  | 					next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 				} else if ((tag_val_length == 6) && (strncmp(tag_val, "\"data:", 6) == 0)) { | 
					
						
							|  |  |  | 					/* RFC2397 inline data */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					/* Rewind one byte so we don't recursuive */ | 
					
						
							|  |  |  | 					if (file_buff_o2 && (file_buff_o2->length > 0)) { | 
					
						
							|  |  |  | 						file_buff_o2->length--; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 					if (quoted != NOT_QUOTED) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					tag_val_length = 0; | 
					
						
							|  |  |  | 					state = HTML_RFC2397_TYPE; | 
					
						
							|  |  |  | 					next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 				} else if (*ptr == '&') { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = HTML_CHAR_REF; | 
					
						
							|  |  |  | 					next_state = HTML_TAG_ARG_VAL; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else if (*ptr == '\'') { | 
					
						
							|  |  |  | 					if (tag_val_length == 0) { | 
					
						
							|  |  |  | 						quoted = SINGLE_QUOTED; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 						if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						if (!escape && (quoted==SINGLE_QUOTED)) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 							if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							tag_val[tag_val_length] = '\0'; | 
					
						
							|  |  |  | 							html_tag_arg_add(&tag_args, tag_arg, tag_val); | 
					
						
							|  |  |  | 							ptr++; | 
					
						
							|  |  |  | 							state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 							tag_arg_length=0; | 
					
						
							|  |  |  | 							next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 							if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							ptr++; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (*ptr == '"') { | 
					
						
							|  |  |  | 					if (tag_val_length == 0) { | 
					
						
							|  |  |  | 						quoted = DOUBLE_QUOTED; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 						if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 						if (!escape && (quoted==DOUBLE_QUOTED)) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 							if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							tag_val[tag_val_length] = '\0'; | 
					
						
							|  |  |  | 							html_tag_arg_add(&tag_args, tag_arg, tag_val); | 
					
						
							|  |  |  | 							ptr++; | 
					
						
							|  |  |  | 							state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 							tag_arg_length=0; | 
					
						
							|  |  |  | 							next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, '"'); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 							if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							ptr++; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (isspace(*ptr) || (*ptr == '>')) { | 
					
						
							|  |  |  | 					if (quoted == NOT_QUOTED) { | 
					
						
							|  |  |  | 						tag_val[tag_val_length] = '\0'; | 
					
						
							|  |  |  | 						html_tag_arg_add(&tag_args, tag_arg, tag_val); | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						tag_arg_length=0; | 
					
						
							|  |  |  | 						next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, *ptr); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 						if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							if (isspace(*ptr)) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = ' '; | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '>'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						escape = FALSE; | 
					
						
							|  |  |  | 						quoted = NOT_QUOTED; | 
					
						
							|  |  |  | 						next_state = HTML_TAG_ARG_VAL; | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, tolower(*ptr)); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							| 
									
										
										
										
											2004-09-15 09:07:45 +00:00
										 |  |  | 						tag_val[tag_val_length++] = *ptr; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				if (*ptr == '\\') { | 
					
						
							|  |  |  | 					escape = TRUE; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					escape = FALSE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_COMMENT: | 
					
						
							| 
									
										
										
										
											2008-02-26 11:05:11 +00:00
										 |  |  | 				if (in_script && !isspace(*ptr)) { | 
					
						
							| 
									
										
										
										
											2008-03-01 09:24:37 +00:00
										 |  |  | 					unsigned char c = tolower(*ptr); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					/* dump script to nocomment.html, since we no longer have
 | 
					
						
							|  |  |  | 					 * comment.html/script.html */ | 
					
						
							| 
									
										
										
										
											2008-03-01 09:24:37 +00:00
										 |  |  | 					if(c == '\'') c = '"'; | 
					
						
							|  |  |  | 					html_output_c(file_buff_o2, c); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				if (*ptr == '>') { | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					next_state = HTML_NORM; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				ptr++; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_PROCESS_TAG: | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				/* Default to no action for this tag */ | 
					
						
							|  |  |  | 				state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 				next_state = HTML_NORM; | 
					
						
							|  |  |  | 				if (tag[0] == '/') { | 
					
						
							|  |  |  | 					/* End tag */ | 
					
						
							|  |  |  | 					state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 					next_state = HTML_NORM; | 
					
						
							|  |  |  | 					if (strcmp(tag, "/script") == 0) { | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 						in_script = FALSE; | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 						if(js_state) { | 
					
						
							| 
									
										
										
										
											2008-07-14 19:31:56 +00:00
										 |  |  | 							js_end = ptr; | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 							js_process(js_state, js_begin, js_end, line, ptr, in_script, dirname); | 
					
						
							|  |  |  | 							js_state = NULL; | 
					
						
							|  |  |  | 							js_begin = js_end = NULL; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-02-27 15:57:30 +00:00
										 |  |  | 						/*don't output newlines in nocomment.html
 | 
					
						
							|  |  |  | 						 * html_output_c(file_buff_o2, '\n');*/ | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 					if (hrefs && hrefs->scanContents && in_ahref) { | 
					
						
							|  |  |  | 						if(strcmp(tag,"/a") == 0) { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 							html_tag_contents_done(hrefs,in_ahref, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							in_ahref=0;/* we are no longer inside an <a href>
 | 
					
						
							|  |  |  | 							nesting <a> tags not supported, and shouldn't be supported*/ | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						href_contents_begin=ptr; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (strcmp(tag, "/form") == 0)  { | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 						if (in_form_action) | 
					
						
							|  |  |  | 							free(in_form_action); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 						in_form_action = NULL; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} else if (strcmp(tag, "script") == 0) { | 
					
						
							|  |  |  | 					arg_value = html_tag_arg_value(&tag_args, "language"); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					/* TODO: maybe we can output all tags only via html_output_tag */ | 
					
						
							| 
									
										
										
										
											2004-09-17 10:09:23 +00:00
										 |  |  | 					if (arg_value && (strcasecmp(arg_value, "jscript.encode") == 0)) { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 						html_tag_arg_set(&tag_args, "language", "javascript"); | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						next_state = HTML_JSDECODE; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						/* we already output the old tag, output the new tag now */ | 
					
						
							|  |  |  | 						html_output_tag(file_buff_o2, tag, &tag_args); | 
					
						
							| 
									
										
										
										
											2004-09-17 10:09:23 +00:00
										 |  |  | 					} else if (arg_value && (strcasecmp(arg_value, "vbscript.encode") == 0)) { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 						html_tag_arg_set(&tag_args, "language", "vbscript"); | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						next_state = HTML_JSDECODE; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						/* we already output the old tag, output the new tag now */ | 
					
						
							|  |  |  | 						html_output_tag(file_buff_o2, tag, &tag_args); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					in_script = TRUE; | 
					
						
							|  |  |  | 					if(dconf_js && !js_state) { | 
					
						
							|  |  |  | 						js_state = cli_js_init(); | 
					
						
							|  |  |  | 						if(!js_state) { | 
					
						
							|  |  |  | 							cli_dbgmsg("htmlnorm: Failed to initialize js parser"); | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 						js_begin = ptr; | 
					
						
							|  |  |  | 						js_end = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 				} else if(strcmp(tag, "%@") == 0) { | 
					
						
							|  |  |  | 					arg_value = html_tag_arg_value(&tag_args, "language"); | 
					
						
							| 
									
										
										
										
											2008-07-14 16:24:43 +00:00
										 |  |  | 					if(arg_value && (strcasecmp(arg_value,"jscript.encode") == 0|| | 
					
						
							|  |  |  | 							strcasecmp(arg_value, "vbscript.encode") == 0)) { | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						saved_next_state = next_state; | 
					
						
							|  |  |  | 						next_state = state; | 
					
						
							| 
									
										
										
										
											2008-08-01 17:37:06 +00:00
										 |  |  | 						look_for_screnc = FALSE; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 						state = HTML_LOOKFOR_SCRENC; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 				} else if (hrefs) { | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 					if(in_ahref && !href_contents_begin) | 
					
						
							|  |  |  | 						href_contents_begin=ptr; | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 					if (strcmp(tag, "a") == 0) { | 
					
						
							|  |  |  | 						arg_value = html_tag_arg_value(&tag_args, "href"); | 
					
						
							|  |  |  | 						if (arg_value && strlen(arg_value) > 0) { | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if (hrefs->scanContents) { | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 								unsigned char* arg_value_title = html_tag_arg_value(&tag_args,"title"); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 								/*beginning of an <a> tag*/ | 
					
						
							|  |  |  | 								if (in_ahref) | 
					
						
							|  |  |  | 									/*we encountered nested <a> tags, pretend previous closed*/ | 
					
						
							|  |  |  | 									if (href_contents_begin) { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 										html_tag_contents_append(&contents, href_contents_begin, ptrend); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 										/*add pending contents between tags*/ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 										html_tag_contents_done(hrefs, in_ahref, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 										in_ahref=0; | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 								if (arg_value_title) { | 
					
						
							|  |  |  | 									/* title is a 'displayed link'*/ | 
					
						
							|  |  |  | 									html_tag_arg_add(hrefs,"href_title",arg_value_title); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 									html_tag_contents_append(&contents,arg_value, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 										arg_value+strlen(arg_value)); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 									html_tag_contents_done(hrefs, hrefs->count, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 								} | 
					
						
							|  |  |  | 								if (in_form_action) { | 
					
						
							|  |  |  | 									/* form action is the real URL, and href is the 'displayed' */ | 
					
						
							|  |  |  | 									html_tag_arg_add(hrefs,"form",arg_value); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 									contents.pos = 0; | 
					
						
							|  |  |  | 									html_tag_contents_append(&contents, in_form_action, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 											in_form_action + strlen(in_form_action)); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 									html_tag_contents_done(hrefs, hrefs->count, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 							html_tag_arg_add(hrefs, "href", arg_value); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if (hrefs->scanContents) { | 
					
						
							|  |  |  | 								in_ahref=hrefs->count; /* index of this tag (counted from 1) */ | 
					
						
							|  |  |  | 								href_contents_begin=ptr;/* contents begin after <a ..> ends */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								contents.pos = 0; | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 					} else if (strcmp(tag,"form") == 0 && hrefs->scanContents) { | 
					
						
							|  |  |  | 						const unsigned char* arg_action_value = html_tag_arg_value(&tag_args,"action"); | 
					
						
							| 
									
										
										
										
											2006-10-10 23:51:49 +00:00
										 |  |  | 						if (arg_action_value) { | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 							if(in_form_action) | 
					
						
							|  |  |  | 								free(in_form_action); | 
					
						
							| 
									
										
										
										
											2007-01-31 18:13:17 +00:00
										 |  |  | 							in_form_action = cli_strdup(arg_action_value); | 
					
						
							| 
									
										
										
										
											2006-10-10 23:51:49 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 					} else if (strcmp(tag, "img") == 0) { | 
					
						
							|  |  |  | 						arg_value = html_tag_arg_value(&tag_args, "src"); | 
					
						
							|  |  |  | 						if (arg_value && strlen(arg_value) > 0) { | 
					
						
							|  |  |  | 							html_tag_arg_add(hrefs, "src", arg_value); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if(hrefs->scanContents && in_ahref) | 
					
						
							|  |  |  | 								/* "contents" of an img tag, is the URL of its parent <a> tag */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								hrefs->contents[hrefs->count-1] = cli_strdup(hrefs->value[in_ahref-1]); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if (in_form_action) { | 
					
						
							|  |  |  | 								/* form action is the real URL, and href is the 'displayed' */ | 
					
						
							|  |  |  | 								html_tag_arg_add(hrefs,"form",arg_value); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								contents.pos = 0; | 
					
						
							|  |  |  | 								html_tag_contents_append(&contents, in_form_action, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 										in_form_action + strlen(in_form_action)); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								html_tag_contents_done(hrefs, hrefs->count, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						arg_value = html_tag_arg_value(&tag_args, "dynsrc"); | 
					
						
							|  |  |  | 						if (arg_value && strlen(arg_value) > 0) { | 
					
						
							|  |  |  | 							html_tag_arg_add(hrefs, "dynsrc", arg_value); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if(hrefs->scanContents && in_ahref) | 
					
						
							|  |  |  | 								/* see above */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								hrefs->contents[hrefs->count-1] = cli_strdup(hrefs->value[in_ahref-1]); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if (in_form_action) { | 
					
						
							|  |  |  | 								/* form action is the real URL, and href is the 'displayed' */ | 
					
						
							|  |  |  | 								html_tag_arg_add(hrefs,"form",arg_value); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								contents.pos = 0; | 
					
						
							|  |  |  | 								html_tag_contents_append(&contents, in_form_action, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 										in_form_action + strlen(in_form_action)); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								html_tag_contents_done(hrefs, hrefs->count, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} else if (strcmp(tag, "iframe") == 0) { | 
					
						
							|  |  |  | 						arg_value = html_tag_arg_value(&tag_args, "src"); | 
					
						
							|  |  |  | 						if (arg_value && strlen(arg_value) > 0) { | 
					
						
							|  |  |  | 							html_tag_arg_add(hrefs, "iframe", arg_value); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if(hrefs->scanContents && in_ahref) | 
					
						
							|  |  |  | 								/* see above */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								hrefs->contents[hrefs->count-1] = cli_strdup(hrefs->value[in_ahref-1]); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if (in_form_action) { | 
					
						
							|  |  |  | 								/* form action is the real URL, and href is the 'displayed' */ | 
					
						
							|  |  |  | 								html_tag_arg_add(hrefs,"form",arg_value); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								contents.pos = 0; | 
					
						
							|  |  |  | 								html_tag_contents_append(&contents, in_form_action, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 										in_form_action + strlen(in_form_action)); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								html_tag_contents_done(hrefs, hrefs->count, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2004-10-27 08:32:37 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 					} else if (strcmp(tag,"area") == 0) { | 
					
						
							|  |  |  | 						arg_value = html_tag_arg_value(&tag_args,"href"); | 
					
						
							|  |  |  | 						if (arg_value && strlen(arg_value) > 0) { | 
					
						
							|  |  |  | 							html_tag_arg_add(hrefs, "area", arg_value); | 
					
						
							|  |  |  | 							if(hrefs->scanContents && in_ahref) | 
					
						
							|  |  |  | 								/* see above */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								hrefs->contents[hrefs->count-1] = cli_strdup(hrefs->value[in_ahref-1]); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							if (in_form_action) { | 
					
						
							|  |  |  | 								/* form action is the real URL, and href is the 'displayed' */ | 
					
						
							|  |  |  | 								html_tag_arg_add(hrefs,"form",arg_value); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								contents.pos = 0; | 
					
						
							|  |  |  | 								html_tag_contents_append(&contents, in_form_action, | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 									in_form_action + strlen(in_form_action)); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 								html_tag_contents_done(hrefs, hrefs->count, &contents); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					/* TODO:imagemaps can have urls too */ | 
					
						
							| 
									
										
										
										
											2008-02-13 10:24:51 +00:00
										 |  |  | 				} else if (strcmp(tag, "a") == 0) { | 
					
						
							|  |  |  | 					/* a/img tags for buff_text can be processed only if we're not processing hrefs */ | 
					
						
							|  |  |  | 					arg_value = html_tag_arg_value(&tag_args, "href"); | 
					
						
							|  |  |  | 					if(arg_value && arg_value[0]) { | 
					
						
							|  |  |  | 						html_output_str(file_buff_text, arg_value, strlen(arg_value)); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_text, ' '); | 
					
						
							| 
									
										
										
										
											2008-02-26 11:05:11 +00:00
										 |  |  | 						text_space_written = TRUE; | 
					
						
							| 
									
										
										
										
											2008-02-13 10:24:51 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} else if (strcmp(tag, "img") == 0) { | 
					
						
							|  |  |  | 					arg_value = html_tag_arg_value(&tag_args, "src"); | 
					
						
							|  |  |  | 					if(arg_value && arg_value[0]) { | 
					
						
							|  |  |  | 						html_output_str(file_buff_text, arg_value, strlen(arg_value)); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_text, ' '); | 
					
						
							| 
									
										
										
										
											2008-02-26 11:05:11 +00:00
										 |  |  | 						text_space_written = TRUE; | 
					
						
							| 
									
										
										
										
											2008-02-13 10:24:51 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				html_tag_arg_free(&tag_args); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_CHAR_REF: | 
					
						
							|  |  |  | 				if (*ptr == '#') { | 
					
						
							|  |  |  | 					value = 0; | 
					
						
							|  |  |  | 					hex = FALSE; | 
					
						
							|  |  |  | 					state = HTML_CHAR_REF_DECODE; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 					if(dconf_entconv) | 
					
						
							|  |  |  | 						state = HTML_ENTITY_REF_DECODE; | 
					
						
							|  |  |  | 					else { | 
					
						
							|  |  |  | 						if(next_state == HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							tag_val[tag_val_length++] = '&'; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '&'); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 						state = next_state; | 
					
						
							|  |  |  | 						next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_ENTITY_REF_DECODE: | 
					
						
							|  |  |  | 				if(*ptr == ';') { | 
					
						
							|  |  |  | 					size_t i; | 
					
						
							| 
									
										
										
										
											2008-01-21 15:52:21 +00:00
										 |  |  | 					const char* normalized; | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 					entity_val[entity_val_length] = '\0'; | 
					
						
							|  |  |  | 					normalized = entity_norm(&conv, entity_val); | 
					
						
							|  |  |  | 					if(normalized) { | 
					
						
							|  |  |  | 						for(i=0; i < strlen(normalized); i++) { | 
					
						
							| 
									
										
										
										
											2008-01-21 15:52:21 +00:00
										 |  |  | 							const unsigned char c = normalized[i]&0xff; | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, c); | 
					
						
							| 
									
										
										
										
											2007-03-05 19:31:17 +00:00
										 |  |  | 							if (next_state == HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 								tag_val[tag_val_length++] = c; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '&'); | 
					
						
							| 
									
										
										
										
											2007-03-05 19:31:17 +00:00
										 |  |  | 						if (next_state == HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '&'; | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 						for(i=0; i < entity_val_length; i++) { | 
					
						
							|  |  |  | 							const char c = tolower(entity_val[i]); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, c); | 
					
						
							| 
									
										
										
										
											2007-03-05 19:31:17 +00:00
										 |  |  | 							if (next_state == HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 								tag_val[tag_val_length++] = c; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2007-03-05 19:31:17 +00:00
										 |  |  | 						if (next_state == HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							tag_val[tag_val_length++] = ';'; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, ';'); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					entity_val_length = 0; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = next_state; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else if ( (isalnum(*ptr) || *ptr=='_' || *ptr==':' || (*ptr=='-')) && entity_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 					entity_val[entity_val_length++] = *ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else { | 
					
						
							|  |  |  | 						/* entity too long, or not valid, dump it */ | 
					
						
							|  |  |  | 						size_t i; | 
					
						
							| 
									
										
										
										
											2007-03-05 19:31:17 +00:00
										 |  |  | 						if (next_state==HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '&'; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_buff_o2, '&'); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 						for(i=0; i < entity_val_length; i++) { | 
					
						
							|  |  |  | 							const char c = tolower(entity_val[i]); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, c); | 
					
						
							| 
									
										
										
										
											2007-03-05 19:31:17 +00:00
										 |  |  | 							if (next_state==HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 								tag_val[tag_val_length++] = c; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						state = next_state; | 
					
						
							|  |  |  | 						next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 						entity_val_length = 0; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_CHAR_REF_DECODE: | 
					
						
							|  |  |  | 				if ((value==0) && ((*ptr == 'x') || (*ptr == 'X'))) { | 
					
						
							|  |  |  | 					hex=TRUE; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else if (*ptr == ';') { | 
					
						
							| 
									
										
										
										
											2007-06-16 17:03:42 +00:00
										 |  |  | 					if (next_state==HTML_TAG_ARG_VAL && tag_val_length < HTML_STR_LENGTH) { | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 							tag_val[tag_val_length++] = value; /* store encoded values too */ | 
					
						
							| 
									
										
										
										
											2007-06-16 17:03:42 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					if(dconf_entconv) { | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 						if(value < 0x80) | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, tolower(value)); | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 						else { | 
					
						
							|  |  |  | 							unsigned char buff[10]; | 
					
						
							| 
									
										
										
										
											2008-01-23 15:43:32 +00:00
										 |  |  | 							unsigned char* out = u16_normalize_tobuffer(value, buff, 10); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							if(out && out>buff) { | 
					
						
							|  |  |  | 								html_output_str(file_buff_o2, buff, out-buff-1); | 
					
						
							| 
									
										
										
										
											2008-01-23 15:43:32 +00:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} else | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 							html_output_c(file_buff_o2, tolower(value&0xff)); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = next_state; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else if (isdigit(*ptr) || (hex && isxdigit(*ptr))) { | 
					
						
							|  |  |  | 					if (hex) { | 
					
						
							|  |  |  | 						value *= 16; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						value *= 10; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (isdigit(*ptr)) { | 
					
						
							|  |  |  | 						value += (*ptr - '0'); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						value += (tolower(*ptr) - 'a' + 10); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, value); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					state = next_state; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 			case HTML_LOOKFOR_SCRENC: | 
					
						
							|  |  |  | 				look_for_screnc = TRUE; | 
					
						
							|  |  |  | 				ptr_screnc = strstr(ptr, "#@~^"); | 
					
						
							|  |  |  | 				if(ptr_screnc) { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 					ptr_screnc[0] = '/'; | 
					
						
							|  |  |  | 					ptr_screnc[1] = '/'; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 					ptr_screnc += 4; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				state = next_state; | 
					
						
							|  |  |  | 				next_state = saved_next_state; | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			case HTML_JSDECODE: | 
					
						
							|  |  |  | 				/* Check for start marker */ | 
					
						
							|  |  |  | 				if (strncmp(ptr, "#@~^", 4) == 0) { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 					ptr[0] = '/'; | 
					
						
							|  |  |  | 					ptr[1] = '/'; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					ptr += 4; | 
					
						
							|  |  |  | 					state = HTML_JSDECODE_LENGTH; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, tolower(*ptr)); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_JSDECODE_LENGTH: | 
					
						
							|  |  |  | 				if (strlen(ptr) < 8) { | 
					
						
							|  |  |  | 					state = HTML_NORM; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 				memset(&screnc_state, 0, sizeof(screnc_state)); | 
					
						
							|  |  |  | 				screnc_state.length = base64_chars[ptr[0]] << 2; | 
					
						
							|  |  |  | 				screnc_state.length += base64_chars[ptr[1]] >> 4; | 
					
						
							|  |  |  | 				screnc_state.length += (base64_chars[ptr[1]] & 0x0f) << 12; | 
					
						
							|  |  |  | 				screnc_state.length += (base64_chars[ptr[2]] >> 2) << 8; | 
					
						
							|  |  |  | 				screnc_state.length += (base64_chars[ptr[2]] & 0x03) << 22; | 
					
						
							|  |  |  | 				screnc_state.length += base64_chars[ptr[3]] << 16; | 
					
						
							|  |  |  | 				screnc_state.length += (base64_chars[ptr[4]] << 2) << 24; | 
					
						
							|  |  |  | 				screnc_state.length += (base64_chars[ptr[5]] >> 4) << 24; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				state = HTML_JSDECODE_DECRYPT; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 				in_screnc = TRUE; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				next_state = HTML_BAD_STATE; | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 				/* for JS normalizer */ | 
					
						
							|  |  |  | 				ptr[7] = '\n'; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				ptr += 8; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_JSDECODE_DECRYPT: | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 				screnc_decode(ptr, &screnc_state); | 
					
						
							|  |  |  | 				if(!screnc_state.length) { | 
					
						
							|  |  |  | 					state = HTML_NORM; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 					in_screnc = FALSE; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					state = HTML_NORM; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 			case HTML_RFC2397_TYPE: | 
					
						
							|  |  |  | 				if (*ptr == '\'') { | 
					
						
							|  |  |  | 					if (!escape && (quoted==SINGLE_QUOTED)) { | 
					
						
							|  |  |  | 						/* Early end of data detected. Error */ | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						tag_arg_length=0; | 
					
						
							|  |  |  | 						next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (*ptr == '"') { | 
					
						
							|  |  |  | 					if (!escape && (quoted==DOUBLE_QUOTED)) { | 
					
						
							|  |  |  | 						/* Early end of data detected. Error */ | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						tag_arg_length=0; | 
					
						
							|  |  |  | 						next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							tag_val[tag_val_length++] = '"'; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (isspace(*ptr) || (*ptr == '>')) { | 
					
						
							|  |  |  | 					if (quoted == NOT_QUOTED) { | 
					
						
							|  |  |  | 						/* Early end of data detected. Error */ | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						tag_arg_length=0; | 
					
						
							|  |  |  | 						next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 							if (isspace(*ptr)) { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = ' '; | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								tag_val[tag_val_length++] = '>'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 						escape = FALSE; | 
					
						
							|  |  |  | 						quoted = NOT_QUOTED; | 
					
						
							|  |  |  | 						next_state = HTML_RFC2397_TYPE; | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (*ptr == ',') { | 
					
						
							|  |  |  | 					/* Beginning of data */ | 
					
						
							|  |  |  | 					tag_val[tag_val_length] = '\0'; | 
					
						
							|  |  |  | 					state = HTML_RFC2397_INIT; | 
					
						
							|  |  |  | 					escape = FALSE; | 
					
						
							|  |  |  | 					next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					if (tag_val_length < HTML_STR_LENGTH) { | 
					
						
							|  |  |  | 						tag_val[tag_val_length++] = tolower(*ptr); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (*ptr == '\\') { | 
					
						
							|  |  |  | 					escape = TRUE; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					escape = FALSE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_RFC2397_INIT: | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 				if (dirname) { | 
					
						
							|  |  |  | 					file_tmp_o1 = (file_buff_t *) cli_malloc(sizeof(file_buff_t)); | 
					
						
							|  |  |  | 					if (!file_tmp_o1) { | 
					
						
							|  |  |  | 						goto abort; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					snprintf(filename, 1024, "%s/rfc2397", dirname); | 
					
						
							|  |  |  | 					tmp_file = cli_gentemp(filename); | 
					
						
							| 
									
										
										
										
											2008-03-06 20:19:22 +00:00
										 |  |  | 					if(!tmp_file) { | 
					
						
							|  |  |  | 						free(file_tmp_o1); | 
					
						
							|  |  |  | 						goto abort; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 					cli_dbgmsg("RFC2397 data file: %s\n", tmp_file); | 
					
						
							|  |  |  | 					file_tmp_o1->fd = open(tmp_file, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); | 
					
						
							|  |  |  | 					free(tmp_file); | 
					
						
							| 
									
										
										
										
											2008-08-21 15:58:02 +00:00
										 |  |  | 					if (file_tmp_o1->fd < 0) { | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 						cli_dbgmsg("open failed: %s\n", filename); | 
					
						
							|  |  |  | 						free(file_tmp_o1); | 
					
						
							|  |  |  | 						goto abort; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					file_tmp_o1->length = 0; | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 					html_output_str(file_tmp_o1, "From html-normalise\n", 20); | 
					
						
							|  |  |  | 					html_output_str(file_tmp_o1, "Content-type: ", 14); | 
					
						
							|  |  |  | 					if ((tag_val_length == 0) && (*tag_val == ';')) { | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 						html_output_str(file_tmp_o1, "text/plain\n", 11); | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					html_output_str(file_tmp_o1, tag_val, tag_val_length); | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_tmp_o1, '\n'); | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 					if (strstr(tag_val, ";base64") != NULL) { | 
					
						
							|  |  |  | 						html_output_str(file_tmp_o1, "Content-transfer-encoding: base64\n", 34); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_tmp_o1, '\n'); | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					file_tmp_o1 = NULL; | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				state = HTML_RFC2397_DATA; | 
					
						
							|  |  |  | 				binary = TRUE; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_RFC2397_DATA: | 
					
						
							|  |  |  | 				if (*ptr == '&') { | 
					
						
							|  |  |  | 					state = HTML_CHAR_REF; | 
					
						
							|  |  |  | 					next_state = HTML_RFC2397_DATA; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else if (*ptr == '%') { | 
					
						
							|  |  |  | 					length = 0; | 
					
						
							|  |  |  | 					value = 0; | 
					
						
							|  |  |  | 					state = HTML_ESCAPE_CHAR; | 
					
						
							|  |  |  | 					next_state = HTML_RFC2397_ESC; | 
					
						
							|  |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} else if (*ptr == '\'') { | 
					
						
							|  |  |  | 					if (!escape && (quoted==SINGLE_QUOTED)) { | 
					
						
							|  |  |  | 						state = HTML_RFC2397_FINISH; | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_tmp_o1, *ptr); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (*ptr == '\"') { | 
					
						
							| 
									
										
										
										
											2006-03-22 18:07:31 +00:00
										 |  |  | 					if (!escape && (quoted==DOUBLE_QUOTED)) { | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 						state = HTML_RFC2397_FINISH; | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_tmp_o1, *ptr); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else if (isspace(*ptr) || (*ptr == '>')) { | 
					
						
							|  |  |  | 					if (quoted == NOT_QUOTED) { | 
					
						
							|  |  |  | 						state = HTML_RFC2397_FINISH; | 
					
						
							|  |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 						html_output_c(file_tmp_o1, *ptr); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 						ptr++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_tmp_o1, *ptr); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 					ptr++; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (*ptr == '\\') { | 
					
						
							|  |  |  | 					escape = TRUE; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					escape = FALSE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_RFC2397_FINISH: | 
					
						
							| 
									
										
										
										
											2007-08-21 20:30:15 +00:00
										 |  |  | 				if(file_tmp_o1) { | 
					
						
							|  |  |  | 					html_output_flush(file_tmp_o1); | 
					
						
							|  |  |  | 					close(file_tmp_o1->fd); | 
					
						
							|  |  |  | 					free(file_tmp_o1); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				state = HTML_SKIP_WS; | 
					
						
							|  |  |  | 				escape = FALSE; | 
					
						
							|  |  |  | 				quoted = NOT_QUOTED; | 
					
						
							|  |  |  | 				next_state = HTML_TAG_ARG; | 
					
						
							|  |  |  | 				binary = FALSE; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case HTML_RFC2397_ESC: | 
					
						
							|  |  |  | 				if (length == 2) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_tmp_o1, value); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				} else if (length == 1) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_tmp_o1, '%'); | 
					
						
							|  |  |  | 					html_output_c(file_tmp_o1, value+'0'); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_tmp_o1, '%'); | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				state = HTML_RFC2397_DATA; | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2005-01-14 14:56:09 +00:00
										 |  |  | 			case HTML_ESCAPE_CHAR: | 
					
						
							|  |  |  | 				value *= 16; | 
					
						
							|  |  |  | 				length++; | 
					
						
							|  |  |  | 				if (isxdigit(*ptr)) { | 
					
						
							|  |  |  | 					if (isdigit(*ptr)) { | 
					
						
							|  |  |  | 						value += (*ptr - '0'); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						value += (tolower(*ptr) - 'a' + 10); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					state = next_state; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (length == 2) { | 
					
						
							|  |  |  | 					state = next_state; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				ptr++; | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 		if(hrefs && hrefs->scanContents && in_ahref && href_contents_begin) | 
					
						
							|  |  |  | 			/* end of line, append contents now, resume on next line */ | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  | 			html_tag_contents_append(&contents,href_contents_begin,ptr); | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 		ptrend = NULL; | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if(js_state) { | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:27 +00:00
										 |  |  | 			js_process(js_state, js_begin, js_end, line, ptr, in_script, dirname); | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 			js_begin = js_end = NULL; | 
					
						
							|  |  |  | 			if(!in_script) { | 
					
						
							|  |  |  | 				js_state = NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 		if(look_for_screnc && ptr_screnc) { | 
					
						
							|  |  |  | 			/* start found, and stuff before it already processed */ | 
					
						
							|  |  |  | 			ptr = ptr_screnc; | 
					
						
							|  |  |  | 			ptr_screnc = NULL; | 
					
						
							|  |  |  | 			state = HTML_JSDECODE_LENGTH; | 
					
						
							|  |  |  | 			next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-02-01 19:38:52 +00:00
										 |  |  | 		free(line); | 
					
						
							|  |  |  | 		ptr = line = cli_readchunk(stream_in, m_area, 8192); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 		if (in_screnc) { | 
					
						
							|  |  |  | 			state = HTML_JSDECODE_DECRYPT; | 
					
						
							|  |  |  | 			next_state = HTML_BAD_STATE; | 
					
						
							| 
									
										
										
										
											2008-08-01 17:37:06 +00:00
										 |  |  | 		} else if(look_for_screnc && !ptr_screnc && | 
					
						
							|  |  |  | 				state != HTML_LOOKFOR_SCRENC) { | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 			saved_next_state = next_state; | 
					
						
							|  |  |  | 			next_state = state; | 
					
						
							|  |  |  | 			state = HTML_LOOKFOR_SCRENC; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-08-01 17:37:06 +00:00
										 |  |  | 		if(next_state == state) { | 
					
						
							|  |  |  | 			/* safeguard against infloop */ | 
					
						
							|  |  |  | 			cli_dbgmsg("htmlnorm.c: next_state == state, changing next_state\n"); | 
					
						
							|  |  |  | 			next_state = HTML_BAD_STATE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-01-20 22:18:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(dconf_entconv) { | 
					
						
							|  |  |  | 		/* handle "unfinished" entitites */ | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 		size_t i; | 
					
						
							| 
									
										
										
										
											2008-01-21 15:52:21 +00:00
										 |  |  | 		const char* normalized; | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 		entity_val[entity_val_length] = '\0'; | 
					
						
							|  |  |  | 		normalized = entity_norm(&conv, entity_val); | 
					
						
							|  |  |  | 		if(normalized) { | 
					
						
							|  |  |  | 			for(i=0; i < strlen(normalized); i++) | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 				html_output_c(file_buff_o2, normalized[i]&0xff); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			if(entity_val_length) { | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 				html_output_c(file_buff_o2, '&'); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 				for(i=0; i < entity_val_length; i++) | 
					
						
							| 
									
										
										
										
											2008-02-20 15:49:43 +00:00
										 |  |  | 					html_output_c(file_buff_o2, tolower(entity_val[i])); | 
					
						
							| 
									
										
										
										
											2006-12-26 16:17:02 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	retval = TRUE; | 
					
						
							|  |  |  | abort: | 
					
						
							| 
									
										
										
										
											2006-09-12 22:06:03 +00:00
										 |  |  | 	if (in_form_action) | 
					
						
							|  |  |  | 		free(in_form_action); | 
					
						
							| 
									
										
										
										
											2008-09-23 20:52:33 +00:00
										 |  |  |         if (in_ahref) /* tag not closed, force closing */ | 
					
						
							|  |  |  |                 html_tag_contents_done(hrefs, in_ahref, &contents); | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 	if(js_state) { | 
					
						
							| 
									
										
										
										
											2008-08-01 14:50:50 +00:00
										 |  |  | 		/*  output script so far */ | 
					
						
							|  |  |  | 		cli_js_parse_done(js_state); | 
					
						
							|  |  |  | 		cli_js_output(js_state, dirname); | 
					
						
							| 
									
										
										
										
											2008-07-08 11:33:32 +00:00
										 |  |  | 		cli_js_destroy(js_state); | 
					
						
							|  |  |  | 		js_state = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	html_tag_arg_free(&tag_args); | 
					
						
							|  |  |  | 	if (!m_area) { | 
					
						
							|  |  |  | 		fclose(stream_in); | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 11:02:35 +00:00
										 |  |  | 	if (file_buff_o2) { | 
					
						
							|  |  |  | 		html_output_flush(file_buff_o2); | 
					
						
							| 
									
										
										
										
											2008-08-21 15:58:02 +00:00
										 |  |  | 		if(file_buff_o2->fd != -1) | 
					
						
							|  |  |  | 			close(file_buff_o2->fd); | 
					
						
							| 
									
										
										
										
											2004-09-13 11:02:35 +00:00
										 |  |  | 		free(file_buff_o2); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 	if(file_buff_text) { | 
					
						
							|  |  |  | 		html_output_flush(file_buff_text); | 
					
						
							| 
									
										
										
										
											2008-08-21 15:58:02 +00:00
										 |  |  | 		if(file_buff_text->fd != -1) | 
					
						
							|  |  |  | 			close(file_buff_text->fd); | 
					
						
							| 
									
										
										
										
											2008-02-11 21:41:58 +00:00
										 |  |  | 		free(file_buff_text); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	return retval; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | int html_normalise_mem(unsigned char *in_buff, off_t in_size, const char *dirname, tag_arguments_t *hrefs,const struct cli_dconf* dconf) | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	m_area_t m_area; | 
					
						
							| 
									
										
										
										
											2008-02-01 19:38:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	m_area.buffer = in_buff; | 
					
						
							|  |  |  | 	m_area.length = in_size; | 
					
						
							|  |  |  | 	m_area.offset = 0; | 
					
						
							| 
									
										
										
										
											2008-02-01 19:38:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 	return cli_html_normalise(-1, &m_area, dirname, hrefs, dconf); | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | int html_normalise_fd(int fd, const char *dirname, tag_arguments_t *hrefs,const struct cli_dconf* dconf) | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | #if HAVE_MMAP
 | 
					
						
							|  |  |  | 	int retval=FALSE; | 
					
						
							|  |  |  | 	m_area_t m_area; | 
					
						
							|  |  |  | 	struct stat statbuf; | 
					
						
							| 
									
										
										
										
											2008-02-01 19:38:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	if (fstat(fd, &statbuf) == 0) { | 
					
						
							|  |  |  | 		m_area.length = statbuf.st_size; | 
					
						
							|  |  |  | 		m_area.buffer = (unsigned char *) mmap(NULL, m_area.length, PROT_READ, MAP_PRIVATE, fd, 0); | 
					
						
							|  |  |  | 		m_area.offset = 0; | 
					
						
							|  |  |  | 		if (m_area.buffer == MAP_FAILED) { | 
					
						
							|  |  |  | 			cli_dbgmsg("mmap HTML failed\n"); | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 			retval = cli_html_normalise(fd, NULL, dirname, hrefs, dconf); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			cli_dbgmsg("mmap'ed file\n"); | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 			retval = cli_html_normalise(-1, &m_area, dirname, hrefs, dconf); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			munmap(m_area.buffer, m_area.length); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		cli_dbgmsg("fstat HTML failed\n"); | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 		retval = cli_html_normalise(fd, NULL, dirname, hrefs, dconf); | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	return retval; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2007-05-01 16:46:52 +00:00
										 |  |  | 	return cli_html_normalise(fd, NULL, dirname, hrefs, dconf); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | int html_screnc_decode(int fd, const char *dirname) | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-08-25 14:18:48 +00:00
										 |  |  | 	int fd_tmp, count, retval=FALSE; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	unsigned char *line, tmpstr[6]; | 
					
						
							|  |  |  | 	unsigned char *ptr, filename[1024]; | 
					
						
							|  |  |  | 	FILE *stream_in; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	int ofd; | 
					
						
							|  |  |  | 	struct screnc_state screnc_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lseek(fd, 0, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	fd_tmp = dup(fd); | 
					
						
							|  |  |  | 	if (fd_tmp < 0) { | 
					
						
							|  |  |  | 		return FALSE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	stream_in = fdopen(fd_tmp, "r"); | 
					
						
							|  |  |  | 	if (!stream_in) { | 
					
						
							|  |  |  | 		close(fd_tmp); | 
					
						
							|  |  |  | 		return FALSE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	snprintf(filename, 1024, "%s/screnc.html", dirname); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	ofd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-21 15:58:02 +00:00
										 |  |  | 	if (ofd < 0) { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		cli_dbgmsg("open failed: %s\n", filename); | 
					
						
							|  |  |  | 		fclose(stream_in); | 
					
						
							|  |  |  | 		return FALSE; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 	while ((line = cli_readchunk(stream_in, NULL, 8192)) != NULL) { | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		ptr = strstr(line, "#@~^"); | 
					
						
							|  |  |  | 		if (ptr) { | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		free(line); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 	if (!line) { | 
					
						
							|  |  |  | 		goto abort; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	/* Calculate the length of the encoded string */ | 
					
						
							|  |  |  | 	ptr += 4; | 
					
						
							|  |  |  | 	count = 0; | 
					
						
							|  |  |  | 	do { | 
					
						
							|  |  |  | 		if (! *ptr) { | 
					
						
							|  |  |  | 			free(line); | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			ptr = line = cli_readchunk(stream_in, NULL, 8192); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 			if (!line) { | 
					
						
							|  |  |  | 				goto abort; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 		if(count < 6) | 
					
						
							|  |  |  | 			tmpstr[count] = *ptr; | 
					
						
							|  |  |  | 		count++; | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		ptr++; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	} while (count < 8); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memset(&screnc_state, 0, sizeof(screnc_state)); | 
					
						
							|  |  |  | 	screnc_state.length = base64_chars[tmpstr[0]] << 2; | 
					
						
							|  |  |  | 	screnc_state.length += base64_chars[tmpstr[1]] >> 4; | 
					
						
							|  |  |  | 	screnc_state.length += (base64_chars[tmpstr[1]] & 0x0f) << 12; | 
					
						
							|  |  |  | 	screnc_state.length += (base64_chars[tmpstr[2]] >> 2) << 8; | 
					
						
							|  |  |  | 	screnc_state.length += (base64_chars[tmpstr[2]] & 0x03) << 22; | 
					
						
							|  |  |  | 	screnc_state.length += base64_chars[tmpstr[3]] << 16; | 
					
						
							|  |  |  | 	screnc_state.length += (base64_chars[tmpstr[4]] << 2) << 24; | 
					
						
							|  |  |  | 	screnc_state.length += (base64_chars[tmpstr[5]] >> 4) << 24; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-01 09:56:48 +00:00
										 |  |  | 	cli_writen(ofd, "<script>",strlen("<script>")); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	while (screnc_state.length && line) { | 
					
						
							|  |  |  | 		screnc_decode(ptr, &screnc_state); | 
					
						
							| 
									
										
										
										
											2008-08-21 20:21:43 +00:00
										 |  |  | 		cli_writen(ofd, ptr, strlen(ptr)); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 		free(line); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 		if (screnc_state.length) { | 
					
						
							| 
									
										
										
										
											2008-01-25 16:39:40 +00:00
										 |  |  | 			ptr = line = cli_readchunk(stream_in, NULL, 8192); | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-10-01 09:56:48 +00:00
										 |  |  | 	cli_writen(ofd, "</script>",strlen("</script>")); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	if(screnc_state.length) | 
					
						
							| 
									
										
										
										
											2008-08-21 20:21:43 +00:00
										 |  |  | 		cli_dbgmsg("html_screnc_decode: missing %u bytes\n",screnc_state.length); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	retval = TRUE; | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | abort: | 
					
						
							|  |  |  | 	fclose(stream_in); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:50:52 +00:00
										 |  |  | 	close(ofd); | 
					
						
							| 
									
										
										
										
											2004-09-13 10:30:14 +00:00
										 |  |  | 	return retval; | 
					
						
							| 
									
										
										
										
											2004-07-02 23:00:58 +00:00
										 |  |  | } |