1 // IOChannel.cpp - a virtual IO channel, for Gnash
2 //
3 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 //   Free Software Foundation, Inc
5 //
6 // This program 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 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 //
20 
21 
22 #include "IOChannel.h"
23 
24 namespace gnash
25 {
26 
27 std::uint32_t
read_le32()28 IOChannel::read_le32()
29 {
30     // read_byte() is std::uint8_t, so no masks with 0xff are required.
31     std::uint32_t result = static_cast<std::uint32_t>(read_byte());
32     result |= static_cast<std::uint32_t>(read_byte()) << 8;
33     result |= static_cast<std::uint32_t>(read_byte()) << 16;
34     result |= static_cast<std::uint32_t>(read_byte()) << 24;
35     return(result);
36 }
37 
38 std::uint16_t
read_le16()39 IOChannel::read_le16()
40 {
41     std::uint16_t result = static_cast<std::uint16_t>(read_byte());
42     result |= static_cast<std::uint16_t>(read_byte()) << 8;
43     return(result);
44 }
45 
46 int
read_string(char * dst,int max_length)47 IOChannel::read_string(char* dst, int max_length)
48 {
49     int i = 0;
50     while (i<max_length)
51     {
52         dst[i] = read_byte();
53         if (dst[i]=='\0') return i;
54         i++;
55     }
56 
57     dst[max_length - 1] = '\0';    // force termination.
58 
59     return -1;
60 }
61 
62 std::uint8_t
read_byte()63 IOChannel::read_byte()
64 {
65     std::uint8_t u;
66     if ( read(&u, 1) == -1 )
67     {
68         throw IOException("Could not read a single byte from input");
69     }
70     return u;
71 }
72 
73 std::streamsize
write(const void *,std::streamsize)74 IOChannel::write(const void* /*src*/, std::streamsize /*num*/)
75 {
76     throw IOException("This IOChannel implementation doesn't support output");
77 }
78 
79 } // namespace gnash
80