forked from CoolerVoid/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beer.h
854 lines (731 loc) · 16 KB
/
beer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*
last check up
25/01/2012
------------
_____________
( )
(____________ )
| |
| |
_| |_
/ \
/ \
(___________________)
| |
| ...:::Beer::: |
|..::Version:0.05::.|
| |
|___________________|
| |
| |
| |
| |
| |
(___________________)
0101010101010110101010101010101101010101010
is held by Apache license 2.0
-----------------------
Beer simple C header
Authors: Cooler_,m0nad,ryonagana,b4r0n,utroz,mentebinaria
E-mail: c00f3r[at]gmail[dot]com
date: 03/08/2011
thanks:
_mlk_,m0nad,
I4K,sigsegv,b-man,kov,utroz,eremitah,vorazbr,mentebinaria
delfo,c0lt7r,B4r0n,joey,fokerbug,
zepplin,voidpointer,muzgo,memset,b4r0n,novato_br...
Beer is a C header to help you in small problems
Have a lot functions how
math functions(mmc,mdc,bitwise_sqrt,quadratic equation,factorial,test if number is prime..)
parsers,
string functions (split,strrev..),
validate e-mail,
SQL query filter,
Files Jobs how READ,WRITE,COPY,list DIRs,bytes of file...
strings,numbers convert
|-hex2int
|-dec2bin
|-char2hex
|-hex2char
|-string2hex
bitwise macros ,another things
do you want help us ?
send your function...
in the future this header will a dynamic library...
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//time functions...
#include <time.h>
// use to dir functions
#include <dirent.h>
//macro to read stdin string
#define ReadString(a) fgets(a,sizeof(a),stdin),a[strlen(a)-1] = '\0';
//return elements of array
#define array_elements(array) (sizeof(array) / sizeof *(array))
//hide and unhide strings
#define UNHIDE_STR(str) do { char *p = str; while (*p) *p++ += 0x19; } while (0)
#define HIDE_STR(str) do { char *p = str; while (*p) *p++ -= 0x19; } while (0)
// bitwise macros
#define XorSwap(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
#define BIT_POS(N) ( 1U << (N) )
#define SET_FLAG(N, F) ( (N) |= (F) )
#define CLR_FLAG(N, F) ( (N) &= -(F) )
#define TST_FLAG(N, F) ( (N) & (F) )
#define BIT_RANGE(N, M) ( BIT_POS((M)+1 - (N))-1 << (N) )
#define BIT_SHIFTL(B, N) ( (unsigned)(B) << (N) )
#define BIT_SHIFTR(B, N) ( (unsigned)(B) >> (N) )
#define SET_MFLAG(N, F, V) ( CLR_FLAG(N, F), SET_FLAG(N, V) )
#define CLR_MFLAG(N, F) ( (N) &= ~(F) )
#define GET_MFLAG(N, F) ( (N) & (F) )
#define BUGVIEW 1
#define DEBUG(x, s...) do { \
if (!BUGVIEW) { break; } \
time_t t = time(NULL); \
char *d = ctime(&t); \
fprintf(stderr, "%.*s %s[%d] %s(): ", \
(int)strlen(d) - 1, d, __FILE__, \
__LINE__, __FUNCTION__); \
fprintf(stderr, x, ## s); \
} while (0);
// HEAP alignment :-X
void *xmalloc(unsigned int len)
{
void *ptr;
ptr=malloc(len);
if(ptr==NULL)
DEBUG("fail malloc");
return ptr;
}
// convert decimal to binary
char * dec2bin(int n, char * string)
{
int i;
static int size = 8 * sizeof(int);
for(i = size - 1; i >= 0; i--, n >>= 1)
string[i] = (01 & n) + '0';
string[size] = '\0';
return string;
}
// Hexadecimal to Character
// with bitwise
char Hex2Char(char *Hex)
{
char rch=0;
int i=0;
while(i<2)
{
if ( *(Hex + i) >= '0' && *(Hex + i) <= '9' )
rch = (rch << 4) + (*(Hex + i) - '0');
else if ( *(Hex + i) >= 'A' && *(Hex + i) <= 'F' )
rch = (rch << 4) + (*(Hex + i) - 'A' + 10);
else
break;
i++;
}
return rch;
}
//simple version
static char char2Hex(unsigned char c)
{
static const char *HexCharacters = "0123456789ABCDEF";
if(c > 15)
{
DEBUG("error in char2Hex() %c",HexCharacters[c]);
return -1;
}
return HexCharacters[c];
}
// unsigned char x[]={"google.com"}; string2hex(y,x,16)...
static void string2hex(char *Output, unsigned char *Input, int Length)
{
char *Out;
char First;
char Second;
Out = Output;
while (Length > 0)
{
First = *Input / 0x10;
Second = *Input % 0x10;
*Out = char2Hex(First);
Out++;
*Out = char2Hex(Second);
Out++;
Length--;
Input++;
}
*Out = '\0';
return;
}
unsigned long hex2int(char *a, unsigned int len)
{
int i=0;
unsigned long val = 0;
while(i<len)
{
if(a[i] <= 57)
val += (a[i]-48)*(1<<(4*(len-1-i)));
else
val += (a[i]-55)*(1<<(4*(len-1-i)));
i++;
}
return val;
}
// Math functions
/*
MDC Máximo Divisor Comum,dois números inteiros é o maior número inteiro que divide
ambos sem deixar resto.
*/
int mdc(int a,int b)
{
if(!b)
return a;
else
return mdc(b,a%b);
}
/*
MMC = mínimo múltiplo comum entre dois números é representado
pelo menor valor comum pertencente aos múltiplos dos números.
*/
int mmc(int a,int b)
{
int formula;
if(!b)
return a;
else
formula = (a*b)/(mdc(a,b));
return (formula);
}
// quadratic equation
char * baskara(float a, float b, float c, float *raizes)
{
float delta=((b*b)-4*(a*c));
float x1=0,x2=0;
char *reply;
if((!a)||(delta<0))
{
DEBUG("error in equation baskara(), delta: %f ",delta);
return 0;
}
if(delta>0)
x1=((-b + ((float)bit_sqrt(delta)) )/(2*a));
x2=((-b - ((float)bit_sqrt(delta)) )/(2*a));
snprintf(reply,sizeof(reply)," X1 : %5.2f \n X2 : %5.2f\n",x1,x2);
return reply;
}
// simple factorial
int factorial( int n )
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}
// test if var is prime
int isprime(int n)
{
int d=2;
if(n<=0)
{
DEBUG("error isprime(), n = %d ",n);
return -1;
}
while(d<n)
{
if(!(n%d))
return 0;
d+=2;
}
return 1;
}
//square root by Cooler_
int bit_sqrt(int num)
{
//so 32 is sizeof(int)<<3
int num0=num,result=0,tbit=1<<((sizeof(int)<<3)-2);
if(num<=0)
{
DEBUG("error bit_sqrt(), num = %d ",num);
return -1;
}
while(tbit>num0)
tbit>>=2;
while(tbit^0)
{
if(num0>=result+tbit)
{
num0-=result+tbit;
result=(result>>1)+tbit;
}else
result>>=1;
tbit>>=2;
}
return result;
}
// test if is palindrome
int palindrome(const char *s)
{
int x,y;
y = strlen(s);
for(x=0; x<y/2; x++)
{
if( s[x] != s[y-x-1] )
return 0;
}
return 1;
}
// return time
// example OutPut Wed Aug 3 18:26:24 2011
char *TimeNow()
{
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
local = gmtime(&t);
return asctime(local);
}
// Files jobs ##############################################################
int CopyFile(char *fromfile, char *tofile)
{
FILE *ifp, *ofp;
int c;
if((ifp = fopen(fromfile, "r")) == NULL)
return -1;
if((ofp = fopen(tofile, "w")) == NULL)
{
fclose(ifp);
DEBUG("Error CopyFile()");
return -1;
}
while((c = getc(ifp)) != EOF)
putc(c, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}
int WriteFile(char *file,char *str)
{
FILE *arq;
arq=fopen(file,"a");
if(!arq)
{
DEBUG("error in WriteFile() %s",file);
return 0;
}
fprintf(arq,"%s\n",str);
fclose(arq);
return 1;
}
// return lines from file, example: const char *buff=readLine("log.txt"),printf("%s",buff);
const char *readLine(char * NameFile)
{
FILE * file;
file = fopen(NameFile, "r");
if(!file)
{
DEBUG("error in file");
return;
}
char *lineBuffer=calloc(1,1), line[128];
if(!lineBuffer)
{
DEBUG("error in readLine() at %s",NameFile);
return;
}
while(fgets(line,sizeof line,file))
{
lineBuffer=realloc(lineBuffer,strlen(lineBuffer)+strlen(line)+1);
if(!lineBuffer)
{
DEBUG("error in readLine() at %s",NameFile);
return;
}
strcat(lineBuffer,line);
}
return lineBuffer;
}
// ListDir(directory_name_2_list,Max_chars)
char *ListDir(char *file2list,int MAX)
{
DIR *d;
struct dirent *dir;
char *ret=xmalloc(sizeof(char)*MAX);
d = opendir(file2list);
if(!d)
{
DEBUG("error in directory");
return NULL;
}
while((dir = readdir(d)) != NULL)
{
strncat(ret,dir->d_name,strlen(dir->d_name));
strncat(ret,"\n",1);
}
free(ret);
closedir(d);
return ret;
}
// return size of bytes on file , same to unix cmd "du -b file"
long FileSize(const char *file)
{
long ret;
FILE *fh = fopen(file, "r");
if(!fh)
{
DEBUG("error in file");
return 0;
}
fseek(fh, 0, SEEK_END);
ret = ftell(fh);
fclose(fh);
return ret;
}
/* #################################################################################
to use with Sort functions
example:
char *list[] = { "madruga", "chaves", "kiko", "chiquinha", "inhonho" };
qsort((void *)list, array_elements(list), sizeof(list[0]), sort_char);
*/
int sort_char( const void *a, const void *b)
{
return( strcmp(a,b) );
}
int sort_int(const void *a, const void *b)
{
long *a1, *b1;
a1 = (long *)a; b1 = (long *)b;
return (*a1 > *b1);
}
//example mergesort(a, 0, sizeof(a)/sizeof(i) - 1);
void mergesort(int *array, size_t first, size_t last)
{
int middle;
if(first>=last)
return;
middle = (first + last) / 2;
mergesort(array, first, middle);
mergesort(array, middle + 1, last);
int *temp;
size_t i = first,j = middle + 1,tp = 0;
temp = (int *) xmalloc(sizeof(int) * (last - first + 1));
while(i <= middle && j <= last)
{
if(array[i] <= array[j])
{
temp[tp] = array[i];
++i;
}
else
{
temp[tp] = array[j];
++j;
}
++tp;
}
while(j<=last)
{
temp[tp] = array[j];
++tp;
j++;
}
while(i<=middle)
{
temp[tp] = array[i];
++tp;
i++;
}
i=first;
while(i<=last)
{
array[i] = temp[i - first];
i++;
}
free(temp);
}
void bubbleSort(void *p, int width, int N, int(*fptr)(const void *, const void *))
{
int i, j, k;
unsigned char buf[256];
unsigned char *bp = p;
for (i = N-1; i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
k = fptr((void *)(bp + width*(j-1)), (void *)(bp + j*width));
if(k > 0)
{
memcpy(buf, bp + width*(j-1), width);
memcpy(bp + width*(j-1), bp + j*width , width);
memcpy(bp + j*width, buf, width);
}
}
}
}
// ########################### other functions
char *RandomIp(void)
{
char *ipRand=NULL;
int r1,r2,r3,r4;
r1 = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
r2 = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
r3 = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
r4 = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
ipRand=xmalloc(12*sizeof(char *));
sprintf(ipRand,"%d.%d.%d.%d",r1,r2,r3,r4);
return ipRand;
}
//test if e-mails is valid
int email_isvalid(const char *address)
{
int count = 0;
const char *c, *domain;
static char *rfc822_specials = "()<>@,;:\\\"[]";
/* first we validate the name portion (name@domain) */
for (c = address; *c; c++)
{
if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"'))
{
while (*++c)
{
if (*c == '\"')
break;
if (*c == '\\' && (*++c == ' '))
continue;
if (*c <= ' ' || *c >= 127)
return 0;
}
if(!*c++)
return 0;
if(*c == '@')
break;
if(*c != '.')
return 0;
continue;
}
if(*c == '@')
break;
if(*c <= ' ' || *c >= 127)
return 0;
if(strchr(rfc822_specials, *c))
return 0;
}
if(c == address || *(c - 1) == '.')
return 0;
/* next we validate the domain portion (name@domain) */
if(!*(domain = ++c))
return 0;
do {
if (*c == '.')
{
if (c == domain || *(c - 1) == '.') return 0;
count++;
}
if (*c <= ' ' || *c >= 127) return 0;
if (strchr(rfc822_specials, *c)) return 0;
} while (*++c);
return (count >= 1);
}
//use input to escape SQL Injection
char *scapeSQL(const char *input, char quote, int wildcards) {
char *out, *ptr;
const char *c;
/* If every character in the input needs to be escaped, the resulting string
* would at most double in size. Also, include room for the surrounding
* quotes.
*/
if (quote != '\'' && quote != '\"') return 0;
if (!(out = ptr = (char *)sbrk(strlen(input) * 2 + 2 + 1))) return 0;
*ptr++ = quote;
for (c = input; *c; c++) {
switch (*c) {
case '\'': case '\"':
if (quote == *c) *ptr++ = *c;
*ptr++ = *c;
break;
case '%': case '_': case '[': case ']':
if (wildcards) *ptr++ = '\\';
*ptr++ = *c;
break;
case '\\': *ptr++ = '\\'; *ptr++ = '\\'; break;
case '\b': *ptr++ = '\\'; *ptr++ = 'b'; break;
case '\n': *ptr++ = '\\'; *ptr++ = 'n'; break;
case '\r': *ptr++ = '\\'; *ptr++ = 'r'; break;
case '\t': *ptr++ = '\\'; *ptr++ = 't'; break;
default:
*ptr++ = *c;
break;
}
}
*ptr++ = quote;
*ptr = 0;
return out;
}
// func by m0nad
void urlobfuscator (char * url, char * obf)
{
int i;
for (i = 0; i < (int)strlen (url); i++)
snprintf (obf+strlen(obf), sizeof obf, "%02X", url [i]);
// obf [strlen (obf)] = 0;
obf[i*2] = 0;
}
//base 64 encode
static inline char* b64_encode(char* str, unsigned long len)
{
const char b64[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
char *ret, *chunk;
chunk = malloc(((len + 3) << 2) / 4);
ret=chunk;
do {
if (len >= 3)
{
unsigned long bitwise = (str[0] << 16) | (str[1] << 8) | str[2];
*(chunk++) = b64[bitwise >> 18];
*(chunk++) = b64[(bitwise >> 12) & 0x3F];
*(chunk++) = b64[(bitwise >> 6) & 0x3F];
*(chunk++) = b64[bitwise & 0x3F];
len -= 3;
str += 3;
} else if (len == 2) {
unsigned long bitwise = (str[0] << 16) | (str[1] << 8);
*(chunk++) = b64[bitwise >> 18];
*(chunk++) = b64[(bitwise >> 12) & 0x3F];
*(chunk++) = b64[(bitwise >> 6) & 0x3D];
*(chunk++) = '=';
len -= 2;
str += 2;
} else {
unsigned long bitwise = (str[0] << 16);;
*(chunk++) = b64[bitwise >> 18];
*(chunk++) = b64[(bitwise >> 12) & 0x3F];
*(chunk++) = '=';
*(chunk++) = '=';
len--;
str++;
}
} while(len);
*chunk=0;
return ret;
}
//func by ryonagana
void changeCharacter(char *dest, const char* str, const char search, const char replace)
{
while(*dest = *str)
{
if(*dest == search)
*dest = replace;
*dest++;
*str++;
}
}
// return reverse string
char *strrev(char *str)
{
char *p1, *p2;
if(! str || ! *str)
return str;
for(p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
char *StrChunk(char *src, const int start, const int count)
{
char *tmp,*tmp2;
tmp = (char *)xmalloc(count+1);
if (tmp == NULL)
{
DEBUG("error");
return NULL;
}
strncpy(tmp, src+start, count);
tmp[count] = '\0';
strncpy(tmp2,tmp,count);
free(tmp);
return tmp;
}
// simple split return array of strings between string separator
char **split(char *src, const char *token, int *total)
{
char **str;
register int i, j, count, item, start;
int len;
if(!src || !token)
{
*total = 0;
return NULL;
}
count = item = start = 0;
j = 1;
len = strlen(src);
for(i = 0; i < len; i++)
{
if(src[i] == *token)
count++;
}
if(!count)
{
*total = 0;
return NULL;
}
str = (char **)xmalloc(count * sizeof(char *));
if(str == NULL)
DEBUG("error");
for(i = 0; i < len; i++)
{
if(src[i] != *token)
j++;
else {
str[item] = (char *)xmalloc(j-start);
if (str[item] == NULL)
{
DEBUG("error");
return NULL;
}
*(str+item) = StrChunk(src, start, j-1);
str[item][j-start-1] = '\0';
item++;
start = j++;
}
}
*(str+count) = (char *)xmalloc(j);
if(str[count] == NULL)
DEBUG("error");
str[count] = StrChunk(src, start, j);
str[count][j-start] = '\0';
*total = ++count;
free(str);
return str;
}
// return string of bin file, coded by Mente Binária "http://www.mentebinaria.com.br/"
void strings(FILE *fp)
{
unsigned char byte;
int aux=0;
int ofs=0;
while (fread(&byte, sizeof(unsigned char), 1, fp))
{
if (aux==1)
{
putchar('\n');
printf("%#x ", ofs);
}
else if (!ofs && isprint(byte))
printf("0x0 ");
if (isprint(byte))
{
putchar(byte);
aux=0;
}
else aux++;
ofs++;
}
putchar('\n');
}