diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2006-12-31 11:02:58 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2006-12-31 11:02:58 +0100 |
commit | 7783f518f63f885808639374091ea5a67038c935 (patch) | |
tree | f9f464ff1d05f577afa7cbb3563217c292a0ef23 /nslcd-common.h | |
parent | 20683054f699ca3dec7be6f847255819c42a5966 (diff) |
extract some more common macros
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@208 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd-common.h')
-rw-r--r-- | nslcd-common.h | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/nslcd-common.h b/nslcd-common.h index ee2e1d8..6150075 100644 --- a/nslcd-common.h +++ b/nslcd-common.h @@ -159,6 +159,24 @@ static void debug_dump(const void *ptr,size_t size) #define BUF_SKIP(sz) \ bufptr+=(size_t)(sz); +/* move BUF_CUR foreward so that it is aligned to the specified + type width */ +#define BUF_ALIGN(fp,type) \ + /* figure out number of bytes to skip foreward */ \ + tmp2int32=(sizeof(type)-((BUF_CUR-(char *)NULL)%sizeof(type)))%sizeof(type); \ + /* check and skip */ \ + BUF_CHECK(fp,tmp2int32); \ + BUF_SKIP(tmp2int32); + +/* allocate a piece of the buffer to store an array in */ +#define BUF_ALLOC(fp,ptr,type,num) \ + /* check that we have enough room */ \ + BUF_CHECK(fp,(num)*sizeof(type)); \ + /* store the pointer */ \ + (ptr)=(type *)BUF_CUR; \ + /* reserve the space */ \ + BUF_SKIP((num)*sizeof(type)); + /* read string in the buffer (using buffer, buflen and bufptr) and store the actual location of the string in field */ #define READ_STRING_BUF(fp,field) \ @@ -196,6 +214,15 @@ static void debug_dump(const void *ptr,size_t size) buffer[tmpint32]='\0'; \ DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"",buffer); +/* read a binary blob into the buffer */ +#define READ_BUF(fp,ptr,sz) \ + /* check that there is enough room and read */ \ + BUF_CHECK(fp,sz); \ + READ(fp,BUF_CUR,(size_t)sz); \ + /* store pointer and skip */ \ + (ptr)=BUF_CUR; \ + BUF_SKIP(sz); + /* read an array from a stram and store the length of the array in num (size for the array is allocated) */ #define READ_STRINGLIST_NUM(fp,arr,num) \ @@ -203,10 +230,8 @@ static void debug_dump(const void *ptr,size_t size) READ_INT32(fp,(num)); \ DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d",(int)(num)); \ /* allocate room for *char[num] */ \ - tmpint32*=sizeof(char *); \ - BUF_CHECK(fp,tmpint32); \ - (arr)=(char **)BUF_CUR; \ - BUF_SKIP(tmpint32); \ + BUF_ALLOC(fp,arr,char *,tmpint32); \ + /* read all the strings */ \ for (tmp2int32=0;tmp2int32<(num);tmp2int32++) \ { \ READ_STRING_BUF(fp,(arr)[tmp2int32]); \ @@ -219,10 +244,7 @@ static void debug_dump(const void *ptr,size_t size) READ_TYPE(fp,tmp3int32,int32_t); \ DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d",(int)tmp3int32); \ /* allocate room for *char[num+1] */ \ - tmp2int32=(tmp3int32+1)*sizeof(char *); \ - BUF_CHECK(fp,tmp2int32); \ - (arr)=(char **)BUF_CUR; \ - BUF_SKIP(tmp2int32); \ + BUF_ALLOC(fp,arr,char *,tmp3int32+1); \ /* read all entries */ \ for (tmp2int32=0;tmp2int32<tmp3int32;tmp2int32++) \ { \ |