/* Works in-place since the final string is never longer */ void httpunescape(BYTE * httpstr) { BYTE *in = httpstr; /* input string pointer */ BYTE *out = httpstr; /* output string pointer */ while (in) { switch (*in) { case '+': *out++ = ' '; break; case '\0': *out++ = '\0'; return; case '%': if ( isxdigit(in[1]) && isxdigit(in[2]) ) { ++in; *out++ = string2num((char *) in); ++in; break ; } // fall through default: *out++ = *in; break; } in++; } }
static void GetNextByte( const ASCII ** strpointer, BYTE default_byte, BYTE * sn ) { if ( (*strpointer)[0] == '.' ) { ++*strpointer ; } if ( isxdigit((*strpointer)[0]) && isxdigit((*strpointer)[1]) ) { *sn = string2num(*strpointer) ; *strpointer += 2 ; } else { *sn = default_byte ; } }
/* returns length */ static int hex_convert(char *str) { char *leader = str; BYTE *trailer = (BYTE *) str; while (*leader) { *trailer++ = string2num(leader); leader += 2 ; } return trailer - ((BYTE *) str); }
static void GetDeviceName(const ASCII ** strpointer, struct connection_in * in) { BYTE sn[SERIAL_NUMBER_SIZE] ; BYTE dn[SERIAL_NUMBER_SIZE] ; if ( isxdigit((*strpointer)[0]) && isxdigit((*strpointer)[1]) ) { // family code specified sn[0] = string2num(*strpointer); *strpointer += 2; GetDefaultDeviceName( dn, sn, in ) ; // Choice of default or specified ID GetNextByte(strpointer,dn[1],&sn[1]); GetNextByte(strpointer,dn[2],&sn[2]); GetNextByte(strpointer,dn[3],&sn[3]); GetNextByte(strpointer,dn[4],&sn[4]); GetNextByte(strpointer,dn[5],&sn[5]); GetNextByte(strpointer,dn[6],&sn[6]); } else { const ASCII * name_to_familycode = namefind((*strpointer)) ; if ( name_to_familycode != NULL) { // device name specified (e.g. DS2401) sn[0] = string2num(name_to_familycode); GetDefaultDeviceName( dn, sn, in ) ; sn[1] = dn[1] ; sn[2] = dn[2] ; sn[3] = dn[3] ; sn[4] = dn[4] ; sn[5] = dn[5] ; sn[6] = dn[6] ; } else { // Bad device name LEVEL_DEFAULT("Device %d <%s> not recognized for %s %d -- ignored",DirblobElements(&(in->master.fake.main))+1,*strpointer,in->adapter_name,in->master.fake.index); return ; } } sn[SERIAL_NUMBER_SIZE-1] = CRC8compute(sn, SERIAL_NUMBER_SIZE-1, 0); DirblobAdd(sn, &(in->master.fake.main)); // Ignore bad return }
static bool scan_line(const char* buffer, int bufsize, enum file_state *state, int *file_size, struct timeval *file_time, const char **path) { *state = char_to_state(*buffer); if (*state == -1) { return false; } file_time->tv_sec = 0; file_time->tv_usec = 0; if (*state == DIRECTORY || *state == LINK) { // directory // format is as in printf("D %s", path) *path = buffer + 2; *file_size = 0; return true; } else { // VERSION_1: format is as in printf("%c %d %s", kind, length, path) const char* filename; *file_size = string2num(buffer+2, buffer+bufsize, &filename); if (*file_size < 0) { return false; } // VERSION_2: format is as in printf("%c %d %d %d %s", kind, length, seconds, milliseconds, path) // so the beginning is the same, and two long values are inserted before path if (protocol_version == VERSION_2) { file_time->tv_sec = string2num(filename, buffer+bufsize, &filename); if (file_time->tv_sec < 0) { return false; } file_time->tv_usec = string2num(filename, buffer+bufsize, &filename); if (file_time->tv_sec < 0) { return false; } } *path = filename; return true; } }
static GOOD_OR_BAD LINK_PowerByte(const BYTE data, BYTE * resp, const UINT delay, const struct parsedname *pn) { struct connection_in * in = pn->selected_connection ; ASCII buf[3] = "pxx"; BYTE respond[2+in->CRLF_size] ; num2string(&buf[1], data); RETURN_BAD_IF_BAD(LINK_write(LINK_string(buf), 3, in) ) ; // delay UT_delay(delay); // flush the buffers RETURN_BAD_IF_BAD(LINK_write(LINK_string("\r"), 1, in) ) ; RETURN_BAD_IF_BAD( LINK_readback_data( LINK_string(respond), 2, in) ) ; resp[0] = string2num((const ASCII *) respond); return gbGOOD ; }
static GOOD_OR_BAD LINK_directory(struct device_search *ds, struct connection_in * in) { char resp[DEVICE_LENGTH+COMMA_LENGTH+PLUS_LENGTH+in->CRLF_size]; DirblobClear( &(ds->gulp) ); // Send the configuration command and check response RETURN_BAD_IF_BAD( LINK_search_type( ds, in )) ; // send the first search RETURN_BAD_IF_BAD(LINK_write(LINK_string("f"), 1, in)) ; //One needs to check the first character returned. //If nothing is found, the link will timeout rather then have a quick //return. This happens when looking at the alarm directory and //there are no alarms pending //So we grab the first character and check it. If not an E leave it //in the resp buffer and get the rest of the response from the LINK //device RETURN_BAD_IF_BAD(LINK_read(LINK_string(resp), 1, in)) ; switch (resp[0]) { case 'E': LEVEL_DEBUG("LINK returned E: No devices in alarm"); // pass through case 'N': // remove extra 2 bytes LEVEL_DEBUG("LINK returned E or N: Empty bus"); if (ds->search != _1W_CONDITIONAL_SEARCH_ROM) { in->AnyDevices = anydevices_no; } return gbGOOD ; default: break ; } if ( BAD(LINK_read(LINK_string(&resp[1+in->CRLF_size]), DEVICE_LENGTH+COMMA_LENGTH+PLUS_LENGTH-1-in->CRLF_size, in)) ) { return gbBAD; } // Check if we should start scanning switch (resp[0]) { case '-': case '+': if (ds->search != _1W_CONDITIONAL_SEARCH_ROM) { in->AnyDevices = anydevices_yes; } break; default: LEVEL_DEBUG("LINK_search unrecognized case"); return gbBAD; } /* Join the loop after the first query -- subsequent handled differently */ while ((resp[0] == '+') || (resp[0] == '-')) { BYTE sn[SERIAL_NUMBER_SIZE]; sn[7] = string2num(&resp[2]); sn[6] = string2num(&resp[4]); sn[5] = string2num(&resp[6]); sn[4] = string2num(&resp[8]); sn[3] = string2num(&resp[10]); sn[2] = string2num(&resp[12]); sn[1] = string2num(&resp[14]); sn[0] = string2num(&resp[16]); LEVEL_DEBUG("SN found: " SNformat, SNvar(sn)); // CRC check if (CRC8(sn, SERIAL_NUMBER_SIZE) || (sn[0] == 0x00)) { LEVEL_DEBUG("BAD family or CRC8"); return gbBAD; } DirblobAdd(sn, &(ds->gulp) ); switch (resp[0]) { case '+': // get next element if ( BAD(LINK_write(LINK_string("n"), 1, in))) { return gbBAD; } if ( BAD(LINK_read(LINK_string((resp)), DEVICE_LENGTH+COMMA_LENGTH+PLUS_LENGTH, in)) ) { return gbBAD; } break; case '-': return gbGOOD; default: break; } } return gbGOOD; }
int main (void) { printf("%d\n", string2num("777", 8)); printf("%d\n", string2num("777", 10)); printf("%d\n", isdigit('1')); return 0; }