2012-08-15 00:04:19 -07:00
|
|
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-08-15 00:10:56 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-08-15 00:04:19 -07:00
|
|
|
#ifndef SBEntries_h__
|
|
|
|
#define SBEntries_h__
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsICryptoHash.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
extern PRLogModuleInfo *gUrlClassifierDbServiceLog;
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4)
|
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#define LOG_ENABLED() (false)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
#include "plbase64.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace safebrowsing {
|
|
|
|
|
|
|
|
#define PREFIX_SIZE 4
|
|
|
|
#define COMPLETE_SIZE 32
|
|
|
|
|
2012-10-11 16:38:04 -07:00
|
|
|
template <uint32_t S, class Comparator>
|
2012-08-15 00:04:19 -07:00
|
|
|
struct SafebrowsingHash
|
|
|
|
{
|
2012-10-11 16:38:04 -07:00
|
|
|
static const uint32_t sHashSize = S;
|
2012-08-15 00:04:19 -07:00
|
|
|
typedef SafebrowsingHash<S, Comparator> self_type;
|
2012-10-11 16:38:04 -07:00
|
|
|
uint8_t buf[S];
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
nsresult FromPlaintext(const nsACString& aPlainText, nsICryptoHash* aHash) {
|
|
|
|
// From the protocol doc:
|
|
|
|
// Each entry in the chunk is composed
|
|
|
|
// of the SHA 256 hash of a suffix/prefix expression.
|
|
|
|
|
|
|
|
nsresult rv = aHash->Init(nsICryptoHash::SHA256);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aHash->Update
|
2012-10-11 16:38:04 -07:00
|
|
|
(reinterpret_cast<const uint8_t*>(aPlainText.BeginReading()),
|
2012-08-15 00:04:19 -07:00
|
|
|
aPlainText.Length());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hashed;
|
2012-08-15 00:04:19 -07:00
|
|
|
rv = aHash->Finish(false, hashed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(hashed.Length() >= sHashSize,
|
|
|
|
"not enough characters in the hash");
|
|
|
|
|
|
|
|
memcpy(buf, hashed.BeginReading(), sHashSize);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assign(const nsACString& aStr) {
|
|
|
|
NS_ASSERTION(aStr.Length() >= sHashSize,
|
|
|
|
"string must be at least sHashSize characters long");
|
|
|
|
memcpy(buf, aStr.BeginReading(), sHashSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Compare(const self_type& aOther) const {
|
|
|
|
return Comparator::Compare(buf, aOther.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const self_type& aOther) const {
|
|
|
|
return Comparator::Compare(buf, aOther.buf) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const self_type& aOther) const {
|
|
|
|
return Comparator::Compare(buf, aOther.buf) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const self_type& aOther) const {
|
|
|
|
return Comparator::Compare(buf, aOther.buf) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void ToString(nsACString& aStr) const {
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t len = ((sHashSize + 2) / 3) * 4;
|
2012-08-15 00:04:19 -07:00
|
|
|
aStr.SetCapacity(len + 1);
|
|
|
|
PL_Base64Encode((char*)buf, sHashSize, aStr.BeginWriting());
|
|
|
|
aStr.BeginWriting()[len] = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ToUint32() const {
|
2012-10-11 16:38:04 -07:00
|
|
|
return *((uint32_t*)buf);
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
void FromUint32(uint32_t aHash) {
|
2012-10-11 16:38:04 -07:00
|
|
|
*((uint32_t*)buf) = aHash;
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PrefixComparator {
|
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
static int Compare(const uint8_t* a, const uint8_t* b) {
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t first = *((uint32_t*)a);
|
|
|
|
uint32_t second = *((uint32_t*)b);
|
2012-08-15 00:08:37 -07:00
|
|
|
if (first > second) {
|
|
|
|
return 1;
|
|
|
|
} else if (first == second) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef SafebrowsingHash<PREFIX_SIZE, PrefixComparator> Prefix;
|
|
|
|
typedef nsTArray<Prefix> PrefixArray;
|
|
|
|
|
|
|
|
class CompletionComparator {
|
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
static int Compare(const uint8_t* a, const uint8_t* b) {
|
2012-08-15 00:04:19 -07:00
|
|
|
return memcmp(a, b, COMPLETE_SIZE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef SafebrowsingHash<COMPLETE_SIZE, CompletionComparator> Completion;
|
|
|
|
typedef nsTArray<Completion> CompletionArray;
|
|
|
|
|
|
|
|
struct AddPrefix {
|
|
|
|
Prefix prefix;
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t addChunk;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
AddPrefix() : addChunk(0) {}
|
|
|
|
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t Chunk() const { return addChunk; }
|
2012-08-15 00:04:19 -07:00
|
|
|
const Prefix &PrefixHash() const { return prefix; }
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
int Compare(const T& other) const {
|
|
|
|
int cmp = prefix.Compare(other.PrefixHash());
|
|
|
|
if (cmp != 0) {
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
return addChunk - other.addChunk;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AddComplete {
|
|
|
|
union {
|
|
|
|
Prefix prefix;
|
|
|
|
Completion complete;
|
|
|
|
} hash;
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t addChunk;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
AddComplete() : addChunk(0) {}
|
|
|
|
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t Chunk() const { return addChunk; }
|
2012-08-15 00:04:19 -07:00
|
|
|
const Prefix &PrefixHash() const { return hash.prefix; }
|
|
|
|
const Completion &CompleteHash() const { return hash.complete; }
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
int Compare(const T& other) const {
|
|
|
|
int cmp = hash.complete.Compare(other.CompleteHash());
|
|
|
|
if (cmp != 0) {
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
return addChunk - other.addChunk;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SubPrefix {
|
|
|
|
Prefix prefix;
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t addChunk;
|
|
|
|
uint32_t subChunk;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
SubPrefix(): addChunk(0), subChunk(0) {}
|
|
|
|
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t Chunk() const { return subChunk; }
|
|
|
|
uint32_t AddChunk() const { return addChunk; }
|
2012-08-15 00:04:19 -07:00
|
|
|
const Prefix &PrefixHash() const { return prefix; }
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
int Compare(const T& aOther) const {
|
|
|
|
int cmp = prefix.Compare(aOther.PrefixHash());
|
|
|
|
if (cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
if (addChunk != aOther.addChunk)
|
|
|
|
return addChunk - aOther.addChunk;
|
|
|
|
return subChunk - aOther.subChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
int CompareAlt(const T& aOther) const {
|
|
|
|
int cmp = prefix.Compare(aOther.PrefixHash());
|
|
|
|
if (cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
return addChunk - aOther.addChunk;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SubComplete {
|
|
|
|
union {
|
|
|
|
Prefix prefix;
|
|
|
|
Completion complete;
|
|
|
|
} hash;
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t addChunk;
|
|
|
|
uint32_t subChunk;
|
2012-08-15 00:04:19 -07:00
|
|
|
|
|
|
|
SubComplete() : addChunk(0), subChunk(0) {}
|
|
|
|
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t Chunk() const { return subChunk; }
|
|
|
|
uint32_t AddChunk() const { return addChunk; }
|
2012-08-15 00:04:19 -07:00
|
|
|
const Prefix &PrefixHash() const { return hash.prefix; }
|
|
|
|
const Completion &CompleteHash() const { return hash.complete; }
|
|
|
|
|
|
|
|
int Compare(const SubComplete& aOther) const {
|
|
|
|
int cmp = hash.complete.Compare(aOther.hash.complete);
|
|
|
|
if (cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
if (addChunk != aOther.addChunk)
|
|
|
|
return addChunk - aOther.addChunk;
|
|
|
|
return subChunk - aOther.subChunk;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef nsTArray<AddPrefix> AddPrefixArray;
|
|
|
|
typedef nsTArray<AddComplete> AddCompleteArray;
|
|
|
|
typedef nsTArray<SubPrefix> SubPrefixArray;
|
|
|
|
typedef nsTArray<SubComplete> SubCompleteArray;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares chunks by their add chunk, then their prefix.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class EntryCompare {
|
|
|
|
public:
|
|
|
|
typedef T elem_type;
|
2012-08-15 00:10:07 -07:00
|
|
|
static int Compare(const void* e1, const void* e2) {
|
2012-08-15 00:04:19 -07:00
|
|
|
const elem_type* a = static_cast<const elem_type*>(e1);
|
|
|
|
const elem_type* b = static_cast<const elem_type*>(e2);
|
|
|
|
return a->Compare(*b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort an array of store entries. nsTArray::Sort uses Equal/LessThan
|
|
|
|
* to sort, this does a single Compare so it's a bit quicker over the
|
|
|
|
* large sorts we do.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
EntrySort(nsTArray<T>& aArray)
|
|
|
|
{
|
2012-08-15 00:10:07 -07:00
|
|
|
qsort(aArray.Elements(), aArray.Length(), sizeof(T),
|
|
|
|
EntryCompare<T>::Compare);
|
2012-08-15 00:04:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
ReadTArray(nsIInputStream* aStream, nsTArray<T>* aArray, uint32_t aNumElements)
|
2012-08-15 00:04:19 -07:00
|
|
|
{
|
|
|
|
if (!aArray->SetLength(aNumElements))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
void *buffer = aArray->Elements();
|
|
|
|
nsresult rv = NS_ReadInputStreamToBuffer(aStream, &buffer,
|
|
|
|
(aNumElements * sizeof(T)));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
nsresult
|
|
|
|
WriteTArray(nsIOutputStream* aStream, nsTArray<T>& aArray)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t written;
|
2012-08-15 00:04:19 -07:00
|
|
|
return aStream->Write(reinterpret_cast<char*>(aArray.Elements()),
|
|
|
|
aArray.Length() * sizeof(T),
|
|
|
|
&written);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|