• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 16:07
CET 22:07
KST 06:07
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Revival - 2025 Season Finals Preview7RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3
StarCraft 2
General
RSL Revival - 2025 Season Finals Preview Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced
Tourneys
RSL Offline Finals Info - Dec 13 and 14! Tenacious Turtle Tussle 2025 RSL Offline Finals Dates + Ticket Sales! Sparkling Tuna Cup - Weekly Open Tournament StarCraft2.fi 15th Anniversary Cup
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress
Brood War
General
How Rain Became ProGamer in Just 3 Months BGH Auto Balance -> http://bghmmr.eu/ [BSL21] RO8 Bracket & Prediction Contest BW General Discussion FlaSh on: Biggest Problem With SnOw's Playstyle
Tourneys
[ASL20] Grand Finals [BSL21] RO8 - Day 2 - Sunday 21:00 CET [BSL21] RO8 - Day 1 - Saturday 21:00 CET Small VOD Thread 2.0
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Fighting Spirit mining rates Current Meta
Other Games
General Games
Dawn of War IV Path of Exile Stormgate/Frost Giant Megathread Awesome Games Done Quick 2026! Nintendo Switch Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread US Politics Mega-thread YouTube Thread European Politico-economics QA Mega-thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream?
Blogs
How Sleep Deprivation Affect…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1471 users

C++ project help

Blogs > atmablade
Post a Reply
atmablade
Profile Blog Joined July 2007
United States334 Posts
Last Edited: 2008-05-13 19:37:18
May 13 2008 19:25 GMT
#1
Ok, here's the dire situation. For my project I have to make a very basic virus scanner that takes in two arguments, the directory to start searching files in (then recursively traverse down) and the signature file (just a normal text file formatted however I want it). We can write it in any code (C,C++,Java,Perl) except shell since that would take like 4 lines of code. So I decided to start it in C++ since I'm more familiar with that.

For each file, the program will test if it contains a binary substring than matches a signature listed in the input file.
Then the output of the program is a report that indicates which files in the directory contain which signatures in the list.

The signature file I have setup right now is very basic and looks like this:

abc
1a2a3a

Now onto my plead. From what I get, I have to compare both files on a byte to byte level, but how does this work? I'm not sure what the code/pseudo-code looks like for this whole compare part.

The next problem is the recursion. I have never messed with directory traversing much so the code for that is also pretty murky.

Can anyone point me in the right direction and help me out?



zdd
Profile Blog Joined October 2004
1463 Posts
Last Edited: 2008-05-13 21:04:25
May 13 2008 19:55 GMT
#2
you basically just want a function that you can put a directory into and loop through each file
convert your search pattern to binary and then to a string for easy comparison (or you can just do binary to binary)

bin_file = to_string(convert_to_binary(input_file))
matches = empty string

function loop_directory(dir) {
      for each file in dir {
            if (file = directory) { loop_directory(file) } //recurse through any subdirectories
             else {
                    file_to_search = open(file)
                    for each character in file_to_search {
                    loop_this_many_times = length(bin_file)-1 //loop through character spot + length of bin_file - 1
                    while loop_this_many_times>0 {
                    active_bin_string[loop_this_many_times] = file_to_search[character+loop_this_many_times]
                    loop_this_many_times--
                    }
                     active_bin_string = reverse(active_bin_string) //reverse it because you searched backwards
                    if bin_file = to_string(active_bin_string) {
                    matches += to_string(file)
                     }
             }
      }
}
print matches
All you need in life is a strong will to succeed and unrelenting determination. If you meet these prerequisites, you can become anything you want with absolutely no luck, fortune or natural ability.
FreeZEternal
Profile Joined January 2003
Korea (South)3396 Posts
Last Edited: 2008-05-13 20:02:50
May 13 2008 20:02 GMT
#3
is it just me or suddenly we have lots of programming questions. I would have done this in Java or Ruby.
MasterOfChaos
Profile Blog Joined April 2007
Germany2896 Posts
Last Edited: 2008-05-13 20:38:08
May 13 2008 20:33 GMT
#4
for matching the signature you can use a simple binary safe StringPos function. If you want to code it yourself you can use sth like the following:
function CheckForSig(File:String;Sig:String);
var i,j:integer;
hit:boolean;
for i:=0 to length(file)-1 do
hit:=true;
for j:=0 to min(length(file)-1-i,length(sig)-1) do
if file[i+j]!=sig[j]then hit:=false;break;end;
end;
if hit then return true; end;
end;
return false;
end;

of cause that is not the fastest possible way of doing it, for a large signature set.

edit: Pos function used by Delphi (asmcode )
+ Show Spoiler +
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The implementation of function Pos is subject to the
* Mozilla Public License Version 1.1 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Fastcode
*
* The Initial Developer of the Original Code is Fastcode
*
* Portions created by the Initial Developer are Copyright (C) 2002-2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Aleksandr Sharahov
*
* ***** END LICENSE BLOCK ***** *)
function Pos(const substr, str: AnsiString): Integer; overload;
asm
push ebx
push esi
add esp, -16
test edx, edx
jz @NotFound
test eax, eax
jz @NotFound
mov esi, [edx-4] //Length(Str)
mov ebx, [eax-4] //Length(Substr)
cmp esi, ebx
jl @NotFound
test ebx, ebx
jle @NotFound
dec ebx
add esi, edx
add edx, ebx
mov [esp+8], esi
add eax, ebx
mov [esp+4], edx
neg ebx
movzx ecx, byte ptr [eax]
mov [esp], ebx
jnz @FindString

sub esi, 2
mov [esp+12], esi

@FindChar2:
cmp cl, [edx]
jz @Matched0ch
cmp cl, [edx+1]
jz @Matched1ch
add edx, 2
cmp edx, [esp+12]
jb @FindChar4
cmp edx, [esp+8]
jb @FindChar2
@NotFound:
xor eax, eax
jmp @Exit0ch

@FindChar4:
cmp cl, [edx]
jz @Matched0ch
cmp cl, [edx+1]
jz @Matched1ch
cmp cl, [edx+2]
jz @Matched2ch
cmp cl, [edx+3]
jz @Matched3ch
add edx, 4
cmp edx, [esp+12]
jb @FindChar4
cmp edx, [esp+8]
jb @FindChar2
xor eax, eax
jmp @Exit0ch

@Matched2ch:
add edx, 2
@Matched0ch:
inc edx
mov eax, edx
sub eax, [esp+4]
@Exit0ch:
add esp, 16
pop esi
pop ebx
ret

@Matched3ch:
add edx, 2
@Matched1ch:
add edx, 2
xor eax, eax
cmp edx, [esp+8]
ja @Exit1ch
mov eax, edx
sub eax, [esp+4]
@Exit1ch:
add esp, 16
pop esi
pop ebx
ret

@FindString4:
cmp cl, [edx]
jz @Test0
cmp cl, [edx+1]
jz @Test1
cmp cl, [edx+2]
jz @Test2
cmp cl, [edx+3]
jz @Test3
add edx, 4
cmp edx, [esp+12]
jb @FindString4
cmp edx, [esp+8]
jb @FindString2
xor eax, eax
jmp @Exit1

@FindString:
sub esi, 2
mov [esp+12], esi
@FindString2:
cmp cl, [edx]
jz @Test0
@AfterTest0:
cmp cl, [edx+1]
jz @Test1
@AfterTest1:
add edx, 2
cmp edx, [esp+12]
jb @FindString4
cmp edx, [esp+8]
jb @FindString2
xor eax, eax
jmp @Exit1

@Test3:
add edx, 2
@Test1:
mov esi, [esp]
@Loop1:
movzx ebx, word ptr [esi+eax]
cmp bx, word ptr [esi+edx+1]
jnz @AfterTest1
add esi, 2
jl @Loop1
add edx, 2
xor eax, eax
cmp edx, [esp+8]
ja @Exit1
@RetCode1:
mov eax, edx
sub eax, [esp+4]
@Exit1:
add esp, 16
pop esi
pop ebx
ret

@Test2:
add edx,2
@Test0:
mov esi, [esp]
@Loop0:
movzx ebx, word ptr [esi+eax]
cmp bx, word ptr [esi+edx]
jnz @AfterTest0
add esi, 2
jl @Loop0
inc edx
@RetCode0:
mov eax, edx
sub eax, [esp+4]
add esp, 16
pop esi
pop ebx
end;
LiquipediaOne eye to kill. Two eyes to live.
FreeZEternal
Profile Joined January 2003
Korea (South)3396 Posts
Last Edited: 2008-05-13 20:45:03
May 13 2008 20:38 GMT
#5
holy shit lol. Haven't seen asm for a long!!!!! time
LastWish
Profile Blog Joined September 2004
2015 Posts
May 13 2008 21:21 GMT
#6
Use google to find examples on anything something like : "C++ find file".
And combine it together.

You may easily read the file into String (delphi, java or ruby, lil harder in C++) and use some substring or pos or whatever function.
Really easy task.

Delphi code :
use FindFirst and FindNext functions(see help)
use
S : String;
AssignFile(F,FileName);
SetLength(S,FileSize(F));
BlockRead(F, S[1], FileSize(F));
CloseFile(F);

use Pos function to find whether there is an appropriate match
- It's all just treason - They bring me down with their lies - Don't know the reason - My life is fire and ice -
yenta
Profile Blog Joined April 2006
Poland1142 Posts
Last Edited: 2008-05-14 00:02:59
May 13 2008 23:56 GMT
#7
What you're describing can be written very simply in perl.

pseudo code:

@signatures = <SIGNATURE_FILE>
foreach $file in recursive_glob(PARENT_DIRECTORY) {
@contents = <$file>
foreach $chunk in @contents {
if $chunk in @signatures warn "virus"
}
}

just make sure you use binmode
Trutacz Practice Discord - https://discord.gg/PWF7Pv
Please log in or register to reply.
Live Events Refresh
BSL 21
20:00
RO8 - Day 2
Tech vs Cross
Bonyth vs eOnzErG
ZZZero.O287
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 480
IndyStarCraft 172
JuggernautJason106
DisKSc2 32
UpATreeSC 17
StarCraft: Brood War
Britney 15990
Calm 2366
Shuttle 469
Mini 334
ZZZero.O 287
EffOrt 181
Dewaltoss 75
ggaemo 62
Dota 2
Gorgc5417
420jenkins222
LuMiX1
Counter-Strike
fl0m9737
byalli549
Super Smash Bros
hungrybox130
Heroes of the Storm
Khaldor533
Liquid`Hasu484
Other Games
Grubby4342
B2W.Neo544
ArmadaUGS155
Fuzer 81
OptimusSC21
Organizations
Other Games
gamesdonequick1521
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• HeavenSC 44
• sitaska23
• Adnapsc2 20
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• Airneanach27
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV506
Other Games
• imaqtpie2468
• Shiphtur575
Upcoming Events
Replay Cast
11h 53m
Wardi Open
14h 53m
Monday Night Weeklies
19h 53m
Sparkling Tuna Cup
1d 12h
OSC
2 days
YoungYakov vs Mixu
ForJumy vs TBD
Percival vs TBD
Shameless vs TBD
Replay Cast
3 days
The PondCast
3 days
OSC
4 days
CranKy Ducklings
5 days
SC Evo League
5 days
[ Show More ]
BSL 21
5 days
Sparkling Tuna Cup
6 days
BSL 21
6 days
Liquipedia Results

Completed

Acropolis #4 - TS3
RSL Revival: Season 3
Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
Slon Tour Season 2
WardiTV 2025
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.