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.
 
 
 
 

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