From 2793582a69fea7e5c3945abc570ad75d691630eb Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Wed, 19 Dec 2012 17:37:24 +0000 Subject: [PATCH] HTMLv: safe string copy git-svn-id: svn://kolibrios.org@3128 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/browser/TWB.h | 8 +- programs/cmm/browser/include/history.h | 8 +- programs/cmm/browser/include/parce_tag.h | 18 +- programs/cmm/lib/kolibri.h | 6 + programs/cmm/lib/lib.obj/iconv.h | 17 +- programs/cmm/lib/mem.h | 14 +- programs/cmm/lib/strings.h | 252 ++++++++++++++--------- 7 files changed, 180 insertions(+), 143 deletions(-) diff --git a/programs/cmm/browser/TWB.h b/programs/cmm/browser/TWB.h index 2e220496da..d2a0d698a7 100644 --- a/programs/cmm/browser/TWB.h +++ b/programs/cmm/browser/TWB.h @@ -24,10 +24,10 @@ struct TWebBrowser { void DrawScroller(); }; -TWebBrowser WB1; - byte rez, b_text, i_text, u_text, s_text, pre_text, blq_text, li_text, - link, ignor_text, li_tab, first_line_drawed, cur_encoding; +link, ignor_text, li_tab, first_line_drawed, cur_encoding; + +TWebBrowser WB1; dword text_colors[300], @@ -310,7 +310,7 @@ void TWebBrowser::ShowPage() edit1.size = edit1.pos = strlen(#editURL); edit1.offset=0; edit_box_draw stdcall(#edit1); //рисуем строку адреса - + if (!filesize) { DrawBar(left, top, width+4, height, 0xFFFFFF); //закрашиваем всё донизу diff --git a/programs/cmm/browser/include/history.h b/programs/cmm/browser/include/history.h index 5f77d41f21..e8620ccaf2 100644 --- a/programs/cmm/browser/include/history.h +++ b/programs/cmm/browser/include/history.h @@ -31,11 +31,11 @@ void UrlsHistory::AddUrl() // history_current/=2; for (i=0; isizeof(options)) - strcpy(#options, #tagparam + sizeof(options)); - else - strcpy(#options, #tagparam + i); + strlcpy(#options, #tagparam + i, sizeof(options)); } else { WHILE((i > 0) && (tagparam[i] <>'=')) i--; //i=strrchr(#tagparam, '=')+1; i++; - if (i>sizeof(options)) - strcpy(#options, #tagparam + sizeof(options)); - else - strcpy(#options, #tagparam + i); + strlcpy(#options, #tagparam + i, sizeof(options)); WHILE (options[0] == ' ') strcpy(#options, #options+1); } @@ -38,13 +32,7 @@ unsigned int GetNextParam() IF (tagparam[i] == '=') //дерзкая заглушка tagparam[i + 1] = 0x00; } - - if (i>sizeof(parametr)) - strcpy(#parametr, #tagparam + sizeof(parametr)); - else - strcpy(#parametr, #tagparam + i + 1); - + strlcpy(#parametr, #tagparam + i + 1, sizeof(parametr)); tagparam[i] = 0x00; - return 1; } \ No newline at end of file diff --git a/programs/cmm/lib/kolibri.h b/programs/cmm/lib/kolibri.h index cc899122ef..bbafd797ae 100644 --- a/programs/cmm/lib/kolibri.h +++ b/programs/cmm/lib/kolibri.h @@ -325,6 +325,7 @@ inline fastcall int PlaySpeaker( ESI) inline fastcall void debug( EDX) { + $push eax $push ebx $push ecx $mov eax, 63 @@ -343,13 +344,18 @@ DONE: $int 0x40 $pop ecx $pop ebx + $pop eax } inline fastcall void debugch( ECX) { + $push eax + $push ebx $mov eax,63 $mov ebx,1 $int 0x40 + $pop ebx + $pop eax } //------------------------------------------------------------------------------ diff --git a/programs/cmm/lib/lib.obj/iconv.h b/programs/cmm/lib/lib.obj/iconv.h index 6cc6267b36..109ac50675 100644 --- a/programs/cmm/lib/lib.obj/iconv.h +++ b/programs/cmm/lib/lib.obj/iconv.h @@ -16,14 +16,18 @@ char aIconv[6] = "iconv\0"; dword ChangeCharset(dword from_chs, to_chs, conv_buf) { dword cd, in_len, out_len, new_buf; - debug (from_chs); - debug (to_chs); iconv_open stdcall (from_chs, to_chs); //CP866, CP1251, CP1252, KOI8-RU, UTF-8, ISO8859-5 - if (EAX==-1) { debug("iconv: wrong charset,\nuse only CP866, CP1251, CP1252, KOI8-RU, UTF-8, ISO8859-5"); return 0; } + if (EAX==-1) + { + debug (from_chs); + debug (to_chs); + debug("iconv: wrong charset,\nuse only CP866, CP1251, CP1252, KOI8-RU, UTF-8, ISO8859-5"); + return 0; + } cd = EAX; - in_len = out_len = strlen(conv_buf); + in_len = out_len = strlen(conv_buf)+1; new_buf = mem_Alloc(in_len); iconv stdcall (cd, #conv_buf, #in_len, #new_buf, #out_len); cd = EAX; @@ -36,6 +40,9 @@ dword ChangeCharset(dword from_chs, to_chs, conv_buf) debug("out_len"); debug(itoa(out_len)); new_buf = 0; + return 0; } - return new_buf; + strcpy(conv_buf, new_buf); + free(new_buf); + return conv_buf; } \ No newline at end of file diff --git a/programs/cmm/lib/mem.h b/programs/cmm/lib/mem.h index a5d338e5b1..afae0f1491 100644 --- a/programs/cmm/lib/mem.h +++ b/programs/cmm/lib/mem.h @@ -89,6 +89,8 @@ L2: } } + + #define mem_Alloc malloc #define mem_ReAlloc realloc #define mem_Free free @@ -123,18 +125,6 @@ L2: } } -:void fastcall memcpy( EDI, ESI, ECX) -{ - asm { - MOV EDX, ECX - SHR ECX, 2 - REP MOVSD - MOV ECX, EDX - AND ECX, 3 - REP MOVSB - } -} - :void fastcall memsetd( EDI, ECX, EAX) { asm { diff --git a/programs/cmm/lib/strings.h b/programs/cmm/lib/strings.h index abae7916b5..07939d34f6 100644 --- a/programs/cmm/lib/strings.h +++ b/programs/cmm/lib/strings.h @@ -13,7 +13,7 @@ // strttl( EDX) // strtok( ESI) // strcpyb(dword searchin, copyin, startstr, endstr) --- copy string between strings -//chrnum(dword searchin, char symbol) --- count of symbol in string +// strnumb(dword searchin, startstr, endstr) --- get number between strings //------------------------------------------------------------------------------ inline fastcall signed int strcmp( ESI, EDI) @@ -71,6 +71,21 @@ L2: } +inline fastcall int strlcpy(dword ESI, EDI, EBX) +{ + EDX=0; + do { + DSBYTE[ESI]=DSBYTE[EDI]; + ESI++; + EDI++; + EDX++; + if (EDX==EBX) { DSBYTE[ESI]='\0'; return -1;} + } while(DSBYTE[EDI-1]!='\0'); + return 0; +} + + + inline fastcall strcat( EDI, ESI) { asm { @@ -99,6 +114,119 @@ inline fastcall strcat( EDI, ESI) } } + +inline fastcall signed int strchr( ESI,BL) +{ + int jj=0; + do{ + jj++; + $lodsb + IF(AL==BL) return jj; + } while(AL!=0); + return 0; +} + + +inline fastcall signed int strrchr( ESI,BL) +{ + int jj=0, last=0; + do{ + jj++; + $lodsb + IF(AL==BL) last=jj; + } while(AL!=0); + return last; +} + + +int chrnum(dword searchin, char symbol) +{ + int num = 0; + while(DSBYTE[searchin]) + { + if (DSBYTE[searchin] == symbol) num++; + searchin++; + } + return num; +} + + +inline fastcall dword strstr( EBX, EDX) +{ + asm { + MOV EDI, EDX + XOR ECX, ECX + XOR EAX, EAX + DEC ECX + REPNE SCASB + NOT ECX + DEC ECX + JE LS2 + MOV ESI, ECX + XOR ECX, ECX + MOV EDI, EBX + DEC ECX + REPNE SCASB + NOT ECX + SUB ECX, ESI + JBE LS2 + MOV EDI, EBX + LEA EBX, DSDWORD[ ESI-1] +LS1: MOV ESI, EDX + LODSB + REPNE SCASB + JNE LS2 + MOV EAX, ECX + PUSH EDI + MOV ECX, EBX + REPE CMPSB + POP EDI + MOV ECX, EAX + JNE LS1 + LEA EAX, DSDWORD[ EDI-1] + JMP SHORT LS3 +LS2: XOR EAX, EAX +LS3: + } +} + + +dword strstri(dword searchin, usestr_s) +{ + dword usestr_e = usestr_s; + char si, ue; + + while(DSBYTE[searchin]) + { + si = DSBYTE[searchin]; + ue = DSBYTE[usestr_e]; + if (si>='A') && (si<='Z') si +=32; + if (ue>='A') && (ue<='Z') ue +=32; + if (si == ue) usestr_e++; else usestr_e = usestr_s; + searchin++; + if (DSBYTE[usestr_e]=='\0') return searchin; + } + return 0; +} + + +void strcpyb(dword searchin, copyin, startstr, endstr) +{ + dword startp, endp; + startp = strstr(searchin, startstr) + strlen(startstr); + endp = strstr(startp, endstr); + if (startp==endp) return; + do + { + DSBYTE[copyin] = DSBYTE[startp]; + copyin++; + startp++; + } + while (startp='0') && (DSBYTE[ESI]<='9') + { + $xor ebx, ebx + EBX = DSBYTE[ESI]-'0'; + EAX *= 10; + EAX += EBX; ESI++; - }while(DSBYTE[ESI]>='0'); - IF(DSBYTE[EDI]=='-') -EAX; + } + IF (DSBYTE[EDI]=='-') -EAX; + $pop esi + $pop ebx } -inline fastcall signed int strchr( ESI,BL) -{ - int jj=0; - do{ - jj++; - $lodsb - IF(AL==BL) return jj; - } while(AL!=0); - return 0; -} - - -inline fastcall signed int strrchr( ESI,BL) -{ - int jj=0, last=0; - do{ - jj++; - $lodsb - IF(AL==BL) last=jj; - } while(AL!=0); - return last; -} - inline fastcall strupr( ESI) { @@ -225,78 +336,13 @@ inline fastcall strttl( EDX) }while(AL!=0); } - - -inline fastcall dword strstr( EBX, EDX) +void debugi(dword d_int) { - asm { - MOV EDI, EDX - XOR ECX, ECX - XOR EAX, EAX - DEC ECX - REPNE SCASB - NOT ECX - DEC ECX - JE LS2 - MOV ESI, ECX - XOR ECX, ECX - MOV EDI, EBX - DEC ECX - REPNE SCASB - NOT ECX - SUB ECX, ESI - JBE LS2 - MOV EDI, EBX - LEA EBX, DSDWORD[ ESI-1] -LS1: MOV ESI, EDX - LODSB - REPNE SCASB - JNE LS2 - MOV EAX, ECX - PUSH EDI - MOV ECX, EBX - REPE CMPSB - POP EDI - MOV ECX, EAX - JNE LS1 - LEA EAX, DSDWORD[ EDI-1] - JMP SHORT LS3 -LS2: XOR EAX, EAX -LS3: - } + char tmpch[11]; + strcpy(#tmpch, itoa(d_int)); + debug(#tmpch); } -// -void strcpyb(dword searchin, copyin, startstr, endstr) -{ - dword startp, endp; - startp = strstr(searchin, startstr) + strlen(startstr); - endp = strstr(startp, endstr); - if (startp==endp) return; - do - { - DSBYTE[copyin] = DSBYTE[startp]; - copyin++; - startp++; - } - while (startp