<commit_before>/*
   nslcd-common.h - helper macros for reading and writing in
                    protocol streams

   Copyright (C) 2006 West Consulting
   Copyright (C) 2006 <NAME>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   MA 02110-1301 USA
*/

#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
   */

#include <stdio.h>

#ifdef DEBUG_PROT
/* define a debugging macro to output logging */
#include <string.h>
#include <errno.h>
#define DEBUG_PRINT(fmt,arg) \
  fprintf(stderr,"%s:%d:%s: " fmt"\n",__FILE__,__LINE__,__PRETTY_FUNCTION__,arg);
#else /* DEBUG_PROT */
/* define an empty debug macro to disable logging */
#define DEBUG_PRINT(fmt,arg)
#endif /* not DEBUG_PROT */

#define WRITE(fp,ptr,size) \
  DEBUG_PRINT("WRITE       : var="__STRING(ptr)" size=%d",(int)size); \
  if (fwrite(ptr,size,1,fp)<1) \
  { \
    DEBUG_PRINT("WRITE       : var="__STRING(ptr)" error: %s",strerror(errno)); \
    ERROR_OUT_WRITEERROR(fp); \
  }

#define WRITE_TYPE(fp,field,type) \
  WRITE(fp,&(field),sizeof(type))

#define WRITE_INT32(fp,i) \
  DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%d",(int)i); \
  tmpint32=(int32_t)(i); \
  WRITE_TYPE(fp,tmpint32,int32_t)

#define WRITE_STRING(fp,str) \
  DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"",str); \
  WRITE_INT32(fp,strlen(str)); \
  if (tmpint32>0) \
    { WRITE(fp,str,tmpint32); }

#define WRITE_FLUSH(fp) \
  if (fflush(fp)<0) \
  { \
    DEBUG_PRINT("WRITE_FLUSH : error: %s",strerror(errno)); \
    ERROR_OUT_WRITEERROR(fp); \
  }

#define WRITE_STRINGLIST_NUM(fp,arr,num) \
  /* write number of strings */ \
  DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",(int)num); \
  WRITE_INT32(fp,num); \
  /* write strings */ \
  for (tmp2int32=0;tmp2int32<(num);tmp2int32++) \
  { \
    WRITE_STRING(fp,(arr)[tmp2int32]); \
  }

#define WRITE_STRINGLIST_NULLTERM(fp,arr) \
  /* first determin length of array */ \
  for (tmp3int32=0;(arr)[tmp3int32]!=NULL;tmp3int32++) \
    /*noting*/ ; \
  /* write number of strings */ \
  DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",(int)tmp3int32); \
  WRITE_TYPE(fp,tmp3int32,int32_t); \
  /* write strings */ \
  for (tmp2int32=0;tmp2int32<tmp3int32;tmp2int32++) \
  { \
    WRITE_STRING(fp,(arr)[tmp2int32]); \
  }

/* 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) \
  { \
    DEBUG_PRINT("READ       : var="__STRING(ptr)" error: %s",strerror(errno)); \
    ERROR_OUT_READERROR(fp); \
  } \
  DEBUG_PRINT("READ       : var="__STRING(ptr)" size=%d",(int)size);

#define READ_TYPE(fp,field,type) \
  READ(fp,&(field),sizeof(type))

#define READ_INT32(fp,i) \
  READ_TYPE(fp,tmpint32,int32_t); \
  i=tmpint32; \
  DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32=%d",(int)i);

/* current position in the buffer */
#define BUF_CUR \
  (buffer+bufptr)

/* check that the buffer has sz bytes left in it */
#define BUF_CHECK(fp,sz) \
  if ((bufptr+(size_t)(sz))>buflen) \
  { \
    DEBUG_PRINT("READ       : buffer error: %d bytes missing",((sz)-(buflen))); \
    ERROR_OUT_BUFERROR(fp); \
  } /* will not fit */

/* move the buffer pointer */
#define BUF_SKIP(sz) \
  bufptr+=(size_t)(sz);

/* 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); \
  DEBUG_PRINT("READ_STRING: var="__STRING(field)" strlen=%d",tmpint32); \
  /* check if read would fit */ \
  BUF_CHECK(fp,tmpint32+1); \
  /* read string from the stream */ \
  if (tmpint32>0) \
    { READ(fp,BUF_CUR,(size_t)tmpint32); } \
  /* null-terminate string in buffer */ \
  BUF_CUR[tmpint32]='\0'; \
  DEBUG_PRINT("READ_STRING: var="__STRING(field)" string=\"%s\"",BUF_CUR); \
  /* prepare result */ \
  (field)=BUF_CUR; \
  BUF_SKIP(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) \
  { \
    DEBUG_PRINT("READ_STRING: var="__STRING(field)" malloc() error: %s",strerror(errno)); \
    ERROR_OUT_ALLOCERROR(fp); \
  } /* problem allocating */ \
  /* read string from the stream */ \
  if (tmpint32>0) \
    { READ(fp,name,(size_t)tmpint32); } \
  /* null-terminate string */ \
  (name)[tmpint32]='\0'; \
  DEBUG_PRINT("READ_STRING: var="__STRING(field)" string=\"%s\"",(name));

/* read an array from a stram and store the length of the
   array in num (size for the array is allocated) */<commit_msg>fix bug that always causes a READ_STRING_ALLOC to read to a variable called name<commit_after>/*
   nslcd-common.h - helper macros for reading and writing in
                    protocol streams

   Copyright (C) 2006 West Consulting
   Copyright (C) 2006 <NAME>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   MA 02110-1301 USA
*/

#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
   */

#include <stdio.h>

#ifdef DEBUG_PROT
/* define a debugging macro to output logging */
#include <string.h>
#include <errno.h>
#define DEBUG_PRINT(fmt,arg) \
  fprintf(stderr,"%s:%d:%s: " fmt"\n",__FILE__,__LINE__,__PRETTY_FUNCTION__,arg);
#else /* DEBUG_PROT */
/* define an empty debug macro to disable logging */
#define DEBUG_PRINT(fmt,arg)
#endif /* not DEBUG_PROT */

#define WRITE(fp,ptr,size) \
  DEBUG_PRINT("WRITE       : var="__STRING(ptr)" size=%d",(int)size); \
  if (fwrite(ptr,size,1,fp)<1) \
  { \
    DEBUG_PRINT("WRITE       : var="__STRING(ptr)" error: %s",strerror(errno)); \
    ERROR_OUT_WRITEERROR(fp); \
  }

#define WRITE_TYPE(fp,field,type) \
  WRITE(fp,&(field),sizeof(type))

#define WRITE_INT32(fp,i) \
  DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%d",(int)i); \
  tmpint32=(int32_t)(i); \
  WRITE_TYPE(fp,tmpint32,int32_t)

#define WRITE_STRING(fp,str) \
  DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"",str); \
  WRITE_INT32(fp,strlen(str)); \
  if (tmpint32>0) \
    { WRITE(fp,str,tmpint32); }

#define WRITE_FLUSH(fp) \
  if (fflush(fp)<0) \
  { \
    DEBUG_PRINT("WRITE_FLUSH : error: %s",strerror(errno)); \
    ERROR_OUT_WRITEERROR(fp); \
  }

#define WRITE_STRINGLIST_NUM(fp,arr,num) \
  /* write number of strings */ \
  DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",(int)num); \
  WRITE_INT32(fp,num); \
  /* write strings */ \
  for (tmp2int32=0;tmp2int32<(num);tmp2int32++) \
  { \
    WRITE_STRING(fp,(arr)[tmp2int32]); \
  }

#define WRITE_STRINGLIST_NULLTERM(fp,arr) \
  /* first determin length of array */ \
  for (tmp3int32=0;(arr)[tmp3int32]!=NULL;tmp3int32++) \
    /*noting*/ ; \
  /* write number of strings */ \
  DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d",(int)tmp3int32); \
  WRITE_TYPE(fp,tmp3int32,int32_t); \
  /* write strings */ \
  for (tmp2int32=0;tmp2int32<tmp3int32;tmp2int32++) \
  { \
    WRITE_STRING(fp,(arr)[tmp2int32]); \
  }

/* 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) \
  { \
    DEBUG_PRINT("READ       : var="__STRING(ptr)" error: %s",strerror(errno)); \
    ERROR_OUT_READERROR(fp); \
  } \
  DEBUG_PRINT("READ       : var="__STRING(ptr)" size=%d",(int)size);

#define READ_TYPE(fp,field,type) \
  READ(fp,&(field),sizeof(type))

#define READ_INT32(fp,i) \
  READ_TYPE(fp,tmpint32,int32_t); \
  i=tmpint32; \
  DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32=%d",(int)i);

/* current position in the buffer */
#define BUF_CUR \
  (buffer+bufptr)

/* check that the buffer has sz bytes left in it */
#define BUF_CHECK(fp,sz) \
  if ((bufptr+(size_t)(sz))>buflen) \
  { \
    DEBUG_PRINT("READ       : buffer error: %d bytes missing",((sz)-(buflen))); \
    ERROR_OUT_BUFERROR(fp); \
  } /* will not fit */

/* move the buffer pointer */
#define BUF_SKIP(sz) \
  bufptr+=(size_t)(sz);

/* 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); \
  DEBUG_PRINT("READ_STRING: var="__STRING(field)" strlen=%d",tmpint32); \
  /* check if read would fit */ \
  BUF_CHECK(fp,tmpint32+1); \
  /* read string from the stream */ \
  if (tmpint32>0) \
    { READ(fp,BUF_CUR,(size_t)tmpint32); } \
  /* null-terminate string in buffer */ \
  BUF_CUR[tmpint32]='\0'; \
  DEBUG_PRINT("READ_STRING: var="__STRING(field)" string=\"%s\"",BUF_CUR); \
  /* prepare result */ \
  (field)=BUF_CUR; \
  BUF_SKIP(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) \
  { \
    DEBUG_PRINT("READ_STRING: var="__STRING(field)" malloc() error: %s",strerror(errno)); \
    ERROR_OUT_ALLOCERROR(fp); \
  } /* problem allocating */ \
  /* read string from the stream */ \
  if (tmpint32>0) \
    { READ(fp,(field),(size_t)tmpint32); } \
  /* null-terminate string */ \
  (field)[tmpint32]='\0'; \
  DEBUG_PRINT("READ_STRING: var="__STRING(field)" string=\"%s\"",(field));

/* read an array from a stram and store the length of the
   array in num (size for the array is allocated) */
{'fraction_non_alphanumeric': 0.14544547872340424, 'fraction_numerical': 0.025099734042553192, 'mean_word_length': 3.7336742722265934, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '862860', 'n_tokens_mistral': 4401, 'n_tokens_neox': 4203, 'n_words': 1077}