• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 17:05
CEST 23:05
KST 06:05
  • 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
Tournament Spotlight: FEL Cracow 20259Power Rank - Esports World Cup 202577RSL Season 1 - Final Week9[ASL19] Finals Recap: Standing Tall15HomeStory Cup 27 - Info & Preview18
Community News
EWC 2025 - Replay Pack1Google Play ASL (Season 20) Announced25BSL Team Wars - Bonyth, Dewalt, Hawk & Sziky teams10Weekly Cups (July 14-20): Final Check-up0Esports World Cup 2025 - Brackets Revealed19
StarCraft 2
General
EWC 2025 - Replay Pack #1: Maru - Greatest Players of All Time Tournament Spotlight: FEL Cracow 2025 Power Rank - Esports World Cup 2025 I offer completely free coaching services
Tourneys
FEL Cracov 2025 (July 27) - $10,000 live event Esports World Cup 2025 $25,000 Streamerzone StarCraft Pro Series announced $5,000 WardiTV Summer Championship 2025 WardiTV Mondays
Strategy
How did i lose this ZvP, whats the proper response
Custom Maps
External Content
Mutation # 484 Magnetic Pull Mutation #239 Bad Weather Mutation # 483 Kill Bot Wars Mutation # 482 Wheel of Misfortune
Brood War
General
Google Play ASL (Season 20) Announced [Update] ShieldBattery: 2025 Redesign Dewalt's Show Matches in China BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion
Tourneys
[Megathread] Daily Proleagues [BSL20] Non-Korean Championship 4x BSL + 4x China CSL Xiamen International Invitational [CSLPRO] It's CSLAN Season! - Last Chance
Strategy
Simple Questions, Simple Answers [G] Mineral Boosting Does 1 second matter in StarCraft?
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Total Annihilation Server - TAForever [MMORPG] Tree of Savior (Successor of Ragnarok) Path of Exile
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
UK Politics Mega-thread US Politics Mega-thread Stop Killing Games - European Citizens Initiative Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread [\m/] Heavy Metal Thread Movie Discussion! [Manga] One Piece Korean Music Discussion
Sports
2024 - 2025 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
Installation of Windows 10 suck at "just a moment" Computer Build, Upgrade & Buying Resource Thread
TL Community
TeamLiquid Team Shirt On Sale The Automated Ban List
Blogs
Ping To Win? Pings And Their…
TrAiDoS
momentary artworks from des…
tankgirl
from making sc maps to makin…
Husyelt
StarCraft improvement
iopq
Socialism Anyone?
GreenHorizons
Eight Anniversary as a TL…
Mizenhauer
Customize Sidebar...

Website Feedback

Closed Threads



Active: 718 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
2013 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
BSL20 Non-Korean Champi…
18:00
RO8 Round Robin Group - Day 4
Bonyth vs Zhanhun
Dewalt vs Mihu
Hawk vs Sziky
Sziky vs QiaoGege
Mihu vs Hawk
Zhanhun vs Dewalt
Fengzi vs Bonyth
ZZZero.O272
LiquipediaDiscussion
FEL
09:00
Cracow 2025
Reynor vs ClemLIVE!
RotterdaM2387
ComeBackTV 2100
IndyStarCraft 619
WardiTV450
CranKy Ducklings186
3DClanTV 147
EnkiAlexander 117
Rex63
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 2675
IndyStarCraft 604
Rex 63
JuggernautJason30
StarCraft: Brood War
ZZZero.O 276
NaDa 8
Dota 2
capcasts274
LuMiX2
League of Legends
JimRising 359
febbydoto10
Counter-Strike
fl0m2706
Fnx 2348
Stewie2K960
flusha424
Super Smash Bros
hungrybox2575
Mew2King1339
AZ_Axe349
Westballz16
Heroes of the Storm
Liquid`Hasu599
Khaldor347
Other Games
tarik_tv7736
Grubby3018
summit1g2068
mouzStarbuck139
Sick36
Organizations
Other Games
gamesdonequick3146
StarCraft 2
angryscii 16
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• davetesta34
• Adnapsc2 12
• intothetv
• Kozan
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• IndyKCrew
StarCraft: Brood War
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota22512
• WagamamaTV1105
League of Legends
• Doublelift4443
Other Games
• imaqtpie1642
• Shiphtur449
Upcoming Events
Wardi Open
13h 55m
Sparkling Tuna Cup
1d 12h
WardiTV European League
1d 18h
Online Event
1d 20h
uThermal 2v2 Circuit
2 days
The PondCast
3 days
Replay Cast
4 days
Korean StarCraft League
5 days
CranKy Ducklings
5 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

CSLPRO Last Chance 2025
Esports World Cup 2025
Murky Cup #2

Ongoing

Copa Latinoamericana 4
Jiahua Invitational
BSL 20 Non-Korean Championship
BSL 20 Team Wars
FEL Cracov 2025
CC Div. A S7
Underdog Cup #2
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25

Upcoming

ASL Season 20: Qualifier #1
ASL Season 20: Qualifier #2
ASL Season 20
CSLPRO Chat StarLAN 3
BSL Season 21
RSL Revival: Season 2
Maestros of the Game
SEL Season 2 Championship
WardiTV Summer 2025
uThermal 2v2 Main Event
HCC Europe
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
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.