Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8d5fa6da authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Remove strtok from adb.

Also fix android::base::Split to behave like Java, Python, and google3.

Change-Id: Ifbffd4e92950a79e7aea5d153c95fe0980648417
parent 06d2128f
Loading
Loading
Loading
Loading
+36 −59
Original line number Original line Diff line number Diff line
@@ -33,6 +33,7 @@
#include <string>
#include <string>


#include <base/stringprintf.h>
#include <base/stringprintf.h>
#include <base/strings.h>


#include "adb_auth.h"
#include "adb_auth.h"
#include "adb_io.h"
#include "adb_io.h"
@@ -381,84 +382,60 @@ static const char* connection_state_name(atransport *t)
}
}
#endif // ADB_HOST
#endif // ADB_HOST


/* qual_overwrite is used to overwrite a qualifier string.  dst is a
// qual_overwrite is used to overwrite a qualifier string.  dst is a
 * pointer to a char pointer.  It is assumed that if *dst is non-NULL, it
// pointer to a char pointer.  It is assumed that if *dst is non-NULL, it
 * was malloc'ed and needs to freed.  *dst will be set to a dup of src.
// was malloc'ed and needs to freed.  *dst will be set to a dup of src.
 */
// TODO: switch to std::string for these atransport fields instead.
static void qual_overwrite(char **dst, const char *src)
static void qual_overwrite(char** dst, const std::string& src) {
{
    if (!dst)
        return;

    free(*dst);
    free(*dst);
    *dst = NULL;
    *dst = strdup(src.c_str());
}


    if (!src || !*src)
void parse_banner(const char* banner, atransport* t) {
        return;
    D("parse_banner: %s\n", banner);


    *dst = strdup(src);
    // The format is something like:
}
    // "device::ro.product.name=x;ro.product.model=y;ro.product.device=z;".
    std::vector<std::string> pieces = android::base::Split(banner, ":");


void parse_banner(char *banner, atransport *t)
    if (pieces.size() > 2) {
{
        const std::string& props = pieces[2];
    static const char *prop_seps = ";";
        for (auto& prop : android::base::Split(props, ";")) {
    static const char key_val_sep = '=';
            // The list of properties was traditionally ;-terminated rather than ;-separated.
    char *cp;
            if (prop.empty()) continue;
    char *type;


    D("parse_banner: %s\n", banner);
            std::vector<std::string> key_value = android::base::Split(prop, "=");
    type = banner;
            if (key_value.size() != 2) continue;
    cp = strchr(type, ':');

    if (cp) {
            const std::string& key = key_value[0];
        *cp++ = 0;
            const std::string& value = key_value[1];
        /* Nothing is done with second field. */
            if (key == "ro.product.name") {
        cp = strchr(cp, ':');
                qual_overwrite(&t->product, value);
        if (cp) {
            } else if (key == "ro.product.model") {
            char *save;
                qual_overwrite(&t->model, value);
            char *key;
            } else if (key == "ro.product.device") {
            key = adb_strtok_r(cp + 1, prop_seps, &save);
                qual_overwrite(&t->device, value);
            while (key) {
            }
                cp = strchr(key, key_val_sep);
        }
                if (cp) {
    }
                    *cp++ = '\0';

                    if (!strcmp(key, "ro.product.name"))
    const std::string& type = pieces[0];
                        qual_overwrite(&t->product, cp);
    if (type == "bootloader") {
                    else if (!strcmp(key, "ro.product.model"))
                        qual_overwrite(&t->model, cp);
                    else if (!strcmp(key, "ro.product.device"))
                        qual_overwrite(&t->device, cp);
                }
                key = adb_strtok_r(NULL, prop_seps, &save);
            }
        }
    }

    if(!strcmp(type, "bootloader")){
        D("setting connection_state to CS_BOOTLOADER\n");
        D("setting connection_state to CS_BOOTLOADER\n");
        t->connection_state = CS_BOOTLOADER;
        t->connection_state = CS_BOOTLOADER;
        update_transports();
        update_transports();
        return;
    } else if (type == "device") {
    }

    if(!strcmp(type, "device")) {
        D("setting connection_state to CS_DEVICE\n");
        D("setting connection_state to CS_DEVICE\n");
        t->connection_state = CS_DEVICE;
        t->connection_state = CS_DEVICE;
        update_transports();
        update_transports();
        return;
    } else if (type == "recovery") {
    }

    if(!strcmp(type, "recovery")) {
        D("setting connection_state to CS_RECOVERY\n");
        D("setting connection_state to CS_RECOVERY\n");
        t->connection_state = CS_RECOVERY;
        t->connection_state = CS_RECOVERY;
        update_transports();
        update_transports();
        return;
    } else if (type == "sideload") {
    }

    if(!strcmp(type, "sideload")) {
        D("setting connection_state to CS_SIDELOAD\n");
        D("setting connection_state to CS_SIDELOAD\n");
        t->connection_state = CS_SIDELOAD;
        t->connection_state = CS_SIDELOAD;
        update_transports();
        update_transports();
        return;
    }
    }


    t->connection_state = CS_HOST;
    t->connection_state = CS_HOST;
@@ -493,7 +470,7 @@ void handle_packet(apacket *p, atransport *t)
            handle_offline(t);
            handle_offline(t);
        }
        }


        parse_banner((char*) p->data, t);
        parse_banner(reinterpret_cast<const char*>(p->data), t);


        if (HOST || !auth_enabled) {
        if (HOST || !auth_enabled) {
            handle_online(t);
            handle_online(t);
+21 −41
Original line number Original line Diff line number Diff line
@@ -43,6 +43,7 @@
#include "mincrypt/rsa.h"
#include "mincrypt/rsa.h"
#undef RSA_verify
#undef RSA_verify


#include <base/strings.h>
#include <cutils/list.h>
#include <cutils/list.h>


#include <openssl/evp.h>
#include <openssl/evp.h>
@@ -172,7 +173,7 @@ static int write_public_keyfile(RSA *private_key, const char *private_key_path)
        return 0;
        return 0;
    }
    }


    outfile = fopen(path, "w");
    outfile = fopen(path, "we");
    if (!outfile) {
    if (!outfile) {
        D("Failed to open '%s'\n", path);
        D("Failed to open '%s'\n", path);
        return 0;
        return 0;
@@ -191,7 +192,7 @@ static int write_public_keyfile(RSA *private_key, const char *private_key_path)
    encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
    encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
#endif
#endif


    encoded = reinterpret_cast<uint8_t*>(malloc(encoded_length));
    encoded = new uint8_t[encoded_length];
    if (encoded == nullptr) {
    if (encoded == nullptr) {
        D("Allocation failure");
        D("Allocation failure");
        goto out;
        goto out;
@@ -212,9 +213,7 @@ static int write_public_keyfile(RSA *private_key, const char *private_key_path)
    if (outfile != NULL) {
    if (outfile != NULL) {
        fclose(outfile);
        fclose(outfile);
    }
    }
    if (encoded != NULL) {
    delete[] encoded;
        free(encoded);
    }
    return ret;
    return ret;
}
}


@@ -240,7 +239,7 @@ static int generate_key(const char *file)


    old_mask = umask(077);
    old_mask = umask(077);


    f = fopen(file, "w");
    f = fopen(file, "we");
    if (!f) {
    if (!f) {
        D("Failed to open '%s'\n", file);
        D("Failed to open '%s'\n", file);
        umask(old_mask);
        umask(old_mask);
@@ -274,30 +273,24 @@ static int read_key(const char *file, struct listnode *list)
{
{
    D("read_key '%s'\n", file);
    D("read_key '%s'\n", file);


    FILE* f = fopen(file, "r");
    FILE* fp = fopen(file, "re");
    if (!f) {
    if (!fp) {
        D("Failed to open '%s'\n", file);
        D("Failed to open '%s': %s\n", file, strerror(errno));
        return 0;
        return 0;
    }
    }


    adb_private_key* key = reinterpret_cast<adb_private_key*>(
    adb_private_key* key = new adb_private_key;
        malloc(sizeof(adb_private_key)));
    if (!key) {
        D("Failed to alloc key\n");
        fclose(f);
        return 0;
    }
    key->rsa = RSA_new();
    key->rsa = RSA_new();


    if (!PEM_read_RSAPrivateKey(f, &key->rsa, NULL, NULL)) {
    if (!PEM_read_RSAPrivateKey(fp, &key->rsa, NULL, NULL)) {
        D("Failed to read key\n");
        D("Failed to read key\n");
        fclose(f);
        fclose(fp);
        RSA_free(key->rsa);
        RSA_free(key->rsa);
        free(key);
        delete key;
        return 0;
        return 0;
    }
    }


    fclose(f);
    fclose(fp);
    list_add_tail(list, &key->node);
    list_add_tail(list, &key->node);
    return 1;
    return 1;
}
}
@@ -362,29 +355,16 @@ static int get_user_key(struct listnode *list)
    return read_key(path, list);
    return read_key(path, list);
}
}


static void get_vendor_keys(struct listnode *list)
static void get_vendor_keys(struct listnode* key_list) {
{
    const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
    const char *adb_keys_path;
    if (adb_keys_path == nullptr) {
    char keys_path[MAX_PAYLOAD];
    char *path;
    char *save;
    struct stat buf;

    adb_keys_path = getenv("ADB_VENDOR_KEYS");
    if (!adb_keys_path)
        return;
        return;
    strncpy(keys_path, adb_keys_path, sizeof(keys_path));
    }

    path = adb_strtok_r(keys_path, ENV_PATH_SEPARATOR_STR, &save);
    while (path) {
        D("Reading: '%s'\n", path);

        if (stat(path, &buf))
            D("Can't read '%s'\n", path);
        else if (!read_key(path, list))
            D("Failed to read '%s'\n", path);


        path = adb_strtok_r(NULL, ENV_PATH_SEPARATOR_STR, &save);
    for (auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
        if (!read_key(path.c_str(), key_list)) {
            D("Failed to read '%s'\n", path.c_str());
        }
    }
    }
}
}


+0 −10
Original line number Original line Diff line number Diff line
@@ -271,8 +271,6 @@ static __inline__ int adb_is_absolute_host_path( const char* path )
    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
}
}


extern char*  adb_strtok_r(char *str, const char *delim, char **saveptr);

#else /* !_WIN32 a.k.a. Unix */
#else /* !_WIN32 a.k.a. Unix */


#include "fdevent.h"
#include "fdevent.h"
@@ -517,19 +515,11 @@ static __inline__ int adb_is_absolute_host_path( const char* path )
    return path[0] == '/';
    return path[0] == '/';
}
}


static __inline__ char*  adb_strtok_r(char *str, const char *delim, char **saveptr)
{
    return strtok_r(str, delim, saveptr);
}

static __inline__ unsigned long adb_thread_id()
static __inline__ unsigned long adb_thread_id()
{
{
    return (unsigned long)pthread_self();
    return (unsigned long)pthread_self();
}
}


#undef   strtok_r
#define  strtok_r  ___xxx_strtok_r

#endif /* !_WIN32 */
#endif /* !_WIN32 */


#endif /* _ADB_SYSDEPS_H */
#endif /* _ADB_SYSDEPS_H */
+0 −79
Original line number Original line Diff line number Diff line
@@ -2151,85 +2151,6 @@ adb_sysdeps_init( void )
    InitializeCriticalSection( &_win32_lock );
    InitializeCriticalSection( &_win32_lock );
}
}


/* Windows doesn't have strtok_r.  Use the one from bionic. */

/*
 * Copyright (c) 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

char *
adb_strtok_r(char *s, const char *delim, char **last)
{
	char *spanp;
	int c, sc;
	char *tok;


	if (s == NULL && (s = *last) == NULL)
		return (NULL);

	/*
	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
	 */
cont:
	c = *s++;
	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
		if (c == sc)
			goto cont;
	}

	if (c == 0) {		/* no non-delimiter characters */
		*last = NULL;
		return (NULL);
	}
	tok = s - 1;

	/*
	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
	 * Note that delim must have one NUL; we stop if we see that, too.
	 */
	for (;;) {
		c = *s++;
		spanp = (char *)delim;
		do {
			if ((sc = *spanp++) == c) {
				if (c == 0)
					s = NULL;
				else
					s[-1] = 0;
				*last = s;
				return (tok);
			}
		} while (sc != 0);
	}
	/* NOTREACHED */
}

/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/*****                                                                *****/
/*****                                                                *****/
+0 −2
Original line number Original line Diff line number Diff line
@@ -27,8 +27,6 @@ namespace base {
//
//
// The string is split at each occurrence of a character in delimiters.
// The string is split at each occurrence of a character in delimiters.
//
//
// Empty splits will be omitted. I.e. Split("a,,b", ",") -> {"a", "b"}
//
// The empty string is not a valid delimiter list.
// The empty string is not a valid delimiter list.
std::vector<std::string> Split(const std::string& s,
std::vector<std::string> Split(const std::string& s,
                               const std::string& delimiters);
                               const std::string& delimiters);
Loading