PRIVATE int HTGuess_put_block (HTStream * me, const char * b, int l) { while (!me->transparent && l-- > 0) { int status; if (me->target) { if ((status = HTGuess_flush(me)) != HT_OK) return status; } else { me->cnt++; if (*b == LF) me->lf_cnt++; else if (*b == CR) me->cr_cnt++; else if (*b == 12) me->pg_cnt++; else if (*b =='\t') me->text_cnt++; else if ((unsigned char)*b < 32) me->ctrl_cnt++; else if ((unsigned char)*b < 128) me->text_cnt++; else me->high_cnt++; *me->write_ptr++ = *b++; if (me->cnt >= SAMPLE_SIZE) { if ((status = HTGuess_flush(me)) != HT_OK) return status; } } } if (l > 0) return PUT_BLOCK(b, l); return HT_OK; }
/********************************************************************** * Convert ActiveX control templates to AtlAxWin class instances */ static LPDLGTEMPLATEW AX_ConvertDialogTemplate(LPCDLGTEMPLATEW src_tmpl) { #define GET_WORD(x) (*(const WORD *)(x)) #define GET_DWORD(x) (*(const DWORD *)(x)) #define PUT_BLOCK(x,y) do {if (!advance_array(&output, &allocated, &filled, (x), (y))) return NULL;} while (0) #define PUT_WORD(x) do {WORD w = (x);PUT_BLOCK(&w, 1);} while(0) #define PUT_DWORD(x) do {DWORD w = (x);PUT_BLOCK(&w, 2);} while(0) const WORD *tmp, *src = (const WORD *)src_tmpl; WORD *output; DWORD allocated, filled; /* in WORDs */ BOOL ext; WORD signature, dlgver, rescount; DWORD style; filled = 0; allocated = 256; output = HeapAlloc( GetProcessHeap(), 0, allocated * sizeof(WORD) ); if (!output) return NULL; /* header */ tmp = src; signature = GET_WORD(src); dlgver = GET_WORD(src + 1); if (signature == 1 && dlgver == 0xFFFF) { ext = TRUE; src += 6; style = GET_DWORD(src); src += 2; rescount = GET_WORD(src++); src += 4; if ( GET_WORD(src) == 0xFFFF ) /* menu */ src += 2; else src += strlenW(src) + 1; if ( GET_WORD(src) == 0xFFFF ) /* class */ src += 2; else src += strlenW(src) + 1; src += strlenW(src) + 1; /* title */ if ( style & (DS_SETFONT | DS_SHELLFONT) ) { src += 3; src += strlenW(src) + 1; } } else { ext = FALSE; style = GET_DWORD(src); src += 4; rescount = GET_WORD(src++); src += 4; if ( GET_WORD(src) == 0xFFFF ) /* menu */ src += 2; else src += strlenW(src) + 1; if ( GET_WORD(src) == 0xFFFF ) /* class */ src += 2; else src += strlenW(src) + 1; src += strlenW(src) + 1; /* title */ if ( style & DS_SETFONT ) { src++; src += strlenW(src) + 1; } } PUT_BLOCK(tmp, src-tmp); while(rescount--) { src = (const WORD *)( ( ((ULONG_PTR)src) + 3) & ~3); /* align on DWORD boundary */ filled = (filled + 1) & ~1; /* depends on DWORD-aligned allocation unit */ tmp = src; if (ext) src += 12; else src += 9; PUT_BLOCK(tmp, src-tmp); tmp = src; if ( GET_WORD(src) == 0xFFFF ) /* class */ { src += 2; } else { src += strlenW(src) + 1; } src += strlenW(src) + 1; /* title */ if ( GET_WORD(tmp) == '{' ) /* all this mess created because of this line */ { static const WCHAR AtlAxWin[] = {'A','t','l','A','x','W','i','n', 0}; PUT_BLOCK(AtlAxWin, sizeof(AtlAxWin)/sizeof(WCHAR)); PUT_BLOCK(tmp, strlenW(tmp)+1); } else PUT_BLOCK(tmp, src-tmp); if ( GET_WORD(src) ) { WORD size = (GET_WORD(src)+sizeof(WORD)-1) / sizeof(WORD); /* quite ugly :( Maybe use BYTE* instead of WORD* everywhere ? */ PUT_BLOCK(src, size); src+=size; } else { PUT_WORD(0); src++; } } return (LPDLGTEMPLATEW) output; }
PRIVATE int HTGuess_flush (HTStream * me) { if (!me->transparent) { HTResponse * response = me->response; /* ** First we look for magic tokens and evaluate the contents of the buffer ** that we are investigating. */ if (me->cnt) { HTTRACE(STREAM_TRACE, "GUESSING.... Result of content analysis: Text=%d%% Newlines=%d%% Ctrl=%d%% High=%d%%\n" _ (int)(100*me->text_cnt/me->cnt + 0.5) _ (int)(100*me->lf_cnt /me->cnt + 0.5) _ (int)(100*me->ctrl_cnt/me->cnt + 0.5) _ (int)(100*me->high_cnt/me->cnt + 0.5)); } if (!me->ctrl_cnt || me->text_cnt + me->lf_cnt >= 16 * (me->ctrl_cnt + me->high_cnt)) { char *ptr; /* some kind of text */ *me->write_ptr = 0; /* terminate buffer */ if (me->high_cnt > 0) HTResponse_setContentTransferEncoding(response, WWW_CODING_8BIT); else HTResponse_setContentTransferEncoding(response, WWW_CODING_7BIT); if (is_html(me->buffer)) HTResponse_setFormat(response, HTAtom_for("text/html")); else if (!strncmp(me->buffer, "%!", 2)) HTResponse_setFormat(response, HTAtom_for("application/postscript")); else if (strstr(me->buffer, "#define") && strstr(me->buffer, "_width") && strstr(me->buffer, "_bits")) HTResponse_setFormat(response, HTAtom_for("image/x-xbitmap")); else if ((ptr = strstr(me->buffer, "converted with BinHex"))!=NULL) HTResponse_setContentTransferEncoding(response, WWW_CODING_MACBINHEX); else if (!strncmp(me->buffer, "begin ", 6)) HTResponse_setContentTransferEncoding(response, WWW_CODING_BASE64); else HTResponse_setFormat(response, WWW_PLAINTEXT); } else { if (!strncmp(me->buffer, "GIF", 3)) HTResponse_setFormat(response, WWW_GIF); else if (!strncmp(me->buffer, "\377\330\377\340", 4)) HTResponse_setFormat(response, WWW_JPEG); else if (!strcmp(me->buffer, "MM")) /* MM followed by a zero */ HTResponse_setFormat(response, WWW_TIFF); else if (!strncmp(me->buffer, "\211PNG\r\n\032\n", 8)) HTResponse_setFormat(response, WWW_PNG); else if (!strncmp(me->buffer, ".snd", 4)) HTResponse_setFormat(response, WWW_AUDIO); else if (!strncmp(me->buffer, "\037\235", 2)) HTResponse_addEncoding(response, WWW_CODING_COMPRESS); else if (!strncmp(me->buffer, "\037\213", 2)) HTResponse_addEncoding(response, WWW_CODING_GZIP); else HTResponse_setFormat(response, WWW_BINARY); } /* ** If we couldn't find any magic tokens then we try and look at the suffix ** of the URL file name and use our own bindings to see if that gives any ** results. */ if (HTResponse_format(response) == WWW_UNKNOWN) { HTParentAnchor * anchor = HTRequest_anchor(me->request); char * addr = HTAnchor_physical(anchor); HTTRACE(STREAM_TRACE, "GUESSING.... Hmm - trying local bindings\n"); HTBind_getResponseBindings (response, addr); } /* ** If nothing worked then give up and say binary... */ if (HTResponse_format(response) == WWW_UNKNOWN) { HTTRACE(STREAM_TRACE, "GUESSING.... That's it - I'm giving up!\n"); HTResponse_setFormat(response, WWW_BINARY); } HTTRACE(STREAM_TRACE, "Guessed..... Content-Type `%s\'\n" _ HTAtom_name(HTResponse_format(response))); /* ** Set up the new stream stack with the type we figured out */ if ((me->target = HTStreamStack(HTResponse_format(response), me->output_format, me->output_stream, me->request, NO)) == NULL) { HTTRACE(STREAM_TRACE, "HTGuess..... Can't convert media type\n"); me->target = HTErrorStream(); } me->transparent = YES; return PUT_BLOCK(me->buffer, me->cnt); } return HT_OK; }