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.

486 lines
12 KiB

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