1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/core/serialization.h"
16 :
17 : #include <windows.h> // NOLINT
18 : #include <dbghelp.h>
19 : #include <stdio.h>
20 : #include <algorithm>
21 :
22 : #include "base/time/time.h"
23 :
24 : namespace core {
25 :
26 E : FileOutStream::FileOutStream(FILE* file) : file_(file) {
27 E : DCHECK(file != NULL);
28 E : }
29 :
30 E : bool FileOutStream::Write(size_t length, const Byte* bytes) {
31 E : return fwrite(bytes, sizeof(Byte), length, file_) == length;
32 E : }
33 :
34 E : bool FileOutStream::Flush() {
35 E : ::fflush(file_);
36 E : return true;
37 E : }
38 :
39 E : FileInStream::FileInStream(FILE* file) : file_(file) {
40 E : DCHECK(file != NULL);
41 E : }
42 :
43 E : bool FileInStream::ReadImpl(size_t length, Byte* bytes, size_t* bytes_read) {
44 E : DCHECK(bytes != NULL);
45 E : DCHECK(bytes_read != NULL);
46 E : *bytes_read = ::fread(bytes, sizeof(Byte), length, file_);
47 :
48 E : if (*bytes_read == length)
49 E : return true;
50 :
51 : // If we didn't read the full number of bytes expected, figure out why. It's
52 : // not an error if we're at the end of the stream.
53 E : if (!::feof(file_))
54 i : return false;
55 :
56 E : return true;
57 E : }
58 :
59 : // Serialization of base::Time.
60 : // We serialize to 'number of seconds since epoch' (represented as a double)
61 : // as this is consistent regardless of the underlying representation used in
62 : // base::Time (which may vary wrt timer resolution).
63 :
64 E : bool Save(const base::Time& time, OutArchive* out_archive) {
65 E : DCHECK(out_archive != NULL);
66 E : return out_archive->Save(time.ToDoubleT());
67 E : }
68 :
69 E : bool Load(base::Time* time, InArchive* in_archive) {
70 E : DCHECK(in_archive != NULL);
71 : double t;
72 E : if (!in_archive->Load(&t))
73 i : return false;
74 E : *time = base::Time::FromDoubleT(t);
75 E : return true;
76 E : }
77 :
78 : // Serialization of OMAP, defined in dbghelp.h.
79 :
80 : bool Save(const OMAP& omap, OutArchive* out_archive) {
81 : DCHECK(out_archive != NULL);
82 : return out_archive->Save(omap.rva) &&
83 : out_archive->Save(omap.rvaTo);
84 : }
85 :
86 : bool Load(OMAP* omap, InArchive* in_archive) {
87 : DCHECK(omap != NULL);
88 : DCHECK(in_archive != NULL);
89 : return in_archive->Load(&omap->rva) &&
90 : in_archive->Load(&omap->rvaTo);
91 : }
92 :
93 : } // namespace core
|