2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "include/wrapper/cef_byte_read_handler.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
|
2014-01-14 03:45:56 +01:00
|
|
|
#include <algorithm>
|
2014-05-22 23:01:22 +02:00
|
|
|
#include <cstdio>
|
2014-01-14 03:45:56 +01:00
|
|
|
#include <cstdlib>
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefByteReadHandler::CefByteReadHandler(const unsigned char* bytes,
|
|
|
|
size_t size,
|
2017-02-09 23:07:43 +01:00
|
|
|
CefRefPtr<CefBaseRefCounted> source)
|
2017-05-17 11:29:28 +02:00
|
|
|
: bytes_(bytes), size_(size), offset_(0), source_(source) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
size_t CefByteReadHandler::Read(void* ptr, size_t size, size_t n) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-09-14 23:51:50 +02:00
|
|
|
size_t s = static_cast<size_t>(size_ - offset_) / size;
|
2012-04-03 03:34:16 +02:00
|
|
|
size_t ret = std::min(n, s);
|
|
|
|
memcpy(ptr, bytes_ + offset_, ret * size);
|
|
|
|
offset_ += ret * size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefByteReadHandler::Seek(int64 offset, int whence) {
|
|
|
|
int rv = -1L;
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
switch (whence) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case SEEK_CUR:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (offset_ + offset > size_ || offset_ + offset < 0) {
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
2017-05-17 11:29:28 +02:00
|
|
|
case SEEK_END: {
|
2012-09-19 17:43:33 +02:00
|
|
|
#if defined(OS_WIN)
|
2017-05-17 11:29:28 +02:00
|
|
|
int64 offset_abs = _abs64(offset);
|
2012-09-19 17:43:33 +02:00
|
|
|
#else
|
2017-05-17 11:29:28 +02:00
|
|
|
int64 offset_abs = std::abs(offset);
|
2012-09-19 17:43:33 +02:00
|
|
|
#endif
|
2023-01-02 23:59:03 +01:00
|
|
|
if (offset_abs > size_) {
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
offset_ = size_ - offset_abs;
|
|
|
|
rv = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
2017-05-17 11:29:28 +02:00
|
|
|
}
|
|
|
|
case SEEK_SET:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (offset > size_ || offset < 0) {
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 CefByteReadHandler::Tell() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefByteReadHandler::Eof() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return (offset_ >= size_);
|
|
|
|
}
|