]>
cygwin.com Git - cygwin-apps/setup.git/blob - KeysSetting.cc
2 * Copyright (c) 2008, Dave Korn.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * This implements the ExtraKeysSetting class, which persists and reads
13 * in (and saves) extra public DSA signing keys for the verification process.
14 * It stores them all in a contiguous memory buffer. Each is one line of
15 * ASCII text terminated by LF. THERE IS NO NUL-TERMINATION HERE, TAKE CARE!
16 * The buffer is sized to the exact size of the content including the terminating
17 * LF of the last entry. There is no zero after it. After reading the file,
18 * any partial last line is truncated.
20 * Written by Dave Korn <dave.korn.cygwin@gmail.com>
25 static const char *cvsid
=
31 #include "UserSettings.h"
32 #include "io_stream.h"
33 #include "KeysSetting.h"
35 ExtraKeysSetting
*ExtraKeysSetting::global
;
37 ExtraKeysSetting::ExtraKeysSetting ():
38 keybuffer (NULL
), bufsize (0), numkeys (0)
41 const char *p
= UserSettings::instance().get ("extrakeys");
44 keybuffer
= strdup (p
);
45 // Calling count_keys gets the count but also sizes the buffer
46 // correctly, discarding any trailing non-LF-terminated data.
47 bufsize
= count_keys ();
51 ExtraKeysSetting::~ExtraKeysSetting ()
54 UserSettings::instance().set ("extrakeys", keybuffer
);
58 ExtraKeysSetting::flush (void)
68 ExtraKeysSetting::realloc (size_t newbufsize
)
70 char *newbuf
= new char[newbufsize
];
73 memcpy (newbuf
, keybuffer
, newbufsize
< bufsize
? newbufsize
: bufsize
);
81 ExtraKeysSetting::count_keys (void)
83 size_t offs
= 0, size
= 0;
85 while (offs
< bufsize
)
86 if (keybuffer
[offs
++] == 0x0a)
95 ExtraKeysSetting::num_keys (void)
101 ExtraKeysSetting::get_key (size_t num
, size_t *size
)
103 if (!numkeys
|| (num
>= numkeys
))
106 const char *ptr
= keybuffer
;
108 while (*ptr
++ != 0x0a);
110 // Count its size if requested.
113 const char *ptr2
= ptr
;
125 ExtraKeysSetting::add_unique_key (const char *key
)
127 size_t osize
= bufsize
;
128 realloc (bufsize
+ strlen (key
) + 1);
129 strcpy (keybuffer
+ osize
, key
);
130 keybuffer
[bufsize
- 1] = 0x0a;
135 ExtraKeysSetting::add_key (const char *key
)
137 /* Only add key if we don't already have it. */
138 const char *ptr
= keybuffer
;
139 size_t remain
= bufsize
;
140 size_t keylen
= strlen (key
);
142 while (remain
>= keylen
)
144 if (memcmp (ptr
, key
, keylen
) == 0)
151 add_unique_key (key
);
This page took 0.042523 seconds and 5 git commands to generate.