Addr(const char *smac) noexcept { uint8_t macaddr[PARTS_LEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; for (size_t i = 0; i < PARTS_LEN; i++) { macaddr[i] = dehex(*smac++) << 4; macaddr[i] |= dehex(*smac++); smac++; } memcpy(part, macaddr, PARTS_LEN); }
static void transmit(struct atrf_dsc *dsc, const char *msg, int hex, int times) { uint8_t buf[MAX_PSDU]; int len; atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_PLL_ON); /* * 180 us, according to AVR2001 section 4.3. We time out after * nominally 200 us. */ wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 1); /* * We need to copy the message to append the CRC placeholders. */ if (hex) { len = dehex(buf, msg); } else { strcpy((void *) buf, msg); len = strlen(msg); } atrf_buf_write(dsc, buf, len+2); while (run && times--) { /* @@@ should wait for clear channel */ atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TX_START); /* wait up to 10 ms (nominally) */ wait_for_interrupt(dsc, IRQ_TRX_END, IRQ_TRX_END | IRQ_PLL_LOCK, 10); } }
gpr_slice gpr_permissive_percent_decode_slice(gpr_slice slice_in) { const uint8_t *p = GPR_SLICE_START_PTR(slice_in); const uint8_t *in_end = GPR_SLICE_END_PTR(slice_in); size_t out_length = 0; bool any_percent_encoded_stuff = false; while (p != in_end) { if (*p == '%') { if (!valid_hex(p + 1, in_end) || !valid_hex(p + 2, in_end)) { p++; out_length++; } else { p += 3; out_length++; any_percent_encoded_stuff = true; } } else { p++; out_length++; } } if (!any_percent_encoded_stuff) { return gpr_slice_ref(slice_in); } p = GPR_SLICE_START_PTR(slice_in); gpr_slice out = gpr_slice_malloc(out_length); uint8_t *q = GPR_SLICE_START_PTR(out); while (p != in_end) { if (*p == '%') { if (!valid_hex(p + 1, in_end) || !valid_hex(p + 2, in_end)) { *q++ = *p++; } else { *q++ = (uint8_t)(dehex(p[1]) << 4) | (dehex(p[2])); p += 3; } } else { *q++ = *p++; } } GPR_ASSERT(q == GPR_SLICE_END_PTR(out)); return out; }
bool gpr_strict_percent_decode_slice(gpr_slice slice_in, const uint8_t *unreserved_bytes, gpr_slice *slice_out) { const uint8_t *p = GPR_SLICE_START_PTR(slice_in); const uint8_t *in_end = GPR_SLICE_END_PTR(slice_in); size_t out_length = 0; bool any_percent_encoded_stuff = false; while (p != in_end) { if (*p == '%') { if (!valid_hex(++p, in_end)) return false; if (!valid_hex(++p, in_end)) return false; p++; out_length++; any_percent_encoded_stuff = true; } else if (is_unreserved_character(*p, unreserved_bytes)) { p++; out_length++; } else { return false; } } if (!any_percent_encoded_stuff) { *slice_out = gpr_slice_ref(slice_in); return true; } p = GPR_SLICE_START_PTR(slice_in); *slice_out = gpr_slice_malloc(out_length); uint8_t *q = GPR_SLICE_START_PTR(*slice_out); while (p != in_end) { if (*p == '%') { *q++ = (uint8_t)(dehex(p[1]) << 4) | (dehex(p[2])); p += 3; } else { *q++ = *p++; } } GPR_ASSERT(q == GPR_SLICE_END_PTR(*slice_out)); return true; }
static size_t form_argument_decode(const char *in, size_t inlen, char *out, size_t outlen) { const char *ein = in+inlen; size_t written = 0; for(; in < ein; in++) { switch(*in) { case '+': if ( ++written < outlen ) *out++ = ' '; break; case '%': if ( in+2 < ein ) { int h1 = dehex(*(++in)); int h2 = dehex(*(++in)); if ( h1 < 0 || h2 < 0 ) return (size_t)-1; if ( ++written < outlen ) *out++ = h1<<4|h2; } else return (size_t)-1; /* syntax error */ break; default: if ( ++written < outlen ) *out++ = *in; break; } } if ( written < outlen ) *out++ = '\0'; return written; }
/** * Convert an encoding of a set of keypresses into actual keypresses. */ void keypress_from_text(struct keypress *buf, size_t len, const char *str) { size_t cur = 0; byte mods = 0; memset(buf, 0, len * sizeof *buf); #define STORE(buffer, pos, mod, cod) \ { \ int p = (pos); \ keycode_t c = (cod); \ byte m = (mod); \ \ if ((m & KC_MOD_CONTROL) && ENCODE_KTRL(c)) { \ m &= ~KC_MOD_CONTROL; \ c = KTRL(c); \ } \ \ buffer[p].mods = m; \ buffer[p].code = c; \ } /* Analyze the "ascii" string */ while (*str && cur < len) { buf[cur].type = EVT_KBRD; if (*str == '\\') { str++; if (*str == '\0') break; switch (*str) { /* Hex-mode */ case 'x': { if (isxdigit((unsigned char)(*(str + 1))) && isxdigit((unsigned char)(*(str + 2)))) { int v1 = dehex(*++str) * 16; int v2 = dehex(*++str); /* store a nice hex digit */ STORE(buf, cur++, mods, v1 + v2); } else { /* invalids get ignored */ STORE(buf, cur++, mods, '?'); } break; } case 'a': STORE(buf, cur++, mods, '\a'); break; case '\\': STORE(buf, cur++, mods, '\\'); break; case '^': STORE(buf, cur++, mods, '^'); break; case '[': STORE(buf, cur++, mods, '['); break; default: STORE(buf, cur++, mods, *str); break; } mods = 0; /* Skip the final char */ str++; } else if (*str == '[') { /* parse non-ascii keycodes */ char *end; keycode_t kc; if (*str++ == 0) return; end = strchr(str, (unsigned char) ']'); if (!end) return; kc = keycode_find_code(str, (size_t) (end - str)); if (!kc) return; STORE(buf, cur++, mods, kc); mods = 0; str = end + 1; } else if (*str == '{') { /* Specify modifier for next character */ str++; if (*str == '\0' || !strchr(str, (unsigned char) '}')) return; /* analyze modifier chars */ while (*str != '}') { switch (*str) { case '^': mods |= KC_MOD_CONTROL; break; case 'S': mods |= KC_MOD_SHIFT; break; case 'A': mods |= KC_MOD_ALT; break; case 'M': mods |= KC_MOD_META; break; case 'K': mods |= KC_MOD_KEYPAD; break; default: return; } str++; } /* skip ending bracket */ str++; } else if (*str == '^') { mods |= KC_MOD_CONTROL; str++; } else { /* everything else */ STORE(buf, cur++, mods, *str++); mods = 0; } } /* Terminate */ cur = MIN(cur, len); buf[cur].type = EVT_NONE; }