File: fingerprint_v8_snapshot.py

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (86 lines) | stat: -rwxr-xr-x 2,442 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
#
# Copyright 2015 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.

"""Fingerprints the V8 snapshot blob files.

Constructs a SHA256 fingerprint of the V8 natives and snapshot blob files and
creates a .cc file which includes these fingerprint as variables.
"""

import hashlib
import optparse
import os
import sys

_HEADER = """// Copyright 2015 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.

// This file was generated by fingerprint_v8_snapshot.py.

namespace gin {
"""

_FOOTER = """
}  // namespace gin
"""


def FingerprintFile(file_path):
  input_file = open(file_path, 'rb')
  sha256 = hashlib.sha256()
  while True:
    block = input_file.read(sha256.block_size)
    if not block:
      break
    sha256.update(block)
  return sha256.digest()


def WriteFingerprint(output_file, variable_name, fingerprint):
  output_file.write('\nextern const unsigned char %s[] = { ' % variable_name)
  for byte in fingerprint:
    output_file.write(str(ord(byte)) + ', ')
  output_file.write('};\n')


def WriteOutputFile(natives_fingerprint,
                    snapshot_fingerprint,
                    output_file_path):
  output_dir_path = os.path.dirname(output_file_path)
  if not os.path.exists(output_dir_path):
    os.makedirs(output_dir_path)
  output_file = open(output_file_path, 'w')

  output_file.write(_HEADER)
  WriteFingerprint(output_file, 'g_natives_fingerprint', natives_fingerprint)
  output_file.write('\n')
  WriteFingerprint(output_file, 'g_snapshot_fingerprint', snapshot_fingerprint)
  output_file.write(_FOOTER)


def main():
  parser = optparse.OptionParser()

  parser.add_option('--snapshot_file',
                    help='The input V8 snapshot blob file path.')
  parser.add_option('--natives_file',
                    help='The input V8 natives blob file path.')
  parser.add_option('--output_file',
                    help='The path for the output cc file which will be write.')

  options, _ = parser.parse_args()

  natives_fingerprint = FingerprintFile(options.natives_file)
  snapshot_fingerprint = FingerprintFile(options.snapshot_file)
  WriteOutputFile(
      natives_fingerprint, snapshot_fingerprint, options.output_file)

  return 0


if __name__ == '__main__':
  sys.exit(main())