지난번에 적용했던 base64 의 코드를 사용하다가,
'+' 기호부분이 URL 에 적용될 때 ' ' (space 기호) 로 바뀔 수 있다...라는 얘기가 나와서(?)
-)
'+' 기호 갯수를 어떻게 줄일까...를 고민하다가
표시하는 코드의 길이를 줄여보자...는 생각을 하고
열심히(?) 연구했습니다.
원문출처 : - Javascript base64 : WebToolkit - http://www.webtoolkit.info/javascript-base64.html
1차 펌 : http://killofki.tistory.com/entry/펌-base64encodedecode-for-Javascript-함수
...
<script>
var ttt=0;
// pack 'String.fromCharCode()'
function chr110301_1() {
var ar=arguments, ov='', i;
for (i=0; i<ar.length; i++) ov+=String.fromCharCode(ar[i]);
return ov;
}
var
_keyStr110301_1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
function _keyStrCharAt110301_1() {
var ar=arguments, i, ov='';
for (i=0; i<ar.length; i++) ov+=_keyStr110301_1.charAt(ar[i]);
return ov;
}
function base64_encode110301_1(input) {
var enterReg=/\r\n/g, enterChar='\n';
// -- get utf8
function _utf8_char(s) {
// s = s.replace(enterReg, enterChar);
if (!s) return '';
var codeValue;
codeValue = s.charCodeAt(0);
if (codeValue < 128)
return chr110301_1(codeValue);
else if((codeValue > 127) && (codeValue < 2048))
return chr110301_1((codeValue >> 6) | 192, (codeValue & 63) | 128);
return chr110301_1((codeValue >> 12) | 224, ((codeValue >> 6) & 63) | 128, (codeValue & 63) | 128);
}
// -- main variable
var output = '', outbuf
, queue, nextp, cutp, cutl, testv, testp, testl, testl2;
// -- get testc's int value n testp
function pushfromtestPtotestV() {
var testc;
while (testp<input.length) {
testv+=(testc=_utf8_char(input.charAt(testp++)));
if ((testl2-=testc.length)<=0) break;
}
}
// -- get testv to outbuf
function gettestVtooutBuf() {
var i, chr1, chr2, chr3, enc1, enc2, enc3, enc4;
outbuf=''; i=0;
while (i < testv.length) {
// input : charCodeAt(i++)
chr1 = testv.charCodeAt(i++);
chr2 = testv.charCodeAt(i++);
chr3 = testv.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) enc3 = enc4 = 64;
else if (isNaN(chr3)) enc4 = 64;
outbuf +=_keyStrCharAt110301_1(enc1, enc2, enc3, enc4);
//
}
}
// -- put outbuf to output
function putoutBuftoOutput() {
output+=outbuf.substr(0, 4);
queue=testv.substring(3, cutl);
}
// -- main process
// input = _utf8_encode(input);
nextp=0;
while (nextp<input.length) {
if (!queue)
while (nextp<input.length)
if (queue=_utf8_char(input.charAt(nextp++))) break;
testl=6-queue.length;
while (testl>0) { // start in testl
// process by testl length
testv=queue; testp=nextp;
testl2=testl<3?testl:3;
pushfromtestPtotestV();
cutp=testp; cutl=testv.length;
testl2=testl-cutl;
pushfromtestPtotestV();
gettestVtooutBuf();
if (outbuf.indexOf('+')<0)
nextp=cutp;
else {
if ((--testl)>0) continue;
testv=queue;
gettestVtooutBuf();
}
putoutBuftoOutput();
break;
} // end of testl
} // end of nextp
if (queue) {
testv=queue;
gettestVtooutBuf();
putoutBuftoOutput();
}
return output;
}
function base64_decode110301_1(input) {
// -- set function for optimizng
function _keyStrindexOfinputcharAt(p) {
return _keyStr110301_1.indexOf(input.charAt(p));
}
// -- put char from utf8
function _utf8_decode (utftext) {
var s = '', i = 0, c, c2, c3;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
s += chr110301_1(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
s += chr110301_1(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
s += chr110301_1(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return s;
}
// -- main process
var output = ''
,chr1, chr2, chr3
,enc1, enc2, enc3, enc4
, i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
while (i < input.length) {
enc1 = _keyStrindexOfinputcharAt(i++);
enc2 = _keyStrindexOfinputcharAt(i++);
enc3 = _keyStrindexOfinputcharAt(i++);
enc4 = _keyStrindexOfinputcharAt(i++);
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output += chr110301_1(chr1);
if (enc3 != 64) output += chr110301_1(chr2);
if (enc4 != 64) output += chr110301_1(chr3);
}
output = _utf8_decode(output);
return output;
}
</script>
<div>
<input value="@killofki@ <110213 코엑스나들이 Summery...:http://www.cyworld.com/killofki_/4249509> 코엑스에 가서 전시회 관람 끝내고 한바퀴₍?₎ 돌아봤습니다. 일반인으로써는 이벤트가 보이는 게 좋아보였죠. smallLet killofki〶. 출처 ː -- 110213 코엑스나들이 Summery... " />
<iinput value="09>" />
<input type=button value="encoding" onclick="var p=this.parentNode, e=p.getElementsByTagName('input')[0]; e.value=base64_encode110301_1(e.value);" />
<input type=button value="decoding" onclick="var p=this.parentNode, e=p.getElementsByTagName('input')[0]; e.value=base64_decode110301_1(e.value);" />
<input type=button value="deErrcoding" onclick="var p=this.parentNode, e=p.getElementsByTagName('input')[0]; alert(e.value.replace(new RegExp('\\+', 'gi'), ' ')); e.value=base64_decode110301_1(e.value.replace(new RegExp('\\+', 'gi'), ' '));" />
</div>
...
...
0)
일단, 기존에 string 으로 전부 치환하던 시스템(?)을
각개격파(?)를 위해(?) 줄여서 받...아봤습니다만,
OTL... 그냥 UTF8 로 받고
pointer 를 잡아서 나눠처리하는 방법이 훨씬 빨랐겠군요...
(괜히 분할해서 처리하는 형식을 집어넣어 복잡해지기만 했음.)
...라고 적고서 다시 생각해보니
"그러면 원래 표시하던 글자 분리가 안되잖아!" 라는 결과가 나오네요.
-.-... 이렇게 만들려고 고민해서(?) 다행입니다...
...
메인함수에서는
1)
일단 utf8 코드를 6개 이상(?) 모으도록 했습니다.
3개만 처리할까...하다가,
첫번째 코드가 무사통과(?)되더라도,
두번째 코드까지 무사할 수 있다...라는 가정은 못세우겠더라구요.
...
그 다음은,
2)
base64 로 encoding 하고,
encoding 된 값에서 '+' 기호를 발견하면 "글자"를 한개씩 줄입니다.
어찌어찌하다보니 이렇게 해결이 되네요.
3)
물론, 중복된 loop 가 있을겁니다.
5개 -> 2개... 로 넘어가지 않는 부분이겠죠.
부분을
로 바꾸는 거라 생각됩니다만...
= =;... 테스트 안해봤으니 그냥(?) 넘어갑니다.
( pushfromtestPtotestV() 참고 )
...
4)
중간에 cutp, cutl 이라는 부분이 있는데요,
간단히 말하자면, '최소출력값'입니다.
queue 가 이전 글자의 이어지는 utf8 값을 보유하고 있는데,
cutp 와 cutl 은 base64 로 네글자를 전송한 후(?),
남을(?) 값을 측정하기 위한 변수입니다.
그래서(?),
이라고 적어놓은거죠.
* base64 가 2^6=64 라는 뜻과 6bit*4 라는 뜻을 모두(?) 포함하는 것으로 보고있습니다.
( pushfromtestPtotestV() 참고 )
...
5)
초기에
라는 부분도 있습니다.
queue 값이 절실(?)하게 필요하다는 생각을 한 이유는,
= =;... 안하면 '+' 기호 skip 하다가 무한루프를 돌릴 것 같아서요...
...
6)
마지막(?)으로,
"deErrcoding" 이라는 부분이 있는데요,
"+" 기호가 " " (space 기호) 로 변했을 때 나올 수 있는 효과(?)를
가식적(?)으로 표현해본겁니다.
위에 나온 함수로는 + 기호 구하기가 어려워(?)서
테스트가 안되겠지만요...
...
7)
일단 이렇게 코딩(?)은 성공(?)을 했는데...
= =;... 이런식으로 줄여도...
실제 사용하려던 디코더쪽에서 받지 않으면 대략난감...해진 결과를 봤기에
좀 아쉽네요...
...
smallLet killofki@.
'+' 기호부분이 URL 에 적용될 때 ' ' (space 기호) 로 바뀔 수 있다...라는 얘기가 나와서(?)
-)
'+' 기호 갯수를 어떻게 줄일까...를 고민하다가
표시하는 코드의 길이를 줄여보자...는 생각을 하고
열심히(?) 연구했습니다.
원문출처 : - Javascript base64 : WebToolkit - http://www.webtoolkit.info/javascript-base64.html
1차 펌 : http://killofki.tistory.com/entry/펌-base64encodedecode-for-Javascript-함수
...
<script>
var ttt=0;
// pack 'String.fromCharCode()'
function chr110301_1() {
var ar=arguments, ov='', i;
for (i=0; i<ar.length; i++) ov+=String.fromCharCode(ar[i]);
return ov;
}
var
_keyStr110301_1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
function _keyStrCharAt110301_1() {
var ar=arguments, i, ov='';
for (i=0; i<ar.length; i++) ov+=_keyStr110301_1.charAt(ar[i]);
return ov;
}
function base64_encode110301_1(input) {
var enterReg=/\r\n/g, enterChar='\n';
// -- get utf8
function _utf8_char(s) {
// s = s.replace(enterReg, enterChar);
if (!s) return '';
var codeValue;
codeValue = s.charCodeAt(0);
if (codeValue < 128)
return chr110301_1(codeValue);
else if((codeValue > 127) && (codeValue < 2048))
return chr110301_1((codeValue >> 6) | 192, (codeValue & 63) | 128);
return chr110301_1((codeValue >> 12) | 224, ((codeValue >> 6) & 63) | 128, (codeValue & 63) | 128);
}
// -- main variable
var output = '', outbuf
, queue, nextp, cutp, cutl, testv, testp, testl, testl2;
// -- get testc's int value n testp
function pushfromtestPtotestV() {
var testc;
while (testp<input.length) {
testv+=(testc=_utf8_char(input.charAt(testp++)));
if ((testl2-=testc.length)<=0) break;
}
}
// -- get testv to outbuf
function gettestVtooutBuf() {
var i, chr1, chr2, chr3, enc1, enc2, enc3, enc4;
outbuf=''; i=0;
while (i < testv.length) {
// input : charCodeAt(i++)
chr1 = testv.charCodeAt(i++);
chr2 = testv.charCodeAt(i++);
chr3 = testv.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) enc3 = enc4 = 64;
else if (isNaN(chr3)) enc4 = 64;
outbuf +=_keyStrCharAt110301_1(enc1, enc2, enc3, enc4);
//
}
}
// -- put outbuf to output
function putoutBuftoOutput() {
output+=outbuf.substr(0, 4);
queue=testv.substring(3, cutl);
}
// -- main process
// input = _utf8_encode(input);
nextp=0;
while (nextp<input.length) {
if (!queue)
while (nextp<input.length)
if (queue=_utf8_char(input.charAt(nextp++))) break;
testl=6-queue.length;
while (testl>0) { // start in testl
// process by testl length
testv=queue; testp=nextp;
testl2=testl<3?testl:3;
pushfromtestPtotestV();
cutp=testp; cutl=testv.length;
testl2=testl-cutl;
pushfromtestPtotestV();
gettestVtooutBuf();
if (outbuf.indexOf('+')<0)
nextp=cutp;
else {
if ((--testl)>0) continue;
testv=queue;
gettestVtooutBuf();
}
putoutBuftoOutput();
break;
} // end of testl
} // end of nextp
if (queue) {
testv=queue;
gettestVtooutBuf();
putoutBuftoOutput();
}
return output;
}
function base64_decode110301_1(input) {
// -- set function for optimizng
function _keyStrindexOfinputcharAt(p) {
return _keyStr110301_1.indexOf(input.charAt(p));
}
// -- put char from utf8
function _utf8_decode (utftext) {
var s = '', i = 0, c, c2, c3;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
s += chr110301_1(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
s += chr110301_1(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
s += chr110301_1(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return s;
}
// -- main process
var output = ''
,chr1, chr2, chr3
,enc1, enc2, enc3, enc4
, i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
while (i < input.length) {
enc1 = _keyStrindexOfinputcharAt(i++);
enc2 = _keyStrindexOfinputcharAt(i++);
enc3 = _keyStrindexOfinputcharAt(i++);
enc4 = _keyStrindexOfinputcharAt(i++);
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output += chr110301_1(chr1);
if (enc3 != 64) output += chr110301_1(chr2);
if (enc4 != 64) output += chr110301_1(chr3);
}
output = _utf8_decode(output);
return output;
}
</script>
<div>
<input value="@killofki@ <110213 코엑스나들이 Summery...:http://www.cyworld.com/killofki_/4249509> 코엑스에 가서 전시회 관람 끝내고 한바퀴₍?₎ 돌아봤습니다. 일반인으로써는 이벤트가 보이는 게 좋아보였죠. smallLet killofki〶. 출처 ː -- 110213 코엑스나들이 Summery... " />
<iinput value="09>" />
<input type=button value="encoding" onclick="var p=this.parentNode, e=p.getElementsByTagName('input')[0]; e.value=base64_encode110301_1(e.value);" />
<input type=button value="decoding" onclick="var p=this.parentNode, e=p.getElementsByTagName('input')[0]; e.value=base64_decode110301_1(e.value);" />
<input type=button value="deErrcoding" onclick="var p=this.parentNode, e=p.getElementsByTagName('input')[0]; alert(e.value.replace(new RegExp('\\+', 'gi'), ' ')); e.value=base64_decode110301_1(e.value.replace(new RegExp('\\+', 'gi'), ' '));" />
</div>
...
...
0)
일단, 기존에 string 으로 전부 치환하던 시스템(?)을
각개격파(?)를 위해(?) 줄여서 받...아봤습니다만,
OTL... 그냥 UTF8 로 받고
pointer 를 잡아서 나눠처리하는 방법이 훨씬 빨랐겠군요...
(괜히 분할해서 처리하는 형식을 집어넣어 복잡해지기만 했음.)
...라고 적고서 다시 생각해보니
"그러면 원래 표시하던 글자 분리가 안되잖아!" 라는 결과가 나오네요.
-.-... 이렇게 만들려고 고민해서(?) 다행입니다...
...
메인함수에서는
1)
일단 utf8 코드를 6개 이상(?) 모으도록 했습니다.
testl=6-queue.length;
3개만 처리할까...하다가,
첫번째 코드가 무사통과(?)되더라도,
두번째 코드까지 무사할 수 있다...라는 가정은 못세우겠더라구요.
...
그 다음은,
2)
base64 로 encoding 하고,
encoding 된 값에서 '+' 기호를 발견하면 "글자"를 한개씩 줄입니다.
while (testl>0) {
...
if (outbuf.indexOf('+')<0)
nextp=cutp;
else {
if ((--testl)>0) continue;
...
}
...
break;
}
...
if (outbuf.indexOf('+')<0)
nextp=cutp;
else {
if ((--testl)>0) continue;
...
}
...
break;
}
어찌어찌하다보니 이렇게 해결이 되네요.
3)
물론, 중복된 loop 가 있을겁니다.
5개 -> 2개... 로 넘어가지 않는 부분이겠죠.
if ((--testl)>0) continue;
부분을
if ((testl-=testc.length)>0) continue;
로 바꾸는 거라 생각됩니다만...
= =;... 테스트 안해봤으니 그냥(?) 넘어갑니다.
( pushfromtestPtotestV() 참고 )
...
4)
중간에 cutp, cutl 이라는 부분이 있는데요,
간단히 말하자면, '최소출력값'입니다.
queue 가 이전 글자의 이어지는 utf8 값을 보유하고 있는데,
cutp 와 cutl 은 base64 로 네글자를 전송한 후(?),
남을(?) 값을 측정하기 위한 변수입니다.
그래서(?),
testl2=testl<3?testl:3;
이라고 적어놓은거죠.
* base64 가 2^6=64 라는 뜻과 6bit*4 라는 뜻을 모두(?) 포함하는 것으로 보고있습니다.
( pushfromtestPtotestV() 참고 )
...
5)
초기에
if (!queue)
while (nextp<input.length)
if (queue=_utf8_char(input.charAt(nextp++))) break;
while (nextp<input.length)
if (queue=_utf8_char(input.charAt(nextp++))) break;
라는 부분도 있습니다.
queue 값이 절실(?)하게 필요하다는 생각을 한 이유는,
= =;... 안하면 '+' 기호 skip 하다가 무한루프를 돌릴 것 같아서요...
...
6)
마지막(?)으로,
"deErrcoding" 이라는 부분이 있는데요,
"+" 기호가 " " (space 기호) 로 변했을 때 나올 수 있는 효과(?)를
가식적(?)으로 표현해본겁니다.
위에 나온 함수로는 + 기호 구하기가 어려워(?)서
테스트가 안되겠지만요...
...
7)
일단 이렇게 코딩(?)은 성공(?)을 했는데...
= =;... 이런식으로 줄여도...
실제 사용하려던 디코더쪽에서 받지 않으면 대략난감...해진 결과를 봤기에
좀 아쉽네요...
...
smallLet killofki@.
'string' 카테고리의 다른 글
RegExp] 태그 범위를 받아봅니다. (0) | 2013.06.06 |
---|---|
tag] html 소스 태그 삭제용 스크립트.. (for IE8) (0) | 2012.11.20 |
펌] base64_encode/decode for Javascript 함수... (0) | 2011.02.16 |
String.replace()]...를 여러번 쓰려던 함수 하나... (0) | 2010.11.26 |
심심풀이] 상하반전문자를 만들어볼까... (0) | 2010.05.12 |