|
1 //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef ProtocolParser_h__ |
|
7 #define ProtocolParser_h__ |
|
8 |
|
9 #include "HashStore.h" |
|
10 #include "nsICryptoHMAC.h" |
|
11 |
|
12 namespace mozilla { |
|
13 namespace safebrowsing { |
|
14 |
|
15 /** |
|
16 * Some helpers for parsing the safe |
|
17 */ |
|
18 class ProtocolParser { |
|
19 public: |
|
20 struct ForwardedUpdate { |
|
21 nsCString table; |
|
22 nsCString url; |
|
23 }; |
|
24 |
|
25 ProtocolParser(); |
|
26 ~ProtocolParser(); |
|
27 |
|
28 nsresult Status() const { return mUpdateStatus; } |
|
29 |
|
30 nsresult Init(nsICryptoHash* aHasher); |
|
31 |
|
32 void SetCurrentTable(const nsACString& aTable); |
|
33 |
|
34 nsresult Begin(); |
|
35 nsresult AppendStream(const nsACString& aData); |
|
36 |
|
37 // Forget the table updates that were created by this pass. It |
|
38 // becomes the caller's responsibility to free them. This is shitty. |
|
39 TableUpdate *GetTableUpdate(const nsACString& aTable); |
|
40 void ForgetTableUpdates() { mTableUpdates.Clear(); } |
|
41 nsTArray<TableUpdate*> &GetTableUpdates() { return mTableUpdates; } |
|
42 |
|
43 // Update information. |
|
44 const nsTArray<ForwardedUpdate> &Forwards() const { return mForwards; } |
|
45 int32_t UpdateWait() { return mUpdateWait; } |
|
46 bool ResetRequested() { return mResetRequested; } |
|
47 |
|
48 private: |
|
49 nsresult ProcessControl(bool* aDone); |
|
50 nsresult ProcessExpirations(const nsCString& aLine); |
|
51 nsresult ProcessChunkControl(const nsCString& aLine); |
|
52 nsresult ProcessForward(const nsCString& aLine); |
|
53 nsresult AddForward(const nsACString& aUrl); |
|
54 nsresult ProcessChunk(bool* done); |
|
55 // Remove this, it's only used for testing |
|
56 nsresult ProcessPlaintextChunk(const nsACString& aChunk); |
|
57 nsresult ProcessShaChunk(const nsACString& aChunk); |
|
58 nsresult ProcessHostAdd(const Prefix& aDomain, uint8_t aNumEntries, |
|
59 const nsACString& aChunk, uint32_t* aStart); |
|
60 nsresult ProcessHostSub(const Prefix& aDomain, uint8_t aNumEntries, |
|
61 const nsACString& aChunk, uint32_t* aStart); |
|
62 nsresult ProcessHostAddComplete(uint8_t aNumEntries, const nsACString& aChunk, |
|
63 uint32_t *aStart); |
|
64 nsresult ProcessHostSubComplete(uint8_t numEntries, const nsACString& aChunk, |
|
65 uint32_t* start); |
|
66 // Digest chunks are very similar to shavar chunks, except digest chunks |
|
67 // always contain the full hash, so there is no need for chunk data to |
|
68 // contain prefix sizes. |
|
69 nsresult ProcessDigestChunk(const nsACString& aChunk); |
|
70 nsresult ProcessDigestAdd(const nsACString& aChunk); |
|
71 nsresult ProcessDigestSub(const nsACString& aChunk); |
|
72 bool NextLine(nsACString& aLine); |
|
73 |
|
74 void CleanupUpdates(); |
|
75 |
|
76 enum ParserState { |
|
77 PROTOCOL_STATE_CONTROL, |
|
78 PROTOCOL_STATE_CHUNK |
|
79 }; |
|
80 ParserState mState; |
|
81 |
|
82 enum ChunkType { |
|
83 // Types for shavar tables. |
|
84 CHUNK_ADD, |
|
85 CHUNK_SUB, |
|
86 // Types for digest256 tables. digest256 tables differ in format from |
|
87 // shavar tables since they only contain complete hashes. |
|
88 CHUNK_ADD_DIGEST, |
|
89 CHUNK_SUB_DIGEST |
|
90 }; |
|
91 |
|
92 struct ChunkState { |
|
93 ChunkType type; |
|
94 uint32_t num; |
|
95 uint32_t hashSize; |
|
96 uint32_t length; |
|
97 void Clear() { num = 0; hashSize = 0; length = 0; } |
|
98 }; |
|
99 ChunkState mChunkState; |
|
100 |
|
101 nsCOMPtr<nsICryptoHash> mCryptoHash; |
|
102 |
|
103 nsresult mUpdateStatus; |
|
104 nsCString mPending; |
|
105 |
|
106 uint32_t mUpdateWait; |
|
107 bool mResetRequested; |
|
108 |
|
109 nsTArray<ForwardedUpdate> mForwards; |
|
110 // Keep track of updates to apply before passing them to the DBServiceWorkers. |
|
111 nsTArray<TableUpdate*> mTableUpdates; |
|
112 // Updates to apply to the current table being parsed. |
|
113 TableUpdate *mTableUpdate; |
|
114 }; |
|
115 |
|
116 } |
|
117 } |
|
118 |
|
119 #endif |