| 1 |
|
| /* -*- c -*- |
| 2 |
|
| * File: pstring.h |
| 3 |
|
| * Author: Igor Vlasenko <vlasenko@imath.kiev.ua> |
| 4 |
|
| * Created: Fri Jul 1 20:11:51 2005 |
| 5 |
|
| * |
| 6 |
|
| * $Id$ |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #include <stdio.h> |
| 10 |
|
| #include <string.h> |
| 11 |
|
| #include <ctype.h> |
| 12 |
|
| #include "pbuffer.h" |
| 13 |
|
| #include "pstring.h" |
| 14 |
|
| |
| 15 |
|
| static |
| 16 |
|
| PSTRING |
| 17 |
934 |
| lowercase_pstring (pbuffer* pBuffer ,PSTRING pstring) { |
| 18 |
934 |
| const size_t size=pstring.endnext-pstring.begin; |
| 19 |
934 |
| char* buf=pbuffer_resize(pBuffer, size+1); |
| 20 |
934 |
| char* inbuf=buf; |
| 21 |
934 |
| const char* i=pstring.begin; |
| 22 |
|
| PSTRING retval; |
| 23 |
5510 |
▶ | while (i<pstring.endnext) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i<pstring.endnext) { |
4576 |
934 |
5510 |
|
| 24 |
4576 |
| *inbuf++=tolower((unsigned char) *i++); |
| 25 |
|
| } |
| 26 |
934 |
| *inbuf=0; |
| 27 |
934 |
| retval.begin=buf; |
| 28 |
934 |
| retval.endnext=buf+size; |
| 29 |
934 |
| return retval; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| /* |
| 33 |
|
| static |
| 34 |
|
| void |
| 35 |
|
| lowercase_pstring_inplace (PSTRING pstring) { |
| 36 |
|
| char* i=pstring.begin; |
| 37 |
|
| while (i<pstring.endnext) { |
| 38 |
|
| *i=tolower(*i); |
| 39 |
|
| i++; |
| 40 |
|
| } |
| 41 |
|
| } |
| 42 |
|
| */ |
| 43 |
|
| |
| 44 |
|
| static |
| 45 |
|
| PSTRING |
| 46 |
426 |
| uppercase_pstring (pbuffer* pBuffer ,PSTRING pstring) { |
| 47 |
426 |
| const size_t size=pstring.endnext-pstring.begin; |
| 48 |
426 |
| char* buf=pbuffer_resize(pBuffer, size+1); |
| 49 |
426 |
| char* inbuf=buf; |
| 50 |
426 |
| const char* i=pstring.begin; |
| 51 |
|
| PSTRING retval; |
| 52 |
2678 |
▶ | while (i<pstring.endnext) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i<pstring.endnext) { |
2252 |
426 |
2678 |
|
| 53 |
2252 |
| *inbuf++=toupper((unsigned char) *i++); |
| 54 |
|
| } |
| 55 |
426 |
| *inbuf=0; |
| 56 |
426 |
| retval.begin=buf; |
| 57 |
426 |
| retval.endnext=buf+size; |
| 58 |
426 |
| return retval; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| |
| 62 |
|
| static |
| 63 |
|
| int |
| 64 |
90 |
| is_pstring_true (PSTRING s) { |
| 65 |
90 |
| const size_t len = s.endnext-s.begin; |
| 66 |
90 |
▶ | if (s.begin == NULL || 0==len) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s.begin == NULL || 0==len) return 0; |
90 |
0 |
90 |
| if (s.begin == NULL || 0==len) return 0; |
0 |
90 |
90 |
|
| 67 |
90 |
▶ | if (1==len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1==len) { |
88 |
2 |
90 |
|
| 68 |
88 |
▶ | if (*(s.begin)=='0') return 0; else return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*(s.begin)=='0') return 0; else return 1; |
35 |
53 |
88 |
|
| 69 |
2 |
▶ | } else if (3==len) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (3==len) { |
0 |
2 |
2 |
|
| 70 |
0 |
▶ | if ('0'==*(s.begin) && '.'==*(s.begin+1) && '0'==*(s.begin+2)) return 0; else return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if ('0'==*(s.begin) && '.'==*(s.begin+1) && '0'==*(s.begin+2)) return 0; else return 1; |
0 |
0 |
0 |
| if ('0'==*(s.begin) && '.'==*(s.begin+1) && '0'==*(s.begin+2)) return 0; else return 1; |
0 |
0 |
0 |
| if ('0'==*(s.begin) && '.'==*(s.begin+1) && '0'==*(s.begin+2)) return 0; else return 1; |
0 |
0 |
0 |
|
| 71 |
2 |
| } else return 1; |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| #define MAX_ESCAPE_SEQ sizeof(""") |
| 75 |
|
| static |
| 76 |
|
| PSTRING |
| 77 |
67 |
| htmlencode_pstring (pbuffer* StrBuffer, PSTRING pstring) { |
| 78 |
67 |
| char* buf=pbuffer_resize(StrBuffer, pstring.endnext-pstring.begin+1+MAX_ESCAPE_SEQ); |
| 79 |
67 |
| const char* curpos=pstring.begin; |
| 80 |
67 |
| size_t offset=0; |
| 81 |
67 |
| size_t buflen=pbuffer_size(StrBuffer); |
| 82 |
|
| PSTRING retval; |
| 83 |
51578 |
▶ | while (curpos<pstring.endnext) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curpos<pstring.endnext) { |
51511 |
67 |
51578 |
|
| 84 |
51511 |
| unsigned char curchar=*curpos++; |
| 85 |
51511 |
| int bufdelta=1; |
| 86 |
51511 |
▶ | if (offset>=buflen-MAX_ESCAPE_SEQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset>=buflen-MAX_ESCAPE_SEQ) { |
0 |
51511 |
51511 |
|
| 87 |
0 |
| buf=pbuffer_resize(StrBuffer, 2*(offset+MAX_ESCAPE_SEQ)); |
| 88 |
0 |
| buflen=pbuffer_size(StrBuffer); |
| 89 |
|
| } |
| 90 |
51511 |
| switch (curchar) { |
| 91 |
|
| /* straight from the CGI.pm bible. (HTML::Template) */ |
| 92 |
11 |
| case '&' : bufdelta=5; strncpy(buf+offset, "&", bufdelta);break; |
| 93 |
29 |
| case '"' : bufdelta=6; strncpy(buf+offset, """,bufdelta);break; |
| 94 |
27 |
| case '>' : bufdelta=4; strncpy(buf+offset, ">", bufdelta);break; |
| 95 |
27 |
| case '<' : bufdelta=4; strncpy(buf+offset, "<", bufdelta);break; |
| 96 |
24 |
| case '\'': bufdelta=5; strncpy(buf+offset, "'", bufdelta);break; |
| 97 |
51393 |
| default: *(buf+offset)=curchar; |
| 98 |
|
| } |
| 99 |
51511 |
| offset+=bufdelta; |
| 100 |
|
| } |
| 101 |
67 |
| retval.begin=buf; |
| 102 |
67 |
| retval.endnext=buf+offset; |
| 103 |
67 |
| return retval; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| static |
| 107 |
|
| PSTRING |
| 108 |
9 |
| jsencode_pstring (pbuffer* StrBuffer, PSTRING pstring) { |
| 109 |
9 |
| char* buf=pbuffer_resize(StrBuffer, pstring.endnext-pstring.begin+1+MAX_ESCAPE_SEQ); |
| 110 |
9 |
| const char* curpos=pstring.begin; |
| 111 |
9 |
| size_t offset=0; |
| 112 |
9 |
| size_t buflen=pbuffer_size(StrBuffer); |
| 113 |
|
| PSTRING retval; |
| 114 |
381 |
▶ | while (curpos<pstring.endnext) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curpos<pstring.endnext) { |
372 |
9 |
381 |
|
| 115 |
372 |
| unsigned char curchar=*curpos++; |
| 116 |
372 |
| int bufdelta=1; |
| 117 |
372 |
▶ | if (offset>=buflen-MAX_ESCAPE_SEQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset>=buflen-MAX_ESCAPE_SEQ) { |
8 |
364 |
372 |
|
| 118 |
8 |
| buf=pbuffer_resize(StrBuffer, 2*(offset+MAX_ESCAPE_SEQ)); |
| 119 |
8 |
| buflen=pbuffer_size(StrBuffer); |
| 120 |
|
| } |
| 121 |
372 |
| switch (curchar) { |
| 122 |
8 |
| case '\\' : bufdelta=6; strncpy(buf+offset, "\\u005c", bufdelta);break; |
| 123 |
18 |
| case '"' : bufdelta=6; strncpy(buf+offset, "\\u0022",bufdelta);break; |
| 124 |
10 |
| case '\'' : bufdelta=6; strncpy(buf+offset, "\\u0027",bufdelta);break; |
| 125 |
9 |
| case '\n' : bufdelta=6; strncpy(buf+offset, "\\u000a",bufdelta);break; |
| 126 |
9 |
| case '\r' : bufdelta=6; strncpy(buf+offset, "\\u000d",bufdelta);break; |
| 127 |
16 |
| case '>' : bufdelta=6; strncpy(buf+offset, "\\u003e;", bufdelta);break; |
| 128 |
16 |
| case '<' : bufdelta=6; strncpy(buf+offset, "\\u003c;", bufdelta);break; |
| 129 |
8 |
| case '&' : bufdelta=6; strncpy(buf+offset, "\\u0026;", bufdelta);break; |
| 130 |
8 |
| case '=' : bufdelta=6; strncpy(buf+offset, "\\u003d;", bufdelta);break; |
| 131 |
8 |
| case '-' : bufdelta=6; strncpy(buf+offset, "\\u002d;", bufdelta);break; |
| 132 |
16 |
| case ';' : bufdelta=6; strncpy(buf+offset, "\\u003b;", bufdelta);break; |
| 133 |
8 |
| case '+' : bufdelta=6; strncpy(buf+offset, "\\u002b;", bufdelta);break; |
| 134 |
238 |
| default: *(buf+offset)=curchar; |
| 135 |
|
| } |
| 136 |
372 |
| offset+=bufdelta; |
| 137 |
|
| } |
| 138 |
9 |
| retval.begin=buf; |
| 139 |
9 |
| retval.endnext=buf+offset; |
| 140 |
9 |
| return retval; |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
|
| static |
| 144 |
|
| PSTRING |
| 145 |
18 |
| urlencode_pstring (pbuffer* StrBuffer, PSTRING pstring) { |
| 146 |
18 |
| char* buf=pbuffer_resize(StrBuffer, pstring.endnext-pstring.begin+1+MAX_ESCAPE_SEQ); |
| 147 |
18 |
| const char* curpos=pstring.begin; |
| 148 |
18 |
| size_t offset=0; |
| 149 |
18 |
| size_t buflen=pbuffer_size(StrBuffer); |
| 150 |
|
| PSTRING retval; |
| 151 |
458 |
▶ | while (curpos<pstring.endnext) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curpos<pstring.endnext) { |
440 |
18 |
458 |
|
| 152 |
440 |
| unsigned char curchar=*curpos++; |
| 153 |
440 |
| int bufdelta=1; |
| 154 |
440 |
▶ | if (offset>=buflen-MAX_ESCAPE_SEQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset>=buflen-MAX_ESCAPE_SEQ) { |
0 |
440 |
440 |
|
| 155 |
0 |
| buf=pbuffer_resize(StrBuffer, 2*(offset+MAX_ESCAPE_SEQ)); |
| 156 |
0 |
| buflen=pbuffer_size(StrBuffer); |
| 157 |
|
| } |
| 158 |
|
| /* |
| 159 |
|
| * # do the translation (RFC 2396 ^uric) |
| 160 |
|
| * s!([^a-zA-Z0-9_.\-])!sprintf('%%%02X', $_) |
| 161 |
|
| * note that \- above means -, not '\\' + '-', and '\\' should not be urlencoded. |
| 162 |
|
| * https://rt.cpan.org/Ticket/Display.html?id=109255 |
| 163 |
|
| */ |
| 164 |
440 |
▶ | if ((curchar>='a' && curchar<='z') || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((curchar>='a' && curchar<='z') || |
221 |
219 |
440 |
| if ((curchar>='a' && curchar<='z') || |
0 |
221 |
221 |
| if ((curchar>='a' && curchar<='z') || |
35 |
184 |
219 |
|
| 165 |
219 |
▶ | (curchar>='A' && curchar<='Z') || |
Branch: 2 branches
| Branch | True | False | Total |
| (curchar>='A' && curchar<='Z') || |
8 |
27 |
35 |
| (curchar>='A' && curchar<='Z') || |
75 |
117 |
192 |
|
| 166 |
192 |
▶ | (curchar>='0' && curchar<='9') || |
Branch: 2 branches
| Branch | True | False | Total |
| (curchar>='0' && curchar<='9') || |
75 |
0 |
75 |
| (curchar>='0' && curchar<='9') || |
192 |
0 |
192 |
|
| 167 |
192 |
▶ | curchar=='_' || curchar=='.' || curchar=='-' |
Branch: 2 branches
| Branch | True | False | Total |
| curchar=='_' || curchar=='.' || curchar=='-' |
192 |
0 |
192 |
| curchar=='_' || curchar=='.' || curchar=='-' |
8 |
184 |
192 |
|
| 168 |
|
| ) |
| 169 |
256 |
| *(buf+offset)=curchar; |
| 170 |
|
| else { |
| 171 |
184 |
| bufdelta=3; sprintf(buf+offset,"%%%.2X",(int) curchar); |
| 172 |
|
| } |
| 173 |
440 |
| offset+=bufdelta; |
| 174 |
|
| } |
| 175 |
18 |
| retval.begin=buf; |
| 176 |
18 |
| retval.endnext=buf+offset; |
| 177 |
18 |
| return retval; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| static |
| 181 |
|
| PSTRING |
| 182 |
94 |
| escape_pstring (pbuffer* strBuffer, PSTRING pstring, const int escapeopt) { |
| 183 |
94 |
| switch (escapeopt) { |
| 184 |
67 |
| case HTML_TEMPLATE_OPT_ESCAPE_HTML: |
| 185 |
67 |
| return htmlencode_pstring(strBuffer, pstring); |
| 186 |
9 |
| case HTML_TEMPLATE_OPT_ESCAPE_JS: |
| 187 |
9 |
| return jsencode_pstring(strBuffer, pstring); |
| 188 |
18 |
| case HTML_TEMPLATE_OPT_ESCAPE_URL: |
| 189 |
18 |
| return urlencode_pstring(strBuffer, pstring); |
| 190 |
0 |
| default : return pstring; |
| 191 |
|
| } |
| 192 |
|
| } |