portableio.c
上传用户:jgz_ta
上传日期:2007-11-29
资源大小:73k
文件大小:9k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. //    Shine is an MP3 encoder
  2. //    Copyright (C) 1999-2000  Gabriel Bouvigne
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. /* Copyright (C) 1988-1991 Apple Computer, Inc.
  14.  * All Rights Reserved.
  15.  *
  16.  * Warranty Information
  17.  * Even though Apple has reviewed this software, Apple makes no warranty
  18.  * or representation, either express or implied, with respect to this
  19.  * software, its quality, accuracy, merchantability, or fitness for a 
  20.  * particular purpose.  As a result, this software is provided "as is,"
  21.  * and you, its user, are assuming the entire risk as to its quality
  22.  * and accuracy.
  23.  *
  24.  * This code may be used and freely distributed as long as it includes
  25.  * this copyright notice and the warranty information.
  26.  *
  27.  *
  28.  * Motorola processors (Macintosh, Sun, Sparc, MIPS, etc)
  29.  * pack bytes from high to low (they are big-endian).
  30.  * Use the HighLow routines to match the native format
  31.  * of these machines.
  32.  *
  33.  * Intel-like machines (PCs, Sequent)
  34.  * pack bytes from low to high (the are little-endian).
  35.  * Use the LowHigh routines to match the native format
  36.  * of these machines.
  37.  *
  38.  * These routines have been tested on the following machines:
  39.  * Apple Macintosh, MPW 3.1 C compiler
  40.  * Apple Macintosh, THINK C compiler
  41.  * Silicon Graphics IRIS, MIPS compiler
  42.  * Cray X/MP and Y/MP
  43.  * Digital Equipment VAX
  44.  *
  45.  *
  46.  * Implemented by Malcolm Slaney and Ken Turkowski.
  47.  *
  48.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  49.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  50.  * floating-point format, and conversions to and from IEEE single-
  51.  * precision floating-point format.
  52.  *
  53.  * In 1991, Ken Turkowski implemented the conversions to and from
  54.  * IEEE double-precision format, added more precision to the extended
  55.  * conversions, and accommodated conversions involving +/- infinity,
  56.  * NaN's, and denormalized numbers.
  57.  *
  58.  * $Id: portableio.c,v 2.6 1991/04/30 17:06:02 malcolm Exp $
  59.  *
  60.  * $Log: portableio.c,v $
  61.  * Revision 2.6  91/04/30  17:06:02  malcolm
  62.  */
  63. #include <stdio.h>
  64. #include <string.h>
  65. #include <math.h>
  66. #include "portableio.h"
  67. /****************************************************************
  68.  * Big/little-endian independent I/O routines.
  69.  ****************************************************************/
  70. int
  71. ReadByte(fp)
  72. FILE *fp;
  73. {
  74. int result;
  75. result = getc(fp) & 0xff;
  76. if (result & 0x80)
  77. result = result - 0x100;
  78. return result;
  79. }
  80. int
  81. Read16BitsLowHigh(fp)
  82. FILE *fp;
  83. {
  84. int first, second, result;
  85. first = 0xff & getc(fp);
  86. second = 0xff & getc(fp);
  87. result = (second << 8) + first;
  88. #ifndef THINK_C42
  89. if (result & 0x8000)
  90. result = result - 0x10000;
  91. #endif /* THINK_C */
  92. return(result);
  93. }
  94. int
  95. Read16BitsHighLow(fp)
  96. FILE *fp;
  97. {
  98. int first, second, result;
  99. first = 0xff & getc(fp);
  100. second = 0xff & getc(fp);
  101. result = (first << 8) + second;
  102. #ifndef THINK_C42
  103. if (result & 0x8000)
  104. result = result - 0x10000;
  105. #endif /* THINK_C */
  106. return(result);
  107. }
  108. void
  109. Write8Bits(fp, i)
  110. FILE *fp;
  111. int i;
  112. {
  113. putc(i&0xff,fp);
  114. }
  115. void
  116. Write16BitsLowHigh(fp, i)
  117. FILE *fp;
  118. int i;
  119. {
  120. putc(i&0xff,fp);
  121. putc((i>>8)&0xff,fp);
  122. }
  123. void
  124. Write16BitsHighLow(fp, i)
  125. FILE *fp;
  126. int i;
  127. {
  128. putc((i>>8)&0xff,fp);
  129. putc(i&0xff,fp);
  130. }
  131. int
  132. Read24BitsHighLow(fp)
  133. FILE *fp;
  134. {
  135. int first, second, third;
  136. int result;
  137. first = 0xff & getc(fp);
  138. second = 0xff & getc(fp);
  139. third = 0xff & getc(fp);
  140. result = (first << 16) + (second << 8) + third;
  141. if (result & 0x800000)
  142. result = result - 0x1000000;
  143. return(result);
  144. }
  145. #define Read32BitsLowHigh(f) Read32Bits(f)
  146. int
  147. Read32Bits(fp)
  148. FILE *fp;
  149. {
  150. int first, second, result;
  151. first = 0xffff & Read16BitsLowHigh(fp);
  152. second = 0xffff & Read16BitsLowHigh(fp);
  153. result = (second << 16) + first;
  154. #ifdef CRAY
  155. if (result & 0x80000000)
  156. result = result - 0x100000000;
  157. #endif /* CRAY */
  158. return(result);
  159. }
  160. int
  161. Read32BitsHighLow(fp)
  162. FILE *fp;
  163. {
  164. int first, second, result;
  165. first = 0xffff & Read16BitsHighLow(fp);
  166. second = 0xffff & Read16BitsHighLow(fp);
  167. result = (first << 16) + second;
  168. #ifdef CRAY
  169. if (result & 0x80000000)
  170. result = result - 0x100000000;
  171. #endif
  172. return(result);
  173. }
  174. void
  175. Write32Bits(fp, i)
  176. FILE *fp;
  177. int i;
  178. {
  179. Write16BitsLowHigh(fp,(int)(i&0xffffL));
  180. Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  181. }
  182. void
  183. Write32BitsLowHigh(fp, i)
  184. FILE *fp;
  185. int i;
  186. {
  187. Write16BitsLowHigh(fp,(int)(i&0xffffL));
  188. Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  189. }
  190. void
  191. Write32BitsHighLow(fp, i)
  192. FILE *fp;
  193. int i;
  194. {
  195. Write16BitsHighLow(fp,(int)((i>>16)&0xffffL));
  196. Write16BitsHighLow(fp,(int)(i&0xffffL));
  197. }
  198. void ReadBytes(fp, p, n)
  199. FILE *fp;
  200. char *p;
  201. int n;
  202. {
  203. while (!feof(fp) && n-- > 0)
  204. *p++ = getc(fp);
  205. }
  206. void ReadBytesSwapped(fp, p, n)
  207. FILE *fp;
  208. char *p;
  209. int n;
  210. {
  211. register char *q = p;
  212. while (!feof(fp) && n-- > 0)
  213. *q++ = getc(fp);
  214. for (q--; p < q; p++, q--){
  215. n = *p;
  216. *p = *q;
  217. *q = n;
  218. }
  219. }
  220. void WriteBytes(fp, p, n)
  221. FILE *fp;
  222. char *p;
  223. int n;
  224. {
  225. while (n-- > 0)
  226. putc(*p++, fp);
  227. }
  228. void WriteBytesSwapped(fp, p, n)
  229. FILE *fp;
  230. char *p;
  231. int n;
  232. {
  233. p += n-1;
  234. while (n-- > 0)
  235. putc(*p--, fp);
  236. }
  237. defdouble
  238. ReadIeeeFloatHighLow(fp)
  239. FILE *fp;
  240. {
  241. char bits[kFloatLength];
  242. ReadBytes(fp, bits, kFloatLength);
  243. return ConvertFromIeeeSingle(bits);
  244. }
  245. defdouble
  246. ReadIeeeFloatLowHigh(fp)
  247. FILE *fp;
  248. {
  249. char bits[kFloatLength];
  250. ReadBytesSwapped(fp, bits, kFloatLength);
  251. return ConvertFromIeeeSingle(bits);
  252. }
  253. defdouble
  254. ReadIeeeDoubleHighLow(fp)
  255. FILE *fp;
  256. {
  257. char bits[kDoubleLength];
  258. ReadBytes(fp, bits, kDoubleLength);
  259. return ConvertFromIeeeDouble(bits);
  260. }
  261. defdouble
  262. ReadIeeeDoubleLowHigh(fp)
  263. FILE *fp;
  264. {
  265. char bits[kDoubleLength];
  266. ReadBytesSwapped(fp, bits, kDoubleLength);
  267. return ConvertFromIeeeDouble(bits);
  268. }
  269. defdouble
  270. ReadIeeeExtendedHighLow(fp)
  271. FILE *fp;
  272. {
  273. char bits[kExtendedLength];
  274. ReadBytes(fp, bits, kExtendedLength);
  275. return ConvertFromIeeeExtended(bits);
  276. }
  277. defdouble
  278. ReadIeeeExtendedLowHigh(fp)
  279. FILE *fp;
  280. {
  281. char bits[kExtendedLength];
  282. ReadBytesSwapped(fp, bits, kExtendedLength);
  283. return ConvertFromIeeeExtended(bits);
  284. }
  285. void
  286. WriteIeeeFloatLowHigh(fp, num)
  287. FILE *fp;
  288. defdouble num;
  289. {
  290. char bits[kFloatLength];
  291. ConvertToIeeeSingle(num,bits);
  292. WriteBytesSwapped(fp,bits,kFloatLength);
  293. }
  294. void
  295. WriteIeeeFloatHighLow(fp, num)
  296. FILE *fp;
  297. defdouble num;
  298. {
  299. char bits[kFloatLength];
  300. ConvertToIeeeSingle(num,bits);
  301. WriteBytes(fp,bits,kFloatLength);
  302. }
  303. void
  304. WriteIeeeDoubleLowHigh(fp, num)
  305. FILE *fp;
  306. defdouble num;
  307. {
  308. char bits[kDoubleLength];
  309. ConvertToIeeeDouble(num,bits);
  310. WriteBytesSwapped(fp,bits,kDoubleLength);
  311. }
  312. void
  313. WriteIeeeDoubleHighLow(fp, num)
  314. FILE *fp;
  315. defdouble num;
  316. {
  317. char bits[kDoubleLength];
  318. ConvertToIeeeDouble(num,bits);
  319. WriteBytes(fp,bits,kDoubleLength);
  320. }
  321. void
  322. WriteIeeeExtendedLowHigh(fp, num)
  323. FILE *fp;
  324. defdouble num;
  325. {
  326. char bits[kExtendedLength];
  327. ConvertToIeeeExtended(num,bits);
  328. WriteBytesSwapped(fp,bits,kExtendedLength);
  329. }
  330. void
  331. WriteIeeeExtendedHighLow(fp, num)
  332. FILE *fp;
  333. defdouble num;
  334. {
  335. char bits[kExtendedLength];
  336. ConvertToIeeeExtended(num,bits);
  337. WriteBytes(fp,bits,kExtendedLength);
  338. }
  339. enum e_byte_order DetermineByteOrder()
  340. {
  341.     char s[ sizeof(long) + 1 ];
  342.     union
  343.     {
  344.         long longval;
  345.         char charval[ sizeof(long) ];
  346.     } probe;
  347.     probe.longval = 0x41424344L;  /* ABCD in ASCII */
  348.     strncpy( s, probe.charval, sizeof(long) );
  349.     s[ sizeof(long) ] = '';
  350.     /* fprintf( stderr, "byte order is %sn", s ); */
  351.     if ( strcmp(s, "ABCD") == 0 )
  352.         return order_bigEndian;
  353.     else
  354.         if ( strcmp(s, "DCBA") == 0 )
  355.             return order_littleEndian;
  356.         else
  357.             return order_unknown;
  358. }
  359. void SwapBytesInWords( short *loc, int words )
  360. {
  361.     int i;
  362.     short thisval;
  363.     char *dst, *src;
  364.     src = (char *) &thisval;
  365.     for ( i = 0; i < words; i++ )
  366.     {
  367.         thisval = *loc;
  368.         dst = (char *) loc++;
  369.         dst[0] = src[1];
  370.         dst[1] = src[0];
  371.     }
  372. }