Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

574 lines
15 KiB

21 years ago
20 years ago
20 years ago
12 years ago
6 years ago
12 years ago
12 years ago
21 years ago
21 years ago
6 years ago
6 years ago
21 years ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
6 years ago
6 years ago
6 years ago
12 years ago
15 years ago
17 years ago
17 years ago
17 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
21 years ago
21 years ago
21 years ago
  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 "parse.h"
  30. #include "CAS.h"
  31. #include "io_internal.h"
  32. #define rdtscl(low) \
  33. __asm__ __volatile__ ("lfence\nrdtsc" : "=a" (low) : : "edx")
  34. // #define atomic_add(mem,val) asm volatile ("lock; add%z0 %1, %0": "+m" (mem): "ir" (val))
  35. // compiletimeassert(sizeof(long) == 2);
  36. int64 writecb(int64 fd,const void* buf,uint64 n) {
  37. (void)fd;
  38. (void)buf;
  39. (void)n;
  40. #if 0
  41. int r;
  42. int todo=n>=65536?65536:n;
  43. r=write(fd,buf,todo);
  44. if (r==-1 && r!=EAGAIN) r=-3;
  45. return r;
  46. #endif
  47. return -1;
  48. }
  49. int ret0(const char* s,void* foo) {
  50. (void)foo;
  51. assert(strcmp(s,"fnord")==0);
  52. return 0;
  53. }
  54. int ret1(const char* s,void* foo) {
  55. static int i;
  56. (void)foo;
  57. switch (i) {
  58. case 0: assert(strcmp(s,"fnord")==0); break;
  59. case 1: assert(strcmp(s,"fnord2")==0); break;
  60. default: return -1;
  61. }
  62. ++i;
  63. return 1;
  64. }
  65. int main(int argc,char* argv[]) {
  66. (void)argc;
  67. (void)argv;
  68. int64 pfd[2];
  69. size_t i;
  70. io_socketpair(pfd);
  71. io_fd(pfd[0]);
  72. io_fd(pfd[1]);
  73. io_batch* b = iob_new(1024);
  74. // first write 1024 blocks (to activate the splitting code path)
  75. for (i=0; i<1024; ++i) {
  76. if ((i%50) == 0) {
  77. static char c = 0;
  78. char* s = alloca(1);
  79. *s = c;
  80. iob_addbuf(b, s, 1);
  81. ++c;
  82. } else
  83. iob_addbuf(b, "a", 1);
  84. }
  85. assert(iob_send(pfd[0], b) == 1024);
  86. {
  87. char buf[1024];
  88. assert(read(pfd[1], buf, 1024) == 1024);
  89. // drain the pipe
  90. }
  91. char boilerplate[]="Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time tozz get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.";
  92. // now try setting the socket to non-blocking and write more than
  93. // fits, to check proper return value
  94. io_nonblock(pfd[0]);
  95. size_t total=0;
  96. for (i=0; i<1024; ++i) {
  97. if ((i%50) == 0) {
  98. static char c = 0;
  99. char* s = alloca(1);
  100. *s = c;
  101. iob_addbuf(b, s, 1);
  102. total += 1;
  103. ++c;
  104. } else {
  105. iob_addbuf(b, boilerplate, strlen(boilerplate));
  106. total += strlen(boilerplate);
  107. }
  108. }
  109. io_wantwrite(pfd[0]);
  110. io_wantread(pfd[1]);
  111. size_t totalread = 0;
  112. size_t totalwrite = 0;
  113. for (;;) {
  114. io_wait();
  115. int fd;
  116. while ((fd = io_canwrite()) != -1) {
  117. // printf("write event on fd %d\n", fd);
  118. long r = iob_send(pfd[0], b);
  119. if (r > 0) totalwrite += r;
  120. if (r == 0)
  121. io_dontwantwrite(pfd[0]);
  122. printf("r = %ld (sum written %zu, total back size %zu)\n", r, totalwrite, total);
  123. }
  124. while ((fd = io_canread()) != -1) {
  125. char buf[1024];
  126. ssize_t r = read(fd, buf, sizeof buf);
  127. if (r > 0) totalread += r;
  128. if (r < (ssize_t)sizeof buf) io_eagain(fd);
  129. if (r == 0 || totalread == total) {
  130. printf("read %zu bytes total\n", totalread);
  131. return totalread == total;
  132. }
  133. }
  134. }
  135. #if 0
  136. char buf[10];
  137. buf[fmt_cescape(buf,"\003foo\xc0",5)]=0;
  138. puts(buf);
  139. #endif
  140. #if 0
  141. struct bytestream bs1 = BS_FROM_MEMBUF("fnord", 5);
  142. struct bytestream bs2 = BS_FROM_BUFFER(buffer_0);
  143. #endif
  144. #if 0
  145. compiletimeassert(sizeof(char) < sizeof(int));
  146. io_wait();
  147. #endif
  148. #if 0
  149. char buf[1024];
  150. size_t n;
  151. scan_base64("eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9",buf,&n);
  152. write(1,buf,n); write(1,"\n",1);
  153. scan_base64("eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",buf,&n);
  154. write(1,buf,n); write(1,"\n",1);
  155. #endif
  156. #if 0
  157. int s=socket_udp6();
  158. #endif
  159. #if 0
  160. char buf[100];
  161. assert(fmt_varint(buf,1)==1 && buf[0]==1);
  162. fmt_varint(buf,300);
  163. assert(fmt_varint(buf,300)==2 && buf[0]==(char)0xac && buf[1]==0x02);
  164. #endif
  165. #if 0
  166. const char buf[]="fnord\n";
  167. buffer_puts(buffer_1,buf);
  168. strcpy((char*)buf,"foo\n");
  169. buffer_puts(buffer_1,buf);
  170. buffer_puts(buffer_1,"bar\n");
  171. buffer_flush(buffer_1);
  172. #endif
  173. #if 0
  174. static critbit0_tree t;
  175. assert(critbit0_insert(&t,"fnord")==2);
  176. assert(critbit0_insert(&t,"fnord2")==2);
  177. assert(critbit0_insert(&t,"fnord2")==1);
  178. assert(critbit0_contains(&t,"foo")==0);
  179. assert(critbit0_contains(&t,"fnord")==1);
  180. assert(critbit0_allprefixed(&t,"fnord",ret1,NULL)==1);
  181. assert(critbit0_allprefixed(&t,"fnord",ret0,NULL)==0);
  182. assert(critbit0_delete(&t,"fnord2")==1);
  183. assert(critbit0_delete(&t,"foo")==0);
  184. #endif
  185. #if 0
  186. int s = socket_tcp6();
  187. #endif
  188. #if 0
  189. iarray i;
  190. iarray_init(&i,sizeof(size_t));
  191. printf("%p\n",iarray_get(&i,0));
  192. printf("%p\n",iarray_allocate(&i,0));
  193. printf("%p\n",iarray_allocate(&i,0));
  194. printf("%p\n",iarray_get(&i,0));
  195. #endif
  196. #if 0
  197. char buf[1024];
  198. size_t l;
  199. unsigned char c;
  200. (void)writecb;
  201. (void)argc;
  202. (void)argv;
  203. (void)c;
  204. assert(fmt_jsonescape(buf,"foo\nbar\\",8)==10 && byte_equal(buf,10,"foo\\nbar\\\\"));
  205. memset(buf,0,sizeof(buf));
  206. assert(scan_jsonescape("foo\\u000abar\\\\",buf,&l)==14 && l==8 && byte_equal(buf,8,"foo\nbar\\"));
  207. memset(buf,0,sizeof(buf));
  208. /* example from the json spec: G clef U+1D11E encoded using UTF-16 surrogates*/
  209. assert(scan_jsonescape("\\uD834\\uDD1Exyz",buf,&l)==15 && l==7 && byte_equal(buf,7,"\xf0\x9d\x84\x9exyz"));
  210. /*
  211. 1D11E -> 0001 1101 0001 0001 1110
  212. -> ______00 __011101 __000100 __011110
  213. as utf8: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  214. 11110000 10011101 10000100 10011110
  215. f 0 9 d 8 4 9 e
  216. */
  217. #endif
  218. #if 0
  219. static size_t x;
  220. x=23;
  221. atomic_add(&x,3);
  222. printf("%u\n",x);
  223. printf("%u\n",atomic_add_return(&x,-3));
  224. printf("%u\n",compare_and_swap(&x,26,17));
  225. printf("%u\n",compare_and_swap(&x,23,17));
  226. #endif
  227. #if 0
  228. atomic_add(&x,3); printf("%u\n",x);
  229. x=23;
  230. atomic_add(&x,3); assert(x==26);
  231. atomic_or(&x,1); assert(x==27);
  232. atomic_and(&x,-2); assert(x==26);
  233. #endif
  234. #if 0
  235. iarray a;
  236. char* c;
  237. iarray_init(&a,sizeof(io_entry));
  238. printf("15 -> %p\n",c=iarray_allocate(&a,15));
  239. printf("23 -> %p\n",c=iarray_allocate(&a,23));
  240. printf("1234567 -> %p\n",c=iarray_allocate(&a,1234567));
  241. printf("23 -> %p\n",iarray_get(&a,23));
  242. #endif
  243. #if 0
  244. io_batch* b=iob_new(1234);
  245. int64 fd=open("t.c",0);
  246. iob_addbuf(b,"fnord",5);
  247. iob_addfile_close(b,fd,0,7365);
  248. iob_write(1,b,writecb);
  249. #endif
  250. #if 0
  251. char dest[1024];
  252. unsigned long len;
  253. scan_urlencoded2("libstdc++.tar.gz",dest,&len);
  254. buffer_putmflush(buffer_1,dest,"\n");
  255. #endif
  256. #if 0
  257. static stralloc sa;
  258. stralloc_copym(&sa,"foo ","bar ","baz.\n");
  259. write(1,sa.s,sa.len);
  260. #endif
  261. #if 0
  262. buffer_putmflush(buffer_1,"foo ","bar ","baz.\n");
  263. #endif
  264. #if 0
  265. char* c="fnord";
  266. int fd=open_read(c);
  267. errmsg_iam(argv[0]);
  268. carp("could not open file `",c,"'");
  269. diesys(23,"could not open file `",c,"'");
  270. #endif
  271. #if 0
  272. errmsg_warn("could not open file `",c,"'",0);
  273. errmsg_warnsys("could not open file `",c,"'",0);
  274. #endif
  275. #if 0
  276. char buf[100]="/usr/bin/sh";
  277. int len=str_len(buf);
  278. assert(byte_rchr(buf,len,'/')==8);
  279. assert(byte_rchr(buf,len,'@')==len);
  280. assert(byte_rchr(buf,len,'h')==len-1);
  281. printf("%d\n",byte_rchr("x",1,'x'));
  282. #endif
  283. #if 0
  284. char buf[IP6_FMT+100];
  285. int i;
  286. char ip[16];
  287. uint32 scope_id;
  288. char* s="fec0::1:220:e0ff:fe69:ad92%eth0/64";
  289. char blubip[16]="\0\0\0\0\0\0\0\0\0\0\xff\xff\x7f\0\0\001";
  290. i=scan_ip6if(s,ip,&scope_id);
  291. assert(s[i]=='/');
  292. buffer_put(buffer_1,buf,fmt_ip6if(buf,ip,scope_id));
  293. buffer_putnlflush(buffer_1);
  294. buffer_put(buffer_1,buf,fmt_ip6ifc(buf,blubip,scope_id));
  295. buffer_putnlflush(buffer_1);
  296. scan_ip6("2001:7d0:0:f015:0:0:0:1",ip);
  297. buffer_put(buffer_1,buf,fmt_ip6(buf,ip));
  298. buffer_putnlflush(buffer_1);
  299. #endif
  300. #if 0
  301. char buf[100];
  302. int i;
  303. printf("%d\n",i=fmt_pad(buf,"fnord",5,7,10));
  304. buf[i]=0;
  305. puts(buf);
  306. #endif
  307. #if 0
  308. char ip[16];
  309. char buf[32];
  310. printf("%d (expect 2)\n",scan_ip6("::",ip));
  311. printf("%d (expect 3)\n",scan_ip6("::1",ip));
  312. printf("%d (expect 16)\n",scan_ip6("fec0:0:0:ffff::1/0",ip));
  313. printf("%.*s\n",fmt_ip6(buf,ip),buf);
  314. #endif
  315. #if 0
  316. static stralloc s,t;
  317. stralloc_copys(&s,"fnord");
  318. stralloc_copys(&t,"abc"); printf("%d\n",stralloc_diff(&s,&t));
  319. stralloc_copys(&t,"fnor"); printf("%d\n",stralloc_diff(&s,&t));
  320. stralloc_copys(&t,"fnord"); printf("%d\n",stralloc_diff(&s,&t));
  321. stralloc_copys(&t,"fnordh"); printf("%d\n",stralloc_diff(&s,&t));
  322. stralloc_copys(&t,"hausen"); printf("%d\n",stralloc_diff(&s,&t));
  323. #endif
  324. #if 0
  325. static stralloc s;
  326. stralloc_copys(&s,"fnord");
  327. printf("%d\n",stralloc_diffs(&s,"abc"));
  328. printf("%d\n",stralloc_diffs(&s,"fnor"));
  329. printf("%d\n",stralloc_diffs(&s,"fnord"));
  330. printf("%d\n",stralloc_diffs(&s,"fnordh"));
  331. printf("%d\n",stralloc_diffs(&s,"hausen"));
  332. #endif
  333. #if 0
  334. printf("%d\n",case_starts("fnordhausen","FnOrD"));
  335. printf("%d\n",case_starts("fnordhausen","blah"));
  336. #endif
  337. #if 0
  338. char buf[]="FnOrD";
  339. case_lowers(buf);
  340. puts(buf);
  341. #endif
  342. #if 0
  343. char buf[100]="foo bar baz";
  344. printf("%d (expect 7)\n",byte_rchr(buf,11,' '));
  345. #endif
  346. #if 0
  347. unsigned long size;
  348. char* buf=mmap_read(argv[1],&size);
  349. if (buf) {
  350. unsigned int x=fmt_yenc(0,buf,size);
  351. unsigned int y;
  352. char* tmp=malloc(x+1);
  353. y=fmt_yenc(tmp,buf,size);
  354. write(1,tmp,x);
  355. }
  356. #endif
  357. #if 0
  358. char buf[100];
  359. char buf2[100];
  360. unsigned int len,len2;
  361. buf[fmt_yenc(buf,"http://localhost/~fefe",22)]=0;
  362. buffer_puts(buffer_1,buf);
  363. buffer_putsflush(buffer_1,"\n");
  364. if ((buf[len2=scan_yenc(buf,buf2,&len)])!='\n') {
  365. buffer_putsflush(buffer_2,"parse error!\n");
  366. return 1;
  367. }
  368. buffer_put(buffer_1,buf2,len2);
  369. buffer_putsflush(buffer_1,"\n");
  370. return 0;
  371. #endif
  372. #if 0
  373. char buf[100];
  374. char buf2[100];
  375. unsigned int len,len2;
  376. buf[fmt_base64(buf,"foo:bar",7)]=0;
  377. buffer_puts(buffer_1,buf);
  378. buffer_putsflush(buffer_1,"\n");
  379. if ((buf[len2=scan_base64(buf,buf2,&len)])!=0) {
  380. buffer_putsflush(buffer_2,"parse error!\n");
  381. return 1;
  382. }
  383. buffer_put(buffer_1,buf2,len2);
  384. buffer_putsflush(buffer_1,"\n");
  385. return 0;
  386. #endif
  387. #if 0
  388. unsigned long size;
  389. char* buf=mmap_read(argv[1],&size);
  390. if (buf) {
  391. unsigned int x=fmt_uuencoded(0,buf,size);
  392. unsigned int y;
  393. char* tmp=malloc(x+1);
  394. y=fmt_uuencoded(tmp,buf,size);
  395. write(1,tmp,x);
  396. }
  397. #endif
  398. #if 0
  399. char buf[]="00000000000000000000000000000001";
  400. char ip[16];
  401. if (scan_ip6_flat(buf,ip) != str_len(buf))
  402. buffer_putsflush(buffer_2,"parse error!\n");
  403. #endif
  404. #if 0
  405. int fd=open_read("t.c");
  406. buffer b;
  407. char buf[1024];
  408. char line[20];
  409. int i;
  410. buffer_init(&b,read,fd,buf,1024);
  411. i=buffer_getline(&b,line,19);
  412. buffer_puts(buffer_1,"getline returned ");
  413. buffer_putulong(buffer_1,i);
  414. buffer_puts(buffer_1,"\n");
  415. buffer_puts(buffer_1,line);
  416. buffer_flush(buffer_1);
  417. #endif
  418. #if 0
  419. buffer_putulong(buffer_1,23);
  420. // buffer_putspace(buffer_1);
  421. buffer_putsflush(buffer_1,"\n");
  422. // buffer_flush(buffer_1);
  423. #endif
  424. #if 0
  425. long a,b,c;
  426. char buf[4096];
  427. char buf2[4096];
  428. memcpy(buf,buf2,4096);
  429. byte_copy(buf,4096,buf2);
  430. rdtscl(a);
  431. memcpy(buf,buf2,4096);
  432. rdtscl(b);
  433. byte_copy(buf,4096,buf2);
  434. rdtscl(c);
  435. printf("memcpy: %d - byte_copy: %d\n",b-a,c-b);
  436. #endif
  437. #if 0
  438. char ip[16];
  439. int i;
  440. if ((i=scan_ip6(argv[1],ip))) {
  441. char buf[128];
  442. buf[fmt_ip6(buf,ip)]=0;
  443. puts(buf);
  444. }
  445. #endif
  446. #if 0
  447. char buf[100];
  448. strcpy(buf,"foobarbaz");
  449. buf[fmt_fill(buf,3,5,100)]=0;
  450. printf("\"%s\"\n",buf);
  451. #endif
  452. #if 0
  453. unsigned long len;
  454. char *c=mmap_read("/etc/passwd",&len);
  455. printf("got map %p of len %lu\n",c,len);
  456. #endif
  457. #if 0
  458. char c;
  459. printf("%d\n",buffer_getc(buffer_0,&c));
  460. printf("%c\n",c);
  461. #endif
  462. #if 0
  463. char buf[100]="01234567890123456789012345678901234567890123456789";
  464. long a,b,c;
  465. #endif
  466. #if 0
  467. buf[ip4_fmt(buf,ip4loopback)]=0;
  468. buffer_puts(buffer_1small,buf);
  469. buffer_flush(buffer_1small);
  470. #endif
  471. #if 0
  472. buf[0]=0;
  473. buf[fmt_8long(buf,0)]=0;
  474. puts(buf);
  475. rdtscl(a);
  476. c=str_len(buf);
  477. rdtscl(b);
  478. /*byte_zero_djb(buf,j); */
  479. // printf("\n%lu %d\n",b-a,c);
  480. #endif
  481. #if 0
  482. buffer_puts(buffer_1small,"hello, world\n");
  483. buffer_flush(buffer_1small);
  484. #endif
  485. #if 0
  486. int s=socket_tcp4();
  487. char ip[4]={127,0,0,1};
  488. int t=socket_connect4(s,ip,80);
  489. #endif
  490. #if 0
  491. char buf[100]="foo bar baz fnord ";
  492. char buf2[100]="foo braz fnord";
  493. long a,b,c;
  494. long i=0,j=0,k=0;
  495. double d;
  496. uint32 l,m,n;
  497. stralloc sa={0};
  498. stralloc_copys(&sa,"fnord");
  499. stralloc_catlong0(&sa,-23,5);
  500. stralloc_append(&sa,"\n");
  501. printf("%d %d\n",str_equal("fnord","fnord1"),str_equal("fnord1","fnord"));
  502. write(1,sa.s,sa.len);
  503. printf("%d %d\n",stralloc_starts(&sa,"fnord"),stralloc_starts(&sa,"fnord\na"));
  504. l=0xdeadbeef;
  505. uint32_pack_big((char*)&m,l);
  506. uint32_unpack_big((char*)&m,&n);
  507. printf("%x %x %x\n",l,m,n);
  508. rdtscl(a);
  509. /* i=scan_double("3.1415",&d); */
  510. rdtscl(b);
  511. /*byte_zero_djb(buf,j); */
  512. rdtscl(c);
  513. printf("%lu %lu\n",b-a,c-b);
  514. #endif
  515. #if 0
  516. size_t size;
  517. char* buf=mmap_read(argv[1],&size);
  518. if (buf) {
  519. unsigned int x=fmt_urlencoded2(0,buf,size,"x");
  520. unsigned int y;
  521. char* tmp=malloc(x+1);
  522. y=fmt_urlencoded2(tmp,buf,size,"x");
  523. write(1,tmp,x);
  524. }
  525. #endif
  526. #if 0
  527. printf("%d %d\n",strcmp("foo","bar"),str_diff("foo","bar"));
  528. printf("%d %d\n",strcmp("foo","\xfcar"),str_diff("foo","\xfcar"));
  529. #endif
  530. #if 0
  531. {
  532. int16 a;
  533. int32 b;
  534. int64 c;
  535. assert(imult16(4,10000,&a)==0);
  536. assert(imult16(-4,10000,&a)==0);
  537. assert(imult16(5,10,&a)==1 && a==50);
  538. assert(imult16(-3,10000,&a)==1 && a==-30000);
  539. assert(imult32(0x40000000,2,&b)==0);
  540. assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);
  541. assert(imult64(0x4000000000000000ll,2,&c)==0);
  542. assert(imult64(0x3fffffffffffffffll,2,&c)==1 && c==0x7ffffffffffffffell);
  543. }
  544. #endif
  545. #if 0
  546. stralloc a;
  547. printf("%d\n",stralloc_copym(&a,"fnord",", ","foo"));
  548. #endif
  549. return 0;
  550. }