1 // Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "include/wrapper/cef_byte_read_handler.h"
6
7 #include <algorithm>
8 #include <cstdio>
9 #include <cstdlib>
10
CefByteReadHandler(const unsigned char * bytes,size_t size,CefRefPtr<CefBaseRefCounted> source)11 CefByteReadHandler::CefByteReadHandler(const unsigned char* bytes,
12 size_t size,
13 CefRefPtr<CefBaseRefCounted> source)
14 : bytes_(bytes), size_(size), offset_(0), source_(source) {}
15
Read(void * ptr,size_t size,size_t n)16 size_t CefByteReadHandler::Read(void* ptr, size_t size, size_t n) {
17 base::AutoLock lock_scope(lock_);
18 size_t s = static_cast<size_t>(size_ - offset_) / size;
19 size_t ret = std::min(n, s);
20 memcpy(ptr, bytes_ + offset_, ret * size);
21 offset_ += ret * size;
22 return ret;
23 }
24
Seek(int64 offset,int whence)25 int CefByteReadHandler::Seek(int64 offset, int whence) {
26 int rv = -1L;
27 base::AutoLock lock_scope(lock_);
28 switch (whence) {
29 case SEEK_CUR:
30 if (offset_ + offset > size_ || offset_ + offset < 0)
31 break;
32 offset_ += offset;
33 rv = 0;
34 break;
35 case SEEK_END: {
36 #if defined(OS_WIN)
37 int64 offset_abs = _abs64(offset);
38 #else
39 int64 offset_abs = std::abs(offset);
40 #endif
41 if (offset_abs > size_)
42 break;
43 offset_ = size_ - offset_abs;
44 rv = 0;
45 break;
46 }
47 case SEEK_SET:
48 if (offset > size_ || offset < 0)
49 break;
50 offset_ = offset;
51 rv = 0;
52 break;
53 }
54
55 return rv;
56 }
57
Tell()58 int64 CefByteReadHandler::Tell() {
59 base::AutoLock lock_scope(lock_);
60 return offset_;
61 }
62
Eof()63 int CefByteReadHandler::Eof() {
64 base::AutoLock lock_scope(lock_);
65 return (offset_ >= size_);
66 }
67