PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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