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.
 
 
 
 

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