summaryrefslogtreecommitdiff
path: root/libstore/encode.c
blob: e56dbea1df4e117032a21e4f3415ae6d6930e537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Store wire encoding

   Copyright (C) 1996 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This file is part of the GNU Hurd.

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

   The GNU Hurd 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <string.h>

#include "store.h"

/* Default encoding used for most leaf store types.  */

error_t
store_default_leaf_allocate_encoding (struct store *store,
				      struct store_enc *enc)
{
  enc->num_ports++;
  enc->num_ints += 6;
  enc->num_offsets += store->num_runs;
  if (store->name)
    enc->data_len += strlen (store->name) + 1;
  enc->data_len += store->misc_len;
  return 0;
}

error_t
store_default_leaf_encode (struct store *store, struct store_enc *enc)
{
  int i;
  size_t name_len = (store->name ? strlen (store->name) + 1 : 0);

  enc->ports[enc->cur_port++] = store->port;

  enc->ints[enc->cur_int++] = store->class;
  enc->ints[enc->cur_int++] = store->flags;
  enc->ints[enc->cur_int++] = store->block_size;
  enc->ints[enc->cur_int++] = store->num_runs;
  enc->ints[enc->cur_int++] = name_len;
  enc->ints[enc->cur_int++] = store->misc_len;

  for (i = 0; i < store->num_runs; i++)
    {
      enc->offsets[enc->cur_offset++] = store->runs[i].start;
      enc->offsets[enc->cur_offset++] = store->runs[i].length;
    }

  if (store->name)
    {
      bcopy (store->name, enc->data + enc->cur_data, name_len);
      enc->cur_data += name_len;
    }
  if (store->misc_len)
    {
      bcopy (store->misc, enc->data + enc->cur_data, store->misc_len);
      enc->cur_data += store->misc_len;
    }

  return 0;
}

/* Encode STORE into ENC, which should have been prepared with
   store_enc_init, or return an error.  The contents of ENC may then be
   return as the value of file_get_storage_info; if for some reason this
   can't be done, store_enc_dealloc may be used to deallocate the mmemory
   used by the unsent vectors.  */
error_t
store_encode (const struct store *store, struct store_enc *enc)
{
  error_t err;
  struct store_meths *meths = store->meths;
  /* We zero each vector length for the allocate_encoding method to work, so
     save the old values.  */
  mach_msg_type_number_t init_num_ports = enc->num_ports;
  mach_msg_type_number_t init_num_ints = enc->num_ints;
  mach_msg_type_number_t init_num_offsets = enc->num_offsets;
  mach_msg_type_number_t init_data_len = enc->data_len;

  if (!meths->allocate_encoding || !meths->encode)
    return EOPNOTSUPP;

  enc->num_ports = 0;
  enc->num_ints = 0;
  enc->num_offsets = 0;
  enc->data_len = 0;
  err = (*meths->allocate_encoding) (store, enc);
  if (err)
    return err;

  if (enc->num_ports > init_num_ports)
    err = vm_allocate (mach_task_self (),
		       (vm_address_t *)&enc->ports, enc->num_ports, 1);
  if (!err && enc->num_ints > init_num_ints)
    err = vm_allocate (mach_task_self (),
		       (vm_address_t *)&enc->ints, enc->num_ints, 1);
  if (!err && enc->num_offsets > init_num_offsets)
    err = vm_allocate (mach_task_self (),
		       (vm_address_t *)&enc->offsets, enc->num_offsets, 1);
  if (!err && enc->data_len > init_data_len)
    err = vm_allocate (mach_task_self (),
		       (vm_address_t *)&enc->data, enc->data_len, 1);

  if (! err)
    err = (*meths->encode) (store, enc);

  enc->cur_port = enc->cur_int = enc->cur_offset = enc->cur_data = 0;

  if (err)
    store_enc_dealloc (enc);

  return err;
}