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.

471 lines
11 KiB

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