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.

615 lines
15 KiB

  1. /*
  2. * CWIN3.1 and ETEN format file i/o
  3. *
  4. * Copyright (c) 1997-1999 Microsoft Corporation.
  5. */
  6. #include "stdafx.h"
  7. #pragma pack(2)
  8. #include "extfunc.h"
  9. #include "eten.h"
  10. #define EUDCCODEBASE ((unsigned short)0xe000)
  11. #define ETENBANKID 0x8001
  12. /*
  13. static unsigned short getsval(unsigned char *s);
  14. static unsigned long getlval(unsigned char *l);
  15. static void setsval(unsigned char *m,unsigned short s);
  16. static void setlval(unsigned char *m,unsigned long lval);
  17. static int readHdr(HANDLE fhdl,struct ETENHEADER *hdr);
  18. static int updHdr(void);
  19. static int getETENBankID( HANDLE fh, WORD *BankID);
  20. int openETENBMP(TCHAR *path,int md);
  21. int closeETENBMP(void);
  22. static void setIniHdr(struct R_ETENHEADER *hdr,int width,int height);
  23. int createETENBMP(TCHAR *path,int wid,int hei);
  24. int getETENBMPInf(int *n, int *ng, int *wid,int *hei, char *sign,WORD *bID);
  25. int readETENBMPRec(int rec,LPBYTE buf,int bufsiz,unsigned short *code);
  26. int appendETENBMP(LPBYTE buf,unsigned short code);
  27. int isETENBMP(TCHAR *path);
  28. */
  29. static HANDLE bmpFh = INVALID_HANDLE_VALUE;
  30. static int openmd = -1;
  31. static int nChar;
  32. static int width, height;
  33. static int *recordBuf=0;
  34. int uNum=0;
  35. static int *codep=0;
  36. static char UserFontSign[8];
  37. static WORD BankID;
  38. static unsigned short
  39. getsval( unsigned char *s)
  40. {
  41. unsigned short sval=0;
  42. if (!s)
  43. {
  44. return sval;
  45. }
  46. sval = (unsigned short )*(s+1);
  47. sval <<=8;
  48. sval |= (unsigned short )*s;
  49. return sval;
  50. }
  51. static unsigned long
  52. getlval( unsigned char *l)
  53. {
  54. unsigned long lval=0;
  55. int i;
  56. if (!l)
  57. {
  58. return lval;
  59. }
  60. lval = (unsigned long)*(l+3);
  61. for ( i=2; i>=0; i--) {
  62. lval<<=8;
  63. lval |=(unsigned long)*(l+i);
  64. }
  65. return lval;
  66. }
  67. static void
  68. setsval( unsigned char *m, unsigned short s)
  69. {
  70. if (!m)
  71. {
  72. return;
  73. }
  74. *m = (unsigned char)(s & 0xff);
  75. *(m+1) = (unsigned char)((s>>8) & 0xff);
  76. }
  77. static void
  78. setlval( unsigned char *m, unsigned long lval)
  79. {
  80. int i;
  81. if (!m)
  82. {
  83. return;
  84. }
  85. for ( i=0; i<4; i++) {
  86. *m++ = (unsigned char)(lval & 0xff);
  87. lval>>=8;
  88. }
  89. }
  90. static int
  91. readHdr( HANDLE fhdl, struct ETENHEADER *hdr)
  92. {
  93. struct R_ETENHEADER rhdr;
  94. DWORD nByte;
  95. BOOL res;
  96. if (!hdr)
  97. {
  98. goto ERET;
  99. }
  100. res = ReadFile( fhdl, &rhdr, 256, &nByte, NULL);
  101. if (!res || nByte !=256)
  102. goto ERET;
  103. memset( hdr, 0, sizeof(struct ETENHEADER));
  104. /* Set values to hdr */
  105. hdr->uHeaderSize = getsval( rhdr.uHeaderSize);
  106. memcpy( hdr->idUserFontSign, rhdr.idUserFontSign, 8);
  107. hdr->idMajor = rhdr.idMajor;
  108. hdr->idMinor = rhdr.idMinor;
  109. hdr->ulCharCount = getlval( rhdr.ulCharCount);
  110. hdr->uCharWidth = getsval( rhdr.uCharWidth);
  111. hdr->uCharHeight = getsval( rhdr.uCharHeight);
  112. hdr->cPatternSize = getlval( rhdr.cPatternSize);
  113. hdr->uchBankID = rhdr.uchBankID;
  114. hdr->idInternalBankID = getsval( rhdr.idInternalBankID);
  115. hdr->sFontInfo.uInfoSize = getsval(rhdr.sFontInfo.uInfoSize);
  116. hdr->sFontInfo.idCP = getsval(rhdr.sFontInfo.idCP);
  117. hdr->sFontInfo.idCharSet = rhdr.sFontInfo.idCharSet;
  118. hdr->sFontInfo.fbTypeFace = rhdr.sFontInfo.fbTypeFace;
  119. memcpy( hdr->sFontInfo.achFontName , rhdr.sFontInfo.achFontName,12);
  120. hdr->sFontInfo.ulCharDefine = getlval(rhdr.sFontInfo.ulCharDefine);
  121. hdr->sFontInfo.uCellWidth = getsval(rhdr.sFontInfo.uCellWidth);
  122. hdr->sFontInfo.uCellHeight = getsval(rhdr.sFontInfo.uCellHeight);
  123. hdr->sFontInfo.uCharHeight = getsval(rhdr.sFontInfo.uCharHeight);
  124. hdr->sFontInfo.uBaseLine = getsval(rhdr.sFontInfo.uBaseLine);
  125. hdr->sFontInfo.uUnderLine = getsval(rhdr.sFontInfo.uUnderLine);
  126. hdr->sFontInfo.uUnlnHeight = getsval(rhdr.sFontInfo.uUnlnHeight);
  127. hdr->sFontInfo.fchStrokeWeight = rhdr.sFontInfo.fchStrokeWeight;
  128. hdr->sFontInfo.fCharStyle = getsval(rhdr.sFontInfo.fCharStyle);
  129. hdr->sFontInfo.fbFontAttrib = rhdr.sFontInfo.fbFontAttrib;
  130. hdr->sFontInfo.ulCellWidthMax = getlval(rhdr.sFontInfo.ulCellWidthMax);
  131. hdr->sFontInfo.ulCellHeightMax= getlval(rhdr.sFontInfo.ulCellHeightMax);
  132. return 0;
  133. ERET:
  134. return -1;
  135. }
  136. static int
  137. updHdr( )
  138. {
  139. struct R_ETENHEADER rhdr;
  140. DWORD nByte;
  141. BOOL res;
  142. if ( (long) SetFilePointer( bmpFh, 0L, NULL, FILE_BEGIN)!=0L)
  143. goto ERET;
  144. res = ReadFile( bmpFh, &rhdr, 256, &nByte, NULL);
  145. if (!res || nByte !=256)
  146. goto ERET;
  147. setlval( rhdr.ulCharCount, (long)nChar);
  148. setlval( rhdr.sFontInfo.ulCharDefine, (long)nChar);
  149. if ( (long) SetFilePointer( bmpFh, 0L, NULL, FILE_BEGIN)!=0L)
  150. goto ERET;
  151. res = WriteFile(bmpFh, (char *)&rhdr, 256,&nByte, NULL);
  152. if (!res || nByte !=256)
  153. goto ERET;
  154. return 0;
  155. ERET:
  156. return -1;
  157. }
  158. static int
  159. getETENBankID( HANDLE fh, WORD *BankID)
  160. {
  161. struct R_CODEELEMENT cElm;
  162. long ofs;
  163. DWORD nByte;
  164. BOOL res;
  165. if (!BankID)
  166. {
  167. goto ERET;
  168. }
  169. ofs = sizeof(struct R_ETENHEADER);
  170. if ((long) SetFilePointer( fh, ofs, NULL, FILE_BEGIN) != ofs)
  171. goto ERET;
  172. res = ReadFile( fh, &cElm, sizeof(struct R_CODEELEMENT), &nByte, NULL);
  173. if (!res || nByte !=sizeof(struct R_CODEELEMENT))
  174. goto ERET;
  175. *BankID = getsval( cElm.nBankID);
  176. return 0;
  177. ERET:
  178. return -1;
  179. }
  180. /***
  181. recBuf
  182. +-------+
  183. | rec# | E000
  184. +-------+
  185. | | E001
  186. +-------+
  187. |
  188. +-------+
  189. | | maxUCode
  190. +-------+
  191. ****/
  192. static int
  193. scanETENBMP( int **recBuf, unsigned int maxUCode, int nRec)
  194. {
  195. long ofs;
  196. int recsiz, bmpsiz;
  197. struct R_CODEELEMENT *bhd;
  198. int rec;
  199. char *rbuf;
  200. unsigned short code;
  201. unsigned short ucode;
  202. int urec;
  203. int *recp;
  204. DWORD nByte;
  205. BOOL res;
  206. if (!recBuf)
  207. {
  208. return -1;
  209. }
  210. recp = 0;
  211. rbuf = 0;
  212. if ( maxUCode < EUDCCODEBASE)
  213. return -1;
  214. else if ( nRec <=0)
  215. return -1;
  216. uNum = maxUCode - EUDCCODEBASE+1;
  217. if ( (codep = (int *)malloc( uNum*sizeof(int)))==0)
  218. goto ERET;
  219. ofs = sizeof( struct R_ETENHEADER);
  220. if ( (long) SetFilePointer( bmpFh, ofs, NULL, FILE_BEGIN)!=ofs)
  221. goto ERET;
  222. bmpsiz = (width+7)/8*height;
  223. recsiz =bmpsiz+sizeof (bhd);
  224. if ((rbuf = (char *)malloc( recsiz))==(char *)0)
  225. goto ERET;
  226. for ( code = EUDCCODEBASE; code <= maxUCode; code++)
  227. codep[code-EUDCCODEBASE] = -1;
  228. bhd = (struct R_CODEELEMENT *)rbuf;
  229. for ( rec = 0; rec < nRec; rec++) {
  230. res = ReadFile( bmpFh, rbuf, (unsigned int)recsiz, &nByte, NULL);
  231. if (!res || nByte !=(unsigned int)recsiz)
  232. goto ERET;
  233. code = getsval( bhd->nInternalCode);
  234. if( memcmp(UserFontSign, "CMEX_PTN", 8) ||
  235. BankID != ETENBANKID){
  236. ucode = sjisToUniEUDC( code);
  237. }else ucode = code;
  238. if( ucode > maxUCode || ucode < EUDCCODEBASE)
  239. continue;
  240. urec = (int)(ucode - EUDCCODEBASE);
  241. codep[urec] = rec;
  242. }
  243. free( rbuf);
  244. if ( (recp = (int *)malloc( nRec*sizeof(int)))==0)
  245. goto ERET;
  246. *recBuf=recp;
  247. for ( rec=0; rec < uNum; rec++) {
  248. if ( codep[rec]>0)
  249. *recp++ = codep[rec];
  250. }
  251. return 0;
  252. ERET:
  253. if ( codep) free( codep);
  254. if ( recp) free( recp);
  255. if ( rbuf) free( rbuf);
  256. return -1;
  257. }
  258. int
  259. openETENBMP( TCHAR *path, int md)
  260. {
  261. HANDLE fh=INVALID_HANDLE_VALUE;
  262. struct ETENHEADER hdr;
  263. if (!path)
  264. {
  265. goto ERET;
  266. }
  267. makeUniCodeTbl();
  268. if ( md) {
  269. fh = CreateFile(path,
  270. GENERIC_READ | GENERIC_WRITE,
  271. FILE_SHARE_READ | FILE_SHARE_WRITE,
  272. NULL,
  273. OPEN_EXISTING,
  274. FILE_ATTRIBUTE_NORMAL,
  275. NULL);
  276. if ( fh == INVALID_HANDLE_VALUE)
  277. goto ERET;
  278. bmpFh = fh;
  279. openmd = 1;
  280. }
  281. else {
  282. fh = CreateFile(path,
  283. GENERIC_READ,
  284. FILE_SHARE_READ | FILE_SHARE_WRITE,
  285. NULL,
  286. OPEN_EXISTING,
  287. FILE_ATTRIBUTE_NORMAL,
  288. NULL);
  289. if ( fh == INVALID_HANDLE_VALUE)
  290. goto ERET;
  291. bmpFh = fh;
  292. openmd = 0;
  293. }
  294. if (readHdr( fh, &hdr))
  295. goto ERET;
  296. if (getETENBankID( fh, &BankID))
  297. goto ERET;
  298. nChar = (int)hdr.ulCharCount;
  299. width = (int)hdr.uCharWidth;
  300. height = (int)hdr.uCharHeight;
  301. memcpy((char *)UserFontSign, hdr.idUserFontSign, 8);
  302. if ( scanETENBMP( &recordBuf, getMaxUniCode(), nChar))
  303. goto ERET;
  304. return 0;
  305. ERET:
  306. if (fh != INVALID_HANDLE_VALUE)
  307. CloseHandle(fh);
  308. return -1;
  309. }
  310. int
  311. closeETENBMP( )
  312. {
  313. int sts;
  314. if ( openmd)
  315. sts = updHdr();
  316. else sts = 0;
  317. if ( bmpFh!=INVALID_HANDLE_VALUE)
  318. CloseHandle( bmpFh);
  319. if(recordBuf) {
  320. free(recordBuf);
  321. recordBuf = 0;
  322. }
  323. if ( codep) {
  324. free(codep);
  325. codep = 0;
  326. }
  327. return sts;
  328. }
  329. static void
  330. setIniHdr( struct R_ETENHEADER *hdr, int width, int height)
  331. {
  332. if (!hdr)
  333. {
  334. return;
  335. }
  336. memset( hdr, 0, sizeof(struct R_ETENHEADER));
  337. setsval( hdr->uHeaderSize, sizeof(struct R_ETENHEADER));
  338. memcpy( hdr->idUserFontSign, "CWIN_PTN", 8);
  339. hdr->idMajor = 1;
  340. hdr->idMinor = 0;
  341. setsval( hdr->uCharWidth, (unsigned short)width);
  342. setsval( hdr->uCharHeight, (unsigned short)height);
  343. setlval( hdr->cPatternSize, (unsigned long)(((width+7)/8)*height));
  344. setsval( hdr->sFontInfo.uInfoSize,
  345. (unsigned short)sizeof(struct R_CFONTINFO));
  346. setsval( hdr->sFontInfo.idCP, 938);
  347. hdr->sFontInfo.idCharSet = (char)0x88;
  348. setsval( hdr->sFontInfo.uCellWidth, (unsigned short)width);
  349. setsval( hdr->sFontInfo.uCellHeight, (unsigned short)height);
  350. setsval( hdr->sFontInfo.uCharHeight, (unsigned short)height);
  351. setlval( hdr->sFontInfo.ulCellWidthMax, (unsigned long)width);
  352. setlval( hdr->sFontInfo.ulCellHeightMax, (unsigned long)height);
  353. }
  354. int
  355. createETENBMP( TCHAR *path, int wid, int hei)
  356. {
  357. HANDLE fh=INVALID_HANDLE_VALUE;
  358. struct R_ETENHEADER hdr;
  359. DWORD nByte;
  360. BOOL res;
  361. if (!path)
  362. {
  363. goto ERET;
  364. }
  365. fh = CreateFile(path,
  366. GENERIC_WRITE,
  367. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  368. NULL,
  369. CREATE_ALWAYS,
  370. FILE_ATTRIBUTE_NORMAL,
  371. NULL);
  372. if ( fh == INVALID_HANDLE_VALUE)
  373. goto ERET;
  374. width = wid;
  375. height = hei;
  376. setIniHdr( &hdr, width, height);
  377. res = WriteFile( fh, (char *)&hdr, sizeof(hdr), &nByte, NULL);
  378. if (!res || nByte !=sizeof(hdr))
  379. goto ERET;
  380. bmpFh = fh;
  381. openmd = 1;
  382. nChar =0;
  383. return 0;
  384. ERET:
  385. if (fh != INVALID_HANDLE_VALUE)
  386. CloseHandle(fh);
  387. return -1;
  388. }
  389. int
  390. getETENBMPInf( int *nRec, int *nGlyph, int *wid, int *hei, char *sign,WORD *bID)
  391. {
  392. if ((!nRec) || (!nGlyph) || (!wid) || (!hei) || (!sign) || (!bID))
  393. {
  394. return 0;
  395. }
  396. if ( bmpFh <0) return -1;
  397. *nRec = uNum;
  398. *nGlyph = nChar;
  399. *wid = width;
  400. *hei = height;
  401. *bID = BankID;
  402. memcpy( sign, UserFontSign, 8);
  403. return 0;
  404. }
  405. int
  406. readETENBMPRec( int rec, LPBYTE buf, int bufsiz, unsigned short *code)
  407. {
  408. long ofs;
  409. int recsiz;
  410. struct R_CODEELEMENT bhd;
  411. int rdsiz;
  412. int bWid, wWid;
  413. int y, ylim;
  414. unsigned char *rbuf;
  415. DWORD nByte;
  416. BOOL res;
  417. if ( (!code) || (!buf))
  418. {
  419. goto ERET;
  420. }
  421. bWid = (width+7)/8;
  422. wWid = (bWid+1)/2*2;
  423. recsiz = (width+7)/8*height;
  424. ofs = sizeof( struct R_ETENHEADER)+(long)(recsiz+sizeof (bhd))*rec;
  425. if ( (long) SetFilePointer( bmpFh, ofs, NULL, FILE_BEGIN)!=ofs)
  426. goto ERET;
  427. res = ReadFile( bmpFh, &bhd, sizeof(bhd), &nByte, NULL);
  428. if (!res || nByte !=sizeof(bhd))
  429. goto ERET;
  430. if ( bufsiz<recsiz) rdsiz = bufsiz;
  431. else rdsiz = recsiz;
  432. if ( bWid!=wWid) {
  433. BYTE *src, *dst;
  434. if ((rbuf = (unsigned char *)malloc( recsiz))==(unsigned char *)0)
  435. goto ERET;
  436. res = ReadFile( bmpFh, (char *)rbuf, (unsigned int)recsiz, &nByte, NULL);
  437. if (!res || nByte !=(unsigned int)recsiz) {
  438. free(rbuf);
  439. goto ERET;
  440. }
  441. ylim = rdsiz / bWid;
  442. src = (LPBYTE)rbuf;
  443. dst = buf;
  444. memset( buf, 0xff, rdsiz);
  445. for ( y = 0; y < ylim; y++, src+=bWid, dst+=wWid)
  446. memcpy(dst , src , bWid);
  447. free( rbuf);
  448. }
  449. else {
  450. res = ReadFile( bmpFh, (char *)buf, (unsigned int)rdsiz, &nByte, NULL);
  451. if (!res || nByte !=(unsigned int)rdsiz)
  452. goto ERET;
  453. }
  454. *code = getsval( bhd.nInternalCode);
  455. return 0;
  456. ERET:
  457. return -1;
  458. }
  459. int
  460. appendETENBMP( LPBYTE buf, unsigned short code)
  461. {
  462. struct R_CODEELEMENT bhd;
  463. int bmpsiz;
  464. DWORD nByte;
  465. BOOL res;
  466. if (!buf)
  467. {
  468. goto ERET;
  469. }
  470. SetFilePointer( bmpFh, 0L, NULL, FILE_END);
  471. bmpsiz = (width+7)/8*height;
  472. setsval( bhd.nBankID, 1);
  473. setsval( bhd.nInternalCode, code);
  474. res = WriteFile( bmpFh, (LPBYTE)(&bhd), sizeof( bhd), &nByte, NULL);
  475. if (!res || nByte !=sizeof(bhd))
  476. goto ERET;
  477. res = WriteFile( bmpFh, (LPBYTE)buf, (unsigned int)bmpsiz, &nByte, NULL);
  478. if (!res || nByte != (unsigned int)bmpsiz)
  479. goto ERET;
  480. nChar++;
  481. return 0;
  482. ERET:
  483. return -1;
  484. }
  485. int
  486. isETENBMP(TCHAR *path)
  487. {
  488. struct ETENHEADER hdr;
  489. HANDLE fhdl;
  490. if (!path)
  491. {
  492. return 0;
  493. }
  494. fhdl = CreateFile(path,
  495. GENERIC_READ,
  496. FILE_SHARE_READ | FILE_SHARE_WRITE,
  497. NULL,
  498. OPEN_EXISTING,
  499. FILE_ATTRIBUTE_NORMAL,
  500. NULL);
  501. if ( fhdl == INVALID_HANDLE_VALUE)
  502. return -1;
  503. if ( readHdr( fhdl, &hdr)) {
  504. CloseHandle( fhdl);
  505. return -1;
  506. }
  507. CloseHandle( fhdl);
  508. /* check Header size and keyWord*/
  509. if ( hdr.uHeaderSize != sizeof(struct R_ETENHEADER))
  510. goto NO_ETEN;
  511. if ( memcmp(hdr.idUserFontSign, "CWIN_PTN", 8) &&
  512. memcmp(hdr.idUserFontSign, "CMEX_PTN", 8))
  513. goto NO_ETEN;
  514. return 1;
  515. NO_ETEN:
  516. return 0;
  517. }
  518. int
  519. ETENrecTbl( int **recTbl)
  520. {
  521. if (recTbl)
  522. {
  523. *recTbl=codep;
  524. }
  525. return 0;
  526. }
  527. /* EOF */
  528. /* For test +/
  529. static int
  530. dispHdr( struct ETENHEADER *hdr)
  531. {
  532. printf("hdr->uHeaderSize= %d\n", hdr->uHeaderSize );
  533. printf("hdr->idMajor %d\n", hdr->idMajor );
  534. printf("hdr->idMinor %d\n", hdr->idMinor );
  535. printf("hdr->ulCharCout %ld\n", hdr->ulCharCount );
  536. printf("hdr->uCharWidth %d\n", hdr->uCharWidth );
  537. printf(" hdr->uCharHeight %d\n", hdr->uCharHeight );
  538. printf(" hdr->cPatternSize %d\n", hdr->cPatternSize);
  539. printf("hdr->uchBankID %d\n", hdr->uchBankID);
  540. printf("hdr->idInternalBankID %d\n", hdr->idInternalBankID);
  541. printf("hdr->sFontInfo.uInfoSize %d\n", hdr->sFontInfo.uInfoSize);
  542. printf("hdr->sFontInfo.idCP %d\n", hdr->sFontInfo.idCP);
  543. printf("hdr->sFontInfo.idCharSet %d\n", hdr->sFontInfo.idCharSet);
  544. printf("hdr->sFontInfo.fbTypeFace %d\n", hdr->sFontInfo.fbTypeFace);
  545. printf("hdr->sFontInfo.ulCharDefine %ld\n", hdr->sFontInfo.ulCharDefine);
  546. printf("hdr->sFontInfo.uCellWidth %d\n", hdr->sFontInfo.uCellWidth);
  547. printf("hdr->sFontInfo.uCellHeight %d\n", hdr->sFontInfo.uCellHeight);
  548. printf("hdr->sFontInfo.uCharHeight %d\n", hdr->sFontInfo.uCharHeight);
  549. printf("hdr->sFontInfo.uBaseLine %d\n", hdr->sFontInfo.uBaseLine);
  550. printf("hdr->sFontInfo.uUnderLine %d\n", hdr->sFontInfo.uUnderLine);
  551. printf("hdr->sFontInfo.uUnlnHeight %d\n", hdr->sFontInfo.uUnlnHeight);
  552. printf("hdr->sFontInfo.fchStrokeWeight %d\n", hdr->sFontInfo.fchStrokeWeight);
  553. printf("hdr->sFontInfo.fCharStyle %d\n", hdr->sFontInfo.fCharStyle);
  554. printf("hdr->sFontInfo.fbFontAttrib %d\n", hdr->sFontInfo.fbFontAttrib);
  555. printf("hdr->sFontInfo.ulCellWidthMax %ld\n", hdr->sFontInfo.ulCellWidthMax);
  556. printf("hdr->sFontInfo.ulCellHeightMax %ld\n", hdr->sFontInfo.ulCellHeightMax);
  557. }
  558. main( int argc, char *argv[])
  559. {
  560. int fh;
  561. struct ETENHEADER hdr;
  562. fh = _lopen( argv[1], O_RDONLY | O_BINARY);
  563. readHdr( fh, &hdr);
  564. dispHdr( &hdr);
  565. _lclose(fh);
  566. exit(0);
  567. }
  568. /+ */
  569. /* EOF */