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.

414 lines
9.8 KiB

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