File: pstrutils.inc

package info (click to toggle)
libhtml-template-pro-perl 0.9524-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,512 kB
  • sloc: ansic: 1,841; perl: 1,474; yacc: 404; pascal: 118; makefile: 6
file content (192 lines) | stat: -rw-r--r-- 5,786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* -*- c -*- 
 * File: pstring.h
 * Author: Igor Vlasenko <vlasenko@imath.kiev.ua>
 * Created: Fri Jul  1 20:11:51 2005
 *
 * $Id$
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "pbuffer.h"
#include "pstring.h"

static
PSTRING
lowercase_pstring (pbuffer* pBuffer ,PSTRING pstring) {
  const size_t size=pstring.endnext-pstring.begin;
  char* buf=pbuffer_resize(pBuffer, size+1);
  char* inbuf=buf;
  const char* i=pstring.begin;
  PSTRING retval;
  while (i<pstring.endnext) {
    *inbuf++=tolower((unsigned char) *i++);
  }
  *inbuf=0;
  retval.begin=buf;
  retval.endnext=buf+size;
  return retval;
}

/*
static
void 
lowercase_pstring_inplace (PSTRING pstring) {
  char* i=pstring.begin;
  while (i<pstring.endnext) {
    *i=tolower(*i);
    i++;
  }
}
*/

static
PSTRING
uppercase_pstring (pbuffer* pBuffer ,PSTRING pstring) {
  const size_t size=pstring.endnext-pstring.begin;
  char* buf=pbuffer_resize(pBuffer, size+1);
  char* inbuf=buf;
  const char* i=pstring.begin;
  PSTRING retval;
  while (i<pstring.endnext) {
    *inbuf++=toupper((unsigned char) *i++);
  }
  *inbuf=0;
  retval.begin=buf;
  retval.endnext=buf+size;
  return retval;
}


static
int
is_pstring_true (PSTRING s) {
  const size_t len = s.endnext-s.begin;
  if (s.begin == NULL || 0==len) return 0;
  if (1==len) {
    if (*(s.begin)=='0') return 0; else return 1;
  } else if (3==len) {
    if ('0'==*(s.begin) && '.'==*(s.begin+1) && '0'==*(s.begin+2)) return 0; else return 1;
  } else return 1;
}

#define MAX_ESCAPE_SEQ sizeof("&quot;")
static 
PSTRING 
htmlencode_pstring (pbuffer* StrBuffer, PSTRING pstring) {
  char* buf=pbuffer_resize(StrBuffer, pstring.endnext-pstring.begin+1+MAX_ESCAPE_SEQ);
  const char* curpos=pstring.begin;
  size_t offset=0;
  size_t buflen=pbuffer_size(StrBuffer);
  PSTRING retval;
  while (curpos<pstring.endnext) {
    unsigned char curchar=*curpos++;
    int bufdelta=1;
    if (offset>=buflen-MAX_ESCAPE_SEQ) {
      buf=pbuffer_resize(StrBuffer, 2*(offset+MAX_ESCAPE_SEQ));
      buflen=pbuffer_size(StrBuffer);
    }
    switch (curchar) {
      /* straight from the CGI.pm bible. (HTML::Template) */
    case '&' : bufdelta=5; strncpy(buf+offset, "&amp;", bufdelta);break;
    case '"' : bufdelta=6; strncpy(buf+offset, "&quot;",bufdelta);break;
    case '>' : bufdelta=4; strncpy(buf+offset, "&gt;",  bufdelta);break;
    case '<' : bufdelta=4; strncpy(buf+offset, "&lt;",  bufdelta);break;
    case '\'': bufdelta=5; strncpy(buf+offset, "&#39;", bufdelta);break;
    default: *(buf+offset)=curchar;
    }
    offset+=bufdelta;
  }
  retval.begin=buf;
  retval.endnext=buf+offset;
  return retval;
}

static 
PSTRING 
jsencode_pstring (pbuffer* StrBuffer, PSTRING pstring) {
  char* buf=pbuffer_resize(StrBuffer, pstring.endnext-pstring.begin+1+MAX_ESCAPE_SEQ);
  const char* curpos=pstring.begin;
  size_t offset=0;
  size_t buflen=pbuffer_size(StrBuffer);
  PSTRING retval;
  while (curpos<pstring.endnext) {
    unsigned char curchar=*curpos++;
    int bufdelta=1;
    if (offset>=buflen-MAX_ESCAPE_SEQ) {
      buf=pbuffer_resize(StrBuffer, 2*(offset+MAX_ESCAPE_SEQ));
      buflen=pbuffer_size(StrBuffer);
    }
    switch (curchar) {
    case '\\' : bufdelta=6; strncpy(buf+offset, "\\u005c", bufdelta);break;
    case '"'  : bufdelta=6; strncpy(buf+offset, "\\u0022",bufdelta);break;
    case '\'' : bufdelta=6; strncpy(buf+offset, "\\u0027",bufdelta);break;
    case '\n' : bufdelta=6; strncpy(buf+offset, "\\u000a",bufdelta);break;
    case '\r' : bufdelta=6; strncpy(buf+offset, "\\u000d",bufdelta);break;
    case '>' : bufdelta=6; strncpy(buf+offset, "\\u003e;",  bufdelta);break;
    case '<' : bufdelta=6; strncpy(buf+offset, "\\u003c;",  bufdelta);break;
    case '&' : bufdelta=6; strncpy(buf+offset, "\\u0026;",  bufdelta);break;
    case '=' : bufdelta=6; strncpy(buf+offset, "\\u003d;",  bufdelta);break;
    case '-' : bufdelta=6; strncpy(buf+offset, "\\u002d;",  bufdelta);break;
    case ';' : bufdelta=6; strncpy(buf+offset, "\\u003b;",  bufdelta);break;
    case '+' : bufdelta=6; strncpy(buf+offset, "\\u002b;",  bufdelta);break;
    default: *(buf+offset)=curchar;
    }
    offset+=bufdelta;
  }
  retval.begin=buf;
  retval.endnext=buf+offset;
  return retval;
}

static 
PSTRING 
urlencode_pstring (pbuffer* StrBuffer, PSTRING pstring) {
  char* buf=pbuffer_resize(StrBuffer, pstring.endnext-pstring.begin+1+MAX_ESCAPE_SEQ);
  const char* curpos=pstring.begin;
  size_t offset=0;
  size_t buflen=pbuffer_size(StrBuffer);
  PSTRING retval;
  while (curpos<pstring.endnext) {
    unsigned char curchar=*curpos++;
    int bufdelta=1;
    if (offset>=buflen-MAX_ESCAPE_SEQ) {
      buf=pbuffer_resize(StrBuffer, 2*(offset+MAX_ESCAPE_SEQ));
      buflen=pbuffer_size(StrBuffer);
    }
    /*
     * # do the translation (RFC 2396 ^uric)
     * s!([^a-zA-Z0-9_.\-])!sprintf('%%%02X', $_)
     * note that \- above means -, not '\\' + '-', and '\\' should not be urlencoded.
     * https://rt.cpan.org/Ticket/Display.html?id=109255
     */
    if ((curchar>='a' && curchar<='z') ||
	(curchar>='A' && curchar<='Z') ||
	(curchar>='0' && curchar<='9') ||
	curchar=='_' || curchar=='.' || curchar=='-'
	)
      *(buf+offset)=curchar;
    else {
      bufdelta=3; sprintf(buf+offset,"%%%.2X",(int) curchar);
    }
    offset+=bufdelta;
  }
  retval.begin=buf;
  retval.endnext=buf+offset;
  return retval;
}

static 
PSTRING 
escape_pstring (pbuffer* strBuffer, PSTRING pstring, const int escapeopt) {
  switch (escapeopt) {
  case HTML_TEMPLATE_OPT_ESCAPE_HTML:
    return htmlencode_pstring(strBuffer, pstring);
  case HTML_TEMPLATE_OPT_ESCAPE_JS:
    return jsencode_pstring(strBuffer, pstring);
  case HTML_TEMPLATE_OPT_ESCAPE_URL: 
    return urlencode_pstring(strBuffer, pstring);
  default : return pstring;
  }
}