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/web/linux/WebFontRenderStyle.h"
21 void GetFontFamilyForCharacters(const uint16_t* utf16
,
23 const char* preferred_locale
,
24 WebKit::WebFontFamily
* family
) {
26 request
.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS
);
27 request
.WriteInt(num_utf16
);
28 for (size_t i
= 0; i
< num_utf16
; ++i
)
29 request
.WriteUInt32(utf16
[i
]);
30 request
.WriteString(preferred_locale
);
33 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
34 sizeof(buf
), NULL
, request
);
36 std::string family_name
;
38 bool isItalic
= false;
40 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
41 PickleIterator
pickle_iter(reply
);
42 if (reply
.ReadString(&pickle_iter
, &family_name
) &&
43 reply
.ReadBool(&pickle_iter
, &isBold
) &&
44 reply
.ReadBool(&pickle_iter
, &isItalic
)) {
45 family
->name
= family_name
;
46 family
->isBold
= isBold
;
47 family
->isItalic
= isItalic
;
52 void GetRenderStyleForStrike(const char* family
, int sizeAndStyle
,
53 WebKit::WebFontRenderStyle
* out
) {
55 request
.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE
);
56 request
.WriteString(family
);
57 request
.WriteInt(sizeAndStyle
);
60 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
61 sizeof(buf
), NULL
, request
);
68 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
69 PickleIterator
pickle_iter(reply
);
70 int useBitmaps
, useAutoHint
, useHinting
, hintStyle
, useAntiAlias
;
71 int useSubpixelRendering
, useSubpixelPositioning
;
72 if (reply
.ReadInt(&pickle_iter
, &useBitmaps
) &&
73 reply
.ReadInt(&pickle_iter
, &useAutoHint
) &&
74 reply
.ReadInt(&pickle_iter
, &useHinting
) &&
75 reply
.ReadInt(&pickle_iter
, &hintStyle
) &&
76 reply
.ReadInt(&pickle_iter
, &useAntiAlias
) &&
77 reply
.ReadInt(&pickle_iter
, &useSubpixelRendering
) &&
78 reply
.ReadInt(&pickle_iter
, &useSubpixelPositioning
)) {
79 out
->useBitmaps
= useBitmaps
;
80 out
->useAutoHint
= useAutoHint
;
81 out
->useHinting
= useHinting
;
82 out
->hintStyle
= hintStyle
;
83 out
->useAntiAlias
= useAntiAlias
;
84 out
->useSubpixelRendering
= useSubpixelRendering
;
85 out
->useSubpixelPositioning
= useSubpixelPositioning
;
89 int MatchFontWithFallback(const std::string
& face
, bool bold
,
90 bool italic
, int charset
) {
92 request
.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK
);
93 request
.WriteString(face
);
94 request
.WriteBool(bold
);
95 request
.WriteBool(italic
);
96 request
.WriteUInt32(charset
);
97 uint8_t reply_buf
[64];
99 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf
, sizeof(reply_buf
),
104 bool GetFontTable(int fd
, uint32_t table_tag
, off_t offset
,
105 uint8_t* output
, size_t* output_length
) {
109 size_t data_length
= 0; // the length of the file data.
110 off_t data_offset
= 0; // the offset of the data in the file.
111 if (table_tag
== 0) {
112 // Get the entire font file.
114 if (fstat(fd
, &st
) < 0)
116 data_length
= base::checked_numeric_cast
<size_t>(st
.st_size
);
118 // Get a font table. Read the header to find its offset in the file.
120 ssize_t n
= HANDLE_EINTR(pread(fd
, &num_tables
, sizeof(num_tables
),
121 4 /* skip the font type */));
122 if (n
!= sizeof(num_tables
))
124 // Font data is stored in net (big-endian) order.
125 num_tables
= base::NetToHost16(num_tables
);
127 // Read the table directory.
128 static const size_t kTableEntrySize
= 16;
129 const size_t directory_size
= num_tables
* kTableEntrySize
;
130 scoped_ptr
<uint8_t[]> table_entries(new uint8_t[directory_size
]);
131 n
= HANDLE_EINTR(pread(fd
, table_entries
.get(), directory_size
,
132 12 /* skip the SFNT header */));
133 if (n
!= base::checked_numeric_cast
<ssize_t
>(directory_size
))
136 for (uint16_t i
= 0; i
< num_tables
; ++i
) {
137 uint8_t* entry
= table_entries
.get() + i
* kTableEntrySize
;
138 uint32_t tag
= *reinterpret_cast<uint32_t*>(entry
);
139 if (tag
== table_tag
) {
140 // Font data is stored in net (big-endian) order.
142 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry
+ 8));
144 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry
+ 12));
152 // Clamp |offset| inside the allowable range. This allows the read to succeed
153 // but return 0 bytes.
154 offset
= std::min(offset
, base::checked_numeric_cast
<off_t
>(data_length
));
155 // Make sure it's safe to add the data offset and the caller's logical offset.
156 // Define the maximum positive offset on 32 bit systems.
157 static const off_t kMaxPositiveOffset32
= 0x7FFFFFFF; // 2 GB - 1.
158 if ((offset
> kMaxPositiveOffset32
/ 2) ||
159 (data_offset
> kMaxPositiveOffset32
/ 2))
161 data_offset
+= offset
;
162 data_length
-= offset
;
165 // 'output_length' holds the maximum amount of data the caller can accept.
166 data_length
= std::min(data_length
, *output_length
);
167 ssize_t n
= HANDLE_EINTR(pread(fd
, output
, data_length
, data_offset
));
168 if (n
!= base::checked_numeric_cast
<ssize_t
>(data_length
))
171 *output_length
= data_length
;
176 } // namespace content