Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

620 lines
14 KiB

  1. /*
  2. * Win3.1J EUDC fontfile i/o ( MS-Code base)
  3. *
  4. * Copyright (c) 1997-1999 Microsoft Corporation.
  5. */
  6. #include "stdafx.h"
  7. #pragma pack(2)
  8. #include "extfunc.h"
  9. /*
  10. File Structure */
  11. struct W31_Header {
  12. char identify[72];
  13. short segCnt; /* ??? */
  14. unsigned short sCode,
  15. eCode;
  16. short cCnt;
  17. long ofsCmap;
  18. short sizCmap;
  19. long ofsFil;
  20. short sizFil;
  21. long ofsStbl; /* search tbl*/
  22. short sizStbl;
  23. long ofsBdatSub;
  24. };
  25. struct BDatSubTbl {
  26. long tail;
  27. long ptrOfs;
  28. long head;
  29. short filler2;
  30. /* Following Pointer tbl. */
  31. };
  32. struct BMPHeader {
  33. long bitmapSiz;
  34. short xsiz, ysiz;
  35. };
  36. #define EUDCCODEBASE ((unsigned short)0xe000)
  37. int OpenW31JBMP(TCHAR *path,int omd);
  38. int CloseW31JBMP(void);
  39. int isW31JEUDCBMP(TCHAR *path);
  40. int GetW31JBMPnRecs(int *nRec, int *nGlyph, int *xsiz, int *ysiz);
  41. int GetW31JBMPMeshSize( int *xsiz, int *ysiz);
  42. static int readcmap(void);
  43. static int rectocode(int rec,unsigned short *code);
  44. static int searchCode(unsigned short code);
  45. int GetW31JBMP(unsigned short code,LPBYTE buf,int bufsiz,int *xsiz,int *ysiz);
  46. int GetW31JBMPRec(int rec,LPBYTE buf,int bufsiz,int *xsiz,int *ysiz,unsigned short *code);
  47. int PutW31JBMPRec(int rec,LPBYTE buf,int xsiz,int ysiz);
  48. static int ReadBMPHdr(HANDLE hdl,long ofs,struct BMPHeader *bhdr);
  49. static int WriteBMPHdr(HANDLE hdl,long ofs,struct BMPHeader *bhdr);
  50. static int init = 0;
  51. static HANDLE fHdl;
  52. struct W31_Header hdr;
  53. struct BDatSubTbl bdTbl;
  54. static int rwmode = 0;
  55. static long *ofstbl=0;
  56. static unsigned short *cmap=0;
  57. static int *recordTbl=0;
  58. /***************************************************************
  59. * Initialize
  60. */
  61. /* */ int
  62. /* */ OpenW31JBMP( TCHAR *path, int omd)
  63. /*
  64. * returns : 0, -1
  65. ***************************************************************/
  66. {
  67. int msiz;
  68. DWORD nByte;
  69. BOOL res;
  70. if ( !path)
  71. {
  72. return -1;
  73. }
  74. /* open EUDC Font File */
  75. rwmode = omd ? 1 : 0;
  76. fHdl = CreateFile(path,
  77. omd==0 ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
  78. FILE_SHARE_READ | FILE_SHARE_WRITE,
  79. NULL,
  80. OPEN_EXISTING,
  81. FILE_ATTRIBUTE_NORMAL,
  82. NULL);
  83. if ( fHdl == INVALID_HANDLE_VALUE)
  84. return -1;
  85. /* Read Header */
  86. res = ReadFile( fHdl, (LPBYTE)&hdr, sizeof(struct W31_Header), &nByte, NULL);
  87. if (!res || nByte !=sizeof(struct W31_Header))
  88. goto ERET;
  89. //
  90. // WinSE #13986,
  91. // In Win9x, character number could be well over 1880,
  92. // EUDC EUF file will be trashed if we limit character number to 1880
  93. //
  94. /*
  95. if( hdr.cCnt > 1880)
  96. hdr.cCnt = 1880;
  97. */
  98. /* allocate ofs. tbl. */
  99. msiz = hdr.cCnt*sizeof(long);
  100. if ((ofstbl = (long *)malloc( msiz))==(long *)0)
  101. goto ERET;
  102. /* Read Ofs. tbl.*/
  103. if ( (long) SetFilePointer( fHdl, hdr.ofsBdatSub, NULL, FILE_BEGIN)!=hdr.ofsBdatSub)
  104. goto ERET;
  105. res = ReadFile( fHdl, (LPBYTE)&bdTbl, sizeof(bdTbl), &nByte, NULL);
  106. if (!res || nByte !=sizeof(bdTbl))
  107. goto ERET;
  108. res = ReadFile( fHdl, (LPBYTE)ofstbl, (unsigned int)msiz, &nByte, NULL);
  109. if (!res || nByte !=(unsigned int)msiz)
  110. goto ERET;
  111. init = 1;
  112. /*
  113. if (fHdl != INVALID_HANDLE_VALUE)
  114. {
  115. CloseHandle(fHdl);
  116. fHdl = INVALID_HANDLE_VALUE;
  117. }
  118. */
  119. return 0;
  120. ERET:
  121. if (fHdl != INVALID_HANDLE_VALUE)
  122. {
  123. CloseHandle (fHdl);
  124. fHdl = INVALID_HANDLE_VALUE;
  125. }
  126. if (ofstbl)
  127. {
  128. free( ofstbl);
  129. ofstbl = 0;
  130. }
  131. return -1;
  132. }
  133. /***************************************************************
  134. * Terminate Close
  135. */
  136. /* */ int
  137. /* */ CloseW31JBMP()
  138. /*
  139. * returns : none
  140. ***************************************************************/
  141. {
  142. unsigned int siz;
  143. DWORD nByte;
  144. BOOL res;
  145. if ( rwmode>=1) {
  146. /* update ofstbl*/
  147. if ((long) SetFilePointer( fHdl, hdr.ofsBdatSub, NULL, FILE_BEGIN)!=hdr.ofsBdatSub)
  148. goto ERET;
  149. res = WriteFile( fHdl, (LPBYTE)&bdTbl, sizeof( bdTbl), &nByte, NULL);
  150. if (!res || nByte !=sizeof(bdTbl))
  151. goto ERET;
  152. siz = (unsigned int)hdr.cCnt*sizeof(long);
  153. res = WriteFile( fHdl, (LPBYTE)ofstbl, siz, &nByte, NULL);
  154. if (!res || nByte !=siz)
  155. goto ERET;
  156. }
  157. if ( fHdl !=INVALID_HANDLE_VALUE) {
  158. CloseHandle( fHdl);
  159. fHdl = INVALID_HANDLE_VALUE;
  160. }
  161. if ( ofstbl) {
  162. free(ofstbl);
  163. ofstbl = 0;
  164. }
  165. if ( cmap) {
  166. free(cmap);
  167. cmap = 0;
  168. }
  169. if ( recordTbl) {
  170. free(recordTbl);
  171. recordTbl = 0;
  172. }
  173. init = 0;
  174. return 0;
  175. ERET:
  176. return -1;
  177. }
  178. /***************************************************************
  179. * is Win3.1J EUDC bitmap
  180. */
  181. /* */ int
  182. /* */ isW31JEUDCBMP( TCHAR *path)
  183. /*
  184. * returns : 0 (other), 1 (EUDC bitmap), -1(error)
  185. ***************************************************************/
  186. {
  187. HANDLE fhdl;
  188. struct W31_Header hdr31;
  189. DWORD nByte;
  190. BOOL res;
  191. if (!path)
  192. {
  193. return -1;
  194. }
  195. fhdl = CreateFile(path,
  196. GENERIC_READ,
  197. FILE_SHARE_READ | FILE_SHARE_WRITE,
  198. NULL,
  199. OPEN_EXISTING,
  200. FILE_ATTRIBUTE_NORMAL,
  201. NULL);
  202. if ( fhdl == INVALID_HANDLE_VALUE)
  203. return -1;
  204. res = ReadFile( fhdl, (LPBYTE)&hdr31, sizeof(hdr31), &nByte, NULL);
  205. if (!res || nByte !=sizeof(hdr31))
  206. goto NO_WIN31J;
  207. CloseHandle( fhdl);
  208. fhdl = INVALID_HANDLE_VALUE;
  209. /* compare idendify leading 16 byte, sCode, eCode and cCnt*/
  210. if (memcmp( hdr31.identify, "WINEUDC2Standard", 16))
  211. goto NO_WIN31J;
  212. #if 0
  213. if ( hdr31.sCode != 0x40f0 || hdr31.eCode != 0xfcf9 || hdr31.cCnt != 1880)
  214. #endif
  215. if( hdr31.sCode != 0x40f0)
  216. goto NO_WIN31J;
  217. return 1;
  218. NO_WIN31J:
  219. if (fhdl != INVALID_HANDLE_VALUE)
  220. {
  221. CloseHandle(fhdl);
  222. fhdl = INVALID_HANDLE_VALUE;
  223. }
  224. return 0;
  225. }
  226. /***************************************************************
  227. * Get number of records
  228. */
  229. /* */ int
  230. /* */ GetW31JBMPnRecs( int *nRec, int *nGlyph, int *xsiz, int *ysiz)
  231. /*
  232. * returns : 0, -1
  233. ***************************************************************/
  234. {
  235. struct BMPHeader fhdr;
  236. long ofs;
  237. BOOL bFirst;
  238. int rec;
  239. int gc;
  240. DWORD nByte;
  241. BOOL res;
  242. if ((!nRec) || (!nGlyph) || (!xsiz) || (!ysiz))
  243. {
  244. return -1;
  245. }
  246. bFirst = FALSE;
  247. if ( init==0 || fHdl == INVALID_HANDLE_VALUE)
  248. return -1;
  249. else {
  250. gc = 0;
  251. for ( rec = 0; rec < (int)hdr.cCnt; rec++) {
  252. if( *(ofstbl+rec)){
  253. if( !bFirst){
  254. ofs = *(ofstbl+rec);
  255. ofs += hdr.ofsBdatSub;
  256. if ( (DWORD) SetFilePointer( fHdl,ofs, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
  257. {
  258. DWORD dwErr = GetLastError();
  259. goto ERET;
  260. }
  261. res = ReadFile( fHdl, (LPBYTE)&fhdr,
  262. sizeof(struct BMPHeader), &nByte, NULL);
  263. if (!res || nByte != sizeof( struct BMPHeader))
  264. goto ERET;
  265. bFirst = TRUE;
  266. }
  267. gc++;
  268. }
  269. }
  270. *nRec = (int)hdr.cCnt;
  271. *nGlyph = gc;
  272. *xsiz = fhdr.xsiz;
  273. *ysiz = fhdr.ysiz;
  274. return 0;
  275. }
  276. ERET:
  277. return( -1);
  278. }
  279. static int
  280. readcmap()
  281. {
  282. unsigned int msiz;
  283. DWORD nByte;
  284. BOOL res;
  285. msiz = (unsigned int)hdr.cCnt*sizeof(unsigned short);
  286. if ((cmap = (unsigned short*)malloc(msiz))==(unsigned short *)0)
  287. goto ERET;
  288. if ((long) SetFilePointer( fHdl, hdr.ofsCmap, NULL, FILE_BEGIN)!=hdr.ofsCmap)
  289. goto ERET;
  290. res = ReadFile( fHdl, (LPBYTE)cmap, msiz, &nByte, NULL);
  291. if (!res || nByte !=msiz)
  292. goto ERET;
  293. return 0;
  294. ERET:
  295. return -1;
  296. }
  297. static int
  298. rectocode( int rec, unsigned short *code)
  299. {
  300. if ( cmap==0) {
  301. if (readcmap())
  302. return -1;
  303. }
  304. *code = *(cmap+rec);
  305. return 0;
  306. }
  307. static int
  308. searchCode( unsigned short code)
  309. {
  310. int high, low, mid;
  311. if ( cmap==(unsigned short *)0) {
  312. if (readcmap())
  313. goto ERET;
  314. }
  315. high = hdr.cCnt-1;
  316. low = 0;
  317. while ( high >= low) {
  318. mid = (high+low)/2;
  319. if ( *(cmap+mid)==code)
  320. return mid;
  321. else if ( *(cmap+mid)>code)
  322. high = mid-1;
  323. else
  324. low = mid+1;
  325. }
  326. ERET:
  327. return -1;
  328. }
  329. /***************************************************************
  330. * Read Bitmap by code number
  331. */
  332. /* */ int
  333. /* */ GetW31JBMP(
  334. /* */ unsigned short code, /* code Number */
  335. /* */ LPBYTE buf, /* buffer to set bitmap */
  336. /* */ int bufsiz, /* Buffer Size */
  337. /* */ int *xsiz, /* Bitmap X,Ysiz */
  338. /* */ int *ysiz)
  339. /*
  340. * returns : >=0, -1
  341. ***************************************************************/
  342. {
  343. int rec;
  344. int sts;
  345. unsigned short rcode;
  346. /* search code */
  347. if ( (rec = searchCode( code)) <0)
  348. return -1;
  349. else {
  350. sts = GetW31JBMPRec( rec, buf, bufsiz, xsiz, ysiz, &rcode);
  351. return sts;
  352. }
  353. }
  354. /****************************************/
  355. /* */
  356. /* Get W31JEUDC's Bmp Mesh Size */
  357. /* */
  358. /****************************************/
  359. int
  360. GetW31JBMPMeshSize(
  361. int *xsiz,
  362. int *ysiz)
  363. {
  364. long ofs;
  365. struct BMPHeader fhdr;
  366. int bmpsiz;
  367. DWORD nByte;
  368. BOOL res;
  369. if ( (!xsiz) || (!ysiz))
  370. {
  371. goto ERET;;
  372. }
  373. if (init==0)
  374. return -1;
  375. ofs = *(ofstbl);
  376. if ( ofs==0L)
  377. return 0;
  378. ofs += hdr.ofsBdatSub;
  379. if ( (long) SetFilePointer(fHdl, ofs, NULL, FILE_BEGIN)!=ofs)
  380. goto ERET;
  381. res = ReadFile( fHdl, (LPBYTE)&fhdr, sizeof(struct BMPHeader), &nByte, NULL);
  382. if (!res || nByte != sizeof( struct BMPHeader))
  383. goto ERET;
  384. *xsiz = fhdr.xsiz;
  385. *ysiz = fhdr.ysiz;
  386. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  387. return bmpsiz;
  388. ERET:
  389. return (-1);
  390. }
  391. /***************************************************************
  392. * Read Bitmap by record number
  393. */
  394. /* */ int
  395. /* */ GetW31JBMPRec(
  396. /* */ int rec, /* Record Number */
  397. /* */ LPBYTE buf, /* buffer to set bitmap */
  398. /* */ int bufsiz, /* Buffer Size */
  399. /* */ int *xsiz, /* Bitmap X,Ysiz */
  400. /* */ int *ysiz,
  401. /* */ unsigned short *code)
  402. /*
  403. * returns : bitmapsiz >=0, -1
  404. ***************************************************************/
  405. {
  406. long ofs;
  407. struct BMPHeader fhdr;
  408. int bmpsiz;
  409. int rdsiz;
  410. DWORD nByte;
  411. BOOL res;
  412. if ( (!buf) || (!xsiz) || (!ysiz) || (!code) )
  413. {
  414. goto ERET;;
  415. }
  416. if (init==0)
  417. return -1;
  418. ofs = *(ofstbl+rec);
  419. if ( ofs==0L)
  420. return 0;
  421. ofs += hdr.ofsBdatSub;
  422. /* read Bitmap Header
  423. bitmap is Word aligned */
  424. if ( (long) SetFilePointer( fHdl, ofs, NULL, FILE_BEGIN)!=ofs)
  425. goto ERET;
  426. res = ReadFile( fHdl, (LPBYTE)&fhdr, sizeof(struct BMPHeader), &nByte, NULL);
  427. if (!res || nByte != sizeof( struct BMPHeader))
  428. goto ERET;
  429. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  430. /* Read Bitmap Body */
  431. rdsiz = bmpsiz > bufsiz ? bufsiz : bmpsiz;
  432. if ( rdsiz > 0) {
  433. res = ReadFile( fHdl, buf, (unsigned int)rdsiz, &nByte, NULL);
  434. if (!res || nByte !=(unsigned int)rdsiz)
  435. goto ERET;
  436. }
  437. *xsiz = fhdr.xsiz;
  438. *ysiz = fhdr.ysiz;
  439. if ( rectocode( rec, code))
  440. goto ERET;
  441. return bmpsiz;
  442. ERET:
  443. return -1;
  444. }
  445. /***************************************************************
  446. * Write Bitmap by record number
  447. */
  448. /* */ int
  449. /* */ PutW31JBMPRec(
  450. /* */ int rec, /* Record Number */
  451. /* */ LPBYTE buf, /* buffer to set bitmap */
  452. /* */ int xsiz, /* Bitmap X,Ysiz */
  453. /* */ int ysiz)
  454. /*
  455. * returns : 0, -1
  456. ***************************************************************/
  457. {
  458. long ofs;
  459. struct BMPHeader fhdr;
  460. int bmpsiz;
  461. unsigned int wbmpsiz;
  462. DWORD nByte;
  463. BOOL res;
  464. if (!buf)
  465. {
  466. goto ERET;
  467. }
  468. if (init==0)
  469. return -1;
  470. else if ( rwmode==0)
  471. return -1;
  472. rwmode = 2;
  473. wbmpsiz = (unsigned int) ((xsiz+15)/16 *2 * ysiz);
  474. ofs = *(ofstbl+rec);
  475. if ( ofs != 0L) {
  476. /* read Bitmap Header
  477. bitmap is Word aligned */
  478. if ( ReadBMPHdr( fHdl, ofs, &fhdr))
  479. goto ERET;
  480. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  481. if ( bmpsiz<(int)wbmpsiz)
  482. ofs = 0L;
  483. }
  484. if ( ofs == 0L)
  485. ofs = bdTbl.tail;
  486. /* Write Bitmap Header */
  487. fhdr.xsiz = (short)xsiz;
  488. fhdr.ysiz = (short)ysiz;
  489. fhdr.bitmapSiz = wbmpsiz+sizeof(fhdr);
  490. if ( WriteBMPHdr( fHdl, ofs, &fhdr))
  491. goto ERET;
  492. /* Write Bitmap Body */
  493. res = WriteFile( fHdl, buf, wbmpsiz, &nByte, NULL);
  494. if (!res || nByte !=wbmpsiz)
  495. goto ERET;
  496. /* write bitmap ptr on subTable */
  497. *(ofstbl+rec) = ofs;
  498. bdTbl.tail = ofs + wbmpsiz+sizeof(fhdr);
  499. return 0;
  500. ERET:
  501. return -1;
  502. }
  503. static int
  504. ReadBMPHdr( HANDLE hdl, long ofs, struct BMPHeader *bhdr)
  505. {
  506. DWORD nByte;
  507. BOOL res;
  508. if ((hdl == INVALID_HANDLE_VALUE) || (!bhdr))
  509. {
  510. goto ERET;;
  511. }
  512. ofs += hdr.ofsBdatSub;
  513. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!=ofs)
  514. goto ERET;
  515. res = ReadFile( hdl, (LPBYTE) bhdr, sizeof( struct BMPHeader), &nByte, NULL);
  516. if (!res || nByte !=sizeof( struct BMPHeader))
  517. goto ERET;
  518. return 0;
  519. ERET:
  520. return -1;
  521. }
  522. static int
  523. WriteBMPHdr( HANDLE hdl, long ofs, struct BMPHeader *bhdr)
  524. {
  525. DWORD nByte;
  526. BOOL res;
  527. if (( hdl== INVALID_HANDLE_VALUE) || ( !bhdr))
  528. {
  529. goto ERET;;
  530. }
  531. ofs += hdr.ofsBdatSub;
  532. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!=ofs)
  533. goto ERET;
  534. res = WriteFile(hdl, (LPBYTE )bhdr, sizeof( struct BMPHeader), &nByte, NULL);
  535. if (!res || nByte !=sizeof( struct BMPHeader))
  536. goto ERET;
  537. return 0;
  538. ERET:
  539. return -1;
  540. }
  541. int
  542. W31JrecTbl( int **recTbl, BOOL bIsWin95EUDC)
  543. {
  544. int rec;
  545. int *tp;
  546. unsigned short code;
  547. if ( !recTbl)
  548. {
  549. return -1;
  550. }
  551. if ( cmap==0) {
  552. if (readcmap())
  553. return -1;
  554. }
  555. if ( (tp = (int *)malloc( sizeof(int)*hdr.cCnt))==(int *)0)
  556. return -1;
  557. for ( rec = 0; rec < hdr.cCnt; rec++) {
  558. if ( *(ofstbl + rec)!=0L) {
  559. code = *(cmap+rec);
  560. if (!bIsWin95EUDC)
  561. code = sjisToUniEUDC( code);
  562. tp[(int)(code - EUDCCODEBASE)] = rec;
  563. }
  564. else
  565. tp[rec] = -1;
  566. }
  567. *recTbl = recordTbl = tp;
  568. return 0;
  569. }
  570. /* EOF */