@@ -64,44 +64,45 @@ void packet_buf_flush(struct strbuf *buf)
64
64
}
65
65
66
66
#define hex (a ) (hexchar[(a) & 15])
67
- static char buffer [1000 ];
68
- static unsigned format_packet (const char * fmt , va_list args )
67
+ static void format_packet (struct strbuf * out , const char * fmt , va_list args )
69
68
{
70
69
static char hexchar [] = "0123456789abcdef" ;
71
- unsigned n ;
70
+ size_t orig_len , n ;
72
71
73
- n = vsnprintf (buffer + 4 , sizeof (buffer ) - 4 , fmt , args );
74
- if (n >= sizeof (buffer )- 4 )
72
+ orig_len = out -> len ;
73
+ strbuf_addstr (out , "0000" );
74
+ strbuf_vaddf (out , fmt , args );
75
+ n = out -> len - orig_len ;
76
+
77
+ if (n > LARGE_PACKET_MAX )
75
78
die ("protocol error: impossibly long line" );
76
- n += 4 ;
77
- buffer [0 ] = hex (n >> 12 );
78
- buffer [1 ] = hex (n >> 8 );
79
- buffer [2 ] = hex (n >> 4 );
80
- buffer [3 ] = hex (n );
81
- packet_trace (buffer + 4 , n - 4 , 1 );
82
- return n ;
79
+
80
+ out -> buf [orig_len + 0 ] = hex (n >> 12 );
81
+ out -> buf [orig_len + 1 ] = hex (n >> 8 );
82
+ out -> buf [orig_len + 2 ] = hex (n >> 4 );
83
+ out -> buf [orig_len + 3 ] = hex (n );
84
+ packet_trace (out -> buf + orig_len + 4 , n - 4 , 1 );
83
85
}
84
86
85
87
void packet_write (int fd , const char * fmt , ...)
86
88
{
89
+ static struct strbuf buf = STRBUF_INIT ;
87
90
va_list args ;
88
- unsigned n ;
89
91
92
+ strbuf_reset (& buf );
90
93
va_start (args , fmt );
91
- n = format_packet (fmt , args );
94
+ format_packet (& buf , fmt , args );
92
95
va_end (args );
93
- write_or_die (fd , buffer , n );
96
+ write_or_die (fd , buf . buf , buf . len );
94
97
}
95
98
96
99
void packet_buf_write (struct strbuf * buf , const char * fmt , ...)
97
100
{
98
101
va_list args ;
99
- unsigned n ;
100
102
101
103
va_start (args , fmt );
102
- n = format_packet (fmt , args );
104
+ format_packet (buf , fmt , args );
103
105
va_end (args );
104
- strbuf_add (buf , buffer , n );
105
106
}
106
107
107
108
static int get_packet_data (int fd , char * * src_buf , size_t * src_size ,
0 commit comments