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.
 
 
 
 

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