/src/types.c

https://code.google.com/ · C · 92 lines · 54 code · 9 blank · 29 comment · 7 complexity · f402d318ff1d539f9c8cbed3bb359eee MD5 · raw file

  1. /*
  2. $Id: types.c 231 2011-06-27 13:46:19Z marc.noirot $
  3. FLV Metadata updater
  4. Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
  5. This file is part of FLVMeta.
  6. FLVMeta is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. FLVMeta is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with FLVMeta; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "types.h"
  19. #ifndef WORDS_BIGENDIAN
  20. /* swap 64 bits doubles */
  21. typedef union __convert_u {
  22. uint64 i;
  23. number64 f;
  24. } convert_u;
  25. number64 swap_number64(number64 n) {
  26. convert_u c;
  27. c.f = n;
  28. c.i = (((c.i & 0x00000000000000FFULL) << 56) |
  29. ((c.i & 0x000000000000FF00ULL) << 40) |
  30. ((c.i & 0x0000000000FF0000ULL) << 24) |
  31. ((c.i & 0x00000000FF000000ULL) << 8) |
  32. ((c.i & 0x000000FF00000000ULL) >> 8) |
  33. ((c.i & 0x0000FF0000000000ULL) >> 24) |
  34. ((c.i & 0x00FF000000000000ULL) >> 40) |
  35. ((c.i & 0xFF00000000000000ULL) >> 56));
  36. return c.f;
  37. }
  38. #endif /* !defined WORDS_BIGENDIAN */
  39. /* convert native integers into 24 bits big endian integers */
  40. uint24_be uint32_to_uint24_be(uint32 l) {
  41. uint24_be r;
  42. r.b[0] = (uint8)((l & 0x00FF0000U) >> 16);
  43. r.b[1] = (uint8)((l & 0x0000FF00U) >> 8);
  44. r.b[2] = (uint8) (l & 0x000000FFU);
  45. return r;
  46. }
  47. #ifdef WIN32
  48. /*
  49. These functions assume fpos_t is a 64-bit signed integer
  50. */
  51. file_offset_t lfs_ftell(FILE * stream) {
  52. fpos_t p;
  53. if (fgetpos(stream, &p) == 0) {
  54. return (file_offset_t)p;
  55. }
  56. else {
  57. return -1LL;
  58. }
  59. }
  60. int lfs_fseek(FILE * stream, file_offset_t offset, int whence) {
  61. fpos_t p;
  62. if (fgetpos(stream, &p) == 0) {
  63. switch (whence) {
  64. case SEEK_CUR: p += offset; break;
  65. case SEEK_SET: p = offset; break;
  66. /*case SEEK_END:; not implemented here */
  67. default:
  68. return -1;
  69. }
  70. fsetpos(stream, &p);
  71. return 0;
  72. }
  73. else {
  74. return -1;
  75. }
  76. }
  77. #endif /* WIN32 */