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.
 
 
 
 

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