diff options
author | tommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-15 19:07:35 +0000 |
---|---|---|
committer | tommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-15 19:09:03 +0000 |
commit | 8c83fe076ebe86c04e6d703be2bfb2f2e8729158 (patch) | |
tree | 268331ca7a39e75a7e9aeabd3d7cf2ca923156b3 /components/crx_file/crx_file.cc | |
parent | 41e4e2adfd63eadf7c2067654863ff558a9cd70f (diff) | |
download | chromium_src-8c83fe076ebe86c04e6d703be2bfb2f2e8729158.zip chromium_src-8c83fe076ebe86c04e6d703be2bfb2f2e8729158.tar.gz chromium_src-8c83fe076ebe86c04e6d703be2bfb2f2e8729158.tar.bz2 |
Componentize component_updater: Split crx_file code off into its own component.
CRX file code currently lives under extensions/. However,
component_updater also uses them to package components. The
CRX code should therefore live as its own component.
BUG=371463
TBR=blundell@chromium.org
Review URL: https://codereview.chromium.org/474633005
Cr-Commit-Position: refs/heads/master@{#289966}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289966 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/crx_file/crx_file.cc')
-rw-r--r-- | components/crx_file/crx_file.cc | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/components/crx_file/crx_file.cc b/components/crx_file/crx_file.cc new file mode 100644 index 0000000..391d549 --- /dev/null +++ b/components/crx_file/crx_file.cc @@ -0,0 +1,81 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/crx_file/crx_file.h" + +namespace crx_file { + +namespace { + +// The current version of the crx format. +static const uint32 kCurrentVersion = 2; + +// The current version of the crx diff format. +static const uint32 kCurrentDiffVersion = 0; + +// The maximum size the crx parser will tolerate for a public key. +static const uint32 kMaxPublicKeySize = 1 << 16; + +// The maximum size the crx parser will tolerate for a signature. +static const uint32 kMaxSignatureSize = 1 << 16; + +} // namespace + +// The magic string embedded in the header. +const char kCrxFileHeaderMagic[] = "Cr24"; +const char kCrxDiffFileHeaderMagic[] = "CrOD"; + +scoped_ptr<CrxFile> CrxFile::Parse(const CrxFile::Header& header, + CrxFile::Error* error) { + if (HeaderIsValid(header, error)) + return scoped_ptr<CrxFile>(new CrxFile(header)); + return scoped_ptr<CrxFile>(); +} + +scoped_ptr<CrxFile> CrxFile::Create(const uint32 key_size, + const uint32 signature_size, + CrxFile::Error* error) { + CrxFile::Header header; + memcpy(&header.magic, kCrxFileHeaderMagic, kCrxFileHeaderMagicSize); + header.version = kCurrentVersion; + header.key_size = key_size; + header.signature_size = signature_size; + if (HeaderIsValid(header, error)) + return scoped_ptr<CrxFile>(new CrxFile(header)); + return scoped_ptr<CrxFile>(); +} + +CrxFile::CrxFile(const Header& header) : header_(header) { +} + +bool CrxFile::HeaderIsDelta(const CrxFile::Header& header) { + return !strncmp(kCrxDiffFileHeaderMagic, header.magic, sizeof(header.magic)); +} + +bool CrxFile::HeaderIsValid(const CrxFile::Header& header, + CrxFile::Error* error) { + bool valid = false; + bool diffCrx = false; + if (!strncmp(kCrxDiffFileHeaderMagic, header.magic, sizeof(header.magic))) + diffCrx = true; + if (strncmp(kCrxFileHeaderMagic, header.magic, sizeof(header.magic)) && + !diffCrx) + *error = kWrongMagic; + else if (header.version != kCurrentVersion + && !(diffCrx && header.version == kCurrentDiffVersion)) + *error = kInvalidVersion; + else if (header.key_size > kMaxPublicKeySize) + *error = kInvalidKeyTooLarge; + else if (header.key_size == 0) + *error = kInvalidKeyTooSmall; + else if (header.signature_size > kMaxSignatureSize) + *error = kInvalidSignatureTooLarge; + else if (header.signature_size == 0) + *error = kInvalidSignatureTooSmall; + else + valid = true; + return valid; +} + +} // namespace crx_file |