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.
 
 
 
 

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