Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/nslcd-common.h
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2006-10-31 13:18:49 +0100
committerArthur de Jong <arthur@arthurdejong.org>2006-10-31 13:18:49 +0100
commit0fd56fe921624596d2cdca7b42e048e63570d8d6 (patch)
tree9c986b74c2ff77fd07ac6b268d85d42489755a51 /nslcd-common.h
parentfe8fb00dd57714528535de7e3a8d42f64e7a79aa (diff)
add header file defining read and write macros
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/libnss_ldapd@35 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd-common.h')
-rw-r--r--nslcd-common.h84
1 files changed, 84 insertions, 0 deletions
diff --git a/nslcd-common.h b/nslcd-common.h
new file mode 100644
index 0000000..c08050e
--- /dev/null
+++ b/nslcd-common.h
@@ -0,0 +1,84 @@
+
+/* common macros for reading and writing from streams */
+
+#ifndef _NSLCD_COMMON_H
+#define _NSLCD_COMMON_H 1
+
+/* WRITE marcos, used for writing data, on write error they will
+ call the ERROR_OUT_WRITEERROR macro
+ these macros may require the availability of the following
+ variables:
+ int32_t tmpint32; - temporary variable
+ */
+
+#define WRITE(fp,ptr,size) \
+ if (fwrite(ptr,size,1,fp)<1) \
+ { ERROR_OUT_WRITEERROR(fp) }
+
+#define WRITE_TYPE(fp,field,type) \
+ WRITE(fp,&(field),sizeof(type))
+
+#define WRITE_INT32(fp,i) \
+ tmpint32=(int32_t)(i); \
+ WRITE_TYPE(fp,tmpint32,int32_t)
+
+#define WRITE_STRING(fp,str) \
+ WRITE_INT32(fp,strlen(str)); \
+ WRITE(fp,str,strlen(str));
+
+#define WRITE_FLUSH(fp) \
+ if (fflush(fp)<0) \
+ { ERROR_OUT_WRITEERROR(fp) }
+
+/* READ macros, used for reading data, on read error they will
+ call the ERROR_OUT_READERROR or ERROR_OUT_BUFERROR macro
+ these macros may require the availability of the following
+ variables:
+ int32_t tmpint32; - temporary variable
+ char *buffer; - pointer to a buffer for reading strings
+ size_t buflen; - the size of the buffer
+ size_t bufptr; - the current position in the buffer
+ */
+
+#define READ(fp,ptr,size) \
+ if (fread(ptr,size,1,fp)<1) \
+ { ERROR_OUT_READERROR(fp) }
+
+#define READ_TYPE(fp,field,type) \
+ READ(fp,&(field),sizeof(type))
+
+#define READ_INT32(fp,i) \
+ READ_TYPE(fp,tmpint32,int32_t); \
+ i=tmpint32;
+
+/* 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) \
+ /* read the size of the string */ \
+ READ_TYPE(fp,tmpint32,int32_t); \
+ /* check if read would fit */ \
+ if ((bufptr+(size_t)tmpint32+1)>buflen) \
+ { ERROR_OUT_BUFERROR(fp) } /* will not fit */ \
+ /* read string from the stream */ \
+ READ(fp,buffer+bufptr,(size_t)tmpint32); \
+ /* null-terminate string in buffer */ \
+ buffer[bufptr+tmpint32]='\0'; \
+ /* prepare result */ \
+ (field)=buffer+bufptr; \
+ bufptr+=tmpint32+1;
+
+/* read a string from the stream dynamically allocating memory
+ for the string (don't forget to call free() later on) */
+#define READ_STRING_ALLOC(fp,field) \
+ /* read the size of the string */ \
+ READ_TYPE(fp,tmpint32,int32_t); \
+ /* allocate memory */ \
+ (field)=(char *)malloc((size_t)(tmpint32+1)); \
+ if ((field)==NULL) \
+ { ERROR_OUT_ALLOCERROR(fp) } /* problem allocating */ \
+ /* read string from the stream */ \
+ READ(fp,name,(size_t)tmpint32); \
+ /* null-terminate string in buffer */ \
+ (name)[tmpint32]='\0';
+
+#endif /* not _NSLCD_COMMON_H */