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