Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

490 satır
12 KiB

  1. #include "fmt.h"
  2. #include "scan.h"
  3. #include "str.h"
  4. #include "uint16.h"
  5. #include "uint32.h"
  6. #include "stralloc.h"
  7. #include "socket.h"
  8. #include "buffer.h"
  9. #include "ip4.h"
  10. #include "ip6.h"
  11. #include "mmap.h"
  12. #include "open.h"
  13. #include "byte.h"
  14. #include "textcode.h"
  15. #include "dns.h"
  16. #include "case.h"
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include "errmsg.h"
  23. #include "iob.h"
  24. #include "safemult.h"
  25. #include "iarray.h"
  26. #include "critbit.h"
  27. #include <assert.h>
  28. #include "compiletimeassert.h"
  29. #include "CAS.h"
  30. #include "io_internal.h"
  31. #define rdtscl(low) \
  32. __asm__ __volatile__ ("lfence\nrdtsc" : "=a" (low) : : "edx")
  33. // #define atomic_add(mem,val) asm volatile ("lock; add%z0 %1, %0": "+m" (mem): "ir" (val))
  34. int64 writecb(int64 fd,const void* buf,uint64 n) {
  35. (void)fd;
  36. (void)buf;
  37. (void)n;
  38. #if 0
  39. int r;
  40. int todo=n>=65536?65536:n;
  41. r=write(fd,buf,todo);
  42. if (r==-1 && r!=EAGAIN) r=-3;
  43. return r;
  44. #endif
  45. return -1;
  46. }
  47. int ret0(const char* s,void* foo) {
  48. (void)foo;
  49. assert(strcmp(s,"fnord")==0);
  50. return 0;
  51. }
  52. int ret1(const char* s,void* foo) {
  53. static int i;
  54. (void)foo;
  55. switch (i) {
  56. case 0: assert(strcmp(s,"fnord")==0); break;
  57. case 1: assert(strcmp(s,"fnord2")==0); break;
  58. default: return -1;
  59. }
  60. ++i;
  61. return 1;
  62. }
  63. int main(int argc,char* argv[]) {
  64. (void)argc;
  65. (void)argv;
  66. compiletimeassert(sizeof(char) < sizeof(int));
  67. #if 0
  68. char buf[1024];
  69. size_t n;
  70. scan_base64("eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9",buf,&n);
  71. write(1,buf,n); write(1,"\n",1);
  72. scan_base64("eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",buf,&n);
  73. write(1,buf,n); write(1,"\n",1);
  74. #endif
  75. #if 0
  76. int s=socket_udp6();
  77. #endif
  78. #if 0
  79. char buf[100];
  80. assert(fmt_varint(buf,1)==1 && buf[0]==1);
  81. fmt_varint(buf,300);
  82. assert(fmt_varint(buf,300)==2 && buf[0]==(char)0xac && buf[1]==0x02);
  83. #endif
  84. #if 0
  85. const char buf[]="fnord\n";
  86. buffer_puts(buffer_1,buf);
  87. strcpy((char*)buf,"foo\n");
  88. buffer_puts(buffer_1,buf);
  89. buffer_puts(buffer_1,"bar\n");
  90. buffer_flush(buffer_1);
  91. #endif
  92. #if 0
  93. static critbit0_tree t;
  94. assert(critbit0_insert(&t,"fnord")==2);
  95. assert(critbit0_insert(&t,"fnord2")==2);
  96. assert(critbit0_insert(&t,"fnord2")==1);
  97. assert(critbit0_contains(&t,"foo")==0);
  98. assert(critbit0_contains(&t,"fnord")==1);
  99. assert(critbit0_allprefixed(&t,"fnord",ret1,NULL)==1);
  100. assert(critbit0_allprefixed(&t,"fnord",ret0,NULL)==0);
  101. assert(critbit0_delete(&t,"fnord2")==1);
  102. assert(critbit0_delete(&t,"foo")==0);
  103. #endif
  104. #if 0
  105. int s = socket_tcp6();
  106. #endif
  107. #if 0
  108. iarray i;
  109. iarray_init(&i,sizeof(size_t));
  110. printf("%p\n",iarray_get(&i,0));
  111. printf("%p\n",iarray_allocate(&i,0));
  112. printf("%p\n",iarray_allocate(&i,0));
  113. printf("%p\n",iarray_get(&i,0));
  114. #endif
  115. #if 1
  116. char buf[1024];
  117. size_t l;
  118. unsigned char c;
  119. (void)writecb;
  120. (void)argc;
  121. (void)argv;
  122. (void)c;
  123. assert(fmt_jsonescape(buf,"foo\nbar\\",8)==10 && byte_equal(buf,10,"foo\\nbar\\\\"));
  124. memset(buf,0,sizeof(buf));
  125. assert(scan_jsonescape("foo\\u000abar\\\\",buf,&l)==14 && l==8 && byte_equal(buf,8,"foo\nbar\\"));
  126. memset(buf,0,sizeof(buf));
  127. /* example from the json spec: G clef U+1D11E encoded using UTF-16 surrogates*/
  128. assert(scan_jsonescape("\\uD834\\uDD1Exyz",buf,&l)==15 && l==7 && byte_equal(buf,7,"\xf0\x9d\x84\x9exyz"));
  129. /*
  130. 1D11E -> 0001 1101 0001 0001 1110
  131. -> ______00 __011101 __000100 __011110
  132. as utf8: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  133. 11110000 10011101 10000100 10011110
  134. f 0 9 d 8 4 9 e
  135. */
  136. #endif
  137. #if 0
  138. static size_t x;
  139. x=23;
  140. atomic_add(&x,3);
  141. printf("%u\n",x);
  142. printf("%u\n",atomic_add_return(&x,-3));
  143. printf("%u\n",compare_and_swap(&x,26,17));
  144. printf("%u\n",compare_and_swap(&x,23,17));
  145. #endif
  146. #if 0
  147. atomic_add(&x,3); printf("%u\n",x);
  148. x=23;
  149. atomic_add(&x,3); assert(x==26);
  150. atomic_or(&x,1); assert(x==27);
  151. atomic_and(&x,-2); assert(x==26);
  152. #endif
  153. #if 0
  154. iarray a;
  155. char* c;
  156. iarray_init(&a,sizeof(io_entry));
  157. printf("15 -> %p\n",c=iarray_allocate(&a,15));
  158. printf("23 -> %p\n",c=iarray_allocate(&a,23));
  159. printf("1234567 -> %p\n",c=iarray_allocate(&a,1234567));
  160. printf("23 -> %p\n",iarray_get(&a,23));
  161. #endif
  162. #if 0
  163. io_batch* b=iob_new(1234);
  164. int64 fd=open("t.c",0);
  165. iob_addbuf(b,"fnord",5);
  166. iob_addfile_close(b,fd,0,7365);
  167. iob_write(1,b,writecb);
  168. #endif
  169. #if 0
  170. char dest[1024];
  171. unsigned long len;
  172. scan_urlencoded2("libstdc++.tar.gz",dest,&len);
  173. buffer_putmflush(buffer_1,dest,"\n");
  174. #endif
  175. #if 0
  176. static stralloc sa;
  177. stralloc_copym(&sa,"foo ","bar ","baz.\n");
  178. write(1,sa.s,sa.len);
  179. #endif
  180. #if 0
  181. buffer_putmflush(buffer_1,"foo ","bar ","baz.\n");
  182. #endif
  183. #if 0
  184. char* c="fnord";
  185. int fd=open_read(c);
  186. errmsg_iam(argv[0]);
  187. carp("could not open file `",c,"'");
  188. diesys(23,"could not open file `",c,"'");
  189. #endif
  190. #if 0
  191. errmsg_warn("could not open file `",c,"'",0);
  192. errmsg_warnsys("could not open file `",c,"'",0);
  193. #endif
  194. #if 0
  195. char buf[100]="/usr/bin/sh";
  196. int len=str_len(buf);
  197. assert(byte_rchr(buf,len,'/')==8);
  198. assert(byte_rchr(buf,len,'@')==len);
  199. assert(byte_rchr(buf,len,'h')==len-1);
  200. printf("%d\n",byte_rchr("x",1,'x'));
  201. #endif
  202. #if 0
  203. char buf[IP6_FMT+100];
  204. int i;
  205. char ip[16];
  206. uint32 scope_id;
  207. char* s="fec0::1:220:e0ff:fe69:ad92%eth0/64";
  208. char blubip[16]="\0\0\0\0\0\0\0\0\0\0\xff\xff\x7f\0\0\001";
  209. i=scan_ip6if(s,ip,&scope_id);
  210. assert(s[i]=='/');
  211. buffer_put(buffer_1,buf,fmt_ip6if(buf,ip,scope_id));
  212. buffer_putnlflush(buffer_1);
  213. buffer_put(buffer_1,buf,fmt_ip6ifc(buf,blubip,scope_id));
  214. buffer_putnlflush(buffer_1);
  215. scan_ip6("2001:7d0:0:f015:0:0:0:1",ip);
  216. buffer_put(buffer_1,buf,fmt_ip6(buf,ip));
  217. buffer_putnlflush(buffer_1);
  218. #endif
  219. #if 0
  220. char buf[100];
  221. int i;
  222. printf("%d\n",i=fmt_pad(buf,"fnord",5,7,10));
  223. buf[i]=0;
  224. puts(buf);
  225. #endif
  226. #if 0
  227. char ip[16];
  228. char buf[32];
  229. printf("%d (expect 2)\n",scan_ip6("::",ip));
  230. printf("%d (expect 3)\n",scan_ip6("::1",ip));
  231. printf("%d (expect 16)\n",scan_ip6("fec0:0:0:ffff::1/0",ip));
  232. printf("%.*s\n",fmt_ip6(buf,ip),buf);
  233. #endif
  234. #if 0
  235. static stralloc s,t;
  236. stralloc_copys(&s,"fnord");
  237. stralloc_copys(&t,"abc"); printf("%d\n",stralloc_diff(&s,&t));
  238. stralloc_copys(&t,"fnor"); printf("%d\n",stralloc_diff(&s,&t));
  239. stralloc_copys(&t,"fnord"); printf("%d\n",stralloc_diff(&s,&t));
  240. stralloc_copys(&t,"fnordh"); printf("%d\n",stralloc_diff(&s,&t));
  241. stralloc_copys(&t,"hausen"); printf("%d\n",stralloc_diff(&s,&t));
  242. #endif
  243. #if 0
  244. static stralloc s;
  245. stralloc_copys(&s,"fnord");
  246. printf("%d\n",stralloc_diffs(&s,"abc"));
  247. printf("%d\n",stralloc_diffs(&s,"fnor"));
  248. printf("%d\n",stralloc_diffs(&s,"fnord"));
  249. printf("%d\n",stralloc_diffs(&s,"fnordh"));
  250. printf("%d\n",stralloc_diffs(&s,"hausen"));
  251. #endif
  252. #if 0
  253. printf("%d\n",case_starts("fnordhausen","FnOrD"));
  254. printf("%d\n",case_starts("fnordhausen","blah"));
  255. #endif
  256. #if 0
  257. char buf[]="FnOrD";
  258. case_lowers(buf);
  259. puts(buf);
  260. #endif
  261. #if 0
  262. char buf[100]="foo bar baz";
  263. printf("%d (expect 7)\n",byte_rchr(buf,11,' '));
  264. #endif
  265. #if 0
  266. unsigned long size;
  267. char* buf=mmap_read(argv[1],&size);
  268. if (buf) {
  269. unsigned int x=fmt_yenc(0,buf,size);
  270. unsigned int y;
  271. char* tmp=malloc(x+1);
  272. y=fmt_yenc(tmp,buf,size);
  273. write(1,tmp,x);
  274. }
  275. #endif
  276. #if 0
  277. char buf[100];
  278. char buf2[100];
  279. unsigned int len,len2;
  280. buf[fmt_yenc(buf,"http://localhost/~fefe",22)]=0;
  281. buffer_puts(buffer_1,buf);
  282. buffer_putsflush(buffer_1,"\n");
  283. if ((buf[len2=scan_yenc(buf,buf2,&len)])!='\n') {
  284. buffer_putsflush(buffer_2,"parse error!\n");
  285. return 1;
  286. }
  287. buffer_put(buffer_1,buf2,len2);
  288. buffer_putsflush(buffer_1,"\n");
  289. return 0;
  290. #endif
  291. #if 0
  292. char buf[100];
  293. char buf2[100];
  294. unsigned int len,len2;
  295. buf[fmt_base64(buf,"foo:bar",7)]=0;
  296. buffer_puts(buffer_1,buf);
  297. buffer_putsflush(buffer_1,"\n");
  298. if ((buf[len2=scan_base64(buf,buf2,&len)])!=0) {
  299. buffer_putsflush(buffer_2,"parse error!\n");
  300. return 1;
  301. }
  302. buffer_put(buffer_1,buf2,len2);
  303. buffer_putsflush(buffer_1,"\n");
  304. return 0;
  305. #endif
  306. #if 0
  307. unsigned long size;
  308. char* buf=mmap_read(argv[1],&size);
  309. if (buf) {
  310. unsigned int x=fmt_uuencoded(0,buf,size);
  311. unsigned int y;
  312. char* tmp=malloc(x+1);
  313. y=fmt_uuencoded(tmp,buf,size);
  314. write(1,tmp,x);
  315. }
  316. #endif
  317. #if 0
  318. char buf[]="00000000000000000000000000000001";
  319. char ip[16];
  320. if (scan_ip6_flat(buf,ip) != str_len(buf))
  321. buffer_putsflush(buffer_2,"parse error!\n");
  322. #endif
  323. #if 0
  324. int fd=open_read("t.c");
  325. buffer b;
  326. char buf[1024];
  327. char line[20];
  328. int i;
  329. buffer_init(&b,read,fd,buf,1024);
  330. i=buffer_getline(&b,line,19);
  331. buffer_puts(buffer_1,"getline returned ");
  332. buffer_putulong(buffer_1,i);
  333. buffer_puts(buffer_1,"\n");
  334. buffer_puts(buffer_1,line);
  335. buffer_flush(buffer_1);
  336. #endif
  337. #if 0
  338. buffer_putulong(buffer_1,23);
  339. // buffer_putspace(buffer_1);
  340. buffer_putsflush(buffer_1,"\n");
  341. // buffer_flush(buffer_1);
  342. #endif
  343. #if 0
  344. long a,b,c;
  345. char buf[4096];
  346. char buf2[4096];
  347. memcpy(buf,buf2,4096);
  348. byte_copy(buf,4096,buf2);
  349. rdtscl(a);
  350. memcpy(buf,buf2,4096);
  351. rdtscl(b);
  352. byte_copy(buf,4096,buf2);
  353. rdtscl(c);
  354. printf("memcpy: %d - byte_copy: %d\n",b-a,c-b);
  355. #endif
  356. #if 0
  357. char ip[16];
  358. int i;
  359. if ((i=scan_ip6(argv[1],ip))) {
  360. char buf[128];
  361. buf[fmt_ip6(buf,ip)]=0;
  362. puts(buf);
  363. }
  364. #endif
  365. #if 0
  366. char buf[100];
  367. strcpy(buf,"foobarbaz");
  368. buf[fmt_fill(buf,3,5,100)]=0;
  369. printf("\"%s\"\n",buf);
  370. #endif
  371. #if 0
  372. unsigned long len;
  373. char *c=mmap_read("/etc/passwd",&len);
  374. printf("got map %p of len %lu\n",c,len);
  375. #endif
  376. #if 0
  377. char c;
  378. printf("%d\n",buffer_getc(buffer_0,&c));
  379. printf("%c\n",c);
  380. #endif
  381. #if 0
  382. char buf[100]="01234567890123456789012345678901234567890123456789";
  383. long a,b,c;
  384. #endif
  385. #if 0
  386. buf[ip4_fmt(buf,ip4loopback)]=0;
  387. buffer_puts(buffer_1small,buf);
  388. buffer_flush(buffer_1small);
  389. #endif
  390. #if 0
  391. buf[0]=0;
  392. buf[fmt_8long(buf,0)]=0;
  393. puts(buf);
  394. rdtscl(a);
  395. c=str_len(buf);
  396. rdtscl(b);
  397. /*byte_zero_djb(buf,j); */
  398. // printf("\n%lu %d\n",b-a,c);
  399. #endif
  400. #if 0
  401. buffer_puts(buffer_1small,"hello, world\n");
  402. buffer_flush(buffer_1small);
  403. #endif
  404. #if 0
  405. int s=socket_tcp4();
  406. char ip[4]={127,0,0,1};
  407. int t=socket_connect4(s,ip,80);
  408. #endif
  409. #if 0
  410. char buf[100]="foo bar baz fnord ";
  411. char buf2[100]="foo braz fnord";
  412. long a,b,c;
  413. long i=0,j=0,k=0;
  414. double d;
  415. uint32 l,m,n;
  416. stralloc sa={0};
  417. stralloc_copys(&sa,"fnord");
  418. stralloc_catlong0(&sa,-23,5);
  419. stralloc_append(&sa,"\n");
  420. printf("%d %d\n",str_equal("fnord","fnord1"),str_equal("fnord1","fnord"));
  421. write(1,sa.s,sa.len);
  422. printf("%d %d\n",stralloc_starts(&sa,"fnord"),stralloc_starts(&sa,"fnord\na"));
  423. l=0xdeadbeef;
  424. uint32_pack_big((char*)&m,l);
  425. uint32_unpack_big((char*)&m,&n);
  426. printf("%x %x %x\n",l,m,n);
  427. rdtscl(a);
  428. /* i=scan_double("3.1415",&d); */
  429. rdtscl(b);
  430. /*byte_zero_djb(buf,j); */
  431. rdtscl(c);
  432. printf("%lu %lu\n",b-a,c-b);
  433. #endif
  434. #if 0
  435. size_t size;
  436. char* buf=mmap_read(argv[1],&size);
  437. if (buf) {
  438. unsigned int x=fmt_urlencoded2(0,buf,size,"x");
  439. unsigned int y;
  440. char* tmp=malloc(x+1);
  441. y=fmt_urlencoded2(tmp,buf,size,"x");
  442. write(1,tmp,x);
  443. }
  444. #endif
  445. #if 0
  446. printf("%d %d\n",strcmp("foo","bar"),str_diff("foo","bar"));
  447. printf("%d %d\n",strcmp("foo","\xfcar"),str_diff("foo","\xfcar"));
  448. #endif
  449. #if 0
  450. {
  451. int16 a;
  452. int32 b;
  453. int64 c;
  454. assert(imult16(4,10000,&a)==0);
  455. assert(imult16(-4,10000,&a)==0);
  456. assert(imult16(5,10,&a)==1 && a==50);
  457. assert(imult16(-3,10000,&a)==1 && a==-30000);
  458. assert(imult32(0x40000000,2,&b)==0);
  459. assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);
  460. assert(imult64(0x4000000000000000ll,2,&c)==0);
  461. assert(imult64(0x3fffffffffffffffll,2,&c)==1 && c==0x7ffffffffffffffell);
  462. }
  463. #endif
  464. #if 0
  465. stralloc a;
  466. printf("%d\n",stralloc_copym(&a,"fnord",", ","foo"));
  467. #endif
  468. return 0;
  469. }