4 * Copyright (c) 2009-2010 project bchan
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
34 #include "sjisstring.h"
36 EXPORT W sjstring_appendasciistring(UB **dest, W *dest_len, UB *str, W len)
42 *dest = realloc(*dest, *dest_len + len + 1);
47 memcpy((*dest)+*dest_len, str, len);
49 (*dest)[*dest_len] = '\0';
54 LOCAL UB dec[] = "0123456789";
56 EXPORT W sjstring_appendUWstring(UB **dest, W *dlen, UW n)
58 W i = 0,digit,draw = 0;
61 digit = n / 1000000000 % 10;
62 if ((digit != 0)||(draw != 0)) {
63 str[i++] = dec[digit];
66 digit = n / 100000000 % 10;
67 if ((digit != 0)||(draw != 0)) {
68 str[i++] = dec[digit];
71 digit = n / 10000000 % 10;
72 if ((digit != 0)||(draw != 0)) {
73 str[i++] = dec[digit];
76 digit = n / 1000000 % 10;
77 if ((digit != 0)||(draw != 0)) {
78 str[i++] = dec[digit];
81 digit = n / 100000 % 10;
82 if ((digit != 0)||(draw != 0)) {
83 str[i++] = dec[digit];
86 digit = n / 10000 % 10;
87 if ((digit != 0)||(draw != 0)) {
88 str[i++] = dec[digit];
91 digit = n / 1000 % 10;
92 if ((digit != 0)||(draw != 0)) {
93 str[i++] = dec[digit];
97 if ((digit != 0)||(draw != 0)) {
98 str[i++] = dec[digit];
102 if ((digit != 0)||(draw != 0)) {
103 str[i++] = dec[digit];
107 str[i++] = dec[digit];
109 return sjstring_appendasciistring(dest, dlen, str, i);
113 LOCAL W urlencode_needlength(UB *bin, W len)
117 for (i=0;i<len;i++) {
118 if (bin[i] == 0x2E) { /* '.' */
120 } else if (bin[i] == 0x2D) { /* '-' */
122 } else if (bin[i] == 0x5F) { /* '_' */
124 } else if (bin[i] == 0x20) { /* ' ' */
126 } else if (isalnum(bin[i])) {
136 LOCAL W urlencode_convert(UB *dest, W dest_len, UB *src, W src_len)
139 static UB num[] = "0123456789ABCDEF";
141 for (i=0;i<src_len;i++) {
142 if (src[i] == 0x2E) { /* '.' */
144 } else if (src[i] == 0x2D) { /* '-' */
146 } else if (src[i] == 0x5F) { /* '_' */
148 } else if (src[i] == 0x20) { /* ' ' */
149 dest[j++] = 0x2B; /* '+' */
150 } else if (isalnum(src[i])) {
153 dest[j++] = 0x25; /* '%' */
157 dest[j++] = num[(src[i] >> 4)];
161 dest[j++] = num[(src[i] & 0xF)];
171 EXPORT W sjstring_appendurlencodestring(UB **dest, W *dest_len, UB *str, W len)
179 encoded_len = urlencode_needlength(str, len);
181 *dest = realloc(*dest, *dest_len + encoded_len + 1);
183 return -1; /* TODO */
186 urlencode_convert(*dest + *dest_len, encoded_len, str, len);
188 *dest_len += encoded_len;
189 (*dest)[*dest_len] = '\0';
194 EXPORT W sjstring_appendconvartingTCstring(UB **dest, W *dest_len, TC *str, W len)
196 W i,j,converted_len = 0, ret;
199 for (i = 0; i < len; i++) {
200 ret = tctosj(NULL, str[i]);
202 return -1; /* TODO */
204 converted_len += ret;
207 *dest = realloc(*dest, *dest_len + converted_len + 1);
209 return -1; /* TODO */
213 for (i = 0; i < len; i++) {
214 ret = tctosj(conv, str[i]);
216 return -1; /* TODO */
218 memcpy(*(dest)+*dest_len+j, conv, ret);
222 *dest_len += converted_len;
223 (*dest)[*dest_len] = '\0';
228 EXPORT W sjstring_appendformpoststring(UB **dest, W *dest_len, UB *str, W len)
234 for (i = 0; i < len; i++) {
236 if (((0x81 <= ch0)&&(ch0 <= 0x9f))
237 ||((0xe0 <= ch0)&&(ch0 <= 0xef))) {
243 err = sjstring_appendurlencodestring(dest, dest_len, str+si, i-si);
247 err = sjstring_appendurlencodestring(dest, dest_len, "<", 4);
252 } else if (ch0 == '>') {
253 err = sjstring_appendurlencodestring(dest, dest_len, str+si, i-si);
257 err = sjstring_appendurlencodestring(dest, dest_len, ">", 4);
262 } else if (ch0 == '"') {
263 err = sjstring_appendurlencodestring(dest, dest_len, str+si, i-si);
267 err = sjstring_appendurlencodestring(dest, dest_len, """, 6);
275 return sjstring_appendurlencodestring(dest, dest_len, str+si, i-si);
278 EXPORT UB* sjstring_searchchar(UB *str, W len, UB ch)
283 /* not lower byte of 2 bytes character in Shift_JIS. */
284 if (!(((0x40 <= ch)&&(ch <= 0x7e))||((0x80 < ch)&&(ch < 0xfc)))) {
285 for (i = 0; i < len; i++) {
289 if (str[i] == '\0') {
296 for (i = 0; i < len; i++) {
298 if (((0x81 <= ch0)&&(ch0 <= 0x9f))
299 ||((0xe0 <= ch0)&&(ch0 <= 0xef))) {
314 EXPORT W sjstring_totcs(UB *sjstr, UB sjstr_len, TC *tcstr)
319 for (i = 0; i < sjstr_len; i++) {
320 ret = sjtotc(NULL, sjstr + i);
338 for (i = 0; i < sjstr_len; i++) {
339 ret = sjtotc(tcstr + tclen, sjstr + i);
358 /* from rfc1738. 2.2. URL Character Encoding Issues */
359 /* should be more speed up. */
360 EXPORT Bool sjstring_isurlusablecharacter(UB ch)
362 /* No corresponding graphic US-ASCII */
363 if ((0x00 <= ch)&&(ch <= 0x1F)) {
369 if ((0x80 <= ch)&&(ch <= 0xFF)) {
412 /* should be ignore? */
413 /* by "APPENDIX: Recommendations for URLs in Context" */
414 /* to handling these, need parsing. */
431 #ifdef BCHAN_CONFIG_DEBUG
434 EXPORT VOID SJSTRING_DP(UB *str, W len)
439 print_len = sjstotcs(NULL, str);
440 print = malloc(sizeof(TC)*(print_len + 1));
445 sjstotcs(print, str);
446 print[print_len] = TNULL;
448 for (i=0;i<print_len;i++) {
449 printf("%C", print[i]);