/*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ static const char rcsid[] = "$Id: mar_user_c.c,v 1.6 2004/12/14 13:09:23 marayl Exp $"; #include "mar_user_c.h" #include "mar_defs_c.h" #include "mar_format_c.h" #include "mar_info_c.h" #include #if !defined(WIN32) #include #else /* WIN32 */ #include "mar_windows_c.h" #endif /* WIN32 */ static void* mar_resize(mar_seq_t seq, size_t isize, size_t size, int trunct) { void* addr; struct mar_info info; if (-1 == mar_info_(seq, isize, &info)) return NULL; if (-1 == mar_setregion_(seq, MAR_USER(info.isize_), MAR_USER_SIZE(info.usize_))) return NULL; if (info.usize_ == size || (info.usize_ >= size && !trunct)) return (mar_byte_t*)mar_seqaddr_(seq) + MAR_UDATA_OFFSET; addr = mar_resizeseq_(seq, MAR_USER_SIZE(size)); if (!addr) return NULL; if (info.usize_ < size) bzero((mar_byte_t*)addr + MAR_UDATA_OFFSET + info.usize_, size - info.usize_); mar_encode32_((mar_byte_t*)addr + MAR_USIZE_OFFSET, size); return (mar_byte_t*)addr + MAR_UDATA_OFFSET; } MAR_EXTERN ssize_t mar_read_(mar_seq_t seq, size_t isize, size_t offset, void* data, size_t size) { const void* addr; size_t local; if (0 == size) return 0; addr = mar_user_(seq, isize, &local); if (!addr) return -1; local -= offset; if (0 >= local) return 0; if (size > local) size = local; memcpy(data, (const mar_byte_t*)addr + offset, size); return size; } MAR_EXTERN int mar_setuser_(mar_seq_t seq, size_t isize, const void* data, size_t size) { void* addr = mar_resize(seq, isize, size, 1); if (!addr) return -1; memcpy(addr, data, size); return 0; } MAR_EXTERN int mar_truncate_(mar_seq_t seq, size_t isize, size_t size) { return mar_resize(seq, isize, size, 1) ? 0 : -1; } MAR_EXTERN ssize_t mar_write_(mar_seq_t seq, size_t isize, size_t offset, const void* data, size_t size) { mar_byte_t* addr = (mar_byte_t*)mar_resize(seq, isize, offset + size, 0); if (!addr) return -1; memcpy(addr + offset, data, size); return size; } MAR_EXTERN const void* mar_user_(mar_seq_t seq, size_t isize, size_t* size) { struct mar_info info; if (-1 == mar_info_(seq, isize, &info)) return NULL; if (-1 == mar_setregion_(seq, MAR_UDATA(info.isize_), info.usize_)) return NULL; if (size) *size = info.usize_; return (mar_byte_t*)mar_seqaddr_(seq); } MAR_EXTERN int mar_usersize_(mar_seq_t seq, size_t isize, size_t* size) { struct mar_info info; if (-1 == mar_info_(seq, isize, &info)) return -1; *size = info.usize_; return 0; }