29 #include <nsComponentManagerUtils.h>
30 #include <nsICryptoHash.h>
32 #include <nsIScriptableUConv.h>
34 #include <sbIDatabaseQuery.h>
35 #include <sbIDatabaseResult.h>
36 #include <sbIDatabasePreparedStatement.h>
50 mHashProc = do_CreateInstance(
"@mozilla.org/security/hash;1", &rv);
51 NS_ENSURE_SUCCESS(rv, rv);
52 mHashProc->Init(nsICryptoHash::MD5);
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"));
61 nsString sql(NS_LITERAL_STRING(
"CREATE TABLE IF NOT EXISTS itunes_signatures (id TEXT UNIQUE NOT NULL, signature TEXT NOT NULL)"));
63 rv = mDBQuery->AddQuery(sql);
64 NS_ENSURE_SUCCESS(rv, rv);
67 rv = mDBQuery->Execute(&result);
68 NS_ENSURE_SUCCESS(rv, rv);
69 NS_ENSURE_TRUE(result == 0, NS_ERROR_FAILURE);
71 rv = mDBQuery->ResetQuery();
72 NS_ENSURE_SUCCESS(rv, rv);
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);
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);
92 nsCString
const & byteString = ::NS_ConvertUTF16toUTF8(aStringData);
93 rv = mHashProc->Update(reinterpret_cast<PRUint8 const *>(byteString.BeginReading()),
95 NS_ENSURE_SUCCESS(rv, rv);
102 if (mSignature.IsEmpty()) {
104 nsresult rv = mHashProc->Finish(PR_TRUE, hashData);
105 NS_ENSURE_SUCCESS(rv, rv);
107 nsCString hashString;
108 unsigned char const ZERO =
'0';
109 for (
unsigned char const * c =
110 reinterpret_cast<unsigned char const *>(hashData.BeginReading());
113 unsigned char nibble = (*c >> 4) + ZERO;
114 hashString.Append(nibble);
115 nibble = (*c & 0xF) + ZERO;
116 hashString.Append(nibble);
118 mSignature = NS_ConvertASCIItoUTF16(hashString);
120 aSignature = mSignature;
126 nsAString
const & aSignature) {
128 nsresult rv = mDBQuery->AddPreparedStatement(mInsertSig);
129 NS_ENSURE_SUCCESS(rv, rv);
131 rv = mDBQuery->BindStringParameter(0, aID);
132 NS_ENSURE_SUCCESS(rv, rv);
134 rv = mDBQuery->BindStringParameter(1, aSignature);
135 NS_ENSURE_SUCCESS(rv, rv);
138 rv = mDBQuery->Execute(&result);
139 NS_ENSURE_SUCCESS(rv, rv);
140 NS_ENSURE_TRUE(result == 0, NS_ERROR_FAILURE);
142 rv = mDBQuery->ResetQuery();
143 NS_ENSURE_SUCCESS(rv, rv);
150 nsAString & aSignature) {
152 nsresult rv = mDBQuery->AddPreparedStatement(mRetrieveSig);
153 NS_ENSURE_SUCCESS(rv, rv);
155 rv = mDBQuery->BindStringParameter(0, aID);
156 NS_ENSURE_SUCCESS(rv, rv);
159 rv = mDBQuery->Execute(&dbResult);
160 NS_ENSURE_SUCCESS(rv, rv);
161 NS_ENSURE_TRUE(dbResult == 0, NS_ERROR_FAILURE);
163 nsCOMPtr<sbIDatabaseResult> result;
164 rv = mDBQuery->GetResultObject(getter_AddRefs(result));
165 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
167 rv = result->GetRowCell(0, 0, aSignature);
168 NS_ENSURE_SUCCESS(rv, rv);
170 rv = mDBQuery->ResetQuery();
171 NS_ENSURE_SUCCESS(rv, rv);
char const SB_ITUNES_DB_GUID[]
nsresult StoreSignature(nsAString const &aID, nsAString const &aSignature)
nsresult Update(nsAString const &aStringData)
nsresult RetrieveSignature(nsAString const &aID, nsAString &aSignature)
nsresult GetSignature(nsAString &aSignature)