File: au-netstring.c

package info (click to toggle)
autounit 0.20.1-5
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,700 kB
  • sloc: sh: 8,777; ansic: 2,606; makefile: 119
file content (273 lines) | stat: -rw-r--r-- 6,356 bytes parent folder | download | duplicates (4)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* Autounit test
 * Copyright (C) 2001-2002  Simon Janes
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <autounit.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

#include <glib.h>
#include <au-netstring.h>
#include <autounit-private.h>

GString *
au_gstring_to_netstring(GString *s)
{
  /* Converts the contents of a GString into a djb-style netstring, why
  ** reinvent the wheel? 
  */
  GString *ns;
  ns = g_string_new("");
  g_string_sprintfa(ns,"%u:",s->len);
  ns = g_string_append(ns,s->str);
  ns = g_string_append(ns,",");
  return ns;
}

GString *
au_netstring_to_gstring(GString *ns)
{
  GString *str;
  size_t ns_len;
  char *tnbuf;
  char *tbuf;
  str = g_string_new("");
  sscanf(ns->str,"%ld:",&ns_len);
  tnbuf = strchr(ns->str,(int)':')+1;
  tbuf = g_strndup(tnbuf,ns_len); /* strndup is a GNU extention, this is
				   just for prototyping. */
  str = g_string_append(str,tbuf);
  g_free(tbuf);
  return str;
}

GString *
au_gboolean_to_netstring(gboolean b)
{
  GString *ns;
  ns = g_string_new("");
  g_string_sprintfa(ns,"%u:",1);
  ns = g_string_append(ns,b==TRUE?"1":"0");
  ns = g_string_append(ns,",");
  return ns;
}

gboolean
au_netstring_to_gboolean(GString *ns)
{
  GString *str;
  size_t ns_len;
  char *tnbuf;
  gint tval;
  gboolean result;
  str = g_string_new("");
  sscanf(ns->str,"%d:",&ns_len);
  tnbuf = strchr(ns->str,(int)':')+1;
  sscanf(tnbuf,"%d",&tval);
  result = tval == 0 ? FALSE : TRUE;
  return result;
}

GString *
au_gint_to_netstring(gint gi)
{
  GString *gint_str;
  GString *ns;
  gint_str = g_string_new("");
  g_string_sprintf(gint_str,"%d",gi);
  ns = g_string_new("");
  g_string_sprintfa(ns,"%u:",gint_str->len);
  ns = g_string_append(ns,gint_str->str);
  ns = g_string_append(ns,",");
  g_string_free(gint_str,TRUE);
  return ns;
}

gint
au_netstring_to_gint(GString *ns)
{
  GString *str;
  size_t ns_len;
  char *tnbuf;
  gint tval;
  gint result;
  str = g_string_new("");
  sscanf(ns->str,"%d:",&ns_len);
  tnbuf = strchr(ns->str,(int)':')+1;
  sscanf(tnbuf,"%d",&tval);
  result = tval;
  g_string_free(str,TRUE);
  return result;
}

GString *
au_gulong_to_netstring(gulong gul)
{
  GString *gul_str;
  GString *ns;
  gul_str = g_string_new("");
  g_string_sprintf(gul_str,"%lu",gul);
  ns = g_string_new("");
  g_string_sprintfa(ns,"%u:",gul_str->len);
  ns = g_string_append(ns,gul_str->str);
  ns = g_string_append(ns,",");
  g_string_free(gul_str,TRUE);
  return ns;
}

gulong
au_netstring_to_gulong(GString *ns)
{
  GString *str;
  size_t ns_len;
  char *tnbuf;
  gulong tval;
  gulong result;
  str = g_string_new("");
  sscanf(ns->str,"%d:",&ns_len);
  tnbuf = strchr(ns->str,(int)':')+1;
  sscanf(tnbuf,"%lu",&tval);
  result = tval;
  g_string_free(str,TRUE);
  return result;
}

GString *
au_gdouble_to_netstring(gdouble gd)
{
  GString *gdouble_str;
  GString *ns;
  gdouble_str = g_string_new("");
  g_string_sprintf(gdouble_str,"%f",gd);
  ns = g_string_new("");
  g_string_sprintfa(ns,"%u:",gdouble_str->len);
  ns = g_string_append(ns,gdouble_str->str);
  ns = g_string_append(ns,",");
  g_string_free(gdouble_str,TRUE);
  return ns;
}

gdouble
au_netstring_to_gdouble(GString *ns)
{
  GString *str,*tmpstr;
  size_t ns_len;
  gdouble tval;
  gdouble result;
  str = g_string_new("");
  sscanf(ns->str,"%d:",&ns_len);
  tmpstr = g_string_new(strchr(ns->str,(int)':')+1);
  g_string_truncate(tmpstr,tmpstr->len-1);
  sscanf(tmpstr->str,"%lf",&tval);
  result = tval;
  g_string_free(str,TRUE);
  g_string_free(tmpstr,TRUE);
  return result;
}

GString *
au_read_netstring(int fd)
{
  /* Read a netstring from a file descriptor into a GString
     returns the contents of the netstring, not the netstring "wrapper".

     djb's sample code: (assumes you're reading from stdin, is here for
     reference)

      if (scanf("%9lu",&len) < 1) barf(); >999999999 bytes is bad
      if (getchar() != ':') barf();
      buf = malloc(len + 1);       malloc(0) is not portable
      if (!buf) barf();
      if (fread(buf,1,len,stdin) < len) barf();
      if (getchar() != ',') barf();
   */
  FILE *pipestream;
  GString *tns;
  gulong len;
  int read;
  pipestream = fdopen(fd,"r");
  if (!pipestream) {
    fprintf(stderr,_("fatal: %s\n"),strerror(errno));
    abort();
  }
  if (fscanf(pipestream,"%9lu",&len) < 1) {
    return 0;
  }
  if (fgetc(pipestream) != ':') {
    return 0;
  }
  tns = g_string_sized_new(len+1);
  read = 0;
  do
  {
      read += fread(tns->str + read, 1, len, pipestream);
  } while (read < len && !feof(pipestream));
  
  if (read < len) {
    g_string_free(tns,TRUE);
    return 0;
  }
  if (fgetc(pipestream) != ',') {
    g_string_free(tns,TRUE);
    return 0;
  }
  fclose(pipestream);
  tns->str[len] = 0;
  tns->len = len;
  return tns;
}

GString *
au_pop_netstring(GString *ns)
{
  /* Returns the netstring at the front of the GString, and changes
   * the input GString.  This API may change.  I'm not proud of this code.
   */
  GString *tns;
  gulong str_len;
  gint digit_len;
  char *digits;
  char *popped;
  GString *token_tmp;  

  token_tmp = g_string_new(ns->str);

  digits = strtok(token_tmp->str,":");

  if (!digits) {
    g_string_free(token_tmp,TRUE);
    return 0;
  }
  digit_len = strlen(digits);
  str_len = strtoul(digits,0,10);
  if (str_len > 9999999) {
    g_string_free(token_tmp,TRUE);
    return 0;
  }

  /* length ':' string ',' \0 */
  tns = g_string_sized_new(digit_len+1+str_len+1+1);
  popped = g_strndup(ns->str,digit_len+1+str_len+1);
  g_string_append(tns,popped);
  g_string_erase(ns,0,digit_len+1+str_len+1);
  free(popped);
  return tns;
}