+map<string, string>
+split_get_request (string url)
+{
+ enum {
+ AWAITING_QUESTION_MARK,
+ KEY,
+ VALUE
+ } state = AWAITING_QUESTION_MARK;
+
+ map<string, string> r;
+ string k;
+ string v;
+ for (size_t i = 0; i < url.length(); ++i) {
+ switch (state) {
+ case AWAITING_QUESTION_MARK:
+ if (url[i] == '?') {
+ state = KEY;
+ }
+ break;
+ case KEY:
+ if (url[i] == '=') {
+ v.clear ();
+ state = VALUE;
+ } else {
+ k += url[i];
+ }
+ break;
+ case VALUE:
+ if (url[i] == '&') {
+ r.insert (make_pair (k, v));
+ k.clear ();
+ state = KEY;
+ } else {
+ v += url[i];
+ }
+ break;
+ }
+ }
+
+ if (state == VALUE) {
+ r.insert (make_pair (k, v));
+ }
+
+ return r;
+}
+
+dcp::Size
+fit_ratio_within (float ratio, dcp::Size full_frame)
+{
+ if (ratio < full_frame.ratio ()) {
+ return dcp::Size (rint (full_frame.height * ratio), full_frame.height);
+ }
+
+ return dcp::Size (full_frame.width, rint (full_frame.width / ratio));
+}
+
+void *
+wrapped_av_malloc (size_t s)
+{
+ void* p = av_malloc (s);
+ if (!p) {
+ throw bad_alloc ();
+ }
+ return p;
+}
+
+string
+entities_to_text (string e)
+{
+ boost::algorithm::replace_all (e, "%3A", ":");
+ boost::algorithm::replace_all (e, "%2F", "/");
+ return e;
+}
+
+int64_t
+divide_with_round (int64_t a, int64_t b)
+{
+ if (a % b >= (b / 2)) {
+ return (a + b - 1) / b;
+ } else {
+ return a / b;
+ }
+}