EG Information

Main Index
EG Manual
Disclaimer
Legal Information
Hall of Fame
Hall of Shame
Member Rankings
Members List
Meet the Staff
Hacker's Home Page

Training Missions

Read Me First
Basic Skills
Realistic Scenarios
Cryptography
Software Cracking
Linux ELF Binary Cracking
Logical Thinking
Programming
Captcha Cracking
Patching
Steganography
Deface This Wall
/dev/null
/dev/urandom
/dev/extra

Knowledge Bank

Discussion Forums
Exploit Database New
PasteBin New
RSS Feeds RSS
Articles / Tutorials
Videos
Online EG MP3 Player Radio
Downloads
Tools

Code Resources

Submit Code
Ajax
ASM
Bash
C
CPP
Csharp
Delphi
Haskell
Java
Javascript
Jython
Lisp
mIRC
MySQL
Perl
PHP
Python
QBASIC
VisualBasic

Pimp Us Out!

Review enigmagroup.org on alexa.com

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.

Enigma Group

Enigma Group

Enigma Group

Enigma Group

 

Affiliates

hackhound.org

suck-o.com

hack.org.za

flyninja.net

 

Enigma Group's Code Bank


C String Algorithms

By: Ultraminor  -  Date Submitted: 2010-10-11 19:12:43

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. unsigned int StringLength(char *string) //Returns length of string, not including null-terminator
  5. {
  6. char *stringPosition=string;
  7. while(*(string++));
  8. return string-stringPosition-1;
  9. }
  10.  
  11. int StringContains(char *source, char *search) //Returns 1 if search exists within source, 0 otherwise
  12. {
  13. unsigned int charsFound=0;
  14. while(*source)
  15. {
  16. if(*source==search[charsFound])
  17. {
  18. charsFound++;
  19. if(!search[charsFound])
  20. {
  21. return 1;
  22. }
  23. }
  24. else
  25. {
  26. charsFound=0;
  27. }
  28. source++;
  29. }
  30. return 0;
  31. }
  32.  
  33. int StringBeginsWith(char *source, char *search) //Returns 1 if search is the beginning of source, 0 otherwise
  34. {
  35. while(*source&&*search)
  36. {
  37. if(*source!=*search)
  38. {
  39. return 0;
  40. }
  41. source++;
  42. search++;
  43. }
  44. if(!(*source)&&*search)
  45. {
  46. return 0;
  47. }
  48. return 1;
  49. }
  50.  
  51. int StringEndsWith(char *source, char *search) //Returns 1 if search is the end of source, 0 otherwise
  52. {
  53. unsigned int charsFound=0;
  54. while(*source)
  55. {
  56. if(*source==search[charsFound])
  57. {
  58. charsFound++;
  59. }
  60. else
  61. {
  62. charsFound=0;
  63. }
  64. source++;
  65. }
  66. if(search[charsFound])
  67. {
  68. return 0;
  69. }
  70. return 1;
  71. }
  72.  
  73. int IndexOfString(char *source, char *search) //Returns the index of the first occurence of search within source, -1 if not found
  74. {
  75. unsigned int charsFound=0, i=0;
  76. while(*source)
  77. {
  78. if(*source==search[charsFound])
  79. {
  80. charsFound++;
  81. if(!search[charsFound])
  82. {
  83. return i-charsFound+1;
  84. }
  85. }
  86. else
  87. {
  88. charsFound=0;
  89. }
  90. i++;
  91. source++;
  92. }
  93. return -1;
  94. }
  95.  
  96. unsigned int CopyString(char *dest, char *source, unsigned int bufferSize) //Copies the string from source to dest, to a maximum of bufferSize characters
  97. //(including null-byte). Safe function; ALWAYS null-terminates.
  98. {
  99. unsigned int bytesCopied=0;
  100. bufferSize--;
  101. while(*source&&bytesCopied<bufferSize)
  102. {
  103. dest[bytesCopied]=*source;
  104. bytesCopied++;
  105. source++;
  106. }
  107. dest[bytesCopied++]=0;
  108. return bytesCopied;
  109. }
  110.  
  111. unsigned int ConcatenateStrings(char *dest, char *source, unsigned int bufferSize) //Concatenates source to dest; length of end result will never exceed bufferSize
  112. //(including null-byte). Safe function; ALWAYS null-terminates.
  113. {
  114. unsigned int i=0;
  115. while(dest[i]) { i++; }
  116. bufferSize--;
  117. while(*source&&i<bufferSize)
  118. {
  119. dest[i]=*source;
  120. i++;
  121. source++;
  122. }
  123. dest[i]=0;
  124. return i;
  125. }
  126.  
  127. void ReverseString(char *string) //Reverses the given string 'in-place'
  128. {
  129. char charBuffer;
  130. unsigned int length=StringLength(string), i, bound=length/2, swap;
  131. for(i=0;i<bound;i++)
  132. {
  133. swap=length-i-1;
  134. charBuffer=string[i];
  135. string[i]=string[swap];
  136. string[swap]=charBuffer;
  137. }
  138. }
  139.  
  140. void Substring(char *string, unsigned int startIndex, unsigned int length, char *outputString) //Copies the substring (starting at startIndex, continuing for length)
  141. //from string to output string. This function does not check for overflows.
  142. {
  143. unsigned int i;
  144. for(i=0;i<length;i++)
  145. {
  146. if(!string[i+startIndex]) { break; }
  147. *outputString=string[i+startIndex];
  148. outputString++;
  149. }
  150. *outputString=0;
  151. }
  152.  
  153. void PutString(char *string) //Puts a given string to stdin
  154. {
  155. while(*string)
  156. {
  157. putchar(*(string++));
  158. }
  159. }
  160.  
  161. void PutStringFile(char *string, FILE *output) //Puts a given string to a file handle
  162. {
  163. while(*string)
  164. {
  165. fputc(*(string++),output);
  166. }
  167. }
  168.  
  169. void SetMemory(unsigned char *memory, unsigned char value, unsigned int length) //Sets a given block of memory to a given unsigned char value
  170. {
  171. while(length--)
  172. {
  173. *(memory++)=value;
  174. }
  175. }
  176.  
  177. void CopyMemory(unsigned char *source, unsigned char *dest, unsigned int length) //Copies memory from source to dest (not overlap-safe)
  178. {
  179. while(length--)
  180. {
  181. *(dest++)=*(source++);
  182. }
  183. }
  184.  
  185. int CopyMemoryBuffered(unsigned char *source, unsigned char *dest, unsigned int length) //Same as above, except works correctly when source and dest overlap
  186. //Returns 0 on failure, 1 on success
  187. {
  188. unsigned char *buf=malloc(length);
  189. if(!buf) { return 0; }
  190. unsigned int i=0;
  191. while(i<length)
  192. {
  193. buf[i]=*source;
  194. source++;
  195. i++;
  196. }
  197. while(length--)
  198. {
  199. *(dest++)=*(buf++);
  200. }
  201. return 1;
  202. }
  203.  
  204. int CompareMemory(unsigned char *source, unsigned char *compare, unsigned int length) //Returns 1 if memory blocks are equal, 0 otherwise
  205. {
  206. while(length--)
  207. {
  208. if(*(source++)!=*(compare++))
  209. {
  210. return 0;
  211. }
  212. }
  213. return 1;
  214. }
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
 
Enigma Group