EG Information
Training Missions
Knowledge Bank
Pimp Us Out!
Has Enigma Group Helped You? Then Help Us By Advertising For Us. Place One Of The Following Images On Your Site And Create A Link Back To Enigma Group.
|
| |
Affiliates
|
|
Enigma Group's Code Bank
C String AlgorithmsBy: Ultraminor - Date Submitted: 2010-10-11 19:12:43 #include <stdio.h> #include <stdlib.h> unsigned int StringLength(char *string) //Returns length of string, not including null-terminator { char *stringPosition=string; while(*(string++)); return string-stringPosition-1; } int StringContains(char *source, char *search) //Returns 1 if search exists within source, 0 otherwise { unsigned int charsFound=0; while(*source) { if(*source==search[charsFound]) { charsFound++; if(!search[charsFound]) { return 1; } } else { charsFound=0; } source++; } return 0; } int StringBeginsWith(char *source, char *search) //Returns 1 if search is the beginning of source, 0 otherwise { while(*source&&*search) { if(*source!=*search) { return 0; } source++; search++; } if(!(*source)&&*search) { return 0; } return 1; } int StringEndsWith(char *source, char *search) //Returns 1 if search is the end of source, 0 otherwise { unsigned int charsFound=0; while(*source) { if(*source==search[charsFound]) { charsFound++; } else { charsFound=0; } source++; } if(search[charsFound]) { return 0; } return 1; } int IndexOfString(char *source, char *search) //Returns the index of the first occurence of search within source, -1 if not found { unsigned int charsFound=0, i=0; while(*source) { if(*source==search[charsFound]) { charsFound++; if(!search[charsFound]) { return i-charsFound+1; } } else { charsFound=0; } i++; source++; } return -1; } unsigned int CopyString(char *dest, char *source, unsigned int bufferSize) //Copies the string from source to dest, to a maximum of bufferSize characters //(including null-byte). Safe function; ALWAYS null-terminates. { unsigned int bytesCopied=0; bufferSize--; while(*source&&bytesCopied<bufferSize) { dest[bytesCopied]=*source; bytesCopied++; source++; } dest[bytesCopied++]=0; return bytesCopied; } unsigned int ConcatenateStrings(char *dest, char *source, unsigned int bufferSize) //Concatenates source to dest; length of end result will never exceed bufferSize //(including null-byte). Safe function; ALWAYS null-terminates. { unsigned int i=0; while(dest[i]) { i++; } bufferSize--; while(*source&&i<bufferSize) { dest[i]=*source; i++; source++; } dest[i]=0; return i; } void ReverseString(char *string) //Reverses the given string 'in-place' { char charBuffer; unsigned int length=StringLength(string), i, bound=length/2, swap; for(i=0;i<bound;i++) { swap=length-i-1; charBuffer=string[i]; string[i]=string[swap]; string[swap]=charBuffer; } } void Substring(char *string, unsigned int startIndex, unsigned int length, char *outputString) //Copies the substring (starting at startIndex, continuing for length) //from string to output string. This function does not check for overflows. { unsigned int i; for(i=0;i<length;i++) { if(!string[i+startIndex]) { break; } *outputString=string[i+startIndex]; outputString++; } *outputString=0; } void PutString(char *string) //Puts a given string to stdin { while(*string) { putchar(*(string++)); } } void PutStringFile(char *string, FILE *output) //Puts a given string to a file handle { while(*string) { fputc(*(string++),output); } } void SetMemory(unsigned char *memory, unsigned char value, unsigned int length) //Sets a given block of memory to a given unsigned char value { while(length--) { *(memory++)=value; } } void CopyMemory(unsigned char *source, unsigned char *dest, unsigned int length) //Copies memory from source to dest (not overlap-safe) { while(length--) { *(dest++)=*(source++); } } int CopyMemoryBuffered(unsigned char *source, unsigned char *dest, unsigned int length) //Same as above, except works correctly when source and dest overlap //Returns 0 on failure, 1 on success { unsigned char *buf=malloc(length); if(!buf) { return 0; } unsigned int i=0; while(i<length) { buf[i]=*source; source++; i++; } while(length--) { *(dest++)=*(buf++); } return 1; } int CompareMemory(unsigned char *source, unsigned char *compare, unsigned int length) //Returns 1 if memory blocks are equal, 0 otherwise { while(length--) { if(*(source++)!=*(compare++)) { return 0; } } return 1; }
|
| Return to
c category list |
|
|
Who Visited EnigmaGroup Today?
1379 Guests, 225 Users (218 Spiders)
TheCheeseDemon, Pabz, plex, rockcraft, recoveryToolbox, Edika, saraf, soufiaane, sickmind, cat1vo, tgm001, mjneat, famous0123, Galagatron, dark_void, CJ_Omaha, junaid_junaid59, JohnJohnJohn, ssmaslov, psychomarine, Dregoon, Patrickk, Aska, Beat_Slayer, M0rdak, Ausome1, Imre, Vreality2007, mmndglxuwn, m0rt, unholyblood, iterrumzz, VurbTrurb, Mayonoula, MAMWOURBROR, mutabor, gobinda, cossyDrybrich, Razin, zaCruBumas8, hunja, johny34, pantoufle, bagy, arctica, hackarchives, UsedDeteKef, Peculator, Fadhilat606, TheTrueMonarch, Pascall01, hackaday, Tjm, arndevil, flairvelocity, lol, alphbond, kdivanov, elizbethallis6, Rik, bn11, BorgBot, SHASHANK101hello, 4poc4lyptic, ksajxai, nbmorri1, electro-technic, شمالي عرعر, lamb, AutobotPrime, Underleaf, The End, tomtombomb, killobyte, snowgirlx, so_saucey, zerolife, Althor, Cramps, Hekser, Hyperborn, cyber-guard, jhgrunn, cobra, Partisan, MAZI_, cyborg, GenbreedX, moel77, cliptoX, pwnpwnlolz, letshavepie, Mrwormz, yshiau, mirmo, roozyoppomo, soft_devil, cls777, scoobywan, Reiversed, joshua, st3alth, Afrika, PaiffDryday, venter, Anthony12796, sh3llcod3, 8FIGURE, Rannim, Evil1, maloaboy, BACanON, SlayingDragons, Repuhlsive, IvanDimitriev, lolzsec, 1RiB, mzungudo, Micro_Geek, iMaxx, aciboummamymn, k0unterkulcher, somebody777, m14m16, GoododotAlcob, negasora, Rastii, UninueMem, Swifsolja, ad.conquest, ngolatkar, Infinity8, Jigoku, thesupervisor, p0is0n5ting, kernel_mod, AKL, GothicLogic, themastersinner, dnatrixene135, ChewBigRed, kalak55, sejem, cve916, pollolololo, triecturn, Violatedsmurf, Ops, jmp, xsiemich, generalisimo, strudels, ga3ttpom, KingOfBritains, epoch_qwert, suten, FriskyKat, Ryuske, Adonis Achilles, ubqbcdzzhf, 3vil, Nightraven, US£RNAM£, Weindittewcon, Batesheelocot, GSmyrlis, MaxMeier, Elite.America, rabbidmind, Psiber_Syn, phoenix22, imittyerrotte, peewster, cyberturtle, ctb, dexgeda, sdw, Pizza, White_widdow, devarian, finesse, Nature112091777, Danc7171, Alphadragon, Estadagause, 53QR10U5, Xargos, Alkomage, hardlock, Barry Gonzoles, MineDweller, Gkjt, N4g4c3N, [I]nfectedbug, wimsteege, aqr5zdcw, xin214, Bugshuppy, SnoopSky, Hessesian, voodooKobra, sKcarr, IROverRated, W1F1G3NJU75U, Baddy, ziadmosaan, gamble86, realzs, CruelDemon, Shinju, aVoid, aquiredanonymity, kukumumu, web_request, callmeneon, KissMyDAFFODIL, Feld Grau, Abhinav2107, prabhataditya, mbuyiselo, shumer, phenom216, princennamdi, huskyboiza, ninety-nine |
| |
|
|
|
|
|