Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: crypto/openssl_bio_string.cc

Issue 286263006: Add OpenSSL BIO method that writes to a std::string. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: unittest should check BIO_new_string result Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "crypto/openssl_bio_string.h"
6
7 #include <openssl/bio.h>
8
9 namespace {
10
11 int bio_string_write(BIO* bio, const char* data, int len) {
12 reinterpret_cast<std::string*>(bio->ptr)->append(data, len);
13 return len;
14 }
15
16 long bio_string_ctrl(BIO* bio, int cmd, long num, void* ptr) {
17 std::string* str = reinterpret_cast<std::string*>(bio->ptr);
18 switch (cmd) {
19 case BIO_CTRL_RESET:
20 str->clear();
21 return 1;
22 case BIO_C_FILE_SEEK:
23 return -1;
24 case BIO_C_FILE_TELL:
25 return str->size();
26 case BIO_CTRL_FLUSH:
27 return 1;
28 default:
29 return 0;
30 }
31 }
32
33 int bio_string_new(BIO* bio) {
34 bio->ptr = NULL;
35 bio->init = 0;
36 return 1;
37 }
38
39 int bio_string_free(BIO* bio) {
40 // The string is owned by the caller, so there's nothing to do here.
41 return bio != NULL;
42 }
43
44 BIO_METHOD bio_string_methods = {
45 // TODO(mattm): Should add some type number too? (bio.h uses 1-24)
46 BIO_TYPE_SOURCE_SINK,
47 "bio_string",
48 bio_string_write,
49 NULL, /* read */
50 NULL, /* puts */
51 NULL, /* gets */
52 bio_string_ctrl,
53 bio_string_new,
54 bio_string_free,
55 NULL, /* callback_ctrl */
56 };
57
58 } // namespace
59
60 namespace crypto {
Ryan Sleevi 2014/05/19 23:25:44 nit: I tend to like this on between lines 8-9, rat
mattm 2014/05/20 00:22:24 Done.
61
62 BIO* BIO_new_string(std::string* out) {
63 BIO* bio = BIO_new(&bio_string_methods);
64 if (!bio)
65 return bio;
66 bio->ptr = out;
67 bio->init = 1;
68 return bio;
69 }
70
71 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698