2010-10-24 18:41:21 +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.
|
|
|
|
|
2011-12-23 18:36:30 +01:00
|
|
|
#include "include/wrapper/cef_byte_read_handler.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include <stdlib.h>
|
2011-09-27 17:02:08 +02:00
|
|
|
#include "libcef_dll/cef_logging.h"
|
2010-10-24 18:41:21 +02:00
|
|
|
|
|
|
|
CefByteReadHandler::CefByteReadHandler(const unsigned char* bytes, size_t size,
|
|
|
|
CefRefPtr<CefBase> source)
|
2012-01-10 00:46:23 +01:00
|
|
|
: bytes_(bytes), size_(size), offset_(0), source_(source) {
|
2010-10-24 18:41:21 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefByteReadHandler::Read(void* ptr, size_t size, size_t n) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-24 18:41:21 +02:00
|
|
|
size_t s = (size_ - offset_) / size;
|
2010-11-15 16:39:56 +01:00
|
|
|
size_t ret = std::min(n, s);
|
2010-10-24 18:41:21 +02:00
|
|
|
memcpy(ptr, bytes_ + offset_, ret * size);
|
|
|
|
offset_ += ret * size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefByteReadHandler::Seek(int64 offset, int whence) {
|
2010-10-24 18:41:21 +02:00
|
|
|
int rv = -1L;
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
switch (whence) {
|
2010-10-24 18:41:21 +02:00
|
|
|
case SEEK_CUR:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset_ + offset > size_ || offset_ + offset < 0)
|
2010-10-24 18:41:21 +02:00
|
|
|
break;
|
|
|
|
offset_ += offset;
|
2010-11-17 23:59:46 +01:00
|
|
|
rv = 0;
|
2010-10-24 18:41:21 +02:00
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
case SEEK_END: {
|
|
|
|
int64 offset_abs = abs(offset);
|
|
|
|
if (offset_abs > size_)
|
2010-10-24 18:41:21 +02:00
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
offset_ = size_ - offset_abs;
|
2010-11-17 23:59:46 +01:00
|
|
|
rv = 0;
|
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
}
|
2010-10-24 18:41:21 +02:00
|
|
|
case SEEK_SET:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset > size_ || offset < 0)
|
2010-10-24 18:41:21 +02:00
|
|
|
break;
|
|
|
|
offset_ = offset;
|
2010-11-17 23:59:46 +01:00
|
|
|
rv = 0;
|
2010-10-24 18:41:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int64 CefByteReadHandler::Tell() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return offset_;
|
2010-10-24 18:41:21 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefByteReadHandler::Eof() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return (offset_ >= size_);
|
2010-10-24 18:41:21 +02:00
|
|
|
}
|