Debian Codegraph @

cassandane/Cassandane/Util/NetString.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 # code for reading and writing "netstrings", originally from FastMail's
41 # server utilities.
42 #
43 # A netstring is defined as:
44 # LENGTH COLON DATA COMMA
45 # where length is decimal [0-9]+ and is a count of BYTES
46 #
47 # Examples:
48 # 0:,
49 # 12:Hello world!,
50 #
51 # NOTE - there are no trailing endlines, just the comma.
52
53 package Cassandane::Util::NetString;
54 use strict;
55 use warnings;
56 use vars qw(@ISA @EXPORT);
57
58 @ISA = qw(Exporter);
59 @EXPORT = qw(print_netstring get_netstring);
60
61 sub print_netstring {
62 my $fh = shift;
63 my $data = shift;
64
65 die "Printing undefined network string" unless defined $data;
66
67 my $size = length $data;
68
69 print $fh "$size:$data,"
70 }
71
72 sub get_netstring {
73 my $fh = shift;
74
75 my($r, $ns);
76 my $s = "";
77 my $len = 0;
78
79 # read the length
80 for (;;) {
81 defined($r = read($fh, $s, 1)) or return undef;
82
83 return "" if !$r;
84 last if $s eq ":";
85 return undef if $s !~ /^[0-9]$/;
86
87 $len = 10 * $len + $s;
88 return undef if $len > 200000000;
89 }
90
91 $s = "";
92
93 # read the string 'body'
94 defined($r = read($fh, $s, $len)) or return undef;
95 return "" if (!$r and $len != 0); # zero length is OK
96 $ns = $s;
97
98 # read the trailing comma
99 defined($r = read($fh, $s, 1)) or return undef;
100 return "" if !$r;
101 return undef if $s ne ",";
102
103 return $ns;
104 }
105
106 1;