RFCMIME.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. static char sccsid[] = "$TOG: RFCMIME.c /main/11 1999/06/30 12:08:55 mgreess $";
  24. /*
  25. * COMPONENT_NAME: desktop
  26. *
  27. * FUNCTIONS: CvtStr
  28. * DtXlateOpToStdLocale
  29. * DtXlateStdToOpLocale
  30. * _converter_
  31. * base64size
  32. * crlf
  33. * getCharSet
  34. * getEncodingType
  35. * mbisspace
  36. * md5PlainText
  37. * rfc1522cpy
  38. * targetTagName
  39. * writeBase64
  40. * writeContentHeaders
  41. * writeQPrint
  42. *
  43. * ORIGINS: 119
  44. *
  45. * OBJECT CODE ONLY SOURCE MATERIALS
  46. */
  47. #include <stdio.h>
  48. #include <errno.h>
  49. #include <unistd.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <md5.h>
  53. #define _ILS_MACROS
  54. #include <ctype.h>
  55. #include <assert.h>
  56. #include <RFCMIME.h>
  57. #include <iconv.h>
  58. #include <EUSCompat.h>
  59. #include <locale.h>
  60. #include <LocaleXlate.h>
  61. #ifdef ICONV_INBUF_CONST
  62. # define ICONV_INBUF_TYPE const char **
  63. #else
  64. # define ICONV_INBUF_TYPE char **
  65. #endif
  66. #define WORKSIZE 1024*10
  67. /*
  68. * The following escape sequence is defined as "To ASCII".
  69. * But is it correct regardless of ISO-2022-XX ???
  70. */
  71. #define ToASCII_NUM 3
  72. static char ToASCII[ToASCII_NUM] = { 0x1b, 0x28, 0x42 };
  73. /*
  74. * _i18nwork1[] is used to convert the passed string with CD iconv.
  75. * in _converter_().
  76. *
  77. */
  78. static void *_i18nwork1 = NULL;
  79. static unsigned long _i18nsize1 = 0;
  80. static int shouldAlloc1 = ~0;
  81. /*
  82. * _i18nwork2[] is used to convert the passed string with CD iconv.
  83. * in CvtStr().
  84. *
  85. */
  86. static void *_i18nwork2 = NULL;
  87. static unsigned long _i18nsize2 = 0;
  88. static int shouldAlloc2 = ~0;
  89. /*
  90. * Forward declarations
  91. */
  92. extern void writeBase64(char * buf, const char * bp, const unsigned long len);
  93. static const char *DfltStdCharset = "us-ascii";
  94. static const char *DfltStdLang = "C";
  95. static void crlf(char *buf)
  96. {
  97. strcat(buf,"\n");
  98. }
  99. /******************************************************************************
  100. * Function: int DtXlateOpToStdLocale (char *operation, char *opLocale,
  101. * char **ret_stdLocale, char **ret_stdLang, char **ret_stdSet)
  102. *
  103. * Parameters:
  104. * operation Operation associated with the locale value
  105. * opLocale An operation-specific locale string
  106. * ret_locale Returns the std locale
  107. * Caller must free this string.
  108. * ret_stdLang Returns the std language & territory string.
  109. * Caller must free this string.
  110. * ret_stdSet Returns the std code set string.
  111. * Caller must free this string.
  112. *
  113. * Return Value:
  114. *
  115. * Purpose: Gets the standard locale given an operation and its locale
  116. *
  117. *****************************************************************************/
  118. void
  119. DtXlateOpToStdLocale (
  120. char *operation,
  121. char *opLocale,
  122. char **ret_stdLocale,
  123. char **ret_stdLang,
  124. char **ret_stdSet)
  125. {
  126. _DtXlateDb MyDb = NULL;
  127. char MyPlatform[_DtPLATFORM_MAX_LEN + 1];
  128. int ExecVer;
  129. int CompVer;
  130. if (_DtLcxOpenAllDbs(&MyDb) == 0 &&
  131. _DtXlateGetXlateEnv(MyDb,MyPlatform,&ExecVer,&CompVer) != 0)
  132. {
  133. _DtLcxCloseDb(&MyDb);
  134. MyDb = NULL;
  135. }
  136. if (MyDb != NULL)
  137. {
  138. (void) _DtLcxXlateOpToStd(MyDb, MyPlatform, ExecVer,
  139. operation,opLocale,
  140. ret_stdLocale, ret_stdLang, ret_stdSet, NULL);
  141. }
  142. /* if failed, give default values */
  143. if (ret_stdLocale != NULL && *ret_stdLocale == NULL)
  144. {
  145. *ret_stdLocale = (char *)malloc(strlen(DfltStdLang)+strlen(DfltStdCharset)+3);
  146. sprintf(*ret_stdLocale,"%s.%s",DfltStdLang,DfltStdCharset);
  147. }
  148. if (ret_stdLang != NULL && *ret_stdLang == NULL)
  149. *ret_stdLang = (char *)strdup(DfltStdLang);
  150. if (ret_stdSet != NULL && *ret_stdSet == NULL)
  151. *ret_stdSet = (char *)strdup(DfltStdCharset);
  152. }
  153. /******************************************************************************
  154. * Function: int DtXlateStdToOpLocale ( char *operation, char *stdLocale,
  155. * char *stdLang, char *stdCodeSet,
  156. * char *dflt_opLocale, char **ret_opLocale)
  157. *
  158. * Parameters:
  159. * operation operation whose locale value will be retrieved
  160. * stdLocale standard locale value
  161. * stdLang standard Lang/Territory Value
  162. * stdCodeSet standard CodeSet Value
  163. * dflt_opLocale operation-specific locale-value
  164. * This is the default value used in error case
  165. * ret_opLocale operation-specific locale-value placed here
  166. * Caller must free this string.
  167. *
  168. * Return Value:
  169. *
  170. * Purpose: Gets an operation-specific locale string given the standard string
  171. *
  172. *****************************************************************************/
  173. void
  174. DtXlateStdToOpLocale (
  175. char *operation,
  176. char *stdLocale,
  177. char *stdLang,
  178. char *stdCodeSet,
  179. char *dflt_opLocale,
  180. char **ret_opLocale)
  181. {
  182. _DtXlateDb MyDb = NULL;
  183. char MyPlatform[_DtPLATFORM_MAX_LEN + 1];
  184. int ExecVer;
  185. int CompVer;
  186. if (_DtLcxOpenAllDbs(&MyDb) == 0 &&
  187. _DtXlateGetXlateEnv(MyDb,MyPlatform,&ExecVer,&CompVer) != 0)
  188. {
  189. _DtLcxCloseDb(&MyDb);
  190. MyDb = NULL;
  191. }
  192. if (MyDb != NULL)
  193. {
  194. (void) _DtLcxXlateStdToOp(MyDb, MyPlatform, ExecVer,
  195. operation, stdLocale, stdLang, stdCodeSet, NULL,
  196. ret_opLocale);
  197. }
  198. /* if translation fails, use a default value */
  199. if (ret_opLocale && *ret_opLocale == NULL)
  200. {
  201. if (dflt_opLocale) *ret_opLocale = (char *)strdup(dflt_opLocale);
  202. else if (stdLocale) *ret_opLocale = (char *)strdup(stdLocale);
  203. }
  204. }
  205. char *
  206. targetTagName(void)
  207. {
  208. char *ret_locale = NULL;
  209. char *ret_lang = NULL;
  210. char *ret_codeset = NULL;
  211. char *ret_target = NULL;
  212. DtXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,
  213. setlocale(LC_CTYPE, NULL),
  214. &ret_locale,
  215. &ret_lang,
  216. &ret_codeset);
  217. DtXlateStdToOpLocale(DtLCX_OPER_INTERCHANGE_CODESET,
  218. NULL,
  219. NULL,
  220. ret_codeset,
  221. NULL,
  222. &ret_target);
  223. DtXlateStdToOpLocale(DtLCX_OPER_MIME,
  224. NULL,
  225. NULL,
  226. ret_target,
  227. NULL,
  228. &ret_codeset);
  229. free(ret_locale);
  230. free(ret_target);
  231. free(ret_lang);
  232. return ret_codeset;
  233. }
  234. void
  235. getCharSet(char * charset)
  236. {
  237. char *mimeCS = NULL;
  238. mimeCS = targetTagName();
  239. if (mimeCS) {
  240. strcpy(charset, mimeCS);
  241. free(mimeCS);
  242. } else {
  243. strcpy(charset, "us-ascii"); /* default MIME codeset */
  244. }
  245. }
  246. void
  247. md5PlainText(const char * bp, const unsigned long len, unsigned char * digest)
  248. {
  249. /* We need to compute the md5 signature based on a message that has
  250. // the CRLF line terminator. Most of our buffers don't so we will need
  251. // to scan the body and do some magic. The approach will be to sum
  252. // one line at a time. If the buffer doesn't have CRLF we will do that
  253. // independently.
  254. */
  255. MD5_CTX context;
  256. unsigned char * local_crlf = (unsigned char *)"\r\n";
  257. const char * last = bp;
  258. const char * cur;
  259. MD5Init(&context);
  260. for (cur = bp; cur < (bp + len); cur++) {
  261. if (*cur == '\n') {
  262. if (cur == bp || *(cur - 1) == '\r') {
  263. MD5Update(&context, (unsigned char *)last,
  264. cur - last + 1);
  265. }
  266. else {
  267. MD5Update(&context, (unsigned char *)last,
  268. cur - last);
  269. MD5Update(&context, local_crlf, 2);
  270. }
  271. last = cur + 1;
  272. }
  273. }
  274. if (bp[len - 1] != '\n') {
  275. /* Need to sum the trailing fraction with a CRLF. */
  276. MD5Update(&context, (unsigned char *)last,
  277. cur - last);
  278. MD5Update(&context, local_crlf, 2);
  279. }
  280. MD5Final(digest, &context);
  281. }
  282. static void _converter_( iconv_t CD,
  283. void *from, unsigned long from_len,
  284. void **to, unsigned long *to_len )
  285. {
  286. char *InBuf;
  287. size_t InBytesLeft;
  288. char *OutBuf = NULL;
  289. size_t OutBytesLeft = 0;
  290. size_t _OutBytesLeft = 0;
  291. size_t iconv_ret;
  292. size_t converted_num = 0;
  293. *to = NULL;
  294. *to_len = 0;
  295. if ( shouldAlloc1 ) {
  296. /* Obtain work area */
  297. _i18nwork1 = (size_t *)malloc( WORKSIZE );
  298. if ( !_i18nwork1 ) {
  299. _i18nwork1 = NULL;
  300. return;
  301. }
  302. _i18nsize1 = WORKSIZE;
  303. shouldAlloc1 = 0;
  304. }
  305. InBuf = (char *)from;
  306. InBytesLeft = from_len;
  307. OutBytesLeft = _i18nsize1;
  308. OutBuf = (char *)_i18nwork1;
  309. /*
  310. * Need to place iconv state to the initial one by
  311. * setting inbuf to NULL of iconv().
  312. */
  313. iconv( CD, (ICONV_INBUF_TYPE)NULL, 0, NULL, 0 );
  314. while( 1 ) {
  315. /*
  316. * InBuf
  317. * v
  318. * +----------------------------+
  319. * | | | |
  320. * +----------------------------+
  321. * <-------------------------->
  322. * InBytesLeft
  323. *
  324. * |
  325. * | iconv()
  326. * V
  327. * (_i18nwork1)
  328. * OutBuf
  329. * v
  330. * +----------------------------+
  331. * | | | |
  332. * +----------------------------+
  333. * <-------------------------->
  334. * InBytesLeft
  335. */
  336. iconv_ret = iconv( CD, (ICONV_INBUF_TYPE)&InBuf, &InBytesLeft,
  337. &OutBuf, &OutBytesLeft );
  338. if ( iconv_ret == 0 ) {
  339. /* iconv done
  340. * InBuf
  341. * v
  342. * +----------------------------+
  343. * |XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
  344. * +----------------------------+
  345. *
  346. * InBytesLeft=0
  347. *
  348. * (_i18nwork1)
  349. * | OutBuf
  350. * V v
  351. * +----------------------------+
  352. * |XXXXXXXXXXXXXXXXX| | | |
  353. * +----------------------------+
  354. * <---------------> <-------->
  355. * converted_num OutBytesLeft
  356. */
  357. converted_num = (unsigned long)((char *)OutBuf-(char *)_i18nwork1);
  358. *to = (void *)_i18nwork1;
  359. *to_len = (unsigned long)converted_num;
  360. break;
  361. } else {
  362. if ( errno == E2BIG ) {
  363. /* Overflow. still data is left.
  364. * InBuf
  365. * v
  366. * +----------------------------+
  367. * |XXXXXXXXXXXXXX| | | |
  368. * +----------------------------+
  369. * <----------->
  370. * InBytesLeft
  371. *
  372. * (_i18nwork1)
  373. * | OutBuf
  374. * V v
  375. * +----------------------------+
  376. * |XXXXXXXXXXXXXXXXXXXXXXXXXXX |
  377. * +----------------------------+
  378. * <------------------------->
  379. * converted_num OutBytesLeft=?
  380. */
  381. void *_p;
  382. /* Check how many converted already.
  383. */
  384. converted_num =
  385. (unsigned long)((char *)OutBuf - (char *)_i18nwork1);
  386. _i18nsize1 += WORKSIZE;
  387. _p = realloc( _i18nwork1, _i18nsize1 );
  388. if ( !_p ) {
  389. *to = NULL;
  390. *to_len = 0;
  391. free( _i18nwork1 );
  392. _i18nwork1 = NULL;
  393. _i18nsize1 = 0;
  394. shouldAlloc1 = ~0;
  395. break;
  396. } else {
  397. _i18nwork1 = _p;
  398. OutBuf = (char *)((char*)_i18nwork1 + converted_num);
  399. OutBytesLeft += WORKSIZE;
  400. }
  401. } else {
  402. *to = NULL;
  403. *to_len = 0;
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. int
  410. CvtStr( char *charSet, void *from, unsigned long from_len,
  411. void **to, unsigned long *to_len, Direction dir )
  412. {
  413. char *ret_locale = NULL;
  414. char *ret_lang = NULL;
  415. char *ret_codeset = NULL;
  416. char *from_codeset = NULL;
  417. char *to_codeset = NULL;
  418. char *CuStdCodeSet = NULL;
  419. char *InterChCodeSet = NULL;
  420. char *StdCodeSet = NULL;
  421. iconv_t CD;
  422. int isASCII=~0;
  423. int isStopASCII = ~0;
  424. unsigned long converted_num = 0;
  425. /* Get CuStdCodeSet */
  426. DtXlateOpToStdLocale( DtLCX_OPER_SETLOCALE,
  427. setlocale( LC_CTYPE, NULL ),
  428. &ret_locale,
  429. &ret_lang,
  430. &CuStdCodeSet );
  431. /*
  432. * If charSet is NULL, it means the passed string's charset in *from is
  433. * unknown by dtmail. In this case, this converter assumes that
  434. * when dir = CURRENT_TO_INTERNET,
  435. * *from's encoding is the current locale's one.
  436. * when dir = INTERNET_TO_CURRENT,
  437. * *from's encoding is the current locale's Internet Message's one.
  438. *
  439. * Example.
  440. * dtmail is running under ja_JP locale.
  441. * dir : CURRENT_TO_INTERNET
  442. * *from = IBM-eucJP
  443. * *to = ISO-2022-JP
  444. * dir : INTERNET_TO_CURRENT
  445. * *from = ISO-2022-JP
  446. * *to = IBM-eucJP
  447. */
  448. /*
  449. * ISO-2022-JP can be converted to either EUC-JP or IBM-932 practically.
  450. * But the current AIX.lcx says
  451. * StdCodeSet InterchangeCodeset
  452. * EUC-JP <--> ISO-2022-JP
  453. * IBM-932 ---> ISO-2022-JP
  454. * HP-SJIS ---> ISO-2022-JP
  455. * HP-KANA8 ---> ISO-2022-JP
  456. * therefore DtXlateOpToStdLocale() can convert ISO-2022-JP to EUC-JP only.
  457. * To fix this, we hard-code'ed this situation with the CDE Standard Name
  458. *
  459. * ???? Is it correct ???
  460. */
  461. if ( dir == INTERNET_TO_CURRENT ) {
  462. /*
  463. * As for from_codeset
  464. */
  465. if ( ( charSet == NULL ) || ( *charSet == '\0' ) ) {
  466. /* Convert CuStdCodeSet to StdInterChangeCodeSet */
  467. DtXlateStdToOpLocale( DtLCX_OPER_INTERCHANGE_CODESET,
  468. NULL,
  469. NULL,
  470. CuStdCodeSet,
  471. NULL,
  472. &InterChCodeSet );
  473. } else {
  474. /* Convert charSet to StdInterChangeCodeSet */
  475. free(ret_locale);
  476. free(ret_lang);
  477. free(ret_codeset);
  478. ret_locale = ret_lang = ret_codeset = NULL;
  479. DtXlateOpToStdLocale( DtLCX_OPER_MIME,
  480. charSet,
  481. &ret_locale,
  482. &ret_lang,
  483. &InterChCodeSet );
  484. }
  485. /* Convert StdInterChangeCodeSet to OpIVONC3 codeset */
  486. DtXlateStdToOpLocale( DtLCX_OPER_ICONV3,
  487. NULL,
  488. NULL,
  489. InterChCodeSet,
  490. NULL,
  491. &from_codeset );
  492. /*
  493. * As for to_codeset
  494. */
  495. if ( ( charSet == NULL ) || ( *charSet == '\0' ) ) {
  496. /* Convert CuStdCodeSet to OpIVONC3 codeset */
  497. DtXlateStdToOpLocale( DtLCX_OPER_ICONV3,
  498. NULL,
  499. NULL,
  500. CuStdCodeSet,
  501. NULL,
  502. &to_codeset );
  503. } else {
  504. #ifdef _AIX
  505. if ( (!strncasecmp(InterChCodeSet,"ISO-2022-JP",11) &&
  506. !strncasecmp(CuStdCodeSet,"IBM-932",7) ) ||
  507. (!strncasecmp(InterChCodeSet,"ISO-2022-JP",11) &&
  508. !strncasecmp(CuStdCodeSet,"EUC-JP",6) ) ) {
  509. ret_codeset = CuStdCodeSet;
  510. } else
  511. #endif /* _AIX */
  512. {
  513. /* Convert InterChCodeSet to StdCodeSet */
  514. free(ret_locale);
  515. free(ret_lang);
  516. free(ret_codeset);
  517. ret_locale = ret_lang = ret_codeset = NULL;
  518. DtXlateOpToStdLocale( DtLCX_OPER_INTERCHANGE_CODESET,
  519. InterChCodeSet,
  520. &ret_locale,
  521. &ret_lang,
  522. &ret_codeset );
  523. }
  524. DtXlateStdToOpLocale( DtLCX_OPER_ICONV3,
  525. NULL,
  526. NULL,
  527. ret_codeset,
  528. NULL,
  529. &to_codeset );
  530. }
  531. } else { /* dir == CURRENT_TO_INTERNET */
  532. /*
  533. * As for from_codeset
  534. */
  535. if ( ( charSet == NULL ) || ( *charSet == '\0' ) ) {
  536. /* Convert CuStdCodeSet to OpICONV3 codeset */
  537. DtXlateStdToOpLocale( DtLCX_OPER_ICONV3,
  538. NULL,
  539. NULL,
  540. CuStdCodeSet,
  541. NULL,
  542. &from_codeset );
  543. } else {
  544. /* Convert charSet to StdInterChangeCodeSet */
  545. free(ret_locale);
  546. free(ret_lang);
  547. free(ret_codeset);
  548. ret_locale = ret_lang = ret_codeset = NULL;
  549. DtXlateOpToStdLocale( DtLCX_OPER_MIME,
  550. charSet,
  551. &ret_locale,
  552. &ret_lang,
  553. &ret_codeset );
  554. /* Convert StdInterChangeCodeSet to OpIVONC3 codeset */
  555. DtXlateStdToOpLocale( DtLCX_OPER_ICONV3,
  556. NULL,
  557. NULL,
  558. ret_codeset,
  559. NULL,
  560. &from_codeset );
  561. }
  562. /*
  563. * As for to_codeset
  564. */
  565. if ( ( charSet == NULL ) || ( *charSet == '\0' ) ) {
  566. /* Convert CuStdCodeSet to StdInterChangeCodeSet */
  567. DtXlateStdToOpLocale( DtLCX_OPER_INTERCHANGE_CODESET,
  568. NULL,
  569. NULL,
  570. CuStdCodeSet,
  571. NULL,
  572. &InterChCodeSet );
  573. } else {
  574. /* Convert charSet to StdInterChangeCodeSet */
  575. free(ret_locale);
  576. free(ret_lang);
  577. free(ret_codeset);
  578. ret_locale = ret_lang = ret_codeset = NULL;
  579. DtXlateOpToStdLocale( DtLCX_OPER_MIME,
  580. charSet,
  581. &ret_locale,
  582. &ret_lang,
  583. &InterChCodeSet );
  584. }
  585. /* Convert StdInterChangeCodeSet to OpIVONC3 codeset */
  586. DtXlateStdToOpLocale( DtLCX_OPER_ICONV3,
  587. NULL,
  588. NULL,
  589. InterChCodeSet,
  590. NULL,
  591. &to_codeset );
  592. }
  593. free(ret_locale);
  594. free(ret_lang);
  595. free(ret_codeset);
  596. *to = NULL;
  597. *to_len = 0;
  598. if ( shouldAlloc2 ) {
  599. /* Obtain work area */
  600. _i18nwork2 = (size_t *)malloc( WORKSIZE );
  601. if ( !_i18nwork2 ) {
  602. _i18nwork2 = NULL;
  603. return( isASCII );
  604. }
  605. _i18nsize2 = WORKSIZE;
  606. shouldAlloc2 = 0;
  607. }
  608. if (NULL == to_codeset || NULL == from_codeset)
  609. return( isASCII );
  610. if ( ( CD = iconv_open( to_codeset, from_codeset ) ) != (iconv_t)-1 ) {
  611. /*
  612. * According to several RFCs( 822, 1468, 1557, ..... )
  613. * the escape sequence to switch to ASCII is needed just before
  614. * '\n'. IBM-eucJP/IBM-932 <--> fold 7 does while the other doesn't.
  615. * Therefore CvtStr() does take care of this here.
  616. */
  617. if ( dir == INTERNET_TO_CURRENT ) {
  618. _converter_( CD, from, from_len, to, to_len );
  619. } else {
  620. void *new_from = from;
  621. unsigned long new_from_len = from_len;
  622. unsigned long _passed = 0;
  623. size_t clen;
  624. void *_tmp = NULL;
  625. unsigned long _tmp_len = 0;
  626. while ( _passed < from_len ) {
  627. /*
  628. * Find \n or \0
  629. */
  630. for ( ; _passed < from_len; _passed += clen ) {
  631. clen = mblen(&(((char *)from)[_passed]), MB_CUR_MAX);
  632. if ( clen < 0 )
  633. break;
  634. if ( ( clen > 1 ) || !isascii( ((char*)from)[_passed] ) ){
  635. /* Here, maybe MB or non-ASCII */
  636. isASCII = 0;
  637. isStopASCII = 0;
  638. } else {
  639. if ( ( ((char*)from)[_passed] != '\n' ) &&
  640. ( ((char*)from)[_passed] != '\0' ) ) {
  641. isStopASCII = ~0;
  642. }
  643. }
  644. if ( ((char*)from)[_passed] == '\n' ||
  645. ((char*)from)[_passed] == '\0' )
  646. break;
  647. }
  648. new_from_len = &(((char *)from)[_passed])-(char *)new_from;
  649. if ( ( _passed < from_len ) && ( clen == 1 ) &&
  650. ( ((char*)from)[_passed] == '\n' ) ) {
  651. new_from_len++;
  652. }
  653. /*
  654. * new_from from[_passed]
  655. * V V
  656. * +------------------------+--+------.................+
  657. * | | |\n| | |
  658. * +------------------------+--+------.................+
  659. * <-------------------------> $
  660. * new_from_len next new_from
  661. * <------------------------------------------------->
  662. * $ from_len
  663. * from
  664. */
  665. /*
  666. * ********** DO 1 LINE CONVERSION **********
  667. */
  668. _tmp = NULL; _tmp_len = 0;
  669. _converter_( CD, new_from, new_from_len, &_tmp, &_tmp_len );
  670. if ( ( _tmp == NULL ) && ( _tmp_len == 0 ) ) {
  671. /* Conversion fail */
  672. *to = NULL;
  673. *to_len = 0;
  674. break;
  675. }
  676. /*
  677. * _i18nwork2 _tmp
  678. * V V
  679. * +-----------------------+ +-------------+
  680. * |XXXXXXXX | + | |
  681. * +-----------------------+ +-------------+
  682. * <------>
  683. * converted_num
  684. * <---------------------> <----------->
  685. * _18nsize2 _tmp_len
  686. */
  687. /* Append _tmp to target */
  688. if ( converted_num + _tmp_len > _i18nsize2 ) {
  689. /* Need much memory..... */
  690. void *_i18n = NULL;
  691. _i18nsize2 += WORKSIZE;
  692. _i18n = realloc( _i18nwork2, _i18nsize2 );
  693. if ( !_i18n ) {
  694. *to = NULL;
  695. *to_len = 0;
  696. _i18nwork2 = NULL;
  697. _i18nsize2 = 0;
  698. shouldAlloc2 = ~0;
  699. break;
  700. } else {
  701. _i18nwork2 = _i18n;
  702. }
  703. }
  704. /*
  705. * _i18nwork2 _tmp
  706. * V v
  707. * +---------------------------+
  708. * |XXXXXXXXXXX(COPIED)XX |
  709. * +---------------------------+
  710. * <---------><-------->
  711. * (old)conv. _tmp_len
  712. * <------------------->
  713. * (new)converted_num
  714. * <-------------------------->
  715. * _i18nsize2
  716. */
  717. strncpy( (char *)_i18nwork2 + converted_num,
  718. (char *)_tmp, _tmp_len );
  719. converted_num += _tmp_len;
  720. *to = (void *)_i18nwork2;
  721. *to_len = converted_num;
  722. new_from = &(((char *)from)[_passed]) + 1;
  723. _passed++;
  724. /*
  725. * According to RFC1468, if the line is ended with non-ASCII
  726. * char, but not not switch to ASCII before the end of line,
  727. * we must switch to ASCII just before the end of the line.
  728. *
  729. * _i18nwork2 ToASCII
  730. * V ??? V
  731. * +---------------------------+ +------+
  732. * |XXXXXXXXXXXXXXXXXXXXX | + |1b2842|
  733. * +---------------------------+ +------+
  734. * <-------------------> <---->
  735. * converted_num ToASCII_NUM
  736. * <------------------------->
  737. * _i18nsize2
  738. * ========
  739. * 1b 28 42 ??
  740. *
  741. */
  742. if ( !isStopASCII ) {
  743. if ( (((char *)_i18nwork2)[converted_num-1] == '\n') ||
  744. (((char *)_i18nwork2)[converted_num-1] == '\0') ) {
  745. char _p = ((char *)_i18nwork2)[converted_num-1];
  746. if (!((converted_num >=3+1 ) &&
  747. !memcmp((void *)((char *)_i18nwork2+converted_num-3-1),
  748. ToASCII, ToASCII_NUM )) ){
  749. /* if not ToASCII there, then */
  750. /* Append ToASCII */
  751. if ( converted_num + ToASCII_NUM > _i18nsize2 ) {
  752. /* Need much memory..... */
  753. void *_i18n = NULL;
  754. _i18nsize2 += WORKSIZE;
  755. _i18n=realloc(_i18nwork2,_i18nsize2);
  756. if ( !_i18n ) {
  757. *to = NULL;
  758. *to_len = 0;
  759. shouldAlloc2 = ~0;
  760. break;
  761. } else {
  762. _i18nwork2 = _i18n;
  763. }
  764. }
  765. strncpy( (char *)_i18nwork2+converted_num-1,
  766. ToASCII, ToASCII_NUM );
  767. converted_num += ToASCII_NUM;
  768. ((char *)_i18nwork2)[converted_num-1] = _p;
  769. *to = _i18nwork2;
  770. *to_len = converted_num;
  771. isStopASCII = ~0;
  772. }
  773. } else {
  774. if (!((converted_num >=3 ) &&
  775. !memcmp((void *)((char *)_i18nwork2+converted_num-3),
  776. ToASCII, ToASCII_NUM )) ){
  777. /*
  778. // if not ToASCII there, then
  779. // Append ToASCII
  780. */
  781. if ( converted_num + ToASCII_NUM > _i18nsize2 ) {
  782. void *_i18n = NULL;
  783. _i18nsize2 += WORKSIZE;
  784. _i18n=realloc(_i18nwork2,_i18nsize2);
  785. if ( !_i18n ) {
  786. *to = NULL;
  787. *to_len = 0;
  788. shouldAlloc2 = ~0;
  789. break;
  790. } else {
  791. _i18nwork2 = _i18n;
  792. }
  793. }
  794. strncpy( (char *)_i18nwork2+converted_num,
  795. ToASCII, ToASCII_NUM );
  796. converted_num += ToASCII_NUM;
  797. *to = _i18nwork2;
  798. *to_len = converted_num;
  799. isStopASCII = ~0;
  800. }
  801. }
  802. }
  803. }
  804. /*
  805. * Again........
  806. */
  807. if( ( *to != NULL ) && ( *to_len != 0 ) ) {
  808. if ( !isStopASCII ) {
  809. if ( (((char *)_i18nwork2)[converted_num-1] == '\n') ||
  810. (((char *)_i18nwork2)[converted_num-1] == '\0') ) {
  811. char _p = ((char *)_i18nwork2)[converted_num-1];
  812. if (!((converted_num >=3+1 ) &&
  813. !memcmp((void *)((char *)_i18nwork2+converted_num-3-1),
  814. ToASCII, ToASCII_NUM )) ){
  815. /* if not ToASCII there, then
  816. // Append ToASCII
  817. */
  818. if ( converted_num + ToASCII_NUM > _i18nsize2 ) {
  819. void *_i18n = NULL;
  820. _i18nsize2 += WORKSIZE;
  821. _i18n=realloc(_i18nwork2,_i18nsize2);
  822. if ( !_i18n ) {
  823. *to = NULL;
  824. *to_len = 0;
  825. shouldAlloc2 = ~0;
  826. } else {
  827. _i18nwork2 = _i18n;
  828. }
  829. }
  830. strncpy( (char *)_i18nwork2+converted_num-1,
  831. ToASCII, ToASCII_NUM );
  832. converted_num += ToASCII_NUM;
  833. ((char *)_i18nwork2)[converted_num-1] = _p;
  834. *to = _i18nwork2;
  835. *to_len = converted_num;
  836. isStopASCII = ~0;
  837. }
  838. } else {
  839. if (!((converted_num >=3 ) &&
  840. !memcmp((void *)((char *)_i18nwork2+converted_num-3),
  841. ToASCII, ToASCII_NUM )) ){
  842. /* if not ToASCII there, then
  843. // Append ToASCII
  844. */
  845. if ( converted_num + ToASCII_NUM > _i18nsize2 ) {
  846. void *_i18n = NULL;
  847. _i18nsize2 += WORKSIZE;
  848. _i18n=realloc(_i18nwork2,_i18nsize2);
  849. if ( !_i18n ) {
  850. *to = NULL;
  851. *to_len = 0;
  852. shouldAlloc2 = ~0;
  853. } else {
  854. _i18nwork2 = _i18n;
  855. }
  856. }
  857. strncpy( (char *)_i18nwork2+converted_num,
  858. ToASCII, ToASCII_NUM );
  859. converted_num += ToASCII_NUM;
  860. *to = _i18nwork2;
  861. *to_len = converted_num;
  862. isStopASCII = ~0;
  863. }
  864. }
  865. }
  866. }
  867. /*
  868. * If InterChCodeSet is either ISO-2022-JP/ISO-2022-KR/ISO-2022-TW
  869. * ISO-2022-CN, strip MSB here since iconv'ed UDC has MSB set to 1
  870. */
  871. if ( !strncasecmp( InterChCodeSet, "ISO-2022-JP", 11 ) ||
  872. !strncasecmp( InterChCodeSet, "ISO-2022-KR", 11 ) ||
  873. !strncasecmp( InterChCodeSet, "ISO-2022-TW", 11 ) ||
  874. !strncasecmp( InterChCodeSet, "ISO-2022-CN", 11 ) ) {
  875. int _i;
  876. for ( _i = 0; _i < *to_len; _i++ ) {
  877. ((unsigned char *)(*to))[_i] &= (unsigned char)0x7f;
  878. }
  879. }
  880. }
  881. iconv_close( CD );
  882. }
  883. return( isASCII );
  884. }
  885. unsigned int
  886. base64size(const unsigned long len)
  887. {
  888. int b_len = len + (len / 3);
  889. b_len += (b_len / 72 * 2) + 4;
  890. return(b_len);
  891. }
  892. getEncodingType(const char * body,
  893. const unsigned int len,
  894. boolean_t strict_mime)
  895. {
  896. /*
  897. // Our goal here is to produce the most readable, safe encoding.
  898. // We have a couple of parameters that will guide our
  899. // choices:
  900. //
  901. // 1) RFC822 allows lines to be a minimum of 1000 characters,
  902. // but MIME encourages mailers to keep lines to <76 characters
  903. // and use quoted-printable if necessary to achieve this.
  904. //
  905. // 2) The base64 encoding will grow the body size by 33%, and
  906. // also render it unreadable by humans. We don't want to use
  907. // it unless really necessary.
  908. //
  909. // Given the above 2 rules, we want to scan the body part and
  910. // select an encoding. The 3 choices will be decided by:
  911. //
  912. // 1) If the text is 7 bit clean, and all lines are <76 chars,
  913. // then no encoding will be applied.
  914. //
  915. //
  916. // 2) If the text is not 7 bit clean, or there are lines >76 chars,
  917. // and the quoted-printable size is less than the base64 size,
  918. // then quoted-printable will be done.
  919. //
  920. // 3) If 1 & 2 are not true, then base64 will be applied.
  921. //
  922. // If "strict_mime" is false we will only encode if the message
  923. // is not 7 bit clean.
  924. */
  925. int base64_growth = base64size(len) - len;
  926. int qprint_growth = 0;
  927. boolean_t eight_bit = B_FALSE;
  928. boolean_t base64 = B_FALSE;
  929. boolean_t encode = B_FALSE;
  930. Encoding enc;
  931. const char * last_nl = body;
  932. const char * cur;
  933. if (body == NULL || len == 0) {
  934. return(MIME_7BIT);
  935. }
  936. if (strncmp(body, "From ", 5) == 0) {
  937. qprint_growth += 2;
  938. }
  939. for (cur = body; cur < (body + len); cur++) {
  940. if (*cur != (*cur & 0x7f)) {
  941. eight_bit = B_TRUE;
  942. encode = B_TRUE;
  943. qprint_growth += 2;
  944. } else if (*cur == '=' || *cur == 0) {
  945. /*
  946. // These characters don't force encoding, but will be
  947. // encoded if we end up encoding.
  948. qprint_growth += 2;
  949. */
  950. }
  951. if (*cur == '\n') {
  952. if ((cur - last_nl) > 76) {
  953. encode = B_TRUE;
  954. qprint_growth += 2;
  955. }
  956. if ((cur != body && (*(cur - 1) == ' ' || *(cur - 1) == '\t'))) {
  957. encode = B_TRUE;
  958. qprint_growth += 2;
  959. }
  960. if ((cur + 6) < (body + len) &&
  961. strncmp((cur + 1), "From ", 5) == 0) {
  962. encode = B_TRUE;
  963. qprint_growth += 2;
  964. }
  965. last_nl = cur + 1;
  966. }
  967. if (encode && (qprint_growth > base64_growth)) {
  968. base64 = B_TRUE;
  969. break;
  970. }
  971. }
  972. /* Deal with buffers that don't end with a new line. */
  973. if ((cur - last_nl) > 76) {
  974. encode = B_TRUE;
  975. qprint_growth += 2;
  976. }
  977. enc = MIME_7BIT;
  978. if (!strict_mime && !eight_bit) {
  979. /* If strict_mime is off we only encode if we have 8 bit data */
  980. enc = MIME_7BIT;
  981. } else if (encode) {
  982. /* strict_mime is TRUE and we have reason to encode. */
  983. if (base64) {
  984. enc = MIME_BASE64;
  985. } else {
  986. enc = MIME_QPRINT;
  987. }
  988. }
  989. return(enc);
  990. }
  991. void
  992. writeContentHeaders(char * hdr_buf,
  993. const char * type,
  994. const Encoding enc,
  995. const char * digest,
  996. int isAllASCII)
  997. {
  998. char default_charset[64];
  999. strcat(hdr_buf,"Content-Type: ");
  1000. strcat(hdr_buf,type);
  1001. if (isAllASCII)
  1002. strcpy(default_charset,"US-ASCII");
  1003. else
  1004. getCharSet(default_charset);
  1005. strcat(hdr_buf,"; charset=");
  1006. strcat(hdr_buf,default_charset);
  1007. crlf(hdr_buf);
  1008. strcat(hdr_buf,"Content-Transfer-Encoding: ");
  1009. switch (enc) {
  1010. case MIME_7BIT:
  1011. strcat(hdr_buf,"7bit\n");
  1012. break;
  1013. case MIME_8BIT:
  1014. default: /* Assume the worst. */
  1015. strcat(hdr_buf,"8bit\n");
  1016. break;
  1017. case MIME_QPRINT:
  1018. strcat(hdr_buf,"quoted-printable\n");
  1019. break;
  1020. case MIME_BASE64:
  1021. strcat(hdr_buf,"base64\n");
  1022. break;
  1023. }
  1024. strcat(hdr_buf,"Content-MD5: ");
  1025. writeBase64(hdr_buf, digest, 16);
  1026. }
  1027. /*
  1028. //
  1029. // Base64 Alphabet (65-character subset of US-ASCII as per RFC1521)
  1030. //
  1031. */
  1032. static const char base64_chars[] =
  1033. {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  1034. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
  1035. 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  1036. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
  1037. '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
  1038. };
  1039. void
  1040. writeBase64(char * buf, const char * bp, const unsigned long len)
  1041. {
  1042. /* The length has to be a multiple of 3. We will need to pad
  1043. // any extra. Let's just work on the main body and save the
  1044. // fractional stuff for the end.
  1045. */
  1046. unsigned long main_len = len - (len % 3);
  1047. const unsigned char * ubp = (const unsigned char *)bp;
  1048. char line[80];
  1049. unsigned int enc_char;
  1050. int lf = 0;
  1051. int block;
  1052. if (bp == NULL || len == 0) {
  1053. crlf(buf);
  1054. return;
  1055. }
  1056. for (block = 0; block < main_len; block += 3) {
  1057. enc_char = (ubp[block] >> 2) & 0x3f;
  1058. line[lf++] = base64_chars[enc_char];
  1059. enc_char = ((ubp[block] & 0x3) << 4) | ((ubp[block+1] >> 4) & 0xf);
  1060. line[lf++] = base64_chars[enc_char];
  1061. enc_char = ((ubp[block + 1] & 0xf) << 2) | ((ubp[block + 2] >> 6) & 0x3);
  1062. line[lf++] = base64_chars[enc_char];
  1063. enc_char = ubp[block + 2] & 0x3f;
  1064. line[lf++] = base64_chars[enc_char];
  1065. if (lf == 72) {
  1066. strncat(buf,line,lf);
  1067. crlf(buf);
  1068. lf = 0;
  1069. }
  1070. }
  1071. if (lf > 0) {
  1072. strncat(buf, line,lf);
  1073. }
  1074. if (((lf + 4) % 72) == 0) {
  1075. crlf(buf);
  1076. }
  1077. switch(len % 3) {
  1078. case 1:
  1079. enc_char = (ubp[block] >> 2) & 0x3f ;
  1080. strncat(buf, &base64_chars[enc_char], 1);
  1081. enc_char = ((ubp[block] & 0x3) << 4);
  1082. strncat(buf, &base64_chars[enc_char], 1);
  1083. strncat(buf,"==", 2);
  1084. break;
  1085. case 2:
  1086. enc_char = (ubp[block] >> 2) & 0x3f;
  1087. strncat(buf,&base64_chars[enc_char], 1);
  1088. enc_char = ((ubp[block] & 0x3) << 4) | ((ubp[block+1] >> 4) & 0xf);
  1089. strncat(buf,&base64_chars[enc_char], 1);
  1090. enc_char = ((ubp[block + 1] & 0xf) << 2);
  1091. strncat(buf,&base64_chars[enc_char], 1);
  1092. strncat(buf,"=", 1);
  1093. }
  1094. /* crlf(buf); */
  1095. }
  1096. void
  1097. writeQPrint(char *buf, const char * bp, const unsigned long bp_len,
  1098. int is_Special )
  1099. {
  1100. int last_nl = 0;
  1101. int off = 0;
  1102. char line_buf[80];
  1103. const char * start;
  1104. const char * cur;
  1105. const char * white;
  1106. const char * nw;
  1107. int line_len;
  1108. const char *cp_w;
  1109. char prev;
  1110. char tmpbuf[20];
  1111. if (bp == NULL || bp_len == 0) {
  1112. crlf(buf);
  1113. return;
  1114. }
  1115. /*
  1116. // A line buffer for improving formatting performance. Note that
  1117. // QP requires all lines to be < 72 characters plus CRLF. So, a
  1118. // fixed size 80 character buffer is safe.
  1119. */
  1120. /* There are probably more elegant ways to deal with a message that
  1121. // begins with "From ", but we will simply due it this more simplistic
  1122. // way.
  1123. */
  1124. if (strncmp(bp, "From ", 5) == 0) {
  1125. memcpy(&line_buf[off], "=46", 3);
  1126. start = bp + 1;
  1127. off += 3;
  1128. }
  1129. else {
  1130. start = bp;
  1131. }
  1132. /* This loop will apply the encodings, following the rules identified
  1133. // in RFC1521 (though not necessarily in the order presented.
  1134. */
  1135. for (cur = start; cur < (bp + bp_len); cur++) {
  1136. /* Rule #5: Part 1! We will try to break at white space
  1137. // if possible, but it may not be possible. In any case,
  1138. // we want to force the lines to be less than 76 characters.
  1139. */
  1140. if (off > 72) {
  1141. line_buf[off++] = '=';
  1142. strncat(buf,line_buf, off);
  1143. crlf(buf);
  1144. last_nl = 0;
  1145. off = 0;
  1146. }
  1147. /* Rule #1: Any octet, except those indicating a line break
  1148. // according to the newline convention mabe represented by
  1149. // an = followed by a two digit hexadecimal representation
  1150. // of the octet's value. We will represent any non-7bit
  1151. // data this way, but let the rest slide. We do wrap "="
  1152. // just to be safe.
  1153. */
  1154. if (*cur != (*cur & 0x7f) || *cur == '=') {
  1155. char tmp[20];
  1156. sprintf(tmp, "=%02X", (int)(unsigned char)*cur);
  1157. memcpy(&line_buf[off], tmp, 3);
  1158. off += 3;
  1159. continue;
  1160. }
  1161. if ( is_Special ){
  1162. /*
  1163. * Under ISO-2022-XX codeset, several escape sequence may be in
  1164. * From, Subject field. To pass them, writeQPrint() also accept
  1165. * such kind of character.
  1166. */
  1167. if ( *cur == (char)0x1b ) {
  1168. /* Only 0x1b ????? */
  1169. char tmp[3];
  1170. sprintf(tmp, "=%02X", (int)(unsigned char)*cur);
  1171. memcpy(&line_buf[off], tmp, 3);
  1172. off += 3;
  1173. continue;
  1174. }
  1175. }
  1176. /* Rule #2: Octets with decimal values of 33 through 60
  1177. // inclusive and 62 through 126, inclusive, MAY be represented
  1178. // as the ASCII characters which correspond to those octets.
  1179. */
  1180. if ((*cur >= 33 && *cur <= 60) ||
  1181. (*cur >= 62 && *cur <= 126)) {
  1182. line_buf[off++] = *cur;
  1183. continue;
  1184. }
  1185. /* Rule #5: The q-p encoding REQUIRES that encoded lines be
  1186. // no more than 76 characters long. If longer, an equal sign
  1187. // as the last character n the line indicates a soft line break.
  1188. //
  1189. // This is tricky if you want to leave it reasonably readable
  1190. // (why else do this?). We only want to break on white space.
  1191. // At each white gap, we need to count forward to the next
  1192. // white gap and see if we exceed the 76 character limit.
  1193. // We will cheat a few characters to allow us some room
  1194. // for arithmetic.
  1195. */
  1196. if (*cur == ' ' || *cur == '\t') {
  1197. /* Find the end of this clump of white space.
  1198. */
  1199. for (nw = cur;
  1200. nw < (bp + bp_len) && *nw && *nw != '\n'; nw++) {
  1201. if (!isspace(*nw)) {
  1202. break;
  1203. }
  1204. }
  1205. /* Find the end of the next non-white region.
  1206. */
  1207. for (white = nw;
  1208. white < (bp + bp_len) && *white && !isspace(*white);
  1209. white++) {
  1210. continue;
  1211. }
  1212. line_len = (off - last_nl) + (white - cur);
  1213. if (line_len > 72) {
  1214. /* Need a soft line break. Lets put it after the
  1215. // current clump of white space. We will break
  1216. // at 72 characters, even if we arent at the end
  1217. // of the white space. This prevents buffer overruns.
  1218. */
  1219. for (cp_w = cur; cp_w < nw; cp_w++) {
  1220. line_buf[off++] = *cp_w;
  1221. if (off > 72) {
  1222. line_buf[off++] = '=';
  1223. strncat(buf,line_buf, off);
  1224. crlf(buf);
  1225. off = 0;
  1226. last_nl = 0;
  1227. }
  1228. }
  1229. /* There is an edge case that we may have written the last
  1230. // white space character in the for loop above. This will
  1231. // prevent us from spitting an extra continuation line.
  1232. */
  1233. if (off) {
  1234. line_buf[off++] = '=';
  1235. strncat(buf,line_buf, off);
  1236. crlf(buf);
  1237. last_nl = 0;
  1238. off = 0;
  1239. }
  1240. /* If we created a "From " at the front we need to wrap
  1241. // it to protect from parsers.
  1242. */
  1243. if ((nw + 5) < (bp + bp_len) && strncmp(nw, "From ", 5) == 0) {
  1244. memcpy(&line_buf[off], "=46", 3);
  1245. off += 3;
  1246. cur = nw + 1;
  1247. }
  1248. else {
  1249. cur = nw - 1;
  1250. }
  1251. }
  1252. else {
  1253. line_buf[off++] = *cur;
  1254. }
  1255. continue;
  1256. }
  1257. /* Rule 3: Octets with values of 9 and 32 MAY be represented
  1258. // as ASCII TAB and SPACE but MUST NOT be represented at the
  1259. // end of an encoded line. We solve this be encoding the last
  1260. // white space before a new line (except a new line) using
  1261. // Rule #1.
  1262. */
  1263. if (*cur == '\n') {
  1264. if (cur == start) {
  1265. crlf(buf);
  1266. }
  1267. else {
  1268. last_nl = off + 1;
  1269. prev = *(cur - 1);
  1270. if ((prev == ' ' || prev == '\t') && prev != '\n') {
  1271. off = off ? off - 1 : off;
  1272. sprintf(tmpbuf, "=%02X", *(cur - 1));
  1273. memcpy(&line_buf[off], tmpbuf, 3);
  1274. off += 3;
  1275. }
  1276. strncat(buf,line_buf, off);
  1277. last_nl = 0;
  1278. off = 0;
  1279. if (*(cur - 1) == '\r') {
  1280. strncat(buf,cur, 1);
  1281. }
  1282. /*
  1283. else {
  1284. crlf(buf);
  1285. }
  1286. */
  1287. }
  1288. /* We need to munge a line that starts with "From " to it
  1289. // protect from parsers. The simplest way is to encode the
  1290. // "F" using rule #1.
  1291. */
  1292. if ((cur + 5) < (bp + bp_len) && strncmp((cur + 1), "From ", 5) == 0) {
  1293. memcpy(&line_buf[off], "=46", 3);
  1294. off += 3;
  1295. cur += 1;
  1296. }
  1297. continue;
  1298. }
  1299. }
  1300. if (off > 0) {
  1301. strncat(buf,line_buf, off);
  1302. }
  1303. /*
  1304. if (*(cur - 1) != '\n') {
  1305. crlf(buf);
  1306. }
  1307. */
  1308. }
  1309. static int
  1310. mbisspace(int c)
  1311. {
  1312. return((c & 0x7f) == c && isspace(c));
  1313. }
  1314. void
  1315. rfc1522cpy(char * buf, const char * value)
  1316. {
  1317. const char * cur;
  1318. const char * scan_c;
  1319. boolean_t eight_bit = B_FALSE;
  1320. char charset[64];
  1321. char *ret_locale = NULL;
  1322. char *ret_lang = NULL;
  1323. char *ret_codeset = NULL;
  1324. char tmp[1024];
  1325. char *NewBuf = NULL;
  1326. unsigned long _len = 0;
  1327. /*
  1328. // We are going to encode 8 bit data, one word at a time. This may
  1329. // not be the best possible algorithm, but it will get the correct
  1330. // information in the header.
  1331. */
  1332. for (cur = value; *cur; cur++) {
  1333. if (mbisspace(*cur)) {
  1334. strncat(buf,cur, 1);
  1335. continue;
  1336. }
  1337. for (scan_c = cur; *scan_c && !mbisspace(*scan_c); scan_c++) {
  1338. if (*scan_c != (*scan_c & 0x7f)) {
  1339. eight_bit = B_TRUE;
  1340. }
  1341. }
  1342. if (eight_bit == B_FALSE) {
  1343. /* Simple! Copy the chars to the output. */
  1344. strncat(buf,cur,scan_c - cur);
  1345. cur = scan_c - 1;
  1346. }
  1347. else {
  1348. getCharSet( charset );
  1349. /* Convert default_charset to InterchangeCodeset name. */
  1350. DtXlateOpToStdLocale( DtLCX_OPER_MIME,
  1351. charset,
  1352. &ret_locale,
  1353. &ret_lang,
  1354. &ret_codeset );
  1355. /* We have a word here. It has 8 bit data, so we will put
  1356. // it out as RFC1522 chunk.
  1357. */
  1358. if ( !strncasecmp( ret_codeset, "ISO-2022-KR", 11 ) ) {
  1359. /*
  1360. * According to RFC1557, in the Header Field, we don't use
  1361. * ISO-2022-KR encoding char.
  1362. */
  1363. char *_tmp1_;
  1364. char *_tmp2_;
  1365. free(ret_locale);
  1366. free(ret_lang);
  1367. ret_locale = ret_lang = _tmp1_ = NULL;
  1368. DtXlateOpToStdLocale( DtLCX_OPER_INTERCHANGE_CODESET,
  1369. ret_codeset,
  1370. &ret_locale,
  1371. &ret_lang,
  1372. &_tmp1_ );
  1373. _tmp2_ = NULL;
  1374. DtXlateStdToOpLocale( DtLCX_OPER_MIME,
  1375. NULL,
  1376. NULL,
  1377. _tmp1_,
  1378. NULL,
  1379. &_tmp2_ );
  1380. strncat(buf,"=?", 2);
  1381. strncat(buf,_tmp2_, strlen(_tmp2_));
  1382. strncat(buf,"?q?", 3);
  1383. free(_tmp1_);
  1384. free(_tmp2_);
  1385. } else if ( !strncasecmp( ret_codeset, "ISO-2022-JP", 11 ) ) {
  1386. /*
  1387. * According to RFC1468, in the Header Field, we should use
  1388. * B-encoding
  1389. */
  1390. strncat(buf,"=?", 2);
  1391. strncat(buf,charset, strlen(charset));
  1392. strncat(buf,"?b?", 3);
  1393. } else {
  1394. strncat(buf,"=?", 2);
  1395. strncat(buf,charset, strlen(charset));
  1396. strncat(buf,"?q?", 3);
  1397. }
  1398. /*
  1399. * According to RFC1557, in the Header Field, we don't use
  1400. * ISO-2022-KR encoding char. Also in us-ascci, we don't have to
  1401. * call converter.
  1402. */
  1403. memset(tmp, '\0', 1024);
  1404. if (!( !strncasecmp( ret_codeset, "ISO-2022-KR", 11 ) ||
  1405. !strncasecmp( charset, "us-ascii", 8 ) ) )
  1406. (void)CvtStr( (char *)NULL, (void *)cur, scan_c - cur,
  1407. (void **)&NewBuf, &_len, CURRENT_TO_INTERNET );
  1408. if ( ( NewBuf != NULL ) && ( _len != 0 ) ) {
  1409. /*
  1410. * if ret_codeset == ISO-2022-KR, we don't come here.
  1411. */
  1412. /*
  1413. * According to RFC1468, we should use B-encoding.
  1414. */
  1415. if ( !strncasecmp( ret_codeset, "ISO-2022-JP", 11 ) ) {
  1416. writeBase64( tmp, NewBuf, _len );
  1417. } else {
  1418. writeQPrint( tmp, NewBuf, _len,
  1419. (!strncasecmp( ret_codeset, "ISO-2022-TW", 11 ) ||
  1420. !strncasecmp( ret_codeset, "ISO-2022-CN", 11 ) ));
  1421. }
  1422. } else
  1423. writeQPrint( tmp, cur, scan_c - cur, 0 );
  1424. strncat(buf,tmp,strlen(tmp));
  1425. strcat(buf,"?=");
  1426. cur = scan_c - 1;
  1427. }
  1428. }
  1429. free(ret_codeset);
  1430. free(ret_locale);
  1431. free(ret_lang);
  1432. crlf(buf);
  1433. }