| 1 |
/* $Id: misc.c,v 1.5 1999/03/10 07:58:03 boyns Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (C) 1993-99 Mark R. Boyns <[email protected]> |
| 5 |
* |
| 6 |
* This file is part of rplay. |
| 7 |
* |
| 8 |
* rplay is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* rplay is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with rplay; see the file COPYING. If not, write to the |
| 20 |
* Free Software Foundation, Inc., |
| 21 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include "config.h" |
| 28 |
#endif |
| 29 |
#include "rplayd.h" |
| 30 |
#include <string.h> |
| 31 |
#include <sys/file.h> |
| 32 |
#include <sys/types.h> |
| 33 |
#include <sys/stat.h> |
| 34 |
#ifdef ultrix |
| 35 |
#include <fcntl.h> |
| 36 |
#else |
| 37 |
#include <sys/fcntl.h> |
| 38 |
#endif |
| 39 |
#include <sys/param.h> |
| 40 |
#include <sys/time.h> |
| 41 |
#include <sys/signal.h> |
| 42 |
#include <math.h> |
| 43 |
#include "sound.h" |
| 44 |
#include "timer.h" |
| 45 |
|
| 46 |
#ifdef __STDC__ |
| 47 |
char * |
| 48 |
sys_err_str(int error) |
| 49 |
#else |
| 50 |
char * |
| 51 |
sys_err_str(error) |
| 52 |
int error; |
| 53 |
#endif |
| 54 |
{ |
| 55 |
#ifdef HAVE_STRERROR |
| 56 |
return (char *) strerror(error); |
| 57 |
#else |
| 58 |
extern char *sys_errlist[]; |
| 59 |
return sys_errlist[error]; |
| 60 |
#endif |
| 61 |
} |
| 62 |
|
| 63 |
#ifdef __STDC__ |
| 64 |
int |
| 65 |
udp_socket(int port) |
| 66 |
#else |
| 67 |
int |
| 68 |
udp_socket(port) |
| 69 |
int port; |
| 70 |
#endif |
| 71 |
{ |
| 72 |
int fd; |
| 73 |
struct sockaddr_in s; |
| 74 |
|
| 75 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 76 |
if (fd < 0) |
| 77 |
{ |
| 78 |
report(REPORT_ERROR, "socket: %s\n", sys_err_str(errno)); |
| 79 |
done(1); |
| 80 |
} |
| 81 |
|
| 82 |
s.sin_family = AF_INET; |
| 83 |
s.sin_port = htons(port); |
| 84 |
s.sin_addr.s_addr = INADDR_ANY; |
| 85 |
|
| 86 |
if (bind(fd, (struct sockaddr *) &s, sizeof(s)) < 0) |
| 87 |
{ |
| 88 |
report(REPORT_ERROR, "bind: %s\n", sys_err_str(errno)); |
| 89 |
done(1); |
| 90 |
} |
| 91 |
|
| 92 |
return fd; |
| 93 |
} |
| 94 |
|
| 95 |
#ifdef __STDC__ |
| 96 |
int |
| 97 |
tcp_socket(int port) |
| 98 |
#else |
| 99 |
int |
| 100 |
tcp_socket(port) |
| 101 |
int port; |
| 102 |
#endif |
| 103 |
{ |
| 104 |
int fd; |
| 105 |
struct sockaddr_in s; |
| 106 |
int on = 1; |
| 107 |
|
| 108 |
fd = socket(AF_INET, SOCK_STREAM, 0); |
| 109 |
if (fd < 0) |
| 110 |
{ |
| 111 |
report(REPORT_ERROR, "socket: %s\n", sys_err_str(errno)); |
| 112 |
done(1); |
| 113 |
} |
| 114 |
|
| 115 |
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) |
| 116 |
{ |
| 117 |
report(REPORT_ERROR, "setsockopt: SO_REUSEADDR: %s\n", sys_err_str(errno)); |
| 118 |
done(1); |
| 119 |
} |
| 120 |
|
| 121 |
s.sin_family = AF_INET; |
| 122 |
s.sin_port = htons(port); |
| 123 |
s.sin_addr.s_addr = INADDR_ANY; |
| 124 |
|
| 125 |
if (bind(fd, (struct sockaddr *) &s, sizeof(s)) < 0) |
| 126 |
{ |
| 127 |
report(REPORT_ERROR, "bind: %s\n", sys_err_str(errno)); |
| 128 |
done(1); |
| 129 |
} |
| 130 |
|
| 131 |
if (listen(fd, 5) < 0) |
| 132 |
{ |
| 133 |
report(REPORT_ERROR, "listen: %s\n", sys_err_str(errno)); |
| 134 |
done(1); |
| 135 |
} |
| 136 |
|
| 137 |
return fd; |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* make a file descriptor non-blocking |
| 142 |
*/ |
| 143 |
#ifdef __STDC__ |
| 144 |
void |
| 145 |
fd_nonblock(int fd) |
| 146 |
#else |
| 147 |
void |
| 148 |
fd_nonblock(fd) |
| 149 |
int fd; |
| 150 |
#endif |
| 151 |
{ |
| 152 |
int flags; |
| 153 |
|
| 154 |
flags = fcntl(fd, F_GETFL, 0); |
| 155 |
if (flags < 0) |
| 156 |
{ |
| 157 |
report(REPORT_ERROR, "fd_nonblock: F_GETFL fcntl: %s\n", sys_err_str(errno)); |
| 158 |
done(1); |
| 159 |
} |
| 160 |
#ifdef linux |
| 161 |
flags |= O_NONBLOCK; |
| 162 |
#else |
| 163 |
flags |= FNDELAY; |
| 164 |
#endif |
| 165 |
if (fcntl(fd, F_SETFL, flags) < 0) |
| 166 |
{ |
| 167 |
report(REPORT_ERROR, "fd_nonblock: F_SETFL fcntl: %s\n", sys_err_str(errno)); |
| 168 |
done(1); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* make a file descriptor blocking |
| 174 |
*/ |
| 175 |
#ifdef __STDC__ |
| 176 |
void |
| 177 |
fd_block(int fd) |
| 178 |
#else |
| 179 |
void |
| 180 |
fd_block(fd) |
| 181 |
int fd; |
| 182 |
#endif |
| 183 |
{ |
| 184 |
int flags; |
| 185 |
|
| 186 |
flags = fcntl(fd, F_GETFL, 0); |
| 187 |
if (flags < 0) |
| 188 |
{ |
| 189 |
report(REPORT_ERROR, "fd_block: F_GETFL fcntl: %s\n", sys_err_str(errno)); |
| 190 |
done(1); |
| 191 |
} |
| 192 |
#ifdef linux |
| 193 |
flags &= ~O_NONBLOCK; |
| 194 |
#else |
| 195 |
flags &= ~FNDELAY; |
| 196 |
#endif |
| 197 |
if (fcntl(fd, F_SETFL, flags) < 0) |
| 198 |
{ |
| 199 |
report(REPORT_ERROR, "fd_block: F_SETFL fcntl: %s\n", sys_err_str(errno)); |
| 200 |
done(1); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#ifdef __STDC__ |
| 205 |
int |
| 206 |
modified(char *filename, time_t since) |
| 207 |
#else |
| 208 |
int |
| 209 |
modified(filename, since) |
| 210 |
char *filename; |
| 211 |
time_t since; |
| 212 |
#endif |
| 213 |
{ |
| 214 |
struct stat st; |
| 215 |
|
| 216 |
if (stat(filename, &st) < 0) |
| 217 |
{ |
| 218 |
report(REPORT_ERROR, "%s: %s\n", filename, sys_err_str(errno)); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
return ((time_t) st.st_mtime > since) ? 1 : 0; |
| 223 |
} |
| 224 |
|
| 225 |
#ifdef __STDC__ |
| 226 |
char * |
| 227 |
time2string(time_t t) |
| 228 |
#else |
| 229 |
char * |
| 230 |
time2string(t) |
| 231 |
time_t t; |
| 232 |
#endif |
| 233 |
{ |
| 234 |
static char buf[64]; |
| 235 |
int days, hours, mins, secs; |
| 236 |
|
| 237 |
days = t / (60 * 60 * 24); |
| 238 |
t %= (60 * 60 * 24); |
| 239 |
hours = t / (60 * 60); |
| 240 |
t %= (60 * 60); |
| 241 |
mins = t / 60; |
| 242 |
t %= 60; |
| 243 |
secs = t; |
| 244 |
|
| 245 |
buf[0] = '\0'; |
| 246 |
|
| 247 |
if (days > 0) |
| 248 |
{ |
| 249 |
SNPRINTF(SIZE(buf, sizeof(buf)), "%d+", days); |
| 250 |
} |
| 251 |
|
| 252 |
SNPRINTF(SIZE(buf + strlen(buf), sizeof(buf) - strlen(buf)), "%02d:%02d:%02d", hours, mins, secs); |
| 253 |
|
| 254 |
return buf; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
#ifdef __STDC__ |
| 259 |
char * |
| 260 |
audio_format_to_string(int format) |
| 261 |
#else |
| 262 |
char * |
| 263 |
audio_format_to_string(format) |
| 264 |
int format; |
| 265 |
#endif |
| 266 |
{ |
| 267 |
switch (format) |
| 268 |
{ |
| 269 |
case RPLAY_FORMAT_ULAW: |
| 270 |
return "ulaw"; |
| 271 |
|
| 272 |
case RPLAY_FORMAT_LINEAR_8: |
| 273 |
return "linear-8"; |
| 274 |
|
| 275 |
case RPLAY_FORMAT_ULINEAR_8: |
| 276 |
return "ulinear-8"; |
| 277 |
|
| 278 |
case RPLAY_FORMAT_LINEAR_16: |
| 279 |
return "linear-16"; |
| 280 |
|
| 281 |
case RPLAY_FORMAT_ULINEAR_16: |
| 282 |
return "ulinear-16"; |
| 283 |
|
| 284 |
case RPLAY_FORMAT_G721: |
| 285 |
return "g721"; |
| 286 |
|
| 287 |
case RPLAY_FORMAT_G723_3: |
| 288 |
return "g723-3"; |
| 289 |
|
| 290 |
case RPLAY_FORMAT_G723_5: |
| 291 |
return "g723-5"; |
| 292 |
|
| 293 |
case RPLAY_FORMAT_GSM: |
| 294 |
return "gsm"; |
| 295 |
|
| 296 |
default: |
| 297 |
return "unknown"; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
#ifdef __STDC__ |
| 302 |
int |
| 303 |
string_to_audio_format(char *string) |
| 304 |
#else |
| 305 |
int |
| 306 |
string_to_audio_format(string) |
| 307 |
char *string; |
| 308 |
#endif |
| 309 |
{ |
| 310 |
if (strcmp(string, "ulaw") == 0 |
| 311 |
|| strcmp(string, "u_law") == 0 |
| 312 |
|| strcmp(string, "u-law") == 0) |
| 313 |
{ |
| 314 |
return RPLAY_FORMAT_ULAW; |
| 315 |
} |
| 316 |
else if (strcmp(string, "linear-16") == 0 |
| 317 |
|| strcmp(string, "linear_16") == 0 |
| 318 |
|| strcmp(string, "linear16") == 0) |
| 319 |
{ |
| 320 |
return RPLAY_FORMAT_LINEAR_16; |
| 321 |
} |
| 322 |
else if (strcmp(string, "ulinear-16") == 0 |
| 323 |
|| strcmp(string, "ulinear_16") == 0 |
| 324 |
|| strcmp(string, "ulinear16") == 0) |
| 325 |
{ |
| 326 |
return RPLAY_FORMAT_ULINEAR_16; |
| 327 |
} |
| 328 |
else if (strcmp(string, "linear-8") == 0 |
| 329 |
|| strcmp(string, "linear_8") == 0 |
| 330 |
|| strcmp(string, "linear8") == 0) |
| 331 |
{ |
| 332 |
return RPLAY_FORMAT_LINEAR_8; |
| 333 |
} |
| 334 |
else if (strcmp(string, "ulinear-8") == 0 |
| 335 |
|| strcmp(string, "ulinear_8") == 0 |
| 336 |
|| strcmp(string, "ulinear8") == 0) |
| 337 |
{ |
| 338 |
return RPLAY_FORMAT_ULINEAR_8; |
| 339 |
} |
| 340 |
else if (strcmp(string, "g721") == 0) |
| 341 |
{ |
| 342 |
return RPLAY_FORMAT_G721; |
| 343 |
} |
| 344 |
else if (strcmp(string, "g723-3") == 0 |
| 345 |
|| strcmp(string, "g723_3") == 0) |
| 346 |
{ |
| 347 |
return RPLAY_FORMAT_G723_3; |
| 348 |
} |
| 349 |
else if (strcmp(string, "g723-5") == 0 |
| 350 |
|| strcmp(string, "g723_5") == 0) |
| 351 |
{ |
| 352 |
return RPLAY_FORMAT_G723_5; |
| 353 |
} |
| 354 |
else if (strcmp(string, "gsm") == 0 |
| 355 |
|| strcmp(string, "GSM") == 0) |
| 356 |
{ |
| 357 |
return RPLAY_FORMAT_GSM; |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
return RPLAY_FORMAT_NONE; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
#ifdef __STDC__ |
| 366 |
char * |
| 367 |
byte_order_to_string(int byte_order) |
| 368 |
#else |
| 369 |
char * |
| 370 |
byte_order_to_string(byte_order) |
| 371 |
int byte_order; |
| 372 |
#endif |
| 373 |
{ |
| 374 |
switch (byte_order) |
| 375 |
{ |
| 376 |
case RPLAY_BIG_ENDIAN: |
| 377 |
return "big-endian"; |
| 378 |
|
| 379 |
case RPLAY_LITTLE_ENDIAN: |
| 380 |
return "little-endian"; |
| 381 |
|
| 382 |
default: |
| 383 |
return "unknown"; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
#ifdef __STDC__ |
| 388 |
int |
| 389 |
string_to_byte_order(char *string) |
| 390 |
#else |
| 391 |
int |
| 392 |
string_to_byte_order(string) |
| 393 |
char *string; |
| 394 |
#endif |
| 395 |
{ |
| 396 |
if (strcmp(string, "big-endian") == 0 |
| 397 |
|| strcmp(string, "big") == 0) |
| 398 |
{ |
| 399 |
return RPLAY_BIG_ENDIAN; |
| 400 |
} |
| 401 |
else if (strcmp(string, "little-endian") == 0 |
| 402 |
|| strcmp(string, "little") == 0) |
| 403 |
{ |
| 404 |
return RPLAY_LITTLE_ENDIAN; |
| 405 |
} |
| 406 |
else |
| 407 |
{ |
| 408 |
return 0; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
#ifdef __STDC__ |
| 413 |
char * |
| 414 |
storage_to_string(int storage) |
| 415 |
#else |
| 416 |
char * |
| 417 |
storage_to_string(storage) |
| 418 |
int storage; |
| 419 |
#endif |
| 420 |
{ |
| 421 |
switch (storage) |
| 422 |
{ |
| 423 |
case SOUND_STORAGE_NONE: |
| 424 |
return "none"; |
| 425 |
|
| 426 |
case SOUND_STORAGE_DISK: |
| 427 |
return "disk"; |
| 428 |
|
| 429 |
case SOUND_STORAGE_MEMORY: |
| 430 |
return "memory"; |
| 431 |
|
| 432 |
default: |
| 433 |
return "unknown"; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
#ifdef __STDC__ |
| 438 |
int |
| 439 |
string_to_storage(char *string) |
| 440 |
#else |
| 441 |
int |
| 442 |
string_to_storage(string) |
| 443 |
char *string; |
| 444 |
#endif |
| 445 |
{ |
| 446 |
if (strcmp(string, "none") == 0) |
| 447 |
{ |
| 448 |
return SOUND_STORAGE_NONE; |
| 449 |
} |
| 450 |
else if (strcmp(string, "disk") == 0) |
| 451 |
{ |
| 452 |
return SOUND_STORAGE_DISK; |
| 453 |
} |
| 454 |
else if (strcmp(string, "memory") == 0) |
| 455 |
{ |
| 456 |
return SOUND_STORAGE_MEMORY; |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
return SOUND_STORAGE_NULL; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
#ifdef __STDC__ |
| 465 |
char * |
| 466 |
input_to_string(int input) |
| 467 |
#else |
| 468 |
char * |
| 469 |
input_to_string(input) |
| 470 |
int input; |
| 471 |
#endif |
| 472 |
{ |
| 473 |
switch (input) |
| 474 |
{ |
| 475 |
case SOUND_FILE: |
| 476 |
return "file"; |
| 477 |
|
| 478 |
case SOUND_FLOW: |
| 479 |
return "flow"; |
| 480 |
|
| 481 |
#ifdef HAVE_CDROM |
| 482 |
case SOUND_CDROM: |
| 483 |
return "cdrom"; |
| 484 |
#endif /* HAVE_CDROM */ |
| 485 |
|
| 486 |
case SOUND_VIRTUAL: |
| 487 |
return "virtual"; |
| 488 |
|
| 489 |
default: |
| 490 |
return "unknown"; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
#ifdef __STDC__ |
| 495 |
int |
| 496 |
string_to_input(char *string) |
| 497 |
#else |
| 498 |
int |
| 499 |
string_to_input(string) |
| 500 |
char *string; |
| 501 |
#endif |
| 502 |
{ |
| 503 |
if (strcmp(string, "file") == 0) |
| 504 |
{ |
| 505 |
return SOUND_FILE; |
| 506 |
} |
| 507 |
else if (strcmp(string, "flow") == 0) |
| 508 |
{ |
| 509 |
return SOUND_FLOW; |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
return 0; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
#ifdef __STDC__ |
| 518 |
char * |
| 519 |
audio_port_to_string(int port) |
| 520 |
#else |
| 521 |
char * |
| 522 |
audio_port_to_string(port) |
| 523 |
int port; |
| 524 |
#endif |
| 525 |
{ |
| 526 |
static char string[128]; |
| 527 |
int n; |
| 528 |
|
| 529 |
string[0] = '\0'; |
| 530 |
if (BIT(port, RPLAY_AUDIO_PORT_NONE)) |
| 531 |
{ |
| 532 |
strncat(string, "none,", sizeof(string) - strlen(string)); |
| 533 |
} |
| 534 |
if (BIT(port, RPLAY_AUDIO_PORT_SPEAKER)) |
| 535 |
{ |
| 536 |
strncat(string, "speaker,", sizeof(string) - strlen(string)); |
| 537 |
} |
| 538 |
if (BIT(port, RPLAY_AUDIO_PORT_HEADPHONE)) |
| 539 |
{ |
| 540 |
strncat(string, "headphone,", sizeof(string) - strlen(string)); |
| 541 |
} |
| 542 |
if (BIT(port, RPLAY_AUDIO_PORT_LINEOUT)) |
| 543 |
{ |
| 544 |
strncat(string, "lineout,", sizeof(string) - strlen(string)); |
| 545 |
} |
| 546 |
string[strlen(string) - 1] = '\0'; |
| 547 |
|
| 548 |
return string; |
| 549 |
} |
| 550 |
|
| 551 |
unsigned short |
| 552 |
little_short(p) |
| 553 |
char *p; |
| 554 |
{ |
| 555 |
return (((unsigned long) (((unsigned char *) p)[1])) << 8) | |
| 556 |
((unsigned long) (((unsigned char *) p)[0])); |
| 557 |
} |
| 558 |
|
| 559 |
unsigned short |
| 560 |
big_short(p) |
| 561 |
char *p; |
| 562 |
{ |
| 563 |
return (((unsigned long) (((unsigned char *) p)[0])) << 8) | |
| 564 |
((unsigned long) (((unsigned char *) p)[1])); |
| 565 |
} |
| 566 |
|
| 567 |
unsigned long |
| 568 |
little_long(p) |
| 569 |
char *p; |
| 570 |
{ |
| 571 |
return (((unsigned long) (((unsigned char *) p)[3])) << 24) | |
| 572 |
(((unsigned long) (((unsigned char *) p)[2])) << 16) | |
| 573 |
(((unsigned long) (((unsigned char *) p)[1])) << 8) | |
| 574 |
((unsigned long) (((unsigned char *) p)[0])); |
| 575 |
} |
| 576 |
|
| 577 |
unsigned long |
| 578 |
big_long(p) |
| 579 |
char *p; |
| 580 |
{ |
| 581 |
return (((unsigned long) (((unsigned char *) p)[0])) << 24) | |
| 582 |
(((unsigned long) (((unsigned char *) p)[1])) << 16) | |
| 583 |
(((unsigned long) (((unsigned char *) p)[2])) << 8) | |
| 584 |
((unsigned long) (((unsigned char *) p)[3])); |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* C O N V E R T F R O M I E E E E X T E N D E D |
| 589 |
*/ |
| 590 |
|
| 591 |
/* |
| 592 |
* Copyright (C) 1988-1991 Apple Computer, Inc. |
| 593 |
* All rights reserved. |
| 594 |
* |
| 595 |
* Machine-independent I/O routines for IEEE floating-point numbers. |
| 596 |
* |
| 597 |
* NaN's and infinities are converted to HUGE_VAL or HUGE, which |
| 598 |
* happens to be infinity on IEEE machines. Unfortunately, it is |
| 599 |
* impossible to preserve NaN's in a machine-independent way. |
| 600 |
* Infinities are, however, preserved on IEEE machines. |
| 601 |
* |
| 602 |
* These routines have been tested on the following machines: |
| 603 |
* Apple Macintosh, MPW 3.1 C compiler |
| 604 |
* Apple Macintosh, THINK C compiler |
| 605 |
* Silicon Graphics IRIS, MIPS compiler |
| 606 |
* Cray X/MP and Y/MP |
| 607 |
* Digital Equipment VAX |
| 608 |
* |
| 609 |
* |
| 610 |
* Implemented by Malcolm Slaney and Ken Turkowski. |
| 611 |
* |
| 612 |
* Malcolm Slaney contributions during 1988-1990 include big- and little- |
| 613 |
* endian file I/O, conversion to and from Motorola's extended 80-bit |
| 614 |
* floating-point format, and conversions to and from IEEE single- |
| 615 |
* precision floating-point format. |
| 616 |
* |
| 617 |
* In 1991, Ken Turkowski implemented the conversions to and from |
| 618 |
* IEEE double-precision format, added more precision to the extended |
| 619 |
* conversions, and accommodated conversions involving +/- infinity, |
| 620 |
* NaN's, and denormalized numbers. |
| 621 |
*/ |
| 622 |
|
| 623 |
#ifndef HUGE_VAL |
| 624 |
#define HUGE_VAL HUGE |
| 625 |
#endif /*HUGE_VAL */ |
| 626 |
|
| 627 |
#define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0) |
| 628 |
|
| 629 |
/**************************************************************** |
| 630 |
* Extended precision IEEE floating-point conversion routine. |
| 631 |
****************************************************************/ |
| 632 |
|
| 633 |
double |
| 634 |
ConvertFromIeeeExtended(bytes) |
| 635 |
unsigned char *bytes; /* LCN */ |
| 636 |
{ |
| 637 |
double f; |
| 638 |
int expon; |
| 639 |
unsigned long hiMant, loMant; |
| 640 |
|
| 641 |
expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF); |
| 642 |
hiMant = ((unsigned long) (bytes[2] & 0xFF) << 24) |
| 643 |
| ((unsigned long) (bytes[3] & 0xFF) << 16) |
| 644 |
| ((unsigned long) (bytes[4] & 0xFF) << 8) |
| 645 |
| ((unsigned long) (bytes[5] & 0xFF)); |
| 646 |
loMant = ((unsigned long) (bytes[6] & 0xFF) << 24) |
| 647 |
| ((unsigned long) (bytes[7] & 0xFF) << 16) |
| 648 |
| ((unsigned long) (bytes[8] & 0xFF) << 8) |
| 649 |
| ((unsigned long) (bytes[9] & 0xFF)); |
| 650 |
|
| 651 |
if (expon == 0 && hiMant == 0 && loMant == 0) |
| 652 |
{ |
| 653 |
f = 0; |
| 654 |
} |
| 655 |
else |
| 656 |
{ |
| 657 |
if (expon == 0x7FFF) |
| 658 |
{ /* Infinity or NaN */ |
| 659 |
f = HUGE_VAL; |
| 660 |
} |
| 661 |
else |
| 662 |
{ |
| 663 |
expon -= 16383; |
| 664 |
f = ldexp(UnsignedToFloat(hiMant), expon -= 31); |
| 665 |
f += ldexp(UnsignedToFloat(loMant), expon -= 32); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
if (bytes[0] & 0x80) |
| 670 |
return -f; |
| 671 |
else |
| 672 |
return f; |
| 673 |
} |