1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/child_process_sandbox_support_impl_linux.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/pickle.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/posix/unix_domain_socket_linux.h"
13 #include "base/safe_numerics.h"
14 #include "base/sys_byteorder.h"
15 #include "content/common/sandbox_linux.h"
16 #include "third_party/WebKit/public/platform/linux/WebFontFamily.h"
17 #include "third_party/WebKit/public/platform/linux/WebFontRenderStyle.h"
21 void GetFontFamilyForCharacter(int32_t character
,
22 const char* preferred_locale
,
23 WebKit::WebFontFamily
* family
) {
25 request
.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHAR
);
26 request
.WriteInt(character
);
27 request
.WriteString(preferred_locale
);
30 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
31 sizeof(buf
), NULL
, request
);
33 std::string family_name
;
35 bool isItalic
= false;
37 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
38 PickleIterator
pickle_iter(reply
);
39 if (reply
.ReadString(&pickle_iter
, &family_name
) &&
40 reply
.ReadBool(&pickle_iter
, &isBold
) &&
41 reply
.ReadBool(&pickle_iter
, &isItalic
)) {
42 family
->name
= family_name
;
43 family
->isBold
= isBold
;
44 family
->isItalic
= isItalic
;
49 void GetRenderStyleForStrike(const char* family
, int sizeAndStyle
,
50 WebKit::WebFontRenderStyle
* out
) {
52 request
.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE
);
53 request
.WriteString(family
);
54 request
.WriteInt(sizeAndStyle
);
57 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
58 sizeof(buf
), NULL
, request
);
65 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
66 PickleIterator
pickle_iter(reply
);
67 int useBitmaps
, useAutoHint
, useHinting
, hintStyle
, useAntiAlias
;
68 int useSubpixelRendering
, useSubpixelPositioning
;
69 if (reply
.ReadInt(&pickle_iter
, &useBitmaps
) &&
70 reply
.ReadInt(&pickle_iter
, &useAutoHint
) &&
71 reply
.ReadInt(&pickle_iter
, &useHinting
) &&
72 reply
.ReadInt(&pickle_iter
, &hintStyle
) &&
73 reply
.ReadInt(&pickle_iter
, &useAntiAlias
) &&
74 reply
.ReadInt(&pickle_iter
, &useSubpixelRendering
) &&
75 reply
.ReadInt(&pickle_iter
, &useSubpixelPositioning
)) {
76 out
->useBitmaps
= useBitmaps
;
77 out
->useAutoHint
= useAutoHint
;
78 out
->useHinting
= useHinting
;
79 out
->hintStyle
= hintStyle
;
80 out
->useAntiAlias
= useAntiAlias
;
81 out
->useSubpixelRendering
= useSubpixelRendering
;
82 out
->useSubpixelPositioning
= useSubpixelPositioning
;
86 int MatchFontWithFallback(const std::string
& face
, bool bold
,
87 bool italic
, int charset
) {
89 request
.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK
);
90 request
.WriteString(face
);
91 request
.WriteBool(bold
);
92 request
.WriteBool(italic
);
93 request
.WriteUInt32(charset
);
94 uint8_t reply_buf
[64];
96 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf
, sizeof(reply_buf
),
101 bool GetFontTable(int fd
, uint32_t table_tag
, off_t offset
,
102 uint8_t* output
, size_t* output_length
) {
106 size_t data_length
= 0; // the length of the file data.
107 off_t data_offset
= 0; // the offset of the data in the file.
108 if (table_tag
== 0) {
109 // Get the entire font file.
111 if (fstat(fd
, &st
) < 0)
113 data_length
= base::checked_numeric_cast
<size_t>(st
.st_size
);
115 // Get a font table. Read the header to find its offset in the file.
117 ssize_t n
= HANDLE_EINTR(pread(fd
, &num_tables
, sizeof(num_tables
),
118 4 /* skip the font type */));
119 if (n
!= sizeof(num_tables
))
121 // Font data is stored in net (big-endian) order.
122 num_tables
= base::NetToHost16(num_tables
);
124 // Read the table directory.
125 static const size_t kTableEntrySize
= 16;
126 const size_t directory_size
= num_tables
* kTableEntrySize
;
127 scoped_ptr
<uint8_t[]> table_entries(new uint8_t[directory_size
]);
128 n
= HANDLE_EINTR(pread(fd
, table_entries
.get(), directory_size
,
129 12 /* skip the SFNT header */));
130 if (n
!= base::checked_numeric_cast
<ssize_t
>(directory_size
))
133 for (uint16_t i
= 0; i
< num_tables
; ++i
) {
134 uint8_t* entry
= table_entries
.get() + i
* kTableEntrySize
;
135 uint32_t tag
= *reinterpret_cast<uint32_t*>(entry
);
136 if (tag
== table_tag
) {
137 // Font data is stored in net (big-endian) order.
139 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry
+ 8));
141 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry
+ 12));
149 // Clamp |offset| inside the allowable range. This allows the read to succeed
150 // but return 0 bytes.
151 offset
= std::min(offset
, base::checked_numeric_cast
<off_t
>(data_length
));
152 // Make sure it's safe to add the data offset and the caller's logical offset.
153 // Define the maximum positive offset on 32 bit systems.
154 static const off_t kMaxPositiveOffset32
= 0x7FFFFFFF; // 2 GB - 1.
155 if ((offset
> kMaxPositiveOffset32
/ 2) ||
156 (data_offset
> kMaxPositiveOffset32
/ 2))
158 data_offset
+= offset
;
159 data_length
-= offset
;
162 // 'output_length' holds the maximum amount of data the caller can accept.
163 data_length
= std::min(data_length
, *output_length
);
164 ssize_t n
= HANDLE_EINTR(pread(fd
, output
, data_length
, data_offset
));
165 if (n
!= base::checked_numeric_cast
<ssize_t
>(data_length
))
168 *output_length
= data_length
;
173 } // namespace content