1 : // Copyright 2012 Google Inc.
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 : #include "base/time.h"
17 : #include <algorithm>
18 : #include <dbghelp.h>
19 : #include <stdio.h>
20 :
21 : namespace core {
22 :
23 E : FileOutStream::FileOutStream(FILE* file) : file_(file) {
24 E : DCHECK(file != NULL);
25 E : }
26 :
27 E : bool FileOutStream::Write(size_t length, const Byte* bytes) {
28 E : return fwrite(bytes, sizeof(Byte), length, file_) == length;
29 E : }
30 :
31 E : bool FileOutStream::Flush() {
32 E : ::fflush(file_);
33 E : return true;
34 E : }
35 :
36 E : FileInStream::FileInStream(FILE* file) : file_(file) {
37 E : DCHECK(file != NULL);
38 E : }
39 :
40 E : bool FileInStream::ReadImpl(size_t length, Byte* bytes, size_t* bytes_read) {
41 E : DCHECK(bytes != NULL);
42 E : DCHECK(bytes_read != NULL);
43 E : *bytes_read = ::fread(bytes, sizeof(Byte), length, file_);
44 :
45 E : if (*bytes_read == length)
46 E : return true;
47 :
48 : // If we didn't read the full number of bytes expected, figure out why. It's
49 : // not an error if we're at the end of the stream.
50 E : if (!::feof(file_))
51 i : return false;
52 :
53 E : return true;
54 E : }
55 :
56 : // Serialization of base::Time.
57 : // We serialize to 'number of seconds since epoch' (represented as a double)
58 : // as this is consistent regardless of the underlying representation used in
59 : // base::Time (which may vary wrt timer resolution).
60 :
61 E : bool Save(const base::Time& time, OutArchive* out_archive) {
62 E : DCHECK(out_archive != NULL);
63 E : return out_archive->Save(time.ToDoubleT());
64 E : }
65 :
66 E : bool Load(base::Time* time, InArchive* in_archive) {
67 E : DCHECK(in_archive != NULL);
68 : double t;
69 E : if (!in_archive->Load(&t))
70 i : return false;
71 E : *time = base::Time::FromDoubleT(t);
72 E : return true;
73 E : }
74 :
75 : // Serialization of OMAP, defined in dbghelp.h.
76 :
77 : bool Save(const OMAP& omap, OutArchive* out_archive) {
78 : DCHECK(out_archive != NULL);
79 : return out_archive->Save(omap.rva) &&
80 : out_archive->Save(omap.rvaTo);
81 : }
82 :
83 : bool Load(OMAP* omap, InArchive* in_archive) {
84 : DCHECK(omap != NULL);
85 : DCHECK(in_archive != NULL);
86 : return in_archive->Load(&omap->rva) &&
87 : in_archive->Load(&omap->rvaTo);
88 : }
89 :
90 : } // namespace core
|