WiimoteSync: null_pin.patch

File null_pin.patch, 2.9 kB (added by dsmith, 2 years ago)
  • src/textfile.c

    diff -ur bluez-4.39/src/textfile.c bluez-4.39.cwiid/src/textfile.c
    old new  
    343343        return str; 
    344344} 
    345345 
     346static char *read_key_null(const char *pathname, const char *key, int icase, int *val_len) 
     347{ 
     348        struct stat st; 
     349        char *map, *off, *end, *tmp, *str = NULL; 
     350        off_t size; size_t len; 
     351        int fd, err = 0; 
     352 
     353        fd = open(pathname, O_RDONLY); 
     354        if (fd < 0) 
     355                return NULL; 
     356 
     357        if (flock(fd, LOCK_SH) < 0) { 
     358                err = errno; 
     359                goto close; 
     360        } 
     361 
     362        if (fstat(fd, &st) < 0) { 
     363                err = errno; 
     364                goto unlock; 
     365        } 
     366 
     367        size = st.st_size; 
     368 
     369        map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); 
     370        if (!map || map == MAP_FAILED) { 
     371                err = errno; 
     372                goto unlock; 
     373        } 
     374 
     375        len = strlen(key); 
     376        off = find_key(map, size, key, len, icase); 
     377        if (!off) { 
     378                err = EILSEQ; 
     379                goto unmap; 
     380        } 
     381 
     382        end = memchr(off, '\r', size - (off - map)); 
     383        tmp = memchr(off, '\n', size - (off - map)); 
     384        if (tmp && (!end || (tmp < end))) { 
     385                end = tmp; 
     386        } 
     387        if (!end) { 
     388                err = EILSEQ; 
     389                goto unmap; 
     390        } 
     391 
     392        str = malloc(end - off - len); 
     393        if (!str) { 
     394                err = EILSEQ; 
     395                goto unmap; 
     396        } 
     397 
     398        memset(str, 0, end - off - len); 
     399        strncpy(str, off + len + 1, end - off - len - 1); 
     400        if (val_len) { 
     401                *val_len = end - off - len - 1; 
     402        } 
     403 
     404unmap: 
     405        munmap(map, size); 
     406 
     407unlock: 
     408        flock(fd, LOCK_UN); 
     409 
     410close: 
     411        close(fd); 
     412        errno = err; 
     413 
     414        return str; 
     415} 
     416 
    346417int textfile_put(const char *pathname, const char *key, const char *value) 
    347418{ 
    348419        return write_key(pathname, key, value, 0); 
     
    368439        return read_key(pathname, key, 0); 
    369440} 
    370441 
     442char *textfile_get_null(const char *pathname, const char *key, int *len) 
     443{ 
     444        return read_key_null(pathname, key, 0, len); 
     445} 
     446 
    371447char *textfile_caseget(const char *pathname, const char *key) 
    372448{ 
    373449        return read_key(pathname, key, 1); 
  • src/textfile.h

    diff -ur bluez-4.39/src/textfile.h bluez-4.39.cwiid/src/textfile.h
    old new  
    3434int textfile_del(const char *pathname, const char *key); 
    3535int textfile_casedel(const char *pathname, const char *key); 
    3636char *textfile_get(const char *pathname, const char *key); 
     37char *textfile_get_null(const char *pathname, const char *key, int *len); 
    3738char *textfile_caseget(const char *pathname, const char *key); 
    3839 
    3940int textfile_foreach(const char *pathname, 
  • src/storage.c

    diff -ur bluez-4.39/src/storage.c bluez-4.39.cwiid/src/storage.c
    old new  
    618618        create_filename(filename, PATH_MAX, local, "pincodes"); 
    619619 
    620620        ba2str(peer, addr); 
    621         str = textfile_get(filename, addr); 
     621        str = textfile_get_null(filename, addr, &len); 
    622622        if (!str) 
    623623                return -ENOENT; 
    624624 
    625625        strncpy(pin, str, 16); 
    626         len = strlen(pin); 
    627626 
    628627        free(str); 
    629628