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.
 
 
 
 

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