status_t String16::insert(size_t pos, const char16_t* chrs, size_t len) { const size_t myLen = size(); if (myLen == 0) { return setTo(chrs, len); return NO_ERROR; } else if (len == 0) { return NO_ERROR; } if (pos > myLen) pos = myLen; #if 0 printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n", String8(*this).string(), pos, len, myLen, String8(chrs, len).string()); #endif SharedBuffer* buf = SharedBuffer::bufferFromData(mString) ->editResize((myLen+len+1)*sizeof(char16_t)); if (buf) { char16_t* str = (char16_t*)buf->data(); if (pos < myLen) { memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t)); } memcpy(str+pos, chrs, len*sizeof(char16_t)); str[myLen+len] = 0; mString = str; #if 0 printf("Result (%d chrs): %s\n", size(), String8(*this).string()); #endif return NO_ERROR; } return NO_MEMORY; }
static char16_t* allocFromUTF8(const char* u8str, size_t u8len) { if (u8len == 0) return getEmptyString(); const uint8_t* u8cur = (const uint8_t*) u8str; const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len); if (u16len < 0) { return getEmptyString(); } SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1)); if (buf) { u8cur = (const uint8_t*) u8str; char16_t* u16str = (char16_t*)buf->data(); utf8_to_utf16(u8cur, u8len, u16str); //printf("Created UTF-16 string from UTF-8 \"%s\":", in); //printHexData(1, str, buf->size(), 16, 1); //printf("\n"); return u16str; } return getEmptyString(); }
void DocumentLoader::substituteResourceDeliveryTimerFired(Timer<DocumentLoader>*) { if (m_pendingSubstituteResources.isEmpty()) return; ASSERT(m_frame && m_frame->page()); if (m_frame->page()->defersLoading()) return; SubstituteResourceMap copy; copy.swap(m_pendingSubstituteResources); SubstituteResourceMap::const_iterator end = copy.end(); for (SubstituteResourceMap::const_iterator it = copy.begin(); it != end; ++it) { RefPtr<ResourceLoader> loader = it->first; SubstituteResource* resource = it->second.get(); if (resource) { SharedBuffer* data = resource->data(); loader->didReceiveResponse(resource->response()); loader->didReceiveData(data->data(), data->size(), data->size(), true); loader->didFinishLoading(); } else { // A null resource means that we should fail the load. // FIXME: Maybe we should use another error here - something like "not in cache". loader->didFail(loader->cannotShowURLError()); } } }
SkData* ImageFrameGenerator::refEncodedData() { // SkData is returned only when full image (encoded) data is received. This is important // since DeferredImageDecoder::setData is called only once with allDataReceived set to true, // and after that m_data->m_readBuffer.data() is not changed. See also RELEASE_ASSERT used in // ThreadSafeDataTransport::data(). SharedBuffer* buffer = 0; bool allDataReceived = false; m_data->data(&buffer, &allDataReceived); if (!allDataReceived) return nullptr; { // Prevents concurrent access to m_encodedData creation. MutexLocker lock(m_decodeMutex); if (m_encodedData) { m_encodedData->ref(); return m_encodedData; } // m_encodedData is created with initial reference count == 1. ImageFrameGenerator always holds one // reference to m_encodedData, as it prevents write access in SkData::writable_data. m_encodedData = SkData::NewWithProc(buffer->data(), buffer->size(), sharedSkDataReleaseCallback, m_data.get()); // While m_encodedData is referenced, prevent disposing m_data and its content. // it is dereferenced in sharedSkDataReleaseCallback, called when m_encodedData gets dereferenced. m_data->ref(); } // Increase the reference, caller must decrease it. One reference is always kept by ImageFrameGenerator and released // in destructor. m_encodedData->ref(); return m_encodedData; }
void initialize_string16() { SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)); char16_t* str = (char16_t*)buf->data(); *str = 0; gEmptyStringBuf = buf; gEmptyString = str; }
size_t getBytesWithOffset(void *info, void* buffer, size_t offset, size_t count) { SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info); size_t availBytes = count; if (offset + count > sharedBuffer->size()) availBytes -= (offset + count) - sharedBuffer->size(); memcpy(buffer, sharedBuffer->data() + offset, availBytes); return availBytes; }
void* VectorImpl::editArrayImpl() { if (mStorage) { const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage); SharedBuffer* editable = sb->attemptEdit(); if (editable == 0) { // If we're here, we're not the only owner of the buffer. // We must make a copy of it. editable = SharedBuffer::alloc(sb->size()); // Fail instead of returning a pointer to storage that's not // editable. Otherwise we'd be editing the contents of a buffer // for which we're not the only owner, which is undefined behaviour. LOG_ALWAYS_FATAL_IF(editable == NULL); _do_copy(editable->data(), mStorage, mCount); release_storage(); mStorage = editable->data(); } } return mStorage; }
char* String8::lockBuffer(size_t size) { SharedBuffer* buf = SharedBuffer::bufferFromData(mString) ->editResize(size + 1); if (buf) { char* str = (char*)buf->data(); mString = str; return str; } return NULL; }
void* BasicHashtableImpl::allocateBuckets(size_t count) const { size_t bytes = count * mBucketSize; SharedBuffer* sb = SharedBuffer::alloc(bytes); LOG_ALWAYS_FATAL_IF(!sb, "Could not allocate %u bytes for hashtable with %u buckets.", uint32_t(bytes), uint32_t(count)); void* buckets = sb->data(); for (size_t i = 0; i < count; i++) { Bucket& bucket = bucketAt(buckets, i); bucket.cookie = 0; } return buckets; }
SharedBuffer* SharedBuffer::edit() const { if (onlyOwner()) { return const_cast<SharedBuffer*>(this); } SharedBuffer* sb = alloc(mSize); if (sb) { memcpy(sb->data(), data(), size()); release(); } return sb; }
size_t sharedBufferGetBytesAtPosition(void* info, void* buffer, off_t position, size_t count) { SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info); size_t sourceSize = sharedBuffer->size(); if (position >= sourceSize) return 0; const char* source = sharedBuffer->data() + position; size_t amount = min<size_t>(count, sourceSize - position); memcpy(buffer, source, amount); return amount; }
status_t String16::setTo(const char16_t* other, size_t len) { SharedBuffer* buf = SharedBuffer::bufferFromData(mString) ->editResize((len+1)*sizeof(char16_t)); if (buf) { char16_t* str = (char16_t*)buf->data(); memmove(str, other, len*sizeof(char16_t)); str[len] = 0; mString = str; return NO_ERROR; } return NO_MEMORY; }
void initialize_string8() { #ifdef LIBUTILS_NATIVE // Bite me, Darwin! gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects; #endif SharedBuffer* buf = SharedBuffer::alloc(1); char* str = (char*)buf->data(); *str = 0; gEmptyStringBuf = buf; gEmptyString = str; }
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer) { static FT_Library library; if (!library && FT_Init_FreeType(&library)) { library = nullptr; return nullptr; } FT_Face freeTypeFace; if (FT_New_Memory_Face(library, reinterpret_cast<const FT_Byte*>(buffer.data()), buffer.size(), 0, &freeTypeFace)) return nullptr; return std::make_unique<FontCustomPlatformData>(freeTypeFace, buffer); }
static bool canHandleImage(const SharedBuffer& _data) { // We need at least 4 bytes to figure out what kind of image we're dealing with. if (_data.size() < 4) return false; QByteArray data = QByteArray::fromRawData(_data.data(), _data.size()); QBuffer buffer(&data); if (!buffer.open(QBuffer::ReadOnly)) return false; return !QImageReader::imageFormat(&buffer).isEmpty(); }
String16::String16(const char16_t* o, size_t len) { SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t)); ALOG_ASSERT(buf, "Unable to allocate shared buffer"); if (buf) { char16_t* str = (char16_t*)buf->data(); memcpy(str, o, len*sizeof(char16_t)); str[len] = 0; mString = str; return; } mString = getEmptyString(); }
extern "C" void initialize_string8(){ // HACK: This dummy dependency forces linking libutils Static.cpp, // which is needed to initialize String8/String16 classes. // These variables are named for Darwin, but are needed elsewhere too, // including static linking on any platform. //gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects; if (!gEmptyStringBuf) { SharedBuffer* buf = SharedBuffer::alloc(1); char* str = (char*)buf->data(); *str = 0; gEmptyStringBuf = buf; gEmptyString = str; } }
String16::String16(const char16_t* o) { size_t len = strlen16(o); SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t)); ALOG_ASSERT(buf, "Unable to allocate shared buffer"); if (buf) { char16_t* str = (char16_t*)buf->data(); strcpy16(str, o); mString = str; return; } mString = getEmptyString(); }
status_t String8::unlockBuffer(size_t size) { if (size != this->size()) { SharedBuffer* buf = SharedBuffer::bufferFromData(mString) ->editResize(size+1); if (buf) { char* str = (char*)buf->data(); str[size] = 0; mString = str; return NO_ERROR; } } return NO_MEMORY; }
static char* allocFromUTF8(const char* in, size_t len) { if (len > 0) { SharedBuffer* buf = SharedBuffer::alloc(len+1); if (buf) { char* str = (char*)buf->data(); memcpy(str, in, len); str[len] = 0; return str; } return NULL; } return getEmptyString(); }
static xmlDocPtr docLoaderFunc(const xmlChar* uri, xmlDictPtr, int options, void* ctxt, xsltLoadType type) { if (!globalProcessor) return 0; switch (type) { case XSLT_LOAD_DOCUMENT: { xsltTransformContextPtr context = (xsltTransformContextPtr)ctxt; xmlChar* base = xmlNodeGetBase(context->document->doc, context->node); KURL url(KURL(ParsedURLString, reinterpret_cast<const char*>(base)), reinterpret_cast<const char*>(uri)); xmlFree(base); ResourceLoaderOptions fetchOptions(ResourceFetcher::defaultResourceOptions()); FetchRequest request(ResourceRequest(url), FetchInitiatorTypeNames::xml, fetchOptions); request.setOriginRestriction(FetchRequest::RestrictToSameOrigin); ResourcePtr<Resource> resource = globalResourceFetcher->fetchSynchronously(request); if (!resource || !globalProcessor) return 0; PageConsole* console = 0; Frame* frame = globalProcessor->xslStylesheet()->ownerDocument()->frame(); if (frame && frame->page()) console = &frame->page()->console(); xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc); xmlSetGenericErrorFunc(console, XSLTProcessor::genericErrorFunc); // We don't specify an encoding here. Neither Gecko nor WinIE respects // the encoding specified in the HTTP headers. SharedBuffer* data = resource->resourceBuffer(); xmlDocPtr doc = data ? xmlReadMemory(data->data(), data->size(), (const char*)uri, 0, options) : 0; xmlSetStructuredErrorFunc(0, 0); xmlSetGenericErrorFunc(0, 0); return doc; } case XSLT_LOAD_STYLESHEET: return globalProcessor->xslStylesheet()->locateStylesheetSubResource(((xsltStylesheetPtr)ctxt)->doc, uri); default: break; } return 0; }
status_t String8::real_append(const char* other, size_t otherLen) { const size_t myLen = bytes(); SharedBuffer* buf = SharedBuffer::bufferFromData(mString) ->editResize(myLen + otherLen + 1); if (buf) { char* str = (char*)buf->data(); mString = str; str += myLen; memcpy(str, other, otherLen); str[otherLen] = '\0'; return NO_ERROR; } return NO_MEMORY; }
void SynchronousNetworkLoaderClient::sendDelayedReply(NetworkResourceLoader& loader) { ASSERT(m_delayedReply); if (m_response.isNull()) { ASSERT(!m_error.isNull()); //platformSynthesizeErrorResponse(); } Vector<char> responseData; SharedBuffer* buffer = loader.bufferedData(); if (buffer && buffer->size()) responseData.append(buffer->data(), buffer->size()); m_delayedReply->send(m_error, m_response, responseData); m_delayedReply = nullptr; }
void CachedResource::tryReplaceEncodedData(SharedBuffer& newBuffer) { if (!m_data) return; if (!mayTryReplaceEncodedData()) return; // We have to do the memcmp because we can't tell if the replacement file backed data is for the // same resource or if we made a second request with the same URL which gave us a different // resource. We have seen this happen for cached POST resources. if (m_data->size() != newBuffer.size() || memcmp(m_data->data(), newBuffer.data(), m_data->size())) return; if (m_data->tryReplaceContentsWithPlatformBuffer(newBuffer)) didReplaceSharedBufferContents(); }
static void sharedSkDataReleaseCallback(const void* address, void* context) { // This gets called when m_encodedData reference count becomes 0 - and it could happen in // ImageFrameGenerator destructor or later when m_encodedData gets dereferenced. // In this method, we deref ThreadSafeDataTransport, as ThreadSafeDataTransport is the owner // of data returned via refEncodedData. ThreadSafeDataTransport* dataTransport = static_cast<ThreadSafeDataTransport*>(context); #if ENABLE(ASSERT) ASSERT(dataTransport); SharedBuffer* buffer = 0; bool allDataReceived = false; dataTransport->data(&buffer, &allDataReceived); ASSERT(allDataReceived && buffer && buffer->data() == address); #endif // Dereference m_data now. dataTransport->deref(); }
static char* allocFromUTF16(const char16_t* in, size_t len) { if (len == 0) return getEmptyString(); const ssize_t bytes = utf16_to_utf8_length(in, len); if (bytes < 0) { return getEmptyString(); } SharedBuffer* buf = SharedBuffer::alloc(bytes+1); if (!buf) { return getEmptyString(); } char* str = (char*)buf->data(); utf16_to_utf8(in, len, str); return str; }
static char* allocFromUTF16(const char16_t* in, size_t len) { if (len == 0) return getEmptyString(); const size_t bytes = utf8_length_from_utf16(in, len); SharedBuffer* buf = SharedBuffer::alloc(bytes+1); LOG_ASSERT(buf, "Unable to allocate shared buffer"); if (buf) { char* str = (char*)buf->data(); utf16_to_utf8(in, len, str, bytes+1); return str; } return getEmptyString(); }
PassRefPtr<API::Data> WebIconDatabase::iconDataForPageURL(const String& pageURL) { auto* image = imageForPageURL(pageURL); if (!image) return nullptr; SharedBuffer* sharedBuffer = image->data(); if (!sharedBuffer) return nullptr; // Balanced by deref() below. sharedBuffer->ref(); return API::Data::createWithoutCopying(reinterpret_cast<const unsigned char*>(sharedBuffer->data()), sharedBuffer->size(), [](unsigned char*, const void* untypedSharedBuffer) { // Balanced by ref() above. static_cast<SharedBuffer*>(const_cast<void*>(untypedSharedBuffer))->deref(); }, sharedBuffer); }
ssize_t VectorImpl::setCapacity(size_t new_capacity) { size_t current_capacity = capacity(); ssize_t amount = new_capacity - size(); if (amount <= 0) { // we can't reduce the capacity return current_capacity; } SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); if (sb) { void* array = sb->data(); _do_copy(array, mStorage, size()); release_storage(); mStorage = const_cast<void*>(array); } else { return NO_MEMORY; } return new_capacity; }
SharedBuffer* SharedBuffer::editResize(size_t newSize) const { if (onlyOwner()) { SharedBuffer* buf = const_cast<SharedBuffer*>(this); if (buf->mSize == newSize) return buf; buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize); if (buf != NULL) { buf->mSize = newSize; return buf; } } SharedBuffer* sb = alloc(newSize); if (sb) { const size_t mySize = mSize; memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize); release(); } return sb; }