rfc2192.IMAP_URL_scheme.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. Network Working Group C. Newman
  2. Request for Comments: 2192 Innosoft
  3. Category: Standards Track September 1997
  4. IMAP URL Scheme
  5. Status of this memo
  6. This document specifies an Internet standards track protocol for
  7. the Internet community, and requests discussion and suggestions for
  8. improvements. Please refer to the current edition of the "Internet
  9. Official Protocol Standards" (STD 1) for the standardization state
  10. and status of this protocol. Distribution of this memo is
  11. unlimited.
  12. Abstract
  13. IMAP [IMAP4] is a rich protocol for accessing remote message
  14. stores. It provides an ideal mechanism for accessing public
  15. mailing list archives as well as private and shared message stores.
  16. This document defines a URL scheme for referencing objects on an
  17. IMAP server.
  18. 1. Conventions used in this document
  19. The key words "MUST", "MUST NOT", "SHOULD", "SHOULD NOT", and "MAY"
  20. in this document are to be interpreted as defined in "Key words for
  21. use in RFCs to Indicate Requirement Levels" [KEYWORDS].
  22. 2. IMAP scheme
  23. The IMAP URL scheme is used to designate IMAP servers, mailboxes,
  24. messages, MIME bodies [MIME], and search programs on Internet hosts
  25. accessible using the IMAP protocol.
  26. The IMAP URL follows the common Internet scheme syntax as defined
  27. in RFC 1738 [BASIC-URL] except that clear text passwords are not
  28. permitted. If :<port> is omitted, the port defaults to 143.
  29. Newman Standards Track [Page 1]
  30. RFC 2192 IMAP URL Scheme September 1997
  31. An IMAP URL takes one of the following forms:
  32. imap://<iserver>/
  33. imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  34. imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  35. imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  36. The first form is used to refer to an IMAP server, the second form
  37. refers to a list of mailboxes, the third form refers to the
  38. contents of a mailbox or a set of messages resulting from a search,
  39. and the final form refers to a specific message or message part.
  40. Note that the syntax here is informal. The authoritative formal
  41. syntax for IMAP URLs is defined in section 11.
  42. 3. IMAP User Name and Authentication Mechanism
  43. A user name and/or authentication mechanism may be supplied. They
  44. are used in the "LOGIN" or "AUTHENTICATE" commands after making the
  45. connection to the IMAP server. If no user name or authentication
  46. mechanism is supplied, the user name "anonymous" is used with the
  47. "LOGIN" command and the password is supplied as the Internet e-mail
  48. address of the end user accessing the resource. If the URL doesn't
  49. supply a user name, the program interpreting the IMAP URL SHOULD
  50. request one from the user if necessary.
  51. An authentication mechanism can be expressed by adding
  52. ";AUTH=<enc_auth_type>" to the end of the user name. When such an
  53. <enc_auth_type> is indicated, the client SHOULD request appropriate
  54. credentials from that mechanism and use the "AUTHENTICATE" command
  55. instead of the "LOGIN" command. If no user name is specified, one
  56. SHOULD be obtained from the mechanism or requested from the user as
  57. appropriate.
  58. The string ";AUTH=*" indicates that the client SHOULD select an
  59. appropriate authentication mechanism. It MAY use any mechanism
  60. listed in the CAPABILITY command or use an out of band security
  61. service resulting in a PREAUTH connection. If no user name is
  62. specified and no appropriate authentication mechanisms are
  63. available, the client SHOULD fall back to anonymous login as
  64. described above. This allows a URL which grants read-write access
  65. to authorized users, and read-only anonymous access to other users.
  66. If a user name is included with no authentication mechanism, then
  67. ";AUTH=*" is assumed.
  68. Newman Standards Track [Page 2]
  69. RFC 2192 IMAP URL Scheme September 1997
  70. Since URLs can easily come from untrusted sources, care must be
  71. taken when resolving a URL which requires or requests any sort of
  72. authentication. If authentication credentials are supplied to the
  73. wrong server, it may compromise the security of the user's account.
  74. The program resolving the URL should make sure it meets at least
  75. one of the following criteria in this case:
  76. (1) The URL comes from a trusted source, such as a referral server
  77. which the client has validated and trusts according to site policy.
  78. Note that user entry of the URL may or may not count as a trusted
  79. source, depending on the experience level of the user and site
  80. policy.
  81. (2) Explicit local site policy permits the client to connect to the
  82. server in the URL. For example, if the client knows the site
  83. domain name, site policy may dictate that any hostname ending in
  84. that domain is trusted.
  85. (3) The user confirms that connecting to that domain name with the
  86. specified credentials and/or mechanism is permitted.
  87. (4) A mechanism is used which validates the server before passing
  88. potentially compromising client credentials.
  89. (5) An authentication mechanism is used which will not reveal
  90. information to the server which could be used to compromise future
  91. connections.
  92. URLs which do not include a user name must be treated with extra
  93. care, since they are more likely to compromise the user's primary
  94. account. A URL containing ";AUTH=*" must also be treated with
  95. extra care since it might fall back on a weaker security mechanism.
  96. Finally, clients are discouraged from using a plain text password
  97. as a fallback with ";AUTH=*" unless the connection has strong
  98. encryption (e.g. a key length of greater than 56 bits).
  99. A program interpreting IMAP URLs MAY cache open connections to an
  100. IMAP server for later re-use. If a URL contains a user name, only
  101. connections authenticated as that user may be re-used. If a URL
  102. does not contain a user name or authentication mechanism, then only
  103. an anonymous connection may be re-used. If a URL contains an
  104. authentication mechanism without a user name, then any non-
  105. anonymous connection may be re-used.
  106. Note that if unsafe or reserved characters such as " " or ";" are
  107. present in the user name or authentication mechanism, they MUST be
  108. encoded as described in RFC 1738 [BASIC-URL].
  109. Newman Standards Track [Page 3]
  110. RFC 2192 IMAP URL Scheme September 1997
  111. 4. IMAP server
  112. An IMAP URL referring to an IMAP server has the following form:
  113. imap://<iserver>/
  114. A program interpreting this URL would issue the standard set of
  115. commands it uses to present a view of the contents of an IMAP
  116. server. This is likely to be semanticly equivalent to one of the
  117. following URLs:
  118. imap://<iserver>/;TYPE=LIST
  119. imap://<iserver>/;TYPE=LSUB
  120. The program interpreting this URL SHOULD use the LSUB form if it
  121. supports mailbox subscriptions.
  122. 5. Lists of mailboxes
  123. An IMAP URL referring to a list of mailboxes has the following
  124. form:
  125. imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  126. The <list_type> may be either "LIST" or "LSUB", and is case
  127. insensitive. The field ";TYPE=<list_type>" MUST be included.
  128. The <enc_list_mailbox> is any argument suitable for the
  129. list_mailbox field of the IMAP [IMAP4] LIST or LSUB commands. The
  130. field <enc_list_mailbox> may be omitted, in which case the program
  131. interpreting the IMAP URL may use "*" or "%" as the
  132. <enc_list_mailbox>. The program SHOULD use "%" if it supports a
  133. hierarchical view, otherwise it SHOULD use "*".
  134. Note that if unsafe or reserved characters such as " " or "%" are
  135. present in <enc_list_mailbox> they MUST be encoded as described in
  136. RFC 1738 [BASIC-URL]. If the character "/" is present in
  137. enc_list_mailbox, it SHOULD NOT be encoded.
  138. 6. Lists of messages
  139. An IMAP URL referring to a list of messages has the following form:
  140. imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  141. Newman Standards Track [Page 4]
  142. RFC 2192 IMAP URL Scheme September 1997
  143. The <enc_mailbox> field is used as the argument to the IMAP4
  144. "SELECT" command. Note that if unsafe or reserved characters such
  145. as " ", ";", or "?" are present in <enc_mailbox> they MUST be
  146. encoded as described in RFC 1738 [BASIC-URL]. If the character "/"
  147. is present in enc_mailbox, it SHOULD NOT be encoded.
  148. The [uidvalidity] field is optional. If it is present, it MUST be
  149. the argument to the IMAP4 UIDVALIDITY status response at the time
  150. the URL was created. This SHOULD be used by the program
  151. interpreting the IMAP URL to determine if the URL is stale.
  152. The [?<enc_search>] field is optional. If it is not present, the
  153. contents of the mailbox SHOULD be presented by the program
  154. interpreting the URL. If it is present, it SHOULD be used as the
  155. arguments following an IMAP4 SEARCH command with unsafe characters
  156. such as " " (which are likely to be present in the <enc_search>)
  157. encoded as described in RFC 1738 [BASIC-URL].
  158. 7. A specific message or message part
  159. An IMAP URL referring to a specific message or message part has the
  160. following form:
  161. imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  162. The <enc_mailbox> and [uidvalidity] are as defined above.
  163. If [uidvalidity] is present in this form, it SHOULD be used by the
  164. program interpreting the URL to determine if the URL is stale.
  165. The <iuid> refers to an IMAP4 message UID, and SHOULD be used as
  166. the <set> argument to the IMAP4 "UID FETCH" command.
  167. The [isection] field is optional. If not present, the URL refers
  168. to the entire Internet message as returned by the IMAP command "UID
  169. FETCH <uid> BODY.PEEK[]". If present, the URL refers to the object
  170. returned by a "UID FETCH <uid> BODY.PEEK[<section>]" command. The
  171. type of the object may be determined with a "UID FETCH <uid>
  172. BODYSTRUCTURE" command and locating the appropriate part in the
  173. resulting BODYSTRUCTURE. Note that unsafe characters in [isection]
  174. MUST be encoded as described in [BASIC-URL].
  175. Newman Standards Track [Page 5]
  176. RFC 2192 IMAP URL Scheme September 1997
  177. 8. Relative IMAP URLs
  178. Relative IMAP URLs are permitted and are resolved according to the
  179. rules defined in RFC 1808 [REL-URL] with one exception. In IMAP
  180. URLs, parameters are treated as part of the normal path with
  181. respect to relative URL resolution. This is believed to be the
  182. behavior of the installed base and is likely to be documented in a
  183. future revision of the relative URL specification.
  184. The following observations are also important:
  185. The <iauth> grammar element is considered part of the user name for
  186. purposes of resolving relative IMAP URLs. This means that unless a
  187. new login/server specification is included in the relative URL, the
  188. authentication mechanism is inherited from a base IMAP URL.
  189. URLs always use "/" as the hierarchy delimiter for the purpose of
  190. resolving paths in relative URLs. IMAP4 permits the use of any
  191. hierarchy delimiter in mailbox names. For this reason, relative
  192. mailbox paths will only work if the mailbox uses "/" as the
  193. hierarchy delimiter. Relative URLs may be used on mailboxes which
  194. use other delimiters, but in that case, the entire mailbox name
  195. MUST be specified in the relative URL or inherited as a whole from
  196. the base URL.
  197. The base URL for a list of mailboxes or messages which was referred
  198. to by an IMAP URL is always the referring IMAP URL itself. The
  199. base URL for a message or message part which was referred to by an
  200. IMAP URL may be more complicated to determine. The program
  201. interpreting the relative URL will have to check the headers of the
  202. MIME entity and any enclosing MIME entities in order to locate the
  203. "Content-Base" and "Content-Location" headers. These headers are
  204. used to determine the base URL as defined in [HTTP]. For example,
  205. if the referring IMAP URL contains a "/;SECTION=1.2" parameter,
  206. then the MIME headers for section 1.2, for section 1, and for the
  207. enclosing message itself SHOULD be checked in that order for
  208. "Content-Base" or "Content-Location" headers.
  209. 9. Multinational Considerations
  210. IMAP4 [IMAP4] section 5.1.3 includes a convention for encoding
  211. non-US-ASCII characters in IMAP mailbox names. Because this
  212. convention is private to IMAP, it is necessary to convert IMAP's
  213. encoding to one that can be more easily interpreted by a URL
  214. display program. For this reason, IMAP's modified UTF-7 encoding
  215. for mailboxes MUST be converted to UTF-8 [UTF8]. Since 8-bit
  216. characters are not permitted in URLs, the UTF-8 characters are
  217. Newman Standards Track [Page 6]
  218. RFC 2192 IMAP URL Scheme September 1997
  219. encoded as required by the URL specification [BASIC-URL]. Sample
  220. code is included in Appendix A to demonstrate this conversion.
  221. 10. Examples
  222. The following examples demonstrate how an IMAP4 client program
  223. might translate various IMAP4 URLs into a series of IMAP4 commands.
  224. Commands sent from the client to the server are prefixed with "C:",
  225. and responses sent from the server to the client are prefixed with
  226. "S:".
  227. The URL:
  228. <imap://minbari.org/gray-council;UIDVALIDITY=385759045/;UID=20>
  229. Results in the following client commands:
  230. <connect to minbari.org, port 143>
  231. C: A001 LOGIN ANONYMOUS sheridan@babylon5.org
  232. C: A002 SELECT gray-council
  233. <client verifies the UIDVALIDITY matches>
  234. C: A003 UID FETCH 20 BODY.PEEK[]
  235. The URL:
  236. <imap://michael@minbari.org/users.*;type=list>
  237. Results in the following client commands:
  238. <client requests password from user>
  239. <connect to minbari.org imap server, activate strong encryption>
  240. C: A001 LOGIN MICHAEL zipper
  241. C: A002 LIST "" users.*
  242. The URL:
  243. <imap://psicorp.org/~peter/%E6%97%A5%E6%9C%AC%E8%AA%9E/
  244. %E5%8F%B0%E5%8C%97>
  245. Results in the following client commands:
  246. <connect to psicorp.org, port 143>
  247. C: A001 LOGIN ANONYMOUS bester@psycop.psicorp.org
  248. C: A002 SELECT ~peter/&ZeVnLIqe-/&U,BTFw-
  249. <commands the client uses for viewing the contents of a mailbox>
  250. Newman Standards Track [Page 7]
  251. RFC 2192 IMAP URL Scheme September 1997
  252. The URL:
  253. <imap://;AUTH=KERBEROS_V4@minbari.org/gray-council/;uid=20/
  254. ;section=1.2>
  255. Results in the following client commands:
  256. <connect to minbari.org, port 143>
  257. C: A001 AUTHENTICATE KERBEROS_V4
  258. <authentication exchange>
  259. C: A002 SELECT gray-council
  260. C: A003 UID FETCH 20 BODY.PEEK[1.2]
  261. If the following relative URL is located in that body part:
  262. <;section=1.4>
  263. This could result in the following client commands:
  264. C: A004 UID FETCH 20 (BODY.PEEK[1.2.MIME]
  265. BODY.PEEK[1.MIME]
  266. BODY.PEEK[HEADER.FIELDS (Content-Base Content-Location)])
  267. <Client looks for Content-Base or Content-Location headers in
  268. result. If no such headers, then it does the following>
  269. C: A005 UID FETCH 20 BODY.PEEK[1.4]
  270. The URL:
  271. <imap://;AUTH=*@minbari.org/gray%20council?SUBJECT%20shadows>
  272. Could result in the following:
  273. <connect to minbari.org, port 143>
  274. C: A001 CAPABILITY
  275. S: * CAPABILITY IMAP4rev1 AUTH=GSSAPI
  276. S: A001 OK
  277. C: A002 AUTHENTICATE GSSAPI
  278. <authentication exchange>
  279. S: A002 OK user lennier authenticated
  280. C: A003 SELECT "gray council"
  281. ...
  282. C: A004 SEARCH SUBJECT shadows
  283. S: * SEARCH 8 10 13 14 15 16
  284. S: A004 OK SEARCH completed
  285. C: A005 FETCH 8,10,13:16 ALL
  286. ...
  287. Newman Standards Track [Page 8]
  288. RFC 2192 IMAP URL Scheme September 1997
  289. NOTE: In this final example, the client has implementation
  290. dependent choices. The authentication mechanism could be anything,
  291. including PREAUTH. And the final FETCH command could fetch more or
  292. less information about the messages, depending on what it wishes to
  293. display to the user.
  294. 11. Security Considerations
  295. Security considerations discussed in the IMAP specification [IMAP4]
  296. and the URL specification [BASIC-URL] are relevant. Security
  297. considerations related to authenticated URLs are discussed in
  298. section 3 of this document.
  299. Many email clients store the plain text password for later use
  300. after logging into an IMAP server. Such clients MUST NOT use a
  301. stored password in response to an IMAP URL without explicit
  302. permission from the user to supply that password to the specified
  303. host name.
  304. 12. ABNF for IMAP URL scheme
  305. This uses ABNF as defined in RFC 822 [IMAIL]. Terminals from the
  306. BNF for IMAP [IMAP4] and URLs [BASIC-URL] are also used. Strings
  307. are not case sensitive and free insertion of linear-white-space is
  308. not permitted.
  309. achar = uchar / "&" / "=" / "~"
  310. ; see [BASIC-URL] for "uchar" definition
  311. bchar = achar / ":" / "@" / "/"
  312. enc_auth_type = 1*achar
  313. ; encoded version of [IMAP-AUTH] "auth_type"
  314. enc_list_mailbox = 1*bchar
  315. ; encoded version of [IMAP4] "list_mailbox"
  316. enc_mailbox = 1*bchar
  317. ; encoded version of [IMAP4] "mailbox"
  318. enc_search = 1*bchar
  319. ; encoded version of search_program below
  320. enc_section = 1*bchar
  321. ; encoded version of section below
  322. Newman Standards Track [Page 9]
  323. RFC 2192 IMAP URL Scheme September 1997
  324. enc_user = 1*achar
  325. ; encoded version of [IMAP4] "userid"
  326. imapurl = "imap://" iserver "/" [ icommand ]
  327. iauth = ";AUTH=" ( "*" / enc_auth_type )
  328. icommand = imailboxlist / imessagelist / imessagepart
  329. imailboxlist = [enc_list_mailbox] ";TYPE=" list_type
  330. imessagelist = enc_mailbox [ "?" enc_search ] [uidvalidity]
  331. imessagepart = enc_mailbox [uidvalidity] iuid [isection]
  332. isection = "/;SECTION=" enc_section
  333. iserver = [iuserauth "@"] hostport
  334. ; See [BASIC-URL] for "hostport" definition
  335. iuid = "/;UID=" nz_number
  336. ; See [IMAP4] for "nz_number" definition
  337. iuserauth = enc_user [iauth] / [enc_user] iauth
  338. list_type = "LIST" / "LSUB"
  339. search_program = ["CHARSET" SPACE astring SPACE]
  340. search_key *(SPACE search_key)
  341. ; IMAP4 literals may not be used
  342. ; See [IMAP4] for "astring" and "search_key"
  343. section = section_text / (nz_number *["." nz_number]
  344. ["." (section_text / "MIME")])
  345. ; See [IMAP4] for "section_text" and "nz_number"
  346. uidvalidity = ";UIDVALIDITY=" nz_number
  347. ; See [IMAP4] for "nz_number" definition
  348. 13. References
  349. [BASIC-URL] Berners-Lee, Masinter, McCahill, "Uniform Resource
  350. Locators (URL)", RFC 1738, CERN, Xerox Corporation, University of
  351. Minnesota, December 1994.
  352. <ftp://ds.internic.net/rfc/rfc1738.txt>
  353. Newman Standards Track [Page 10]
  354. RFC 2192 IMAP URL Scheme September 1997
  355. [IMAP4] Crispin, M., "Internet Message Access Protocol - Version
  356. 4rev1", RFC 2060, University of Washington, December 1996.
  357. <ftp://ds.internic.net/rfc/rfc2060.txt>
  358. [IMAP-AUTH] Myers, J., "IMAP4 Authentication Mechanism", RFC 1731,
  359. Carnegie-Mellon University, December 1994.
  360. <ftp://ds.internic.net/rfc/rfc1731.txt>
  361. [HTTP] Fielding, Gettys, Mogul, Frystyk, Berners-Lee, "Hypertext
  362. Transfer Protocol -- HTTP/1.1", RFC 2068, UC Irvine, DEC, MIT/LCS,
  363. January 1997.
  364. <ftp://ds.internic.net/rfc/rfc2068.txt>
  365. [IMAIL] Crocker, "Standard for the Format of ARPA Internet Text
  366. Messages", STD 11, RFC 822, University of Delaware, August 1982.
  367. <ftp://ds.internic.net/rfc/rfc822.txt>
  368. [KEYWORDS] Bradner, "Key words for use in RFCs to Indicate
  369. Requirement Levels", RFC 2119, Harvard University, March 1997.
  370. <ftp://ds.internic.net/rfc/rfc2119.txt>
  371. [MIME] Freed, N., Borenstein, N., "Multipurpose Internet Mail
  372. Extensions", RFC 2045, Innosoft, First Virtual, November 1996.
  373. <ftp://ds.internic.net/rfc/rfc2045.txt>
  374. [REL-URL] Fielding, "Relative Uniform Resource Locators", RFC 1808,
  375. UC Irvine, June 1995.
  376. <ftp://ds.internic.net/rfc/rfc1808.txt>
  377. [UTF8] Yergeau, F. "UTF-8, a transformation format of Unicode and
  378. ISO 10646", RFC 2044, Alis Technologies, October 1996.
  379. <ftp://ds.internic.net/rfc/rfc2044.txt>
  380. 14. Author's Address
  381. Chris Newman
  382. Innosoft International, Inc.
  383. 1050 Lakes Drive
  384. West Covina, CA 91790 USA
  385. EMail: chris.newman@innosoft.com
  386. Newman Standards Track [Page 11]
  387. RFC 2192 IMAP URL Scheme September 1997
  388. Appendix A. Sample code
  389. Here is sample C source code to convert between URL paths and IMAP
  390. mailbox names, taking into account mapping between IMAP's modified UTF-7
  391. [IMAP4] and hex-encoded UTF-8 which is more appropriate for URLs. This
  392. code has not been rigorously tested nor does it necessarily behave
  393. reasonably with invalid input, but it should serve as a useful example.
  394. This code just converts the mailbox portion of the URL and does not deal
  395. with parameters, query or server components of the URL.
  396. #include <stdio.h>
  397. #include <string.h>
  398. /* hexadecimal lookup table */
  399. static char hex[] = "0123456789ABCDEF";
  400. /* URL unsafe printable characters */
  401. static char urlunsafe[] = " \"#%&+:;<=>?@[\\]^`{|}";
  402. /* UTF7 modified base64 alphabet */
  403. static char base64chars[] =
  404. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  405. #define UNDEFINED 64
  406. /* UTF16 definitions */
  407. #define UTF16MASK 0x03FFUL
  408. #define UTF16SHIFT 10
  409. #define UTF16BASE 0x10000UL
  410. #define UTF16HIGHSTART 0xD800UL
  411. #define UTF16HIGHEND 0xDBFFUL
  412. #define UTF16LOSTART 0xDC00UL
  413. #define UTF16LOEND 0xDFFFUL
  414. /* Convert an IMAP mailbox to a URL path
  415. * dst needs to have roughly 4 times the storage space of src
  416. * Hex encoding can triple the size of the input
  417. * UTF-7 can be slightly denser than UTF-8
  418. * (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
  419. */
  420. void MailboxToURL(char *dst, char *src)
  421. {
  422. unsigned char c, i, bitcount;
  423. unsigned long ucs4, utf16, bitbuf;
  424. unsigned char base64[256], utf8[6];
  425. Newman Standards Track [Page 12]
  426. RFC 2192 IMAP URL Scheme September 1997
  427. /* initialize modified base64 decoding table */
  428. memset(base64, UNDEFINED, sizeof (base64));
  429. for (i = 0; i < sizeof (base64chars); ++i) {
  430. base64[base64chars[i]] = i;
  431. }
  432. /* loop until end of string */
  433. while (*src != '\0') {
  434. c = *src++;
  435. /* deal with literal characters and &- */
  436. if (c != '&' || *src == '-') {
  437. if (c < ' ' || c > '~' || strchr(urlunsafe, c) != NULL) {
  438. /* hex encode if necessary */
  439. dst[0] = '%';
  440. dst[1] = hex[c >> 4];
  441. dst[2] = hex[c & 0x0f];
  442. dst += 3;
  443. } else {
  444. /* encode literally */
  445. *dst++ = c;
  446. }
  447. /* skip over the '-' if this is an &- sequence */
  448. if (c == '&') ++src;
  449. } else {
  450. /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
  451. bitbuf = 0;
  452. bitcount = 0;
  453. ucs4 = 0;
  454. while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
  455. ++src;
  456. bitbuf = (bitbuf << 6) | c;
  457. bitcount += 6;
  458. /* enough bits for a UTF-16 character? */
  459. if (bitcount >= 16) {
  460. bitcount -= 16;
  461. utf16 = (bitcount ? bitbuf >> bitcount
  462. : bitbuf) & 0xffff;
  463. /* convert UTF16 to UCS4 */
  464. if
  465. (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
  466. ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
  467. continue;
  468. } else if
  469. (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
  470. ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
  471. } else {
  472. ucs4 = utf16;
  473. }
  474. Newman Standards Track [Page 13]
  475. RFC 2192 IMAP URL Scheme September 1997
  476. /* convert UTF-16 range of UCS4 to UTF-8 */
  477. if (ucs4 <= 0x7fUL) {
  478. utf8[0] = ucs4;
  479. i = 1;
  480. } else if (ucs4 <= 0x7ffUL) {
  481. utf8[0] = 0xc0 | (ucs4 >> 6);
  482. utf8[1] = 0x80 | (ucs4 & 0x3f);
  483. i = 2;
  484. } else if (ucs4 <= 0xffffUL) {
  485. utf8[0] = 0xe0 | (ucs4 >> 12);
  486. utf8[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
  487. utf8[2] = 0x80 | (ucs4 & 0x3f);
  488. i = 3;
  489. } else {
  490. utf8[0] = 0xf0 | (ucs4 >> 18);
  491. utf8[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
  492. utf8[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
  493. utf8[3] = 0x80 | (ucs4 & 0x3f);
  494. i = 4;
  495. }
  496. /* convert utf8 to hex */
  497. for (c = 0; c < i; ++c) {
  498. dst[0] = '%';
  499. dst[1] = hex[utf8[c] >> 4];
  500. dst[2] = hex[utf8[c] & 0x0f];
  501. dst += 3;
  502. }
  503. }
  504. }
  505. /* skip over trailing '-' in modified UTF-7 encoding */
  506. if (*src == '-') ++src;
  507. }
  508. }
  509. /* terminate destination string */
  510. *dst = '\0';
  511. }
  512. /* Convert hex coded UTF-8 URL path to modified UTF-7 IMAP mailbox
  513. * dst should be about twice the length of src to deal with non-hex
  514. * coded URLs
  515. */
  516. void URLtoMailbox(char *dst, char *src)
  517. {
  518. unsigned int utf8pos, utf8total, i, c, utf7mode, bitstogo, utf16flag;
  519. unsigned long ucs4, bitbuf;
  520. unsigned char hextab[256];
  521. /* initialize hex lookup table */
  522. Newman Standards Track [Page 14]
  523. RFC 2192 IMAP URL Scheme September 1997
  524. memset(hextab, 0, sizeof (hextab));
  525. for (i = 0; i < sizeof (hex); ++i) {
  526. hextab[hex[i]] = i;
  527. if (isupper(hex[i])) hextab[tolower(hex[i])] = i;
  528. }
  529. utf7mode = 0;
  530. utf8total = 0;
  531. bitstogo = 0;
  532. while ((c = *src) != '\0') {
  533. ++src;
  534. /* undo hex-encoding */
  535. if (c == '%' && src[0] != '\0' && src[1] != '\0') {
  536. c = (hextab[src[0]] << 4) | hextab[src[1]];
  537. src += 2;
  538. }
  539. /* normal character? */
  540. if (c >= ' ' && c <= '~') {
  541. /* switch out of UTF-7 mode */
  542. if (utf7mode) {
  543. if (bitstogo) {
  544. *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
  545. }
  546. *dst++ = '-';
  547. utf7mode = 0;
  548. }
  549. *dst++ = c;
  550. /* encode '&' as '&-' */
  551. if (c == '&') {
  552. *dst++ = '-';
  553. }
  554. continue;
  555. }
  556. /* switch to UTF-7 mode */
  557. if (!utf7mode) {
  558. *dst++ = '&';
  559. utf7mode = 1;
  560. }
  561. /* Encode US-ASCII characters as themselves */
  562. if (c < 0x80) {
  563. ucs4 = c;
  564. utf8total = 1;
  565. } else if (utf8total) {
  566. /* save UTF8 bits into UCS4 */
  567. ucs4 = (ucs4 << 6) | (c & 0x3FUL);
  568. if (++utf8pos < utf8total) {
  569. continue;
  570. }
  571. Newman Standards Track [Page 15]
  572. RFC 2192 IMAP URL Scheme September 1997
  573. } else {
  574. utf8pos = 1;
  575. if (c < 0xE0) {
  576. utf8total = 2;
  577. ucs4 = c & 0x1F;
  578. } else if (c < 0xF0) {
  579. utf8total = 3;
  580. ucs4 = c & 0x0F;
  581. } else {
  582. /* NOTE: can't convert UTF8 sequences longer than 4 */
  583. utf8total = 4;
  584. ucs4 = c & 0x03;
  585. }
  586. continue;
  587. }
  588. /* loop to split ucs4 into two utf16 chars if necessary */
  589. utf8total = 0;
  590. do {
  591. if (ucs4 >= UTF16BASE) {
  592. ucs4 -= UTF16BASE;
  593. bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
  594. + UTF16HIGHSTART);
  595. ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
  596. utf16flag = 1;
  597. } else {
  598. bitbuf = (bitbuf << 16) | ucs4;
  599. utf16flag = 0;
  600. }
  601. bitstogo += 16;
  602. /* spew out base64 */
  603. while (bitstogo >= 6) {
  604. bitstogo -= 6;
  605. *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo)
  606. : bitbuf)
  607. & 0x3F];
  608. }
  609. } while (utf16flag);
  610. }
  611. /* if in UTF-7 mode, finish in ASCII */
  612. if (utf7mode) {
  613. if (bitstogo) {
  614. *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
  615. }
  616. *dst++ = '-';
  617. }
  618. /* tie off string */
  619. *dst = '\0';
  620. }
  621. Newman Standards Track [Page 16]