sbImageParser.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 //------------------------------------------------------------------------------
26 //------------------------------------------------------------------------------
27 //
28 // Songbird image parser.
29 //
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 
38 //------------------------------------------------------------------------------
39 //
40 // Songbird image parser imported services.
41 //
42 //------------------------------------------------------------------------------
43 
44 // Self imports.
45 #include "sbImageParser.h"
46 
47 // Songbird imports
48 #include <sbMemoryUtils.h>
49 
50 // Mozilla imports.
51 #include <nsComponentManagerUtils.h>
52 #include <nsServiceManagerUtils.h>
53 #include <plbase64.h>
54 
55 #include <nsIBinaryInputStream.h>
56 #include <nsIFileStreams.h>
57 #include <nsIIOService.h>
58 
59 #ifdef IS_BIG_ENDIAN
60 #define LITTLE_TO_NATIVE16(x) (((((PRUint16) x) & 0xFF) << 8) | \
61  (((PRUint16) x) >> 8))
62 #define LITTLE_TO_NATIVE32(x) (((((PRUint32) x) & 0xFF) << 24) | \
63  (((((PRUint32) x) >> 8) & 0xFF) << 16) | \
64  (((((PRUint32) x) >> 16) & 0xFF) << 8) | \
65  (((PRUint32) x) >> 24))
66 #else
67 #define LITTLE_TO_NATIVE16(x) x
68 #define LITTLE_TO_NATIVE32(x) x
69 #endif
70 
71 //------------------------------------------------------------------------------
72 //
73 // Songbird image parser nsISupports implementation.
74 //
75 //------------------------------------------------------------------------------
76 
79 
80 
81 //------------------------------------------------------------------------------
82 //
83 // Songbird image parser sbIImageParser implementation.
84 //
85 //------------------------------------------------------------------------------
86 
87 NS_IMETHODIMP
88 sbImageParser::GetICOBySize(nsILocalFile* aICOFile,
89  PRUint32 aSize,
90  nsAString& _retval)
91 {
92  nsresult rv;
93  PRUint32 dataSize;
94  PRUint8* data;
95 
96  nsCOMPtr<nsIFileInputStream> fileStream =
97  do_CreateInstance("@mozilla.org/network/file-input-stream;1", &rv);
98  NS_ENSURE_SUCCESS(rv, rv);
99 
100  rv = fileStream->Init(aICOFile, -1, -1, 0);
101  NS_ENSURE_SUCCESS(rv, rv);
102 
103  // Read the binary data from the file into our buffer
104  rv = fileStream->Available(&dataSize);
105  NS_ENSURE_SUCCESS(rv, rv);
106 
107  nsCOMPtr<nsIBinaryInputStream> imageDataStream =
108  do_CreateInstance("@mozilla.org/binaryinputstream;1", &rv);
109  NS_ENSURE_SUCCESS(rv, rv);
110 
111  rv = imageDataStream->SetInputStream(fileStream);
112  NS_ENSURE_SUCCESS(rv, rv);
113 
114  rv = imageDataStream->ReadByteArray(dataSize, &data);
115  NS_ENSURE_SUCCESS(rv, rv);
116 
117  rv = fileStream->Close();
118  NS_ENSURE_SUCCESS(rv, rv);
119 
120  // Make sure we have a sane number of bytes in the buffer
121  if (dataSize < 6)
122  return NS_ERROR_UNEXPECTED;
123 
124  // Sanity check that the first 4 bytes of the .ico are right
125  if (!(data[0] == 0 && data[1] == 0 && data[2] == 1 && data[3] == 0))
126  return NS_ERROR_UNEXPECTED;
127 
128  // Get the # of icons present in the .ico file
129  PRUint16 numIcons = 0;
130  memcpy(&numIcons, data+4, 2);
131  numIcons = LITTLE_TO_NATIVE16(numIcons);
132 
133  // Loop through the directory entries to find where our icons are
134  for (unsigned int i=0; i<numIcons; i++) {
135  PRUint8 width = 0;
136  if (dataSize < (7 + (i*16)))
137  return NS_ERROR_UNEXPECTED;
138  memcpy(&width, data + 6 + (i*16), 1);
139  if (width == aSize) {
140  rv = GetIcon(i, data, dataSize, _retval);
141  NS_ENSURE_SUCCESS(rv, rv);
142  }
143  }
144 
145  if (_retval.IsEmpty()) {
146  return NS_ERROR_NOT_AVAILABLE;
147  }
148 
149  return NS_OK;
150 }
151 
161 nsresult
162 sbImageParser::GetIcon(PRUint32 imageIndex,
163  PRUint8* data,
164  PRUint32 dataSize,
165  nsAString& iconString)
166 {
167  PRUint32 offset;
168  PRUint32 size = 0;
169 
170  // get the size & offset
171  if (dataSize < (6 + (imageIndex*16) + 12 + 4))
172  return NS_ERROR_UNEXPECTED;
173  memcpy(&size, data + 6 + (imageIndex*16) + 8, 4);
174  memcpy(&offset, data + 6 + (imageIndex*16) + 12, 4);
175  size = LITTLE_TO_NATIVE32(size);
176  offset = LITTLE_TO_NATIVE32(offset);
177 
178  // create a new ico buffer, with 1 image, and 1 directory entry
179  // 22 bytes of ico header & directory entry
180  PRUint8* buffer = static_cast<PRUint8*>(NS_Alloc(size + 22));
181  NS_ENSURE_TRUE(buffer, NS_ERROR_OUT_OF_MEMORY);
182  sbAutoNSMemPtr autoBuffer (buffer);
183 
184  // set type & # of images (1) in file
185  buffer[0] = buffer[1] = buffer[3] = buffer[5] = 0;
186  buffer[2] = buffer[4] = 1;
187 
188  // copy over directory entry
189  memcpy(buffer+6, data + 6 + (imageIndex*16), 16);
190 
191  // munge the offset since we only have one image in this "file"
192  buffer[18] = 22;
193  buffer[19] = buffer[20] = buffer[21] = 0;
194 
195  // copy over icon data bytes
196  if (dataSize < (offset + size))
197  return NS_ERROR_UNEXPECTED;
198  memcpy(buffer+22, data + offset, size);
199 
200  // base64 encode it into a data URI
201  iconString.AssignLiteral("data:image/vnd.microsoft.icon;base64,");
202  iconString.AppendLiteral(PL_Base64Encode((char*)buffer, size+22, NULL));
203 
204  return NS_OK;
205 }
206 
208 {
209 }
210 
212 {
213 }
return NS_OK
Songbird Image Parser Definitions.
#define LITTLE_TO_NATIVE16(x)
function width(ele) rect(ele).width
PRUint32 & offset
#define LITTLE_TO_NATIVE32(x)
virtual ~sbImageParser()
NS_DECL_ISUPPORTS NS_DECL_SBIIMAGEPARSER sbImageParser()
observe data
Definition: FeedWriter.js:1329
_getSelectedPageStyle s i
NS_IMPL_THREADSAFE_ISUPPORTS1(sbImageParser, sbIImageParser) NS_IMETHODIMP sbImageParser