sbiTunesSignature.cpp
Go to the documentation of this file.
1 /*
2  //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
27 #include "sbiTunesSignature.h"
28 
29 #include <nsComponentManagerUtils.h>
30 #include <nsICryptoHash.h>
31 #include <nsMemory.h>
32 #include <nsIScriptableUConv.h>
33 
34 #include <sbIDatabaseQuery.h>
35 #include <sbIDatabaseResult.h>
36 #include <sbIDatabasePreparedStatement.h>
37 
39 
40 
41 char const SB_ITUNES_DB_GUID[] = "songbird";
42 
45 
47  nsresult rv;
48 
49  /* Create a hashing processor. */
50  mHashProc = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
51  NS_ENSURE_SUCCESS(rv, rv);
52  mHashProc->Init(nsICryptoHash::MD5);
53 
54  /* Create a signature database query component. */
55  mDBQuery = do_CreateInstance("@songbirdnest.com/Songbird/DatabaseQuery;1", &rv);
56  NS_ENSURE_SUCCESS(rv, rv);
57  mDBQuery->SetAsyncQuery(PR_FALSE);
58  mDBQuery->SetDatabaseGUID(NS_LITERAL_STRING("songbird"));
59 
60  /* Ensure the signature database table exists. */
61  nsString sql(NS_LITERAL_STRING("CREATE TABLE IF NOT EXISTS itunes_signatures (id TEXT UNIQUE NOT NULL, signature TEXT NOT NULL)"));
62 
63  rv = mDBQuery->AddQuery(sql);
64  NS_ENSURE_SUCCESS(rv, rv);
65 
66  PRInt32 result;
67  rv = mDBQuery->Execute(&result);
68  NS_ENSURE_SUCCESS(rv, rv);
69  NS_ENSURE_TRUE(result == 0, NS_ERROR_FAILURE);
70 
71  rv = mDBQuery->ResetQuery();
72  NS_ENSURE_SUCCESS(rv, rv);
73 
74  NS_NAMED_LITERAL_STRING(INSERT_SQL,
75  "INSERT OR REPLACE INTO itunes_signatures (id, signature) VALUES (?, ?)");
76  rv = mDBQuery->PrepareQuery(INSERT_SQL,
77  getter_AddRefs(mInsertSig));
78  NS_ENSURE_SUCCESS(rv, rv);
79 
80  NS_NAMED_LITERAL_STRING(SELECT_SQL,
81  "SELECT signature FROM itunes_signatures WHERE id = ?");
82  rv = mDBQuery->PrepareQuery(SELECT_SQL,
83  getter_AddRefs(mRetrieveSig));
84  NS_ENSURE_SUCCESS(rv, rv);
85 
86  return NS_OK;
87 }
88 
89 nsresult sbiTunesSignature::Update(nsAString const & aStringData) {
90  nsresult rv;
91 
92  nsCString const & byteString = ::NS_ConvertUTF16toUTF8(aStringData);
93  rv = mHashProc->Update(reinterpret_cast<PRUint8 const *>(byteString.BeginReading()),
94  byteString.Length());
95  NS_ENSURE_SUCCESS(rv, rv);
96 
97  return NS_OK;
98 }
99 
100 nsresult
101 sbiTunesSignature::GetSignature(nsAString & aSignature) {
102  if (mSignature.IsEmpty()) {
103  nsCString hashData;
104  nsresult rv = mHashProc->Finish(PR_TRUE, hashData);
105  NS_ENSURE_SUCCESS(rv, rv);
106 
107  nsCString hashString;
108  unsigned char const ZERO = '0';
109  for (unsigned char const * c =
110  reinterpret_cast<unsigned char const *>(hashData.BeginReading());
111  *c;
112  ++c) {
113  unsigned char nibble = (*c >> 4) + ZERO;
114  hashString.Append(nibble);
115  nibble = (*c & 0xF) + ZERO;
116  hashString.Append(nibble);
117  }
118  mSignature = NS_ConvertASCIItoUTF16(hashString);
119  }
120  aSignature = mSignature;
121  return NS_OK;
122 }
123 
124 nsresult
125 sbiTunesSignature::StoreSignature(nsAString const & aID,
126  nsAString const & aSignature) {
127 
128  nsresult rv = mDBQuery->AddPreparedStatement(mInsertSig);
129  NS_ENSURE_SUCCESS(rv, rv);
130 
131  rv = mDBQuery->BindStringParameter(0, aID);
132  NS_ENSURE_SUCCESS(rv, rv);
133 
134  rv = mDBQuery->BindStringParameter(1, aSignature);
135  NS_ENSURE_SUCCESS(rv, rv);
136 
137  PRInt32 result;
138  rv = mDBQuery->Execute(&result);
139  NS_ENSURE_SUCCESS(rv, rv);
140  NS_ENSURE_TRUE(result == 0, NS_ERROR_FAILURE);
141 
142  rv = mDBQuery->ResetQuery();
143  NS_ENSURE_SUCCESS(rv, rv);
144 
145  return NS_OK;
146 }
147 
148 nsresult
150  nsAString & aSignature) {
151 
152  nsresult rv = mDBQuery->AddPreparedStatement(mRetrieveSig);
153  NS_ENSURE_SUCCESS(rv, rv);
154 
155  rv = mDBQuery->BindStringParameter(0, aID);
156  NS_ENSURE_SUCCESS(rv, rv);
157 
158  PRInt32 dbResult;
159  rv = mDBQuery->Execute(&dbResult);
160  NS_ENSURE_SUCCESS(rv, rv);
161  NS_ENSURE_TRUE(dbResult == 0, NS_ERROR_FAILURE);
162 
163  nsCOMPtr<sbIDatabaseResult> result;
164  rv = mDBQuery->GetResultObject(getter_AddRefs(result));
165  NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
166 
167  rv = result->GetRowCell(0, 0, aSignature);
168  NS_ENSURE_SUCCESS(rv, rv);
169 
170  rv = mDBQuery->ResetQuery();
171  NS_ENSURE_SUCCESS(rv, rv);
172 
173  return NS_OK;
174 }
char const SB_ITUNES_DB_GUID[]
return NS_OK
nsresult StoreSignature(nsAString const &aID, nsAString const &aSignature)
nsresult Update(nsAString const &aStringData)
nsresult RetrieveSignature(nsAString const &aID, nsAString &aSignature)
nsresult GetSignature(nsAString &aSignature)