trunk/test/aaip_0_2.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define Aaip_h_is_includeD   yes

Functions

unsigned int aaip_encode (char aa_name[2], unsigned int num_attrs, char **names, size_t *attr_lengths, char **attrs, size_t *result_len, unsigned char **result, int flag)
int aaip_encode_acl (char *acl_text, size_t *result_len, unsigned char **result, int flag)
size_t aaip_sizeof_aaip_state (void)
int aaip_init (struct aaip_state *aaip, char aa_name[2], int flag)
int aaip_submit_data (struct aaip_state *aaip, unsigned char *data, size_t num_data, size_t *ready_bytes, int flag)
int aaip_fetch_data (struct aaip_state *aaip, char *result, size_t result_size, size_t *num_result, int flag)
int aaip_skip_component (struct aaip_state *aaip, int flag)
int aaip_decode_pair (struct aaip_state *aaip, unsigned char *data, size_t num_data, size_t *consumed, char *name, size_t name_size, size_t *name_fill, char *value, size_t value_size, size_t *value_fill, int flag)
unsigned int aaip_get_pairs_skipped (struct aaip_state *aaip, int flag)
int aaip_decode_acl (unsigned char *data, size_t num_data, size_t *consumed, char *acl_text, size_t acl_text_size, size_t *acl_text_fill, int flag)


Define Documentation

#define Aaip_h_is_includeD   yes

Definition at line 14 of file aaip_0_2.h.


Function Documentation

int aaip_decode_acl ( unsigned char *  data,
size_t  num_data,
size_t *  consumed,
char *  acl_text,
size_t  acl_text_size,
size_t *  acl_text_fill,
int  flag 
)

Definition at line 1232 of file aaip_0_2.c.

References Aaip_ACL_GROUP, Aaip_ACL_GROUP_N, Aaip_ACL_GROUP_OBJ, Aaip_ACL_MASK, Aaip_ACL_OTHER, Aaip_ACL_USER, Aaip_ACL_USER_N, Aaip_ACL_USER_OBJ, Aaip_EXEC, Aaip_FUTURE_VERSION, Aaip_READ, aaip_read_qualifier(), Aaip_SWITCH_MARK, Aaip_TRANSLATE, Aaip_WRITE, and aaip_write_acl_line().

Referenced by decode_acl().

01235 {
01236  unsigned char *rpt;
01237  char perm_text[4], *wpt, name[1024];
01238  int type, qualifier= 0, perm, ret, i, cnt;
01239  size_t w_size, name_fill;
01240  uid_t uid;
01241  gid_t gid;
01242  struct passwd *pwd;
01243  struct group *grp;
01244 
01245  cnt= flag & 1;
01246  *consumed= 0;
01247  wpt= acl_text;
01248  w_size= acl_text_size;
01249  *acl_text_fill= 0;
01250  for(rpt= data; rpt - data < num_data; ) {
01251    perm= *rpt;
01252    strcpy(perm_text, "---");
01253    if(perm & Aaip_READ)
01254      perm_text[0]= 'r';
01255    if(perm & Aaip_WRITE)
01256      perm_text[1]= 'w';
01257    if(perm & Aaip_EXEC)
01258      perm_text[2]= 'x';
01259      
01260    type= (*rpt) >> 4;
01261    if(type == Aaip_FUTURE_VERSION) /* indicate to caller: version mismatch */
01262      return(-3);
01263 
01264    qualifier= !!((*rpt) & 8);
01265    if(qualifier) {
01266      ret= aaip_read_qualifier(rpt + 1, num_data - (rpt + 1 - data),
01267                               name, sizeof(name), &name_fill, 0);
01268      if(ret <= 0)
01269        return(-1);
01270    }
01271 
01272    /* Advance read pointer */
01273    (*consumed)+= 1 + (qualifier ? name_fill + 1 : 0);
01274    rpt+= 1 + (qualifier ? name_fill + 1 : 0);
01275 
01276    ret= 1;
01277    if(type == Aaip_TRANSLATE) {
01278      /* rightfully ignored yet */;
01279  continue;
01280    } else if(type == Aaip_ACL_USER_OBJ) {
01281      /* user::rwx */
01282      ret= aaip_write_acl_line(&wpt, &w_size, "user", "", perm_text, cnt);
01283    } else if(type == Aaip_ACL_USER) {
01284      /* user:<username>:rwx */;
01285      ret= aaip_write_acl_line(&wpt, &w_size, "user", name, perm_text, cnt);
01286    } else if(type == Aaip_ACL_GROUP_OBJ) {
01287      /* user::rwx */
01288      ret= aaip_write_acl_line(&wpt, &w_size, "group", "", perm_text, cnt);
01289    } else if(type == Aaip_ACL_GROUP) {
01290      /* group:<groupname>:rwx */;
01291      ret= aaip_write_acl_line(&wpt, &w_size, "group", name, perm_text, cnt);
01292    } else if(type == Aaip_ACL_MASK) {
01293      /* mask::rwx */
01294      ret= aaip_write_acl_line(&wpt, &w_size, "mask", "", perm_text, cnt);
01295    } else if(type == Aaip_ACL_OTHER) {
01296      /* other::rwx */
01297      ret= aaip_write_acl_line(&wpt, &w_size, "other", "", perm_text, cnt);
01298    } else if(type == Aaip_SWITCH_MARK) {
01299      /* Indicate to caller: end of desired ACL type access/default */
01300      if((!(perm & Aaip_EXEC)) ^ (!!(flag & 2)))
01301        return(2);
01302    } else if(type == Aaip_ACL_USER_N) {
01303      /* determine username from uid */
01304      uid= 0;
01305      for(i= 0; i < name_fill; i++)
01306        uid= (uid << 8) | ((unsigned char *) name)[i];
01307      pwd= getpwuid(uid);
01308      if(pwd == NULL)
01309        sprintf(name, "%.f", (double) uid);
01310      else if(strlen(pwd->pw_name) >= sizeof(name))
01311        sprintf(name, "%.f", (double) uid);
01312      else
01313        strcpy(name, pwd->pw_name);
01314      /* user:<username>:rwx */;
01315      ret= aaip_write_acl_line(&wpt, &w_size, "user", name, perm_text, cnt);
01316    } else if(type == Aaip_ACL_GROUP_N) {
01317      /* determine username from gid */;
01318      gid= 0;
01319      for(i= 0; i < name_fill; i++)
01320        gid= (gid << 8) | ((unsigned char *) name)[i];
01321      grp= getgrgid(gid);
01322      if(grp == NULL)
01323        sprintf(name, "%.f", (double) gid);
01324      else if(strlen(grp->gr_name) >= sizeof(name))
01325        sprintf(name, "%.f", (double) gid);
01326      else
01327        strcpy(name, grp->gr_name);
01328      /* user:<username>:rwx */;
01329      ret= aaip_write_acl_line(&wpt, &w_size, "group", name, perm_text, cnt);
01330    } else {
01331      /* indicate to caller: unknown type */
01332      return(-4);
01333    }
01334    if(ret <= 0)
01335      return(-2);
01336  }
01337  if(flag & 1)
01338    *acl_text_fill= w_size + 1;
01339  return(1);
01340 }

int aaip_decode_pair ( struct aaip_state aaip,
unsigned char *  data,
size_t  num_data,
size_t *  consumed,
char *  name,
size_t  name_size,
size_t *  name_fill,
char *  value,
size_t  value_size,
size_t *  value_fill,
int  flag 
)

Definition at line 1086 of file aaip_0_2.c.

References aaip_state::aa_ends, aaip_advance_pair(), aaip_fetch_data(), aaip_submit_data(), aaip_state::first_is_name, aaip_state::num_components, aaip_state::num_recs, and aaip_state::pair_status.

Referenced by main().

01091 {
01092  int ret;
01093  size_t ready_bytes;
01094 
01095  *consumed= 0;
01096  if(aaip->pair_status < 0 || aaip->pair_status == 4 ||
01097     aaip->pair_status == 5) { /* dead ends */
01098    ret= aaip->pair_status;
01099    goto ex;
01100  } else if(aaip->pair_status == 2 || aaip->pair_status == 3) {
01101    if(aaip->pair_status == 3 && num_data > 0)
01102      {ret= 0; goto ex;}
01103    /* Start a new pair */
01104    if(!aaip->first_is_name) /* Eventually skip orphaned value */
01105      aaip_fetch_data(aaip, NULL, (size_t) 0, NULL, 1);
01106    *name_fill= *value_fill= 0;
01107  }
01108 
01109  if(num_data > 0) {
01110    ret= aaip_submit_data(aaip, data, num_data, &ready_bytes, 0);
01111  } else {
01112    ret= 1;
01113    if(aaip->num_components)
01114      ret= 3;
01115    else if(aaip->num_recs)
01116      ret= 2;
01117  }
01118  if(ret < 0) { /* non-AA field detected */
01119    *consumed= ready_bytes;
01120    {ret= -1; goto ex;}
01121  } else if(ret == 0) { /* buffer overflow */;
01122    /* should not happen with correct usage */ 
01123    {ret= -2; goto ex;}
01124  } else if(ret == 1) { /* no component record complete */
01125    goto ex;
01126  } else if(ret == 2) { /* component record complete, may be delivered */
01127    ;
01128  } else if(ret == 3) { /* component complete, may be delivered */
01129    ;
01130  } else if(ret == 4) { /* no component available, no more data expected */
01131    {ret= 5; goto ex;}
01132  } else 
01133    {ret= -1; goto ex;} /* unknown reply from aaip_submit_data() */
01134 
01135  *consumed= num_data;
01136  ret= aaip_advance_pair(aaip, name, name_size, name_fill,
01137                         value, value_size, value_fill, 0);
01138  if(aaip->aa_ends == 3) {
01139    if(ret >= 2 && ret <= 4)
01140      ret= 4;
01141    else
01142      ret= 5;
01143  }
01144 ex:;
01145  aaip->pair_status= ret;
01146  return(ret);
01147 }

unsigned int aaip_encode ( char  aa_name[2],
unsigned int  num_attrs,
char **  names,
size_t *  attr_lengths,
char **  attrs,
size_t *  result_len,
unsigned char **  result,
int  flag 
)

Definition at line 68 of file aaip_0_2.c.

References aaip_encode_pair().

Referenced by main().

00072 {
00073  size_t mem_size= 0, comp_size;
00074  unsigned int number_of_fields, i, num_recs, total_recs= 0, ret;
00075 
00076  /* Predict memory needs, number of SUSP fields and component records */
00077  *result_len= 0;
00078  for(i= 0; i < num_attrs; i++) {
00079    ret= aaip_encode_pair(names[i], attr_lengths[i], attrs[i],
00080                          &num_recs, &comp_size, NULL, (size_t) 0, 1);
00081    if(ret <= 0)
00082      return(ret);
00083    mem_size+= comp_size;
00084    total_recs= num_recs;
00085  }
00086  number_of_fields= mem_size / 250 + !!(mem_size % 250);
00087  mem_size+= number_of_fields * 5;
00088 
00089 #ifdef Aaip_encode_debuG
00090  *result= (unsigned char *) calloc(1, mem_size + 1024000);
00091                                           /* generous honeypot for overflows */
00092 #else
00093  *result= (unsigned char *) calloc(1, mem_size);
00094 #endif
00095 
00096 
00097  /* Encode pairs into result */
00098  for(i= 0; i < num_attrs; i++) {
00099    ret= aaip_encode_pair(names[i], attr_lengths[i], attrs[i],
00100                          &num_recs, &comp_size, *result, *result_len, 0);
00101    if(ret <= 0)
00102      return(ret);
00103    (*result_len)+= comp_size;
00104  }
00105 
00106  /* write the field headers */
00107  for(i= 0; i < number_of_fields; i++) {
00108    (*result)[i * 255 + 0]= aa_name[0];
00109    (*result)[i * 255 + 1]= aa_name[1];
00110    if(i < number_of_fields - 1 || (mem_size % 255) == 0)
00111      (*result)[i * 255 + 2]= 255;
00112    else 
00113      (*result)[i * 255 + 2]= mem_size % 255;
00114    (*result)[i * 255 + 3]= 1;
00115    (*result)[i * 255 + 4]= (flag & 1) || (i < number_of_fields - 1);
00116  }
00117  (*result_len)+= number_of_fields * 5;
00118 
00119 #ifdef Aaip_encode_debuG
00120  if(*result_len != mem_size) {
00121    fprintf(stderr, "aaip_encode(): MEMORY MISMATCH BY %d BYTES\n",
00122            (int) (mem_size - *result_len));
00123  }
00124  ret= 0;
00125  for(i= 0; i < *result_len; i+= ((unsigned char *) (*result))[i + 2])
00126    ret++;
00127  if(ret != number_of_fields) {
00128    fprintf(stderr, "aaip_encode(): WRONG NUMBER OF FIELDS %d <> %d\n",
00129            number_of_fields, ret);
00130  }
00131 #endif /* Aaip_encode_debuG */
00132 
00133  return(number_of_fields);
00134 }

int aaip_encode_acl ( char *  acl_text,
size_t *  result_len,
unsigned char **  result,
int  flag 
)

Definition at line 215 of file aaip_0_2.c.

References aaip_encode_acl_text().

Referenced by test_acl().

00217 {
00218  ssize_t bytes;
00219 
00220  *result= NULL;
00221  *result_len= 0;
00222  bytes= aaip_encode_acl_text(acl_text, (size_t) 0, NULL, 1 | (flag & 6));
00223  if(bytes < 0)
00224    return(0);
00225  if(flag & 1) {
00226    *result_len= bytes;
00227    return(1);
00228  }
00229  *result= calloc(bytes + 1, 1);
00230  if(*result == NULL)
00231    return(-1);
00232  (*result)[bytes]= 0;
00233  *result_len= bytes;
00234  bytes= aaip_encode_acl_text(acl_text, *result_len, *result, (flag & 6));
00235  if(bytes != *result_len) {
00236    *result_len= 0;
00237    return(0);
00238  }
00239  return(1);
00240 }

int aaip_fetch_data ( struct aaip_state aaip,
char *  result,
size_t  result_size,
size_t *  num_result,
int  flag 
)

Definition at line 913 of file aaip_0_2.c.

References aaip_get_buffer_byte(), aaip_read_from_recs(), aaip_shift_recs(), aaip_state::first_is_name, aaip_state::num_components, aaip_state::num_recs, and aaip_state::ready_bytes.

Referenced by aaip_advance_pair(), aaip_decode_pair(), and aaip_skip_component().

00916 {
00917  int ret= -1, complete= 0, payload;
00918  unsigned int i, num_bytes= 0, h;
00919 
00920  if(aaip->num_recs == 0)
00921    return(-1);
00922 
00923  /* Copy data until end of buffer or end of component */
00924  h= 0;
00925  for(i= 0; i < aaip->num_recs && !complete; i++) {
00926    payload= aaip_get_buffer_byte(aaip, h + 1, 0);
00927    if(!(flag & 1)) {
00928      if(num_bytes + payload > result_size)
00929        return(-2);
00930      aaip_read_from_recs(aaip, h + 2, (unsigned char *) (result + num_bytes),
00931                          payload, 0);
00932      *num_result= num_bytes + payload;
00933    }
00934    num_bytes+= payload;
00935    if(!(aaip_get_buffer_byte(aaip, h, 0) & 1))
00936      complete= 1;
00937    h+= payload + 2;
00938  }
00939  aaip->ready_bytes-= num_bytes;
00940  aaip->num_recs-= i;
00941 
00942  /* Shift buffer */
00943  aaip_shift_recs(aaip, h, 0);
00944 
00945  /* Compute reply */
00946  ret= 2 * !aaip->first_is_name;
00947  if(complete) {
00948    aaip->first_is_name= !aaip->first_is_name;
00949    if(aaip->num_components > 0)
00950      aaip->num_components--;
00951  } else
00952    ret|= 1;
00953 
00954  return(ret);
00955 }

unsigned int aaip_get_pairs_skipped ( struct aaip_state aaip,
int  flag 
)

Definition at line 1150 of file aaip_0_2.c.

References aaip_state::pairs_skipped.

Referenced by main().

01151 {
01152  return(aaip->pairs_skipped);
01153 }

int aaip_init ( struct aaip_state aaip,
char  aa_name[2],
int  flag 
)

Definition at line 475 of file aaip_0_2.c.

References aaip_state::aa_head_missing, aaip_state::aa_missing, aaip_state::aa_name, Aaip_buffer_reservE, Aaip_buffer_sizE, aaip_state::end_of_components, aaip_state::first_is_name, aaip_state::num_components, aaip_state::num_recs, aaip_state::pair_status, aaip_state::pairs_skipped, aaip_state::ready_bytes, aaip_state::rec_ends, aaip_state::rec_head_missing, aaip_state::rec_missing, aaip_state::recs, aaip_state::recs_fill, aaip_state::recs_invalid, and aaip_state::recs_start.

Referenced by main().

00476 {
00477  aaip->aa_name[0]= aa_name[0];
00478  aaip->aa_name[1]= aa_name[1];
00479  aaip->aa_head_missing= 5;
00480  aaip->aa_missing= 0;
00481 
00482  aaip->recs_invalid= 0;
00483  memset(aaip->recs, 0, Aaip_buffer_sizE + Aaip_buffer_reservE);
00484  aaip->recs_fill= 0;
00485  aaip->recs_start= aaip->recs;
00486  aaip->rec_head_missing= 2;
00487  aaip->rec_missing= 0;
00488  aaip->rec_ends= 0;
00489 
00490  aaip->num_recs= 0;
00491  aaip->ready_bytes= 0;
00492 
00493  aaip->num_components= 0;
00494  aaip->end_of_components= 0;
00495  aaip->first_is_name= 1;
00496 
00497  aaip->pair_status= 2;
00498  aaip->pairs_skipped= 0;
00499 
00500  return(1);
00501 }

size_t aaip_sizeof_aaip_state ( void   ) 

Definition at line 469 of file aaip_0_2.c.

Referenced by main().

00470 {
00471  return((size_t) sizeof(struct aaip_state));
00472 }

int aaip_skip_component ( struct aaip_state aaip,
int  flag 
)

Definition at line 968 of file aaip_0_2.c.

References aaip_state::aa_ends, aaip_fetch_data(), aaip_state::first_is_name, aaip_state::num_components, aaip_state::num_recs, aaip_state::ready_bytes, aaip_state::recs_fill, and aaip_state::recs_invalid.

Referenced by aaip_advance_pair().

00969 {
00970  int to_skip= 1;
00971 
00972  if(aaip->first_is_name && !(flag & 1))
00973    to_skip= 2;
00974  if(aaip->recs_invalid) {
00975    aaip->recs_invalid+= to_skip;
00976    return(1);
00977  }
00978 
00979  if(aaip->num_components) {
00980    /* null-fetch */
00981    aaip_fetch_data(aaip, NULL, (size_t) 0, NULL, 1);
00982    to_skip--;
00983  }
00984  if(aaip->num_components && to_skip) {
00985    /* null-fetch */
00986    aaip_fetch_data(aaip, NULL, (size_t) 0, NULL, 1);
00987    to_skip--;
00988  }
00989  if(to_skip) {
00990    aaip->recs_fill= 0;
00991    aaip->num_recs= 0;
00992    aaip->ready_bytes= 0;
00993  }
00994  aaip->recs_invalid= to_skip;
00995  if(aaip->aa_ends == 2 && aaip->num_recs == 0)
00996    aaip->aa_ends= 3;
00997  return(1 + (aaip->num_recs > 0));
00998 }

int aaip_submit_data ( struct aaip_state aaip,
unsigned char *  data,
size_t  num_data,
size_t *  ready_bytes,
int  flag 
)

Definition at line 852 of file aaip_0_2.c.

References aaip_state::aa_ends, aaip_state::aa_head_missing, aaip_state::aa_missing, Aaip_buffer_sizE, aaip_consume_aa_data(), aaip_consume_aa_head(), aaip_state::num_components, aaip_state::num_recs, aaip_state::ready_bytes, and aaip_state::recs_fill.

Referenced by aaip_advance_pair(), and aaip_decode_pair().

00855 {
00856  int ret;
00857  unsigned char *in_data;
00858 
00859  if(aaip->aa_ends == 3)
00860    return(4);
00861  in_data= data;
00862  if(num_data == 0)
00863    goto ex;
00864  if(aaip->recs_fill + num_data > Aaip_buffer_sizE)
00865    return(0);
00866  
00867  while(num_data > 0) {
00868    if(aaip->aa_head_missing > 0) {
00869      ret= aaip_consume_aa_head(aaip, &data, &num_data, 0);
00870      if(ret < 0) {
00871        *ready_bytes= data - in_data;
00872        return(-1);
00873      }
00874      if(num_data == 0 || aaip->aa_missing <= 0)
00875        goto ex;
00876    }
00877    aaip_consume_aa_data(aaip, &data, &num_data, 0);
00878    if(aaip->aa_missing)
00879  break;
00880  }
00881 ex:;
00882  *ready_bytes= aaip->ready_bytes;
00883  if(aaip->num_components > 0)
00884    return(3);
00885  if(aaip->num_recs > 0)
00886    return(2);
00887  if(aaip->aa_ends && aaip->aa_head_missing == 0 && aaip->aa_missing == 0)
00888    aaip->aa_ends= 2;
00889  if(aaip->aa_ends == 2 && aaip->num_recs == 0)
00890    aaip->aa_ends= 3;
00891  if(aaip->aa_ends == 3)
00892    return(4);
00893  return(1);
00894 }


Generated on Thu Jan 1 04:47:00 2009 for libisoburn by  doxygen 1.5.6