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"
11 #include "base/basictypes.h"
12 #include "base/debug/trace_event.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/pickle.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/posix/unix_domain_socket_linux.h"
18 #include "base/sys_byteorder.h"
19 #include "content/common/sandbox_linux/sandbox_linux.h"
20 #include "third_party/WebKit/public/platform/linux/WebFallbackFont.h"
21 #include "third_party/WebKit/public/platform/linux/WebFontRenderStyle.h"
25 void GetFallbackFontForCharacter(int32_t character
,
26 const char* preferred_locale
,
27 blink::WebFallbackFont
* fallbackFont
) {
28 TRACE_EVENT0("sandbox_ipc", "GetFontFamilyForCharacter");
31 request
.WriteInt(LinuxSandbox::METHOD_GET_FALLBACK_FONT_FOR_CHAR
);
32 request
.WriteInt(character
);
33 request
.WriteString(preferred_locale
);
36 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
37 sizeof(buf
), NULL
, request
);
39 std::string family_name
;
41 int fontconfigInterfaceId
= 0;
44 bool isItalic
= false;
46 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
47 PickleIterator
pickle_iter(reply
);
48 if (reply
.ReadString(&pickle_iter
, &family_name
) &&
49 reply
.ReadString(&pickle_iter
, &filename
) &&
50 reply
.ReadInt(&pickle_iter
, &fontconfigInterfaceId
) &&
51 reply
.ReadInt(&pickle_iter
, &ttcIndex
) &&
52 reply
.ReadBool(&pickle_iter
, &isBold
) &&
53 reply
.ReadBool(&pickle_iter
, &isItalic
)) {
54 fallbackFont
->name
= family_name
;
55 fallbackFont
->filename
= filename
;
56 fallbackFont
->fontconfigInterfaceId
= fontconfigInterfaceId
;
57 fallbackFont
->ttcIndex
= ttcIndex
;
58 fallbackFont
->isBold
= isBold
;
59 fallbackFont
->isItalic
= isItalic
;
64 void GetRenderStyleForStrike(const char* family
,
66 blink::WebFontRenderStyle
* out
) {
67 TRACE_EVENT0("sandbox_ipc", "GetRenderStyleForStrike");
71 if (size_and_style
< 0)
74 const bool bold
= size_and_style
& 1;
75 const bool italic
= size_and_style
& 2;
76 const int pixel_size
= size_and_style
>> 2;
77 if (pixel_size
> std::numeric_limits
<uint16
>::max())
81 request
.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE
);
82 request
.WriteString(family
);
83 request
.WriteBool(bold
);
84 request
.WriteBool(italic
);
85 request
.WriteUInt16(pixel_size
);
88 const ssize_t n
= UnixDomainSocket::SendRecvMsg(GetSandboxFD(), buf
,
89 sizeof(buf
), NULL
, request
);
93 Pickle
reply(reinterpret_cast<char*>(buf
), n
);
94 PickleIterator
pickle_iter(reply
);
95 int use_bitmaps
, use_autohint
, use_hinting
, hint_style
, use_antialias
;
96 int use_subpixel_rendering
, use_subpixel_positioning
;
97 if (reply
.ReadInt(&pickle_iter
, &use_bitmaps
) &&
98 reply
.ReadInt(&pickle_iter
, &use_autohint
) &&
99 reply
.ReadInt(&pickle_iter
, &use_hinting
) &&
100 reply
.ReadInt(&pickle_iter
, &hint_style
) &&
101 reply
.ReadInt(&pickle_iter
, &use_antialias
) &&
102 reply
.ReadInt(&pickle_iter
, &use_subpixel_rendering
) &&
103 reply
.ReadInt(&pickle_iter
, &use_subpixel_positioning
)) {
104 out
->useBitmaps
= use_bitmaps
;
105 out
->useAutoHint
= use_autohint
;
106 out
->useHinting
= use_hinting
;
107 out
->hintStyle
= hint_style
;
108 out
->useAntiAlias
= use_antialias
;
109 out
->useSubpixelRendering
= use_subpixel_rendering
;
110 out
->useSubpixelPositioning
= use_subpixel_positioning
;
114 int MatchFontWithFallback(const std::string
& face
,
118 PP_BrowserFont_Trusted_Family fallback_family
) {
119 TRACE_EVENT0("sandbox_ipc", "MatchFontWithFallback");
122 request
.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK
);
123 request
.WriteString(face
);
124 request
.WriteBool(bold
);
125 request
.WriteBool(italic
);
126 request
.WriteUInt32(charset
);
127 request
.WriteUInt32(fallback_family
);
128 uint8_t reply_buf
[64];
130 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf
, sizeof(reply_buf
),
135 bool GetFontTable(int fd
, uint32_t table_tag
, off_t offset
,
136 uint8_t* output
, size_t* output_length
) {
140 size_t data_length
= 0; // the length of the file data.
141 off_t data_offset
= 0; // the offset of the data in the file.
142 if (table_tag
== 0) {
143 // Get the entire font file.
145 if (fstat(fd
, &st
) < 0)
147 data_length
= base::checked_cast
<size_t>(st
.st_size
);
149 // Get a font table. Read the header to find its offset in the file.
151 ssize_t n
= HANDLE_EINTR(pread(fd
, &num_tables
, sizeof(num_tables
),
152 4 /* skip the font type */));
153 if (n
!= sizeof(num_tables
))
155 // Font data is stored in net (big-endian) order.
156 num_tables
= base::NetToHost16(num_tables
);
158 // Read the table directory.
159 static const size_t kTableEntrySize
= 16;
160 const size_t directory_size
= num_tables
* kTableEntrySize
;
161 scoped_ptr
<uint8_t[]> table_entries(new uint8_t[directory_size
]);
162 n
= HANDLE_EINTR(pread(fd
, table_entries
.get(), directory_size
,
163 12 /* skip the SFNT header */));
164 if (n
!= base::checked_cast
<ssize_t
>(directory_size
))
167 for (uint16_t i
= 0; i
< num_tables
; ++i
) {
168 uint8_t* entry
= table_entries
.get() + i
* kTableEntrySize
;
169 uint32_t tag
= *reinterpret_cast<uint32_t*>(entry
);
170 if (tag
== table_tag
) {
171 // Font data is stored in net (big-endian) order.
173 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry
+ 8));
175 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry
+ 12));
183 // Clamp |offset| inside the allowable range. This allows the read to succeed
184 // but return 0 bytes.
185 offset
= std::min(offset
, base::checked_cast
<off_t
>(data_length
));
186 // Make sure it's safe to add the data offset and the caller's logical offset.
187 // Define the maximum positive offset on 32 bit systems.
188 static const off_t kMaxPositiveOffset32
= 0x7FFFFFFF; // 2 GB - 1.
189 if ((offset
> kMaxPositiveOffset32
/ 2) ||
190 (data_offset
> kMaxPositiveOffset32
/ 2))
192 data_offset
+= offset
;
193 data_length
-= offset
;
196 // 'output_length' holds the maximum amount of data the caller can accept.
197 data_length
= std::min(data_length
, *output_length
);
198 ssize_t n
= HANDLE_EINTR(pread(fd
, output
, data_length
, data_offset
));
199 if (n
!= base::checked_cast
<ssize_t
>(data_length
))
202 *output_length
= data_length
;
207 } // namespace content