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.
 
 
 
 

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