Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F3318908
utf7.inc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
9 KB
Referenced Files
None
Subscribers
None
utf7.inc
View Options
<
?
php
//
//
// utf7.inc - Routines to encode bytes to UTF7 and decode UTF7 strings
//
// Copyright (C) 1999, 2002 Ziberex and Torben Rybner
//
//
// Version 1.01 2002-06-08 19:00
//
// - Adapted for use in IlohaMail (modified UTF-7 decoding)
// - Converted from C to PHP4
//
//
// Version 1.00 1999-09-03 19:00
//
// - Encodes bytes to UTF7 strings
// *OutString = '\0';
// StartBase64Encode();
// for (CP = InString; *CP; CP++)
// strcat(OutString, Base64Encode(*CP));
// strcat(OutString, StopBase64Encode());
// - Decodes Base64 strings to bytes
// StartBase64Decode();
// for (CP1 = InString, CP2 = OutString; *CP1 && (*CP1 != '='); CP1++)
// CP2 += Base64Decode(*CP1, CP2);
// StopBase64Decode();
//
$
BASE64LENGTH
=
60
;
$
BASE64DECODE_NO_DATA
=
-
1
;
$
BASE64DECODE_EMPTY_DATA
=
-
2
;
$
BASE64DECODE_INVALID_DATA
=
-
3
;
//
//
// Used for conversion to UTF7
//
$
_ToUTF7
=
array
(
'
A
'
,
'
B
'
,
'
C
'
,
'
D
'
,
'
E
'
,
'
F
'
,
'
G
'
,
'
H
'
,
'
I
'
,
'
J
'
,
'
K
'
,
'
L
'
,
'
M
'
,
'
N
'
,
'
O
'
,
'
P
'
,
'
Q
'
,
'
R
'
,
'
S
'
,
'
T
'
,
'
U
'
,
'
V
'
,
'
W
'
,
'
X
'
,
'
Y
'
,
'
Z
'
,
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
,
'
e
'
,
'
f
'
,
'
g
'
,
'
h
'
,
'
i
'
,
'
j
'
,
'
k
'
,
'
l
'
,
'
m
'
,
'
n
'
,
'
o
'
,
'
p
'
,
'
q
'
,
'
r
'
,
'
s
'
,
'
t
'
,
'
u
'
,
'
v
'
,
'
w
'
,
'
x
'
,
'
y
'
,
'
z
'
,
'
0
'
,
'
1
'
,
'
2
'
,
'
3
'
,
'
4
'
,
'
5
'
,
'
6
'
,
'
7
'
,
'
8
'
,
'
9
'
,
'
+
'
,
'
,
'
);
//
//
// Used for conversion from UTF7
// (0x80 => Illegal, 0x40 => CR/LF)
//
$
_FromUTF7
=
array
(
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
// 00 - 07 - Ctrl -
0
x80
,
0
x80
,
0
x40
,
0
x80
,
0
x80
,
0
x40
,
0
x80
,
0
x80
,
// 08 - 0F - Ctrl -
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
// 10 - 17 - Ctrl -
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
// 18 - 1F - Ctrl -
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
// 20 - 27 !"#$%&'
0
x80
,
0
x80
,
0
x80
,
0
x3E
,
0
x3F
,
0
x80
,
0
x80
,
0
x3F
,
// 28 - 2F ()*+,-./
0
x34
,
0
x35
,
0
x36
,
0
x37
,
0
x38
,
0
x39
,
0
x3A
,
0
x3B
,
// 30 - 37 01234567
0
x3C
,
0
x3D
,
0
x80
,
0
x80
,
0
x80
,
0
x00
,
0
x80
,
0
x80
,
// 38 - 3F 89:;<=>?
0
x80
,
0
x00
,
0
x01
,
0
x02
,
0
x03
,
0
x04
,
0
x05
,
0
x06
,
// 40 - 47 @ABCDEFG
0
x07
,
0
x08
,
0
x09
,
0
x0A
,
0
x0B
,
0
x0C
,
0
x0D
,
0
x0E
,
// 48 - 4F HIJKLMNO
0
x0F
,
0
x10
,
0
x11
,
0
x12
,
0
x13
,
0
x14
,
0
x15
,
0
x16
,
// 50 - 57 PQRSTUVW
0
x17
,
0
x18
,
0
x19
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
// 58 - 5F XYZ[\]^_
0
x80
,
0
x1A
,
0
x1B
,
0
x1C
,
0
x1D
,
0
x1E
,
0
x1F
,
0
x20
,
// 60 - 67 `abcdefg
0
x21
,
0
x22
,
0
x23
,
0
x24
,
0
x25
,
0
x26
,
0
x27
,
0
x28
,
// 68 - 6F hijklmno
0
x29
,
0
x2A
,
0
x2B
,
0
x2C
,
0
x2D
,
0
x2E
,
0
x2F
,
0
x30
,
// 70 - 77 pqrstuvw
0
x31
,
0
x32
,
0
x33
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
0
x80
,
// 78 - 7F xyz{|}~
);
//
//
// UTF7EncodeInit:
//
// Start the encoding of bytes
//
function
UTF7EncodeInit
(
&$
Context
)
{
$
Context
[
"Data"
]
=
""
;
$
Context
[
"Count"
]
=
0
;
$
Context
[
"Pos"
]
=
0
;
$
Context
[
"State"
]
=
0
;
}
// UTF7EncodeInit
//
//
// UTF7EncodeByte:
//
// Encodes one byte to UTF7
//
function
UTF7EncodeByte
(
&$
Context
,
$
Byte
)
{
global
$
_ToUTF7
;
$
Byte
=
ord
(
$
Byte
);
switch
(
$
Context
[
"State"
])
{
case
0
:
// Convert into a byte
$
Context
[
"Data"
]
=
$
_ToUTF7
[
$
Byte
>>
2
];
$
Context
[
"Pos"
]
++
;
// Save residue for next converted byte
$
Context
[
"Residue"
]
=
(
$
Byte
&
0
x03
)
<<
4
;
// This is the first byte in this line
$
Context
[
"Count"
]
=
1
;
// Next state is 1
$
Context
[
"State"
]
=
1
;
break
;
case
1
:
// Convert into a byte
$
Context
[
"Data"
]
.
=
$
_ToUTF7
[
$
Context
[
"Residue"
]
|
(
$
Byte
>>
4
)
];
$
Context
[
"Pos"
]
++
;
// Save residue for next converted byte
$
Context
[
"Residue"
]
=
(
$
Byte
&
0
x0F
)
<<
2
;
// Bumb byte counter
$
Context
[
"Count"
]
++
;
// Next state is 2
$
Context
[
"State"
]
=
2
;
break
;
case
2
:
// Convert into a byte
$
Context
[
"Data"
]
.
=
$
_ToUTF7
[
$
Context
[
"Residue"
]
|
(
$
Byte
>>
6
)
];
$
Context
[
"Pos"
]
++
;
// Residue fits precisely into the next byte
$
Context
[
"Data"
]
.
=
$
_ToUTF7
[
$
Byte
&
0
x3F
];
$
Context
[
"Pos"
]
++
;
// Bumb byte counter
$
Context
[
"Count"
]
++
;
// Next state is 3
$
Context
[
"State"
]
=
3
;
break
;
case
3
:
// Convert into a byte
$
Context
[
"Data"
]
.
=
$
_ToUTF7
[
$
Byte
>>
2
];
$
Context
[
"Pos"
]
++
;
// Save residue for next converted byte
$
Context
[
"Residue"
]
=
(
$
Byte
&
0
x03
)
<<
4
;
// Bumb byte counter
$
Context
[
"Count"
]
++
;
// Next state is 1
$
Context
[
"State"
]
=
1
;
break
;
default
:
// printf("Internal error in UTF7Encode: State is %d\n", $Context[ "State" ]);
// exit(1);
break
;
}
}
// UTF7EncodeByte
//
//
// UTF7EncodeFinal:
//
// Terminates the encoding of bytes
//
function
UTF7EncodeFinal
(
&$
Context
)
{
if
(
$
Context
[
"State"
]
==
0
)
return
""
;
if
(
$
Context
[
"State"
]
!
=
3
)
UTF7EncodeByte
(
$
Context
,
"\0"
);
return
$
Context
[
"Data"
];
}
// UTF7EncodeFinal
//
//
// UTF7EncodeString
//
// Encodes a string to modified UTF-7 format
//
function
UTF7EncodeString
(
$
String
)
{
// Not during encoding, yet
$
Encoding
=
false
;
// Go through the string
for
(
$
I
=
0
;
$
I
<
strlen
(
$
String
);
$
I
++
)
{
$
Ch
=
substr
(
$
String
,
$
I
,
1
);
if
(
ord
(
$
Ch
)
>
0
x7F
)
{
if
(
!
$
Encoding
)
{
$
RetVal
.
=
"&"
;
$
Encoding
=
true
;
// Initialise UTF7 context
UTF7EncodeInit
(
$
Context
);
}
UTF7EncodeByte
(
$
Context
,
"\0"
);
UTF7EncodeByte
(
$
Context
,
$
Ch
);
}
elseif
(
$
Ch
==
"&"
)
{
if
(
!
$
Encoding
)
{
$
RetVal
.
=
"&"
;
$
Encoding
=
true
;
// Initialise UTF7 context
UTF7EncodeInit
(
$
Context
);
}
else
{
UTF7EncodeByte
(
$
Context
,
"\0"
);
UTF7EncodeByte
(
$
Context
,
$
Ch
);
}
}
else
{
if
(
$
Encoding
)
{
$
RetVal
.
=
UTF7EncodeFinal
(
$
Context
)
.
"-$Ch"
;
$
Encoding
=
false
;
}
else
$
RetVal
.
=
$
Ch
;
}
}
if
(
$
Encoding
)
$
RetVal
.
=
UTF7EncodeFinal
(
$
Context
)
.
"-"
;
return
$
RetVal
;
}
// UTF7EncodeString
//
//
// UTF7DecodeInit:
//
// Start the decoding of bytes
//
function
UTF7DecodeInit
(
&$
Context
)
{
$
Context
[
"Data"
]
=
""
;
$
Context
[
"State"
]
=
0
;
$
Context
[
"Pos"
]
=
0
;
}
// UTF7DecodeInit
//
//
// UTF7DecodeByte:
//
// Decodes one character from UTF7
//
function
UTF7DecodeByte
(
&$
Context
,
$
Byte
)
{
global
$
BASE64DECODE_INVALID_DATA
;
global
$
_FromUTF7
;
// Restore bits
$
Byte
=
$
_FromUTF7
[
ord
(
$
Byte
)
];
// Ignore carriage returns and linefeeds
if
(
$
Byte
==
0
x40
)
return
""
;
// Invalid byte - Tell caller!
if
(
$
Byte
==
0
x80
)
$
Context
[
"Count"
]
=
$
BASE64DECODE_INVALID_DATA
;
switch
(
$
Context
[
"State"
])
{
case
0
:
// Save residue
$
Context
[
"Residue"
]
=
$
Byte
;
// Initialise count
$
Context
[
"Count"
]
=
0
;
// Next state
$
Context
[
"State"
]
=
1
;
break
;
case
1
:
// Store byte
$
Context
[
"Data"
]
.
=
chr
((
$
Context
[
"Residue"
]
<<
2
)
|
(
$
Byte
>>
4
));
$
Context
[
"Pos"
]
++
;
// Update count
$
Context
[
"Count"
]
++
;
// Save residue
$
Context
[
"Residue"
]
=
$
Byte
;
// Next state
$
Context
[
"State"
]
=
2
;
break
;
case
2
:
// Store byte
$
Context
[
"Data"
]
.
=
chr
((
$
Context
[
"Residue"
]
<<
4
)
|
(
$
Byte
>>
2
));
$
Context
[
"Pos"
]
++
;
// Update count
$
Context
[
"Count"
]
++
;
// Save residue
$
Context
[
"Residue"
]
=
$
Byte
;
// Next state
$
Context
[
"State"
]
=
3
;
break
;
case
3
:
// Store byte
$
Context
[
"Data"
]
.
=
chr
((
$
Context
[
"Residue"
]
<<
6
)
|
$
Byte
);
$
Context
[
"Pos"
]
++
;
// Update count
$
Context
[
"Count"
]
++
;
// Next state
$
Context
[
"State"
]
=
4
;
break
;
case
4
:
// Save residue
$
Context
[
"Residue"
]
=
$
Byte
;
// Next state
$
Context
[
"State"
]
=
1
;
break
;
}
}
// UTF7DecodeByte
//
//
// UTF7DecodeFinal:
//
// Decodes one character from UTF7
//
function
UTF7DecodeFinal
(
&$
Context
)
{
// Buffer not empty - Return remainder!
if
(
$
Context
[
"Count"
])
{
$
Context
[
"Pos"
]
=
0
;
$
Context
[
"State"
]
=
0
;
return
$
Context
[
"Data"
];
}
return
""
;
}
// UTF7DecodeFinal
//
//
// UTF7DecodeString
//
// Converts a string encoded in modified UTF-7 encoding
// to ISO 8859-1.
// OBS: Works only for valid ISO 8859-1 characters in the
// encoded data
//
function
UTF7DecodeString
(
$
String
)
{
$
Decoding
=
false
;
for
(
$
I
=
0
;
$
I
<
strlen
(
$
String
);
$
I
++
)
{
$
Ch
=
substr
(
$
String
,
$
I
,
1
);
if
(
$
Decoding
)
{
if
(
$
Ch
==
"-"
)
{
$
RetVal
.
=
UTF7DecodeFinal
(
$
Context
);
$
Decoding
=
false
;
}
else
UTF7DecodeByte
(
$
Context
,
$
Ch
);
}
elseif
(
$
Ch
==
"&"
)
{
if
((
$
I
<
strlen
(
$
String
)
-
1
)
&&
(
substr
(
$
String
,
$
I
+
1
,
1
)
==
"-"
))
{
$
RetVal
.
=
$
Ch
;
$
I
++
;
}
else
{
UTF7DecodeInit
(
$
Context
);
$
Decoding
=
true
;
}
}
else
$
RetVal
.
=
$
Ch
;
}
return
str_replace
(
"\0"
,
""
,
$
RetVal
);
}
// UTF7DecodeString
?
>
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Thu, Apr 9, 5:47 PM (20 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
517973
Default Alt Text
utf7.inc (9 KB)
Attached To
Mode
R3 roundcubemail
Attached
Detach File
Event Timeline
Log In to Comment