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.

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