JunkBox_Lib  1.10.2
protocol.c
Go to the documentation of this file.
1 
8 #include "protocol.h"
9 
10 
12 // Protocol Header
13 //
14 
34 tList* get_protocol_header_list(Buffer buf, char deli, int fstline, int rcntnt)
35 {
36  tList* lp;
37 
38  if (buf.buf==NULL) return NULL;
39 
40  lp = get_protocol_header_list_seq(NULL, buf, deli, fstline, rcntnt);
41  if (lp!=NULL) lp = find_tList_top(lp);
42 
43  return lp;
44 }
45 
46 
55 Buffer restore_protocol_header(tList* list, char* deli, int mode, int* hdsz)
56 {
57  Buffer buf;
58 
59  buf = init_Buffer();
60  if (list==NULL) return buf;
61 
63 
64  while(list!=NULL) {
65  if (!strcmp((const char*)(list->ldat.key.buf), HDLIST_FIRST_LINE_KEY)) {
66  copy_Buffer(&(list->ldat.val), &buf);
67  cat_s2Buffer("\r\n", &buf);
68  }
69  else if (!strcmp((const char*)(list->ldat.key.buf), HDLIST_CONTINUE)) {
70  buf.buf[buf.vldsz] = CHAR_TAB;
71  buf.vldsz++;
72  cat_Buffer(&(list->ldat.val), &buf);
73  cat_s2Buffer("\r\n", &buf);
74  }
75  else if (!strcmp((const char*)(list->ldat.key.buf), HDLIST_END_KEY)) {
76  break;
77  }
78  else if (strcmp((const char*)(list->ldat.key.buf), HDLIST_CONTENTS_KEY)) {
79  cat_Buffer(&(list->ldat.key), &buf);
80  cat_s2Buffer(deli, &buf);
81  cat_Buffer(&(list->ldat.val), &buf);
82  cat_s2Buffer("\r\n", &buf);
83  }
84 
85  list = list->next;
86  }
87  if (buf.vldsz>0) cat_s2Buffer("\r\n", &buf);
88 
89  if (hdsz!=NULL) *hdsz = buf.vldsz;
90 
91  if (mode==ON) {
92  int nn = 1;
93  tList* pl = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, nn);
94  while (pl!=NULL && pl->ldat.val.buf!=NULL) {
95  cat_Buffer(&(pl->ldat.val), &buf);
96  pl = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, ++nn);
97  }
98  }
99 
100  return buf;
101 }
102 
103 
110 {
111  Buffer buf;
112  tList* lp;
113  int nn = 1;
114 
115  buf = make_Buffer(BUFSZ);
116 
117  lp = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, nn);
118  while (lp!=NULL && lp->ldat.val.buf!=NULL) {
119  cat_Buffer(&(lp->ldat.val), &buf);
120  lp = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, ++nn);
121  }
122 
123  return buf;
124 }
125 
126 
132 void set_protocol_contents(tList* list, Buffer contents)
133 {
134  while (list!=NULL) {
135  if (list->ldat.key.buf!=NULL && !strcmp((char*)list->ldat.key.buf, HDLIST_CONTENTS_KEY)) {
136  free_Buffer(&list->ldat.val);
137  list->ldat.val = dup_Buffer(contents);
138 
139  while (is_header_continue(list)) del_tList_node(&(list->next));
140  break;
141  }
142 
143  list = list->next;
144  }
145 
146  return;
147 }
148 
149 
181 tList* get_protocol_header_list_seq(tList* lp, Buffer buf, char deli, int fstline, int rcntnt)
182 {
183  static int crlfCount = 0;
184  static int inContents = FALSE;
185  int i=0, n=0, size;
186  Buffer key, data;
187 
188  if (buf.buf==NULL) return NULL;
189 
190  size = buf.vldsz;
191  data = make_Buffer(size+1);
192  key = make_Buffer(LBUF);
193 
194  if (lp==NULL) {
195  crlfCount = 0;
196  inContents = FALSE;
197  }
198 
199  // FIRST LINE
200  if (fstline && lp==NULL) {
201  while(buf.buf[i]!=0x0a && buf.buf[i]!='\0' && i<size && n<size) {
202  data.buf[n++] = buf.buf[i++];
203  }
204  if (data.buf[n-1]==0x0d) {
205  i--;
206  n--;
207  }
208 
209  data.buf[n] = '\0';
210  data.vldsz = n;
212  lp = add_tList_node_Buffer(NULL, key, data);
213 
214  if (buf.buf[i]=='\0' || i==size) {
215  free_Buffer(&key);
216  free_Buffer(&data);
217  return lp;
218  }
219  clear_Buffer(&key);
220  clear_Buffer(&data);
221  }
222 
223  // HEADER
224  while(buf.buf[i]!='\0' && i<size && !inContents) {
225  // Check Previous Line's CR & LF
226  if (i+1<size && buf.buf[i]==0x0d && buf.buf[i+1]==0x0a) {
227  i = i + 2;
228  if (crlfCount==1) { // for previous called function
230  lp = add_tList_node_Buffer(lp, key, data);
231  crlfCount = 0;
232  inContents = TRUE;
233  break;
234  }
235  else crlfCount = 1;
236  }
237  else if (buf.buf[i]==0x0a) {
238  i = i + 1;
239  if (crlfCount==1) { // for previous called function
241  lp = add_tList_node_Buffer(lp, key, data);
242  crlfCount = 0;
243  inContents = TRUE;
244  break;
245  }
246  else crlfCount = 1;
247  }
248 
249  //
250  if (i+1<size && buf.buf[i]==0x0d && buf.buf[i+1]==0x0a) {
251  i = i + 2;
252  if (crlfCount==1) {
254  lp = add_tList_node_Buffer(lp, key, data);
255  crlfCount = 0;
256  inContents = TRUE;
257  break;
258  }
259  }
260  else if (i<size && buf.buf[i]==0x0a) {
261  i = i + 1;
262  if (crlfCount==1) {
264  lp = add_tList_node_Buffer(lp, key, data);
265  crlfCount = 0;
266  inContents = TRUE;
267  break;
268  }
269  }
270 
271  if (buf.buf[i]=='\0' || i==size) break;
272 
273  // HEADER KEY
274  n = 0;
275  if (buf.buf[i]==CHAR_TAB || buf.buf[i]==' ') {
277  }
278  else {
279  while(buf.buf[i]!=deli && buf.buf[i]!='\0' && i<size && n<LBUF) {
280  key.buf[n++] = buf.buf[i++];
281  }
282  key.buf[n] = '\0';
283  key.vldsz = n;
284  }
285  while ((buf.buf[i]==deli ||buf.buf[i]==' '||buf.buf[i]==CHAR_TAB) && i<size) i++;
286 
287  // HEADER VALUE
288  n = 0;
289  while(buf.buf[i]!=0x0a && buf.buf[i]!='\0' && i<size && n<size) {
290  data.buf[n++] = buf.buf[i++];
291  }
292  if (data.buf[n-1]==0x0d) {
293  i--;
294  n--;
295  }
296  data.buf[n] = '\0';
297  data.vldsz = n;
298 
299  lp = add_tList_node_Buffer(lp, key, data);
300 
301  crlfCount = 0;
302  clear_Buffer(&key);
303  clear_Buffer(&data);
304  }
305 
306  // CONTENTS (Text or Binary)
307  if (rcntnt && i<size && inContents) {
308  crlfCount = 0;
309  clear_Buffer(&key);
310  clear_Buffer(&data);
312 
313  n = 0;
314  while(i<size && n<size) {
315  data.buf[n++] = buf.buf[i++];
316  }
317  data.vldsz = n;
318 
319  lp = add_tList_node_Buffer(lp, key, data);
320  }
321 
322  free_Buffer(&key);
323  free_Buffer(&data);
324 
325  return lp;
326 }
327 
328 
341 tList* get_protocol_header_list_file(char* fname, char deli, int fstline, int rcntnt)
342 {
343  Buffer buf;
344  tList* lp;
345 
346  if (fname==NULL) return NULL;
347 
348  buf = read_Buffer_file(fname);
349  lp = get_protocol_header_list(buf, deli, fstline, rcntnt);
350 
351  return lp;
352 }
353 
354 
355 
357 // Search
358 
372 Buffer search_protocol_header(tList* list, char* key, int no)
373 {
374  tList* pp;
375  Buffer buf;
376 
377  buf = init_Buffer();
378  if (list==NULL || key==NULL) return buf;
379 
380  pp = strncasecmp_tList(list, key, 0, no); // 完全一致
381  if (pp!=NULL) {
382  buf = dup_Buffer(pp->ldat.val);
383 
384  while (is_header_continue(pp)) {
385  cat_s2Buffer("\r\n", &buf);
386  pp = pp->next;
387  cat_Buffer(&(pp->ldat.val), &buf);
388  }
389  }
390 
391  return buf;
392 }
393 
394 
408 Buffer search_protocol_header_item(tList* list, char* key, int no, char deli, int nm)
409 {
410  Buffer buf, itm;
411 
412  buf = search_protocol_header(list, key, no);
413  if (buf.buf==NULL) return buf;
414 
415  itm = cawk_Buffer(buf, deli, nm);
416  free_Buffer(&buf);
417 
418  return itm;
419 }
420 
421 
436 Buffer search_protocol_header_value(tList* list, char* key, char* data, int no)
437 {
438  Buffer buf;
439  char* str;
440  int len, nm;
441 
442  buf = init_Buffer();
443  if (list==NULL || key==NULL) return buf;
444 
445  if (data==NULL) {
446  buf = search_protocol_header(list, key, no);
447  return buf;
448  }
449 
450  buf = init_Buffer();
451  len = (int)strlen(data);
452 
453  nm = 0;
454  while (list!=NULL) {
455  if (list->ldat.key.buf!=NULL && !strcasecmp((char*)list->ldat.key.buf, key)) {
456  str = (char*)list->ldat.val.buf;
457 
458  if (str!=NULL && !strncasecmp(str, data, len)) {
459  nm++;
460  if (no==nm) {
461  buf = make_Buffer_bystr(str);
462  return buf;
463  }
464  }
465 
466  while (is_header_continue(list)) {
467  list = list->next;
468  str = (char*)list->ldat.val.buf;
469  if (str!=NULL && !strncasecmp(str, data, len)) {
470  nm++;
471  if (no==nm) {
472  buf = make_Buffer_bystr(str);
473  return buf;
474  }
475  }
476  }
477  }
478 
479  list = list->next;
480  }
481 
482  return buf;
483 }
484 
485 
500 Buffer search_protocol_header_partvalue(tList* list, char* key, char* data, int no)
501 {
502  Buffer buf;
503  char* str;
504  int nm;
505 
506  buf = init_Buffer();
507  if (list==NULL || key==NULL) return buf;
508 
509  if (data==NULL) {
510  buf = search_protocol_header(list, key, no);
511  return buf;
512  }
513 
514  buf = init_Buffer();
515  //len = (int)strlen(data);
516 
517  nm = 0;
518  while (list!=NULL) {
519  if (list->ldat.key.buf!=NULL && !strcasecmp((char*)list->ldat.key.buf, key)) {
520  str = (char*)list->ldat.val.buf;
521 
522  if (str!=NULL && strstrcase(str, data)) {
523  nm++;
524  if (no==nm) {
525  buf = make_Buffer_bystr(str);
526  return buf;
527  }
528  }
529 
530  while (is_header_continue(list)) {
531  list = list->next;
532  str = (char*)list->ldat.val.buf;
533  if (str!=NULL && strstrcase(str, data)) {
534  nm++;
535  if (no==nm) {
536  buf = make_Buffer_bystr(str);
537  return buf;
538  }
539  }
540  }
541  }
542 
543  list = list->next;
544  }
545 
546  return buf;
547 }
548 
549 
550 
552 // Set
553 
554 /*
555 int set_protocol_header(tList* list, char* key, char* value, int no, int add_mode)
556 
557 リスト(lt)中の no番目の keyのノード値に valueを設定する.
558 
559 no が 0以下の場合は,全ての keyノードの値に対して設定が行われる.
560 keyノードが存在せず,かつ mode==ON の場合は,リストの最後(コンテンツの前)に追加される.
561 
562 set_value_tList() との違いは,追加時の追加の仕方のみ.(in Lib/tlist.c)
563 
564 @param list 処理対象のリスト
565 @param key 設定を行うノードのキー部
566 @param value 設定される文字列.
567 @param no 何個目のノードに対して設定を行うか.1から数える.
568  0以下の場合はkeyが一致するすべてのノードに対して設定を行う.
569 @param add_mod この値がON かつ指定したノードが無い場合,ノードリストの最後(ただしコンテンツの前)に追加する.
570 
571 @retval 0以上 設定されたノードの数.指定されたノードが存在しない場合は(追加された場合も)0
572 @retval 負数 エラー.
573 */
574 int set_protocol_header(tList* list, char* key, char* value, int no, int add_mode)
575 {
576  int cn = set_value_tList(list, key, no, value, OFF);
577 
578  // Not Found
579  if (add_mode==ON && cn==0) {
580  tList* pm = strncmp_tList(list, (char*)HDLIST_END_KEY, 0, 1);
581  if (pm!=NULL && pm->prev!=NULL) {
582  add_tList_node_str(pm->prev, key, value);
583  }
584  else {
585  add_tList_node_str(list, key, value);
586  }
587  }
588 
589  return cn;
590 }
591 
592 
600 int search_crlfcrlf(char* mesg)
601 {
602  int cr = 0; // dummy
603  int lf = 0;
604  int i;
605 
606  if (mesg==NULL) return JBXL_ARGS_ERROR;
607  if (mesg[0]==0x0a) return 2;
608  if (mesg[0]==0x0d && mesg[1]==0x0a) return 2;
609 
610  i = 0;
611  while(mesg[i]!='\0') {
612  if (mesg[i]==0x0d) cr++; // not used
613  else if (mesg[i]==0x0a) lf++;
614  else {
615  cr = lf = 0;
616  }
617 
618  if (lf==2) return i+1;
619  i++;
620  }
621  return 0;
622 }
623 
624 
632 {
633  if (pp==NULL || pp->next==NULL || pp->next->ldat.key.buf==NULL) return FALSE;
634  if (!strcmp((const char*)pp->next->ldat.key.buf, HDLIST_CONTINUE)) return TRUE;
635  return FALSE;
636 }
637 
638 
644 void print_protocol_header(tList* pp, int content)
645 {
646  if (pp==NULL) return;
647 
648  if (pp->ldat.id==TLIST_ANCHOR_NODE) pp = pp->next;
649  while (pp!=NULL) { /*&& !ex_strcmp(HDLIST_END_KEY, (char*)pp->ldat.key.buf) */
650  //&& !ex_strcmp(HDLIST_CONTENTS_KEY, (char*)pp->ldat.key.buf)) {
651  if (content==ON || !ex_strcmp(HDLIST_CONTENTS_KEY, (char*)pp->ldat.key.buf)) {
652  PRINT_MESG("[%s] [%s]\n", (char*)pp->ldat.key.buf, (char*)pp->ldat.val.buf);
653  }
654  pp = pp->next;
655  }
656  return;
657 }
658 
659 
666 {
667  tList* end = search_key_tList(lp, HDLIST_END_KEY, 1);
668  if (end==NULL) end = search_key_tList(lp, HDLIST_CONTENTS_KEY, 1);
669  //
670  if (end!=NULL) end = end->prev;
671  else end = find_tList_end(lp);
672 
673  return end;
674 }
675 
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition: buffer.c:71
void clear_Buffer(Buffer *str)
Buffer型変数 のバッファ部を 0クリアする.
Definition: buffer.c:272
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition: buffer.c:128
Buffer read_Buffer_file(const char *fn)
ファイル fn の内容を Buffer型変数に読み込む.
Definition: buffer.c:1467
Buffer init_Buffer()
初期化したBuffer型変数を返す.
Definition: buffer.c:47
Buffer dup_Buffer(Buffer buf)
Buffer型変数のコピーをつくる.
Definition: buffer.c:211
int cat_Buffer(Buffer *src, Buffer *dst)
Buffer変数 srcから dstへバッファを catする.
Definition: buffer.c:384
int copy_Buffer(Buffer *src, Buffer *dst)
Buffer型変数 srcから dstへバッファをコピーする.
Definition: buffer.c:315
Buffer cawk_Buffer(Buffer str, char cc, int n)
Buffer文字列に対する(変形の)awk.
Definition: buffer.c:1094
#define copy_s2Buffer(src, dst)
copy_b2Buffer()
Definition: buffer.h:108
#define cat_s2Buffer(src, dst)
cat_b2Buffer()
Definition: buffer.h:122
#define make_Buffer_bystr(str)
set_Buffer()
Definition: buffer.h:57
#define OFF
Definition: common.h:231
#define RECVBUFSZ
256K
Definition: common.h:134
#define LBUF
Definition: common.h:146
#define TRUE
Definition: common.h:226
#define FALSE
Definition: common.h:223
#define BUFSZ
16K
Definition: common.h:138
#define ON
Definition: common.h:230
#define JBXL_ARGS_ERROR
不正な引数(NULLなど)
Definition: jbxl_state.h:42
unsigned char ** buf
Definition: jpeg_tool.h:96
unsigned char unsigned long * len
Definition: jpeg_tool.h:96
Buffer search_protocol_header_item(tList *list, char *key, int no, char deli, int nm)
Definition: protocol.c:408
void set_protocol_contents(tList *list, Buffer contents)
Definition: protocol.c:132
Buffer search_protocol_header(tList *list, char *key, int no)
Definition: protocol.c:372
void print_protocol_header(tList *pp, int content)
Definition: protocol.c:644
tList * find_protocol_end(tList *lp)
Definition: protocol.c:665
int search_crlfcrlf(char *mesg)
Definition: protocol.c:600
int set_protocol_header(tList *list, char *key, char *value, int no, int add_mode)
Definition: protocol.c:574
tList * get_protocol_header_list_seq(tList *lp, Buffer buf, char deli, int fstline, int rcntnt)
Definition: protocol.c:181
Buffer restore_protocol_header(tList *list, char *deli, int mode, int *hdsz)
Definition: protocol.c:55
Buffer restore_protocol_contents(tList *list)
Definition: protocol.c:109
int is_header_continue(tList *pp)
Definition: protocol.c:631
tList * get_protocol_header_list(Buffer buf, char deli, int fstline, int rcntnt)
Definition: protocol.c:34
tList * get_protocol_header_list_file(char *fname, char deli, int fstline, int rcntnt)
Definition: protocol.c:341
Buffer search_protocol_header_value(tList *list, char *key, char *data, int no)
Definition: protocol.c:436
Buffer search_protocol_header_partvalue(tList *list, char *key, char *data, int no)
Definition: protocol.c:500
プロトコル解析ライブラリ ヘッダ
#define HDLIST_CONTINUE
Definition: protocol.h:33
#define HDLIST_CONTENTS_KEY
Definition: protocol.h:32
#define HDLIST_END_KEY
Definition: protocol.h:34
#define HDLIST_FIRST_LINE_KEY
Definition: protocol.h:31
Definition: buffer.h:35
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition: buffer.h:37
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition: buffer.h:39
tList * find_tList_end(tList *pl)
リストの最後のノードを探す.
Definition: tlist.c:1023
tList * strncasecmp_tList(tList *pl, const char *key, int len, int no)
char* 型変数によるノードのサーチ.大文字小文字を無視する
Definition: tlist.c:1094
int set_value_tList(tList *list, const char *key, int no, const char *value, int add_mode)
リスト(lt)中の no番目の keyノードの値に valueを設定する.
Definition: tlist.c:1675
tList * del_tList_node(tList **node)
リスト用のノードを削除.
Definition: tlist.c:270
tList * search_key_tList(tList *pl, const char *key, int no)
リストの中から no番目の keyノード(ldat.key)を探し出し,tListへのポインタを返す.大文字小文字を無視.
Definition: tlist.c:1571
tList * strncmp_tList(tList *pl, const char *key, int len, int no)
char* 型変数によるノードのサーチ.
Definition: tlist.c:1056
tList * find_tList_top(tList *pl)
リストの最初のノードを探す.
Definition: tlist.c:1002
#define add_tList_node_Buffer(p, k, v)
add_tList_node_byBuffer()
Definition: tlist.h:144
#define add_tList_node_str(p, k, v)
add_tList_node_bystr()
Definition: tlist.h:142
#define TLIST_ANCHOR_NODE
アンカーノード
Definition: tlist.h:92
char * strstrcase(const char *buf, const char *nd)
文字列 bufの中に文字列 ndがあるかどうかをチェックする.大文字小文字は区別しない.
Definition: tools.c:736
#define ex_strcmp(a, b)
文字列 aの長さに合わせて比較する.大文字小文字を区別しない.一致するなら TRUE
Definition: tools.h:283
#define CHAR_TAB
タブ
Definition: tools.h:80
#define PRINT_MESG
環境依存用の出力関数.print_message()
Definition: tools.h:475