OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <string.h> |
| 6 |
5 #include "third_party/hunspell/google/bdict.h" | 7 #include "third_party/hunspell/google/bdict.h" |
6 | 8 |
7 // static | 9 // static |
8 bool hunspell::BDict::Verify(const char* bdict_data, size_t bdict_length) { | 10 bool hunspell::BDict::Verify(const char* bdict_data, size_t bdict_length) { |
9 if (bdict_length <= sizeof(hunspell::BDict::Header)) | 11 if (bdict_length <= sizeof(hunspell::BDict::Header)) |
10 return false; | 12 return false; |
11 | 13 |
12 const BDict::Header* header = | 14 const BDict::Header* header = |
13 reinterpret_cast<const hunspell::BDict::Header*>(bdict_data); | 15 reinterpret_cast<const hunspell::BDict::Header*>(bdict_data); |
14 if (header->signature != hunspell::BDict::SIGNATURE || | 16 if (header->signature != hunspell::BDict::SIGNATURE || |
15 header->major_version > hunspell::BDict::MAJOR_VERSION || | 17 header->major_version > hunspell::BDict::MAJOR_VERSION || |
16 header->dic_offset > bdict_length) | 18 header->dic_offset > bdict_length) |
17 return false; | 19 return false; |
18 | 20 |
19 // Get the affix header, make sure there is enough room for it. | 21 // Get the affix header, make sure there is enough room for it. |
20 if (header->aff_offset + sizeof(hunspell::BDict::AffHeader) > bdict_length) | 22 if (header->aff_offset + sizeof(hunspell::BDict::AffHeader) > bdict_length) |
21 return false; | 23 return false; |
22 | 24 |
23 // Make sure there is enough room for the affix group count dword. | 25 // Make sure there is enough room for the affix group count dword. |
24 const hunspell::BDict::AffHeader* aff_header = | 26 const hunspell::BDict::AffHeader* aff_header = |
25 reinterpret_cast<const hunspell::BDict::AffHeader*>( | 27 reinterpret_cast<const hunspell::BDict::AffHeader*>( |
26 &bdict_data[header->aff_offset]); | 28 &bdict_data[header->aff_offset]); |
27 if (aff_header->affix_group_offset + sizeof(uint32) > bdict_length) | 29 if (aff_header->affix_group_offset + sizeof(uint32_t) > bdict_length) |
28 return false; | 30 return false; |
29 | 31 |
30 // The new BDICT header has a MD5 digest of the dictionary data. Compare the | 32 // The new BDICT header has a MD5 digest of the dictionary data. Compare the |
31 // MD5 digest of the data with the one in the BDICT header. | 33 // MD5 digest of the data with the one in the BDICT header. |
32 if (header->major_version >= 2) { | 34 if (header->major_version >= 2) { |
33 base::MD5Digest digest; | 35 base::MD5Digest digest; |
34 base::MD5Sum(aff_header, bdict_length - header->aff_offset, &digest); | 36 base::MD5Sum(aff_header, bdict_length - header->aff_offset, &digest); |
35 if (memcmp(&digest, &header->digest, sizeof(digest))) | 37 if (memcmp(&digest, &header->digest, sizeof(digest))) |
36 return false; | 38 return false; |
37 } | 39 } |
38 | 40 |
39 return true; | 41 return true; |
40 } | 42 } |
OLD | NEW |